[clang-tools-extra] [Clang][Sema] Skip RecordDecl when checking scope of declarations (PR #69432)

Qiu Chaofan via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 26 03:00:23 PDT 2023


https://github.com/ecnelises updated https://github.com/llvm/llvm-project/pull/69432

>From f97807afc177ab04c97d0c346081fc92d3e79e6c Mon Sep 17 00:00:00 2001
From: Qiu Chaofan <qiucofan at cn.ibm.com>
Date: Wed, 18 Oct 2023 16:02:02 +0800
Subject: [PATCH 1/3] [Clang][Sema] Skip RecordDecl when checking scope of
 declarations

---
 clang/lib/Sema/IdentifierResolver.cpp | 4 +++-
 clang/test/Sema/nested-redef.c        | 6 +++++-
 clang/test/SemaObjC/ivar-lookup.m     | 4 ++--
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Sema/IdentifierResolver.cpp b/clang/lib/Sema/IdentifierResolver.cpp
index 98a6f3b45089b3a..2213c3c837243ad 100644
--- a/clang/lib/Sema/IdentifierResolver.cpp
+++ b/clang/lib/Sema/IdentifierResolver.cpp
@@ -109,7 +109,9 @@ bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S,
     return false;
   if (Ctx->isFunctionOrMethod() || (S && S->isFunctionPrototypeScope())) {
     // Ignore the scopes associated within transparent declaration contexts.
-    while (S->getEntity() && S->getEntity()->isTransparentContext())
+    while (S->getEntity() &&
+           (S->getEntity()->isTransparentContext() ||
+            (!LangOpt.CPlusPlus && isa<RecordDecl>(S->getEntity()))))
       S = S->getParent();
 
     if (S->isDeclScope(D))
diff --git a/clang/test/Sema/nested-redef.c b/clang/test/Sema/nested-redef.c
index bbc485936770478..c5a726c30422f98 100644
--- a/clang/test/Sema/nested-redef.c
+++ b/clang/test/Sema/nested-redef.c
@@ -19,4 +19,8 @@ void f2(void) {
   struct U u;
 }
 
-
+void f3(void) {
+  struct G { // expected-note{{previous definition is here}}
+    struct G {}; // expected-error{{nested redefinition of 'G'}}
+  };
+}
diff --git a/clang/test/SemaObjC/ivar-lookup.m b/clang/test/SemaObjC/ivar-lookup.m
index 898ffac99692ced..d88299e58e0f594 100644
--- a/clang/test/SemaObjC/ivar-lookup.m
+++ b/clang/test/SemaObjC/ivar-lookup.m
@@ -95,11 +95,11 @@ - (int) test
   union U {
     __typeof(myStatus) __in;  // fails.
   };
-  struct S {
+  struct S { // expected-note{{previous definition is here}}
     __typeof(myStatus) __in;  // fails.
     struct S1 { // expected-warning {{declaration does not declare anything}}
       __typeof(myStatus) __in;  // fails.
-      struct S { // expected-warning {{declaration does not declare anything}}
+      struct S { // expected-error {{nested redefinition of 'S'}}
         __typeof(myStatus) __in;  // fails.
       };
     };

>From 48c50ddc4380a6e28c81b5270d602c30d71843e0 Mon Sep 17 00:00:00 2001
From: Qiu Chaofan <qiucofan at cn.ibm.com>
Date: Thu, 26 Oct 2023 16:47:33 +0800
Subject: [PATCH 2/3] Add original case

---
 clang/test/Sema/nested-redef.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/clang/test/Sema/nested-redef.c b/clang/test/Sema/nested-redef.c
index c5a726c30422f98..adc57ff4e04ab29 100644
--- a/clang/test/Sema/nested-redef.c
+++ b/clang/test/Sema/nested-redef.c
@@ -24,3 +24,11 @@ void f3(void) {
     struct G {}; // expected-error{{nested redefinition of 'G'}}
   };
 }
+
+void f4(void) {
+  struct G { // expected-note 2{{previous definition is here}}
+    struct G {}; // expected-error{{nested redefinition of 'G'}}
+  };
+
+  struct G {}; // expected-error{{redefinition of 'G'}}
+}

>From 4d3e00b36608ddeb80e3c92186f504e299baf7a8 Mon Sep 17 00:00:00 2001
From: Qiu Chaofan <qiucofan at cn.ibm.com>
Date: Thu, 26 Oct 2023 18:00:00 +0800
Subject: [PATCH 3/3] Add releasenotes

---
 clang/docs/ReleaseNotes.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 074116d2edf9f99..3e94544166552d1 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -667,6 +667,8 @@ Miscellaneous Clang Crashes Fixed
   `Issue 64564 <https://github.com/llvm/llvm-project/issues/64564>`_
 - Fixed a crash when an ObjC ivar has an invalid type. See
   (`#68001 <https://github.com/llvm/llvm-project/pull/68001>`_)
+- Fixed a crash in C when redefined struct is another nested redefinition.
+  `Issue 41302 <https://github.com/llvm/llvm-project/issues/41302>`_
 
 Target Specific Changes
 -----------------------



More information about the cfe-commits mailing list