Skip to content

Commit 2fc4014

Browse files
weliveindetailvgvassilev
authored andcommitted
[clang-repl] Set up executor implicitly to account for init PTUs
1 parent a3f07d3 commit 2fc4014

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

clang/lib/Interpreter/Interpreter.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,30 @@ IncrementalCompilerBuilder::CreateCudaHost() {
229229
}
230230

231231
Interpreter::Interpreter(std::unique_ptr<CompilerInstance> CI,
232-
llvm::Error &Err) {
233-
llvm::ErrorAsOutParameter EAO(&Err);
232+
llvm::Error &ErrOut) {
233+
llvm::ErrorAsOutParameter EAO(&ErrOut);
234234
auto LLVMCtx = std::make_unique<llvm::LLVMContext>();
235235
TSCtx = std::make_unique<llvm::orc::ThreadSafeContext>(std::move(LLVMCtx));
236-
IncrParser = std::make_unique<IncrementalParser>(*this, std::move(CI),
237-
*TSCtx->getContext(), Err);
236+
IncrParser = std::make_unique<IncrementalParser>(
237+
*this, std::move(CI), *TSCtx->getContext(), ErrOut);
238+
if (ErrOut)
239+
return;
240+
241+
// Not all frontends support code-generation, e.g. ast-dump actions don't
242+
if (IncrParser->getCodeGen()) {
243+
if (llvm::Error Err = CreateExecutor()) {
244+
ErrOut = joinErrors(std::move(ErrOut), std::move(Err));
245+
return;
246+
}
247+
248+
// Process the PTUs that came from initialization. For example -include will
249+
// give us a header that's processed at initialization of the preprocessor.
250+
for (PartialTranslationUnit &PTU : IncrParser->getPTUs())
251+
if (llvm::Error Err = Execute(PTU)) {
252+
ErrOut = joinErrors(std::move(ErrOut), std::move(Err));
253+
return;
254+
}
255+
}
238256
}
239257

240258
Interpreter::~Interpreter() {
@@ -395,10 +413,16 @@ llvm::Error Interpreter::CreateExecutor() {
395413
return llvm::make_error<llvm::StringError>("Operation failed. "
396414
"Execution engine exists",
397415
std::error_code());
416+
if (!IncrParser->getCodeGen())
417+
return llvm::make_error<llvm::StringError>("Operation failed. "
418+
"No code generator available",
419+
std::error_code());
420+
398421
llvm::Expected<std::unique_ptr<llvm::orc::LLJITBuilder>> JB =
399422
CreateJITBuilder(*getCompilerInstance());
400423
if (!JB)
401424
return JB.takeError();
425+
402426
llvm::Error Err = llvm::Error::success();
403427
auto Executor = std::make_unique<IncrementalExecutor>(*TSCtx, **JB, Err);
404428
if (!Err)

clang/test/Interpreter/execute.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
// RUN: cat %s | clang-repl | FileCheck %s
99
// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
10+
// RUN: clang-repl -Xcc -include -Xcc %s | FileCheck %s
11+
// RUN: clang-repl -Xcc -fsyntax-only -Xcc -include -Xcc %s
1012
extern "C" int printf(const char *, ...);
1113
int i = 42;
1214
auto r1 = printf("i = %d\n", i);
@@ -19,5 +21,3 @@ auto r2 = printf("S[f=%f, m=0x%llx]\n", s.f, reinterpret_cast<unsigned long long
1921

2022
inline int foo() { return 42; }
2123
int r3 = foo();
22-
23-
%quit

0 commit comments

Comments
 (0)