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 // <rdar://problem/10063539>
17 template<int (*Compare)(const char *s1, const char *s2)>
18 int equal(const char *s1, const char *s2) {
19   return Compare(s1, s2) == 0;
20 }
21 template int equal<&__builtin_strcmp>(const char*, const char*); // expected-error {{builtin functions must be directly called}}
22 
23 // PR13195
24 void f2() {
25   __builtin_isnan; // expected-error {{builtin functions must be directly called}}
26 }
27 
28 // pr14895
29 typedef __typeof(sizeof(int)) size_t;
30 extern "C" void *__builtin_alloca (size_t);
31 
32 namespace addressof {
33   struct S {} s;
34   static_assert(__builtin_addressof(s) == &s, "");
35 
36   struct T { constexpr T *operator&() const { return nullptr; } int n; } t;
37   constexpr T *pt = __builtin_addressof(t);
38   static_assert(&pt->n == &t.n, "");
39 
40   struct U { int n : 5; } u;
41   int *pbf = __builtin_addressof(u.n); // expected-error {{address of bit-field requested}}
42 
43   S *ptmp = __builtin_addressof(S{}); // expected-error {{taking the address of a temporary}}
44 }
45 
46 namespace function_start {
47 void a(void) {}
48 int n;
49 void *p = __builtin_function_start(n);               // expected-error {{argument must be a function}}
50 static_assert(__builtin_function_start(a) == a, ""); // expected-error {{static assertion expression is not an integral constant expression}}
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_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);
91 }
92 
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}}
97 
98   TEST_TYPE(p, char*);
99   TEST_TYPE(ip, const volatile int*);
100   TEST_TYPE(fp, const float*);
101   TEST_TYPE(dp, double *__restrict);
102 
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);
107 }
108 
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 (**)());
114 }
115 
116 template <class Tp>
117 constexpr Tp *test_constexpr_launder(Tp *tp) {
118   return __builtin_launder(tp);
119 }
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), "");
125 
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}}
130 }
131 
132 constexpr bool test_in_constexpr(const int &i) {
133   return (__builtin_launder(&i) == &i);
134 }
135 
136 static_assert(test_in_constexpr(const_int), "");
137 void f() {
138   constexpr int i = 42;
139   static_assert(test_in_constexpr(i), "");
140 }
141 
142 struct Incomplete; // expected-note {{forward declaration}}
143 struct IncompleteMember {
144   Incomplete &i;
145 };
146 void test_incomplete(Incomplete *i, IncompleteMember *im) {
147   // expected-error@+1 {{incomplete type 'test_launder::Incomplete' where a complete type is required}}
148   __builtin_launder(i);
149   __builtin_launder(&i); // OK
150   __builtin_launder(im); // OK
151 }
152 
153 void test_noexcept(int *i) {
154   static_assert(noexcept(__builtin_launder(i)), "");
155 }
156 #undef TEST_TYPE
157 } // end namespace test_launder
158 
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}}
163 }
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}}
167