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