[openmp] [clang] [flang] [llvm] [libcxxabi] [libcxx] [libc] [clang-tools-extra] [mlir] [compiler-rt] [clang] Avoid -Wshadow warning when init-capture named same as class field (PR #74512)
Mariya Podchishchaeva via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 6 09:23:07 PST 2023
https://github.com/Fznamznon updated https://github.com/llvm/llvm-project/pull/74512
>From 169d962b64b8ae242c3a6d332677296cf7503839 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <mariya.podchishchaeva at intel.com>
Date: Tue, 5 Dec 2023 10:28:44 -0800
Subject: [PATCH 1/3] [clang] Avoid -Wshadow warning when init-capture named
same as class field
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/lib/Sema/SemaDecl.cpp | 8 +++++---
clang/test/SemaCXX/warn-shadow-in-lambdas.cpp | 18 ++++++++++++++++++
3 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 828dd10e3d6db..7ac81e16492d1 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -655,6 +655,9 @@ Bug Fixes in This Version
Fixes (`#64467 <https://github.com/llvm/llvm-project/issues/64467>`_)
- Clang's ``-Wchar-subscripts`` no longer warns on chars whose values are known non-negative constants.
Fixes (`#18763 <https://github.com/llvm/llvm-project/issues/18763>`_)
+- Clang's ``-Wshadow`` no longer warns when init-capture named same as class
+ field.
+ Fixes (`#71976 <https://github.com/llvm/llvm-project/issues/71976>`_)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index f12424d33b7da..65d095b2431dd 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8395,10 +8395,11 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
unsigned WarningDiag = diag::warn_decl_shadow;
SourceLocation CaptureLoc;
- if (isa<VarDecl>(D) && isa<VarDecl>(ShadowedDecl) && NewDC &&
- isa<CXXMethodDecl>(NewDC)) {
+ if (isa<VarDecl>(D) && NewDC && isa<CXXMethodDecl>(NewDC)) {
if (const auto *RD = dyn_cast<CXXRecordDecl>(NewDC->getParent())) {
if (RD->isLambda() && OldDC->Encloses(NewDC->getLexicalParent())) {
+ if (!isa<VarDecl>(ShadowedDecl))
+ return;
if (RD->getLambdaCaptureDefault() == LCD_None) {
// Try to avoid warnings for lambdas with an explicit capture list.
const auto *LSI = cast<LambdaScopeInfo>(getCurFunction());
@@ -8416,7 +8417,8 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
}
}
- if (cast<VarDecl>(ShadowedDecl)->hasLocalStorage()) {
+ if (const auto *VD = dyn_cast<VarDecl>(ShadowedDecl);
+ VD && VD->hasLocalStorage()) {
// A variable can't shadow a local variable in an enclosing scope, if
// they are separated by a non-capturing declaration context.
for (DeclContext *ParentDC = NewDC;
diff --git a/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp b/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp
index bda6a65c02168..58af7a2e65c55 100644
--- a/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp
+++ b/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp
@@ -179,3 +179,21 @@ void f() {
#endif
}
}
+
+namespace GH71976 {
+struct A {
+ int b = 5;
+ int foo() {
+ return [b = b]() { return b; }();
+ }
+};
+
+struct B {
+ int a;
+ void foo() {
+ auto b = [a = this->a] {
+
+ };
+ }
+};
+}
>From 3798e1f25a8d812c082ac5815c490eb9c7f67e62 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <mariya.podchishchaeva at intel.com>
Date: Wed, 6 Dec 2023 02:24:59 -0800
Subject: [PATCH 2/3] Apply comments
---
clang/docs/ReleaseNotes.rst | 4 ++--
clang/lib/Sema/SemaDecl.cpp | 1 +
clang/test/SemaCXX/warn-shadow-in-lambdas.cpp | 4 ++--
3 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2d1f3ed22cf2e..f5e0d4fa39737 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -661,8 +661,8 @@ Bug Fixes in This Version
- Fixed false positive error emitted when templated alias inside a class
used private members of the same class.
Fixes (`#41693 <https://github.com/llvm/llvm-project/issues/41693>`_)
-- Clang's ``-Wshadow`` no longer warns when init-capture named same as class
- field.
+- Clang's ``-Wshadow`` no longer warns when an init-capture is named the same as
+ a class field.
Fixes (`#71976 <https://github.com/llvm/llvm-project/issues/71976>`_)
Bug Fixes to Compiler Builtins
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 65d095b2431dd..f590d1b3ade81 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8398,6 +8398,7 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
if (isa<VarDecl>(D) && NewDC && isa<CXXMethodDecl>(NewDC)) {
if (const auto *RD = dyn_cast<CXXRecordDecl>(NewDC->getParent())) {
if (RD->isLambda() && OldDC->Encloses(NewDC->getLexicalParent())) {
+ // If it is not VarDecl then it can not shadow.
if (!isa<VarDecl>(ShadowedDecl))
return;
if (RD->getLambdaCaptureDefault() == LCD_None) {
diff --git a/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp b/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp
index 58af7a2e65c55..3008e9018f30b 100644
--- a/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp
+++ b/clang/test/SemaCXX/warn-shadow-in-lambdas.cpp
@@ -184,14 +184,14 @@ namespace GH71976 {
struct A {
int b = 5;
int foo() {
- return [b = b]() { return b; }();
+ return [b = b]() { return b; }(); // no diagnostic, init-capture does not shadow b
}
};
struct B {
int a;
void foo() {
- auto b = [a = this->a] {
+ auto b = [a = this->a] { // no diagnostic, init-capture does not shadow a
};
}
>From 3f01cd31f5a1609402be40e5bf9d8a01b325e798 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <mariya.podchishchaeva at intel.com>
Date: Wed, 6 Dec 2023 09:21:38 -0800
Subject: [PATCH 3/3] Remove comment
---
clang/lib/Sema/SemaDecl.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index f590d1b3ade81..65d095b2431dd 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8398,7 +8398,6 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
if (isa<VarDecl>(D) && NewDC && isa<CXXMethodDecl>(NewDC)) {
if (const auto *RD = dyn_cast<CXXRecordDecl>(NewDC->getParent())) {
if (RD->isLambda() && OldDC->Encloses(NewDC->getLexicalParent())) {
- // If it is not VarDecl then it can not shadow.
if (!isa<VarDecl>(ShadowedDecl))
return;
if (RD->getLambdaCaptureDefault() == LCD_None) {
More information about the cfe-commits
mailing list