Skip to content

Commit 629683d

Browse files
committed
[clang-repl] Teach clang-repl how to load PCHs.
1 parent c79d1fa commit 629683d

File tree

6 files changed

+33
-10
lines changed

6 files changed

+33
-10
lines changed

clang/include/clang/CodeGen/ModuleBuilder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ namespace CodeGen {
5252
class CodeGenerator : public ASTConsumer {
5353
virtual void anchor();
5454

55+
protected:
56+
/// True if we've finished generating IR. This prevents us from generating
57+
/// additional LLVM IR after emitting output in HandleTranslationUnit. This
58+
/// can happen when Clang plugins trigger additional AST deserialization.
59+
bool IRGenFinished = false;
60+
5561
public:
5662
/// Return an opaque reference to the CodeGenModule object, which can
5763
/// be used in various secondary APIs. It is valid as long as the

clang/lib/CodeGen/BackendConsumer.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ class BackendConsumer : public ASTConsumer {
4141
llvm::Timer LLVMIRGeneration;
4242
unsigned LLVMIRGenerationRefCount;
4343

44-
/// True if we've finished generating IR. This prevents us from generating
45-
/// additional LLVM IR after emitting output in HandleTranslationUnit. This
46-
/// can happen when Clang plugins trigger additional AST deserialization.
47-
bool IRGenFinished = false;
48-
4944
bool TimerIsEnabled = false;
5045

5146
std::unique_ptr<CodeGenerator> Gen;

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ void BackendConsumer::HandleInlineFunctionDefinition(FunctionDecl *D) {
221221
}
222222

223223
void BackendConsumer::HandleInterestingDecl(DeclGroupRef D) {
224-
// Ignore interesting decls from the AST reader after IRGen is finished.
225-
if (!IRGenFinished)
226-
HandleTopLevelDecl(D);
224+
HandleTopLevelDecl(D);
227225
}
228226

229227
// Links each entry in LinkModules into our module. Returns true on error.
@@ -280,8 +278,6 @@ void BackendConsumer::HandleTranslationUnit(ASTContext &C) {
280278
if (LLVMIRGenerationRefCount == 0)
281279
LLVMIRGeneration.stopTimer();
282280
}
283-
284-
IRGenFinished = true;
285281
}
286282

287283
// Silently ignore if we weren't initialized for some reason.

clang/lib/CodeGen/ModuleBuilder.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ namespace {
138138
assert(!M && "Replacing existing Module?");
139139
M.reset(new llvm::Module(ExpandModuleName(ModuleName, CodeGenOpts), C));
140140

141+
IRGenFinished = false;
142+
141143
std::unique_ptr<CodeGenModule> OldBuilder = std::move(Builder);
142144

143145
Initialize(*Ctx);
@@ -179,6 +181,10 @@ namespace {
179181
}
180182

181183
bool HandleTopLevelDecl(DeclGroupRef DG) override {
184+
// Ignore interesting decls from the AST reader after IRGen is finished.
185+
if (IRGenFinished)
186+
return true; // We can't CodeGen more but pass to other consumers.
187+
182188
// FIXME: Why not return false and abort parsing?
183189
if (Diags.hasUnrecoverableErrorOccurred())
184190
return true;
@@ -282,6 +288,8 @@ namespace {
282288
}
283289

284290
void HandleTranslationUnit(ASTContext &Ctx) override {
291+
IRGenFinished = true;
292+
285293
// Release the Builder when there is no error.
286294
if (!Diags.hasUnrecoverableErrorOccurred() && Builder)
287295
Builder->Release();

clang/lib/Interpreter/IncrementalParser.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ IncrementalParser::IncrementalParser(Interpreter &Interp,
219219
Consumer = &CI->getASTConsumer();
220220
P.reset(
221221
new Parser(CI->getPreprocessor(), CI->getSema(), /*SkipBodies=*/false));
222+
223+
if (ExternalASTSource *External = CI->getASTContext().getExternalSource())
224+
External->StartTranslationUnit(Consumer);
225+
222226
P->Initialize();
223227

224228
// An initial PTU is needed as CUDA includes some headers automatically
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// REQUIRES: host-supports-jit
2+
// UNSUPPORTED: system-aix
3+
4+
// RUN: rm -f %t.pch
5+
// RUN: %clang_cc1 -fmax-type-align=16 -pic-level 2 -fdeprecated-macro -stack-protector 1 -fblocks -fskip-odr-check-in-gmf -fexceptions -fcxx-exceptions -fgnuc-version=0 -triple=%target_triple -DPCH -fincremental-extensions -emit-pch -x c++-header -o %t.pch %s
6+
// RUN: clang-repl -Xcc -fgnuc-version=0 -Xcc -triple=%target_triple -Xcc -include-pch -Xcc %t.pch '#include "%s"' | FileCheck %s
7+
8+
#ifdef PCH
9+
int f_pch() { return 5; }
10+
#endif // PCH
11+
12+
extern "C" int printf(const char *, ...);
13+
auto r1 = printf("f_pch = %d\n", f_pch());
14+
// CHECK: f_pch = 5

0 commit comments

Comments
 (0)