Author: Anutosh Bhat
Date: 2025-10-22T23:37:47+05:30
New Revision: 04b5cc6a2457dbd6b320c8345959cf60c94e3cc6

URL: 
https://github.com/llvm/llvm-project/commit/04b5cc6a2457dbd6b320c8345959cf60c94e3cc6
DIFF: 
https://github.com/llvm/llvm-project/commit/04b5cc6a2457dbd6b320c8345959cf60c94e3cc6.diff

LOG: [clang-repl] Fix duplicate definition error for symbols in C mode (#164597)

Fixes #164596

Added: 
    

Modified: 
    clang/lib/Sema/Sema.cpp
    clang/test/Interpreter/pretty-print.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 215ac184a5337..8ed3df7cf17a5 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -1484,6 +1484,13 @@ void Sema::ActOnEndOfTranslationUnit() {
       Consumer.CompleteTentativeDefinition(VD);
   }
 
+  // In incremental mode, tentative definitions belong to the current
+  // partial translation unit (PTU). Once they have been completed and
+  // emitted to codegen, drop them to prevent re-emission in future PTUs.
+  if (PP.isIncrementalProcessingEnabled())
+    
TentativeDefinitions.erase(TentativeDefinitions.begin(ExternalSource.get()),
+                               TentativeDefinitions.end());
+
   for (auto *D : ExternalDeclarations) {
     if (!D || D->isInvalidDecl() || D->getPreviousDecl() || !D->isUsed())
       continue;

diff  --git a/clang/test/Interpreter/pretty-print.c 
b/clang/test/Interpreter/pretty-print.c
index 588df70e33e84..d0712fb152107 100644
--- a/clang/test/Interpreter/pretty-print.c
+++ b/clang/test/Interpreter/pretty-print.c
@@ -75,9 +75,10 @@ int * ptr = (int*)0x123; ptr
 int * null_ptr = (int*)0; null_ptr
 // CHECK-NEXT: (int *) 0x0
 
+union U { int I; float F; } u; u.I = 12; u.I
+// CHECK-NEXT: (int) 12
+
 // TODO: _Bool, _Complex, _Atomic, and _BitInt
-// union U { int I; float F; } u; u.I = 12; u.I
-// TODO-CHECK-NEXT: (int) 12
 // struct S1{} s1; s1
 // TODO-CHECK-NEXT: (S1 &) @0x{{[0-9a-f]+}}
 
@@ -86,4 +87,21 @@ int * null_ptr = (int*)0; null_ptr
 // E.d
 // TODO-CHECK-NEXT: (int) 22
 
+// 
-----------------------------------------------------------------------------
+// Tentative definition handling (C99 6.9.2)
+// Verify that multiple distinct tentative definitions across inputs no longer
+// conflict. Each variable should emit correctly in its own incremental module.
+// 
-----------------------------------------------------------------------------
+
+int t1;
+int t2;
+int t3;
+t1 = 1; t2 = 2; t3 = 3;
+t1 + t2 + t3
+// CHECK-NEXT: (int) 6
+
+// A redefinition of an existing tentative variable should still fail.
+int t1;
+// expected-error {{duplicate definition of symbol '_t1'}}
+
 %quit


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to