[clang] 2dcad77 - [clang] Don't clear AST if we have consumers running after the main action

Arthur Eubanks via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 21 09:04:29 PDT 2021


Author: Arthur Eubanks
Date: 2021-10-21T09:03:57-07:00
New Revision: 2dcad7754a204d5dbb78fef8f6b13cd005456e33

URL: https://github.com/llvm/llvm-project/commit/2dcad7754a204d5dbb78fef8f6b13cd005456e33
DIFF: https://github.com/llvm/llvm-project/commit/2dcad7754a204d5dbb78fef8f6b13cd005456e33.diff

LOG: [clang] Don't clear AST if we have consumers running after the main action

Downstream users may have Clang plugins. By default these plugins run
after the main action if they are specified on the command line.

Since these plugins are ASTConsumers, presumably they inspect the AST.
So we shouldn't clear it if any plugins run after the main action.

Reviewed By: dblaikie, hans

Differential Revision: https://reviews.llvm.org/D112190

Added: 
    clang/test/Misc/clear-ast-before-backend-plugins.c

Modified: 
    clang/lib/Frontend/FrontendAction.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp
index b56db78d3e71a..089f40b36089a 100644
--- a/clang/lib/Frontend/FrontendAction.cpp
+++ b/clang/lib/Frontend/FrontendAction.cpp
@@ -217,8 +217,13 @@ FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
 
   // Add to Consumers the main consumer, then all the plugins that go after it
   Consumers.push_back(std::move(Consumer));
-  for (auto &C : AfterConsumers) {
-    Consumers.push_back(std::move(C));
+  if (!AfterConsumers.empty()) {
+    // If we have plugins after the main consumer, which may be the codegen
+    // action, they likely will need the ASTContext, so don't clear it in the
+    // codegen action.
+    CI.getCodeGenOpts().ClearASTBeforeBackend = false;
+    for (auto &C : AfterConsumers)
+      Consumers.push_back(std::move(C));
   }
 
   return std::make_unique<MultiplexConsumer>(std::move(Consumers));

diff  --git a/clang/test/Misc/clear-ast-before-backend-plugins.c b/clang/test/Misc/clear-ast-before-backend-plugins.c
new file mode 100644
index 0000000000000..c00bee4450d98
--- /dev/null
+++ b/clang/test/Misc/clear-ast-before-backend-plugins.c
@@ -0,0 +1,9 @@
+// REQUIRES: plugins, examples, asserts
+
+// RUN: %clang_cc1 -mllvm -debug-only=codegenaction -clear-ast-before-backend -emit-obj -o /dev/null -load %llvmshlibdir/PrintFunctionNames%pluginext %s 2>&1 | FileCheck %s --check-prefix=YES
+// YES: Clearing AST
+
+// RUN: %clang_cc1 -mllvm -debug-only=codegenaction -clear-ast-before-backend -emit-obj -o /dev/null -load %llvmshlibdir/PrintFunctionNames%pluginext -add-plugin print-fns -plugin-arg-print-fns help %s 2>&1 | FileCheck %s --check-prefix=NO
+// NO-NOT: Clearing AST
+
+void f() {}


        


More information about the cfe-commits mailing list