Skip to content

Commit c98a734

Browse files
committed
Fixing creation of Interpreter
1 parent c3384fb commit c98a734

File tree

5 files changed

+36
-10
lines changed

5 files changed

+36
-10
lines changed

lib/Interpreter/CXCppInterOp.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,15 @@ static inline compat::Interpreter* getInterpreter(const CXInterpreterImpl* I) {
271271
}
272272

273273
CXInterpreter clang_createInterpreter(const char* const* argv, int argc) {
274-
auto* I = new CXInterpreterImpl(); // NOLINT(*-owning-memory)
274+
auto I = std::make_unique<CXInterpreterImpl>(); // NOLINT(*-owning-memory)
275+
#ifdef CPPINTEROP_USE_CLING
275276
I->Interp = std::make_unique<compat::Interpreter>(argc, argv);
277+
#else
278+
I->Interp = compat::Interpreter::create(argc, argv);
279+
if (!I->Interp) {
280+
return nullptr;
281+
}
282+
#endif
276283
// create a bridge between CXTranslationUnit and clang::Interpreter
277284
// auto AU = std::make_unique<ASTUnit>(false);
278285
// AU->FileMgr = I->Interp->getCompilerInstance().getFileManager();
@@ -281,7 +288,7 @@ CXInterpreter clang_createInterpreter(const char* const* argv, int argc) {
281288
// AU->Ctx = &I->Interp->getSema().getASTContext();
282289
// I->TU.reset(MakeCXTranslationUnit(static_cast<CIndexer*>(clang_createIndex(0,
283290
// 0)), AU));
284-
return I;
291+
return I.release();
285292
}
286293

287294
CXInterpreter clang_createInterpreterFromRawPtr(TInterp_t I) {

lib/Interpreter/Compatibility.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,11 @@ createClangInterpreter(std::vector<const char*>& args) {
294294
return nullptr;
295295
}
296296
if (CudaEnabled) {
297-
if (auto Err = (*innerOrErr)->LoadDynamicLibrary("libcudart.so"))
297+
if (auto Err = (*innerOrErr)->LoadDynamicLibrary("libcudart.so")) {
298298
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(),
299299
"Failed load libcudart.so runtime:");
300+
return nullptr;
301+
}
300302
}
301303

302304
return std::move(*innerOrErr);

lib/Interpreter/CppInterOp.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -2957,7 +2957,15 @@ namespace Cpp {
29572957
std::back_inserter(ClingArgv),
29582958
[&](const std::string& str) { return str.c_str(); });
29592959

2960+
#ifdef CPPINTEROP_USE_CLING
29602961
auto I = new compat::Interpreter(ClingArgv.size(), &ClingArgv[0]);
2962+
#else
2963+
auto Interp = compat::Interpreter::create(
2964+
static_cast<int>(ClingArgv.size()), ClingArgv.data());
2965+
if (!Interp)
2966+
return nullptr;
2967+
auto* I = Interp.release();
2968+
#endif
29612969

29622970
// Honor -mllvm.
29632971
//

lib/Interpreter/CppInterOpInterpreter.h

+14-5
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,14 @@ class Interpreter {
136136
private:
137137
std::unique_ptr<clang::Interpreter> inner;
138138

139+
Interpreter(std::unique_ptr<clang::Interpreter> CI) : inner(std::move(CI)) {}
140+
139141
public:
140-
Interpreter(int argc, const char* const* argv, const char* llvmdir = 0,
141-
const std::vector<std::shared_ptr<clang::ModuleFileExtension>>&
142-
moduleExtensions = {},
143-
void* extraLibHandle = 0, bool noRuntime = true) {
142+
static std::unique_ptr<Interpreter>
143+
create(int argc, const char* const* argv, const char* llvmdir = nullptr,
144+
const std::vector<std::shared_ptr<clang::ModuleFileExtension>>&
145+
moduleExtensions = {},
146+
void* extraLibHandle = nullptr, bool noRuntime = true) {
144147
// Initialize all targets (required for device offloading)
145148
llvm::InitializeAllTargetInfos();
146149
llvm::InitializeAllTargets();
@@ -150,7 +153,13 @@ class Interpreter {
150153
std::vector<const char*> vargs(argv + 1, argv + argc);
151154
vargs.push_back("-include");
152155
vargs.push_back("new");
153-
inner = compat::createClangInterpreter(vargs);
156+
auto CI = compat::createClangInterpreter(vargs);
157+
if (!CI) {
158+
llvm::errs() << "Interpreter creation failed\n";
159+
return nullptr;
160+
}
161+
162+
return std::unique_ptr<Interpreter>(new Interpreter(std::move(CI)));
154163
}
155164

156165
~Interpreter() {}

unittests/CppInterOp/CUDATest.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static bool HasCudaSDK() {
1313
// FIXME: Enable this for cling.
1414
return false;
1515
#endif // CLANG_VERSION_MAJOR < 16
16-
Cpp::CreateInterpreter({}, {"--cuda"});
16+
if(!Cpp::CreateInterpreter({}, {"--cuda"})) return false;
1717
return Cpp::Declare("__global__ void test_func() {}"
1818
"test_func<<<1,1>>>();") == 0;
1919
};
@@ -30,7 +30,7 @@ static bool HasCudaRuntime() {
3030
if (!HasCudaSDK())
3131
return false;
3232

33-
Cpp::CreateInterpreter({}, {"--cuda"});
33+
if(!Cpp::CreateInterpreter({}, {"--cuda"})) return false;
3434
if (Cpp::Declare("__global__ void test_func() {}"
3535
"test_func<<<1,1>>>();"))
3636
return false;

0 commit comments

Comments
 (0)