[clang] [Clang] eliminate shadowing warnings for parameters using deducing this (PR #114813)
Oleksandr T. via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 21 03:19:29 PST 2024
https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/114813
>From 91ff2b4226110ea35c78f0f1b6ff89b4bec2c788 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Mon, 4 Nov 2024 17:38:46 +0200
Subject: [PATCH 1/5] [Clang] eliminate shadowing warnings for parameters using
deducing this
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/Sema/SemaDecl.cpp | 7 +++++--
clang/test/SemaCXX/cxx2b-warn-shadow.cpp | 10 ++++++++++
3 files changed, 17 insertions(+), 2 deletions(-)
create mode 100644 clang/test/SemaCXX/cxx2b-warn-shadow.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1372e49dfac03c..af8288ee8a8296 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -464,6 +464,8 @@ Improvements to Clang's diagnostics
- Clang now diagnoses ``[[deprecated]]`` attribute usage on local variables (#GH90073).
+- Clang now omits shadowing warnings for parameter names using deducing this (#GH95707).
+
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 1aa3e8edfe1b13..38b07b15eb3eb2 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8236,11 +8236,14 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
DeclContext *NewDC = D->getDeclContext();
if (FieldDecl *FD = dyn_cast<FieldDecl>(ShadowedDecl)) {
- // Fields are not shadowed by variables in C++ static methods.
- if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC))
+ if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC)) {
+ // Fields are not shadowed by variables in C++ static methods.
if (MD->isStatic())
return;
+ if (!MD->getParent()->isLambda() && MD->isExplicitObjectMemberFunction())
+ return;
+ }
// Fields shadowed by constructor parameters are a special case. Usually
// the constructor initializes the field with the parameter.
if (isa<CXXConstructorDecl>(NewDC))
diff --git a/clang/test/SemaCXX/cxx2b-warn-shadow.cpp b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
new file mode 100644
index 00000000000000..0cf47f3848979c
--- /dev/null
+++ b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -verify -fsyntax-only -std=c++2b -Wshadow-all %s
+
+namespace GH95707 {
+struct Foo {
+ int a; // expected-note {{previous declaration is here}}
+
+ void f1(this auto &self, int a) { self.a = a; }
+ void f2(int a) { } // expected-warning {{declaration shadows a field of 'GH95707::Foo'}}
+};
+} // namespace GH95707
>From 849515a74f1c82845b0baa100729aa80bf236f66 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Tue, 5 Nov 2024 15:33:14 +0200
Subject: [PATCH 2/5] update tests/release notes
---
clang/docs/ReleaseNotes.rst | 2 +-
clang/test/SemaCXX/cxx2b-warn-shadow.cpp | 3 +++
2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index af8288ee8a8296..5ecde32bb70429 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -464,7 +464,7 @@ Improvements to Clang's diagnostics
- Clang now diagnoses ``[[deprecated]]`` attribute usage on local variables (#GH90073).
-- Clang now omits shadowing warnings for parameter names using deducing this (#GH95707).
+- Clang now omits shadowing warnings for parameter names in explicit object member functions (#GH95707).
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/test/SemaCXX/cxx2b-warn-shadow.cpp b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
index 0cf47f3848979c..19db49a4566fa7 100644
--- a/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
+++ b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
@@ -6,5 +6,8 @@ struct Foo {
void f1(this auto &self, int a) { self.a = a; }
void f2(int a) { } // expected-warning {{declaration shadows a field of 'GH95707::Foo'}}
+ void f3() {
+ [&](this auto &self, int x) { }; // expected-warning {{expression result unused}}
+ }
};
} // namespace GH95707
>From 2a2cb900b090d4bb2f1e66c3e027cd947d8053c4 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Wed, 6 Nov 2024 12:18:00 +0200
Subject: [PATCH 3/5] remove unnesessary warning
---
clang/test/SemaCXX/cxx2b-warn-shadow.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/SemaCXX/cxx2b-warn-shadow.cpp b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
index 19db49a4566fa7..e6615e969910e2 100644
--- a/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
+++ b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
@@ -7,7 +7,7 @@ struct Foo {
void f1(this auto &self, int a) { self.a = a; }
void f2(int a) { } // expected-warning {{declaration shadows a field of 'GH95707::Foo'}}
void f3() {
- [&](this auto &self, int x) { }; // expected-warning {{expression result unused}}
+ (void)[&](this auto &self, int x) { };
}
};
} // namespace GH95707
>From fa897f136484fedb96a2eb38e344611a363d415d Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Wed, 20 Nov 2024 16:51:17 +0200
Subject: [PATCH 4/5] avoid shadowing warnings for lambdas with explicit object
parameters
---
clang/lib/Sema/SemaDecl.cpp | 2 +-
clang/test/SemaCXX/cxx2b-warn-shadow.cpp | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 223a532df41d9b..99558e5da0485a 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8269,7 +8269,7 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
if (MD->isStatic())
return;
- if (!MD->getParent()->isLambda() && MD->isExplicitObjectMemberFunction())
+ if (!MD->getType().isNull() && MD->isExplicitObjectMemberFunction())
return;
}
// Fields shadowed by constructor parameters are a special case. Usually
diff --git a/clang/test/SemaCXX/cxx2b-warn-shadow.cpp b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
index e6615e969910e2..5b984e588008b8 100644
--- a/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
+++ b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
@@ -7,7 +7,7 @@ struct Foo {
void f1(this auto &self, int a) { self.a = a; }
void f2(int a) { } // expected-warning {{declaration shadows a field of 'GH95707::Foo'}}
void f3() {
- (void)[&](this auto &self, int x) { };
+ (void)[&](this auto &self, int a) { };
}
};
} // namespace GH95707
>From 52a98dda99bebdd7cdfeedfc80ae9e88feb46df2 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Wed, 20 Nov 2024 18:42:04 +0200
Subject: [PATCH 5/5] revert changes to handle shadow in lambda with explicit
object parameter
---
clang/lib/Sema/SemaDecl.cpp | 2 +-
clang/test/SemaCXX/cxx2b-warn-shadow.cpp | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 99558e5da0485a..223a532df41d9b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8269,7 +8269,7 @@ void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
if (MD->isStatic())
return;
- if (!MD->getType().isNull() && MD->isExplicitObjectMemberFunction())
+ if (!MD->getParent()->isLambda() && MD->isExplicitObjectMemberFunction())
return;
}
// Fields shadowed by constructor parameters are a special case. Usually
diff --git a/clang/test/SemaCXX/cxx2b-warn-shadow.cpp b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
index 5b984e588008b8..76866c4269474c 100644
--- a/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
+++ b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
@@ -2,12 +2,12 @@
namespace GH95707 {
struct Foo {
- int a; // expected-note {{previous declaration is here}}
+ int a; // expected-note 2 {{previous declaration is here}}
void f1(this auto &self, int a) { self.a = a; }
void f2(int a) { } // expected-warning {{declaration shadows a field of 'GH95707::Foo'}}
void f3() {
- (void)[&](this auto &self, int a) { };
+ (void)[&](this auto &self, int a) { }; // expected-warning {{declaration shadows a field of 'GH95707::Foo'}}
}
};
} // namespace GH95707
More information about the cfe-commits
mailing list