Debian Codegraph @

clang/test/SemaCXX/builtins.cpp

raw
1 // RUN: %clang_cc1 %s -fsyntax-only -verify -std=c++11 -fcxx-exceptions
2 // RUN: %clang_cc1 %s -fsyntax-only -verify -std=c++1z -fcxx-exceptions
3 typedef const struct __CFString * CFStringRef;
4 #define CFSTR __builtin___CFStringMakeConstantString
5
6 void f() {
7 #if !defined(__MVS__) && !defined(_AIX)
8 // Builtin function __builtin___CFStringMakeConstantString is currently
9 // unsupported on z/OS and AIX.
10 (void)CFStringRef(CFSTR("Hello"));
11 #endif
12 }
13
14 void a() { __builtin_va_list x, y; ::__builtin_va_copy(x, y); }
15
16 template<int (*Compare)(const char *s1, const char *s2)>
17 int equal(const char *s1, const char *s2) {
18 return Compare(s1, s2) == 0;
19 }
20 template int equal<&__builtin_strcmp>(const char*, const char*); // expected-error {{builtin functions must be directly called}}
21
22 // PR13195
23 void f2() {
24 __builtin_isnan; // expected-error {{builtin functions must be directly called}}
25 }
26
27 // pr14895
28 typedef __typeof(sizeof(int)) size_t;
29 extern "C" void *__builtin_alloca (size_t);
30
31 namespace addressof {
32 struct S {} s;
33 static_assert(__builtin_addressof(s) == &s, "");
34
35 struct T { constexpr T *operator&() const { return nullptr; } int n; } t;
36 constexpr T *pt = __builtin_addressof(t);
37 static_assert(&pt->n == &t.n, "");
38
39 struct U { int n : 5; } u;
40 int *pbf = __builtin_addressof(u.n); // expected-error {{address of bit-field requested}}
41
42 S *ptmp = __builtin_addressof(S{}); // expected-error {{taking the address of a temporary}} expected-warning {{temporary whose address is used as value of local variable 'ptmp' will be destroyed at the end of the full-expression}}
43 }
44
45 namespace function_start {
46 void a(void) {}
47 int n;
48 void *p = __builtin_function_start(n); // expected-error {{argument must be a function}}
49 static_assert(__builtin_function_start(a) == a, ""); // expected-error {{static assertion expression is not an integral constant expression}}
50 // expected-note@-1 {{comparison of addresses of literals has unspecified value}}
51 } // namespace function_start
52
53 void no_ms_builtins() {
54 __assume(1); // expected-error {{use of undeclared}}
55 __noop(1); // expected-error {{use of undeclared}}
56 __debugbreak(); // expected-error {{use of undeclared}}
57 }
58
59 struct FILE;
60 extern "C" int vfprintf(FILE *__restrict, const char *__restrict,
61 __builtin_va_list va);
62
63 void synchronize_args() {
64 __sync_synchronize(0); // expected-error {{too many arguments}}
65 }
66
67 namespace test_launder {
68 #define TEST_TYPE(Ptr, Type) \
69 static_assert(__is_same(decltype(__builtin_launder(Ptr)), Type), "expected same type")
70
71 struct Dummy {};
72
73 using FnType = int(char);
74 using MemFnType = int (Dummy::*)(char);
75 using ConstMemFnType = int (Dummy::*)() const;
76
77 void foo() {}
78
79 void test_builtin_empty_parentheses_diags() {
80 __is_trivially_copyable(); // expected-error {{expected a type}}
81 __is_trivially_copyable(1); // expected-error {{expected a type}}
82 }
83
84 void test_builtin_launder_diags(void *vp, const void *cvp, FnType *fnp,
85 MemFnType mfp, ConstMemFnType cmfp, int (&Arr)[5]) {
86 __builtin_launder(vp); // expected-error {{void pointer argument to '__builtin_launder' is not allowed}}
87 __builtin_launder(cvp); // expected-error {{void pointer argument to '__builtin_launder' is not allowed}}
88 __builtin_launder(fnp); // expected-error {{function pointer argument to '__builtin_launder' is not allowed}}
89 __builtin_launder(mfp); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
90 __builtin_launder(cmfp); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
91 (void)__builtin_launder(&fnp);
92 __builtin_launder(42); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
93 __builtin_launder(nullptr); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
94 __builtin_launder(foo); // expected-error {{function pointer argument to '__builtin_launder' is not allowed}}
95 (void)__builtin_launder(Arr);
96 }
97
98 void test_builtin_launder(char *p, const volatile int *ip, const float *&fp,
99 double *__restrict dp) {
100 int x;
101 __builtin_launder(x); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
102
103 TEST_TYPE(p, char*);
104 TEST_TYPE(ip, const volatile int*);
105 TEST_TYPE(fp, const float*);
106 TEST_TYPE(dp, double *__restrict);
107
108 char *d = __builtin_launder(p);
109 const volatile int *id = __builtin_launder(ip);
110 int *id2 = __builtin_launder(ip); // expected-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const volatile int *'}}
111 const float* fd = __builtin_launder(fp);
112 }
113
114 void test_launder_return_type(const int (&ArrayRef)[101], int (&MArrRef)[42][13],
115 void (**&FuncPtrRef)()) {
116 TEST_TYPE(ArrayRef, const int *);
117 TEST_TYPE(MArrRef, int(*)[13]);
118 TEST_TYPE(FuncPtrRef, void (**)());
119 }
120
121 template <class Tp>
122 constexpr Tp *test_constexpr_launder(Tp *tp) {
123 return __builtin_launder(tp);
124 }
125 constexpr int const_int = 42;
126 constexpr int const_int2 = 101;
127 constexpr const int *const_ptr = test_constexpr_launder(&const_int);
128 static_assert(&const_int == const_ptr, "");
129 static_assert(const_ptr != test_constexpr_launder(&const_int2), "");
130
131 void test_non_constexpr() {
132 constexpr int i = 42; // expected-note {{address of non-static constexpr variable 'i' may differ on each invocation}}
133 constexpr const int *ip = __builtin_launder(&i); // expected-error {{constexpr variable 'ip' must be initialized by a constant expression}}
134 // expected-note@-1 {{pointer to 'i' is not a constant expression}}
135 }
136
137 constexpr bool test_in_constexpr(const int &i) {
138 return (__builtin_launder(&i) == &i);
139 }
140
141 static_assert(test_in_constexpr(const_int), "");
142 void f() {
143 constexpr int i = 42;
144 static_assert(test_in_constexpr(i), "");
145 }
146
147 struct Incomplete; // expected-note {{forward declaration}}
148 struct IncompleteMember {
149 Incomplete &i;
150 };
151 void test_incomplete(Incomplete *i, IncompleteMember *im) {
152 // expected-error@+1 {{incomplete type 'Incomplete' where a complete type is required}}
153 __builtin_launder(i);
154 __builtin_launder(&i); // OK
155 __builtin_launder(im); // OK
156 }
157
158 void test_noexcept(int *i) {
159 static_assert(noexcept(__builtin_launder(i)), "");
160 }
161 #undef TEST_TYPE
162 } // end namespace test_launder
163
164 template<typename T> void test_builtin_complex(T v, double d) {
165 (void)__builtin_complex(v, d); // expected-error {{different types}} expected-error {{not a real floating}}
166 (void)__builtin_complex(d, v); // expected-error {{different types}} expected-error {{not a real floating}}
167 (void)__builtin_complex(v, v); // expected-error {{not a real floating}}
168 }
169 template void test_builtin_complex(double, double);
170 template void test_builtin_complex(float, double); // expected-note {{instantiation of}}
171 template void test_builtin_complex(int, double); // expected-note {{instantiation of}}
172
173 #ifdef __x86_64__
174 // This previously would cause an assertion when emitting the note diagnostic.
175 static void __builtin_cpu_init(); // expected-error {{static declaration of '__builtin_cpu_init' follows non-static declaration}} \
176 expected-note {{'__builtin_cpu_init' is a builtin with type 'void () noexcept'}}
177 #endif