|
8 | 8 |
|
9 | 9 | #include "clang/Analysis/Analyses/UnsafeBufferUsage.h"
|
10 | 10 | #include "clang/ASTMatchers/ASTMatchFinder.h"
|
| 11 | +#include "clang/AST/RecursiveASTVisitor.h" |
11 | 12 | #include "llvm/ADT/SmallVector.h"
|
12 | 13 |
|
13 | 14 | using namespace llvm;
|
14 | 15 | using namespace clang;
|
15 | 16 | using namespace ast_matchers;
|
16 | 17 |
|
| 18 | +namespace clang::ast_matchers::internal { |
| 19 | +// A `RecursiveASTVisitor` that traverses all descendants of a given node "n" |
| 20 | +// except for those belonging to a different callable of "n". |
| 21 | +class MatchDescendantVisitor |
| 22 | + : public RecursiveASTVisitor<MatchDescendantVisitor> { |
| 23 | +public: |
| 24 | + typedef RecursiveASTVisitor<MatchDescendantVisitor> VisitorBase; |
| 25 | + |
| 26 | + // Creates an AST visitor that matches `Matcher` on all |
| 27 | + // descendants of a given node "n" except for the ones |
| 28 | + // belonging to a different callable of "n". |
| 29 | + MatchDescendantVisitor(const DynTypedMatcher *Matcher, ASTMatchFinder *Finder, |
| 30 | + BoundNodesTreeBuilder *Builder, |
| 31 | + ASTMatchFinder::BindKind Bind) |
| 32 | + : Matcher(Matcher), Finder(Finder), Builder(Builder), Bind(Bind), |
| 33 | + Matches(false) {} |
| 34 | + |
| 35 | + // Returns true if a match is found in a subtree of `DynNode`, which belongs |
| 36 | + // to the same callable of `DynNode`. |
| 37 | + bool findMatch(const DynTypedNode &DynNode) { |
| 38 | + Matches = false; |
| 39 | + if (const Stmt *StmtNode = DynNode.get<Stmt>()) { |
| 40 | + TraverseStmt(const_cast<Stmt *>(StmtNode)); |
| 41 | + *Builder = ResultBindings; |
| 42 | + return Matches; |
| 43 | + } |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + // The following are overriding methods from the base visitor class. |
| 48 | + // They are public only to allow CRTP to work. They are *not *part |
| 49 | + // of the public API of this class. |
| 50 | + |
| 51 | + // For the matchers so far used in safe buffers, we only need to match |
| 52 | + // `Stmt`s. To override more as needed. |
| 53 | + |
| 54 | + bool TraverseDecl(Decl *Node) { |
| 55 | + if (!Node) |
| 56 | + return true; |
| 57 | + if (!match(*Node)) |
| 58 | + return false; |
| 59 | + // To skip callables: |
| 60 | + if (isa<FunctionDecl, BlockDecl, ObjCMethodDecl>(Node)) |
| 61 | + return true; |
| 62 | + // Traverse descendants |
| 63 | + return VisitorBase::TraverseDecl(Node); |
| 64 | + } |
| 65 | + |
| 66 | + bool TraverseStmt(Stmt *Node, DataRecursionQueue *Queue = nullptr) { |
| 67 | + if (!Node) |
| 68 | + return true; |
| 69 | + if (!match(*Node)) |
| 70 | + return false; |
| 71 | + // To skip callables: |
| 72 | + if (isa<LambdaExpr>(Node)) |
| 73 | + return true; |
| 74 | + return VisitorBase::TraverseStmt(Node); |
| 75 | + } |
| 76 | + |
| 77 | + bool shouldVisitTemplateInstantiations() const { return true; } |
| 78 | + bool shouldVisitImplicitCode() const { |
| 79 | + // TODO: let's ignore implicit code for now |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | +private: |
| 84 | + // Sets 'Matched' to true if 'Matcher' matches 'Node' |
| 85 | + // |
| 86 | + // Returns 'true' if traversal should continue after this function |
| 87 | + // returns, i.e. if no match is found or 'Bind' is 'BK_All'. |
| 88 | + template <typename T> bool match(const T &Node) { |
| 89 | + BoundNodesTreeBuilder RecursiveBuilder(*Builder); |
| 90 | + |
| 91 | + if (Matcher->matches(DynTypedNode::create(Node), Finder, |
| 92 | + &RecursiveBuilder)) { |
| 93 | + ResultBindings.addMatch(RecursiveBuilder); |
| 94 | + Matches = true; |
| 95 | + if (Bind != ASTMatchFinder::BK_All) |
| 96 | + return false; // Abort as soon as a match is found. |
| 97 | + } |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + const DynTypedMatcher *const Matcher; |
| 102 | + ASTMatchFinder *const Finder; |
| 103 | + BoundNodesTreeBuilder *const Builder; |
| 104 | + BoundNodesTreeBuilder ResultBindings; |
| 105 | + const ASTMatchFinder::BindKind Bind; |
| 106 | + bool Matches; |
| 107 | +}; |
| 108 | + |
| 109 | +AST_MATCHER_P(Stmt, forEveryDescendant, Matcher<Stmt>, innerMatcher) { |
| 110 | + MatchDescendantVisitor Visitor(new DynTypedMatcher(innerMatcher), Finder, |
| 111 | + Builder, ASTMatchFinder::BK_All); |
| 112 | + return Visitor.findMatch(DynTypedNode::create(Node)); |
| 113 | +} |
| 114 | +} // namespace clang::ast_matchers::internal |
| 115 | + |
17 | 116 | namespace {
|
18 | 117 | // Because the analysis revolves around variables and their types, we'll need to
|
19 | 118 | // track uses of variables (aka DeclRefExprs).
|
@@ -349,7 +448,7 @@ static std::pair<GadgetList, DeclUseTracker> findGadgets(const Decl *D) {
|
349 | 448 |
|
350 | 449 | // clang-format off
|
351 | 450 | M.addMatcher(
|
352 |
| - stmt(forEachDescendant( |
| 451 | + stmt(forEveryDescendant( |
353 | 452 | stmt(anyOf(
|
354 | 453 | // Add Gadget::matcher() for every gadget in the registry.
|
355 | 454 | #define GADGET(x) \
|
|
0 commit comments