[clang] b8e8ff3 - [Clang] Implement diagnostics for why is_empty is false (#145044)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 25 07:34:55 PDT 2025
Author: Samarth Narang
Date: 2025-06-25T10:34:51-04:00
New Revision: b8e8ff3dd6698335a73ff808e39021ec5d1478ea
URL: https://github.com/llvm/llvm-project/commit/b8e8ff3dd6698335a73ff808e39021ec5d1478ea
DIFF: https://github.com/llvm/llvm-project/commit/b8e8ff3dd6698335a73ff808e39021ec5d1478ea.diff
LOG: [Clang] Implement diagnostics for why is_empty is false (#145044)
Expands on https://github.com/llvm/llvm-project/issues/141911
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaTypeTraits.cpp
clang/test/SemaCXX/type-traits-unsatisfied-diags-std.cpp
clang/test/SemaCXX/type-traits-unsatisfied-diags.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 047b1b929c0d9..ec1e1e7334d90 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -647,7 +647,7 @@ Improvements to Clang's diagnostics
#GH69470, #GH59391, #GH58172, #GH46215, #GH45915, #GH45891, #GH44490,
#GH36703, #GH32903, #GH23312, #GH69874.
-
+
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 9392cbb39c021..6eba0619883d3 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1767,7 +1767,8 @@ def note_unsatisfied_trait
: Note<"%0 is not %enum_select<TraitName>{"
"%TriviallyRelocatable{trivially relocatable}|"
"%Replaceable{replaceable}|"
- "%TriviallyCopyable{trivially copyable}"
+ "%TriviallyCopyable{trivially copyable}|"
+ "%Empty{empty}"
"}1">;
def note_unsatisfied_trait_reason
@@ -1787,6 +1788,10 @@ def note_unsatisfied_trait_reason
"%NonReplaceableField{has a non-replaceable member %1 of type %2}|"
"%NTCBase{has a non-trivially-copyable base %1}|"
"%NTCField{has a non-trivially-copyable member %1 of type %2}|"
+ "%NonEmptyMember{has a non-static data member %1 of type %2}|"
+ "%VirtualFunction{has a virtual function %1}|"
+ "%NonEmptyBase{has a base class %1 that is not empty}|"
+ "%NonZeroLengthField{field %1 is a non-zero-length bit-field}|"
"%DeletedDtr{has a %select{deleted|user-provided}1 destructor}|"
"%UserProvidedCtr{has a user provided %select{copy|move}1 "
"constructor}|"
diff --git a/clang/lib/Sema/SemaTypeTraits.cpp b/clang/lib/Sema/SemaTypeTraits.cpp
index bf59bbb87fd8a..c8a764d19c3d5 100644
--- a/clang/lib/Sema/SemaTypeTraits.cpp
+++ b/clang/lib/Sema/SemaTypeTraits.cpp
@@ -1958,6 +1958,7 @@ static std::optional<TypeTrait> StdNameToTypeTrait(StringRef Name) {
.Case("is_replaceable", TypeTrait::UTT_IsReplaceable)
.Case("is_trivially_copyable", TypeTrait::UTT_IsTriviallyCopyable)
.Case("is_assignable", TypeTrait::BTT_IsAssignable)
+ .Case("is_empty", TypeTrait::UTT_IsEmpty)
.Default(std::nullopt);
}
@@ -2313,6 +2314,74 @@ static void DiagnoseNonAssignableReason(Sema &SemaRef, SourceLocation Loc,
SemaRef.Diag(D->getLocation(), diag::note_defined_here) << D;
}
+static void DiagnoseIsEmptyReason(Sema &S, SourceLocation Loc,
+ const CXXRecordDecl *D) {
+ // Non-static data members (ignore zero-width bitāfields).
+ for (const auto *Field : D->fields()) {
+ if (Field->isZeroLengthBitField())
+ continue;
+ if (Field->isBitField()) {
+ S.Diag(Loc, diag::note_unsatisfied_trait_reason)
+ << diag::TraitNotSatisfiedReason::NonZeroLengthField << Field
+ << Field->getSourceRange();
+ continue;
+ }
+ S.Diag(Loc, diag::note_unsatisfied_trait_reason)
+ << diag::TraitNotSatisfiedReason::NonEmptyMember << Field
+ << Field->getType() << Field->getSourceRange();
+ }
+
+ // Virtual functions.
+ for (const auto *M : D->methods()) {
+ if (M->isVirtual()) {
+ S.Diag(Loc, diag::note_unsatisfied_trait_reason)
+ << diag::TraitNotSatisfiedReason::VirtualFunction << M
+ << M->getSourceRange();
+ break;
+ }
+ }
+
+ // Virtual bases and non-empty bases.
+ for (const auto &B : D->bases()) {
+ const auto *BR = B.getType()->getAsCXXRecordDecl();
+ if (!BR || BR->isInvalidDecl())
+ continue;
+ if (B.isVirtual()) {
+ S.Diag(Loc, diag::note_unsatisfied_trait_reason)
+ << diag::TraitNotSatisfiedReason::VBase << B.getType()
+ << B.getSourceRange();
+ }
+ if (!BR->isEmpty()) {
+ S.Diag(Loc, diag::note_unsatisfied_trait_reason)
+ << diag::TraitNotSatisfiedReason::NonEmptyBase << B.getType()
+ << B.getSourceRange();
+ }
+ }
+}
+
+static void DiagnoseIsEmptyReason(Sema &S, SourceLocation Loc, QualType T) {
+ // Emit primary "not empty" diagnostic.
+ S.Diag(Loc, diag::note_unsatisfied_trait) << T << diag::TraitName::Empty;
+
+ // While diagnosing is_empty<T>, we want to look at the actual type, not a
+ // reference or an array of it. So we need to massage the QualType param to
+ // strip refs and arrays.
+ if (T->isReferenceType())
+ S.Diag(Loc, diag::note_unsatisfied_trait_reason)
+ << diag::TraitNotSatisfiedReason::Ref;
+ T = T.getNonReferenceType();
+
+ if (auto *AT = S.Context.getAsArrayType(T))
+ T = AT->getElementType();
+
+ if (auto *D = T->getAsCXXRecordDecl()) {
+ if (D->hasDefinition()) {
+ DiagnoseIsEmptyReason(S, Loc, D);
+ S.Diag(D->getLocation(), diag::note_defined_here) << D;
+ }
+ }
+}
+
void Sema::DiagnoseTypeTraitDetails(const Expr *E) {
E = E->IgnoreParenImpCasts();
if (E->containsErrors())
@@ -2336,6 +2405,9 @@ void Sema::DiagnoseTypeTraitDetails(const Expr *E) {
case BTT_IsAssignable:
DiagnoseNonAssignableReason(*this, E->getBeginLoc(), Args[0], Args[1]);
break;
+ case UTT_IsEmpty:
+ DiagnoseIsEmptyReason(*this, E->getBeginLoc(), Args[0]);
+ break;
default:
break;
}
diff --git a/clang/test/SemaCXX/type-traits-unsatisfied-diags-std.cpp b/clang/test/SemaCXX/type-traits-unsatisfied-diags-std.cpp
index 33b92326aec21..89fbad0c5b9b8 100644
--- a/clang/test/SemaCXX/type-traits-unsatisfied-diags-std.cpp
+++ b/clang/test/SemaCXX/type-traits-unsatisfied-diags-std.cpp
@@ -28,6 +28,13 @@ struct is_assignable {
template <typename T, typename U>
constexpr bool is_assignable_v = __is_assignable(T, U);
+
+template <typename T>
+struct is_empty {
+ static constexpr bool value = __is_empty(T);
+};
+template <typename T>
+constexpr bool is_empty_v = __is_empty(T);
#endif
#ifdef STD2
@@ -63,6 +70,15 @@ using is_assignable = __details_is_assignable<T, U>;
template <typename T, typename U>
constexpr bool is_assignable_v = __is_assignable(T, U);
+
+template <typename T>
+struct __details_is_empty {
+ static constexpr bool value = __is_empty(T);
+};
+template <typename T>
+using is_empty = __details_is_empty<T>;
+template <typename T>
+constexpr bool is_empty_v = __is_empty(T);
#endif
@@ -101,6 +117,13 @@ using is_assignable = __details_is_assignable<T, U>;
template <typename T, typename U>
constexpr bool is_assignable_v = is_assignable<T, U>::value;
+
+template <typename T>
+struct __details_is_empty : bool_constant<__is_empty(T)> {};
+template <typename T>
+using is_empty = __details_is_empty<T>;
+template <typename T>
+constexpr bool is_empty_v = is_empty<T>::value;
#endif
}
@@ -127,6 +150,18 @@ static_assert(std::is_trivially_copyable_v<int&>);
// expected-note at -1 {{'int &' is not trivially copyable}} \
// expected-note at -1 {{because it is a reference type}}
+static_assert(!std::is_empty<int>::value);
+
+static_assert(std::is_empty<int&>::value);
+// expected-error-re at -1 {{static assertion failed due to requirement 'std::{{.*}}is_empty<int &>::value'}} \
+// expected-note at -1 {{'int &' is not empty}} \
+// expected-note at -1 {{because it is a reference type}}
+static_assert(std::is_empty_v<int&>);
+// expected-error at -1 {{static assertion failed due to requirement 'std::is_empty_v<int &>'}} \
+// expected-note at -1 {{'int &' is not empty}} \
+// expected-note at -1 {{because it is a reference type}}
+
+
static_assert(std::is_assignable<int&, int>::value);
static_assert(std::is_assignable<int&, void>::value);
@@ -162,6 +197,15 @@ namespace test_namespace {
static_assert(is_assignable_v<int&, void>);
// expected-error at -1 {{static assertion failed due to requirement 'is_assignable_v<int &, void>'}} \
// expected-error at -1 {{assigning to 'int' from incompatible type 'void'}}
+
+ static_assert(is_empty<int&>::value);
+ // expected-error-re at -1 {{static assertion failed due to requirement '{{.*}}is_empty<int &>::value'}} \
+ // expected-note at -1 {{'int &' is not empty}} \
+ // expected-note at -1 {{because it is a reference type}}
+ static_assert(is_empty_v<int&>);
+ // expected-error at -1 {{static assertion failed due to requirement 'is_empty_v<int &>'}} \
+ // expected-note at -1 {{'int &' is not empty}} \
+ // expected-note at -1 {{because it is a reference type}}
}
diff --git a/clang/test/SemaCXX/type-traits-unsatisfied-diags.cpp b/clang/test/SemaCXX/type-traits-unsatisfied-diags.cpp
index 975174353a2a1..f1624f0a6e553 100644
--- a/clang/test/SemaCXX/type-traits-unsatisfied-diags.cpp
+++ b/clang/test/SemaCXX/type-traits-unsatisfied-diags.cpp
@@ -559,3 +559,78 @@ static_assert(__is_assignable(C1, C1));
// expected-note@#ama-C1 {{implicitly declared private here}} \
// expected-note@#a-C1 {{'C1' defined here}}
}
+
+namespace is_empty_tests {
+ // Non-static data member.
+ struct A { int x; }; // #e-A
+ static_assert(__is_empty(A));
+ // expected-error at -1 {{static assertion failed due to requirement '__is_empty(is_empty_tests::A)'}} \
+ // expected-note at -1 {{'A' is not empty}} \
+ // expected-note at -1 {{because it has a non-static data member 'x' of type 'int'}} \
+ // expected-note@#e-A {{'A' defined here}}
+
+ // Reference member.
+ struct R {int &r; }; // #e-R
+ static_assert(__is_empty(R));
+ // expected-error at -1 {{static assertion failed due to requirement '__is_empty(is_empty_tests::R)'}} \
+ // expected-note at -1 {{'R' is not empty}} \
+ // expected-note at -1 {{because it has a non-static data member 'r' of type 'int &'}} \
+ // expected-note@#e-R {{'R' defined here}}
+
+ // Virtual function.
+ struct VirtualFunc {virtual void f(); }; // #e-VirtualFunc
+ static_assert(__is_empty(VirtualFunc));
+ // expected-error at -1 {{static assertion failed due to requirement '__is_empty(is_empty_tests::VirtualFunc)'}} \
+ // expected-note at -1 {{'VirtualFunc' is not empty}} \
+ // expected-note at -1 {{because it has a virtual function 'f'}} \
+ // expected-note@#e-VirtualFunc {{'VirtualFunc' defined here}}
+
+ // Virtual base class.
+ struct EB {};
+ struct VB: virtual EB {}; // #e-VB
+ static_assert(__is_empty(VB));
+ // expected-error at -1 {{static assertion failed due to requirement '__is_empty(is_empty_tests::VB)'}} \
+ // expected-note at -1 {{'VB' is not empty}} \
+ // expected-note at -1 {{because it has a virtual base 'EB'}} \
+ // expected-note@#e-VB {{'VB' defined here}}
+
+ // Non-empty base class.
+ struct Base { int b; }; // #e-Base
+ struct Derived : Base {}; // #e-Derived
+ static_assert(__is_empty(Derived));
+ // expected-error at -1 {{static assertion failed due to requirement '__is_empty(is_empty_tests::Derived)'}} \
+ // expected-note at -1 {{'Derived' is not empty}} \
+ // expected-note at -1 {{because it has a base class 'Base' that is not empty}} \
+ // expected-note@#e-Derived {{'Derived' defined here}}
+
+ // Combination of the above.
+ struct Multi : Base, virtual EB { // #e-Multi
+ int z;
+ virtual void g();
+ };
+ static_assert(__is_empty(Multi));
+ // expected-error at -1 {{static assertion failed due to requirement '__is_empty(is_empty_tests::Multi)'}} \
+ // expected-note at -1 {{'Multi' is not empty}} \
+ // expected-note at -1 {{because it has a non-static data member 'z' of type 'int'}} \
+ // expected-note at -1 {{because it has a virtual function 'g'}} \
+ // expected-note at -1 {{because it has a base class 'Base' that is not empty}} \
+ // expected-note at -1 {{because it has a virtual base 'EB'}} \
+ // expected-note@#e-Multi {{'Multi' defined here}}
+
+ // Zero-width bit-field.
+ struct BitField { int : 0; }; // #e-BitField
+ static_assert(__is_empty(BitField)); // no diagnostics
+
+ // Dependent bit-field width.
+ template <int N>
+ struct DependentBitField { int : N; }; // #e-DependentBitField
+
+ static_assert(__is_empty(DependentBitField<0>)); // no diagnostics
+
+ static_assert(__is_empty(DependentBitField<2>));
+ // expected-error at -1 {{static assertion failed due to requirement '__is_empty(is_empty_tests::DependentBitField<2>)'}} \
+ // expected-note at -1 {{'DependentBitField<2>' is not empty}} \
+ // expected-note at -1 {{because it field '' is a non-zero-length bit-field}} \
+ // expected-note@#e-DependentBitField {{'DependentBitField<2>' defined here}}
+
+}
More information about the cfe-commits
mailing list