Skip to content

Commit b9256d7

Browse files
kr-2003Abhinav Kumar
andauthored
Added undo command for CppInterOp (#513)
* added undo command for repl * added undo command for repl * added check for cling in CppInterOp.cpp * added undo test for n > 1 * added unload feature for cling * clang-format changes * fixed c-api for undo * test coverage * test coverage * test coverage * skipped undo test for emscripten build * skipped undo test for emscripten build * using Cpp::Process ret val * removed testing undo on first transac * removed test for windows --------- Co-authored-by: Abhinav Kumar <[email protected]>
1 parent a22df96 commit b9256d7

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-1
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,11 @@ namespace Cpp {
785785
unsigned complete_line = 1U,
786786
unsigned complete_column = 1U);
787787

788+
/// Reverts the last N operations performed by the interpreter.
789+
///\param[in] N The number of operations to undo. Defaults to 1.
790+
///\returns 0 on success, non-zero on failure.
791+
CPPINTEROP_API int Undo(unsigned N = 1);
792+
788793
} // end namespace Cpp
789794

790795
#endif // CPPINTEROP_CPPINTEROP_H

lib/Interpreter/CXCppInterOp.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ TInterp_t clang_Interpreter_takeInterpreterAsPtr(CXInterpreter I) {
306306

307307
enum CXErrorCode clang_Interpreter_undo(CXInterpreter I, unsigned int N) {
308308
#ifdef CPPINTEROP_USE_CLING
309-
return CXError_Failure;
309+
auto* interp = getInterpreter(I);
310+
cling::Interpreter::PushTransactionRAII RAII(interp);
311+
interp->unload(N);
312+
return CXError_Success;
310313
#else
311314
return getInterpreter(I)->Undo(N) ? CXError_Failure : CXError_Success;
312315
#endif // CPPINTEROP_USE_CLING

lib/Interpreter/CppInterOp.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3684,4 +3684,15 @@ namespace Cpp {
36843684
complete_column);
36853685
}
36863686

3687+
int Undo(unsigned N) {
3688+
#ifdef CPPINTEROP_USE_CLING
3689+
auto& I = getInterp();
3690+
cling::Interpreter::PushTransactionRAII RAII(&I);
3691+
I.unload(N);
3692+
return compat::Interpreter::kSuccess;
3693+
#else
3694+
return getInterp().undo(N);
3695+
#endif
3696+
}
3697+
36873698
} // end namespace Cpp

lib/Interpreter/CppInterOpInterpreter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,15 @@ class Interpreter {
429429
return ret; // TODO: Implement
430430
}
431431

432+
CompilationResult undo(unsigned N = 1) {
433+
if (llvm::Error Err = Undo(N)) {
434+
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(),
435+
"Failed to undo via ::undo");
436+
return kFailure;
437+
}
438+
return kSuccess;
439+
}
440+
432441
}; // Interpreter
433442
} // namespace Cpp
434443

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,3 +1615,30 @@ TEST(FunctionReflectionTest, Destruct) {
16151615
clang_Interpreter_takeInterpreterAsPtr(I);
16161616
clang_Interpreter_dispose(I);
16171617
}
1618+
1619+
TEST(FunctionReflectionTest, UndoTest) {
1620+
#ifdef _WIN32
1621+
GTEST_SKIP() << "Disabled on Windows. Needs fixing.";
1622+
#endif
1623+
#ifdef EMSCRIPTEN
1624+
GTEST_SKIP() << "Test fails for Emscipten builds";
1625+
#else
1626+
Cpp::CreateInterpreter();
1627+
EXPECT_EQ(Cpp::Process("int a = 5;"), 0);
1628+
EXPECT_EQ(Cpp::Process("int b = 10;"), 0);
1629+
EXPECT_EQ(Cpp::Process("int x = 5;"), 0);
1630+
Cpp::Undo();
1631+
EXPECT_NE(Cpp::Process("int y = x;"), 0);
1632+
EXPECT_EQ(Cpp::Process("int x = 10;"), 0);
1633+
EXPECT_EQ(Cpp::Process("int y = 10;"), 0);
1634+
Cpp::Undo(2);
1635+
EXPECT_EQ(Cpp::Process("int x = 20;"), 0);
1636+
EXPECT_EQ(Cpp::Process("int y = 20;"), 0);
1637+
int ret = Cpp::Undo(100);
1638+
#ifdef CPPINTEROP_USE_CLING
1639+
EXPECT_EQ(ret, 0);
1640+
#else
1641+
EXPECT_EQ(ret, 1);
1642+
#endif
1643+
#endif
1644+
}

0 commit comments

Comments
 (0)