diff options
author | Aaron Ballman <aaron@aaronballman.com> | 2018-11-02 21:04:44 +0000 |
---|---|---|
committer | Aaron Ballman <aaron@aaronballman.com> | 2018-11-02 21:04:44 +0000 |
commit | 38ae8879fa59652fb89e9383c6ff89bfad099439 (patch) | |
tree | 94b0c097467926c381211fe0eb13659cfb03c8bc /lib | |
parent | 90458949c1c82c72ec45267838ad6b313d25b745 (diff) | |
download | clang-38ae8879fa59652fb89e9383c6ff89bfad099439.tar.gz |
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
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Sema/SemaDecl.cpp | 7 | ||||
-rw-r--r-- | lib/Sema/SemaDeclCXX.cpp | 29 |
2 files changed, 22 insertions, 14 deletions
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<CXXRecordDecl>(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<CXXRecordDecl*, NamedDecl*> 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);
+ }
} } |