[clang] [Clang] eliminate shadowing warnings for parameters using deducing this (PR #114813)

Oleksandr T. via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 5 05:33:30 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/2] [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/2] 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



More information about the cfe-commits mailing list