annotate clang/test/SemaCXX/builtins.cpp @ 236:c4bab56944e8 llvm-original

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