blob: 22e2bcf06a6ebb86fd88e65cadbe3c197ae39c69 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- Arg.h - Parsed Argument Classes --------------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Andrew Scullcdfcccc2018-10-05 20:58:37 +010010/// Defines the llvm::Arg class for parsed arguments.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010011///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_OPTION_ARG_H
15#define LLVM_OPTION_ARG_H
16
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Option/Option.h"
20#include <string>
21
22namespace llvm {
23
24class raw_ostream;
25
26namespace opt {
27
28class ArgList;
29
Andrew Scullcdfcccc2018-10-05 20:58:37 +010030/// A concrete instance of a particular driver option.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010031///
32/// The Arg class encodes just enough information to be able to
33/// derive the argument values efficiently.
34class Arg {
35private:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010036 /// The option this argument is an instance of.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010037 const Option Opt;
38
Andrew Scullcdfcccc2018-10-05 20:58:37 +010039 /// The argument this argument was derived from (during tool chain
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010040 /// argument translation), if any.
41 const Arg *BaseArg;
42
Andrew Scullcdfcccc2018-10-05 20:58:37 +010043 /// How this instance of the option was spelled.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010044 StringRef Spelling;
45
Andrew Scullcdfcccc2018-10-05 20:58:37 +010046 /// The index at which this argument appears in the containing
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010047 /// ArgList.
48 unsigned Index;
49
Andrew Scullcdfcccc2018-10-05 20:58:37 +010050 /// Was this argument used to effect compilation?
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010051 ///
52 /// This is used for generating "argument unused" diagnostics.
53 mutable unsigned Claimed : 1;
54
Andrew Scullcdfcccc2018-10-05 20:58:37 +010055 /// Does this argument own its values?
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010056 mutable unsigned OwnsValues : 1;
57
Andrew Scullcdfcccc2018-10-05 20:58:37 +010058 /// The argument values, as C strings.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010059 SmallVector<const char *, 2> Values;
60
Andrew Walbran3d2c1972020-04-07 12:24:26 +010061 /// If this arg was created through an alias, this is the original alias arg.
62 /// For example, *this might be "-finput-charset=utf-8" and Alias might
63 /// point to an arg representing "/source-charset:utf-8".
64 std::unique_ptr<Arg> Alias;
65
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010066public:
67 Arg(const Option Opt, StringRef Spelling, unsigned Index,
68 const Arg *BaseArg = nullptr);
69 Arg(const Option Opt, StringRef Spelling, unsigned Index,
70 const char *Value0, const Arg *BaseArg = nullptr);
71 Arg(const Option Opt, StringRef Spelling, unsigned Index,
72 const char *Value0, const char *Value1, const Arg *BaseArg = nullptr);
73 Arg(const Arg &) = delete;
74 Arg &operator=(const Arg &) = delete;
75 ~Arg();
76
77 const Option &getOption() const { return Opt; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010078
79 /// Returns the used prefix and name of the option:
80 /// For `--foo=bar`, returns `--foo=`.
81 /// This is often the wrong function to call:
82 /// * Use `getValue()` to get `bar`.
83 /// * Use `getAsString()` to get a string suitable for printing an Arg in
84 /// a diagnostic.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010085 StringRef getSpelling() const { return Spelling; }
Andrew Walbran3d2c1972020-04-07 12:24:26 +010086
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010087 unsigned getIndex() const { return Index; }
88
Andrew Scullcdfcccc2018-10-05 20:58:37 +010089 /// Return the base argument which generated this arg.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010090 ///
91 /// This is either the argument itself or the argument it was
92 /// derived from during tool chain specific argument translation.
93 const Arg &getBaseArg() const {
94 return BaseArg ? *BaseArg : *this;
95 }
96 void setBaseArg(const Arg *BaseArg) { this->BaseArg = BaseArg; }
97
Andrew Walbran3d2c1972020-04-07 12:24:26 +010098 /// Args are converted to their unaliased form. For args that originally
99 /// came from an alias, this returns the alias the arg was produced from.
100 const Arg* getAlias() const { return Alias.get(); }
101 void setAlias(std::unique_ptr<Arg> Alias) { this->Alias = std::move(Alias); }
102
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100103 bool getOwnsValues() const { return OwnsValues; }
104 void setOwnsValues(bool Value) const { OwnsValues = Value; }
105
106 bool isClaimed() const { return getBaseArg().Claimed; }
107
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100108 /// Set the Arg claimed bit.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100109 void claim() const { getBaseArg().Claimed = true; }
110
111 unsigned getNumValues() const { return Values.size(); }
112
113 const char *getValue(unsigned N = 0) const {
114 return Values[N];
115 }
116
117 SmallVectorImpl<const char *> &getValues() { return Values; }
118 const SmallVectorImpl<const char *> &getValues() const { return Values; }
119
120 bool containsValue(StringRef Value) const {
121 for (unsigned i = 0, e = getNumValues(); i != e; ++i)
122 if (Values[i] == Value)
123 return true;
124 return false;
125 }
126
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100127 /// Append the argument onto the given array as strings.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100128 void render(const ArgList &Args, ArgStringList &Output) const;
129
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100130 /// Append the argument, render as an input, onto the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100131 /// array as strings.
132 ///
133 /// The distinction is that some options only render their values
134 /// when rendered as a input (e.g., Xlinker).
135 void renderAsInput(const ArgList &Args, ArgStringList &Output) const;
136
137 void print(raw_ostream &O) const;
138 void dump() const;
139
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100140 /// Return a formatted version of the argument and its values, for
141 /// diagnostics. Since this is for diagnostics, if this Arg was produced
142 /// through an alias, this returns the string representation of the alias
143 /// that the user wrote.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100144 std::string getAsString(const ArgList &Args) const;
145};
146
147} // end namespace opt
148
149} // end namespace llvm
150
151#endif // LLVM_OPTION_ARG_H