Skip to content

Commit 738a047

Browse files
authored
[Clang] Skip shadow warnings for enum constants in distinct class scopes (#115656)
Fixes #62588
1 parent 43f84e7 commit 738a047

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,8 @@ Improvements to Clang's diagnostics
537537

538538
- Improved diagnostic message for ``__builtin_bit_cast`` size mismatch (#GH115870).
539539

540+
- Clang now omits shadow warnings for enum constants in separate class scopes (#GH62588).
541+
540542
Improvements to Clang's time-trace
541543
----------------------------------
542544

clang/lib/Sema/SemaDecl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8350,9 +8350,15 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
83508350
return;
83518351

83528352
// Only warn about certain kinds of shadowing for class members.
8353-
if (NewDC && NewDC->isRecord()) {
8353+
if (NewDC) {
83548354
// In particular, don't warn about shadowing non-class members.
8355-
if (!OldDC->isRecord())
8355+
if (NewDC->isRecord() && !OldDC->isRecord())
8356+
return;
8357+
8358+
// Skip shadowing check if we're in a class scope, dealing with an enum
8359+
// constant in a different context.
8360+
DeclContext *ReDC = NewDC->getRedeclContext();
8361+
if (ReDC->isRecord() && isa<EnumConstantDecl>(D) && !OldDC->Equals(ReDC))
83568362
return;
83578363

83588364
// TODO: should we warn about static data members shadowing
@@ -8363,7 +8369,6 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
83638369
// shadowing context, but that's just a false negative.
83648370
}
83658371

8366-
83678372
DeclarationName Name = R.getLookupName();
83688373

83698374
// Emit warning and note.

clang/test/SemaCXX/warn-shadow.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,17 @@ void test4() {
307307
}
308308

309309
}; // namespace structured_binding_tests
310+
311+
namespace GH62588 {
312+
class Outer {
313+
public:
314+
char *foo(); // expected-note {{previous declaration is here}} \
315+
// expected-note {{previous definition is here}}
316+
enum Outer_E { foo }; // expected-error {{redefinition of 'foo'}} \
317+
// expected-warning {{declaration shadows a static data member of 'GH62588::Outer'}}
318+
class Inner {
319+
public:
320+
enum Inner_E { foo }; // ok
321+
};
322+
};
323+
} // namespace GH62588

0 commit comments

Comments
 (0)