]> granicus.if.org Git - clang/commitdiff
Driver: Add argument translation utilities to ArgList.
authorDaniel Dunbar <daniel@zuster.org>
Wed, 18 Mar 2009 09:29:36 +0000 (09:29 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Wed, 18 Mar 2009 09:29:36 +0000 (09:29 +0000)
 - Support things like telling which -ffoo -fno-foo option won, and
   forwarding all arguments matching a certain set of options to the
   tool.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67189 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Driver/ArgList.h
lib/Driver/ArgList.cpp

index 9e6996bd08e3194d0b693ee8583213076cf579a2..67b08aa943e26c563fd8fb78a53d068ae33b267a 100644 (file)
@@ -112,6 +112,31 @@ namespace driver {
     /// given option \arg Id, with the provided \arg Value.
     Arg *MakeJoinedArg(const Option *Opt, const char *Value) const;
 
+    /// @}
+    /// @name Translation Utilities
+    /// @{
+
+    /// hasFlag - Given an option \arg Pos and its negative form \arg
+    /// Neg, return true if the option is present, false if the
+    /// negation is present, and \arg Default if neither option is
+    /// given. If both the option and its negation are present, the
+    /// last one wins.
+    bool hasFlag(options::ID Pos, options::ID Neg, bool Default) const;
+
+    /// AddLastArg - Render only the last argument match \arg Id0, if
+    /// present.
+    void AddLastArg(ArgStringList &Output, options::ID Id0) const;
+
+    /// AddAllArgs - Render all arguments matching the given ids.
+    void AddAllArgs(ArgStringList &Output, options::ID Id0) const;
+    void AddAllArgs(ArgStringList &Output, options::ID Id0, options::ID Id1) const;
+    void AddAllArgs(ArgStringList &Output, options::ID Id0, options::ID Id1, 
+                    options::ID Id2) const;
+
+    /// AddAllArgValues - Render the argument values of all arguments
+    /// matching the given ids.
+    void AddAllArgValues(ArgStringList &Output, options::ID Id0) const;
+
     /// @}
   };
 } // end namespace driver
index 3c67f8f1e9bdaab60c6f9e2d355605df632a0db5..ee201f28bd145a821f2c3b2756a42ab6b8563fcc 100644 (file)
@@ -58,6 +58,16 @@ Arg *ArgList::getLastArg(options::ID Id0, options::ID Id1, bool Claim) const {
   return Res;
 }
 
+bool ArgList::hasFlag(options::ID Pos, options::ID Neg, bool Default) const {
+  Arg *PosA = getLastArg(Pos);
+  Arg *NegA = getLastArg(Pos);
+  if (PosA && NegA)
+    return NegA->getIndex() < PosA->getIndex();
+  if (PosA) return true;
+  if (NegA) return false;
+  return Default;
+}
+
 unsigned ArgList::MakeIndex(const char *String0) const {
   unsigned Index = ArgStrings.size();
 
@@ -97,3 +107,58 @@ Arg *ArgList::MakeJoinedArg(const Option *Opt, const char *Value) const {
   Joined += Value;
   return new JoinedArg(Opt, MakeIndex(Joined.c_str()));
 }
+
+void ArgList::AddLastArg(ArgStringList &Output, options::ID Id) const {
+  if (Arg *A = getLastArg(Id)) {
+    A->claim();
+    A->render(*this, Output);
+  }
+}
+
+void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0) const {
+  // FIXME: Make fast.
+  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
+    const Arg *A = *it;
+    if (A->getOption().matches(Id0)) {
+      A->claim();
+      A->render(*this, Output);
+    }
+  }
+}
+
+void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0, 
+                         options::ID Id1) const {
+  // FIXME: Make fast.
+  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
+    const Arg *A = *it;
+    if (A->getOption().matches(Id0) || A->getOption().matches(Id1)) {
+      A->claim();
+      A->render(*this, Output);
+    }
+  }
+}
+
+void ArgList::AddAllArgs(ArgStringList &Output, options::ID Id0, 
+                         options::ID Id1, options::ID Id2) const {
+  // FIXME: Make fast.
+  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
+    const Arg *A = *it;
+    if (A->getOption().matches(Id0) || A->getOption().matches(Id1) ||
+        A->getOption().matches(Id2)) {
+      A->claim();
+      A->render(*this, Output);
+    }
+  }
+}
+
+void ArgList::AddAllArgValues(ArgStringList &Output, options::ID Id0) const {
+  // FIXME: Make fast.
+  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
+    const Arg *A = *it;
+    if (A->getOption().matches(Id0)) {
+      A->claim();
+      for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
+        Output.push_back(A->getValue(*this, i));
+    }
+  }
+}