[clang] fixed clang frontend crash with friend class declaration and overload == (PR #133878)
Ankur Ahir via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 2 08:21:32 PDT 2025
https://github.com/Ankur-0429 updated https://github.com/llvm/llvm-project/pull/133878
>From 289f8630dccf916b8cf7cc43758bae60df036c5d Mon Sep 17 00:00:00 2001
From: Ankur Ahir <ankurahir at google.com>
Date: Wed, 2 Apr 2025 00:53:33 -0700
Subject: [PATCH 1/4] clang frontend crash with friend class declaration and
overloaded operator ==
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaDeclCXX.cpp | 3 +--
.../class/class.compare/class.compare.default/p1.cpp | 12 ++++++++++++
3 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index daad01919ecd4..431ae0863d9e4 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -342,6 +342,7 @@ Bug Fixes to Attribute Support
Bug Fixes to C++ Support
^^^^^^^^^^^^^^^^^^^^^^^^
+- Clang Now Supports Implicitly-Defined Comparison Operators for Friend-Declarations
- Clang now diagnoses copy constructors taking the class by value in template instantiations. (#GH130866)
- Clang is now better at keeping track of friend function template instance contexts. (#GH55509)
- Clang now prints the correct instantiation context for diagnostics suppressed
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 676d53a1f4b45..d1516860dfe71 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9037,8 +9037,7 @@ bool Sema::CheckExplicitlyDefaultedComparison(Scope *S, FunctionDecl *FD,
return true;
if (llvm::none_of(RD->friends(), [&](const FriendDecl *F) {
- return FD->getCanonicalDecl() ==
- F->getFriendDecl()->getCanonicalDecl();
+ return declaresSameEntity(F->getFriendDecl(), FD);
})) {
Diag(FD->getLocation(), diag::err_defaulted_comparison_not_friend)
<< int(DCK) << int(0) << RD;
diff --git a/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp b/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
index a195e0548152d..5db4f05da552c 100644
--- a/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
+++ b/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
@@ -285,3 +285,15 @@ struct j {
};
bool j::operator==(const j &) const = default;
}
+
+namespace evil2 {
+ struct k {
+ };
+
+ struct l {
+ friend bool operator==(const l& a, const l& b);
+ friend class k;
+ };
+
+ bool operator==(const l& a, const l& b) = default;
+}
\ No newline at end of file
>From 78fecd58986e25147b47220ee9ef0a392c45651e Mon Sep 17 00:00:00 2001
From: Ankur Ahir <ankurahir at google.com>
Date: Wed, 2 Apr 2025 00:53:33 -0700
Subject: [PATCH 2/4] clang frontend crash with friend class declaration and
overloaded operator ==
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaDeclCXX.cpp | 3 +--
.../class/class.compare/class.compare.default/p1.cpp | 12 ++++++++++++
3 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index daad01919ecd4..88ed3018c58ea 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -342,6 +342,7 @@ Bug Fixes to Attribute Support
Bug Fixes to C++ Support
^^^^^^^^^^^^^^^^^^^^^^^^
+- Clang Now supports Implicitly-Defined Comparison Operators for Friend-Declarations
- Clang now diagnoses copy constructors taking the class by value in template instantiations. (#GH130866)
- Clang is now better at keeping track of friend function template instance contexts. (#GH55509)
- Clang now prints the correct instantiation context for diagnostics suppressed
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 676d53a1f4b45..d1516860dfe71 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9037,8 +9037,7 @@ bool Sema::CheckExplicitlyDefaultedComparison(Scope *S, FunctionDecl *FD,
return true;
if (llvm::none_of(RD->friends(), [&](const FriendDecl *F) {
- return FD->getCanonicalDecl() ==
- F->getFriendDecl()->getCanonicalDecl();
+ return declaresSameEntity(F->getFriendDecl(), FD);
})) {
Diag(FD->getLocation(), diag::err_defaulted_comparison_not_friend)
<< int(DCK) << int(0) << RD;
diff --git a/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp b/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
index a195e0548152d..5db4f05da552c 100644
--- a/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
+++ b/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
@@ -285,3 +285,15 @@ struct j {
};
bool j::operator==(const j &) const = default;
}
+
+namespace evil2 {
+ struct k {
+ };
+
+ struct l {
+ friend bool operator==(const l& a, const l& b);
+ friend class k;
+ };
+
+ bool operator==(const l& a, const l& b) = default;
+}
\ No newline at end of file
>From 37e05c8161205288be7ad54fe7fd557556a0f851 Mon Sep 17 00:00:00 2001
From: Ankur Ahir <69181589+Ankur-0429 at users.noreply.github.com>
Date: Wed, 2 Apr 2025 08:09:45 -0700
Subject: [PATCH 3/4] Update ReleaseNotes.rst
Co-authored-by: cor3ntin <corentinjabot at gmail.com>
---
clang/docs/ReleaseNotes.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9da326e2dd0cb..eae82b05731b2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -355,7 +355,7 @@ Bug Fixes to Attribute Support
Bug Fixes to C++ Support
^^^^^^^^^^^^^^^^^^^^^^^^
-- Clang Now supports Implicitly-Defined Comparison Operators for Friend-Declarations
+- Clang now supports implicitly defined comparison operators for friend declarations. (#GH132249)
- Clang now diagnoses copy constructors taking the class by value in template instantiations. (#GH130866)
- Clang is now better at keeping track of friend function template instance contexts. (#GH55509)
- Clang now prints the correct instantiation context for diagnostics suppressed
>From d3c1e77033e7646ab5d5c988d83b3474bc609aef Mon Sep 17 00:00:00 2001
From: Ankur Ahir <ankurahir at google.com>
Date: Wed, 2 Apr 2025 08:20:34 -0700
Subject: [PATCH 4/4] updated p1.cpp
---
clang/test/CXX/class/class.compare/class.compare.default/p1.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp b/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
index 5db4f05da552c..f3e241c7bbd51 100644
--- a/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
+++ b/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
@@ -296,4 +296,4 @@ namespace evil2 {
};
bool operator==(const l& a, const l& b) = default;
-}
\ No newline at end of file
+}
More information about the cfe-commits
mailing list