How to completely unload the module and start over

Hello folks,

I’m trying to do the following setup:

  1. Create cling::Interpreter object and pass the *.pch file to it.
  2. Load the C++ file with few declarations, and function definition.
  3. Run the function.
  4. Reset the state to the state 1.

So far I’ve been trying this:

cling::Interpreter interp("-include-pch" /* pseudo code, passing pch */);



// loop 
{
cling::Interpreter::Transaction pTx = nullptr;
interp.loadFile("my_cppfile", false, &pTx);
interp.process("call_fn();");
interp.unload(&pTx);
}

However, sometimes it seems that the entire PCH gets unloaded (so the “my_cppfile.cpp” fails to compile even). Am I doing something obviously wrong? I’ve tried to search for examples, but I couldn’t find many. I also tried to have two transactions, one in loadFile, one in process, to pass the same one, or to do unload(2), but I had no lock.

Thank you!

Unloading is the toughest operation for cling - it’s something clang really doesn’t do, ever, and we know of issues with it.

As an alternative you could consider starting a child interpreter. That mostly depends on the complexity of the code in the PCH: if clang implemented AST copying / injection for it you can set up a parent interpreter with the PCH, and then move any subsequent operations into a short-lived child interpreter. See the constructor here: cling: cling::Interpreter Class Reference

(And we really need to get our CI to update those doxygen pages…)

Axel

Thank you, Axel, I will try this and report back.

Hi Axel, just to report back - it seems that for the particular PCH, clang didn’t do whatever is supposed to do, so if I create the child interpreter, none of the symbols from PCH are visible (unless I pass the PCH again via -include-pch, but that causes a significant startup hit).

Thank you anyway for the software, it’s a pleasure tinkering with it.

Hmm that’s surprising, I did not expect complete failure with the child interpreter…

OK well, so we need to figure out what fails in the unloading case. Can you provide us with a minimal reproducer of m_cppfile that shows this issue of unloading and reloading?

Thanks for your kind words!

I’ll try to do that over the weekend, thanks!