Home | History | Annotate | Download | only in Driver
      1 //===--- Arg.h - Parsed Argument Classes ------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef CLANG_DRIVER_ARG_H_
     11 #define CLANG_DRIVER_ARG_H_
     12 
     13 #include "Util.h"
     14 #include "llvm/ADT/SmallVector.h"
     15 #include "llvm/ADT/StringRef.h"
     16 #include <string>
     17 
     18 namespace clang {
     19 namespace driver {
     20   class ArgList;
     21   class Option;
     22 
     23   /// Arg - A concrete instance of a particular driver option.
     24   ///
     25   /// The Arg class encodes just enough information to be able to
     26   /// derive the argument values efficiently. In addition, Arg
     27   /// instances have an intrusive double linked list which is used by
     28   /// ArgList to provide efficient iteration over all instances of a
     29   /// particular option.
     30   class Arg {
     31     Arg(const Arg &); // DO NOT IMPLEMENT
     32     void operator=(const Arg &); // DO NOT IMPLEMENT
     33 
     34   private:
     35     /// The option this argument is an instance of.
     36     const Option *Opt;
     37 
     38     /// The argument this argument was derived from (during tool chain
     39     /// argument translation), if any.
     40     const Arg *BaseArg;
     41 
     42     /// The index at which this argument appears in the containing
     43     /// ArgList.
     44     unsigned Index;
     45 
     46     /// Was this argument used to effect compilation; used for generating
     47     /// "argument unused" diagnostics.
     48     mutable unsigned Claimed : 1;
     49 
     50     /// Does this argument own its values.
     51     mutable unsigned OwnsValues : 1;
     52 
     53     /// The argument values, as C strings.
     54     SmallVector<const char *, 2> Values;
     55 
     56   public:
     57     Arg(const Option *Opt, unsigned Index, const Arg *BaseArg = 0);
     58     Arg(const Option *Opt, unsigned Index,
     59         const char *Value0, const Arg *BaseArg = 0);
     60     Arg(const Option *Opt, unsigned Index,
     61         const char *Value0, const char *Value1, const Arg *BaseArg = 0);
     62     ~Arg();
     63 
     64     const Option &getOption() const { return *Opt; }
     65     unsigned getIndex() const { return Index; }
     66 
     67     /// getBaseArg - Return the base argument which generated this
     68     /// arg; this is either the argument itself or the argument it was
     69     /// derived from during tool chain specific argument translation.
     70     const Arg &getBaseArg() const {
     71       return BaseArg ? *BaseArg : *this;
     72     }
     73     void setBaseArg(const Arg *_BaseArg) {
     74       BaseArg = _BaseArg;
     75     }
     76 
     77     bool getOwnsValues() const { return OwnsValues; }
     78     void setOwnsValues(bool Value) const { OwnsValues = Value; }
     79 
     80     bool isClaimed() const { return getBaseArg().Claimed; }
     81 
     82     /// claim - Set the Arg claimed bit.
     83     void claim() const { getBaseArg().Claimed = true; }
     84 
     85     unsigned getNumValues() const { return Values.size(); }
     86     const char *getValue(const ArgList &Args, unsigned N=0) const {
     87       return Values[N];
     88     }
     89 
     90     SmallVectorImpl<const char*> &getValues() {
     91       return Values;
     92     }
     93 
     94     bool containsValue(StringRef Value) const {
     95       for (unsigned i = 0, e = getNumValues(); i != e; ++i)
     96         if (Values[i] == Value)
     97           return true;
     98       return false;
     99     }
    100 
    101     /// render - Append the argument onto the given array as strings.
    102     void render(const ArgList &Args, ArgStringList &Output) const;
    103 
    104     /// renderAsInput - Append the argument, render as an input, onto
    105     /// the given array as strings. The distinction is that some
    106     /// options only render their values when rendered as a input
    107     /// (e.g., Xlinker).
    108     void renderAsInput(const ArgList &Args, ArgStringList &Output) const;
    109 
    110     static bool classof(const Arg *) { return true; }
    111 
    112     void dump() const;
    113 
    114     /// getAsString - Return a formatted version of the argument and
    115     /// its values, for debugging and diagnostics.
    116     std::string getAsString(const ArgList &Args) const;
    117   };
    118 
    119 } // end namespace driver
    120 } // end namespace clang
    121 
    122 #endif
    123