https://github.com/dzbarsky updated https://github.com/llvm/llvm-project/pull/202661
>From dad6d72781e7c78c7a9411780b85616d28fd0c82 Mon Sep 17 00:00:00 2001 From: David Zbarsky <[email protected]> Date: Tue, 9 Jun 2026 09:39:17 -0400 Subject: [PATCH] [clang] Share RecursiveASTVisitor for AST listing 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. --- clang/lib/Frontend/ASTConsumers.cpp | 49 ++++++++++++++--------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/clang/lib/Frontend/ASTConsumers.cpp b/clang/lib/Frontend/ASTConsumers.cpp index 67c8761511e0c..02b1cf17905b6 100644 --- a/clang/lib/Frontend/ASTConsumers.cpp +++ b/clang/lib/Frontend/ASTConsumers.cpp @@ -30,7 +30,14 @@ namespace { typedef RecursiveASTVisitor<ASTPrinter> base; public: - enum Kind { DumpFull, Dump, Print, None }; + enum Kind { + DumpFull, + Dump, + Print, + // Traverse the complete AST and print each NamedDecl's qualified name. + List, + None + }; ASTPrinter(std::unique_ptr<raw_ostream> Out, Kind K, ASTDumpOutputFormat Format, StringRef FilterString, bool DumpLookups = false, bool DumpDeclTypes = false) @@ -48,6 +55,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 +69,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 +88,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 +160,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 +195,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()); } //===----------------------------------------------------------------------===// _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
