diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -5311,9 +5311,13 @@ def disable_free : Flag<["-"], "disable-free">, HelpText<"Disable freeing of memory on exit">, MarshallingInfoFlag>; -def clear_ast_before_backend : Flag<["-"], "clear-ast-before-backend">, - HelpText<"Clear the Clang AST before running backend code generation">, - MarshallingInfoFlag>; +defm clear_ast_before_backend : BoolOption<"", + "clear-ast-before-backend", + CodeGenOpts<"ClearASTBeforeBackend">, + DefaultFalse, + PosFlag, + NegFlag, + BothFlags<[], " the Clang AST before running backend code generation">>; def enable_noundef_analysis : Flag<["-"], "enable-noundef-analysis">, Group, HelpText<"Enable analyzing function argument and return types for mandatory definedness">, MarshallingInfoFlag>; diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp --- a/clang/lib/CodeGen/CodeGenAction.cpp +++ b/clang/lib/CodeGen/CodeGenAction.cpp @@ -52,6 +52,8 @@ using namespace clang; using namespace llvm; +#define DEBUG_TYPE "codegenaction" + namespace clang { class BackendConsumer; class ClangDiagnosticHandler final : public DiagnosticHandler { @@ -352,6 +354,7 @@ } if (CodeGenOpts.ClearASTBeforeBackend) { + LLVM_DEBUG(llvm::dbgs() << "Clearing AST...\n"); // Access to the AST is no longer available after this. // Other things that the ASTContext manages are still available, e.g. // the SourceManager. It'd be nice if we could separate out all the diff --git a/clang/test/Misc/clear-ast-before-backend.c b/clang/test/Misc/clear-ast-before-backend.c --- a/clang/test/Misc/clear-ast-before-backend.c +++ b/clang/test/Misc/clear-ast-before-backend.c @@ -1,9 +1,14 @@ -// RUN: %clang_cc1 -clear-ast-before-backend %s -emit-obj -o /dev/null -O1 -// RUN: %clang_cc1 -clear-ast-before-backend %s -emit-obj -o /dev/null -print-stats 2>&1 | FileCheck %s +// REQUIRES: asserts -// CHECK: *** Decl Stats: -// CHECK: {{.*}} decls total -// CHECK: 1 Function decls -// CHECK: Total bytes = +// RUN: %clang_cc1 -mllvm -debug-only=codegenaction -clear-ast-before-backend %s -emit-obj -o /dev/null -O1 2>&1 | FileCheck %s --check-prefix=YES +// RUN: %clang_cc1 -mllvm -debug-only=codegenaction -clear-ast-before-backend -no-clear-ast-before-backend %s -emit-obj -o /dev/null -O1 2>&1 | FileCheck %s --allow-empty --check-prefix=NO +// RUN: %clang_cc1 -clear-ast-before-backend %s -emit-obj -o /dev/null -print-stats 2>&1 | FileCheck %s --check-prefix=STATS + +// YES: Clearing AST +// NO-NOT: Clearing AST +// STATS: *** Decl Stats: +// STATS: {{.*}} decls total +// STATS: 1 Function decls +// STATS: Total bytes = void f() {}