Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Tooling/Transformer/Stencil.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Stencil.cpp - Stencil implementation -------------------*- 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
#include "clang/Tooling/Transformer/Stencil.h"
10
#include "clang/AST/ASTContext.h"
11
#include "clang/AST/ASTTypeTraits.h"
12
#include "clang/AST/Expr.h"
13
#include "clang/ASTMatchers/ASTMatchFinder.h"
14
#include "clang/ASTMatchers/ASTMatchers.h"
15
#include "clang/Basic/SourceLocation.h"
16
#include "clang/Lex/Lexer.h"
17
#include "clang/Tooling/Transformer/SourceCode.h"
18
#include "clang/Tooling/Transformer/SourceCodeBuilders.h"
19
#include "llvm/ADT/SmallVector.h"
20
#include "llvm/ADT/Twine.h"
21
#include "llvm/Support/Errc.h"
22
#include "llvm/Support/Error.h"
23
#include <atomic>
24
#include <memory>
25
#include <string>
26
27
using namespace clang;
28
using namespace transformer;
29
30
using ast_matchers::BoundNodes;
31
using ast_matchers::MatchFinder;
32
using llvm::errc;
33
using llvm::Error;
34
using llvm::Expected;
35
using llvm::StringError;
36
37
static llvm::Expected<DynTypedNode> getNode(const BoundNodes &Nodes,
38
6
                                            StringRef Id) {
39
6
  auto &NodesMap = Nodes.getMap();
40
6
  auto It = NodesMap.find(Id);
41
6
  if (It == NodesMap.end())
42
0
    return llvm::make_error<llvm::StringError>(llvm::errc::invalid_argument,
43
0
                                               "Id not bound: " + Id);
44
6
  return It->second;
45
6
}
46
47
static Error printNode(StringRef Id, const MatchFinder::MatchResult &Match,
48
6
                       std::string *Result) {
49
6
  std::string Output;
50
6
  llvm::raw_string_ostream Os(Output);
51
6
  auto NodeOrErr = getNode(Match.Nodes, Id);
52
6
  if (auto Err = NodeOrErr.takeError())
53
0
    return Err;
54
6
  NodeOrErr->print(Os, PrintingPolicy(Match.Context->getLangOpts()));
55
6
  *Result += Os.str();
56
6
  return Error::success();
57
6
}
58
59
// FIXME: Consider memoizing this function using the `ASTContext`.
60
11
static bool isSmartPointerType(QualType Ty, ASTContext &Context) {
61
11
  using namespace ::clang::ast_matchers;
62
63
  // Optimization: hard-code common smart-pointer types. This can/should be
64
  // removed if we start caching the results of this function.
65
11
  auto KnownSmartPointer =
66
11
      cxxRecordDecl(hasAnyName("::std::unique_ptr", "::std::shared_ptr"));
67
11
  const auto QuacksLikeASmartPointer = cxxRecordDecl(
68
11
      hasMethod(cxxMethodDecl(hasOverloadedOperatorName("->"),
69
11
                              returns(qualType(pointsTo(type()))))),
70
11
      hasMethod(cxxMethodDecl(hasOverloadedOperatorName("*"),
71
11
                              returns(qualType(references(type()))))));
72
11
  const auto SmartPointer = qualType(hasDeclaration(
73
11
      cxxRecordDecl(anyOf(KnownSmartPointer, QuacksLikeASmartPointer))));
74
11
  return match(SmartPointer, Ty, Context).size() > 0;
75
11
}
76
77
// Identifies use of `operator*` on smart pointers, and returns the underlying
78
// smart-pointer expression; otherwise, returns null.
79
4
static const Expr *isSmartDereference(const Expr &E, ASTContext &Context) {
80
4
  using namespace ::clang::ast_matchers;
81
82
4
  const auto HasOverloadedArrow = cxxRecordDecl(hasMethod(cxxMethodDecl(
83
4
      hasOverloadedOperatorName("->"), returns(qualType(pointsTo(type()))))));
84
  // Verify it is a smart pointer by finding `operator->` in the class
85
  // declaration.
86
4
  auto Deref = cxxOperatorCallExpr(
87
4
      hasOverloadedOperatorName("*"), hasUnaryOperand(expr().bind("arg")),
88
4
      callee(cxxMethodDecl(ofClass(HasOverloadedArrow))));
89
4
  return selectFirst<Expr>("arg", match(Deref, E, Context));
90
4
}
91
92
namespace {
93
// An arbitrary fragment of code within a stencil.
94
class RawTextStencil : public StencilInterface {
95
  std::string Text;
96
97
public:
98
149
  explicit RawTextStencil(std::string T) : Text(std::move(T)) {}
99
100
24
  std::string toString() const override {
101
24
    std::string Result;
102
24
    llvm::raw_string_ostream OS(Result);
103
24
    OS << "\"";
104
24
    OS.write_escaped(Text);
105
24
    OS << "\"";
106
24
    OS.flush();
107
24
    return Result;
108
24
  }
109
110
  Error eval(const MatchFinder::MatchResult &Match,
111
162
             std::string *Result) const override {
112
162
    Result->append(Text);
113
162
    return Error::success();
114
162
  }
115
};
116
117
// A debugging operation to dump the AST for a particular (bound) AST node.
118
class DebugPrintNodeStencil : public StencilInterface {
119
  std::string Id;
120
121
public:
122
1
  explicit DebugPrintNodeStencil(std::string S) : Id(std::move(S)) {}
123
124
1
  std::string toString() const override {
125
1
    return (llvm::Twine("dPrint(\"") + Id + "\")").str();
126
1
  }
127
128
  Error eval(const MatchFinder::MatchResult &Match,
129
0
             std::string *Result) const override {
130
0
    return printNode(Id, Match, Result);
131
0
  }
132
};
133
134
// Operators that take a single node Id as an argument.
135
enum class UnaryNodeOperator {
136
  Parens,
137
  Deref,
138
  MaybeDeref,
139
  AddressOf,
140
  MaybeAddressOf,
141
  Describe,
142
};
143
144
// Generic container for stencil operations with a (single) node-id argument.
145
class UnaryOperationStencil : public StencilInterface {
146
  UnaryNodeOperator Op;
147
  std::string Id;
148
149
public:
150
  UnaryOperationStencil(UnaryNodeOperator Op, std::string Id)
151
33
      : Op(Op), Id(std::move(Id)) {}
152
153
4
  std::string toString() const override {
154
4
    StringRef OpName;
155
4
    switch (Op) {
156
1
    case UnaryNodeOperator::Parens:
157
1
      OpName = "expression";
158
1
      break;
159
1
    case UnaryNodeOperator::Deref:
160
1
      OpName = "deref";
161
1
      break;
162
0
    case UnaryNodeOperator::MaybeDeref:
163
0
      OpName = "maybeDeref";
164
0
      break;
165
1
    case UnaryNodeOperator::AddressOf:
166
1
      OpName = "addressOf";
167
1
      break;
168
0
    case UnaryNodeOperator::MaybeAddressOf:
169
0
      OpName = "maybeAddressOf";
170
0
      break;
171
1
    case UnaryNodeOperator::Describe:
172
1
      OpName = "describe";
173
1
      break;
174
4
    }
175
4
    return (OpName + "(\"" + Id + "\")").str();
176
4
  }
177
178
  Error eval(const MatchFinder::MatchResult &Match,
179
29
             std::string *Result) const override {
180
    // The `Describe` operation can be applied to any node, not just
181
    // expressions, so it is handled here, separately.
182
29
    if (Op == UnaryNodeOperator::Describe)
183
6
      return printNode(Id, Match, Result);
184
185
23
    const auto *E = Match.Nodes.getNodeAs<Expr>(Id);
186
23
    if (E == nullptr)
187
1
      return llvm::make_error<StringError>(errc::invalid_argument,
188
1
                                           "Id not bound or not Expr: " + Id);
189
22
    llvm::Optional<std::string> Source;
190
22
    switch (Op) {
191
3
    case UnaryNodeOperator::Parens:
192
3
      Source = tooling::buildParens(*E, *Match.Context);
193
3
      break;
194
3
    case UnaryNodeOperator::Deref:
195
3
      Source = tooling::buildDereference(*E, *Match.Context);
196
3
      break;
197
7
    case UnaryNodeOperator::MaybeDeref:
198
7
      if (E->getType()->isAnyPointerType() ||
199
7
          
isSmartPointerType(E->getType(), *Match.Context)3
) {
200
        // Strip off any operator->. This can only occur inside an actual arrow
201
        // member access, so we treat it as equivalent to an actual object
202
        // expression.
203
6
        if (const auto *OpCall = dyn_cast<clang::CXXOperatorCallExpr>(E)) {
204
1
          if (OpCall->getOperator() == clang::OO_Arrow &&
205
1
              OpCall->getNumArgs() == 1) {
206
1
            E = OpCall->getArg(0);
207
1
          }
208
1
        }
209
6
        Source = tooling::buildDereference(*E, *Match.Context);
210
6
        break;
211
6
      }
212
1
      *Result += tooling::getText(*E, *Match.Context);
213
1
      return Error::success();
214
4
    case UnaryNodeOperator::AddressOf:
215
4
      Source = tooling::buildAddressOf(*E, *Match.Context);
216
4
      break;
217
5
    case UnaryNodeOperator::MaybeAddressOf:
218
5
      if (E->getType()->isAnyPointerType() ||
219
5
          
isSmartPointerType(E->getType(), *Match.Context)3
) {
220
        // Strip off any operator->. This can only occur inside an actual arrow
221
        // member access, so we treat it as equivalent to an actual object
222
        // expression.
223
3
        if (const auto *OpCall = dyn_cast<clang::CXXOperatorCallExpr>(E)) {
224
1
          if (OpCall->getOperator() == clang::OO_Arrow &&
225
1
              OpCall->getNumArgs() == 1) {
226
1
            E = OpCall->getArg(0);
227
1
          }
228
1
        }
229
3
        *Result += tooling::getText(*E, *Match.Context);
230
3
        return Error::success();
231
3
      }
232
2
      Source = tooling::buildAddressOf(*E, *Match.Context);
233
2
      break;
234
0
    case UnaryNodeOperator::Describe:
235
0
      llvm_unreachable("This case is handled at the start of the function");
236
22
    }
237
18
    if (!Source)
238
0
      return llvm::make_error<StringError>(
239
0
          errc::invalid_argument,
240
0
          "Could not construct expression source from ID: " + Id);
241
18
    *Result += *Source;
242
18
    return Error::success();
243
18
  }
244
};
245
246
// The fragment of code corresponding to the selected range.
247
class SelectorStencil : public StencilInterface {
248
  RangeSelector Selector;
249
250
public:
251
19
  explicit SelectorStencil(RangeSelector S) : Selector(std::move(S)) {}
252
253
2
  std::string toString() const override { return "selection(...)"; }
254
255
  Error eval(const MatchFinder::MatchResult &Match,
256
44
             std::string *Result) const override {
257
44
    auto RawRange = Selector(Match);
258
44
    if (!RawRange)
259
1
      return RawRange.takeError();
260
43
    CharSourceRange Range = Lexer::makeFileCharRange(
261
43
        *RawRange, *Match.SourceManager, Match.Context->getLangOpts());
262
43
    if (Range.isInvalid()) {
263
      // Validate the original range to attempt to get a meaningful error
264
      // message. If it's valid, then something else is the cause and we just
265
      // return the generic failure message.
266
3
      if (auto Err =
267
3
              tooling::validateEditRange(*RawRange, *Match.SourceManager))
268
3
        return handleErrors(std::move(Err), [](std::unique_ptr<StringError> E) {
269
3
          assert(E->convertToErrorCode() ==
270
3
                     llvm::make_error_code(errc::invalid_argument) &&
271
3
                 "Validation errors must carry the invalid_argument code");
272
0
          return llvm::createStringError(
273
3
              errc::invalid_argument,
274
3
              "selected range could not be resolved to a valid source range; " +
275
3
                  E->getMessage());
276
3
        });
277
0
      return llvm::createStringError(
278
0
          errc::invalid_argument,
279
0
          "selected range could not be resolved to a valid source range");
280
3
    }
281
    // Validate `Range`, because `makeFileCharRange` accepts some ranges that
282
    // `validateEditRange` rejects.
283
40
    if (auto Err = tooling::validateEditRange(Range, *Match.SourceManager))
284
0
      return joinErrors(
285
0
          llvm::createStringError(errc::invalid_argument,
286
0
                                  "selected range is not valid for editing"),
287
0
          std::move(Err));
288
40
    *Result += tooling::getText(Range, *Match.Context);
289
40
    return Error::success();
290
40
  }
291
};
292
293
// A stencil operation to build a member access `e.m` or `e->m`, as appropriate.
294
class AccessStencil : public StencilInterface {
295
  std::string BaseId;
296
  Stencil Member;
297
298
public:
299
  AccessStencil(StringRef BaseId, Stencil Member)
300
18
      : BaseId(std::string(BaseId)), Member(std::move(Member)) {}
301
302
8
  std::string toString() const override {
303
8
    return (llvm::Twine("access(\"") + BaseId + "\", " + Member->toString() +
304
8
            ")")
305
8
        .str();
306
8
  }
307
308
  Error eval(const MatchFinder::MatchResult &Match,
309
10
             std::string *Result) const override {
310
10
    const auto *E = Match.Nodes.getNodeAs<Expr>(BaseId);
311
10
    if (E == nullptr)
312
0
      return llvm::make_error<StringError>(errc::invalid_argument,
313
0
                                           "Id not bound: " + BaseId);
314
10
    if (!E->isImplicitCXXThis()) {
315
9
      llvm::Optional<std::string> S;
316
9
      if (E->getType()->isAnyPointerType() ||
317
9
          
isSmartPointerType(E->getType(), *Match.Context)5
) {
318
        // Strip off any operator->. This can only occur inside an actual arrow
319
        // member access, so we treat it as equivalent to an actual object
320
        // expression.
321
5
        if (const auto *OpCall = dyn_cast<clang::CXXOperatorCallExpr>(E)) {
322
1
          if (OpCall->getOperator() == clang::OO_Arrow &&
323
1
              OpCall->getNumArgs() == 1) {
324
1
            E = OpCall->getArg(0);
325
1
          }
326
1
        }
327
5
        S = tooling::buildArrow(*E, *Match.Context);
328
5
      } else 
if (const auto *4
Operand4
= isSmartDereference(*E, *Match.Context)) {
329
        // `buildDot` already handles the built-in dereference operator, so we
330
        // only need to catch overloaded `operator*`.
331
1
        S = tooling::buildArrow(*Operand, *Match.Context);
332
3
      } else {
333
3
        S = tooling::buildDot(*E, *Match.Context);
334
3
      }
335
9
      if (S.hasValue())
336
9
        *Result += *S;
337
0
      else
338
0
        return llvm::make_error<StringError>(
339
0
            errc::invalid_argument,
340
0
            "Could not construct object text from ID: " + BaseId);
341
9
    }
342
10
    return Member->eval(Match, Result);
343
10
  }
344
};
345
346
class IfBoundStencil : public StencilInterface {
347
  std::string Id;
348
  Stencil TrueStencil;
349
  Stencil FalseStencil;
350
351
public:
352
  IfBoundStencil(StringRef Id, Stencil TrueStencil, Stencil FalseStencil)
353
      : Id(std::string(Id)), TrueStencil(std::move(TrueStencil)),
354
5
        FalseStencil(std::move(FalseStencil)) {}
355
356
3
  std::string toString() const override {
357
3
    return (llvm::Twine("ifBound(\"") + Id + "\", " + TrueStencil->toString() +
358
3
            ", " + FalseStencil->toString() + ")")
359
3
        .str();
360
3
  }
361
362
  Error eval(const MatchFinder::MatchResult &Match,
363
2
             std::string *Result) const override {
364
2
    auto &M = Match.Nodes.getMap();
365
2
    return (M.find(Id) != M.end() ? 
TrueStencil1
:
FalseStencil1
)
366
2
        ->eval(Match, Result);
367
2
  }
368
};
369
370
class SelectBoundStencil : public clang::transformer::StencilInterface {
371
  static bool containsNoNullStencils(
372
7
      const std::vector<std::pair<std::string, Stencil>> &Cases) {
373
7
    for (const auto &S : Cases)
374
18
      if (S.second == nullptr)
375
0
        return false;
376
7
    return true;
377
7
  }
378
379
public:
380
  SelectBoundStencil(std::vector<std::pair<std::string, Stencil>> Cases,
381
                     Stencil Default)
382
7
      : CaseStencils(std::move(Cases)), DefaultStencil(std::move(Default)) {
383
7
    assert(containsNoNullStencils(CaseStencils) &&
384
7
           "cases of selectBound may not be null");
385
7
  }
386
7
  ~SelectBoundStencil() override{};
387
388
  llvm::Error eval(const MatchFinder::MatchResult &match,
389
4
                   std::string *result) const override {
390
4
    const BoundNodes::IDToNodeMap &NodeMap = match.Nodes.getMap();
391
8
    for (const auto &S : CaseStencils) {
392
8
      if (NodeMap.count(S.first) > 0) {
393
2
        return S.second->eval(match, result);
394
2
      }
395
8
    }
396
397
2
    if (DefaultStencil != nullptr) {
398
1
      return DefaultStencil->eval(match, result);
399
1
    }
400
401
1
    llvm::SmallVector<llvm::StringRef, 2> CaseIDs;
402
1
    CaseIDs.reserve(CaseStencils.size());
403
1
    for (const auto &S : CaseStencils)
404
4
      CaseIDs.emplace_back(S.first);
405
406
1
    return llvm::createStringError(
407
1
        errc::result_out_of_range,
408
1
        llvm::Twine("selectBound failed: no cases bound and no default: {") +
409
1
            llvm::join(CaseIDs, ", ") + "}");
410
2
  }
411
412
3
  std::string toString() const override {
413
3
    std::string Buffer;
414
3
    llvm::raw_string_ostream Stream(Buffer);
415
3
    Stream << "selectBound({";
416
3
    bool First = true;
417
5
    for (const auto &S : CaseStencils) {
418
5
      if (First)
419
3
        First = false;
420
2
      else
421
2
        Stream << "}, ";
422
5
      Stream << "{\"" << S.first << "\", " << S.second->toString();
423
5
    }
424
3
    Stream << "}}";
425
3
    if (DefaultStencil != nullptr) {
426
1
      Stream << ", " << DefaultStencil->toString();
427
1
    }
428
3
    Stream << ")";
429
3
    return Stream.str();
430
3
  }
431
432
private:
433
  std::vector<std::pair<std::string, Stencil>> CaseStencils;
434
  Stencil DefaultStencil;
435
};
436
437
class SequenceStencil : public StencilInterface {
438
  std::vector<Stencil> Stencils;
439
440
public:
441
  SequenceStencil(std::vector<Stencil> Stencils)
442
11
      : Stencils(std::move(Stencils)) {}
443
444
4
  std::string toString() const override {
445
4
    llvm::SmallVector<std::string, 2> Parts;
446
4
    Parts.reserve(Stencils.size());
447
4
    for (const auto &S : Stencils)
448
10
      Parts.push_back(S->toString());
449
4
    return (llvm::Twine("seq(") + llvm::join(Parts, ", ") + ")").str();
450
4
  }
451
452
  Error eval(const MatchFinder::MatchResult &Match,
453
24
             std::string *Result) const override {
454
24
    for (const auto &S : Stencils)
455
94
      if (auto Err = S->eval(Match, Result))
456
3
        return Err;
457
21
    return Error::success();
458
24
  }
459
};
460
461
class RunStencil : public StencilInterface {
462
  MatchConsumer<std::string> Consumer;
463
464
public:
465
2
  explicit RunStencil(MatchConsumer<std::string> C) : Consumer(std::move(C)) {}
466
467
1
  std::string toString() const override { return "run(...)"; }
468
469
  Error eval(const MatchFinder::MatchResult &Match,
470
1
             std::string *Result) const override {
471
472
1
    Expected<std::string> Value = Consumer(Match);
473
1
    if (!Value)
474
0
      return Value.takeError();
475
1
    *Result += *Value;
476
1
    return Error::success();
477
1
  }
478
};
479
} // namespace
480
481
149
Stencil transformer::detail::makeStencil(StringRef Text) {
482
149
  return std::make_shared<RawTextStencil>(std::string(Text));
483
149
}
484
485
19
Stencil transformer::detail::makeStencil(RangeSelector Selector) {
486
19
  return std::make_shared<SelectorStencil>(std::move(Selector));
487
19
}
488
489
1
Stencil transformer::dPrint(StringRef Id) {
490
1
  return std::make_shared<DebugPrintNodeStencil>(std::string(Id));
491
1
}
492
493
5
Stencil transformer::expression(llvm::StringRef Id) {
494
5
  return std::make_shared<UnaryOperationStencil>(UnaryNodeOperator::Parens,
495
5
                                                 std::string(Id));
496
5
}
497
498
4
Stencil transformer::deref(llvm::StringRef ExprId) {
499
4
  return std::make_shared<UnaryOperationStencil>(UnaryNodeOperator::Deref,
500
4
                                                 std::string(ExprId));
501
4
}
502
503
7
Stencil transformer::maybeDeref(llvm::StringRef ExprId) {
504
7
  return std::make_shared<UnaryOperationStencil>(UnaryNodeOperator::MaybeDeref,
505
7
                                                 std::string(ExprId));
506
7
}
507
508
5
Stencil transformer::addressOf(llvm::StringRef ExprId) {
509
5
  return std::make_shared<UnaryOperationStencil>(UnaryNodeOperator::AddressOf,
510
5
                                                 std::string(ExprId));
511
5
}
512
513
5
Stencil transformer::maybeAddressOf(llvm::StringRef ExprId) {
514
5
  return std::make_shared<UnaryOperationStencil>(
515
5
      UnaryNodeOperator::MaybeAddressOf, std::string(ExprId));
516
5
}
517
518
7
Stencil transformer::describe(StringRef Id) {
519
7
  return std::make_shared<UnaryOperationStencil>(UnaryNodeOperator::Describe,
520
7
                                                 std::string(Id));
521
7
}
522
523
18
Stencil transformer::access(StringRef BaseId, Stencil Member) {
524
18
  return std::make_shared<AccessStencil>(BaseId, std::move(Member));
525
18
}
526
527
Stencil transformer::ifBound(StringRef Id, Stencil TrueStencil,
528
5
                             Stencil FalseStencil) {
529
5
  return std::make_shared<IfBoundStencil>(Id, std::move(TrueStencil),
530
5
                                          std::move(FalseStencil));
531
5
}
532
533
Stencil transformer::selectBound(
534
    std::vector<std::pair<std::string, Stencil>> CaseStencils,
535
7
    Stencil DefaultStencil) {
536
7
  return std::make_shared<SelectBoundStencil>(std::move(CaseStencils),
537
7
                                              std::move(DefaultStencil));
538
7
}
539
540
2
Stencil transformer::run(MatchConsumer<std::string> Fn) {
541
2
  return std::make_shared<RunStencil>(std::move(Fn));
542
2
}
543
544
131
Stencil transformer::catVector(std::vector<Stencil> Parts) {
545
  // Only one argument, so don't wrap in sequence.
546
131
  if (Parts.size() == 1)
547
120
    return std::move(Parts[0]);
548
11
  return std::make_shared<SequenceStencil>(std::move(Parts));
549
131
}