[llvm-branch-commits] [clang] [clang] Avoid -Wshadow warning when init-capture named same as class … (PR #84912)

2024-03-12 Thread Neil Henning via llvm-branch-commits

https://github.com/sheredom milestoned 
https://github.com/llvm/llvm-project/pull/84912
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [clang] Avoid -Wshadow warning when init-capture named same as class … (PR #84912)

2024-03-12 Thread Neil Henning via llvm-branch-commits

https://github.com/sheredom created 
https://github.com/llvm/llvm-project/pull/84912

…field (#74512)

Shadowing warning doesn't make much sense since field is not available in 
lambda's body without capturing this.

Fixes https://github.com/llvm/llvm-project/issues/71976

>From d2740d74a34982f21ec18e2cc494d67187d5c7f2 Mon Sep 17 00:00:00 2001
From: Mariya Podchishchaeva 
Date: Mon, 12 Feb 2024 12:44:20 +0300
Subject: [PATCH] [clang] Avoid -Wshadow warning when init-capture named same
 as class field (#74512)

Shadowing warning doesn't make much sense since field is not available
in lambda's body without capturing this.

Fixes https://github.com/llvm/llvm-project/issues/71976
---
 clang/docs/ReleaseNotes.rst   |  3 +
 clang/include/clang/Sema/ScopeInfo.h  |  4 +-
 clang/lib/Sema/SemaDecl.cpp   | 73 +--
 clang/test/SemaCXX/warn-shadow-in-lambdas.cpp | 92 ++-
 4 files changed, 141 insertions(+), 31 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index fc27297aea2d6c..14703e23ba0be0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -899,6 +899,9 @@ Bug Fixes in This Version
 - Clang now doesn't produce false-positive warning `-Wconstant-logical-operand`
   for logical operators in C23.
   Fixes (`#64356 `_).
+- Clang's ``-Wshadow`` no longer warns when an init-capture is named the same 
as
+  a class field unless the lambda can capture this.
+  Fixes (`#71976 `_)
 
 Bug Fixes to Compiler Builtins
 ^^
diff --git a/clang/include/clang/Sema/ScopeInfo.h 
b/clang/include/clang/Sema/ScopeInfo.h
index 6eaa74382685ba..06e47eed4e93b6 100644
--- a/clang/include/clang/Sema/ScopeInfo.h
+++ b/clang/include/clang/Sema/ScopeInfo.h
@@ -925,8 +925,8 @@ class LambdaScopeInfo final :
   /// that were defined in parent contexts. Used to avoid warnings when the
   /// shadowed variables are uncaptured by this lambda.
   struct ShadowedOuterDecl {
-const VarDecl *VD;
-const VarDecl *ShadowedDecl;
+const NamedDecl *VD;
+const NamedDecl *ShadowedDecl;
   };
   llvm::SmallVector ShadowingDecls;
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index a300badc6d0260..f5bb3e0b42e26c 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8396,28 +8396,40 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl 
*ShadowedDecl,
 
   unsigned WarningDiag = diag::warn_decl_shadow;
   SourceLocation CaptureLoc;
-  if (isa(D) && isa(ShadowedDecl) && NewDC &&
-  isa(NewDC)) {
+  if (isa(D) && NewDC && isa(NewDC)) {
 if (const auto *RD = dyn_cast(NewDC->getParent())) {
   if (RD->isLambda() && OldDC->Encloses(NewDC->getLexicalParent())) {
-if (RD->getLambdaCaptureDefault() == LCD_None) {
-  // Try to avoid warnings for lambdas with an explicit capture list.
+if (const auto *VD = dyn_cast(ShadowedDecl)) {
   const auto *LSI = cast(getCurFunction());
-  // Warn only when the lambda captures the shadowed decl explicitly.
-  CaptureLoc = getCaptureLocation(LSI, cast(ShadowedDecl));
-  if (CaptureLoc.isInvalid())
-WarningDiag = diag::warn_decl_shadow_uncaptured_local;
-} else {
-  // Remember that this was shadowed so we can avoid the warning if the
-  // shadowed decl isn't captured and the warning settings allow it.
+  if (RD->getLambdaCaptureDefault() == LCD_None) {
+// Try to avoid warnings for lambdas with an explicit capture
+// list. Warn only when the lambda captures the shadowed decl
+// explicitly.
+CaptureLoc = getCaptureLocation(LSI, VD);
+if (CaptureLoc.isInvalid())
+  WarningDiag = diag::warn_decl_shadow_uncaptured_local;
+  } else {
+// Remember that this was shadowed so we can avoid the warning if
+// the shadowed decl isn't captured and the warning settings allow
+// it.
+cast(getCurFunction())
+->ShadowingDecls.push_back({D, VD});
+return;
+  }
+}
+if (isa(ShadowedDecl)) {
+  // If lambda can capture this, then emit default shadowing warning,
+  // Otherwise it is not really a shadowing case since field is not
+  // available in lambda's body.
+  // At this point we don't know that lambda can capture this, so
+  // remember that this was shadowed and delay until we know.
   cast(getCurFunction())
-  ->ShadowingDecls.push_back(
-  {cast(D), cast(ShadowedDecl)});
+  ->ShadowingDecls.push_back({D, ShadowedDecl});
   return;
 }
   }
-
-  if (cast(ShadowedDecl)->hasLocalStorage()) {
+  if (const auto *VD = dyn_cast(Shadowed

[llvm-branch-commits] [clang] [clang] Avoid -Wshadow warning when init-capture named same as class … (PR #84912)

2024-03-12 Thread Neil Henning via llvm-branch-commits

https://github.com/sheredom edited 
https://github.com/llvm/llvm-project/pull/84912
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] release/18x: [clang] Avoid -Wshadow warning when init-capture named same as class … (PR #84912)

2024-03-12 Thread Neil Henning via llvm-branch-commits

sheredom wrote:

Yeah indeed - its not a clang 17 regression. It's a _clang 16_ regression. At 
the minute we're holding our internal compiler toolchain on clang 16, and for 
our users we've got to special case disable the warning entirely for clang 17.

https://github.com/llvm/llvm-project/pull/84912
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits