LLVM  4.0.0
Arg.h
Go to the documentation of this file.
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 &) = delete;
33  void operator=(const Arg &) = delete;
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.
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 
70  const Option &getOption() const { return Opt; }
71  StringRef getSpelling() const { return Spelling; }
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.
78  const Arg &getBaseArg() const {
79  return BaseArg ? *BaseArg : *this;
80  }
81  void setBaseArg(const Arg *BaseArg) { this->BaseArg = BaseArg; }
82 
83  bool getOwnsValues() const { return OwnsValues; }
84  void setOwnsValues(bool Value) const { OwnsValues = Value; }
85 
86  bool isClaimed() const { return getBaseArg().Claimed; }
87 
88  /// \brief Set the Arg claimed bit.
89  void claim() const { getBaseArg().Claimed = true; }
90 
91  unsigned getNumValues() const { return Values.size(); }
92  const char *getValue(unsigned N = 0) const {
93  return Values[N];
94  }
95 
97  const SmallVectorImpl<const char *> &getValues() const { return Values; }
98 
100  for (unsigned i = 0, e = getNumValues(); i != e; ++i)
101  if (Values[i] == Value)
102  return true;
103  return false;
104  }
105 
106  /// \brief Append the argument onto the given array as strings.
107  void render(const ArgList &Args, ArgStringList &Output) const;
108 
109  /// \brief Append the argument, render as an input, onto the given
110  /// array as strings.
111  ///
112  /// The distinction is that some options only render their values
113  /// when rendered as a input (e.g., Xlinker).
114  void renderAsInput(const ArgList &Args, ArgStringList &Output) const;
115 
116  void print(raw_ostream &O) const;
117  void dump() const;
118 
119  /// \brief Return a formatted version of the argument and
120  /// its values, for debugging and diagnostics.
121  std::string getAsString(const ArgList &Args) const;
122 };
123 
124 } // end namespace opt
125 } // end namespace llvm
126 
127 #endif
unsigned getIndex() const
Definition: Arg.h:72
const Option & getOption() const
Definition: Arg.h:70
size_t i
const char * getValue(unsigned N=0) const
Definition: Arg.h:92
bool isClaimed() const
Definition: Arg.h:86
void setBaseArg(const Arg *BaseArg)
Definition: Arg.h:81
StringRef getSpelling() const
Definition: Arg.h:71
SmallVectorImpl< const char * > & getValues()
Definition: Arg.h:96
void print(raw_ostream &O) const
Definition: Arg.cpp:47
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:44
A concrete instance of a particular driver option.
Definition: Arg.h:31
void dump() const
Definition: Arg.cpp:64
void claim() const
Set the Arg claimed bit.
Definition: Arg.h:89
void renderAsInput(const ArgList &Args, ArgStringList &Output) const
Append the argument, render as an input, onto the given array as strings.
Definition: Arg.cpp:82
void render(const ArgList &Args, ArgStringList &Output) const
Append the argument onto the given array as strings.
Definition: Arg.cpp:91
bool getOwnsValues() const
Definition: Arg.h:83
void setOwnsValues(bool Value) const
Definition: Arg.h:84
const SmallVectorImpl< const char * > & getValues() const
Definition: Arg.h:97
unsigned getNumValues() const
Definition: Arg.h:91
#define N
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
bool containsValue(StringRef Value) const
Definition: Arg.h:99
std::string getAsString(const ArgList &Args) const
Return a formatted version of the argument and its values, for debugging and diagnostics.
Definition: Arg.cpp:66
LLVM Value Representation.
Definition: Value.h:71
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
ArgList - Ordered collection of driver arguments.
Definition: ArgList.h:94
const Arg & getBaseArg() const
Return the base argument which generated this arg.
Definition: Arg.h:78