Skip to content
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

Add support for an external interpreter #346

Open
wants to merge 3 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
9 changes: 9 additions & 0 deletions include/clang/Interpreter/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,15 @@ 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);

CPPINTEROP_API bool OwnsInterpreter();

/// 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
23 changes: 16 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;
aaronj0 marked this conversation as resolved.
Show resolved Hide resolved
static compat::Interpreter* sInterpreter = nullptr;
aaronj0 marked this conversation as resolved.
Show resolved Hide resolved
aaronj0 marked this conversation as resolved.
Show resolved Hide resolved
// 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() {}
aaronj0 marked this conversation as resolved.
Show resolved Hide resolved
} 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,14 +2694,20 @@ 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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we write a test for this?

}

bool OwnsInterpreter() { return OwningSInterpreter; }

void AddSearchPath(const char *dir, bool isUser,
bool prepend) {
getInterp().getDynamicLibraryManager()->addSearchPath(dir, isUser, prepend);
Expand Down
42 changes: 42 additions & 0 deletions unittests/CppInterOp/InterpreterTest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@

#include "Utils.h"

#include "clang/Interpreter/CppInterOp.h"

#ifdef USE_CLING
#include "cling/Interpreter/Interpreter.h"
#endif // USE_CLING

#ifdef USE_REPL
#include "clang/Interpreter/Interpreter.h"
#endif // USE_REPL

#include "clang/Basic/Version.h"

#include "llvm/ADT/SmallString.h"
Expand Down Expand Up @@ -162,3 +172,35 @@ TEST(InterpreterTest, CodeCompletion) {
GTEST_SKIP();
#endif
}

TEST(InterpreterTest, ExternalInterpreterTest) {

#ifdef USE_REPL
llvm::ExitOnError ExitOnErr;
clang::IncrementalCompilerBuilder CB;
CB.SetCompilerArgs({"-std=c++20"});

// Create the incremental compiler instance.
std::unique_ptr<clang::CompilerInstance> CI;
CI = ExitOnErr(CB.CreateCpp());

// Create the interpreter instance.
std::unique_ptr<clang::Interpreter> I =
ExitOnErr(clang::Interpreter::create(std::move(CI)));
#endif

#ifdef USE_CLING
std::string MainExecutableName = sys::fs::getMainExecutable(nullptr, nullptr);
std::string ResourceDir = compat::MakeResourceDir(LLVM_BINARY_DIR);
std::vector<const char *> ClingArgv = {"-resource-dir", ResourceDir.c_str(),
"-std=c++14"};
ClingArgv.insert(ClingArgv.begin(), MainExecutableName.c_str());
auto I = new compat::Interpreter(ClingArgv.size(), &ClingArgv[0]);
#endif

auto ExtInterp = I.get();
EXPECT_NE(ExtInterp, nullptr);
Cpp::UseExternalInterpreter(ExtInterp);
EXPECT_FALSE(Cpp::OwnsInterpreter());
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need this probably. We can delete ExtInterp and write a death test.

I.release();
}
Loading