aaron.ballman created this revision. aaron.ballman added reviewers: rsmith, dblaikie.
Currently, we do not check for enumerators that shadow other enumerators as part of -Wshadow, but gcc does provide such a diagnostic for this case. This is intended to catch shadowing issues like: enum E1{e1}; void f(void) { enum E2{e1}; } This patch addresses PR24718. https://reviews.llvm.org/D52400 Files: lib/Sema/SemaDecl.cpp test/Sema/warn-shadow.c Index: test/Sema/warn-shadow.c =================================================================== --- test/Sema/warn-shadow.c +++ test/Sema/warn-shadow.c @@ -59,3 +59,8 @@ void test8() { int bob; // expected-warning {{declaration shadows a variable in the global scope}} } + +enum PR24718_1{pr24718}; // expected-note {{previous declaration is here}} +void PR24718(void) { + enum PR24718_2{pr24718}; // expected-warning {{declaration shadows a variable in the global scope}} +} Index: lib/Sema/SemaDecl.cpp =================================================================== --- lib/Sema/SemaDecl.cpp +++ lib/Sema/SemaDecl.cpp @@ -16290,8 +16290,10 @@ // Verify that there isn't already something declared with this name in this // scope. - NamedDecl *PrevDecl = LookupSingleName(S, Id, IdLoc, LookupOrdinaryName, - ForVisibleRedeclaration); + LookupResult R(*this, Id, IdLoc, LookupOrdinaryName, ForVisibleRedeclaration); + LookupName(R, S); + NamedDecl *PrevDecl = R.getAsSingle<NamedDecl>(); + if (PrevDecl && PrevDecl->isTemplateParameter()) { // Maybe we will complain about the shadowed template parameter. DiagnoseTemplateParameterShadow(IdLoc, PrevDecl); @@ -16314,6 +16316,9 @@ return nullptr; if (PrevDecl) { + // Check for other kinds of shadowing not already handled. + CheckShadow(New, PrevDecl, R); + // When in C++, we may get a TagDecl with the same name; in this case the // enum constant will 'hide' the tag. assert((getLangOpts().CPlusPlus || !isa<TagDecl>(PrevDecl)) &&
Index: test/Sema/warn-shadow.c =================================================================== --- test/Sema/warn-shadow.c +++ test/Sema/warn-shadow.c @@ -59,3 +59,8 @@ void test8() { int bob; // expected-warning {{declaration shadows a variable in the global scope}} } + +enum PR24718_1{pr24718}; // expected-note {{previous declaration is here}} +void PR24718(void) { + enum PR24718_2{pr24718}; // expected-warning {{declaration shadows a variable in the global scope}} +} Index: lib/Sema/SemaDecl.cpp =================================================================== --- lib/Sema/SemaDecl.cpp +++ lib/Sema/SemaDecl.cpp @@ -16290,8 +16290,10 @@ // Verify that there isn't already something declared with this name in this // scope. - NamedDecl *PrevDecl = LookupSingleName(S, Id, IdLoc, LookupOrdinaryName, - ForVisibleRedeclaration); + LookupResult R(*this, Id, IdLoc, LookupOrdinaryName, ForVisibleRedeclaration); + LookupName(R, S); + NamedDecl *PrevDecl = R.getAsSingle<NamedDecl>(); + if (PrevDecl && PrevDecl->isTemplateParameter()) { // Maybe we will complain about the shadowed template parameter. DiagnoseTemplateParameterShadow(IdLoc, PrevDecl); @@ -16314,6 +16316,9 @@ return nullptr; if (PrevDecl) { + // Check for other kinds of shadowing not already handled. + CheckShadow(New, PrevDecl, R); + // When in C++, we may get a TagDecl with the same name; in this case the // enum constant will 'hide' the tag. assert((getLangOpts().CPlusPlus || !isa<TagDecl>(PrevDecl)) &&
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits