Skip to content

Commit

Permalink
[Clang] Skip shadow warnings for enum constants in distinct class sco…
Browse files Browse the repository at this point in the history
…pes (#115656)

Fixes #62588
  • Loading branch information
a-tarasyuk authored Nov 19, 2024
1 parent 43f84e7 commit 738a047
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------------------------

Expand Down
11 changes: 8 additions & 3 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions clang/test/SemaCXX/warn-shadow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 738a047

Please sign in to comment.