Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ Improvements to Clang's diagnostics

- Clang now diagnoses ``[[deprecated]]`` attribute usage on local variables (#GH90073).

- Clang now omits shadow warnings for enum constants in separate class scopes (#GH62588).

Improvements to Clang's time-trace
----------------------------------

Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -3453,7 +3453,7 @@ class Sema final : public SemaBase {
///
/// \param ShadowedDecl the declaration that is shadowed by the given variable
/// \param R the lookup of the name
void CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
void CheckShadow(Scope *S, NamedDecl *D, NamedDecl *ShadowedDecl,
const LookupResult &R);

/// Check -Wshadow without the advantage of a previous lookup.
Expand Down
15 changes: 10 additions & 5 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6732,7 +6732,7 @@ Sema::ActOnTypedefNameDecl(Scope *S, DeclContext *DC, TypedefNameDecl *NewTD,
}

if (ShadowedDecl && !Redeclaration)
CheckShadow(NewTD, ShadowedDecl, Previous);
CheckShadow(S, NewTD, ShadowedDecl, Previous);

// If this is the C FILE type, notify the AST context.
if (IdentifierInfo *II = NewTD->getIdentifier())
Expand Down Expand Up @@ -8092,7 +8092,7 @@ NamedDecl *Sema::ActOnVariableDeclarator(

// Diagnose shadowed variables iff this isn't a redeclaration.
if (!IsPlaceholderVariable && ShadowedDecl && !D.isRedeclaration())
CheckShadow(NewVD, ShadowedDecl, Previous);
CheckShadow(S, NewVD, ShadowedDecl, Previous);

ProcessPragmaWeak(S, NewVD);

Expand Down Expand Up @@ -8237,7 +8237,7 @@ NamedDecl *Sema::getShadowedDeclaration(const BindingDecl *D,
: nullptr;
}

void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
void Sema::CheckShadow(Scope *S, NamedDecl *D, NamedDecl *ShadowedDecl,
const LookupResult &R) {
DeclContext *NewDC = D->getDeclContext();

Expand Down Expand Up @@ -8341,6 +8341,11 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
// shadowing context, but that's just a false negative.
}

// Skip shadowing check if we're in a class scope, dealing with an enum
// constant in a different context.
if (S->isClassScope() && isa<EnumConstantDecl>(D) &&
!OldDC->Equals(NewDC->getRedeclContext()))
return;

DeclarationName Name = R.getLookupName();

Expand Down Expand Up @@ -8389,7 +8394,7 @@ void Sema::CheckShadow(Scope *S, VarDecl *D) {
RedeclarationKind::ForVisibleRedeclaration);
LookupName(R, S);
if (NamedDecl *ShadowedDecl = getShadowedDeclaration(D, R))
CheckShadow(D, ShadowedDecl, R);
CheckShadow(S, D, ShadowedDecl, R);
}

/// Check if 'E', which is an expression that is about to be modified, refers
Expand Down Expand Up @@ -19736,7 +19741,7 @@ Decl *Sema::ActOnEnumConstant(Scope *S, Decl *theEnumDecl, Decl *lastEnumConst,
if (PrevDecl) {
if (!TheEnumDecl->isScoped() && isa<ValueDecl>(PrevDecl)) {
// Check for other kinds of shadowing not already handled.
CheckShadow(New, PrevDecl, R);
CheckShadow(S, New, PrevDecl, R);
}

// When in C++, we may get a TagDecl with the same name; in this case the
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ Sema::ActOnDecompositionDeclarator(Scope *S, Declarator &D,
Diag(Old->getLocation(), diag::note_previous_definition);
}
} else if (ShadowedDecl && !D.isRedeclaration()) {
CheckShadow(BD, ShadowedDecl, Previous);
CheckShadow(S, BD, ShadowedDecl, Previous);
}
PushOnScopeChains(BD, S, true);
Bindings.push_back(BD);
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
Loading