[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / SemaCXX / builtin-object-size-cxx14.cpp
blobb7c6f6be01f5444c682f5593516edcb9a1b18eea
1 // RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx14 -std=c++14 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++2a %s
4 typedef __SIZE_TYPE__ size_t;
6 namespace basic {
7 // Ensuring that __bos can be used in constexpr functions without anything
8 // sketchy going on...
9 constexpr int bos0() {
10 int k = 5;
11 char cs[10] = {};
12 return __builtin_object_size(&cs[k], 0);
15 constexpr int bos1() {
16 int k = 5;
17 char cs[10] = {};
18 return __builtin_object_size(&cs[k], 1);
21 constexpr int bos2() {
22 int k = 5;
23 char cs[10] = {};
24 return __builtin_object_size(&cs[k], 2);
27 constexpr int bos3() {
28 int k = 5;
29 char cs[10] = {};
30 return __builtin_object_size(&cs[k], 3);
33 static_assert(bos0() == sizeof(char) * 5, "");
34 static_assert(bos1() == sizeof(char) * 5, "");
35 static_assert(bos2() == sizeof(char) * 5, "");
36 static_assert(bos3() == sizeof(char) * 5, "");
39 namespace in_enable_if {
40 // The code that prompted these changes was __bos in enable_if
42 void copy5CharsInto(char *buf) // expected-note{{candidate}}
43 __attribute__((enable_if(__builtin_object_size(buf, 0) != -1 &&
44 __builtin_object_size(buf, 0) > 5,
45 "")));
47 // We use different EvalModes for __bos with type 0 versus 1. Ensure 1 works,
48 // too...
49 void copy5CharsIntoStrict(char *buf) // expected-note{{candidate}}
50 __attribute__((enable_if(__builtin_object_size(buf, 1) != -1 &&
51 __builtin_object_size(buf, 1) > 5,
52 "")));
54 struct LargeStruct {
55 int pad;
56 char buf[6];
57 int pad2;
60 struct SmallStruct {
61 int pad;
62 char buf[5];
63 int pad2;
66 void noWriteToBuf() {
67 char buf[6];
68 copy5CharsInto(buf);
70 LargeStruct large;
71 copy5CharsIntoStrict(large.buf);
74 void initTheBuf() {
75 char buf[6] = {};
76 copy5CharsInto(buf);
78 LargeStruct large = {0, {}, 0};
79 copy5CharsIntoStrict(large.buf);
82 int getI();
83 void initTheBufWithALoop() {
84 char buf[6] = {};
85 for (unsigned I = getI(); I != sizeof(buf); ++I)
86 buf[I] = I;
87 copy5CharsInto(buf);
89 LargeStruct large;
90 for (unsigned I = getI(); I != sizeof(buf); ++I)
91 large.buf[I] = I;
92 copy5CharsIntoStrict(large.buf);
95 void tooSmallBuf() {
96 char buf[5];
97 copy5CharsInto(buf); // expected-error{{no matching function for call}}
99 SmallStruct small;
100 copy5CharsIntoStrict(small.buf); // expected-error{{no matching function for call}}
104 namespace InvalidBase {
105 // Ensure this doesn't crash.
106 struct S { const char *name; };
107 S invalid_base();
108 constexpr size_t bos_name = __builtin_object_size(invalid_base().name, 1);
109 static_assert(bos_name == -1, "");
111 struct T { ~T(); };
112 T invalid_base_2();
113 constexpr size_t bos_dtor = __builtin_object_size(&(T&)(T&&)invalid_base_2(), 0);
114 static_assert(bos_dtor == -1, "");
117 // PR44268
118 constexpr int bos_new() { // cxx14-error {{constant expression}}
119 void *p = new int; // cxx14-note {{until C++20}}
120 return __builtin_object_size(p, 0);