Coverage Report

Created: 2019-07-24 05:18

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