Skip to content

Commit 267a437

Browse files
authored
[Clang][Sema] Skip RecordDecl when checking scope of declarations (llvm#69432)
In non C++ mode, struct definitions does not create a scope for declaration.
1 parent db37d25 commit 267a437

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,8 @@ Miscellaneous Clang Crashes Fixed
712712
`Issue 64564 <https://github.com/llvm/llvm-project/issues/64564>`_
713713
- Fixed a crash when an ObjC ivar has an invalid type. See
714714
(`#68001 <https://github.com/llvm/llvm-project/pull/68001>`_)
715+
- Fixed a crash in C when redefined struct is another nested redefinition.
716+
`Issue 41302 <https://github.com/llvm/llvm-project/issues/41302>`_
715717

716718
Target Specific Changes
717719
-----------------------

clang/lib/Sema/IdentifierResolver.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S,
109109
return false;
110110
if (Ctx->isFunctionOrMethod() || (S && S->isFunctionPrototypeScope())) {
111111
// Ignore the scopes associated within transparent declaration contexts.
112-
while (S->getEntity() && S->getEntity()->isTransparentContext())
112+
while (S->getEntity() &&
113+
(S->getEntity()->isTransparentContext() ||
114+
(!LangOpt.CPlusPlus && isa<RecordDecl>(S->getEntity()))))
113115
S = S->getParent();
114116

115117
if (S->isDeclScope(D))

clang/test/Sema/nested-redef.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,16 @@ void f2(void) {
1919
struct U u;
2020
}
2121

22+
void f3(void) {
23+
struct G { // expected-note{{previous definition is here}}
24+
struct G {}; // expected-error{{nested redefinition of 'G'}}
25+
};
26+
}
27+
28+
void f4(void) {
29+
struct G { // expected-note 2{{previous definition is here}}
30+
struct G {}; // expected-error{{nested redefinition of 'G'}}
31+
};
2232

33+
struct G {}; // expected-error{{redefinition of 'G'}}
34+
}

clang/test/SemaObjC/ivar-lookup.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ - (int) test
9595
union U {
9696
__typeof(myStatus) __in; // fails.
9797
};
98-
struct S {
98+
struct S { // expected-note{{previous definition is here}}
9999
__typeof(myStatus) __in; // fails.
100100
struct S1 { // expected-warning {{declaration does not declare anything}}
101101
__typeof(myStatus) __in; // fails.
102-
struct S { // expected-warning {{declaration does not declare anything}}
102+
struct S { // expected-error {{nested redefinition of 'S'}}
103103
__typeof(myStatus) __in; // fails.
104104
};
105105
};

0 commit comments

Comments
 (0)