[clang] [clang-repl] Expose markUserCodeStart() in extended Interpreter interface (PR #87064)

Stefan Gränitz via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 29 06:10:34 PDT 2024


https://github.com/weliveindetail created https://github.com/llvm/llvm-project/pull/87064

Adding code for built-in functionality during initialization is very common. Call this function afterwards to hide it from Undo. Any serious interpreter needs it.

>From e924da63f1e926f7a7008f661730b1fd478818c6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20Gr=C3=A4nitz?= <stefan.graenitz at gmail.com>
Date: Tue, 26 Mar 2024 15:31:04 +0100
Subject: [PATCH] [clang-repl] Expose markUserCodeStart() in extended
 Interpreter interface

---
 clang/include/clang/Interpreter/Interpreter.h |  7 ++++--
 .../Interpreter/InterpreterExtensionsTest.cpp | 22 +++++++++++++++----
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h
index 970e0245417b51..b21a95009918dc 100644
--- a/clang/include/clang/Interpreter/Interpreter.h
+++ b/clang/include/clang/Interpreter/Interpreter.h
@@ -44,7 +44,7 @@ class IncrementalParser;
 /// Create a pre-configured \c CompilerInstance for incremental processing.
 class IncrementalCompilerBuilder {
 public:
-  IncrementalCompilerBuilder() {}
+  IncrementalCompilerBuilder() = default;
 
   void SetCompilerArgs(const std::vector<const char *> &Args) {
     UserArgs = Args;
@@ -122,6 +122,10 @@ class Interpreter {
   // JIT engine. In particular, it doesn't run cleanup or destructors.
   void ResetExecutor();
 
+  // Adding code for built-in functionality during initialization is very
+  // common. Call this function afterwards to hide it from Undo.
+  void markUserCodeStart();
+
   // Lazily construct the RuntimeInterfaceBuilder. The provided instance will be
   // used for the entire lifetime of the interpreter. The default implementation
   // targets the in-process __clang_Interpreter runtime. Override this to use a
@@ -184,7 +188,6 @@ class Interpreter {
 
 private:
   size_t getEffectivePTUSize() const;
-  void markUserCodeStart();
 
   llvm::DenseMap<CXXRecordDecl *, llvm::orc::ExecutorAddr> Dtors;
 
diff --git a/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp b/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp
index 1ba865a79ed778..bb39946a31a1d3 100644
--- a/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterExtensionsTest.cpp
@@ -66,10 +66,9 @@ struct LLVMInitRAII {
   ~LLVMInitRAII() { llvm::llvm_shutdown(); }
 } LLVMInit;
 
-class TestCreateResetExecutor : public Interpreter {
+class TestInterpreter : public Interpreter {
 public:
-  TestCreateResetExecutor(std::unique_ptr<CompilerInstance> CI,
-                          llvm::Error &Err)
+  TestInterpreter(std::unique_ptr<CompilerInstance> CI, llvm::Error &Err)
       : Interpreter(std::move(CI), Err) {}
 
   llvm::Error testCreateJITBuilderError() {
@@ -83,6 +82,7 @@ class TestCreateResetExecutor : public Interpreter {
   }
 
   void resetExecutor() { Interpreter::ResetExecutor(); }
+  void markUserCodeStart() { Interpreter::markUserCodeStart(); }
 
 private:
   llvm::Expected<std::unique_ptr<llvm::orc::LLJITBuilder>>
@@ -95,6 +95,20 @@ class TestCreateResetExecutor : public Interpreter {
   std::unique_ptr<llvm::orc::LLJITBuilder> JB;
 };
 
+TEST(InterpreterExtensionsTest, MarkUserCodeStart) {
+  clang::IncrementalCompilerBuilder CB;
+  llvm::Error ErrOut = llvm::Error::success();
+  TestInterpreter Interp(cantFail(CB.CreateCpp()), ErrOut);
+  cantFail(std::move(ErrOut));
+  llvm::Error Parse = Interp.Parse("int builtin = 42;").takeError();
+  EXPECT_THAT_ERROR(std::move(Parse), llvm::Succeeded());
+  // Hide above PTU from Undo
+  Interp.markUserCodeStart();
+  llvm::Error Undo = Interp.Undo(1);
+  EXPECT_THAT_ERROR(std::move(Undo), llvm::FailedWithMessage(
+                                         "Operation failed. Too many undos"));
+}
+
 #ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT
 TEST(InterpreterExtensionsTest, DISABLED_ExecutorCreateReset) {
 #else
@@ -106,7 +120,7 @@ TEST(InterpreterExtensionsTest, ExecutorCreateReset) {
 
   clang::IncrementalCompilerBuilder CB;
   llvm::Error ErrOut = llvm::Error::success();
-  TestCreateResetExecutor Interp(cantFail(CB.CreateCpp()), ErrOut);
+  TestInterpreter Interp(cantFail(CB.CreateCpp()), ErrOut);
   cantFail(std::move(ErrOut));
   EXPECT_THAT_ERROR(Interp.testCreateJITBuilderError(),
                     llvm::FailedWithMessage("TestError"));



More information about the cfe-commits mailing list