Skip to content

[Clang] eliminate shadowing warnings for parameters using deducing this - #114813

Merged
Sirraide merged 13 commits into
llvm:mainfrom
a-tarasyuk:fix/95707
Nov 21, 2024
Merged

[Clang] eliminate shadowing warnings for parameters using deducing this#114813
Sirraide merged 13 commits into
llvm:mainfrom
a-tarasyuk:fix/95707

Conversation

@a-tarasyuk

Copy link
Copy Markdown
Member

Fixes #95707

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 4, 2024
@llvmbot

llvmbot commented Nov 4, 2024

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

Changes

Fixes #95707


Full diff: https://github.com/llvm/llvm-project/pull/114813.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/Sema/SemaDecl.cpp (+5-2)
  • (added) clang/test/SemaCXX/cxx2b-warn-shadow.cpp (+10)
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

Comment thread clang/docs/ReleaseNotes.rst Outdated
Comment thread clang/lib/Sema/SemaDecl.cpp
@a-tarasyuk
a-tarasyuk requested a review from Sirraide November 5, 2024 21:10
Comment thread clang/test/SemaCXX/cxx2b-warn-shadow.cpp Outdated
@a-tarasyuk
a-tarasyuk requested a review from Sirraide November 20, 2024 12:33
Comment thread clang/test/SemaCXX/cxx2b-warn-shadow.cpp Outdated
@a-tarasyuk
a-tarasyuk requested a review from Sirraide November 20, 2024 15:39
Comment thread clang/test/SemaCXX/cxx2b-warn-shadow.cpp Outdated
Comment thread clang/lib/Sema/SemaDecl.cpp Outdated
@a-tarasyuk
a-tarasyuk requested a review from Sirraide November 20, 2024 16:47
@Sirraide
Sirraide merged commit d23449d into llvm:main Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive warning with -Wshadow on parameter names using Deducing This

3 participants