[clang] [clang-repl] Optionally capture the value of `x; ` (PR #240109)

Jonas Rembser via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 8 01:06:01 PST 2026


https://github.com/guitargeek created https://github.com/llvm/llvm-project/pull/240109

ParseAndExecute(Code, &V) only returns the value of an expression statement without a trailing semicolon:
```
  Interp->ParseAndExecute("x", &V);    // V holds the value of x
  Interp->ParseAndExecute("x;", &V);   // V stays empty
```

Clients that need the value of an input like `&f;` had to remove the semicolon; ROOT's TROOT::ProcessLine() returns that value. That changes how an input is parsed: `std::map<int, std::string> m;` is a declaration, but an invalid expression without the semicolon. So the client has to parse such an input twice and hide the diagnostics of the first attempt. That attempt also leaves template instantiations behind that never reach CodeGen, which #228330 fixes separately.

Add Interpreter::setCaptureValueOfExprStmts(), which makes value capture also apply to expression statements with a semicolon. Statements that are not expressions, like declarations and loops, are unaffected. It is off by default, so clang-repl's printing doesn't change.

Assisted-by: Claude Opus 5.5

This is another PR that addresses a problem when using Clang-Repl instead of Cling in ROOT.

>From 81c72a0e760ce462b4be6b019f49f4501861fff7 Mon Sep 17 00:00:00 2001
From: Jonas Rembser <jonas.rembser at cern.ch>
Date: Thu, 1 Oct 2026 20:17:29 +0000
Subject: [PATCH] [clang-repl] Optionally capture the value of `x;`

ParseAndExecute(Code, &V) only returns the value of an expression
statement without a trailing semicolon (`x`). Clients that need the value
of an input like `&f;` (ROOT's TROOT::ProcessLine() returns it) had to
remove the semicolon. That changes how an input is parsed: an input that
is a declaration with the semicolon (`std::map<int, std::string> m;`)
becomes an invalid expression without it, so the client has to parse it
twice and hide the diagnostics of the first attempt.

Add Interpreter::setCaptureValueOfExprStmts(), which makes value capture
also apply to expression statements with a semicolon. Off by default, so
clang-repl's printing is unchanged.

Assisted-by: Claude Opus 5.5
---
 clang/include/clang/Interpreter/Interpreter.h | 13 ++++++++
 clang/lib/Interpreter/IncrementalAction.cpp   |  4 ++-
 .../unittests/Interpreter/InterpreterTest.cpp | 31 +++++++++++++++++++
 3 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h
index 5228b7fe80bd32..ce976899c7bd4b 100644
--- a/clang/include/clang/Interpreter/Interpreter.h
+++ b/clang/include/clang/Interpreter/Interpreter.h
@@ -130,6 +130,9 @@ class Interpreter {
   // printing happens, it's in an invalid state.
   Value LastValue;
 
+  /// See setCaptureValueOfExprStmts().
+  bool CaptureValueOfExprStmts = false;
+
   /// Compiler instance performing the incremental compilation.
   std::unique_ptr<CompilerInstance> CI;
 
@@ -178,6 +181,16 @@ class Interpreter {
   /// Undo N previous incremental inputs.
   llvm::Error Undo(unsigned N = 1);
 
+  /// By default, only the value of an expression statement without a trailing
+  /// semicolon (`x`) is captured. Also capture it for expression statements
+  /// with a semicolon (`x;`), so that clients get the value of an input like
+  /// `&f;` from ParseAndExecute(). Removing the semicolon instead can change
+  /// how an input is parsed.
+  void setCaptureValueOfExprStmts(bool Capture) {
+    CaptureValueOfExprStmts = Capture;
+  }
+  bool capturesValueOfExprStmts() const { return CaptureValueOfExprStmts; }
+
   /// Link a dynamic library
   llvm::Error LoadDynamicLibrary(const char *name);
 
diff --git a/clang/lib/Interpreter/IncrementalAction.cpp b/clang/lib/Interpreter/IncrementalAction.cpp
index 2c1ad404a3e7d7..caa5e726d9c8ad 100644
--- a/clang/lib/Interpreter/IncrementalAction.cpp
+++ b/clang/lib/Interpreter/IncrementalAction.cpp
@@ -144,7 +144,9 @@ bool InProcessPrintingASTConsumer::HandleTopLevelDecl(DeclGroupRef DGR) {
 
   for (Decl *D : DGR)
     if (auto *TLSD = llvm::dyn_cast<TopLevelStmtDecl>(D))
-      if (TLSD && TLSD->isSemiMissing()) {
+      if (TLSD &&
+          (TLSD->isSemiMissing() || Interp.capturesValueOfExprStmts()) &&
+          isa<Expr>(TLSD->getStmt())) {
         auto ExprOrErr = Interp.convertExprToValue(cast<Expr>(TLSD->getStmt()));
         if (llvm::Error E = ExprOrErr.takeError()) {
           llvm::logAllUnhandledErrors(std::move(E), llvm::errs(),
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp
index 41c1facc8f2f08..e9c76ad1dd3fcb 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -688,4 +688,35 @@ TEST_F(InterpreterTest, EmscriptenExceptionHandling) {
       Interp->ParseAndExecute("try { throw 1; } catch (...) { 0; }"));
 }
 
+TEST_F(InterpreterTest, CaptureValueOfExprStmts) {
+  std::unique_ptr<Interpreter> Interp = createInterpreter();
+  cantFail(Interp->ParseAndExecute("int x = 42;"));
+
+  Value V;
+  cantFail(Interp->ParseAndExecute("x;", &V));
+  EXPECT_FALSE(V.isValid());
+
+  Interp->setCaptureValueOfExprStmts(true);
+  cantFail(Interp->ParseAndExecute("x;", &V));
+  ASSERT_TRUE(V.isValid());
+  EXPECT_EQ(V.getInt(), 42);
+
+  Value Addr;
+  cantFail(Interp->ParseAndExecute("&x;", &Addr));
+  ASSERT_TRUE(Addr.isValid());
+  EXPECT_TRUE(Addr.getType()->isPointerType());
+  EXPECT_EQ(*static_cast<int *>(Addr.getPtr()), 42);
+
+  // Statements that are not expressions have no value; declarations are
+  // unaffected.
+  Value None;
+  cantFail(
+      Interp->ParseAndExecute("for (int i = 0; i < 2; ++i) x += i;", &None));
+  EXPECT_FALSE(None.isValid());
+  cantFail(Interp->ParseAndExecute("int y = x;", &None));
+  EXPECT_FALSE(None.isValid());
+  cantFail(Interp->ParseAndExecute("y;", &V));
+  EXPECT_EQ(V.getInt(), 43);
+}
+
 } // end anonymous namespace



More information about the cfe-commits mailing list