[clang] [Feat] Allow Finding across only parts of an AST. (PR #127423)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 16 17:23:48 PST 2025
https://github.com/matts1 created https://github.com/llvm/llvm-project/pull/127423
This is relevant for clang modules, as they are imported into the AST, but are actually part of a different TU. It can result in hundreds of milliseconds of additional time to also traverse the AST of these modules, and often for no benefit, as they are frequently already traversed in their own TU.
>From fb532e74460c9c6e5830a5a69e1e85aacdb6e358 Mon Sep 17 00:00:00 2001
From: Matt Stark <msta at google.com>
Date: Mon, 17 Feb 2025 12:18:12 +1100
Subject: [PATCH] [Feat] Allow Finding across only parts of an AST.
This is relevant for clang modules, as they are imported into the AST, but are actually part of a different TU.
It can result in hundreds of milliseconds of additional time to also traverse the AST of these modules, and often for no benefit, as they are frequently already traversed in their own TU.
---
clang/include/clang/ASTMatchers/ASTMatchFinder.h | 7 +++++++
clang/lib/ASTMatchers/ASTMatchFinder.cpp | 2 +-
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/ASTMatchers/ASTMatchFinder.h b/clang/include/clang/ASTMatchers/ASTMatchFinder.h
index a387d9037b7da..62d42d9604eff 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchFinder.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchFinder.h
@@ -139,6 +139,13 @@ class MatchFinder {
///
/// It prints a report after match.
std::optional<Profiling> CheckProfiling;
+
+ /// Whether to traverse a Decl. This is relevant for clang modules, as they
+ /// are imported into the AST, but are actually part of a different TU.
+ /// It can result in hundreds of milliseconds of additional time to also
+ /// traverse the AST of these modules, and often for no benefit, as they
+ /// are frequently already traversed in their own TU.
+ std::optional<llvm::function_ref<bool(const Decl *)>> ShouldTraverseDecl;
};
MatchFinder(MatchFinderOptions Options = MatchFinderOptions());
diff --git a/clang/lib/ASTMatchers/ASTMatchFinder.cpp b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
index 3d01a70395a9b..365ffed7f9471 100644
--- a/clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -1443,7 +1443,7 @@ bool MatchASTVisitor::objcClassIsDerivedFrom(
}
bool MatchASTVisitor::TraverseDecl(Decl *DeclNode) {
- if (!DeclNode) {
+ if (!DeclNode || (Options.ShouldTraverseDecl && !(*Options.ShouldTraverseDecl)(DeclNode))) {
return true;
}
More information about the cfe-commits
mailing list