Skip to content

Commit 9a3cb00

Browse files
kr-2003anutosh491
authored andcommitted
added undo command for repl
1 parent eccc90a commit 9a3cb00

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
@@ -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/CppInterOp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3672,4 +3672,6 @@ namespace Cpp {
36723672
complete_column);
36733673
}
36743674

3675+
int Undo(unsigned N) { return getInterp().undo(N); }
3676+
36753677
} // 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
@@ -1517,3 +1517,17 @@ TEST(FunctionReflectionTest, Destruct) {
15171517
output = testing::internal::GetCapturedStdout();
15181518
EXPECT_EQ(output, "Destructor Executed");
15191519
}
1520+
1521+
TEST(FunctionReflectionTest, UndoTest) {
1522+
Cpp::CreateInterpreter();
1523+
std::string cerrs;
1524+
testing::internal::CaptureStderr();
1525+
Cpp::Process("int x = 5;");
1526+
cerrs = testing::internal::GetCapturedStderr();
1527+
EXPECT_STREQ(cerrs.c_str(), "");
1528+
Cpp::Undo();
1529+
testing::internal::CaptureStderr();
1530+
Cpp::Process("int x = 10;");
1531+
cerrs = testing::internal::GetCapturedStderr();
1532+
EXPECT_STREQ(cerrs.c_str(), "");
1533+
}

0 commit comments

Comments
 (0)