================ @@ -186,106 +212,193 @@ class MatchDescendantVisitor : public DynamicRecursiveASTVisitor { return DynamicRecursiveASTVisitor::TraverseStmt(Node); } + void setASTContext(ASTContext &Context) { ActiveASTContext = &Context; } + + void setHandler(const UnsafeBufferUsageHandler &NewHandler) { + Handler = &NewHandler; + } + private: // Sets 'Matched' to true if 'Matcher' matches 'Node' // // Returns 'true' if traversal should continue after this function // returns, i.e. if no match is found or 'Bind' is 'BK_All'. template <typename T> bool match(const T &Node) { - internal::BoundNodesTreeBuilder RecursiveBuilder(*Builder); - - if (Matcher->matches(DynTypedNode::create(Node), Finder, - &RecursiveBuilder)) { - ResultBindings.addMatch(RecursiveBuilder); + if (Matcher->matches(DynTypedNode::create(Node), *ActiveASTContext, + *Handler)) { Matches = true; - if (Bind != internal::ASTMatchFinder::BK_All) + if (!FindAll) return false; // Abort as soon as a match is found. } return true; } - const internal::DynTypedMatcher *const Matcher; - internal::ASTMatchFinder *const Finder; - internal::BoundNodesTreeBuilder *const Builder; - internal::BoundNodesTreeBuilder ResultBindings; - const internal::ASTMatchFinder::BindKind Bind; + FastMatcher *const Matcher; + // When true, finds all matches. When false, finds the first match and stops. + const bool FindAll; bool Matches; bool ignoreUnevaluatedContext; + ASTContext *ActiveASTContext; + const UnsafeBufferUsageHandler *Handler; }; // Because we're dealing with raw pointers, let's define what we mean by that. -static auto hasPointerType() { - return hasType(hasCanonicalType(pointerType())); +static bool hasPointerType(const Expr &E) { + return isa<PointerType>(E.getType().getCanonicalType()); } -static auto hasArrayType() { return hasType(hasCanonicalType(arrayType())); } - -AST_MATCHER_P(Stmt, forEachDescendantEvaluatedStmt, internal::Matcher<Stmt>, - innerMatcher) { - const DynTypedMatcher &DTM = static_cast<DynTypedMatcher>(innerMatcher); - - MatchDescendantVisitor Visitor(&DTM, Finder, Builder, ASTMatchFinder::BK_All, - true); - return Visitor.findMatch(DynTypedNode::create(Node)); +static bool hasArrayType(const Expr &E) { + return isa<ArrayType>(E.getType().getCanonicalType()); } -AST_MATCHER_P(Stmt, forEachDescendantStmt, internal::Matcher<Stmt>, - innerMatcher) { - const DynTypedMatcher &DTM = static_cast<DynTypedMatcher>(innerMatcher); +static void +forEachDescendantEvaluatedStmt(const Stmt *S, ASTContext &Ctx, + const UnsafeBufferUsageHandler &Handler, + FastMatcher &Matcher) { + MatchDescendantVisitor Visitor(Matcher, /*FindAll=*/true, + /*ignoreUnevaluatedContext=*/true); + Visitor.setASTContext(Ctx); + Visitor.setHandler(Handler); ---------------- ziqingluo-90 wrote:
nit: Why can't `Ctx` and `Handler` be set at construction? https://github.com/llvm/llvm-project/pull/125492 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits