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

Arthur Eubanks via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 20 15:46:23 PDT 2021


aeubanks created this revision.
aeubanks requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112190

Files:
  clang/lib/Frontend/FrontendAction.cpp
  clang/test/Misc/clear-ast-before-backend-plugins.c


Index: clang/test/Misc/clear-ast-before-backend-plugins.c
===================================================================
--- /dev/null
+++ clang/test/Misc/clear-ast-before-backend-plugins.c
@@ -0,0 +1,8 @@
+// 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
+// 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 --allow-empty
+// REQUIRES: plugins, examples, asserts
+
+// YES: Clearing AST
+// NO-NOT: Clearing AST
+
+void f() {}
\ No newline at end of file
Index: clang/lib/Frontend/FrontendAction.cpp
===================================================================
--- clang/lib/Frontend/FrontendAction.cpp
+++ clang/lib/Frontend/FrontendAction.cpp
@@ -217,8 +217,13 @@
 
   // 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));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112190.381111.patch
Type: text/x-patch
Size: 1637 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211020/183327cf/attachment.bin>


More information about the cfe-commits mailing list