From: Aaron Ballman Date: Wed, 5 Dec 2018 18:56:57 +0000 (+0000) Subject: Do not check for parameters shadowing fields in function declarations. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=70fd64fb70bed802a4708986a300e6fbb801ae76;p=clang 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@348400 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 4f891e092f..d82bd20eff 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -12141,6 +12141,18 @@ bool Sema::CheckParmsForFunctionDef(ArrayRef Parameters, 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(DC->getParent())) + CheckShadowInheritedFields(Param->getLocation(), Param->getDeclName(), + RD, /*DeclIsField*/ false); + } + } } return HasInvalidParm; diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 5ec5929433..da6d414a42 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -12427,13 +12427,6 @@ Decl *Sema::ActOnParamDeclarator(Scope *S, Declarator &D) { D.setInvalidType(true); } } - - if (LangOpts.CPlusPlus) { - DeclarationNameInfo DNI = GetNameForDeclarator(D); - if (auto *RD = dyn_cast(CurContext)) - CheckShadowInheritedFields(DNI.getLoc(), DNI.getName(), RD, - /*DeclIsField*/ false); - } } // Temporarily put parameter variables in the translation unit, not diff --git a/test/SemaCXX/warn-shadow.cpp b/test/SemaCXX/warn-shadow.cpp index 283fb6df23..f4a904b7ed 100644 --- a/test/SemaCXX/warn-shadow.cpp +++ b/test/SemaCXX/warn-shadow.cpp @@ -201,7 +201,7 @@ void avoidWarningWhenRedefining(int b) { // expected-note {{previous definition 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; };