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; }
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; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits