[PATCH] D151365: [Sema] cast to CXXRecoradDecl correctly when diag a default comparison method

Congcong Cai via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 24 12:44:26 PDT 2023


HerrCai0907 created this revision.
Herald added a project: All.
HerrCai0907 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Fixed: https://github.com/llvm/llvm-project/issues/62791
in c++20, default comparison is supported. `getLexicalDeclContext` maybe cannot
get the `CXXRecord` if default comparison defined out of `CXXRecord`.
This patch want to visit all the declaration of error function and find the
correct `CXXRecord`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151365

Files:
  clang/lib/Sema/SemaTemplateInstantiate.cpp
  clang/test/SemaCXX/cxx20-default-compare.cpp


Index: clang/test/SemaCXX/cxx20-default-compare.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/cxx20-default-compare.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 %s -std=c++23 -verify -Wfloat-equal
+
+struct Foo {
+  float val;
+  bool operator==(const Foo &) const;
+  friend bool operator==(const Foo &, const Foo &);
+  friend bool operator==(Foo, Foo );
+};
+
+// Declare the defaulted comparison function as a member function.
+bool Foo::operator==(const Foo &) const = default; // expected-warning {{comparing floating point with == or != is unsafe}} expected-note {{in defaulted equality comparison operator for 'Foo' first required here}}
+
+// Declare the defaulted comparison function as a non-member function.
+bool operator==(const Foo &, const Foo &) = default;  // expected-warning {{comparing floating point with == or != is unsafe}} expected-note {{in defaulted equality comparison operator for 'Foo' first required here}}
+
+// Declare the defaulted comparison function as a non-member function. Arguments are passed by value.
+bool operator==(Foo, Foo) = default;  // expected-warning {{comparing floating point with == or != is unsafe}} expected-note {{in defaulted equality comparison operator for 'Foo' first required here}}
Index: clang/lib/Sema/SemaTemplateInstantiate.cpp
===================================================================
--- clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -961,11 +961,17 @@
             << MD->isExplicitlyDefaulted() << DFK.asSpecialMember()
             << Context.getTagDeclType(MD->getParent());
       } else if (DFK.isComparison()) {
-        Diags.Report(Active->PointOfInstantiation,
-                     diag::note_comparison_synthesized_at)
-            << (int)DFK.asComparison()
-            << Context.getTagDeclType(
-                   cast<CXXRecordDecl>(FD->getLexicalDeclContext()));
+        CXXRecordDecl *RD = nullptr;
+        for (Decl *D : FD->redecls()) {
+          RD = dyn_cast<CXXRecordDecl>(D->getLexicalDeclContext());
+          if (RD)
+            break;
+        }
+        if (RD)
+          Diags.Report(Active->PointOfInstantiation,
+                      diag::note_comparison_synthesized_at)
+              << (int)DFK.asComparison()
+              << Context.getTagDeclType(RD);
       }
       break;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151365.525303.patch
Type: text/x-patch
Size: 2415 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230524/f48ca773/attachment.bin>


More information about the cfe-commits mailing list