Skip to content

Conversation

@kr-2003
Copy link
Contributor

@kr-2003 kr-2003 commented Mar 3, 2025

Description

Added Undo command for Clang-REPL. Reverts the last N operations performed by the interpreter.

Type of change

  • New feature

Testing

  1. Tests in unittests/CppInterOp/FunctionReflectionTest.cpp.
  2. Demo - https://res.cloudinary.com/abhi9av/video/upload/v1740997292/wqgfbgqefrwpx2o7ko95.mp4

Checklist

  • I have read the contribution guide recently

@anutosh491
Copy link
Collaborator

anutosh491 commented Mar 3, 2025

Hi,

@kr-2003 and I were interested in supporting %undo through xeus-cpp/xeus-cpp-lite but we realize it is not being exposed through cppinterop.

Hence I asked him to add the same here.

@anutosh491
Copy link
Collaborator

Weird that we found it being exposed through the libclang inspired C API but not through the standard one.

enum CXErrorCode clang_Interpreter_undo(CXInterpreter I, unsigned int N) {
#ifdef CPPINTEROP_USE_CLING
return CXError_Failure;
#else
return getInterpreter(I)->Undo(N) ? CXError_Failure : CXError_Success;
#endif // CPPINTEROP_USE_CLING
}

@codecov
Copy link

codecov bot commented Mar 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 75.46%. Comparing base (a22df96) to head (77489d7).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #513      +/-   ##
==========================================
+ Coverage   75.33%   75.46%   +0.13%     
==========================================
  Files           9        9              
  Lines        3620     3628       +8     
==========================================
+ Hits         2727     2738      +11     
+ Misses        893      890       -3     
Files with missing lines Coverage Δ
include/clang/Interpreter/CppInterOp.h 96.29% <ø> (ø)
lib/Interpreter/CXCppInterOp.cpp 47.27% <ø> (ø)
lib/Interpreter/CppInterOp.cpp 83.07% <100.00%> (+0.01%) ⬆️
lib/Interpreter/CppInterOpInterpreter.h 80.32% <100.00%> (+1.23%) ⬆️

... and 1 file with indirect coverage changes

Files with missing lines Coverage Δ
include/clang/Interpreter/CppInterOp.h 96.29% <ø> (ø)
lib/Interpreter/CXCppInterOp.cpp 47.27% <ø> (ø)
lib/Interpreter/CppInterOp.cpp 83.07% <100.00%> (+0.01%) ⬆️
lib/Interpreter/CppInterOpInterpreter.h 80.32% <100.00%> (+1.23%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 3, 2025

clang-tidy review says "All clean, LGTM! 👍"

Copy link
Collaborator

@Vipul-Cariappa Vipul-Cariappa left a comment

Choose a reason for hiding this comment

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

@anutosh491 asked me to look at this.

LGTM! If you can just update the test with the changes I specified.

@kr-2003 kr-2003 changed the title Added undo command for Clang-REPL Added undo command for CppInterOp Mar 3, 2025
@kr-2003
Copy link
Contributor Author

kr-2003 commented Mar 4, 2025

@vgvassilev I have included both cling and clang-REPL for undo feature. All tests are passing successfully. Is there any other work pending?

@anutosh491
Copy link
Collaborator

I was actually hopeful we could have this before/in 1.6.0 (which was released on the same day we started discussing undo in xeus-cpp and lite 😅 . We actually made good progress here as xeus-cpp would just naturally have it and I was able to get undo to quite some extent for lite too)

Can we have it through a patch release (1.6.1) or something ?

@anutosh491 anutosh491 requested a review from mcbarton March 4, 2025 10:28
@vgvassilev
Copy link
Contributor

I was actually hopeful we could have this before/in 1.6.0 (which was released on the same day we started discussing undo in xeus-cpp and lite 😅 . We actually made good progress here as xeus-cpp would just naturally have it and I was able to get undo to quite some extent for lite too)

Can we have it through a patch release (1.6.1) or something ?

We do not foresee a patch version for now. It will wait for the next shipping vehicle.

@anutosh491
Copy link
Collaborator

anutosh491 commented Mar 4, 2025

It will wait for the next shipping vehicle.

No issues. Let's move it in if we consider it ready though ?

Also hopefully till the next release, I shall have enough context on how to get this working through lite. I was able to implement it (and if you go through the video carefully you will see the flaw in the video)

Screen.Recording.2025-02-28.at.12.02.54.PM.mp4

Have been discussing why this goes wrong with Sam Clegg who says symbols from subsequent dlopen() operations don't override existing global symbols from previous dlopen() calls. So yeah an error in emscripten is what we think this is !

enum CXErrorCode clang_Interpreter_undo(CXInterpreter I, unsigned int N) {
#ifdef CPPINTEROP_USE_CLING
return CXError_Failure;
auto* interp = getInterpreter(I);
Copy link
Collaborator

Choose a reason for hiding this comment

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

If using clang_repl you can return CXError_Failure if it fails. With cling and the c api currently as it stands you cannot. Can you fix this @kr-2003

@mcbarton
Copy link
Collaborator

mcbarton commented Mar 4, 2025

Codecov Report

Attention: Patch coverage is 75.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 72.54%. Comparing base (7caf10b) to head (87114a5).

Files with missing lines Patch % Lines
lib/Interpreter/CppInterOpInterpreter.h 66.66% 2 Missing ⚠️
Additional details and impacted files

@kr-2003 can you update the tests so your tests cover the 2 missing lines from the patch?

#ifdef CPPINTEROP_USE_CLING
auto& I = getInterp();
cling::Interpreter::PushTransactionRAII RAII(&I);
I.unload(N);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vgvassilev How do I even check if unload function logs any errors, since its a void function? Should I use try-catch block(but it is disabled in the project) or use system level redirection of the output to stream using pipes(this might be an overkill for capturing)? unload function logs cling::errs(), if it fails to run.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vgvassilev Could you please guide me on how to proceed here?

Copy link
Contributor

Choose a reason for hiding this comment

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

You should assume that the operation was successful.

Copy link
Collaborator

@anutosh491 anutosh491 Mar 7, 2025

Choose a reason for hiding this comment

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

Copy link
Collaborator

@anutosh491 anutosh491 left a comment

Choose a reason for hiding this comment

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

I think this looks okay to me.

Maybe the two lines that are not being covered (see comment below) can be addressed.

#513 (comment)

This demands for a negation case (testing a case where undo fails)
Probably can be done through trying undo(1000) when only few operations have taken place. Let's try something relevant out and then move this in ?

@kr-2003
Copy link
Contributor Author

kr-2003 commented Mar 10, 2025

@mcbarton Test coverage review has been addressed. Can we take this in now?

Cpp::Process("int x = 5;");
cerrs = testing::internal::GetCapturedStderr();
EXPECT_STREQ(cerrs.c_str(), "");
Cpp::Undo();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe unloading this first transaction here is problematic for destructors, and is unsupported by Cling, which can be seen in the error thrown in the logs. We should drop this call to Cpp::Undo()

Copy link
Collaborator

Choose a reason for hiding this comment

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

@kr-2003 can you address this review comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i added two more instructions before calling the first Cpp::Undo().

@mcbarton
Copy link
Collaborator

mcbarton commented Mar 14, 2025

@kr-2003 can you rebase? Now that we have automated wasm tests we need to check that the new test passes for an Emscripten build.

@mcbarton
Copy link
Collaborator

@anutosh491 Given you want this PR in can you help @kr-2003 fix the test which fails for the Emscripten case?

@anutosh491
Copy link
Collaborator

anutosh491 commented Mar 17, 2025

Perfect. So undo isn't expected to work with clang-repl-lite untill emscripten supports dlclose correctly.

Abhinav only run these tests when emscripten is not defined

So

#ifndef EMSCRIPTEN
  place your tests here
#endif

or if you place this construct inside your test, you can do

TEST(FunctionReflectionTest, UndoTest) {
#ifdef EMSCRIPTEN
  GTEST_SKIP() << "Currently not implemented while running clang-repl in the broswer";
#endif

@kr-2003
Copy link
Contributor Author

kr-2003 commented Mar 19, 2025

@mcbarton
https://github.com/compiler-research/CppInterOp/actions/runs/13938614873/job/39011414822?pr=513#step:8:623
All tests are passing but after that the workflow is exiting with code -1 in windows. Can you help me out here?

@mcbarton
Copy link
Collaborator

@mcbarton https://github.com/compiler-research/CppInterOp/actions/runs/13938614873/job/39011414822?pr=513#step:8:623 All tests are passing but after that the workflow is exiting with code -1 in windows. Can you help me out here?

@kr-2003 I suspect (but won't know for sure until you skip the section of the test causing this message) that you are getting the error from here https://github.com/compiler-research/CppInterOp/actions/runs/13938614873/job/39011414822?pr=513#step:8:408 . Basically almost every test which causes the interpreter to write anything to the screen in Windows is currently skipped. Its a known issue, which we are hoping will get fixed soon. Whether this PR is acceptable with this test skipped on Windows will not be upto me though. You would need the approval of @vgvassilev or maybe @aaronj0 .

@kr-2003
Copy link
Contributor Author

kr-2003 commented Mar 19, 2025

@mcbarton https://github.com/compiler-research/CppInterOp/actions/runs/13938614873/job/39011414822?pr=513#step:8:623 All tests are passing but after that the workflow is exiting with code -1 in windows. Can you help me out here?

@kr-2003 I suspect (but won't know for sure until you skip the section of the test causing this message) that you are getting the error from here https://github.com/compiler-research/CppInterOp/actions/runs/13938614873/job/39011414822?pr=513#step:8:408 . Basically almost every test which causes the interpreter to write anything to the screen in Windows is currently skipped. Its a known issue, which we are hoping will get fixed soon. Whether this PR is acceptable with this test skipped on Windows will not be upto me though. You would need the approval of @vgvassilev or maybe @aaronj0 .

So, should I skip these tests for windows? @vgvassilev @aaronj0

@kr-2003
Copy link
Contributor Author

kr-2003 commented Mar 22, 2025

For now I have skipped these tests for windows.

Copy link
Contributor

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

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

Lgtm!

@vgvassilev
Copy link
Contributor

Make sure the ci passes and you squash on merge.

@anutosh491
Copy link
Collaborator

Make sure the ci passes and you squash on merge.

Absolutely
Yeah 15 commits is a bit too much.

@anutosh491 anutosh491 merged commit b9256d7 into compiler-research:main Mar 24, 2025
73 checks passed
@Vipul-Cariappa
Copy link
Collaborator

@kr-2003 Congratulations on getting your first PR merged 🚀.

@kr-2003
Copy link
Contributor Author

kr-2003 commented Mar 25, 2025

@kr-2003 Congratulations on getting your first PR merged 🚀.

Thank you 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants