https://github.com/anutosh491 updated 
https://github.com/llvm/llvm-project/pull/169989

>From a6c9ac10aca449bee2fdebd8e0ec9fe7d363d0ad Mon Sep 17 00:00:00 2001
From: anutosh491 <[email protected]>
Date: Sat, 29 Nov 2025 16:41:56 +0530
Subject: [PATCH 1/2] Drop CodeGen module when an input has parse errors

---
 clang/lib/Interpreter/IncrementalAction.cpp | 11 +++++++++++
 clang/lib/Interpreter/IncrementalAction.h   |  2 ++
 clang/lib/Interpreter/IncrementalParser.cpp |  1 +
 3 files changed, 14 insertions(+)

diff --git a/clang/lib/Interpreter/IncrementalAction.cpp 
b/clang/lib/Interpreter/IncrementalAction.cpp
index 3d489fce54bc6..e2b9d13017ada 100644
--- a/clang/lib/Interpreter/IncrementalAction.cpp
+++ b/clang/lib/Interpreter/IncrementalAction.cpp
@@ -120,6 +120,17 @@ std::unique_ptr<llvm::Module> 
IncrementalAction::GenModule() {
   return nullptr;
 }
 
+void IncrementalAction::discardCurrentCodeGenModule() {
+  if (CodeGenerator *CG = getCodeGen()) {
+    if (auto *CurM = CG->GetModule()) {
+      llvm::LLVMContext &Ctx = CurM->getContext();
+      std::string Name = CurM->getName().str();
+      std::unique_ptr<llvm::Module> Dead(CG->ReleaseModule());
+      CG->StartModule(Name, Ctx);
+    }
+  }
+}
+
 CodeGenerator *IncrementalAction::getCodeGen() const {
   FrontendAction *WrappedAct = getWrapped();
   if (!WrappedAct || !WrappedAct->hasIRSupport())
diff --git a/clang/lib/Interpreter/IncrementalAction.h 
b/clang/lib/Interpreter/IncrementalAction.h
index 725cdd0c27cf4..485cfaa45f793 100644
--- a/clang/lib/Interpreter/IncrementalAction.h
+++ b/clang/lib/Interpreter/IncrementalAction.h
@@ -74,6 +74,8 @@ class IncrementalAction : public WrapperFrontendAction {
 
   /// Generate an LLVM module for the most recent parsed input.
   std::unique_ptr<llvm::Module> GenModule();
+
+  void discardCurrentCodeGenModule();
 };
 
 class InProcessPrintingASTConsumer final : public MultiplexConsumer {
diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index bf08911e23533..53379603c26da 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -82,6 +82,7 @@ IncrementalParser::ParseOrWrapTopLevelDecl() {
 
   DiagnosticsEngine &Diags = S.getDiagnostics();
   if (Diags.hasErrorOccurred()) {
+    Act->discardCurrentCodeGenModule();
     CleanUpPTU(C.getTranslationUnitDecl());
 
     Diags.Reset(/*soft=*/true);

>From 7b4302b02d9a2dbc42530dd9e3141c67dd268200 Mon Sep 17 00:00:00 2001
From: anutosh491 <[email protected]>
Date: Mon, 8 Dec 2025 16:49:18 +0530
Subject: [PATCH 2/2] Gaurd HandleTopLevelDecl call to allow codegen only if no
 error has occured

---
 clang/lib/Interpreter/IncrementalAction.cpp | 16 +++++-----------
 clang/lib/Interpreter/IncrementalAction.h   |  2 --
 clang/lib/Interpreter/IncrementalParser.cpp |  1 -
 3 files changed, 5 insertions(+), 14 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalAction.cpp 
b/clang/lib/Interpreter/IncrementalAction.cpp
index e2b9d13017ada..d22031c8fa893 100644
--- a/clang/lib/Interpreter/IncrementalAction.cpp
+++ b/clang/lib/Interpreter/IncrementalAction.cpp
@@ -120,17 +120,6 @@ std::unique_ptr<llvm::Module> 
IncrementalAction::GenModule() {
   return nullptr;
 }
 
-void IncrementalAction::discardCurrentCodeGenModule() {
-  if (CodeGenerator *CG = getCodeGen()) {
-    if (auto *CurM = CG->GetModule()) {
-      llvm::LLVMContext &Ctx = CurM->getContext();
-      std::string Name = CurM->getName().str();
-      std::unique_ptr<llvm::Module> Dead(CG->ReleaseModule());
-      CG->StartModule(Name, Ctx);
-    }
-  }
-}
-
 CodeGenerator *IncrementalAction::getCodeGen() const {
   FrontendAction *WrappedAct = getWrapped();
   if (!WrappedAct || !WrappedAct->hasIRSupport())
@@ -146,6 +135,11 @@ bool 
InProcessPrintingASTConsumer::HandleTopLevelDecl(DeclGroupRef DGR) {
   if (DGR.isNull())
     return true;
 
+  CompilerInstance *CI = Interp.getCompilerInstance();
+  DiagnosticsEngine &Diags = CI->getDiagnostics();
+  if (Diags.hasErrorOccurred())
+    return true;
+
   for (Decl *D : DGR)
     if (auto *TLSD = llvm::dyn_cast<TopLevelStmtDecl>(D))
       if (TLSD && TLSD->isSemiMissing()) {
diff --git a/clang/lib/Interpreter/IncrementalAction.h 
b/clang/lib/Interpreter/IncrementalAction.h
index 485cfaa45f793..725cdd0c27cf4 100644
--- a/clang/lib/Interpreter/IncrementalAction.h
+++ b/clang/lib/Interpreter/IncrementalAction.h
@@ -74,8 +74,6 @@ class IncrementalAction : public WrapperFrontendAction {
 
   /// Generate an LLVM module for the most recent parsed input.
   std::unique_ptr<llvm::Module> GenModule();
-
-  void discardCurrentCodeGenModule();
 };
 
 class InProcessPrintingASTConsumer final : public MultiplexConsumer {
diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index 53379603c26da..bf08911e23533 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -82,7 +82,6 @@ IncrementalParser::ParseOrWrapTopLevelDecl() {
 
   DiagnosticsEngine &Diags = S.getDiagnostics();
   if (Diags.hasErrorOccurred()) {
-    Act->discardCurrentCodeGenModule();
     CleanUpPTU(C.getTranslationUnitDecl());
 
     Diags.Reset(/*soft=*/true);

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to