]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/tools/clang/include/clang/Driver/ArgList.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / tools / clang / include / clang / Driver / ArgList.h
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/Option.h"
16 #include "clang/Driver/Util.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
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 &) LLVM_DELETED_FUNCTION;
98     void operator=(const ArgList &) LLVM_DELETED_FUNCTION;
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 \p 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 \p Id.
157     void eraseArg(OptSpecifier Id);
158
159     /// @}
160     /// @name Arg Access
161     /// @{
162
163     /// hasArg - Does the arg list contain any option matching \p Id.
164     ///
165     /// \p 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 \p Id, or null.
180     ///
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;
198
199     /// getArgString - Return the input argument string at \p Index.
200     virtual const char *getArgString(unsigned Index) const = 0;
201
202     /// getNumInputArgStrings - Return the number of original argument strings,
203     /// which are guaranteed to be the first strings in the argument string
204     /// list.
205     virtual unsigned getNumInputArgStrings() const = 0;
206
207     /// @}
208     /// @name Argument Lookup Utilities
209     /// @{
210
211     /// getLastArgValue - Return the value of the last argument, or a default.
212     StringRef getLastArgValue(OptSpecifier Id,
213                                     StringRef Default = "") const;
214
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;
220
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);
226     }
227
228     /// getAllArgValues - Get the values of all instances of the given argument
229     /// as strings.
230     std::vector<std::string> getAllArgValues(OptSpecifier Id) const;
231
232     /// @}
233     /// @name Translation Utilities
234     /// @{
235
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;
241
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;
248
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;
253
254     /// AddAllArgs - Render all arguments matching the given ids.
255     void AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
256                     OptSpecifier Id1 = 0U, OptSpecifier Id2 = 0U) const;
257
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;
262
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.
266     ///
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;
272
273     /// ClaimAllArgs - Claim all arguments which match the given
274     /// option id.
275     void ClaimAllArgs(OptSpecifier Id0) const;
276
277     /// ClaimAllArgs - Claim all arguments.
278     ///
279     void ClaimAllArgs() const;
280
281     /// @}
282     /// @name Arg Synthesis
283     /// @{
284
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));
290     }
291     const char *MakeArgString(std::string Str) const {
292       return MakeArgString(StringRef(Str));
293     }
294     const char *MakeArgString(const Twine &Str) const;
295
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;
300
301     /// @}
302
303     void dump();
304   };
305
306   class InputArgList : public ArgList  {
307   private:
308     /// List of argument strings used by the contained Args.
309     ///
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;
314
315     /// Strings for synthesized arguments.
316     ///
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;
321
322     /// The number of original input argument strings.
323     unsigned NumInputArgStrings;
324
325   public:
326     InputArgList(const char* const *ArgBegin, const char* const *ArgEnd);
327     ~InputArgList();
328
329     virtual const char *getArgString(unsigned Index) const {
330       return ArgStrings[Index];
331     }
332
333     virtual unsigned getNumInputArgStrings() const {
334       return NumInputArgStrings;
335     }
336
337     /// @name Arg Synthesis
338     /// @{
339
340   public:
341     /// MakeIndex - Get an index for the given string(s).
342     unsigned MakeIndex(StringRef String0) const;
343     unsigned MakeIndex(StringRef String0, StringRef String1) const;
344
345     virtual const char *MakeArgString(StringRef Str) const;
346
347     /// @}
348   };
349
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;
354
355     /// The list of arguments we synthesized.
356     mutable arglist_type SynthesizedArgs;
357
358   public:
359     /// Construct a new derived arg list from \p BaseArgs.
360     DerivedArgList(const InputArgList &BaseArgs);
361     ~DerivedArgList();
362
363     virtual const char *getArgString(unsigned Index) const {
364       return BaseArgs.getArgString(Index);
365     }
366
367     virtual unsigned getNumInputArgStrings() const {
368       return BaseArgs.getNumInputArgStrings();
369     }
370
371     const InputArgList &getBaseArgs() const {
372       return BaseArgs;
373     }
374
375     /// @name Arg Synthesis
376     /// @{
377
378     /// AddSynthesizedArg - Add a argument to the list of synthesized arguments
379     /// (to be freed).
380     void AddSynthesizedArg(Arg *A) {
381       SynthesizedArgs.push_back(A);
382     }
383
384     virtual const char *MakeArgString(StringRef Str) const;
385
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));
390     }
391
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
394     /// list.
395     void AddPositionalArg(const Arg *BaseArg, const Option Opt,
396                           StringRef Value) {
397       append(MakePositionalArg(BaseArg, Opt, Value));
398     }
399
400
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
403     /// list.
404     void AddSeparateArg(const Arg *BaseArg, const Option Opt,
405                         StringRef Value) {
406       append(MakeSeparateArg(BaseArg, Opt, Value));
407     }
408
409
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,
413                       StringRef Value) {
414       append(MakeJoinedArg(BaseArg, Opt, Value));
415     }
416
417
418     /// MakeFlagArg - Construct a new FlagArg for the given option \p Id.
419     Arg *MakeFlagArg(const Arg *BaseArg, const Option Opt) const;
420
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;
425
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;
430
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;
435
436     /// @}
437   };
438
439 } // end namespace driver
440 } // end namespace clang
441
442 #endif