[Clang] Skip shadow warnings for enum constants in distinct class scopes (#115656)

Fixes #62588
This commit is contained in:
Oleksandr T. 2024-11-19 11:38:49 +02:00 committed by GitHub
parent 43f84e7937
commit 738a047ed6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 3 deletions

View File

@ -537,6 +537,8 @@ Improvements to Clang's diagnostics
- Improved diagnostic message for ``__builtin_bit_cast`` size mismatch (#GH115870).
- Clang now omits shadow warnings for enum constants in separate class scopes (#GH62588).
Improvements to Clang's time-trace
----------------------------------

View File

@ -8350,9 +8350,15 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
return;
// Only warn about certain kinds of shadowing for class members.
if (NewDC && NewDC->isRecord()) {
if (NewDC) {
// In particular, don't warn about shadowing non-class members.
if (!OldDC->isRecord())
if (NewDC->isRecord() && !OldDC->isRecord())
return;
// Skip shadowing check if we're in a class scope, dealing with an enum
// constant in a different context.
DeclContext *ReDC = NewDC->getRedeclContext();
if (ReDC->isRecord() && isa<EnumConstantDecl>(D) && !OldDC->Equals(ReDC))
return;
// TODO: should we warn about static data members shadowing
@ -8363,7 +8369,6 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
// shadowing context, but that's just a false negative.
}
DeclarationName Name = R.getLookupName();
// Emit warning and note.

View File

@ -307,3 +307,17 @@ void test4() {
}
}; // namespace structured_binding_tests
namespace GH62588 {
class Outer {
public:
char *foo(); // expected-note {{previous declaration is here}} \
// expected-note {{previous definition is here}}
enum Outer_E { foo }; // expected-error {{redefinition of 'foo'}} \
// expected-warning {{declaration shadows a static data member of 'GH62588::Outer'}}
class Inner {
public:
enum Inner_E { foo }; // ok
};
};
} // namespace GH62588