xref: /llvm-project/clang/test/SemaCXX/datasizeof.cpp (revision 0211389064a1d493e826512a54ae547cb9859223)
14cc791bcSphilnik777 // RUN: %clang_cc1 -fsyntax-only -triple x86_64-linux-gnu -verify %s
2*02113890STimm Bäder // RUN: %clang_cc1 -fsyntax-only -triple x86_64-linux-gnu -verify %s -fexperimental-new-constant-interpreter
34cc791bcSphilnik777 
44cc791bcSphilnik777 #if !__has_extension(datasizeof)
54cc791bcSphilnik777 #  error "Expected datasizeof extension"
64cc791bcSphilnik777 #endif
74cc791bcSphilnik777 
84cc791bcSphilnik777 struct HasPadding {
94cc791bcSphilnik777   int i;
104cc791bcSphilnik777   char c;
114cc791bcSphilnik777 };
124cc791bcSphilnik777 
134cc791bcSphilnik777 struct HasUsablePadding {
144cc791bcSphilnik777   int i;
154cc791bcSphilnik777   char c;
164cc791bcSphilnik777 
HasUsablePaddingHasUsablePadding174cc791bcSphilnik777   HasUsablePadding() {}
184cc791bcSphilnik777 };
194cc791bcSphilnik777 
204cc791bcSphilnik777 struct Incomplete; // expected-note {{forward declaration of 'Incomplete'}}
214cc791bcSphilnik777 
224cc791bcSphilnik777 static_assert(__datasizeof(int) == 4);
234cc791bcSphilnik777 static_assert(__datasizeof(HasPadding) == 8);
244cc791bcSphilnik777 static_assert(__datasizeof(HasUsablePadding) == 5);
254cc791bcSphilnik777 static_assert(__datasizeof(void)); // expected-error {{invalid application of '__datasizeof' to an incomplete type 'void'}}
264cc791bcSphilnik777 static_assert(__datasizeof(Incomplete)); // expected-error {{invalid application of '__datasizeof' to an incomplete type 'Incomplete'}}
274cc791bcSphilnik777 
__anon4d37a2c80102null284cc791bcSphilnik777 static_assert([] {
294cc791bcSphilnik777   int* p = nullptr;
304cc791bcSphilnik777   HasPadding* p2 = nullptr;
314cc791bcSphilnik777   HasUsablePadding* p3 = nullptr;
324cc791bcSphilnik777   static_assert(__datasizeof(*p) == 4);
334cc791bcSphilnik777   static_assert(__datasizeof *p == 4);
344cc791bcSphilnik777   static_assert(__datasizeof(*p2) == 8);
354cc791bcSphilnik777   static_assert(__datasizeof(*p3) == 5);
364cc791bcSphilnik777 
374cc791bcSphilnik777   return true;
384cc791bcSphilnik777 }());
394cc791bcSphilnik777 
404cc791bcSphilnik777 template <typename Ty>
data_size_of()414cc791bcSphilnik777 constexpr int data_size_of() {
424cc791bcSphilnik777   return __datasizeof(Ty);
434cc791bcSphilnik777 }
444cc791bcSphilnik777 static_assert(data_size_of<int>() == __datasizeof(int));
454cc791bcSphilnik777 static_assert(data_size_of<HasPadding>() == __datasizeof(HasPadding));
464cc791bcSphilnik777 static_assert(data_size_of<HasUsablePadding>() == __datasizeof(HasUsablePadding));
474cc791bcSphilnik777 
484cc791bcSphilnik777 struct S {
494cc791bcSphilnik777   int i = __datasizeof(S);
504cc791bcSphilnik777   float f;
514cc791bcSphilnik777   char c;
524cc791bcSphilnik777 };
534cc791bcSphilnik777 
544cc791bcSphilnik777 static_assert(S{}.i == 9);
559acd61ecSIlya Biryukov 
569acd61ecSIlya Biryukov namespace GH80284 {
579acd61ecSIlya Biryukov struct Bar; // expected-note{{forward declaration}}
589acd61ecSIlya Biryukov struct Foo {
599acd61ecSIlya Biryukov   Bar x; // expected-error{{field has incomplete type}}
609acd61ecSIlya Biryukov };
619acd61ecSIlya Biryukov constexpr int a = __datasizeof(Foo);
629acd61ecSIlya Biryukov }
63