From 38ae8879fa59652fb89e9383c6ff89bfad099439 Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Fri, 2 Nov 2018 21:04:44 +0000 Subject: [PATCH] Diagnose parameter names that shadow the names of inherited fields under -Wshadow-field. This addresses PR34120. Note, unlike GCC, we take into account the accessibility of the field when deciding whether to warn or not. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@346041 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 19 ++++--- include/clang/Sema/Sema.h | 3 +- lib/Sema/SemaDecl.cpp | 7 +++ lib/Sema/SemaDeclCXX.cpp | 29 +++++------ test/SemaCXX/warn-shadow.cpp | 58 ++++++++++++++++------ 5 files changed, 76 insertions(+), 40 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 8403278746..d322457d77 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -9467,16 +9467,15 @@ def warn_block_literal_qualifiers_on_omitted_return_type : Warning< InGroup; def ext_warn_gnu_final : ExtWarn< - "__final is a GNU extension, consider using C++11 final">, - InGroup; - -def warn_shadow_field : - Warning<"non-static data member %0 of %1 shadows member inherited from " - "type %2">, - InGroup, DefaultIgnore; -def note_shadow_field : Note<"declared here">; - -def err_multiversion_required_in_redecl : Error< + "__final is a GNU extension, consider using C++11 final">, + InGroup; + +def warn_shadow_field : Warning< + "%select{parameter|non-static data member}3 %0 %select{|of %1 }3shadows " + "member inherited from type %2">, InGroup, DefaultIgnore; +def note_shadow_field : Note<"declared here">; + +def err_multiversion_required_in_redecl : Error< "function declaration is missing %select{'target'|'cpu_specific' or " "'cpu_dispatch'}0 attribute in a multiversioned function">; def note_multiversioning_caused_here : Note< diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 4740cca466..e4655246b5 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -10588,7 +10588,8 @@ private: /// Check if there is a field shadowing. void CheckShadowInheritedFields(const SourceLocation &Loc, DeclarationName FieldName, - const CXXRecordDecl *RD); + const CXXRecordDecl *RD, + bool DeclIsField = true); /// Check if the given expression contains 'break' or 'continue' /// statement that produces control flow different from GCC. diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index bf15e25d62..f8068dd9c8 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -12366,6 +12366,13 @@ 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/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 51a4090393..af4be999a1 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -2832,13 +2832,14 @@ static const ParsedAttr *getMSPropertyAttr(const ParsedAttributesView &list) { return nullptr; } -// Check if there is a field shadowing. -void Sema::CheckShadowInheritedFields(const SourceLocation &Loc, - DeclarationName FieldName, - const CXXRecordDecl *RD) { - if (Diags.isIgnored(diag::warn_shadow_field, Loc)) - return; - +// Check if there is a field shadowing. +void Sema::CheckShadowInheritedFields(const SourceLocation &Loc, + DeclarationName FieldName, + const CXXRecordDecl *RD, + bool DeclIsField) { + if (Diags.isIgnored(diag::warn_shadow_field, Loc)) + return; + // To record a shadowed field in a base std::map Bases; auto FieldShadowed = [&](const CXXBaseSpecifier *Specifier, @@ -2872,13 +2873,13 @@ void Sema::CheckShadowInheritedFields(const SourceLocation &Loc, continue; auto BaseField = It->second; assert(BaseField->getAccess() != AS_private); - if (AS_none != - CXXRecordDecl::MergeAccess(P.Access, BaseField->getAccess())) { - Diag(Loc, diag::warn_shadow_field) - << FieldName << RD << Base; - Diag(BaseField->getLocation(), diag::note_shadow_field); - Bases.erase(It); - } + if (AS_none != + CXXRecordDecl::MergeAccess(P.Access, BaseField->getAccess())) { + Diag(Loc, diag::warn_shadow_field) + << FieldName << RD << Base << DeclIsField; + Diag(BaseField->getLocation(), diag::note_shadow_field); + Bases.erase(It); + } } } diff --git a/test/SemaCXX/warn-shadow.cpp b/test/SemaCXX/warn-shadow.cpp index 33203f018c..283fb6df23 100644 --- a/test/SemaCXX/warn-shadow.cpp +++ b/test/SemaCXX/warn-shadow.cpp @@ -59,13 +59,13 @@ class A { // expected-warning-re@+1 4 {{constructor parameter 'f{{[0-4]}}' shadows the field 'f{{[0-9]}}' of 'A'}} A(int f1, int f2, int f3, int f4, double overload_dummy) {} - void test() { - char *field; // expected-warning {{declaration shadows a field of 'A'}} - char *data; // expected-warning {{declaration shadows a static data member of 'A'}} + void test() { + char *field; // expected-warning {{declaration shadows a field of 'A'}} + char *data; // expected-warning {{declaration shadows a static data member of 'A'}} char *a1; // no warning - char *a2; // no warning - char *jj; // no warning - char *jjj; // no warning + char *a2; // no warning + char *jj; // no warning + char *jjj; // no warning } void test2() { @@ -196,14 +196,14 @@ void avoidWarningWhenRedefining(int b) { // expected-note {{previous definition int k; // expected-note {{previous definition is here}} typedef int k; // expected-error {{redefinition of 'k'}} - using l=char; // no warning or error. - using l=char; // no warning or error. - typedef char l; // no warning or error. + using l=char; // no warning or error. + using l=char; // no warning or error. + typedef char l; // 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. -} + typedef char n; // no warning or error. + using n=char; // no warning or error. +} } @@ -219,9 +219,37 @@ void f(int a) { struct A { void g(int a) {} A() { int a; } - }; -} -} + }; +} +} + +namespace PR34120 { +struct A { + int B; // expected-note {{declared here}} +}; + +class C : public A { + void D(int B) {} // expected-warning {{parameter 'B' shadows member inherited from type 'A'}} + void E() { + extern void f(int B); // Ok + } +}; + +class Private { + int B; +}; +class Derived : Private { + void D(int B) {} // Ok +}; + +struct Static { + static int B; +}; + +struct Derived2 : Static { + void D(int B) {} +}; +} int PR24718; enum class X { PR24718 }; // Ok, not shadowing -- 2.40.0