1 //===--- ArgList.h - Argument List Management ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef CLANG_DRIVER_ARGLIST_H_
11 #define CLANG_DRIVER_ARGLIST_H_
13 #include "clang/Driver/OptSpecifier.h"
14 #include "clang/Driver/Util.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
34 /// arg_iterator - Iterates through arguments stored inside an ArgList.
36 /// The current argument.
37 llvm::SmallVectorImpl<Arg*>::const_iterator Current;
39 /// The argument list we are iterating over.
42 /// Optional filters on the arguments which will be match. Most clients
43 /// should never want to iterate over arguments without filters, so we won't
44 /// bother to factor this into two separate iterator implementations.
46 // FIXME: Make efficient; the idea is to provide efficient iteration over
47 // all arguments which match a particular id and then just provide an
48 // iterator combinator which takes multiple iterators which can be
49 // efficiently compared and returns them in order.
50 OptSpecifier Id0, Id1, Id2;
55 typedef Arg * const * value_type;
56 typedef Arg * const & reference;
57 typedef Arg * const * pointer;
58 typedef std::forward_iterator_tag iterator_category;
59 typedef std::ptrdiff_t difference_type;
61 arg_iterator(llvm::SmallVectorImpl<Arg*>::const_iterator it,
62 const ArgList &_Args, OptSpecifier _Id0 = 0U,
63 OptSpecifier _Id1 = 0U, OptSpecifier _Id2 = 0U)
64 : Current(it), Args(_Args), Id0(_Id0), Id1(_Id1), Id2(_Id2) {
68 operator const Arg*() { return *Current; }
69 reference operator*() const { return *Current; }
70 pointer operator->() const { return Current; }
72 arg_iterator &operator++() {
78 arg_iterator operator++(int) {
79 arg_iterator tmp(*this);
84 friend bool operator==(arg_iterator LHS, arg_iterator RHS) {
85 return LHS.Current == RHS.Current;
87 friend bool operator!=(arg_iterator LHS, arg_iterator RHS) {
92 /// ArgList - Ordered collection of driver arguments.
94 /// The ArgList class manages a list of Arg instances as well as
95 /// auxiliary data and convenience methods to allow Tools to quickly
96 /// check for the presence of Arg instances for a particular Option
97 /// and to iterate over groups of arguments.
100 ArgList(const ArgList &); // DO NOT IMPLEMENT
101 void operator=(const ArgList &); // DO NOT IMPLEMENT
104 typedef llvm::SmallVector<Arg*, 16> arglist_type;
105 typedef arglist_type::iterator iterator;
106 typedef arglist_type::const_iterator const_iterator;
107 typedef arglist_type::reverse_iterator reverse_iterator;
108 typedef arglist_type::const_reverse_iterator const_reverse_iterator;
111 /// The internal list of arguments.
123 /// append - Append \arg A to the arg list.
126 arglist_type &getArgs() { return Args; }
127 const arglist_type &getArgs() const { return Args; }
129 unsigned size() const { return Args.size(); }
132 /// @name Arg Iteration
135 iterator begin() { return Args.begin(); }
136 iterator end() { return Args.end(); }
138 reverse_iterator rbegin() { return Args.rbegin(); }
139 reverse_iterator rend() { return Args.rend(); }
141 const_iterator begin() const { return Args.begin(); }
142 const_iterator end() const { return Args.end(); }
144 const_reverse_iterator rbegin() const { return Args.rbegin(); }
145 const_reverse_iterator rend() const { return Args.rend(); }
147 arg_iterator filtered_begin(OptSpecifier Id0 = 0U, OptSpecifier Id1 = 0U,
148 OptSpecifier Id2 = 0U) const {
149 return arg_iterator(Args.begin(), *this, Id0, Id1, Id2);
151 arg_iterator filtered_end() const {
152 return arg_iterator(Args.end(), *this);
159 /// hasArg - Does the arg list contain any option matching \arg Id.
161 /// \arg Claim Whether the argument should be claimed, if it exists.
162 bool hasArgNoClaim(OptSpecifier Id) const {
163 return getLastArgNoClaim(Id) != 0;
165 bool hasArg(OptSpecifier Id) const {
166 return getLastArg(Id) != 0;
168 bool hasArg(OptSpecifier Id0, OptSpecifier Id1) const {
169 return getLastArg(Id0, Id1) != 0;
171 bool hasArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const {
172 return getLastArg(Id0, Id1, Id2) != 0;
175 /// getLastArg - Return the last argument matching \arg Id, or null.
177 /// \arg Claim Whether the argument should be claimed, if it exists.
178 Arg *getLastArgNoClaim(OptSpecifier Id) const;
179 Arg *getLastArg(OptSpecifier Id) const;
180 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1) const;
181 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const;
182 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
183 OptSpecifier Id3) const;
185 /// getArgString - Return the input argument string at \arg Index.
186 virtual const char *getArgString(unsigned Index) const = 0;
188 /// getNumInputArgStrings - Return the number of original argument strings,
189 /// which are guaranteed to be the first strings in the argument string
191 virtual unsigned getNumInputArgStrings() const = 0;
194 /// @name Argument Lookup Utilities
197 /// getLastArgValue - Return the value of the last argument, or a default.
198 llvm::StringRef getLastArgValue(OptSpecifier Id,
199 llvm::StringRef Default = "") const;
201 /// getLastArgValue - Return the value of the last argument as an integer,
202 /// or a default. Emits an error if the argument is given, but non-integral.
203 int getLastArgIntValue(OptSpecifier Id, int Default,
204 Diagnostic &Diags) const;
206 /// getAllArgValues - Get the values of all instances of the given argument
208 std::vector<std::string> getAllArgValues(OptSpecifier Id) const;
211 /// @name Translation Utilities
214 /// hasFlag - Given an option \arg Pos and its negative form \arg
215 /// Neg, return true if the option is present, false if the
216 /// negation is present, and \arg Default if neither option is
217 /// given. If both the option and its negation are present, the
219 bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default=true) const;
221 /// AddLastArg - Render only the last argument match \arg Id0, if
223 void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const;
225 /// AddAllArgs - Render all arguments matching the given ids.
226 void AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
227 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
229 /// AddAllArgValues - Render the argument values of all arguments
230 /// matching the given ids.
231 void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
232 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
234 /// AddAllArgsTranslated - Render all the arguments matching the
235 /// given ids, but forced to separate args and using the provided
236 /// name instead of the first option value.
238 /// \param Joined - If true, render the argument as joined with
239 /// the option specifier.
240 void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
241 const char *Translation,
242 bool Joined = false) const;
244 /// ClaimAllArgs - Claim all arguments which match the given
246 void ClaimAllArgs(OptSpecifier Id0) const;
249 /// @name Arg Synthesis
252 /// MakeArgString - Construct a constant string pointer whose
253 /// lifetime will match that of the ArgList.
254 virtual const char *MakeArgString(llvm::StringRef Str) const = 0;
255 const char *MakeArgString(const char *Str) const {
256 return MakeArgString(llvm::StringRef(Str));
258 const char *MakeArgString(std::string Str) const {
259 return MakeArgString(llvm::StringRef(Str));
261 const char *MakeArgString(const llvm::Twine &Str) const;
263 /// \brief Create an arg string for (\arg LHS + \arg RHS), reusing the
264 /// string at \arg Index if possible.
265 const char *GetOrMakeJoinedArgString(unsigned Index, llvm::StringRef LHS,
266 llvm::StringRef RHS) const;
271 class InputArgList : public ArgList {
273 /// List of argument strings used by the contained Args.
275 /// This is mutable since we treat the ArgList as being the list
276 /// of Args, and allow routines to add new strings (to have a
277 /// convenient place to store the memory) via MakeIndex.
278 mutable ArgStringList ArgStrings;
280 /// Strings for synthesized arguments.
282 /// This is mutable since we treat the ArgList as being the list
283 /// of Args, and allow routines to add new strings (to have a
284 /// convenient place to store the memory) via MakeIndex.
285 mutable std::list<std::string> SynthesizedStrings;
287 /// The number of original input argument strings.
288 unsigned NumInputArgStrings;
291 InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
294 virtual const char *getArgString(unsigned Index) const {
295 return ArgStrings[Index];
298 virtual unsigned getNumInputArgStrings() const {
299 return NumInputArgStrings;
302 /// @name Arg Synthesis
306 /// MakeIndex - Get an index for the given string(s).
307 unsigned MakeIndex(llvm::StringRef String0) const;
308 unsigned MakeIndex(llvm::StringRef String0, llvm::StringRef String1) const;
310 virtual const char *MakeArgString(llvm::StringRef Str) const;
315 /// DerivedArgList - An ordered collection of driver arguments,
316 /// whose storage may be in another argument list.
317 class DerivedArgList : public ArgList {
318 const InputArgList &BaseArgs;
320 /// The list of arguments we synthesized.
321 mutable arglist_type SynthesizedArgs;
324 /// Construct a new derived arg list from \arg BaseArgs.
325 DerivedArgList(const InputArgList &BaseArgs);
328 virtual const char *getArgString(unsigned Index) const {
329 return BaseArgs.getArgString(Index);
332 virtual unsigned getNumInputArgStrings() const {
333 return BaseArgs.getNumInputArgStrings();
336 const InputArgList &getBaseArgs() const {
340 /// @name Arg Synthesis
343 /// AddSynthesizedArg - Add a argument to the list of synthesized arguments
345 void AddSynthesizedArg(Arg *A) {
346 SynthesizedArgs.push_back(A);
349 virtual const char *MakeArgString(llvm::StringRef Str) const;
351 /// AddFlagArg - Construct a new FlagArg for the given option \arg Id and
352 /// append it to the argument list.
353 void AddFlagArg(const Arg *BaseArg, const Option *Opt) {
354 append(MakeFlagArg(BaseArg, Opt));
357 /// AddPositionalArg - Construct a new Positional arg for the given option
358 /// \arg Id, with the provided \arg Value and append it to the argument
360 void AddPositionalArg(const Arg *BaseArg, const Option *Opt,
361 llvm::StringRef Value) {
362 append(MakePositionalArg(BaseArg, Opt, Value));
366 /// AddSeparateArg - Construct a new Positional arg for the given option
367 /// \arg Id, with the provided \arg Value and append it to the argument
369 void AddSeparateArg(const Arg *BaseArg, const Option *Opt,
370 llvm::StringRef Value) {
371 append(MakeSeparateArg(BaseArg, Opt, Value));
375 /// AddJoinedArg - Construct a new Positional arg for the given option \arg
376 /// Id, with the provided \arg Value and append it to the argument list.
377 void AddJoinedArg(const Arg *BaseArg, const Option *Opt,
378 llvm::StringRef Value) {
379 append(MakeJoinedArg(BaseArg, Opt, Value));
383 /// MakeFlagArg - Construct a new FlagArg for the given option
385 Arg *MakeFlagArg(const Arg *BaseArg, const Option *Opt) const;
387 /// MakePositionalArg - Construct a new Positional arg for the
388 /// given option \arg Id, with the provided \arg Value.
389 Arg *MakePositionalArg(const Arg *BaseArg, const Option *Opt,
390 llvm::StringRef Value) const;
392 /// MakeSeparateArg - Construct a new Positional arg for the
393 /// given option \arg Id, with the provided \arg Value.
394 Arg *MakeSeparateArg(const Arg *BaseArg, const Option *Opt,
395 llvm::StringRef Value) const;
397 /// MakeJoinedArg - Construct a new Positional arg for the
398 /// given option \arg Id, with the provided \arg Value.
399 Arg *MakeJoinedArg(const Arg *BaseArg, const Option *Opt,
400 llvm::StringRef Value) const;
405 } // end namespace driver
406 } // end namespace clang