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/Basic/LLVM.h"
14 #include "clang/Driver/OptSpecifier.h"
15 #include "clang/Driver/Option.h"
16 #include "clang/Driver/Util.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
24 class DiagnosticsEngine;
31 /// arg_iterator - Iterates through arguments stored inside an ArgList.
33 /// The current argument.
34 SmallVectorImpl<Arg*>::const_iterator Current;
36 /// The argument list we are iterating over.
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.
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;
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;
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) {
65 operator const Arg*() { return *Current; }
66 reference operator*() const { return *Current; }
67 pointer operator->() const { return Current; }
69 arg_iterator &operator++() {
75 arg_iterator operator++(int) {
76 arg_iterator tmp(*this);
81 friend bool operator==(arg_iterator LHS, arg_iterator RHS) {
82 return LHS.Current == RHS.Current;
84 friend bool operator!=(arg_iterator LHS, arg_iterator RHS) {
89 /// ArgList - Ordered collection of driver arguments.
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.
97 ArgList(const ArgList &) LLVM_DELETED_FUNCTION;
98 void operator=(const ArgList &) LLVM_DELETED_FUNCTION;
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;
108 /// The internal list of arguments.
120 /// append - Append \p A to the arg list.
123 arglist_type &getArgs() { return Args; }
124 const arglist_type &getArgs() const { return Args; }
126 unsigned size() const { return Args.size(); }
129 /// @name Arg Iteration
132 iterator begin() { return Args.begin(); }
133 iterator end() { return Args.end(); }
135 reverse_iterator rbegin() { return Args.rbegin(); }
136 reverse_iterator rend() { return Args.rend(); }
138 const_iterator begin() const { return Args.begin(); }
139 const_iterator end() const { return Args.end(); }
141 const_reverse_iterator rbegin() const { return Args.rbegin(); }
142 const_reverse_iterator rend() const { return Args.rend(); }
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);
148 arg_iterator filtered_end() const {
149 return arg_iterator(Args.end(), *this);
153 /// @name Arg Removal
156 /// eraseArg - Remove any option matching \p Id.
157 void eraseArg(OptSpecifier Id);
163 /// hasArg - Does the arg list contain any option matching \p Id.
165 /// \p Claim Whether the argument should be claimed, if it exists.
166 bool hasArgNoClaim(OptSpecifier Id) const {
167 return getLastArgNoClaim(Id) != 0;
169 bool hasArg(OptSpecifier Id) const {
170 return getLastArg(Id) != 0;
172 bool hasArg(OptSpecifier Id0, OptSpecifier Id1) const {
173 return getLastArg(Id0, Id1) != 0;
175 bool hasArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2) const {
176 return getLastArg(Id0, Id1, Id2) != 0;
179 /// getLastArg - Return the last argument matching \p Id, or null.
181 /// \p 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 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
191 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5) const;
192 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
193 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
194 OptSpecifier Id6) const;
195 Arg *getLastArg(OptSpecifier Id0, OptSpecifier Id1, OptSpecifier Id2,
196 OptSpecifier Id3, OptSpecifier Id4, OptSpecifier Id5,
197 OptSpecifier Id6, OptSpecifier Id7) const;
199 /// getArgString - Return the input argument string at \p Index.
200 virtual const char *getArgString(unsigned Index) const = 0;
202 /// getNumInputArgStrings - Return the number of original argument strings,
203 /// which are guaranteed to be the first strings in the argument string
205 virtual unsigned getNumInputArgStrings() const = 0;
208 /// @name Argument Lookup Utilities
211 /// getLastArgValue - Return the value of the last argument, or a default.
212 StringRef getLastArgValue(OptSpecifier Id,
213 StringRef Default = "") const;
215 /// getLastArgValue - Return the value of the last argument as an integer,
216 /// or a default. If Diags is non-null, emits an error if the argument
217 /// is given, but non-integral.
218 int getLastArgIntValue(OptSpecifier Id, int Default,
219 DiagnosticsEngine *Diags = 0) const;
221 /// getLastArgValue - Return the value of the last argument as an integer,
222 /// or a default. Emits an error if the argument is given, but non-integral.
223 int getLastArgIntValue(OptSpecifier Id, int Default,
224 DiagnosticsEngine &Diags) const {
225 return getLastArgIntValue(Id, Default, &Diags);
228 /// getAllArgValues - Get the values of all instances of the given argument
230 std::vector<std::string> getAllArgValues(OptSpecifier Id) const;
233 /// @name Translation Utilities
236 /// hasFlag - Given an option \p Pos and its negative form \p Neg, return
237 /// true if the option is present, false if the negation is present, and
238 /// \p Default if neither option is given. If both the option and its
239 /// negation are present, the last one wins.
240 bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default = true) const;
242 /// hasFlag - Given an option \p Pos, an alias \p PosAlias and its negative
243 /// form \p Neg, return true if the option or its alias is present, false if
244 /// the negation is present, and \p Default if none of the options are
245 /// given. If multiple options are present, the last one wins.
246 bool hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
247 bool Default = true) const;
249 /// AddLastArg - Render only the last argument match \p Id0, if present.
250 void AddLastArg(ArgStringList &Output, OptSpecifier Id0) const;
251 void AddLastArg(ArgStringList &Output, OptSpecifier Id0,
252 OptSpecifier Id1) const;
254 /// AddAllArgs - Render all arguments matching the given ids.
255 void AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
256 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
258 /// AddAllArgValues - Render the argument values of all arguments
259 /// matching the given ids.
260 void AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
261 OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
263 /// AddAllArgsTranslated - Render all the arguments matching the
264 /// given ids, but forced to separate args and using the provided
265 /// name instead of the first option value.
267 /// \param Joined - If true, render the argument as joined with
268 /// the option specifier.
269 void AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
270 const char *Translation,
271 bool Joined = false) const;
273 /// ClaimAllArgs - Claim all arguments which match the given
275 void ClaimAllArgs(OptSpecifier Id0) const;
277 /// ClaimAllArgs - Claim all arguments.
279 void ClaimAllArgs() const;
282 /// @name Arg Synthesis
285 /// MakeArgString - Construct a constant string pointer whose
286 /// lifetime will match that of the ArgList.
287 virtual const char *MakeArgString(StringRef Str) const = 0;
288 const char *MakeArgString(const char *Str) const {
289 return MakeArgString(StringRef(Str));
291 const char *MakeArgString(std::string Str) const {
292 return MakeArgString(StringRef(Str));
294 const char *MakeArgString(const Twine &Str) const;
296 /// \brief Create an arg string for (\p LHS + \p RHS), reusing the
297 /// string at \p Index if possible.
298 const char *GetOrMakeJoinedArgString(unsigned Index, StringRef LHS,
299 StringRef RHS) const;
306 class InputArgList : public ArgList {
308 /// List of argument strings used by the contained Args.
310 /// This is mutable since we treat the ArgList as being the list
311 /// of Args, and allow routines to add new strings (to have a
312 /// convenient place to store the memory) via MakeIndex.
313 mutable ArgStringList ArgStrings;
315 /// Strings for synthesized arguments.
317 /// This is mutable since we treat the ArgList as being the list
318 /// of Args, and allow routines to add new strings (to have a
319 /// convenient place to store the memory) via MakeIndex.
320 mutable std::list<std::string> SynthesizedStrings;
322 /// The number of original input argument strings.
323 unsigned NumInputArgStrings;
326 InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
329 virtual const char *getArgString(unsigned Index) const {
330 return ArgStrings[Index];
333 virtual unsigned getNumInputArgStrings() const {
334 return NumInputArgStrings;
337 /// @name Arg Synthesis
341 /// MakeIndex - Get an index for the given string(s).
342 unsigned MakeIndex(StringRef String0) const;
343 unsigned MakeIndex(StringRef String0, StringRef String1) const;
345 virtual const char *MakeArgString(StringRef Str) const;
350 /// DerivedArgList - An ordered collection of driver arguments,
351 /// whose storage may be in another argument list.
352 class DerivedArgList : public ArgList {
353 const InputArgList &BaseArgs;
355 /// The list of arguments we synthesized.
356 mutable arglist_type SynthesizedArgs;
359 /// Construct a new derived arg list from \p BaseArgs.
360 DerivedArgList(const InputArgList &BaseArgs);
363 virtual const char *getArgString(unsigned Index) const {
364 return BaseArgs.getArgString(Index);
367 virtual unsigned getNumInputArgStrings() const {
368 return BaseArgs.getNumInputArgStrings();
371 const InputArgList &getBaseArgs() const {
375 /// @name Arg Synthesis
378 /// AddSynthesizedArg - Add a argument to the list of synthesized arguments
380 void AddSynthesizedArg(Arg *A) {
381 SynthesizedArgs.push_back(A);
384 virtual const char *MakeArgString(StringRef Str) const;
386 /// AddFlagArg - Construct a new FlagArg for the given option \p Id and
387 /// append it to the argument list.
388 void AddFlagArg(const Arg *BaseArg, const Option Opt) {
389 append(MakeFlagArg(BaseArg, Opt));
392 /// AddPositionalArg - Construct a new Positional arg for the given option
393 /// \p Id, with the provided \p Value and append it to the argument
395 void AddPositionalArg(const Arg *BaseArg, const Option Opt,
397 append(MakePositionalArg(BaseArg, Opt, Value));
401 /// AddSeparateArg - Construct a new Positional arg for the given option
402 /// \p Id, with the provided \p Value and append it to the argument
404 void AddSeparateArg(const Arg *BaseArg, const Option Opt,
406 append(MakeSeparateArg(BaseArg, Opt, Value));
410 /// AddJoinedArg - Construct a new Positional arg for the given option
411 /// \p Id, with the provided \p Value and append it to the argument list.
412 void AddJoinedArg(const Arg *BaseArg, const Option Opt,
414 append(MakeJoinedArg(BaseArg, Opt, Value));
418 /// MakeFlagArg - Construct a new FlagArg for the given option \p Id.
419 Arg *MakeFlagArg(const Arg *BaseArg, const Option Opt) const;
421 /// MakePositionalArg - Construct a new Positional arg for the
422 /// given option \p Id, with the provided \p Value.
423 Arg *MakePositionalArg(const Arg *BaseArg, const Option Opt,
424 StringRef Value) const;
426 /// MakeSeparateArg - Construct a new Positional arg for the
427 /// given option \p Id, with the provided \p Value.
428 Arg *MakeSeparateArg(const Arg *BaseArg, const Option Opt,
429 StringRef Value) const;
431 /// MakeJoinedArg - Construct a new Positional arg for the
432 /// given option \p Id, with the provided \p Value.
433 Arg *MakeJoinedArg(const Arg *BaseArg, const Option Opt,
434 StringRef Value) const;
439 } // end namespace driver
440 } // end namespace clang