[PATCH] D55321: Do not check for parameters shadowing fields in function declarations

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 5 06:37:22 PST 2018


aaron.ballman created this revision.
aaron.ballman added reviewers: rsmith, george.karpenkov.
Herald added subscribers: kristof.beyls, javed.absar.

This patch changes the way we handle `-Wshadow-field` so that we do not issue diagnostics for parameters in function declarations (as those are harmless), but we continue to diagnose parameters of function definitions.


https://reviews.llvm.org/D55321

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


Index: test/SemaCXX/warn-shadow.cpp
===================================================================
--- test/SemaCXX/warn-shadow.cpp
+++ test/SemaCXX/warn-shadow.cpp
@@ -233,6 +233,7 @@
   void E() {
     extern void f(int B); // Ok
   }
+  void F(int B); // Ok, declaration; not definition.
 };
 
 class Private {
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -12427,13 +12427,6 @@
         D.setInvalidType(true);
       }
     }
-
-    if (LangOpts.CPlusPlus) {
-      DeclarationNameInfo DNI = GetNameForDeclarator(D);
-      if (auto *RD = dyn_cast<CXXRecordDecl>(CurContext))
-        CheckShadowInheritedFields(DNI.getLoc(), DNI.getName(), RD,
-                                   /*DeclIsField*/ false);
-    }
   }
 
   // Temporarily put parameter variables in the translation unit, not
Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -12141,6 +12141,18 @@
       if (!Param->getType().isConstQualified())
         Diag(Param->getLocation(), diag::err_attribute_pointers_only)
             << Attr->getSpelling() << 1;
+
+    // Check for parameter names shadowing fields from the class.
+    if (LangOpts.CPlusPlus && !Param->isInvalidDecl()) {
+      // The owning context for the parameter should be the function, but we
+      // want to see if this function's declaration context is a record.
+      DeclContext *DC = Param->getDeclContext();
+      if (DC && DC->isFunctionOrMethod()) {
+        if (auto *RD = dyn_cast<CXXRecordDecl>(DC->getParent()))
+          CheckShadowInheritedFields(Param->getLocation(), Param->getDeclName(),
+                                     RD, /*DeclIsField*/ false);
+      }
+    }
   }
 
   return HasInvalidParm;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55321.176806.patch
Type: text/x-patch
Size: 1929 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181205/548edae3/attachment.bin>


More information about the cfe-commits mailing list