Home | History | Annotate | Download | only in Driver
      1 //===--- ArgList.h - Argument List Management ----------*- 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_ARGLIST_H_
     11 #define CLANG_DRIVER_ARGLIST_H_
     12 
     13 #include "clang/Basic/LLVM.h"
     14 #include "clang/Driver/OptSpecifier.h"
     15 #include "clang/Driver/Util.h"
     16 #include "llvm/ADT/SmallVector.h"
     17 #include "llvm/ADT/StringRef.h"
     18 
     19 #include <list>
     20 #include <string>
     21 #include <vector>
     22 
     23 namespace clang {
     24   class DiagnosticsEngine;
     25 
     26 namespace driver {
     27   class Arg;
     28   class ArgList;
     29   class Option;
     30 
     31   /// arg_iterator - Iterates through arguments stored inside an ArgList.
     32   class arg_iterator {
     33     /// The current argument.
     34     SmallVectorImpl<Arg*>::const_iterator Current;
     35 
     36     /// The argument list we are iterating over.
     37     const ArgList &Args;
     38 
     39     /// Optional filters on the arguments which will be match. Most clients
     40     /// should never want to iterate over arguments without filters, so we won't
     41     /// bother to factor this into two separate iterator implementations.
     42     //
     43     // FIXME: Make efficient; the idea is to provide efficient iteration over
     44     // all arguments which match a particular id and then just provide an
     45     // iterator combinator which takes multiple iterators which can be
     46     // efficiently compared and returns them in order.
     47     OptSpecifier Id0, Id1, Id2;
     48 
     49     void SkipToNextArg();
     50 
     51   public:
     52     typedef Arg * const *                 value_type;
     53     typedef Arg * const &                 reference;
     54     typedef Arg * const *                 pointer;
     55     typedef std::forward_iterator_tag   iterator_category;
     56     typedef std::ptrdiff_t              difference_type;
     57 
     58     arg_iterator(SmallVectorImpl<Arg*>::const_iterator it,
     59                  const ArgList &_Args, OptSpecifier _Id0 = 0U,
     60                  OptSpecifier _Id1 = 0U, OptSpecifier _Id2 = 0U)
     61       : Current(it), Args(_Args), Id0(_Id0), Id1(_Id1), Id2(_Id2) {
     62       SkipToNextArg();
     63     }
     64 
     65     operator const Arg*() { return *Current; }
     66     reference operator*() const { return *Current; }
     67     pointer operator->() const { return Current; }
     68 
     69     arg_iterator &operator++() {
     70       ++Current;
     71       SkipToNextArg();
     72       return *this;
     73     }
     74 
     75     arg_iterator operator++(int) {
     76       arg_iterator tmp(*this);
     77       ++(*this);
     78       return tmp;
     79     }
     80 
     81     friend bool operator==(arg_iterator LHS, arg_iterator RHS) {
     82       return LHS.Current == RHS.Current;
     83     }
     84     friend bool operator!=(arg_iterator LHS, arg_iterator RHS) {
     85       return !(LHS == RHS);
     86     }
     87   };
     88 
     89   /// ArgList - Ordered collection of driver arguments.
     90   ///
     91   /// The ArgList class manages a list of Arg instances as well as
     92   /// auxiliary data and convenience methods to allow Tools to quickly
     93   /// check for the presence of Arg instances for a particular Option
     94   /// and to iterate over groups of arguments.
     95   class ArgList {
     96   private:
     97     ArgList(const ArgList &); // DO NOT IMPLEMENT
     98     void operator=(const ArgList &); // DO NOT IMPLEMENT
     99 
    100   public:
    101     typedef SmallVector<Arg*, 16> arglist_type;
    102     typedef arglist_type::iterator iterator;
    103     typedef arglist_type::const_iterator const_iterator;
    104     typedef arglist_type::reverse_iterator reverse_iterator;
    105     typedef arglist_type::const_reverse_iterator const_reverse_iterator;
    106 
    107   private:
    108     /// The internal list of arguments.
    109     arglist_type Args;
    110 
    111   protected:
    112     ArgList();
    113 
    114   public:
    115     virtual ~ArgList();
    116 
    117     /// @name Arg Access
    118     /// @{
    119 
    120     /// append - Append \arg A to the arg list.
    121     void append(Arg *A);
    122 
    123     arglist_type &getArgs() { return Args; }
    124     const arglist_type &getArgs() const { return Args; }
    125 
    126     unsigned size() const { return Args.size(); }
    127 
    128     /// @}
    129     /// @name Arg Iteration
    130     /// @{
    131 
    132     iterator begin() { return Args.begin(); }
    133     iterator end() { return Args.end(); }
    134 
    135     reverse_iterator rbegin() { return Args.rbegin(); }
    136     reverse_iterator rend() { return Args.rend(); }
    137 
    138     const_iterator begin() const { return Args.begin(); }
    139     const_iterator end() const { return Args.end(); }
    140 
    141     const_reverse_iterator rbegin() const { return Args.rbegin(); }
    142     const_reverse_iterator rend() const { return Args.rend(); }
    143 
    144     arg_iterator filtered_begin(OptSpecifier Id0 = 0U, OptSpecifier Id1 = 0U,
    145                                 OptSpecifier Id2 = 0U) const {
    146       return arg_iterator(Args.begin(), *this, Id0, Id1, Id2);
    147     }
    148     arg_iterator filtered_end() const {
    149       return arg_iterator(Args.end(), *this);
    150     }
    151 
    152     /// @}
    153     /// @name Arg Removal
    154     /// @{
    155 
    156     /// eraseArg - Remove any option matching \arg Id.
    157     void eraseArg(OptSpecifier Id);
    158 
    159     /// @}
    160     /// @name Arg Access
    161     /// @{
    162 
    163     /// hasArg - Does the arg list contain any option matching \arg Id.
    164     ///
    165     /// \arg Claim Whether the argument should be claimed, if it exists.
    166     bool hasArgNoClaim(OptSpecifier Id) const {
    167       return getLastArgNoClaim(Id) != 0;
    168     }
    169     bool hasArg(OptSpecifier Id) const {
    170       return getLastArg(Id) != 0;
    171     }
    172     bool hasArg(OptSpecifier Id0, OptSpecifier Id1) const {
    173       return getLastArg(Id0, Id1) != 0;
    174     }
    175     bool hasArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const {
    176       return getLastArg(Id0, Id1, Id2) != 0;
    177     }
    178 
    179     /// getLastArg - Return the last argument matching \arg Id, or null.
    180     ///
    181     /// \arg Claim Whether the argument should be claimed, if it exists.
    182     Arg *getLastArgNoClaim(OptSpecifier Id) const;
    183     Arg *getLastArg(OptSpecifier Id) const;
    184     Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1) const;
    185     Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const;
    186     Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
    187                     OptSpecifier Id3) const;
    188     Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
    189                     OptSpecifier Id3, OptSpecifier Id4) const;
    190 
    191     /// getArgString - Return the input argument string at \arg Index.
    192     virtual const char *getArgString(unsigned Index) const = 0;
    193 
    194     /// getNumInputArgStrings - Return the number of original argument strings,
    195     /// which are guaranteed to be the first strings in the argument string
    196     /// list.
    197     virtual unsigned getNumInputArgStrings() const = 0;
    198 
    199     /// @}
    200     /// @name Argument Lookup Utilities
    201     /// @{
    202 
    203     /// getLastArgValue - Return the value of the last argument, or a default.
    204     StringRef getLastArgValue(OptSpecifier Id,
    205                                     StringRef Default = "") const;
    206 
    207     /// getLastArgValue - Return the value of the last argument as an integer,
    208     /// or a default. If Diags is non-null, emits an error if the argument
    209     /// is given, but non-integral.
    210     int getLastArgIntValue(OptSpecifier Id, int Default,
    211                            DiagnosticsEngine *Diags = 0) const;
    212 
    213     /// getLastArgValue - Return the value of the last argument as an integer,
    214     /// or a default. Emits an error if the argument is given, but non-integral.
    215     int getLastArgIntValue(OptSpecifier Id, int Default,
    216                            DiagnosticsEngine &Diags) const {
    217       return getLastArgIntValue(Id, Default, &Diags);
    218     }
    219 
    220     /// getAllArgValues - Get the values of all instances of the given argument
    221     /// as strings.
    222     std::vector<std::string> getAllArgValues(OptSpecifier Id) const;
    223 
    224     /// @}
    225     /// @name Translation Utilities
    226     /// @{
    227 
    228     /// hasFlag - Given an option \arg Pos and its negative form \arg
    229     /// Neg, return true if the option is present, false if the
    230     /// negation is present, and \arg Default if neither option is
    231     /// given. If both the option and its negation are present, the
    232     /// last one wins.
    233     bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default=true) const;
    234 
    235     /// AddLastArg - Render only the last argument match \arg Id0, if
    236     /// present.
    237     void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const;
    238 
    239     /// AddAllArgs - Render all arguments matching the given ids.
    240     void AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
    241                     OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
    242 
    243     /// AddAllArgValues - Render the argument values of all arguments
    244     /// matching the given ids.
    245     void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
    246                          OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
    247 
    248     /// AddAllArgsTranslated - Render all the arguments matching the
    249     /// given ids, but forced to separate args and using the provided
    250     /// name instead of the first option value.
    251     ///
    252     /// \param Joined - If true, render the argument as joined with
    253     /// the option specifier.
    254     void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
    255                               const char *Translation,
    256                               bool Joined = false) const;
    257 
    258     /// ClaimAllArgs - Claim all arguments which match the given
    259     /// option id.
    260     void ClaimAllArgs(OptSpecifier Id0) const;
    261 
    262     /// ClaimAllArgs - Claim all arguments.
    263     ///
    264     void ClaimAllArgs() const;
    265 
    266     /// @}
    267     /// @name Arg Synthesis
    268     /// @{
    269 
    270     /// MakeArgString - Construct a constant string pointer whose
    271     /// lifetime will match that of the ArgList.
    272     virtual const char *MakeArgString(StringRef Str) const = 0;
    273     const char *MakeArgString(const char *Str) const {
    274       return MakeArgString(StringRef(Str));
    275     }
    276     const char *MakeArgString(std::string Str) const {
    277       return MakeArgString(StringRef(Str));
    278     }
    279     const char *MakeArgString(const Twine &Str) const;
    280 
    281     /// \brief Create an arg string for (\arg LHS + \arg RHS), reusing the
    282     /// string at \arg Index if possible.
    283     const char *GetOrMakeJoinedArgString(unsigned Index, StringRef LHS,
    284                                          StringRef RHS) const;
    285 
    286     /// @}
    287   };
    288 
    289   class InputArgList : public ArgList  {
    290   private:
    291     /// List of argument strings used by the contained Args.
    292     ///
    293     /// This is mutable since we treat the ArgList as being the list
    294     /// of Args, and allow routines to add new strings (to have a
    295     /// convenient place to store the memory) via MakeIndex.
    296     mutable ArgStringList ArgStrings;
    297 
    298     /// Strings for synthesized arguments.
    299     ///
    300     /// This is mutable since we treat the ArgList as being the list
    301     /// of Args, and allow routines to add new strings (to have a
    302     /// convenient place to store the memory) via MakeIndex.
    303     mutable std::list<std::string> SynthesizedStrings;
    304 
    305     /// The number of original input argument strings.
    306     unsigned NumInputArgStrings;
    307 
    308   public:
    309     InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
    310     ~InputArgList();
    311 
    312     virtual const char *getArgString(unsigned Index) const {
    313       return ArgStrings[Index];
    314     }
    315 
    316     virtual unsigned getNumInputArgStrings() const {
    317       return NumInputArgStrings;
    318     }
    319 
    320     /// @name Arg Synthesis
    321     /// @{
    322 
    323   public:
    324     /// MakeIndex - Get an index for the given string(s).
    325     unsigned MakeIndex(StringRef String0) const;
    326     unsigned MakeIndex(StringRef String0, StringRef String1) const;
    327 
    328     virtual const char *MakeArgString(StringRef Str) const;
    329 
    330     /// @}
    331   };
    332 
    333   /// DerivedArgList - An ordered collection of driver arguments,
    334   /// whose storage may be in another argument list.
    335   class DerivedArgList : public ArgList {
    336     const InputArgList &BaseArgs;
    337 
    338     /// The list of arguments we synthesized.
    339     mutable arglist_type SynthesizedArgs;
    340 
    341   public:
    342     /// Construct a new derived arg list from \arg BaseArgs.
    343     DerivedArgList(const InputArgList &BaseArgs);
    344     ~DerivedArgList();
    345 
    346     virtual const char *getArgString(unsigned Index) const {
    347       return BaseArgs.getArgString(Index);
    348     }
    349 
    350     virtual unsigned getNumInputArgStrings() const {
    351       return BaseArgs.getNumInputArgStrings();
    352     }
    353 
    354     const InputArgList &getBaseArgs() const {
    355       return BaseArgs;
    356     }
    357 
    358     /// @name Arg Synthesis
    359     /// @{
    360 
    361     /// AddSynthesizedArg - Add a argument to the list of synthesized arguments
    362     /// (to be freed).
    363     void AddSynthesizedArg(Arg *A) {
    364       SynthesizedArgs.push_back(A);
    365     }
    366 
    367     virtual const char *MakeArgString(StringRef Str) const;
    368 
    369     /// AddFlagArg - Construct a new FlagArg for the given option \arg Id and
    370     /// append it to the argument list.
    371     void AddFlagArg(const Arg *BaseArg, const Option *Opt) {
    372       append(MakeFlagArg(BaseArg, Opt));
    373     }
    374 
    375     /// AddPositionalArg - Construct a new Positional arg for the given option
    376     /// \arg Id, with the provided \arg Value and append it to the argument
    377     /// list.
    378     void AddPositionalArg(const Arg *BaseArg, const Option *Opt,
    379                           StringRef Value) {
    380       append(MakePositionalArg(BaseArg, Opt, Value));
    381     }
    382 
    383 
    384     /// AddSeparateArg - Construct a new Positional arg for the given option
    385     /// \arg Id, with the provided \arg Value and append it to the argument
    386     /// list.
    387     void AddSeparateArg(const Arg *BaseArg, const Option *Opt,
    388                         StringRef Value) {
    389       append(MakeSeparateArg(BaseArg, Opt, Value));
    390     }
    391 
    392 
    393     /// AddJoinedArg - Construct a new Positional arg for the given option \arg
    394     /// Id, with the provided \arg Value and append it to the argument list.
    395     void AddJoinedArg(const Arg *BaseArg, const Option *Opt,
    396                       StringRef Value) {
    397       append(MakeJoinedArg(BaseArg, Opt, Value));
    398     }
    399 
    400 
    401     /// MakeFlagArg - Construct a new FlagArg for the given option
    402     /// \arg Id.
    403     Arg *MakeFlagArg(const Arg *BaseArg, const Option *Opt) const;
    404 
    405     /// MakePositionalArg - Construct a new Positional arg for the
    406     /// given option \arg Id, with the provided \arg Value.
    407     Arg *MakePositionalArg(const Arg *BaseArg, const Option *Opt,
    408                            StringRef Value) const;
    409 
    410     /// MakeSeparateArg - Construct a new Positional arg for the
    411     /// given option \arg Id, with the provided \arg Value.
    412     Arg *MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
    413                          StringRef Value) const;
    414 
    415     /// MakeJoinedArg - Construct a new Positional arg for the
    416     /// given option \arg Id, with the provided \arg Value.
    417     Arg *MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
    418                        StringRef Value) const;
    419 
    420     /// @}
    421   };
    422 
    423 } // end namespace driver
    424 } // end namespace clang
    425 
    426 #endif
    427