annotate clang/test/SemaCXX/builtins.cpp @ 221:79ff65ed7e25

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