Skip to content

Commit 04b5cc6

Browse files
authored
[clang-repl] Fix duplicate definition error for symbols in C mode (#164597)
Fixes #164596
1 parent 99abda7 commit 04b5cc6

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

clang/lib/Sema/Sema.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,13 @@ void Sema::ActOnEndOfTranslationUnit() {
14841484
Consumer.CompleteTentativeDefinition(VD);
14851485
}
14861486

1487+
// In incremental mode, tentative definitions belong to the current
1488+
// partial translation unit (PTU). Once they have been completed and
1489+
// emitted to codegen, drop them to prevent re-emission in future PTUs.
1490+
if (PP.isIncrementalProcessingEnabled())
1491+
TentativeDefinitions.erase(TentativeDefinitions.begin(ExternalSource.get()),
1492+
TentativeDefinitions.end());
1493+
14871494
for (auto *D : ExternalDeclarations) {
14881495
if (!D || D->isInvalidDecl() || D->getPreviousDecl() || !D->isUsed())
14891496
continue;

clang/test/Interpreter/pretty-print.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ int * ptr = (int*)0x123; ptr
7575
int * null_ptr = (int*)0; null_ptr
7676
// CHECK-NEXT: (int *) 0x0
7777

78+
union U { int I; float F; } u; u.I = 12; u.I
79+
// CHECK-NEXT: (int) 12
80+
7881
// TODO: _Bool, _Complex, _Atomic, and _BitInt
79-
// union U { int I; float F; } u; u.I = 12; u.I
80-
// TODO-CHECK-NEXT: (int) 12
8182
// struct S1{} s1; s1
8283
// TODO-CHECK-NEXT: (S1 &) @0x{{[0-9a-f]+}}
8384

@@ -86,4 +87,21 @@ int * null_ptr = (int*)0; null_ptr
8687
// E.d
8788
// TODO-CHECK-NEXT: (int) 22
8889

90+
// -----------------------------------------------------------------------------
91+
// Tentative definition handling (C99 6.9.2)
92+
// Verify that multiple distinct tentative definitions across inputs no longer
93+
// conflict. Each variable should emit correctly in its own incremental module.
94+
// -----------------------------------------------------------------------------
95+
96+
int t1;
97+
int t2;
98+
int t3;
99+
t1 = 1; t2 = 2; t3 = 3;
100+
t1 + t2 + t3
101+
// CHECK-NEXT: (int) 6
102+
103+
// A redefinition of an existing tentative variable should still fail.
104+
int t1;
105+
// expected-error {{duplicate definition of symbol '_t1'}}
106+
89107
%quit

0 commit comments

Comments
 (0)