https://github.com/IvyXu420 updated 
https://github.com/llvm/llvm-project/pull/230054

>From 27c0546c666b43bd69603666d86698a63d7b5c23 Mon Sep 17 00:00:00 2001
From: Ivy Xu <[email protected]>
Date: Thu, 8 Oct 2026 17:52:21 +0800
Subject: [PATCH 1/2] Fix clang-repl crashing on unusable top-level
 declarations

---
 clang/lib/Parse/ParseDecl.cpp                   |  4 +++-
 clang/unittests/Interpreter/InterpreterTest.cpp | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index fbecae7b80fb9..a984e1c7ee7d2 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -5840,8 +5840,10 @@ Parser::DeclGroupPtrTy Parser::ParseTopLevelStmtDecl() {
   TopLevelStmtDecl *TLSD = Actions.ActOnStartTopLevelStmtDecl(getCurScope());
   StmtResult R = ParseStatementOrDeclaration(Stmts, SubStmtCtx);
   Actions.ActOnFinishTopLevelStmtDecl(TLSD, R.get());
-  if (!R.isUsable())
+  if (!R.isUsable()) {
     R = Actions.ActOnNullStmt(Tok.getLocation());
+    TLSD->setStmt(R.get());
+  }
 
   if (Tok.is(tok::annot_repl_input_end) &&
       Tok.getAnnotationValue() != nullptr) {
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp 
b/clang/unittests/Interpreter/InterpreterTest.cpp
index 3becd00c12820..8d7c75cdcaab8 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -573,4 +573,18 @@ TEST_F(InterpreterTest, TranslationUnit_CanonicalDecl) {
             sema.getASTContext().getTranslationUnitDecl()->getCanonicalDecl());
 }
 
+TEST_F(InterpreterTest, UnusableTopLevelDecl) {
+  std::vector<const char *> Args;
+  std::unique_ptr<Interpreter> Interp = createInterpreter(Args);
+
+  Value V;
+
+  llvm::cantFail(Interp->ParseAndExecute("return;", &V));
+
+  EXPECT_FALSE(V.isValid());
+  EXPECT_FALSE(V.hasValue());
+  ASSERT_EQ(V.getKind(), Value::K_Unspecified);
+  EXPECT_FALSE(V.isManuallyAlloc());
+}
+
 } // end anonymous namespace

>From 93a383e8102fa812efdec5dc89abc0c50da62430 Mon Sep 17 00:00:00 2001
From: Ivy Xu <[email protected]>
Date: Thu, 8 Oct 2026 19:25:26 +0800
Subject: [PATCH 2/2] Fix formatting

---
 clang/unittests/Interpreter/InterpreterTest.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp 
b/clang/unittests/Interpreter/InterpreterTest.cpp
index 0411286c0960f..3bd210f41d786 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -608,6 +608,7 @@ TEST_F(InterpreterTest, UnusableTopLevelDecl) {
   EXPECT_FALSE(V.hasValue());
   ASSERT_EQ(V.getKind(), Value::K_Unspecified);
   EXPECT_FALSE(V.isManuallyAlloc());
+}
 
 TEST_F(InterpreterTest, EmscriptenExceptionHandling) {
 #ifndef __EMSCRIPTEN__

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

Reply via email to