[clang] [Clang] Implement diagnostics for why is_empty is false (PR #145044)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 23 08:26:07 PDT 2025
================
@@ -488,3 +488,61 @@ static_assert(__is_trivially_copyable(S12));
// expected-note at -1 {{'S12' is not trivially copyable}} \
// expected-note@#tc-S12 {{'S12' 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 class '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 class 'EB'}} \
+ // expected-note@#e-Multi {{'Multi' defined here}}
----------------
AaronBallman wrote:
Also missing test coverage for (zero-width) bit-fields.
https://github.com/llvm/llvm-project/pull/145044
More information about the cfe-commits
mailing list