Debian Codegraph @

clang/test/AST/ByteCode/builtin-object-size.cpp

raw
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=both,expected %s
2 // RUN: %clang_cc1 -verify=both,ref %s
3
4 // ref-no-diagnostics
5
6 typedef __SIZE_TYPE__ size_t;
7
8 int a;
9 static_assert(__builtin_object_size(&a, 0) == sizeof(int), "");
10 float f;
11 static_assert(__builtin_object_size(&f, 0) == sizeof(float), "");
12 int arr[2];
13 static_assert(__builtin_object_size(&arr, 0) == (sizeof(int)*2), "");
14
15 float arrf[2];
16 static_assert(__builtin_object_size(&arrf, 0) == (sizeof(float)*2), "");
17 static_assert(__builtin_object_size(&arrf[1], 0) == sizeof(float), "");
18 static_assert(__builtin_object_size(&arrf[2], 0) == 0, "");
19
20 constexpr struct { int a; int b; } F{};
21 static_assert(__builtin_object_size(&F.a, 3) == sizeof(int));
22
23 struct S {
24 int a;
25 char c;
26 };
27
28 S s;
29 static_assert(__builtin_object_size(&s, 0) == sizeof(s), "");
30
31 S ss[2];
32 static_assert(__builtin_object_size(&ss[1], 0) == sizeof(s), "");
33 static_assert(__builtin_object_size(&ss[1].c, 0) == sizeof(int), "");
34
35 struct A { char buf[16]; };
36 struct B : A {};
37 struct C { int i; B bs[1]; } c;
38 static_assert(__builtin_object_size(&c.bs[0], 3) == 16);
39 static_assert(__builtin_object_size(&c.bs[1], 3) == 0);
40
41 /// These are from test/SemaCXX/builtin-object-size-cxx14.
42 /// They all don't work since they are rejected when evaluating the first
43 /// parameter of the __builtin_object_size call.
44 ///
45 /// GCC rejects them as well.
46 namespace InvalidBase {
47 // Ensure this doesn't crash.
48 struct S { const char *name; };
49 S invalid_base(); // expected-note {{declared here}}
50 constexpr size_t bos_name = __builtin_object_size(invalid_base().name, 1); // expected-error {{must be initialized by a constant expression}} \
51 // expected-note {{non-constexpr function 'invalid_base'}}
52
53 struct T { ~T(); };
54 T invalid_base_2();
55 constexpr size_t bos_dtor = __builtin_object_size(&(T&)(T&&)invalid_base_2(), 0); // expected-error {{must be initialized by a constant expression}} \
56 // expected-note {{non-literal type 'T'}}
57 }