Skip to content

Commit 17e9569

Browse files
author
kr-2003
committed
added undo command for repl
1 parent 7caf10b commit 17e9569

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,11 @@ namespace Cpp {
781781
unsigned complete_line = 1U,
782782
unsigned complete_column = 1U);
783783

784+
/// Reverts the last N operations performed by the interpreter.
785+
///\param[in] N The number of operations to undo. Defaults to 1.
786+
///\returns 0 on success, non-zero on failure.
787+
CPPINTEROP_API int Undo(unsigned N = 1);
788+
784789
} // end namespace Cpp
785790

786791
#endif // CPPINTEROP_CPPINTEROP_H

lib/Interpreter/CppInterOp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3616,4 +3616,6 @@ namespace Cpp {
36163616
complete_column);
36173617
}
36183618

3619+
int Undo(unsigned N) { return getInterp().undo(N); }
3620+
36193621
} // end namespace Cpp

lib/Interpreter/CppInterOpInterpreter.h

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

432+
CompilationResult undo(unsigned N = 1) {
433+
#ifdef CPPINTEROP_USE_CLING
434+
llvm::logAllUnhandledErrors(Undo(N), llvm::errs(),
435+
"Undo not implemented in Cling");
436+
return kFailure;
437+
#else
438+
if (llvm::Error Err = Undo(N)) {
439+
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(),
440+
"Failed to undo via ::undo: or something");
441+
return kFailure;
442+
}
443+
return kSuccess;
444+
#endif
445+
}
446+
432447
}; // Interpreter
433448
} // namespace Cpp
434449

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,3 +1251,17 @@ TEST(FunctionReflectionTest, Destruct) {
12511251
output = testing::internal::GetCapturedStdout();
12521252
EXPECT_EQ(output, "Destructor Executed");
12531253
}
1254+
1255+
TEST(FunctionReflectionTest, UndoTest) {
1256+
Cpp::CreateInterpreter();
1257+
std::string cerrs;
1258+
testing::internal::CaptureStderr();
1259+
Cpp::Process("int x = 5;");
1260+
cerrs = testing::internal::GetCapturedStderr();
1261+
EXPECT_STREQ(cerrs.c_str(), "");
1262+
Cpp::Undo();
1263+
testing::internal::CaptureStderr();
1264+
Cpp::Process("int x = 10;");
1265+
cerrs = testing::internal::GetCapturedStderr();
1266+
EXPECT_STREQ(cerrs.c_str(), "");
1267+
}

0 commit comments

Comments
 (0)