[clang] d23449d - [Clang] Eliminate shadowing warnings for parameters of explicit object member functions (#114813)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 21 05:31:31 PST 2024


Author: Oleksandr T.
Date: 2024-11-21T14:31:27+01:00
New Revision: d23449d99c816b2d5b507f8d44f6e324e658e8bc

URL: https://github.com/llvm/llvm-project/commit/d23449d99c816b2d5b507f8d44f6e324e658e8bc
DIFF: https://github.com/llvm/llvm-project/commit/d23449d99c816b2d5b507f8d44f6e324e658e8bc.diff

LOG: [Clang] Eliminate shadowing warnings for parameters of explicit object member functions (#114813)

Fixes #95707.

Added: 
    clang/test/SemaCXX/cxx2b-warn-shadow.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaDecl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 10e27a4f4bc46f..084be14952cfa3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -565,6 +565,8 @@ Improvements to Clang's diagnostics
 - Clang now correctly recognises code after a call to a ``[[noreturn]]`` constructor
   as unreachable (#GH63009).
 
+- Clang now omits shadowing warnings for parameter names in explicit object member functions (#GH95707).
+
 Improvements to Clang's time-trace
 ----------------------------------
 

diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index be570f3a1829d0..223a532df41d9b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -8264,11 +8264,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..76866c4269474c
--- /dev/null
+++ b/clang/test/SemaCXX/cxx2b-warn-shadow.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -verify -fsyntax-only -std=c++2b -Wshadow-all %s
+
+namespace GH95707 {
+struct Foo {
+  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) { }; // expected-warning {{declaration shadows a field of 'GH95707::Foo'}}
+  }
+};
+} // namespace GH95707


        


More information about the cfe-commits mailing list