[clang] [clang] Share RecursiveASTVisitor for AST listing (PR #202661)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 9 07:50:04 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: David Zbarsky (dzbarsky)
<details>
<summary>Changes</summary>
ASTDeclNodeLister and ASTPrinter instantiate nearly identical RecursiveASTVisitor specializations. This duplicates the full generated traversal implementation in clang.
Add a List mode to ASTPrinter and use its existing traversal for CreateASTDeclNodeLister(). VisitNamedDecl preserves the previous qualified-name output while AST dumping and printing retain their existing paths.
In an arm64 release build of current main, standalone clang shrinks from 107,676,664 to 107,440,760 bytes (-235,904, -0.219%). Its stripped size falls by 132,976 bytes, and ASTConsumers.cpp.o falls from 597,248 to 310,544 bytes (-286,704).
The -ast-list output is byte-identical, and filtered AST dumps are identical after normalizing process-specific node addresses. Across 30 paired measurements, compile instructions changed by +0.016%, -ast-list by +0.021%, and filtered-dump by -0.111%; all 95% confidence intervals include zero.
Work towards #<!-- -->202616
---
Full diff: https://github.com/llvm/llvm-project/pull/202661.diff
1 Files Affected:
- (modified) clang/lib/Frontend/ASTConsumers.cpp (+17-25)
``````````diff
diff --git a/clang/lib/Frontend/ASTConsumers.cpp b/clang/lib/Frontend/ASTConsumers.cpp
index 67c8761511e0c..91a5963c316f7 100644
--- a/clang/lib/Frontend/ASTConsumers.cpp
+++ b/clang/lib/Frontend/ASTConsumers.cpp
@@ -30,7 +30,7 @@ namespace {
typedef RecursiveASTVisitor<ASTPrinter> base;
public:
- enum Kind { DumpFull, Dump, Print, None };
+ enum Kind { DumpFull, Dump, Print, List, None };
ASTPrinter(std::unique_ptr<raw_ostream> Out, Kind K,
ASTDumpOutputFormat Format, StringRef FilterString,
bool DumpLookups = false, bool DumpDeclTypes = false)
@@ -48,6 +48,11 @@ namespace {
void HandleTranslationUnit(ASTContext &Context) override {
TranslationUnitDecl *D = Context.getTranslationUnitDecl();
+ if (OutputKind == List) {
+ TraverseDecl(D);
+ return;
+ }
+
if (FilterString.empty())
return print(D);
@@ -57,7 +62,7 @@ namespace {
bool shouldWalkTypesOfTypeLocs() const { return false; }
bool TraverseDecl(Decl *D) {
- if (D && filterMatches(D)) {
+ if (OutputKind != List && D && filterMatches(D)) {
bool ShowColors = Out.has_colors();
if (ShowColors)
Out.changeColor(raw_ostream::BLUE);
@@ -76,6 +81,14 @@ namespace {
return base::TraverseDecl(D);
}
+ bool VisitNamedDecl(NamedDecl *D) {
+ if (OutputKind == List) {
+ D->printQualifiedName(Out);
+ Out << '\n';
+ }
+ return true;
+ }
+
private:
std::string getName(Decl *D) {
if (isa<NamedDecl>(D))
@@ -140,28 +153,6 @@ namespace {
/// Whether to dump the type for each declaration dumped.
bool DumpDeclTypes;
};
-
- class ASTDeclNodeLister : public ASTConsumer,
- public RecursiveASTVisitor<ASTDeclNodeLister> {
- public:
- ASTDeclNodeLister(raw_ostream *Out = nullptr)
- : Out(Out ? *Out : llvm::outs()) {}
-
- void HandleTranslationUnit(ASTContext &Context) override {
- TraverseDecl(Context.getTranslationUnitDecl());
- }
-
- bool shouldWalkTypesOfTypeLocs() const { return false; }
-
- bool VisitNamedDecl(NamedDecl *D) {
- D->printQualifiedName(Out);
- Out << '\n';
- return true;
- }
-
- private:
- raw_ostream &Out;
- };
} // end anonymous namespace
std::unique_ptr<ASTConsumer>
@@ -197,7 +188,8 @@ clang::CreateASTDumper(raw_ostream &Out, StringRef FilterString, bool DumpDecls,
}
std::unique_ptr<ASTConsumer> clang::CreateASTDeclNodeLister() {
- return std::make_unique<ASTDeclNodeLister>(nullptr);
+ return std::make_unique<ASTPrinter>(llvm::outs(), ASTPrinter::List,
+ ADOF_Default, StringRef());
}
//===----------------------------------------------------------------------===//
``````````
</details>
https://github.com/llvm/llvm-project/pull/202661
More information about the cfe-commits
mailing list