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