63 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -triple x86_64-linux-gnu -verify %s2// RUN: %clang_cc1 -fsyntax-only -triple x86_64-linux-gnu -verify %s -fexperimental-new-constant-interpreter3 4#if !__has_extension(datasizeof)5# error "Expected datasizeof extension"6#endif7 8struct HasPadding {9 int i;10 char c;11};12 13struct HasUsablePadding {14 int i;15 char c;16 17 HasUsablePadding() {}18};19 20struct Incomplete; // expected-note {{forward declaration of 'Incomplete'}}21 22static_assert(__datasizeof(int) == 4);23static_assert(__datasizeof(HasPadding) == 8);24static_assert(__datasizeof(HasUsablePadding) == 5);25static_assert(__datasizeof(void)); // expected-error {{invalid application of '__datasizeof' to an incomplete type 'void'}}26static_assert(__datasizeof(Incomplete)); // expected-error {{invalid application of '__datasizeof' to an incomplete type 'Incomplete'}}27 28static_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 40template <typename Ty>41constexpr int data_size_of() {42 return __datasizeof(Ty);43}44static_assert(data_size_of<int>() == __datasizeof(int));45static_assert(data_size_of<HasPadding>() == __datasizeof(HasPadding));46static_assert(data_size_of<HasUsablePadding>() == __datasizeof(HasUsablePadding));47 48struct S {49 int i = __datasizeof(S);50 float f;51 char c;52};53 54static_assert(S{}.i == 9);55 56namespace GH80284 {57struct Bar; // expected-note{{forward declaration}}58struct Foo {59 Bar x; // expected-error{{field has incomplete type}}60};61constexpr int a = __datasizeof(Foo);62}63