@@ -8357,28 +8357,40 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
8357
8357
8358
8358
unsigned WarningDiag = diag::warn_decl_shadow;
8359
8359
SourceLocation CaptureLoc;
8360
- if (isa<VarDecl>(D) && isa<VarDecl>(ShadowedDecl) && NewDC &&
8361
- isa<CXXMethodDecl>(NewDC)) {
8360
+ if (isa<VarDecl>(D) && NewDC && isa<CXXMethodDecl>(NewDC)) {
8362
8361
if (const auto *RD = dyn_cast<CXXRecordDecl>(NewDC->getParent())) {
8363
8362
if (RD->isLambda() && OldDC->Encloses(NewDC->getLexicalParent())) {
8364
- if (RD->getLambdaCaptureDefault() == LCD_None) {
8365
- // Try to avoid warnings for lambdas with an explicit capture list.
8363
+ if (const auto *VD = dyn_cast<VarDecl>(ShadowedDecl)) {
8366
8364
const auto *LSI = cast<LambdaScopeInfo>(getCurFunction());
8367
- // Warn only when the lambda captures the shadowed decl explicitly.
8368
- CaptureLoc = getCaptureLocation(LSI, cast<VarDecl>(ShadowedDecl));
8369
- if (CaptureLoc.isInvalid())
8370
- WarningDiag = diag::warn_decl_shadow_uncaptured_local;
8371
- } else {
8372
- // Remember that this was shadowed so we can avoid the warning if the
8373
- // shadowed decl isn't captured and the warning settings allow it.
8365
+ if (RD->getLambdaCaptureDefault() == LCD_None) {
8366
+ // Try to avoid warnings for lambdas with an explicit capture
8367
+ // list. Warn only when the lambda captures the shadowed decl
8368
+ // explicitly.
8369
+ CaptureLoc = getCaptureLocation(LSI, VD);
8370
+ if (CaptureLoc.isInvalid())
8371
+ WarningDiag = diag::warn_decl_shadow_uncaptured_local;
8372
+ } else {
8373
+ // Remember that this was shadowed so we can avoid the warning if
8374
+ // the shadowed decl isn't captured and the warning settings allow
8375
+ // it.
8376
+ cast<LambdaScopeInfo>(getCurFunction())
8377
+ ->ShadowingDecls.push_back({D, VD});
8378
+ return;
8379
+ }
8380
+ }
8381
+ if (isa<FieldDecl>(ShadowedDecl)) {
8382
+ // If lambda can capture this, then emit default shadowing warning,
8383
+ // Otherwise it is not really a shadowing case since field is not
8384
+ // available in lambda's body.
8385
+ // At this point we don't know that lambda can capture this, so
8386
+ // remember that this was shadowed and delay until we know.
8374
8387
cast<LambdaScopeInfo>(getCurFunction())
8375
- ->ShadowingDecls.push_back(
8376
- {cast<VarDecl>(D), cast<VarDecl>(ShadowedDecl)});
8388
+ ->ShadowingDecls.push_back({D, ShadowedDecl});
8377
8389
return;
8378
8390
}
8379
8391
}
8380
-
8381
- if (cast<VarDecl>(ShadowedDecl) ->hasLocalStorage()) {
8392
+ if (const auto *VD = dyn_cast<VarDecl>(ShadowedDecl);
8393
+ VD && VD ->hasLocalStorage()) {
8382
8394
// A variable can't shadow a local variable in an enclosing scope, if
8383
8395
// they are separated by a non-capturing declaration context.
8384
8396
for (DeclContext *ParentDC = NewDC;
@@ -8429,19 +8441,28 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
8429
8441
/// when these variables are captured by the lambda.
8430
8442
void Sema::DiagnoseShadowingLambdaDecls(const LambdaScopeInfo *LSI) {
8431
8443
for (const auto &Shadow : LSI->ShadowingDecls) {
8432
- const VarDecl *ShadowedDecl = Shadow.ShadowedDecl;
8444
+ const NamedDecl *ShadowedDecl = Shadow.ShadowedDecl;
8433
8445
// Try to avoid the warning when the shadowed decl isn't captured.
8434
- SourceLocation CaptureLoc = getCaptureLocation(LSI, ShadowedDecl);
8435
8446
const DeclContext *OldDC = ShadowedDecl->getDeclContext();
8436
- Diag(Shadow.VD->getLocation(), CaptureLoc.isInvalid()
8437
- ? diag::warn_decl_shadow_uncaptured_local
8438
- : diag::warn_decl_shadow)
8439
- << Shadow.VD->getDeclName()
8440
- << computeShadowedDeclKind(ShadowedDecl, OldDC) << OldDC;
8441
- if (!CaptureLoc.isInvalid())
8442
- Diag(CaptureLoc, diag::note_var_explicitly_captured_here)
8443
- << Shadow.VD->getDeclName() << /*explicitly*/ 0;
8444
- Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration);
8447
+ if (const auto *VD = dyn_cast<VarDecl>(ShadowedDecl)) {
8448
+ SourceLocation CaptureLoc = getCaptureLocation(LSI, VD);
8449
+ Diag(Shadow.VD->getLocation(),
8450
+ CaptureLoc.isInvalid() ? diag::warn_decl_shadow_uncaptured_local
8451
+ : diag::warn_decl_shadow)
8452
+ << Shadow.VD->getDeclName()
8453
+ << computeShadowedDeclKind(ShadowedDecl, OldDC) << OldDC;
8454
+ if (CaptureLoc.isValid())
8455
+ Diag(CaptureLoc, diag::note_var_explicitly_captured_here)
8456
+ << Shadow.VD->getDeclName() << /*explicitly*/ 0;
8457
+ Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration);
8458
+ } else if (isa<FieldDecl>(ShadowedDecl)) {
8459
+ Diag(Shadow.VD->getLocation(),
8460
+ LSI->isCXXThisCaptured() ? diag::warn_decl_shadow
8461
+ : diag::warn_decl_shadow_uncaptured_local)
8462
+ << Shadow.VD->getDeclName()
8463
+ << computeShadowedDeclKind(ShadowedDecl, OldDC) << OldDC;
8464
+ Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration);
8465
+ }
8445
8466
}
8446
8467
}
8447
8468
0 commit comments