Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- VariantValue.cpp - Polymorphic value type -*- C++ -*-===/
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
/// \file
10
/// Polymorphic value type.
11
///
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/ASTMatchers/Dynamic/VariantValue.h"
15
#include "clang/Basic/LLVM.h"
16
#include "llvm/ADT/STLExtras.h"
17
18
namespace clang {
19
namespace ast_matchers {
20
namespace dynamic {
21
22
97
std::string ArgKind::asString() const {
23
97
  switch (getArgKind()) {
24
28
  case AK_Matcher:
25
28
    return (Twine("Matcher<") + MatcherKind.asStringRef() + ">").str();
26
4
  case AK_Boolean:
27
4
    return "boolean";
28
4
  case AK_Double:
29
4
    return "double";
30
10
  case AK_Unsigned:
31
10
    return "unsigned";
32
51
  case AK_String:
33
51
    return "string";
34
0
  }
35
0
  llvm_unreachable("unhandled ArgKind");
36
0
}
37
38
13.2k
bool ArgKind::isConvertibleTo(ArgKind To, unsigned *Specificity) const {
39
13.2k
  if (K != To.K)
40
0
    return false;
41
13.2k
  if (K != AK_Matcher) {
42
0
    if (Specificity)
43
0
      *Specificity = 1;
44
0
    return true;
45
0
  }
46
13.2k
  unsigned Distance;
47
13.2k
  if (!MatcherKind.isBaseOf(To.MatcherKind, &Distance))
48
11.4k
    return false;
49
1.85k
50
1.85k
  if (Specificity)
51
1.13k
    *Specificity = 100 - Distance;
52
1.85k
  return true;
53
1.85k
}
54
55
bool
56
VariantMatcher::MatcherOps::canConstructFrom(const DynTypedMatcher &Matcher,
57
576
                                             bool &IsExactMatch) const {
58
576
  IsExactMatch = Matcher.getSupportedKind().isSame(NodeKind);
59
576
  return Matcher.canConvertTo(NodeKind);
60
576
}
61
62
llvm::Optional<DynTypedMatcher>
63
VariantMatcher::MatcherOps::constructVariadicOperator(
64
    DynTypedMatcher::VariadicOperator Op,
65
13
    ArrayRef<VariantMatcher> InnerMatchers) const {
66
13
  std::vector<DynTypedMatcher> DynMatchers;
67
21
  for (const auto &InnerMatcher : InnerMatchers) {
68
21
    // Abort if any of the inner matchers can't be converted to
69
21
    // Matcher<T>.
70
21
    if (!InnerMatcher.Value)
71
0
      return llvm::None;
72
21
    llvm::Optional<DynTypedMatcher> Inner =
73
21
        InnerMatcher.Value->getTypedMatcher(*this);
74
21
    if (!Inner)
75
1
      return llvm::None;
76
20
    DynMatchers.push_back(*Inner);
77
20
  }
78
13
  
return DynTypedMatcher::constructVariadic(Op, NodeKind, DynMatchers)12
;
79
13
}
80
81
159
VariantMatcher::Payload::~Payload() {}
82
83
class VariantMatcher::SinglePayload : public VariantMatcher::Payload {
84
public:
85
126
  SinglePayload(const DynTypedMatcher &Matcher) : Matcher(Matcher) {}
86
87
20
  llvm::Optional<DynTypedMatcher> getSingleMatcher() const override {
88
20
    return Matcher;
89
20
  }
90
91
27
  std::string getTypeAsString() const override {
92
27
    return (Twine("Matcher<") + Matcher.getSupportedKind().asStringRef() + ">")
93
27
        .str();
94
27
  }
95
96
  llvm::Optional<DynTypedMatcher>
97
282
  getTypedMatcher(const MatcherOps &Ops) const override {
98
282
    bool Ignore;
99
282
    if (Ops.canConstructFrom(Matcher, Ignore))
100
254
      return Matcher;
101
28
    return llvm::None;
102
28
  }
103
104
2
  bool isConvertibleTo(ASTNodeKind Kind, unsigned *Specificity) const override {
105
2
    return ArgKind(Matcher.getSupportedKind())
106
2
        .isConvertibleTo(Kind, Specificity);
107
2
  }
108
109
private:
110
  const DynTypedMatcher Matcher;
111
};
112
113
class VariantMatcher::PolymorphicPayload : public VariantMatcher::Payload {
114
public:
115
  PolymorphicPayload(std::vector<DynTypedMatcher> MatchersIn)
116
27
      : Matchers(std::move(MatchersIn)) {}
117
118
27
  ~PolymorphicPayload() override {}
119
120
2
  llvm::Optional<DynTypedMatcher> getSingleMatcher() const override {
121
2
    if (Matchers.size() != 1)
122
2
      return llvm::Optional<DynTypedMatcher>();
123
0
    return Matchers[0];
124
0
  }
125
126
4
  std::string getTypeAsString() const override {
127
4
    std::string Inner;
128
16
    for (size_t i = 0, e = Matchers.size(); i != e; 
++i12
) {
129
12
      if (i != 0)
130
8
        Inner += "|";
131
12
      Inner += Matchers[i].getSupportedKind().asStringRef();
132
12
    }
133
4
    return (Twine("Matcher<") + Inner + ">").str();
134
4
  }
135
136
  llvm::Optional<DynTypedMatcher>
137
72
  getTypedMatcher(const MatcherOps &Ops) const override {
138
72
    bool FoundIsExact = false;
139
72
    const DynTypedMatcher *Found = nullptr;
140
72
    int NumFound = 0;
141
366
    for (size_t i = 0, e = Matchers.size(); i != e; 
++i294
) {
142
294
      bool IsExactMatch;
143
294
      if (Ops.canConstructFrom(Matchers[i], IsExactMatch)) {
144
70
        if (Found) {
145
0
          if (FoundIsExact) {
146
0
            assert(!IsExactMatch && "We should not have two exact matches.");
147
0
            continue;
148
0
          }
149
70
        }
150
70
        Found = &Matchers[i];
151
70
        FoundIsExact = IsExactMatch;
152
70
        ++NumFound;
153
70
      }
154
294
    }
155
72
    // We only succeed if we found exactly one, or if we found an exact match.
156
72
    if (Found && 
(70
FoundIsExact70
||
NumFound == 112
))
157
70
      return *Found;
158
2
    return llvm::None;
159
2
  }
160
161
0
  bool isConvertibleTo(ASTNodeKind Kind, unsigned *Specificity) const override {
162
0
    unsigned MaxSpecificity = 0;
163
0
    for (const DynTypedMatcher &Matcher : Matchers) {
164
0
      unsigned ThisSpecificity;
165
0
      if (ArgKind(Matcher.getSupportedKind())
166
0
              .isConvertibleTo(Kind, &ThisSpecificity)) {
167
0
        MaxSpecificity = std::max(MaxSpecificity, ThisSpecificity);
168
0
      }
169
0
    }
170
0
    if (Specificity)
171
0
      *Specificity = MaxSpecificity;
172
0
    return MaxSpecificity > 0;
173
0
  }
174
175
  const std::vector<DynTypedMatcher> Matchers;
176
};
177
178
class VariantMatcher::VariadicOpPayload : public VariantMatcher::Payload {
179
public:
180
  VariadicOpPayload(DynTypedMatcher::VariadicOperator Op,
181
                    std::vector<VariantMatcher> Args)
182
6
      : Op(Op), Args(std::move(Args)) {}
183
184
0
  llvm::Optional<DynTypedMatcher> getSingleMatcher() const override {
185
0
    return llvm::Optional<DynTypedMatcher>();
186
0
  }
187
188
1
  std::string getTypeAsString() const override {
189
1
    std::string Inner;
190
3
    for (size_t i = 0, e = Args.size(); i != e; 
++i2
) {
191
2
      if (i != 0)
192
1
        Inner += "&";
193
2
      Inner += Args[i].getTypeAsString();
194
2
    }
195
1
    return Inner;
196
1
  }
197
198
  llvm::Optional<DynTypedMatcher>
199
13
  getTypedMatcher(const MatcherOps &Ops) const override {
200
13
    return Ops.constructVariadicOperator(Op, Args);
201
13
  }
202
203
0
  bool isConvertibleTo(ASTNodeKind Kind, unsigned *Specificity) const override {
204
0
    for (const VariantMatcher &Matcher : Args) {
205
0
      if (!Matcher.isConvertibleTo(Kind, Specificity))
206
0
        return false;
207
0
    }
208
0
    return true;
209
0
  }
210
211
private:
212
  const DynTypedMatcher::VariadicOperator Op;
213
  const std::vector<VariantMatcher> Args;
214
};
215
216
219
VariantMatcher::VariantMatcher() {}
217
218
126
VariantMatcher VariantMatcher::SingleMatcher(const DynTypedMatcher &Matcher) {
219
126
  return VariantMatcher(std::make_shared<SinglePayload>(Matcher));
220
126
}
221
222
VariantMatcher
223
27
VariantMatcher::PolymorphicMatcher(std::vector<DynTypedMatcher> Matchers) {
224
27
  return VariantMatcher(
225
27
      std::make_shared<PolymorphicPayload>(std::move(Matchers)));
226
27
}
227
228
VariantMatcher VariantMatcher::VariadicOperatorMatcher(
229
    DynTypedMatcher::VariadicOperator Op,
230
6
    std::vector<VariantMatcher> Args) {
231
6
  return VariantMatcher(
232
6
      std::make_shared<VariadicOpPayload>(Op, std::move(Args)));
233
6
}
234
235
22
llvm::Optional<DynTypedMatcher> VariantMatcher::getSingleMatcher() const {
236
22
  return Value ? Value->getSingleMatcher() : 
llvm::Optional<DynTypedMatcher>()0
;
237
22
}
238
239
0
void VariantMatcher::reset() { Value.reset(); }
240
241
32
std::string VariantMatcher::getTypeAsString() const {
242
32
  if (Value) return Value->getTypeAsString();
243
0
  return "<Nothing>";
244
0
}
245
246
994
VariantValue::VariantValue(const VariantValue &Other) : Type(VT_Nothing) {
247
994
  *this = Other;
248
994
}
249
250
4
VariantValue::VariantValue(bool Boolean) : Type(VT_Nothing) {
251
4
  setBoolean(Boolean);
252
4
}
253
254
5
VariantValue::VariantValue(double Double) : Type(VT_Nothing) {
255
5
  setDouble(Double);
256
5
}
257
258
17
VariantValue::VariantValue(unsigned Unsigned) : Type(VT_Nothing) {
259
17
  setUnsigned(Unsigned);
260
17
}
261
262
55
VariantValue::VariantValue(StringRef String) : Type(VT_Nothing) {
263
55
  setString(String);
264
55
}
265
266
123
VariantValue::VariantValue(const VariantMatcher &Matcher) : Type(VT_Nothing) {
267
123
  setMatcher(Matcher);
268
123
}
269
270
1.88k
VariantValue::~VariantValue() { reset(); }
271
272
1.61k
VariantValue &VariantValue::operator=(const VariantValue &Other) {
273
1.61k
  if (this == &Other) 
return *this0
;
274
1.61k
  reset();
275
1.61k
  switch (Other.Type) {
276
15
  case VT_Boolean:
277
15
    setBoolean(Other.getBoolean());
278
15
    break;
279
25
  case VT_Double:
280
25
    setDouble(Other.getDouble());
281
25
    break;
282
76
  case VT_Unsigned:
283
76
    setUnsigned(Other.getUnsigned());
284
76
    break;
285
192
  case VT_String:
286
192
    setString(Other.getString());
287
192
    break;
288
160
  case VT_Matcher:
289
160
    setMatcher(Other.getMatcher());
290
160
    break;
291
1.14k
  case VT_Nothing:
292
1.14k
    Type = VT_Nothing;
293
1.14k
    break;
294
1.61k
  }
295
1.61k
  return *this;
296
1.61k
}
297
298
4.16k
void VariantValue::reset() {
299
4.16k
  switch (Type) {
300
247
  case VT_String:
301
247
    delete Value.String;
302
247
    break;
303
283
  case VT_Matcher:
304
283
    delete Value.Matcher;
305
283
    break;
306
0
  // Cases that do nothing.
307
3.63k
  case VT_Boolean:
308
3.63k
  case VT_Double:
309
3.63k
  case VT_Unsigned:
310
3.63k
  case VT_Nothing:
311
3.63k
    break;
312
4.16k
  }
313
4.16k
  Type = VT_Nothing;
314
4.16k
}
315
316
30
bool VariantValue::isBoolean() const {
317
30
  return Type == VT_Boolean;
318
30
}
319
320
19
bool VariantValue::getBoolean() const {
321
19
  assert(isBoolean());
322
19
  return Value.Boolean;
323
19
}
324
325
19
void VariantValue::setBoolean(bool NewValue) {
326
19
  reset();
327
19
  Type = VT_Boolean;
328
19
  Value.Boolean = NewValue;
329
19
}
330
331
41
bool VariantValue::isDouble() const {
332
41
  return Type == VT_Double;
333
41
}
334
335
30
double VariantValue::getDouble() const {
336
30
  assert(isDouble());
337
30
  return Value.Double;
338
30
}
339
340
30
void VariantValue::setDouble(double NewValue) {
341
30
  reset();
342
30
  Type = VT_Double;
343
30
  Value.Double = NewValue;
344
30
}
345
346
109
bool VariantValue::isUnsigned() const {
347
109
  return Type == VT_Unsigned;
348
109
}
349
350
90
unsigned VariantValue::getUnsigned() const {
351
90
  assert(isUnsigned());
352
90
  return Value.Unsigned;
353
90
}
354
355
93
void VariantValue::setUnsigned(unsigned NewValue) {
356
93
  reset();
357
93
  Type = VT_Unsigned;
358
93
  Value.Unsigned = NewValue;
359
93
}
360
361
276
bool VariantValue::isString() const {
362
276
  return Type == VT_String;
363
276
}
364
365
231
const std::string &VariantValue::getString() const {
366
231
  assert(isString());
367
231
  return *Value.String;
368
231
}
369
370
247
void VariantValue::setString(StringRef NewValue) {
371
247
  reset();
372
247
  Type = VT_String;
373
247
  Value.String = new std::string(NewValue);
374
247
}
375
376
563
bool VariantValue::isMatcher() const {
377
563
  return Type == VT_Matcher;
378
563
}
379
380
415
const VariantMatcher &VariantValue::getMatcher() const {
381
415
  assert(isMatcher());
382
415
  return *Value.Matcher;
383
415
}
384
385
283
void VariantValue::setMatcher(const VariantMatcher &NewValue) {
386
283
  reset();
387
283
  Type = VT_Matcher;
388
283
  Value.Matcher = new VariantMatcher(NewValue);
389
283
}
390
391
6
bool VariantValue::isConvertibleTo(ArgKind Kind, unsigned *Specificity) const {
392
6
  switch (Kind.getArgKind()) {
393
0
  case ArgKind::AK_Boolean:
394
0
    if (!isBoolean())
395
0
      return false;
396
0
    *Specificity = 1;
397
0
    return true;
398
0
399
0
  case ArgKind::AK_Double:
400
0
    if (!isDouble())
401
0
      return false;
402
0
    *Specificity = 1;
403
0
    return true;
404
0
405
0
  case ArgKind::AK_Unsigned:
406
0
    if (!isUnsigned())
407
0
      return false;
408
0
    *Specificity = 1;
409
0
    return true;
410
0
411
2
  case ArgKind::AK_String:
412
2
    if (!isString())
413
1
      return false;
414
1
    *Specificity = 1;
415
1
    return true;
416
1
417
4
  case ArgKind::AK_Matcher:
418
4
    if (!isMatcher())
419
2
      return false;
420
2
    return getMatcher().isConvertibleTo(Kind.getMatcherKind(), Specificity);
421
0
  }
422
0
  llvm_unreachable("Invalid Type");
423
0
}
424
425
bool VariantValue::isConvertibleTo(ArrayRef<ArgKind> Kinds,
426
6
                                   unsigned *Specificity) const {
427
6
  unsigned MaxSpecificity = 0;
428
6
  for (const ArgKind& Kind : Kinds) {
429
6
    unsigned ThisSpecificity;
430
6
    if (!isConvertibleTo(Kind, &ThisSpecificity))
431
3
      continue;
432
3
    MaxSpecificity = std::max(MaxSpecificity, ThisSpecificity);
433
3
  }
434
6
  if (Specificity && MaxSpecificity > 0) {
435
3
    *Specificity = MaxSpecificity;
436
3
  }
437
6
  return MaxSpecificity > 0;
438
6
}
439
440
50
std::string VariantValue::getTypeAsString() const {
441
50
  switch (Type) {
442
9
  case VT_String: return "String";
443
30
  case VT_Matcher: return getMatcher().getTypeAsString();
444
2
  case VT_Boolean: return "Boolean";
445
2
  case VT_Double: return "Double";
446
6
  case VT_Unsigned: return "Unsigned";
447
1
  case VT_Nothing: return "Nothing";
448
0
  }
449
0
  llvm_unreachable("Invalid Type");
450
0
}
451
452
} // end namespace dynamic
453
} // end namespace ast_matchers
454
} // end namespace clang