Skip to content

Commit 50fa9c9

Browse files
committed
[clang-repl] Extend the C support.
The IdResolver chain is the main way for C to implement lookup rules. Every new partial translation unit caused clang to exit the top-most scope which in turn cleaned up the IdResolver chain. That was not an issue for C++ because its lookup is implemented on the level of declaration contexts. This patch keeps the IdResolver chain across partial translation units maintaining proper C-style lookup infrastructure.
1 parent 42070a5 commit 50fa9c9

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

clang/lib/Interpreter/IncrementalParser.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,7 @@ std::unique_ptr<llvm::Module> IncrementalParser::GenModule() {
387387

388388
void IncrementalParser::CleanUpPTU(PartialTranslationUnit &PTU) {
389389
TranslationUnitDecl *MostRecentTU = PTU.TUPart;
390-
TranslationUnitDecl *FirstTU = MostRecentTU->getFirstDecl();
391-
if (StoredDeclsMap *Map = FirstTU->getPrimaryContext()->getLookupPtr()) {
390+
if (StoredDeclsMap *Map = MostRecentTU->getPrimaryContext()->getLookupPtr()) {
392391
for (auto &&[Key, List] : *Map) {
393392
DeclContextLookupResult R = List.getLookupResult();
394393
std::vector<NamedDecl *> NamedDeclsToRemove;
@@ -407,6 +406,16 @@ void IncrementalParser::CleanUpPTU(PartialTranslationUnit &PTU) {
407406
}
408407
}
409408
}
409+
410+
// FIXME: We should de-allocate MostRecentTU
411+
for (Decl *D : MostRecentTU->decls()) {
412+
if (!isa<NamedDecl>(D))
413+
continue;
414+
// Check if we need to clean up the IdResolver chain.
415+
NamedDecl *ND = cast<NamedDecl>(D);
416+
if (ND->getDeclName().getFETokenInfo())
417+
getCI()->getSema().IdResolver.RemoveDecl(ND);
418+
}
410419
}
411420

412421
llvm::StringRef IncrementalParser::GetMangledName(GlobalDecl GD) const {

clang/lib/Sema/SemaDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2282,7 +2282,8 @@ void Sema::ActOnPopScope(SourceLocation Loc, Scope *S) {
22822282

22832283
// Remove this name from our lexical scope, and warn on it if we haven't
22842284
// already.
2285-
IdResolver.RemoveDecl(D);
2285+
if (!PP.isIncrementalProcessingEnabled())
2286+
IdResolver.RemoveDecl(D);
22862287
auto ShadowI = ShadowingDecls.find(D);
22872288
if (ShadowI != ShadowingDecls.end()) {
22882289
if (const auto *FD = dyn_cast<FieldDecl>(ShadowI->second)) {

clang/test/Interpreter/execute.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// REQUIRES: host-supports-jit
2+
// UNSUPPORTED: system-aix
3+
4+
// RUN: cat %s | clang-repl -Xcc -xc -Xcc -Xclang -Xcc -verify | FileCheck %s
5+
// RUN: cat %s | clang-repl -Xcc -xc -Xcc -O2 -Xcc -Xclang -Xcc -verify| FileCheck %s
6+
int printf(const char *, ...);
7+
int i = 42; err // expected-error{{use of undeclared identifier}}
8+
int i = 42;
9+
struct S { float f; struct S *m;} s = {1.0, 0};
10+
// FIXME: Making foo inline fails to emit the function.
11+
int foo() { return 42; }
12+
void run() { \
13+
printf("i = %d\n", i); \
14+
printf("S[f=%f, m=0x%llx]\n", s.f, (unsigned long long)s.m); \
15+
int r3 = foo(); \
16+
}
17+
run();
18+
// CHECK: i = 42
19+
// CHECK-NEXT: S[f=1.000000, m=0x0]
20+
21+
%quit

0 commit comments

Comments
 (0)