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

Anutosh Bhat via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 22 06:10:36 PDT 2025


https://github.com/anutosh491 updated https://github.com/llvm/llvm-project/pull/164597

>From 900286b55e29bf833a73abfff6bd377ae5825637 Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat491 at gmail.com>
Date: Wed, 22 Oct 2025 16:15:26 +0530
Subject: [PATCH 1/3] Fix duplicate definition error for symbols in C mode

---
 clang/lib/Sema/Sema.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 215ac184a5337..15c5815398094 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -1447,6 +1447,10 @@ void Sema::ActOnEndOfTranslationUnit() {
     if (!VD || VD->isInvalidDecl() || !Seen.insert(VD).second)
       continue;
 
+    if (PP.isIncrementalProcessingEnabled() &&
+        VD->getTranslationUnitDecl() != Context.getTranslationUnitDecl())
+      continue;
+
     if (const IncompleteArrayType *ArrayT
         = Context.getAsIncompleteArrayType(VD->getType())) {
       // Set the length of the array to 1 (C99 6.9.2p5).

>From 2f5efbac8196b7c6af42bc1372d3fe9fcba9b346 Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat491 at gmail.com>
Date: Wed, 22 Oct 2025 18:34:06 +0530
Subject: [PATCH 2/3] Add tests

---
 clang/lib/Sema/Sema.cpp               | 11 +++++++----
 clang/test/Interpreter/pretty-print.c | 22 ++++++++++++++++++++--
 2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 15c5815398094..2c81377ad78f7 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -1447,10 +1447,6 @@ void Sema::ActOnEndOfTranslationUnit() {
     if (!VD || VD->isInvalidDecl() || !Seen.insert(VD).second)
       continue;
 
-    if (PP.isIncrementalProcessingEnabled() &&
-        VD->getTranslationUnitDecl() != Context.getTranslationUnitDecl())
-      continue;
-
     if (const IncompleteArrayType *ArrayT
         = Context.getAsIncompleteArrayType(VD->getType())) {
       // Set the length of the array to 1 (C99 6.9.2p5).
@@ -1488,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

>From 1d741230acaab74f0f7c4ee9ed79a26c2fc9f2f7 Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat491 at gmail.com>
Date: Wed, 22 Oct 2025 18:40:19 +0530
Subject: [PATCH 3/3] format

---
 clang/lib/Sema/Sema.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 2c81377ad78f7..8ed3df7cf17a5 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -1489,7 +1489,7 @@ void Sema::ActOnEndOfTranslationUnit() {
   // emitted to codegen, drop them to prevent re-emission in future PTUs.
   if (PP.isIncrementalProcessingEnabled())
     TentativeDefinitions.erase(TentativeDefinitions.begin(ExternalSource.get()),
-                                TentativeDefinitions.end());
+                               TentativeDefinitions.end());
 
   for (auto *D : ExternalDeclarations) {
     if (!D || D->isInvalidDecl() || D->getPreviousDecl() || !D->isUsed())



More information about the cfe-commits mailing list