]> granicus.if.org Git - clang/commitdiff
Improve -Wshadow warnings with enumerators.
authorAaron Ballman <aaron@aaronballman.com>
Thu, 11 Oct 2018 16:40:18 +0000 (16:40 +0000)
committerAaron Ballman <aaron@aaronballman.com>
Thu, 11 Oct 2018 16:40:18 +0000 (16:40 +0000)
Addresses PR24718 by checking for enumerators that shadow other enumerators. Catches issues like:

enum E1{e1};
void f(void) {
  enum E2{e1};
}

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@344259 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/Sema/warn-shadow.c
test/SemaCXX/warn-shadow.cpp

index 7ef1ad2bbb0e96f75ca076eba61062b64fe9a985..95623ef100f85b941b2be59f650933412c1db324 100644 (file)
@@ -16269,8 +16269,10 @@ Decl *Sema::ActOnEnumConstant(Scope *S, Decl *theEnumDecl, Decl *lastEnumConst,
 
   // 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);
@@ -16293,6 +16295,11 @@ Decl *Sema::ActOnEnumConstant(Scope *S, Decl *theEnumDecl, Decl *lastEnumConst,
     return nullptr;
 
   if (PrevDecl) {
+    if (!TheEnumDecl->isScoped()) {
+      // 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 32aca8d612b21d6d412e47df0145a43bfe7f2e42..8d1d0aed693f0c92479f54de0190d5a9c41c19e0 100644 (file)
@@ -59,3 +59,8 @@ void rdar8883302() {
 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 3d09c786285ad8bdc783b0d59eb27f67ccb54c4d..e4ad352788a6e7837d9db9991f4ad5a6ee8d1029 100644 (file)
@@ -222,3 +222,6 @@ void f(int a) {
   };
 }
 }
+
+int PR24718;
+enum class X { PR24718 }; // Ok, not shadowing