Skip to content

Commit fe324fa

Browse files
committed
Add support for an external interpreter
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
1 parent 444495e commit fe324fa

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

include/clang/Interpreter/CppInterOp.h

+7
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,13 @@ namespace Cpp {
530530
///\returns the current interpreter instance, if any.
531531
CPPINTEROP_API TInterp_t GetInterpreter();
532532

533+
/// Sets the Interpreter instance with an external interpreter, meant to
534+
/// be called by an external library that manages it's own interpreter.
535+
/// Sets a flag signifying CppInterOp does not have ownership of the
536+
/// sInterpreter.
537+
///\param[in] Args - the pointer to an external interpreter
538+
CPPINTEROP_API void UseExternalInterpreter(TInterp_t I);
539+
533540
/// Adds a Search Path for the Interpreter to get the libraries.
534541
CPPINTEROP_API void AddSearchPath(const char* dir, bool isUser = true,
535542
bool prepend = false);

lib/Interpreter/CppInterOp.cpp

+17-7
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,25 @@ namespace Cpp {
6060
using namespace llvm;
6161
using namespace std;
6262

63-
static std::unique_ptr<compat::Interpreter> sInterpreter;
63+
// Flag to indicate ownership when an external interpreter instance is used.
64+
static bool OwningSInterpreter = true;
65+
static compat::Interpreter* sInterpreter = nullptr;
6466
// Valgrind complains about __cxa_pure_virtual called when deleting
6567
// llvm::SectionMemoryManager::~SectionMemoryManager as part of the dtor chain
6668
// of the Interpreter.
6769
// This might fix the issue https://reviews.llvm.org/D107087
6870
// FIXME: For now we just leak the Interpreter.
6971
struct InterpDeleter {
70-
~InterpDeleter() { sInterpreter.release(); }
72+
~InterpDeleter() {
73+
if (sInterpreter && OwningSInterpreter)
74+
delete sInterpreter;
75+
}
7176
} Deleter;
7277

7378
static compat::Interpreter& getInterp() {
74-
assert(sInterpreter.get() && "Must be set before calling this!");
75-
return *sInterpreter.get();
79+
assert(sInterpreter &&
80+
"Interpreter instance must be set before calling this!");
81+
return *sInterpreter;
7682
}
7783
static clang::Sema& getSema() { return getInterp().getCI()->getSema(); }
7884
static clang::ASTContext& getASTContext() { return getSema().getASTContext(); }
@@ -2691,12 +2697,16 @@ namespace Cpp {
26912697
// FIXME: Enable this assert once we figure out how to fix the multiple
26922698
// calls to CreateInterpreter.
26932699
//assert(!sInterpreter && "Interpreter already set.");
2694-
sInterpreter.reset(I);
2700+
sInterpreter = I;
26952701
return I;
26962702
}
26972703

2698-
TInterp_t GetInterpreter() {
2699-
return sInterpreter.get();
2704+
TInterp_t GetInterpreter() { return sInterpreter; }
2705+
2706+
void UseExternalInterpreter(TInterp_t I) {
2707+
assert(sInterpreter && "sInterpreter already in use!");
2708+
sInterpreter = static_cast<compat::Interpreter*>(I);
2709+
OwningSInterpreter = false;
27002710
}
27012711

27022712
void AddSearchPath(const char *dir, bool isUser,

0 commit comments

Comments
 (0)