@@ -8396,28 +8396,40 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
8396
8396
8397
8397
unsigned WarningDiag = diag::warn_decl_shadow;
8398
8398
SourceLocation CaptureLoc;
8399
- if (isa<VarDecl>(D) && isa<VarDecl>(ShadowedDecl) && NewDC &&
8400
- isa<CXXMethodDecl>(NewDC)) {
8399
+ if (isa<VarDecl>(D) && NewDC && isa<CXXMethodDecl>(NewDC)) {
8401
8400
if (const auto *RD = dyn_cast<CXXRecordDecl>(NewDC->getParent())) {
8402
8401
if (RD->isLambda() && OldDC->Encloses(NewDC->getLexicalParent())) {
8403
- if (RD->getLambdaCaptureDefault() == LCD_None) {
8404
- // Try to avoid warnings for lambdas with an explicit capture list.
8402
+ if (const auto *VD = dyn_cast<VarDecl>(ShadowedDecl)) {
8405
8403
const auto *LSI = cast<LambdaScopeInfo>(getCurFunction());
8406
- // Warn only when the lambda captures the shadowed decl explicitly.
8407
- CaptureLoc = getCaptureLocation(LSI, cast<VarDecl>(ShadowedDecl));
8408
- if (CaptureLoc.isInvalid())
8409
- WarningDiag = diag::warn_decl_shadow_uncaptured_local;
8410
- } else {
8411
- // Remember that this was shadowed so we can avoid the warning if the
8412
- // shadowed decl isn't captured and the warning settings allow it.
8404
+ if (RD->getLambdaCaptureDefault() == LCD_None) {
8405
+ // Try to avoid warnings for lambdas with an explicit capture
8406
+ // list. Warn only when the lambda captures the shadowed decl
8407
+ // explicitly.
8408
+ CaptureLoc = getCaptureLocation(LSI, VD);
8409
+ if (CaptureLoc.isInvalid())
8410
+ WarningDiag = diag::warn_decl_shadow_uncaptured_local;
8411
+ } else {
8412
+ // Remember that this was shadowed so we can avoid the warning if
8413
+ // the shadowed decl isn't captured and the warning settings allow
8414
+ // it.
8415
+ cast<LambdaScopeInfo>(getCurFunction())
8416
+ ->ShadowingDecls.push_back({D, VD});
8417
+ return;
8418
+ }
8419
+ }
8420
+ if (isa<FieldDecl>(ShadowedDecl)) {
8421
+ // If lambda can capture this, then emit default shadowing warning,
8422
+ // Otherwise it is not really a shadowing case since field is not
8423
+ // available in lambda's body.
8424
+ // At this point we don't know that lambda can capture this, so
8425
+ // remember that this was shadowed and delay until we know.
8413
8426
cast<LambdaScopeInfo>(getCurFunction())
8414
- ->ShadowingDecls.push_back(
8415
- {cast<VarDecl>(D), cast<VarDecl>(ShadowedDecl)});
8427
+ ->ShadowingDecls.push_back({D, ShadowedDecl});
8416
8428
return;
8417
8429
}
8418
8430
}
8419
-
8420
- if (cast<VarDecl>(ShadowedDecl) ->hasLocalStorage()) {
8431
+ if (const auto *VD = dyn_cast<VarDecl>(ShadowedDecl);
8432
+ VD && VD ->hasLocalStorage()) {
8421
8433
// A variable can't shadow a local variable in an enclosing scope, if
8422
8434
// they are separated by a non-capturing declaration context.
8423
8435
for (DeclContext *ParentDC = NewDC;
@@ -8468,19 +8480,28 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
8468
8480
/// when these variables are captured by the lambda.
8469
8481
void Sema::DiagnoseShadowingLambdaDecls(const LambdaScopeInfo *LSI) {
8470
8482
for (const auto &Shadow : LSI->ShadowingDecls) {
8471
- const VarDecl *ShadowedDecl = Shadow.ShadowedDecl;
8483
+ const NamedDecl *ShadowedDecl = Shadow.ShadowedDecl;
8472
8484
// Try to avoid the warning when the shadowed decl isn't captured.
8473
- SourceLocation CaptureLoc = getCaptureLocation(LSI, ShadowedDecl);
8474
8485
const DeclContext *OldDC = ShadowedDecl->getDeclContext();
8475
- Diag(Shadow.VD->getLocation(), CaptureLoc.isInvalid()
8476
- ? diag::warn_decl_shadow_uncaptured_local
8477
- : diag::warn_decl_shadow)
8478
- << Shadow.VD->getDeclName()
8479
- << computeShadowedDeclKind(ShadowedDecl, OldDC) << OldDC;
8480
- if (!CaptureLoc.isInvalid())
8481
- Diag(CaptureLoc, diag::note_var_explicitly_captured_here)
8482
- << Shadow.VD->getDeclName() << /*explicitly*/ 0;
8483
- Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration);
8486
+ if (const auto *VD = dyn_cast<VarDecl>(ShadowedDecl)) {
8487
+ SourceLocation CaptureLoc = getCaptureLocation(LSI, VD);
8488
+ Diag(Shadow.VD->getLocation(),
8489
+ CaptureLoc.isInvalid() ? diag::warn_decl_shadow_uncaptured_local
8490
+ : diag::warn_decl_shadow)
8491
+ << Shadow.VD->getDeclName()
8492
+ << computeShadowedDeclKind(ShadowedDecl, OldDC) << OldDC;
8493
+ if (CaptureLoc.isValid())
8494
+ Diag(CaptureLoc, diag::note_var_explicitly_captured_here)
8495
+ << Shadow.VD->getDeclName() << /*explicitly*/ 0;
8496
+ Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration);
8497
+ } else if (isa<FieldDecl>(ShadowedDecl)) {
8498
+ Diag(Shadow.VD->getLocation(),
8499
+ LSI->isCXXThisCaptured() ? diag::warn_decl_shadow
8500
+ : diag::warn_decl_shadow_uncaptured_local)
8501
+ << Shadow.VD->getDeclName()
8502
+ << computeShadowedDeclKind(ShadowedDecl, OldDC) << OldDC;
8503
+ Diag(ShadowedDecl->getLocation(), diag::note_previous_declaration);
8504
+ }
8484
8505
}
8485
8506
}
8486
8507
0 commit comments