clang API Documentation
00001 //===--- ArgList.h - Argument List Management ----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #ifndef CLANG_DRIVER_ARGLIST_H_ 00011 #define CLANG_DRIVER_ARGLIST_H_ 00012 00013 #include "clang/Basic/LLVM.h" 00014 #include "clang/Driver/OptSpecifier.h" 00015 #include "clang/Driver/Util.h" 00016 #include "llvm/ADT/SmallVector.h" 00017 #include "llvm/ADT/StringRef.h" 00018 00019 #include <list> 00020 #include <string> 00021 #include <vector> 00022 00023 namespace clang { 00024 class DiagnosticsEngine; 00025 00026 namespace driver { 00027 class Arg; 00028 class ArgList; 00029 class Option; 00030 00031 /// arg_iterator - Iterates through arguments stored inside an ArgList. 00032 class arg_iterator { 00033 /// The current argument. 00034 SmallVectorImpl<Arg*>::const_iterator Current; 00035 00036 /// The argument list we are iterating over. 00037 const ArgList &Args; 00038 00039 /// Optional filters on the arguments which will be match. Most clients 00040 /// should never want to iterate over arguments without filters, so we won't 00041 /// bother to factor this into two separate iterator implementations. 00042 // 00043 // FIXME: Make efficient; the idea is to provide efficient iteration over 00044 // all arguments which match a particular id and then just provide an 00045 // iterator combinator which takes multiple iterators which can be 00046 // efficiently compared and returns them in order. 00047 OptSpecifier Id0, Id1, Id2; 00048 00049 void SkipToNextArg(); 00050 00051 public: 00052 typedef Arg * const * value_type; 00053 typedef Arg * const & reference; 00054 typedef Arg * const * pointer; 00055 typedef std::forward_iterator_tag iterator_category; 00056 typedef std::ptrdiff_t difference_type; 00057 00058 arg_iterator(SmallVectorImpl<Arg*>::const_iterator it, 00059 const ArgList &_Args, OptSpecifier _Id0 = 0U, 00060 OptSpecifier _Id1 = 0U, OptSpecifier _Id2 = 0U) 00061 : Current(it), Args(_Args), Id0(_Id0), Id1(_Id1), Id2(_Id2) { 00062 SkipToNextArg(); 00063 } 00064 00065 operator const Arg*() { return *Current; } 00066 reference operator*() const { return *Current; } 00067 pointer operator->() const { return Current; } 00068 00069 arg_iterator &operator++() { 00070 ++Current; 00071 SkipToNextArg(); 00072 return *this; 00073 } 00074 00075 arg_iterator operator++(int) { 00076 arg_iterator tmp(*this); 00077 ++(*this); 00078 return tmp; 00079 } 00080 00081 friend bool operator==(arg_iterator LHS, arg_iterator RHS) { 00082 return LHS.Current == RHS.Current; 00083 } 00084 friend bool operator!=(arg_iterator LHS, arg_iterator RHS) { 00085 return !(LHS == RHS); 00086 } 00087 }; 00088 00089 /// ArgList - Ordered collection of driver arguments. 00090 /// 00091 /// The ArgList class manages a list of Arg instances as well as 00092 /// auxiliary data and convenience methods to allow Tools to quickly 00093 /// check for the presence of Arg instances for a particular Option 00094 /// and to iterate over groups of arguments. 00095 class ArgList { 00096 private: 00097 ArgList(const ArgList &); // DO NOT IMPLEMENT 00098 void operator=(const ArgList &); // DO NOT IMPLEMENT 00099 00100 public: 00101 typedef SmallVector<Arg*, 16> arglist_type; 00102 typedef arglist_type::iterator iterator; 00103 typedef arglist_type::const_iterator const_iterator; 00104 typedef arglist_type::reverse_iterator reverse_iterator; 00105 typedef arglist_type::const_reverse_iterator const_reverse_iterator; 00106 00107 private: 00108 /// The internal list of arguments. 00109 arglist_type Args; 00110 00111 protected: 00112 ArgList(); 00113 00114 public: 00115 virtual ~ArgList(); 00116 00117 /// @name Arg Access 00118 /// @{ 00119 00120 /// append - Append \arg A to the arg list. 00121 void append(Arg *A); 00122 00123 arglist_type &getArgs() { return Args; } 00124 const arglist_type &getArgs() const { return Args; } 00125 00126 unsigned size() const { return Args.size(); } 00127 00128 /// @} 00129 /// @name Arg Iteration 00130 /// @{ 00131 00132 iterator begin() { return Args.begin(); } 00133 iterator end() { return Args.end(); } 00134 00135 reverse_iterator rbegin() { return Args.rbegin(); } 00136 reverse_iterator rend() { return Args.rend(); } 00137 00138 const_iterator begin() const { return Args.begin(); } 00139 const_iterator end() const { return Args.end(); } 00140 00141 const_reverse_iterator rbegin() const { return Args.rbegin(); } 00142 const_reverse_iterator rend() const { return Args.rend(); } 00143 00144 arg_iterator filtered_begin(OptSpecifier Id0 = 0U, OptSpecifier Id1 = 0U, 00145 OptSpecifier Id2 = 0U) const { 00146 return arg_iterator(Args.begin(), *this, Id0, Id1, Id2); 00147 } 00148 arg_iterator filtered_end() const { 00149 return arg_iterator(Args.end(), *this); 00150 } 00151 00152 /// @} 00153 /// @name Arg Removal 00154 /// @{ 00155 00156 /// eraseArg - Remove any option matching \arg Id. 00157 void eraseArg(OptSpecifier Id); 00158 00159 /// @} 00160 /// @name Arg Access 00161 /// @{ 00162 00163 /// hasArg - Does the arg list contain any option matching \arg Id. 00164 /// 00165 /// \arg Claim Whether the argument should be claimed, if it exists. 00166 bool hasArgNoClaim(OptSpecifier Id) const { 00167 return getLastArgNoClaim(Id) != 0; 00168 } 00169 bool hasArg(OptSpecifier Id) const { 00170 return getLastArg(Id) != 0; 00171 } 00172 bool hasArg(OptSpecifier Id0, OptSpecifier Id1) const { 00173 return getLastArg(Id0, Id1) != 0; 00174 } 00175 bool hasArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const { 00176 return getLastArg(Id0, Id1, Id2) != 0; 00177 } 00178 00179 /// getLastArg - Return the last argument matching \arg Id, or null. 00180 /// 00181 /// \arg Claim Whether the argument should be claimed, if it exists. 00182 Arg *getLastArgNoClaim(OptSpecifier Id) const; 00183 Arg *getLastArg(OptSpecifier Id) const; 00184 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1) const; 00185 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const; 00186 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2, 00187 OptSpecifier Id3) const; 00188 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2, 00189 OptSpecifier Id3, OptSpecifier Id4) const; 00190 00191 /// getArgString - Return the input argument string at \arg Index. 00192 virtual const char *getArgString(unsigned Index) const = 0; 00193 00194 /// getNumInputArgStrings - Return the number of original argument strings, 00195 /// which are guaranteed to be the first strings in the argument string 00196 /// list. 00197 virtual unsigned getNumInputArgStrings() const = 0; 00198 00199 /// @} 00200 /// @name Argument Lookup Utilities 00201 /// @{ 00202 00203 /// getLastArgValue - Return the value of the last argument, or a default. 00204 StringRef getLastArgValue(OptSpecifier Id, 00205 StringRef Default = "") const; 00206 00207 /// getLastArgValue - Return the value of the last argument as an integer, 00208 /// or a default. If Diags is non-null, emits an error if the argument 00209 /// is given, but non-integral. 00210 int getLastArgIntValue(OptSpecifier Id, int Default, 00211 DiagnosticsEngine *Diags = 0) const; 00212 00213 /// getLastArgValue - Return the value of the last argument as an integer, 00214 /// or a default. Emits an error if the argument is given, but non-integral. 00215 int getLastArgIntValue(OptSpecifier Id, int Default, 00216 DiagnosticsEngine &Diags) const { 00217 return getLastArgIntValue(Id, Default, &Diags); 00218 } 00219 00220 /// getAllArgValues - Get the values of all instances of the given argument 00221 /// as strings. 00222 std::vector<std::string> getAllArgValues(OptSpecifier Id) const; 00223 00224 /// @} 00225 /// @name Translation Utilities 00226 /// @{ 00227 00228 /// hasFlag - Given an option \arg Pos and its negative form \arg 00229 /// Neg, return true if the option is present, false if the 00230 /// negation is present, and \arg Default if neither option is 00231 /// given. If both the option and its negation are present, the 00232 /// last one wins. 00233 bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default=true) const; 00234 00235 /// AddLastArg - Render only the last argument match \arg Id0, if 00236 /// present. 00237 void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const; 00238 00239 /// AddAllArgs - Render all arguments matching the given ids. 00240 void AddAllArgs(ArgStringList &Output, OptSpecifier Id0, 00241 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const; 00242 00243 /// AddAllArgValues - Render the argument values of all arguments 00244 /// matching the given ids. 00245 void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0, 00246 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const; 00247 00248 /// AddAllArgsTranslated - Render all the arguments matching the 00249 /// given ids, but forced to separate args and using the provided 00250 /// name instead of the first option value. 00251 /// 00252 /// \param Joined - If true, render the argument as joined with 00253 /// the option specifier. 00254 void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0, 00255 const char *Translation, 00256 bool Joined = false) const; 00257 00258 /// ClaimAllArgs - Claim all arguments which match the given 00259 /// option id. 00260 void ClaimAllArgs(OptSpecifier Id0) const; 00261 00262 /// ClaimAllArgs - Claim all arguments. 00263 /// 00264 void ClaimAllArgs() const; 00265 00266 /// @} 00267 /// @name Arg Synthesis 00268 /// @{ 00269 00270 /// MakeArgString - Construct a constant string pointer whose 00271 /// lifetime will match that of the ArgList. 00272 virtual const char *MakeArgString(StringRef Str) const = 0; 00273 const char *MakeArgString(const char *Str) const { 00274 return MakeArgString(StringRef(Str)); 00275 } 00276 const char *MakeArgString(std::string Str) const { 00277 return MakeArgString(StringRef(Str)); 00278 } 00279 const char *MakeArgString(const Twine &Str) const; 00280 00281 /// \brief Create an arg string for (\arg LHS + \arg RHS), reusing the 00282 /// string at \arg Index if possible. 00283 const char *GetOrMakeJoinedArgString(unsigned Index, StringRef LHS, 00284 StringRef RHS) const; 00285 00286 /// @} 00287 }; 00288 00289 class InputArgList : public ArgList { 00290 private: 00291 /// List of argument strings used by the contained Args. 00292 /// 00293 /// This is mutable since we treat the ArgList as being the list 00294 /// of Args, and allow routines to add new strings (to have a 00295 /// convenient place to store the memory) via MakeIndex. 00296 mutable ArgStringList ArgStrings; 00297 00298 /// Strings for synthesized arguments. 00299 /// 00300 /// This is mutable since we treat the ArgList as being the list 00301 /// of Args, and allow routines to add new strings (to have a 00302 /// convenient place to store the memory) via MakeIndex. 00303 mutable std::list<std::string> SynthesizedStrings; 00304 00305 /// The number of original input argument strings. 00306 unsigned NumInputArgStrings; 00307 00308 public: 00309 InputArgList(const char* const *ArgBegin, const char* const *ArgEnd); 00310 ~InputArgList(); 00311 00312 virtual const char *getArgString(unsigned Index) const { 00313 return ArgStrings[Index]; 00314 } 00315 00316 virtual unsigned getNumInputArgStrings() const { 00317 return NumInputArgStrings; 00318 } 00319 00320 /// @name Arg Synthesis 00321 /// @{ 00322 00323 public: 00324 /// MakeIndex - Get an index for the given string(s). 00325 unsigned MakeIndex(StringRef String0) const; 00326 unsigned MakeIndex(StringRef String0, StringRef String1) const; 00327 00328 virtual const char *MakeArgString(StringRef Str) const; 00329 00330 /// @} 00331 }; 00332 00333 /// DerivedArgList - An ordered collection of driver arguments, 00334 /// whose storage may be in another argument list. 00335 class DerivedArgList : public ArgList { 00336 const InputArgList &BaseArgs; 00337 00338 /// The list of arguments we synthesized. 00339 mutable arglist_type SynthesizedArgs; 00340 00341 public: 00342 /// Construct a new derived arg list from \arg BaseArgs. 00343 DerivedArgList(const InputArgList &BaseArgs); 00344 ~DerivedArgList(); 00345 00346 virtual const char *getArgString(unsigned Index) const { 00347 return BaseArgs.getArgString(Index); 00348 } 00349 00350 virtual unsigned getNumInputArgStrings() const { 00351 return BaseArgs.getNumInputArgStrings(); 00352 } 00353 00354 const InputArgList &getBaseArgs() const { 00355 return BaseArgs; 00356 } 00357 00358 /// @name Arg Synthesis 00359 /// @{ 00360 00361 /// AddSynthesizedArg - Add a argument to the list of synthesized arguments 00362 /// (to be freed). 00363 void AddSynthesizedArg(Arg *A) { 00364 SynthesizedArgs.push_back(A); 00365 } 00366 00367 virtual const char *MakeArgString(StringRef Str) const; 00368 00369 /// AddFlagArg - Construct a new FlagArg for the given option \arg Id and 00370 /// append it to the argument list. 00371 void AddFlagArg(const Arg *BaseArg, const Option *Opt) { 00372 append(MakeFlagArg(BaseArg, Opt)); 00373 } 00374 00375 /// AddPositionalArg - Construct a new Positional arg for the given option 00376 /// \arg Id, with the provided \arg Value and append it to the argument 00377 /// list. 00378 void AddPositionalArg(const Arg *BaseArg, const Option *Opt, 00379 StringRef Value) { 00380 append(MakePositionalArg(BaseArg, Opt, Value)); 00381 } 00382 00383 00384 /// AddSeparateArg - Construct a new Positional arg for the given option 00385 /// \arg Id, with the provided \arg Value and append it to the argument 00386 /// list. 00387 void AddSeparateArg(const Arg *BaseArg, const Option *Opt, 00388 StringRef Value) { 00389 append(MakeSeparateArg(BaseArg, Opt, Value)); 00390 } 00391 00392 00393 /// AddJoinedArg - Construct a new Positional arg for the given option \arg 00394 /// Id, with the provided \arg Value and append it to the argument list. 00395 void AddJoinedArg(const Arg *BaseArg, const Option *Opt, 00396 StringRef Value) { 00397 append(MakeJoinedArg(BaseArg, Opt, Value)); 00398 } 00399 00400 00401 /// MakeFlagArg - Construct a new FlagArg for the given option 00402 /// \arg Id. 00403 Arg *MakeFlagArg(const Arg *BaseArg, const Option *Opt) const; 00404 00405 /// MakePositionalArg - Construct a new Positional arg for the 00406 /// given option \arg Id, with the provided \arg Value. 00407 Arg *MakePositionalArg(const Arg *BaseArg, const Option *Opt, 00408 StringRef Value) const; 00409 00410 /// MakeSeparateArg - Construct a new Positional arg for the 00411 /// given option \arg Id, with the provided \arg Value. 00412 Arg *MakeSeparateArg(const Arg *BaseArg, const Option *Opt, 00413 StringRef Value) const; 00414 00415 /// MakeJoinedArg - Construct a new Positional arg for the 00416 /// given option \arg Id, with the provided \arg Value. 00417 Arg *MakeJoinedArg(const Arg *BaseArg, const Option *Opt, 00418 StringRef Value) const; 00419 00420 /// @} 00421 }; 00422 00423 } // end namespace driver 00424 } // end namespace clang 00425 00426 #endif