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

Qiu Chaofan via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 18 01:06:49 PDT 2023


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

In non C++ mode, struct definitions does not create a scope for declaration.

Fixes #41302 

Fixes #44080

>From 6828025e788347f21a41d4a9aa138af032017b80 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] [Clang][Sema] Skip RecordDecl when checking scope of
 declarations

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

diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index a3847a73faf8183..845f9b1d1f302af 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -1315,7 +1315,6 @@ bool DeclContext::isDependentContext() const {
 bool DeclContext::isTransparentContext() const {
   if (getDeclKind() == Decl::Enum)
     return !cast<EnumDecl>(this)->isScoped();
-
   return isa<LinkageSpecDecl, ExportDecl, HLSLBufferDecl>(this);
 }
 
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.
       };
     };



More information about the cfe-commits mailing list