• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- Arg.h - Parsed Argument Classes ------------------------*- 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 /// \file
11 /// \brief Defines the llvm::Arg class for parsed arguments.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_OPTION_ARG_H
16 #define LLVM_OPTION_ARG_H
17 
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Option/Option.h"
21 #include <string>
22 
23 namespace llvm {
24 namespace opt {
25 class ArgList;
26 
27 /// \brief A concrete instance of a particular driver option.
28 ///
29 /// The Arg class encodes just enough information to be able to
30 /// derive the argument values efficiently.
31 class Arg {
32   Arg(const Arg &) LLVM_DELETED_FUNCTION;
33   void operator=(const Arg &) LLVM_DELETED_FUNCTION;
34 
35 private:
36   /// \brief The option this argument is an instance of.
37   const Option Opt;
38 
39   /// \brief The argument this argument was derived from (during tool chain
40   /// argument translation), if any.
41   const Arg *BaseArg;
42 
43   /// \brief How this instance of the option was spelled.
44   StringRef Spelling;
45 
46   /// \brief The index at which this argument appears in the containing
47   /// ArgList.
48   unsigned Index;
49 
50   /// \brief Was this argument used to effect compilation?
51   ///
52   /// This is used for generating "argument unused" diagnostics.
53   mutable unsigned Claimed : 1;
54 
55   /// \brief Does this argument own its values?
56   mutable unsigned OwnsValues : 1;
57 
58   /// \brief The argument values, as C strings.
59   SmallVector<const char *, 2> Values;
60 
61 public:
62   Arg(const Option Opt, StringRef Spelling, unsigned Index,
63       const Arg *BaseArg = nullptr);
64   Arg(const Option Opt, StringRef Spelling, unsigned Index,
65       const char *Value0, const Arg *BaseArg = nullptr);
66   Arg(const Option Opt, StringRef Spelling, unsigned Index,
67       const char *Value0, const char *Value1, const Arg *BaseArg = nullptr);
68   ~Arg();
69 
getOption()70   const Option &getOption() const { return Opt; }
getSpelling()71   StringRef getSpelling() const { return Spelling; }
getIndex()72   unsigned getIndex() const { return Index; }
73 
74   /// \brief Return the base argument which generated this arg.
75   ///
76   /// This is either the argument itself or the argument it was
77   /// derived from during tool chain specific argument translation.
getBaseArg()78   const Arg &getBaseArg() const {
79     return BaseArg ? *BaseArg : *this;
80   }
setBaseArg(const Arg * _BaseArg)81   void setBaseArg(const Arg *_BaseArg) {
82     BaseArg = _BaseArg;
83   }
84 
getOwnsValues()85   bool getOwnsValues() const { return OwnsValues; }
setOwnsValues(bool Value)86   void setOwnsValues(bool Value) const { OwnsValues = Value; }
87 
isClaimed()88   bool isClaimed() const { return getBaseArg().Claimed; }
89 
90   /// \brief Set the Arg claimed bit.
claim()91   void claim() const { getBaseArg().Claimed = true; }
92 
getNumValues()93   unsigned getNumValues() const { return Values.size(); }
94   const char *getValue(unsigned N = 0) const {
95     return Values[N];
96   }
97 
getValues()98   SmallVectorImpl<const char*> &getValues() {
99     return Values;
100   }
101 
containsValue(StringRef Value)102   bool containsValue(StringRef Value) const {
103     for (unsigned i = 0, e = getNumValues(); i != e; ++i)
104       if (Values[i] == Value)
105         return true;
106     return false;
107   }
108 
109   /// \brief Append the argument onto the given array as strings.
110   void render(const ArgList &Args, ArgStringList &Output) const;
111 
112   /// \brief Append the argument, render as an input, onto the given
113   /// array as strings.
114   ///
115   /// The distinction is that some options only render their values
116   /// when rendered as a input (e.g., Xlinker).
117   void renderAsInput(const ArgList &Args, ArgStringList &Output) const;
118 
119   void dump() const;
120 
121   /// \brief Return a formatted version of the argument and
122   /// its values, for debugging and diagnostics.
123   std::string getAsString(const ArgList &Args) const;
124 };
125 
126 } // end namespace opt
127 } // end namespace llvm
128 
129 #endif
130