Skip to content

[clang-repl] #149396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions clang/lib/Interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,10 +752,17 @@ Interpreter::getSymbolAddressFromLinkerName(llvm::StringRef Name) const {

llvm::Error Interpreter::Undo(unsigned N) {

if (N > getEffectivePTUSize())
if (getEffectivePTUSize() == 0) {
return llvm::make_error<llvm::StringError>("Operation failed. "
"Too many undos",
"No input left to undo",
std::error_code());
} else if (N > getEffectivePTUSize()) {
return llvm::make_error<llvm::StringError>(
llvm::formatv("Wanted to undo {0} inputs, only have {1}.",
N, getEffectivePTUSize()),
std::error_code());
}

for (unsigned I = 0; I < N; I++) {
if (IncrExecutor) {
if (llvm::Error Err = IncrExecutor->removeModule(PTUs.back()))
Expand Down
4 changes: 2 additions & 2 deletions clang/unittests/Interpreter/InterpreterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ TEST_F(InterpreterTest, UndoCommand) {

// Fail to undo.
auto Err1 = Interp->Undo();
EXPECT_EQ("Operation failed. Too many undos",
EXPECT_EQ("Operation failed. No input left to undo",
llvm::toString(std::move(Err1)));
auto Err2 = Interp->Parse("int foo = 42;");
EXPECT_TRUE(!!Err2);
auto Err3 = Interp->Undo(2);
EXPECT_EQ("Operation failed. Too many undos",
EXPECT_EQ("Operation failed. No input left to undo",
Copy link
Collaborator

@DavidSpickett DavidSpickett Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The complication here is that although the clang-repl %undo only undoes one input, the internal method can actually undo more than that. This test for example is trying to undo 2 things.

So the code needs to be a bit more intelligent. You have two situations:

  • You have nothing left to undo.
  • You have some things left to undo but want to undo more than that.

In both cases you error because you don't want to partially undo something, but the message should be specific. In the same order as above, I suggest:

  • "No input left to undo"
  • "Wanted to undo X inputs, only have Y" (with the message formatted with the correct numbers)

For formatting the error, we have some utilities to do that, I will find those for you.

Side note: "inputs" might not be the right noun, but stick with it for now and we can ask a clang-repl maintainer whether it's ok.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that the internal method can undo more than one input because you might be piping many lines at once into clang-repl? Anyway it's there so we should assume it is there for a good reason.

Copy link
Collaborator

@DavidSpickett DavidSpickett Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return llvm::make_error<llvm::StringError>(
is an example of formatting an error message.

If you've seen Python's format strings, it works like those, automatically figuring out what the type of the parameters are so that you don't have to %<something> each time like in printf. There's a bit more explanation in https://llvm.org/doxygen/FormatVariadic_8h_source.html but searching for other uses of it will be more informative than the formal grammar of it.

llvm::toString(std::move(Err3)));

// Succeed to undo.
Expand Down