[PATCH] D52400: Improve -Wshadow warnings with enumerators

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 28 10:13:25 PDT 2018


aaron.ballman updated this revision to Diff 167507.
aaron.ballman marked an inline comment as done.
aaron.ballman added a comment.

Updating based on review feedback.


https://reviews.llvm.org/D52400

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


Index: test/SemaCXX/warn-shadow.cpp
===================================================================
--- test/SemaCXX/warn-shadow.cpp
+++ test/SemaCXX/warn-shadow.cpp
@@ -222,3 +222,6 @@
   };
 }
 }
+
+int PR24718;
+enum class X { PR24718 }; // Ok, not shadowing
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,11 @@
     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)) &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52400.167507.patch
Type: text/x-patch
Size: 1945 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180928/9b862744/attachment-0001.bin>


More information about the cfe-commits mailing list