@@ -5530,6 +5530,10 @@ Sema::CheckTypedefForVariablyModifiedType(Scope *S, TypedefNameDecl *NewTD) {
5530
5530
NamedDecl*
5531
5531
Sema::ActOnTypedefNameDecl(Scope *S, DeclContext *DC, TypedefNameDecl *NewTD,
5532
5532
LookupResult &Previous, bool &Redeclaration) {
5533
+
5534
+ // Find the shadowed declaration before filtering for scope.
5535
+ NamedDecl *ShadowedDecl = getShadowedDeclaration(NewTD, Previous);
5536
+
5533
5537
// Merge the decl with the existing one if appropriate. If the decl is
5534
5538
// in an outer scope, it isn't the same thing.
5535
5539
FilterLookupForScope(Previous, DC, S, /*ConsiderLinkage*/false,
@@ -5540,6 +5544,9 @@ Sema::ActOnTypedefNameDecl(Scope *S, DeclContext *DC, TypedefNameDecl *NewTD,
5540
5544
MergeTypedefNameDecl(S, NewTD, Previous);
5541
5545
}
5542
5546
5547
+ if (ShadowedDecl && !Redeclaration)
5548
+ CheckShadow(NewTD, ShadowedDecl, Previous);
5549
+
5543
5550
// If this is the C FILE type, notify the AST context.
5544
5551
if (IdentifierInfo *II = NewTD->getIdentifier())
5545
5552
if (!NewTD->isInvalidDecl() &&
@@ -6671,13 +6678,25 @@ NamedDecl *Sema::ActOnVariableDeclarator(
6671
6678
}
6672
6679
6673
6680
/// Enum describing the %select options in diag::warn_decl_shadow.
6674
- enum ShadowedDeclKind { SDK_Local, SDK_Global, SDK_StaticMember, SDK_Field };
6681
+ enum ShadowedDeclKind {
6682
+ SDK_Local,
6683
+ SDK_Global,
6684
+ SDK_StaticMember,
6685
+ SDK_Field,
6686
+ SDK_Typedef,
6687
+ SDK_Using
6688
+ };
6675
6689
6676
6690
/// Determine what kind of declaration we're shadowing.
6677
6691
static ShadowedDeclKind computeShadowedDeclKind(const NamedDecl *ShadowedDecl,
6678
6692
const DeclContext *OldDC) {
6679
- if (isa<RecordDecl>(OldDC))
6693
+ if (isa<TypeAliasDecl>(ShadowedDecl))
6694
+ return SDK_Using;
6695
+ else if (isa<TypedefDecl>(ShadowedDecl))
6696
+ return SDK_Typedef;
6697
+ else if (isa<RecordDecl>(OldDC))
6680
6698
return isa<FieldDecl>(ShadowedDecl) ? SDK_Field : SDK_StaticMember;
6699
+
6681
6700
return OldDC->isFileContext() ? SDK_Global : SDK_Local;
6682
6701
}
6683
6702
@@ -6692,28 +6711,44 @@ static SourceLocation getCaptureLocation(const LambdaScopeInfo *LSI,
6692
6711
return SourceLocation();
6693
6712
}
6694
6713
6714
+ static bool shouldWarnIfShadowedDecl(const DiagnosticsEngine &Diags,
6715
+ const LookupResult &R) {
6716
+ // Only diagnose if we're shadowing an unambiguous field or variable.
6717
+ if (R.getResultKind() != LookupResult::Found)
6718
+ return false;
6719
+
6720
+ // Return false if warning is ignored.
6721
+ return !Diags.isIgnored(diag::warn_decl_shadow, R.getNameLoc());
6722
+ }
6723
+
6695
6724
/// \brief Return the declaration shadowed by the given variable \p D, or null
6696
6725
/// if it doesn't shadow any declaration or shadowing warnings are disabled.
6697
6726
NamedDecl *Sema::getShadowedDeclaration(const VarDecl *D,
6698
6727
const LookupResult &R) {
6699
- // Return if warning is ignored.
6700
- if (Diags.isIgnored(diag::warn_decl_shadow, R.getNameLoc()))
6728
+ if (!shouldWarnIfShadowedDecl(Diags, R))
6701
6729
return nullptr;
6702
6730
6703
6731
// Don't diagnose declarations at file scope.
6704
6732
if (D->hasGlobalStorage())
6705
6733
return nullptr;
6706
6734
6707
- // Only diagnose if we're shadowing an unambiguous field or variable.
6708
- if (R.getResultKind() != LookupResult::Found)
6709
- return nullptr;
6710
-
6711
6735
NamedDecl *ShadowedDecl = R.getFoundDecl();
6712
6736
return isa<VarDecl>(ShadowedDecl) || isa<FieldDecl>(ShadowedDecl)
6713
6737
? ShadowedDecl
6714
6738
: nullptr;
6715
6739
}
6716
6740
6741
+ /// \brief Return the declaration shadowed by the given typedef \p D, or null
6742
+ /// if it doesn't shadow any declaration or shadowing warnings are disabled.
6743
+ NamedDecl *Sema::getShadowedDeclaration(const TypedefNameDecl *D,
6744
+ const LookupResult &R) {
6745
+ if (!shouldWarnIfShadowedDecl(Diags, R))
6746
+ return nullptr;
6747
+
6748
+ NamedDecl *ShadowedDecl = R.getFoundDecl();
6749
+ return isa<TypedefNameDecl>(ShadowedDecl) ? ShadowedDecl : nullptr;
6750
+ }
6751
+
6717
6752
/// \brief Diagnose variable or built-in function shadowing. Implements
6718
6753
/// -Wshadow.
6719
6754
///
@@ -6723,7 +6758,7 @@ NamedDecl *Sema::getShadowedDeclaration(const VarDecl *D,
6723
6758
/// \param ShadowedDecl the declaration that is shadowed by the given variable
6724
6759
/// \param R the lookup of the name
6725
6760
///
6726
- void Sema::CheckShadow(VarDecl *D, NamedDecl *ShadowedDecl,
6761
+ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
6727
6762
const LookupResult &R) {
6728
6763
DeclContext *NewDC = D->getDeclContext();
6729
6764
@@ -6735,13 +6770,13 @@ void Sema::CheckShadow(VarDecl *D, NamedDecl *ShadowedDecl,
6735
6770
6736
6771
// Fields shadowed by constructor parameters are a special case. Usually
6737
6772
// the constructor initializes the field with the parameter.
6738
- if (isa<CXXConstructorDecl>(NewDC) && isa<ParmVarDecl>(D)) {
6739
- // Remember that this was shadowed so we can either warn about its
6740
- // modification or its existence depending on warning settings.
6741
- D = D->getCanonicalDecl();
6742
- ShadowingDecls.insert({D , FD});
6743
- return;
6744
- }
6773
+ if (isa<CXXConstructorDecl>(NewDC))
6774
+ if (const auto PVD = dyn_cast<ParmVarDecl>(D)) {
6775
+ // Remember that this was shadowed so we can either warn about its
6776
+ // modification or its existence depending on warning settings.
6777
+ ShadowingDecls.insert({PVD->getCanonicalDecl() , FD});
6778
+ return;
6779
+ }
6745
6780
}
6746
6781
6747
6782
if (VarDecl *shadowedVar = dyn_cast<VarDecl>(ShadowedDecl))
@@ -6759,7 +6794,8 @@ void Sema::CheckShadow(VarDecl *D, NamedDecl *ShadowedDecl,
6759
6794
6760
6795
unsigned WarningDiag = diag::warn_decl_shadow;
6761
6796
SourceLocation CaptureLoc;
6762
- if (isa<VarDecl>(ShadowedDecl) && NewDC && isa<CXXMethodDecl>(NewDC)) {
6797
+ if (isa<VarDecl>(D) && isa<VarDecl>(ShadowedDecl) && NewDC &&
6798
+ isa<CXXMethodDecl>(NewDC)) {
6763
6799
if (const auto *RD = dyn_cast<CXXRecordDecl>(NewDC->getParent())) {
6764
6800
if (RD->isLambda() && OldDC->Encloses(NewDC->getLexicalParent())) {
6765
6801
if (RD->getLambdaCaptureDefault() == LCD_None) {
@@ -6773,7 +6809,8 @@ void Sema::CheckShadow(VarDecl *D, NamedDecl *ShadowedDecl,
6773
6809
// Remember that this was shadowed so we can avoid the warning if the
6774
6810
// shadowed decl isn't captured and the warning settings allow it.
6775
6811
cast<LambdaScopeInfo>(getCurFunction())
6776
- ->ShadowingDecls.push_back({D, cast<VarDecl>(ShadowedDecl)});
6812
+ ->ShadowingDecls.push_back(
6813
+ {cast<VarDecl>(D), cast<VarDecl>(ShadowedDecl)});
6777
6814
return;
6778
6815
}
6779
6816
}
0 commit comments