]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Option/ArgList.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Option / ArgList.cpp
1 //===- ArgList.cpp - Argument List Management -----------------------------===//
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 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/None.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Config/llvm-config.h"
17 #include "llvm/Option/Arg.h"
18 #include "llvm/Option/ArgList.h"
19 #include "llvm/Option/Option.h"
20 #include "llvm/Option/OptSpecifier.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <algorithm>
25 #include <cassert>
26 #include <memory>
27 #include <string>
28 #include <utility>
29 #include <vector>
30
31 using namespace llvm;
32 using namespace llvm::opt;
33
34 void ArgList::append(Arg *A) {
35   Args.push_back(A);
36
37   // Update ranges for the option and all of its groups.
38   for (Option O = A->getOption().getUnaliasedOption(); O.isValid();
39        O = O.getGroup()) {
40     auto &R =
41         OptRanges.insert(std::make_pair(O.getID(), emptyRange())).first->second;
42     R.first = std::min<unsigned>(R.first, Args.size() - 1);
43     R.second = Args.size();
44   }
45 }
46
47 void ArgList::eraseArg(OptSpecifier Id) {
48   // Zero out the removed entries but keep them around so that we don't
49   // need to invalidate OptRanges.
50   for (Arg *const &A : filtered(Id)) {
51     // Avoid the need for a non-const filtered iterator variant.
52     Arg **ArgsBegin = Args.data();
53     ArgsBegin[&A - ArgsBegin] = nullptr;
54   }
55   OptRanges.erase(Id.getID());
56 }
57
58 ArgList::OptRange
59 ArgList::getRange(std::initializer_list<OptSpecifier> Ids) const {
60   OptRange R = emptyRange();
61   for (auto Id : Ids) {
62     auto I = OptRanges.find(Id.getID());
63     if (I != OptRanges.end()) {
64       R.first = std::min(R.first, I->second.first);
65       R.second = std::max(R.second, I->second.second);
66     }
67   }
68   // Map an empty {-1, 0} range to {0, 0} so it can be used to form iterators.
69   if (R.first == -1u)
70     R.first = 0;
71   return R;
72 }
73
74 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
75   if (Arg *A = getLastArg(Pos, Neg))
76     return A->getOption().matches(Pos);
77   return Default;
78 }
79
80 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
81                       bool Default) const {
82   if (Arg *A = getLastArg(Pos, PosAlias, Neg))
83     return A->getOption().matches(Pos) || A->getOption().matches(PosAlias);
84   return Default;
85 }
86
87 StringRef ArgList::getLastArgValue(OptSpecifier Id, StringRef Default) const {
88   if (Arg *A = getLastArg(Id))
89     return A->getValue();
90   return Default;
91 }
92
93 std::vector<std::string> ArgList::getAllArgValues(OptSpecifier Id) const {
94   SmallVector<const char *, 16> Values;
95   AddAllArgValues(Values, Id);
96   return std::vector<std::string>(Values.begin(), Values.end());
97 }
98
99 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id) const {
100   if (Arg *A = getLastArg(Id)) {
101     A->claim();
102     A->render(*this, Output);
103   }
104 }
105
106 void ArgList::AddLastArg(ArgStringList &Output, OptSpecifier Id0,
107                          OptSpecifier Id1) const {
108   if (Arg *A = getLastArg(Id0, Id1)) {
109     A->claim();
110     A->render(*this, Output);
111   }
112 }
113
114 void ArgList::AddAllArgsExcept(ArgStringList &Output,
115                                ArrayRef<OptSpecifier> Ids,
116                                ArrayRef<OptSpecifier> ExcludeIds) const {
117   for (const Arg *Arg : *this) {
118     bool Excluded = false;
119     for (OptSpecifier Id : ExcludeIds) {
120       if (Arg->getOption().matches(Id)) {
121         Excluded = true;
122         break;
123       }
124     }
125     if (!Excluded) {
126       for (OptSpecifier Id : Ids) {
127         if (Arg->getOption().matches(Id)) {
128           Arg->claim();
129           Arg->render(*this, Output);
130           break;
131         }
132       }
133     }
134   }
135 }
136
137 /// This is a nicer interface when you don't have a list of Ids to exclude.
138 void ArgList::AddAllArgs(ArgStringList &Output,
139                          ArrayRef<OptSpecifier> Ids) const {
140   ArrayRef<OptSpecifier> Exclude = None;
141   AddAllArgsExcept(Output, Ids, Exclude);
142 }
143
144 /// This 3-opt variant of AddAllArgs could be eliminated in favor of one
145 /// that accepts a single specifier, given the above which accepts any number.
146 void ArgList::AddAllArgs(ArgStringList &Output, OptSpecifier Id0,
147                          OptSpecifier Id1, OptSpecifier Id2) const {
148   for (auto Arg: filtered(Id0, Id1, Id2)) {
149     Arg->claim();
150     Arg->render(*this, Output);
151   }
152 }
153
154 void ArgList::AddAllArgValues(ArgStringList &Output, OptSpecifier Id0,
155                               OptSpecifier Id1, OptSpecifier Id2) const {
156   for (auto Arg : filtered(Id0, Id1, Id2)) {
157     Arg->claim();
158     const auto &Values = Arg->getValues();
159     Output.append(Values.begin(), Values.end());
160   }
161 }
162
163 void ArgList::AddAllArgsTranslated(ArgStringList &Output, OptSpecifier Id0,
164                                    const char *Translation,
165                                    bool Joined) const {
166   for (auto Arg: filtered(Id0)) {
167     Arg->claim();
168
169     if (Joined) {
170       Output.push_back(MakeArgString(StringRef(Translation) +
171                                      Arg->getValue(0)));
172     } else {
173       Output.push_back(Translation);
174       Output.push_back(Arg->getValue(0));
175     }
176   }
177 }
178
179 void ArgList::ClaimAllArgs(OptSpecifier Id0) const {
180   for (auto *Arg : filtered(Id0))
181     Arg->claim();
182 }
183
184 void ArgList::ClaimAllArgs() const {
185   for (auto *Arg : *this)
186     if (!Arg->isClaimed())
187       Arg->claim();
188 }
189
190 const char *ArgList::GetOrMakeJoinedArgString(unsigned Index,
191                                               StringRef LHS,
192                                               StringRef RHS) const {
193   StringRef Cur = getArgString(Index);
194   if (Cur.size() == LHS.size() + RHS.size() &&
195       Cur.startswith(LHS) && Cur.endswith(RHS))
196     return Cur.data();
197
198   return MakeArgString(LHS + RHS);
199 }
200
201 void ArgList::print(raw_ostream &O) const {
202   for (Arg *A : *this) {
203     O << "* ";
204     A->print(O);
205   }
206 }
207
208 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
209 LLVM_DUMP_METHOD void ArgList::dump() const { print(dbgs()); }
210 #endif
211
212 void InputArgList::releaseMemory() {
213   // An InputArgList always owns its arguments.
214   for (Arg *A : *this)
215     delete A;
216 }
217
218 InputArgList::InputArgList(const char* const *ArgBegin,
219                            const char* const *ArgEnd)
220   : NumInputArgStrings(ArgEnd - ArgBegin) {
221   ArgStrings.append(ArgBegin, ArgEnd);
222 }
223
224 unsigned InputArgList::MakeIndex(StringRef String0) const {
225   unsigned Index = ArgStrings.size();
226
227   // Tuck away so we have a reliable const char *.
228   SynthesizedStrings.push_back(String0);
229   ArgStrings.push_back(SynthesizedStrings.back().c_str());
230
231   return Index;
232 }
233
234 unsigned InputArgList::MakeIndex(StringRef String0,
235                                  StringRef String1) const {
236   unsigned Index0 = MakeIndex(String0);
237   unsigned Index1 = MakeIndex(String1);
238   assert(Index0 + 1 == Index1 && "Unexpected non-consecutive indices!");
239   (void) Index1;
240   return Index0;
241 }
242
243 const char *InputArgList::MakeArgStringRef(StringRef Str) const {
244   return getArgString(MakeIndex(Str));
245 }
246
247 DerivedArgList::DerivedArgList(const InputArgList &BaseArgs)
248     : BaseArgs(BaseArgs) {}
249
250 const char *DerivedArgList::MakeArgStringRef(StringRef Str) const {
251   return BaseArgs.MakeArgString(Str);
252 }
253
254 void DerivedArgList::AddSynthesizedArg(Arg *A) {
255   SynthesizedArgs.push_back(std::unique_ptr<Arg>(A));
256 }
257
258 Arg *DerivedArgList::MakeFlagArg(const Arg *BaseArg, const Option Opt) const {
259   SynthesizedArgs.push_back(
260       make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
261                        BaseArgs.MakeIndex(Opt.getName()), BaseArg));
262   return SynthesizedArgs.back().get();
263 }
264
265 Arg *DerivedArgList::MakePositionalArg(const Arg *BaseArg, const Option Opt,
266                                        StringRef Value) const {
267   unsigned Index = BaseArgs.MakeIndex(Value);
268   SynthesizedArgs.push_back(
269       make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
270                        Index, BaseArgs.getArgString(Index), BaseArg));
271   return SynthesizedArgs.back().get();
272 }
273
274 Arg *DerivedArgList::MakeSeparateArg(const Arg *BaseArg, const Option Opt,
275                                      StringRef Value) const {
276   unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
277   SynthesizedArgs.push_back(
278       make_unique<Arg>(Opt, MakeArgString(Opt.getPrefix() + Opt.getName()),
279                        Index, BaseArgs.getArgString(Index + 1), BaseArg));
280   return SynthesizedArgs.back().get();
281 }
282
283 Arg *DerivedArgList::MakeJoinedArg(const Arg *BaseArg, const Option Opt,
284                                    StringRef Value) const {
285   unsigned Index = BaseArgs.MakeIndex((Opt.getName() + Value).str());
286   SynthesizedArgs.push_back(make_unique<Arg>(
287       Opt, MakeArgString(Opt.getPrefix() + Opt.getName()), Index,
288       BaseArgs.getArgString(Index) + Opt.getName().size(), BaseArg));
289   return SynthesizedArgs.back().get();
290 }