Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / builtins.cpp
blob567094c94c171b85439f8353ded1975a89398c40
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
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
14 void a() { __builtin_va_list x, y; ::__builtin_va_copy(x, y); }
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;
20 template int equal<&__builtin_strcmp>(const char*, const char*); // expected-error {{builtin functions must be directly called}}
22 // PR13195
23 void f2() {
24 __builtin_isnan; // expected-error {{builtin functions must be directly called}}
27 // pr14895
28 typedef __typeof(sizeof(int)) size_t;
29 extern "C" void *__builtin_alloca (size_t);
31 namespace addressof {
32 struct S {} s;
33 static_assert(__builtin_addressof(s) == &s, "");
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, "");
39 struct U { int n : 5; } u;
40 int *pbf = __builtin_addressof(u.n); // expected-error {{address of bit-field requested}}
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}}
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
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}}
59 struct FILE;
60 extern "C" int vfprintf(FILE *__restrict, const char *__restrict,
61 __builtin_va_list va);
63 void synchronize_args() {
64 __sync_synchronize(0); // expected-error {{too many arguments}}
67 namespace test_launder {
68 #define TEST_TYPE(Ptr, Type) \
69 static_assert(__is_same(decltype(__builtin_launder(Ptr)), Type), "expected same type")
71 struct Dummy {};
73 using FnType = int(char);
74 using MemFnType = int (Dummy::*)(char);
75 using ConstMemFnType = int (Dummy::*)() const;
77 void foo() {}
79 void test_builtin_launder_diags(void *vp, const void *cvp, FnType *fnp,
80 MemFnType mfp, ConstMemFnType cmfp, int (&Arr)[5]) {
81 __builtin_launder(vp); // expected-error {{void pointer argument to '__builtin_launder' is not allowed}}
82 __builtin_launder(cvp); // expected-error {{void pointer argument to '__builtin_launder' is not allowed}}
83 __builtin_launder(fnp); // expected-error {{function pointer argument to '__builtin_launder' is not allowed}}
84 __builtin_launder(mfp); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
85 __builtin_launder(cmfp); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
86 (void)__builtin_launder(&fnp);
87 __builtin_launder(42); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
88 __builtin_launder(nullptr); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
89 __builtin_launder(foo); // expected-error {{function pointer argument to '__builtin_launder' is not allowed}}
90 (void)__builtin_launder(Arr);
93 void test_builtin_launder(char *p, const volatile int *ip, const float *&fp,
94 double *__restrict dp) {
95 int x;
96 __builtin_launder(x); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
98 TEST_TYPE(p, char*);
99 TEST_TYPE(ip, const volatile int*);
100 TEST_TYPE(fp, const float*);
101 TEST_TYPE(dp, double *__restrict);
103 char *d = __builtin_launder(p);
104 const volatile int *id = __builtin_launder(ip);
105 int *id2 = __builtin_launder(ip); // expected-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const volatile int *'}}
106 const float* fd = __builtin_launder(fp);
109 void test_launder_return_type(const int (&ArrayRef)[101], int (&MArrRef)[42][13],
110 void (**&FuncPtrRef)()) {
111 TEST_TYPE(ArrayRef, const int *);
112 TEST_TYPE(MArrRef, int(*)[13]);
113 TEST_TYPE(FuncPtrRef, void (**)());
116 template <class Tp>
117 constexpr Tp *test_constexpr_launder(Tp *tp) {
118 return __builtin_launder(tp);
120 constexpr int const_int = 42;
121 constexpr int const_int2 = 101;
122 constexpr const int *const_ptr = test_constexpr_launder(&const_int);
123 static_assert(&const_int == const_ptr, "");
124 static_assert(const_ptr != test_constexpr_launder(&const_int2), "");
126 void test_non_constexpr() {
127 constexpr int i = 42; // expected-note {{address of non-static constexpr variable 'i' may differ on each invocation}}
128 constexpr const int *ip = __builtin_launder(&i); // expected-error {{constexpr variable 'ip' must be initialized by a constant expression}}
129 // expected-note@-1 {{pointer to 'i' is not a constant expression}}
132 constexpr bool test_in_constexpr(const int &i) {
133 return (__builtin_launder(&i) == &i);
136 static_assert(test_in_constexpr(const_int), "");
137 void f() {
138 constexpr int i = 42;
139 static_assert(test_in_constexpr(i), "");
142 struct Incomplete; // expected-note {{forward declaration}}
143 struct IncompleteMember {
144 Incomplete &i;
146 void test_incomplete(Incomplete *i, IncompleteMember *im) {
147 // expected-error@+1 {{incomplete type 'Incomplete' where a complete type is required}}
148 __builtin_launder(i);
149 __builtin_launder(&i); // OK
150 __builtin_launder(im); // OK
153 void test_noexcept(int *i) {
154 static_assert(noexcept(__builtin_launder(i)), "");
156 #undef TEST_TYPE
157 } // end namespace test_launder
159 template<typename T> void test_builtin_complex(T v, double d) {
160 (void)__builtin_complex(v, d); // expected-error {{different types}} expected-error {{not a real floating}}
161 (void)__builtin_complex(d, v); // expected-error {{different types}} expected-error {{not a real floating}}
162 (void)__builtin_complex(v, v); // expected-error {{not a real floating}}
164 template void test_builtin_complex(double, double);
165 template void test_builtin_complex(float, double); // expected-note {{instantiation of}}
166 template void test_builtin_complex(int, double); // expected-note {{instantiation of}}
168 #ifdef __x86_64__
169 // This previously would cause an assertion when emitting the note diagnostic.
170 static void __builtin_cpu_init(); // expected-error {{static declaration of '__builtin_cpu_init' follows non-static declaration}} \
171 expected-note {{'__builtin_cpu_init' is a builtin with type 'void () noexcept'}}
172 #endif