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 
f()6 void f() {
7   (void)CFStringRef(CFSTR("Hello"));
8 }
9 
a()10 void a() { __builtin_va_list x, y; ::__builtin_va_copy(x, y); }
11 
12 // <rdar://problem/10063539>
13 template<int (*Compare)(const char *s1, const char *s2)>
equal(const char * s1,const char * s2)14 int equal(const char *s1, const char *s2) {
15   return Compare(s1, s2) == 0;
16 }
17 template int equal<&__builtin_strcmp>(const char*, const char*); // expected-error {{builtin functions must be directly called}}
18 
19 // PR13195
f2()20 void f2() {
21   __builtin_isnan; // expected-error {{builtin functions must be directly called}}
22 }
23 
24 // pr14895
25 typedef __typeof(sizeof(int)) size_t;
26 extern "C" void *__builtin_alloca (size_t);
27 
28 namespace addressof {
29   struct S {} s;
30   static_assert(__builtin_addressof(s) == &s, "");
31 
operator &addressof::T32   struct T { constexpr T *operator&() const { return nullptr; } int n; } t;
33   constexpr T *pt = __builtin_addressof(t);
34   static_assert(&pt->n == &t.n, "");
35 
36   struct U { int n : 5; } u;
37   int *pbf = __builtin_addressof(u.n); // expected-error {{address of bit-field requested}}
38 
39   S *ptmp = __builtin_addressof(S{}); // expected-error {{taking the address of a temporary}}
40 }
41 
no_ms_builtins()42 void no_ms_builtins() {
43   __assume(1); // expected-error {{use of undeclared}}
44   __noop(1); // expected-error {{use of undeclared}}
45   __debugbreak(); // expected-error {{use of undeclared}}
46 }
47 
48 struct FILE;
49 extern "C" int vfprintf(FILE *__restrict, const char *__restrict,
50                         __builtin_va_list va);
51 
synchronize_args()52 void synchronize_args() {
53   __sync_synchronize(0); // expected-error {{too many arguments}}
54 }
55 
56 namespace test_launder {
57 #define TEST_TYPE(Ptr, Type) \
58   static_assert(__is_same(decltype(__builtin_launder(Ptr)), Type), "expected same type")
59 
60 struct Dummy {};
61 
62 using FnType = int(char);
63 using MemFnType = int (Dummy::*)(char);
64 using ConstMemFnType = int (Dummy::*)() const;
65 
foo()66 void foo() {}
67 
test_builtin_launder_diags(void * vp,const void * cvp,FnType * fnp,MemFnType mfp,ConstMemFnType cmfp,int (& Arr)[5])68 void test_builtin_launder_diags(void *vp, const void *cvp, FnType *fnp,
69                                 MemFnType mfp, ConstMemFnType cmfp, int (&Arr)[5]) {
70   __builtin_launder(vp);   // expected-error {{void pointer argument to '__builtin_launder' is not allowed}}
71   __builtin_launder(cvp);  // expected-error {{void pointer argument to '__builtin_launder' is not allowed}}
72   __builtin_launder(fnp);  // expected-error {{function pointer argument to '__builtin_launder' is not allowed}}
73   __builtin_launder(mfp);  // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
74   __builtin_launder(cmfp); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
75   (void)__builtin_launder(&fnp);
76   __builtin_launder(42);      // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
77   __builtin_launder(nullptr); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
78   __builtin_launder(foo);     // expected-error {{function pointer argument to '__builtin_launder' is not allowed}}
79   (void)__builtin_launder(Arr);
80 }
81 
test_builtin_launder(char * p,const volatile int * ip,const float * & fp,double * __restrict dp)82 void test_builtin_launder(char *p, const volatile int *ip, const float *&fp,
83                           double *__restrict dp) {
84   int x;
85   __builtin_launder(x); // expected-error {{non-pointer argument to '__builtin_launder' is not allowed}}
86 
87   TEST_TYPE(p, char*);
88   TEST_TYPE(ip, const volatile int*);
89   TEST_TYPE(fp, const float*);
90   TEST_TYPE(dp, double *__restrict);
91 
92   char *d = __builtin_launder(p);
93   const volatile int *id = __builtin_launder(ip);
94   int *id2 = __builtin_launder(ip); // expected-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const volatile int *'}}
95   const float* fd = __builtin_launder(fp);
96 }
97 
test_launder_return_type(const int (& ArrayRef)[101],int (& MArrRef)[42][13],void (** & FuncPtrRef)())98 void test_launder_return_type(const int (&ArrayRef)[101], int (&MArrRef)[42][13],
99                               void (**&FuncPtrRef)()) {
100   TEST_TYPE(ArrayRef, const int *);
101   TEST_TYPE(MArrRef, int(*)[13]);
102   TEST_TYPE(FuncPtrRef, void (**)());
103 }
104 
105 template <class Tp>
test_constexpr_launder(Tp * tp)106 constexpr Tp *test_constexpr_launder(Tp *tp) {
107   return __builtin_launder(tp);
108 }
109 constexpr int const_int = 42;
110 constexpr int const_int2 = 101;
111 constexpr const int *const_ptr = test_constexpr_launder(&const_int);
112 static_assert(&const_int == const_ptr, "");
113 static_assert(const_ptr != test_constexpr_launder(&const_int2), "");
114 
test_non_constexpr()115 void test_non_constexpr() {
116   constexpr int i = 42;                            // expected-note {{address of non-static constexpr variable 'i' may differ on each invocation}}
117   constexpr const int *ip = __builtin_launder(&i); // expected-error {{constexpr variable 'ip' must be initialized by a constant expression}}
118   // expected-note@-1 {{pointer to 'i' is not a constant expression}}
119 }
120 
test_in_constexpr(const int & i)121 constexpr bool test_in_constexpr(const int &i) {
122   return (__builtin_launder(&i) == &i);
123 }
124 
125 static_assert(test_in_constexpr(const_int), "");
f()126 void f() {
127   constexpr int i = 42;
128   static_assert(test_in_constexpr(i), "");
129 }
130 
131 struct Incomplete; // expected-note {{forward declaration}}
132 struct IncompleteMember {
133   Incomplete &i;
134 };
test_incomplete(Incomplete * i,IncompleteMember * im)135 void test_incomplete(Incomplete *i, IncompleteMember *im) {
136   // expected-error@+1 {{incomplete type 'test_launder::Incomplete' where a complete type is required}}
137   __builtin_launder(i);
138   __builtin_launder(&i); // OK
139   __builtin_launder(im); // OK
140 }
141 
test_noexcept(int * i)142 void test_noexcept(int *i) {
143   static_assert(noexcept(__builtin_launder(i)), "");
144 }
145 #undef TEST_TYPE
146 } // end namespace test_launder
147 
test_builtin_complex(T v,double d)148 template<typename T> void test_builtin_complex(T v, double d) {
149   (void)__builtin_complex(v, d); // expected-error {{different types}} expected-error {{not a real floating}}
150   (void)__builtin_complex(d, v); // expected-error {{different types}} expected-error {{not a real floating}}
151   (void)__builtin_complex(v, v); // expected-error {{not a real floating}}
152 }
153 template void test_builtin_complex(double, double);
154 template void test_builtin_complex(float, double); // expected-note {{instantiation of}}
155 template void test_builtin_complex(int, double); // expected-note {{instantiation of}}
156