xref: /llvm-project/clang/test/SemaCXX/datasizeof.cpp (revision 0211389064a1d493e826512a54ae547cb9859223)
1 // RUN: %clang_cc1 -fsyntax-only -triple x86_64-linux-gnu -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -triple x86_64-linux-gnu -verify %s -fexperimental-new-constant-interpreter
3 
4 #if !__has_extension(datasizeof)
5 #  error "Expected datasizeof extension"
6 #endif
7 
8 struct HasPadding {
9   int i;
10   char c;
11 };
12 
13 struct HasUsablePadding {
14   int i;
15   char c;
16 
HasUsablePaddingHasUsablePadding17   HasUsablePadding() {}
18 };
19 
20 struct Incomplete; // expected-note {{forward declaration of 'Incomplete'}}
21 
22 static_assert(__datasizeof(int) == 4);
23 static_assert(__datasizeof(HasPadding) == 8);
24 static_assert(__datasizeof(HasUsablePadding) == 5);
25 static_assert(__datasizeof(void)); // expected-error {{invalid application of '__datasizeof' to an incomplete type 'void'}}
26 static_assert(__datasizeof(Incomplete)); // expected-error {{invalid application of '__datasizeof' to an incomplete type 'Incomplete'}}
27 
__anon4d37a2c80102null28 static_assert([] {
29   int* p = nullptr;
30   HasPadding* p2 = nullptr;
31   HasUsablePadding* p3 = nullptr;
32   static_assert(__datasizeof(*p) == 4);
33   static_assert(__datasizeof *p == 4);
34   static_assert(__datasizeof(*p2) == 8);
35   static_assert(__datasizeof(*p3) == 5);
36 
37   return true;
38 }());
39 
40 template <typename Ty>
data_size_of()41 constexpr int data_size_of() {
42   return __datasizeof(Ty);
43 }
44 static_assert(data_size_of<int>() == __datasizeof(int));
45 static_assert(data_size_of<HasPadding>() == __datasizeof(HasPadding));
46 static_assert(data_size_of<HasUsablePadding>() == __datasizeof(HasUsablePadding));
47 
48 struct S {
49   int i = __datasizeof(S);
50   float f;
51   char c;
52 };
53 
54 static_assert(S{}.i == 9);
55 
56 namespace GH80284 {
57 struct Bar; // expected-note{{forward declaration}}
58 struct Foo {
59   Bar x; // expected-error{{field has incomplete type}}
60 };
61 constexpr int a = __datasizeof(Foo);
62 }
63