Skip to content

Commit

Permalink
Add support for an external interpreter
Browse files Browse the repository at this point in the history
The new interface can be called by an external library like ROOT, that manages it's own `TInterpreter` instance.
In this case the `cling::Interpreter*` initialised by TCling is passed to InterOp and a flag indicates that InterOp does not have ownership
  • Loading branch information
aaronj0 committed Oct 30, 2024
1 parent b2d787b commit 59a16a2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
7 changes: 7 additions & 0 deletions include/clang/Interpreter/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,13 @@ namespace Cpp {
///\returns the current interpreter instance, if any.
CPPINTEROP_API TInterp_t GetInterpreter();

/// Sets the Interpreter instance with an external interpreter, meant to
/// be called by an external library that manages it's own interpreter.
/// Sets a flag signifying CppInterOp does not have ownership of the
/// sInterpreter.
///\param[in] Args - the pointer to an external interpreter
CPPINTEROP_API void UseExternalInterpreter(TInterp_t I);

/// Adds a Search Path for the Interpreter to get the libraries.
CPPINTEROP_API void AddSearchPath(const char* dir, bool isUser = true,
bool prepend = false);
Expand Down
21 changes: 14 additions & 7 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,22 @@ namespace Cpp {
using namespace llvm;
using namespace std;

static std::unique_ptr<compat::Interpreter> sInterpreter;
// Flag to indicate ownership when an external interpreter instance is used.
static bool OwningSInterpreter = true;
static compat::Interpreter* sInterpreter = nullptr;
// Valgrind complains about __cxa_pure_virtual called when deleting
// llvm::SectionMemoryManager::~SectionMemoryManager as part of the dtor chain
// of the Interpreter.
// This might fix the issue https://reviews.llvm.org/D107087
// FIXME: For now we just leak the Interpreter.
struct InterpDeleter {
~InterpDeleter() { sInterpreter.release(); }
~InterpDeleter() {}
} Deleter;

static compat::Interpreter& getInterp() {
assert(sInterpreter.get() && "Must be set before calling this!");
return *sInterpreter.get();
assert(sInterpreter &&
"Interpreter instance must be set before calling this!");
return *sInterpreter;
}
static clang::Sema& getSema() { return getInterp().getCI()->getSema(); }
static clang::ASTContext& getASTContext() { return getSema().getASTContext(); }
Expand Down Expand Up @@ -2691,12 +2694,16 @@ namespace Cpp {
// FIXME: Enable this assert once we figure out how to fix the multiple
// calls to CreateInterpreter.
//assert(!sInterpreter && "Interpreter already set.");
sInterpreter.reset(I);
sInterpreter = I;
return I;
}

TInterp_t GetInterpreter() {
return sInterpreter.get();
TInterp_t GetInterpreter() { return sInterpreter; }

void UseExternalInterpreter(TInterp_t I) {
assert(sInterpreter && "sInterpreter already in use!");
sInterpreter = static_cast<compat::Interpreter*>(I);
OwningSInterpreter = false;

Check warning on line 2706 in lib/Interpreter/CppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CppInterOp.cpp#L2703-L2706

Added lines #L2703 - L2706 were not covered by tests
}

void AddSearchPath(const char *dir, bool isUser,
Expand Down

0 comments on commit 59a16a2

Please sign in to comment.