[clang] [clang-repl] Optionally capture the value of `x; ` (PR #240109)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 8 01:06:36 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Jonas Rembser (guitargeek)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/240109.diff
3 Files Affected:
- (modified) clang/include/clang/Interpreter/Interpreter.h (+13)
- (modified) clang/lib/Interpreter/IncrementalAction.cpp (+3-1)
- (modified) clang/unittests/Interpreter/InterpreterTest.cpp (+31)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/240109
More information about the cfe-commits
mailing list