r348400 - Do not check for parameters shadowing fields in function declarations.

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 5 10:56:57 PST 2018


Author: aaronballman
Date: Wed Dec  5 10:56:57 2018
New Revision: 348400

URL: http://llvm.org/viewvc/llvm-project?rev=348400&view=rev
Log:
Do not check for parameters shadowing fields in function declarations.

We would issue a false-positive diagnostic for parameters in function declarations shadowing fields; we now only issue the diagnostic on a function definition instead.

Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/SemaCXX/warn-shadow.cpp

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=348400&r1=348399&r2=348400&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Wed Dec  5 10:56:57 2018
@@ -12141,6 +12141,18 @@ bool Sema::CheckParmsForFunctionDef(Arra
       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;

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=348400&r1=348399&r2=348400&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Dec  5 10:56:57 2018
@@ -12427,13 +12427,6 @@ Decl *Sema::ActOnParamDeclarator(Scope *
         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

Modified: cfe/trunk/test/SemaCXX/warn-shadow.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-shadow.cpp?rev=348400&r1=348399&r2=348400&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-shadow.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-shadow.cpp Wed Dec  5 10:56:57 2018
@@ -201,7 +201,7 @@ void avoidWarningWhenRedefining(int b) {
   typedef char l; // no warning or error.
  
   typedef char n; // no warning or error. 
-  typedef char n; // no warning or error.
+  typedef char n; // no warning or error.
   using n=char; // no warning or error.
 }
 
@@ -225,7 +225,7 @@ void f(int a) {
 
 namespace PR34120 {
 struct A {
-  int B; // expected-note {{declared here}}
+  int B; // expected-note 2 {{declared here}}
 };
 
 class C : public A {
@@ -233,8 +233,13 @@ class C : public A {
   void E() {
     extern void f(int B); // Ok
   }
+  void F(int B); // Ok, declaration; not definition.
+  void G(int B);
 };
 
+void C::G(int B) { // expected-warning {{parameter 'B' shadows member inherited from type 'A'}}
+}
+
 class Private {
   int B;
 };




More information about the cfe-commits mailing list