1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // <functional>
10
11 // class function<R(ArgTypes...)>
12
13 // function(Fp);
14
15 // Ensure that __not_null works for all function types.
16 // See https://llvm.org/PR23589
17
18 // This test runs in C++03, but we have deprecated using std::function in C++03.
19 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
20
21 //------------------------------------------------------------------------------
22 // TESTING std::function<...>::__not_null(Callable)
23 //
24 // Concerns:
25 // 1) The call __not_null(Callable) is well formed and correct for each
26 // possible 'Callable' type category. These categories include:
27 // 1a) function pointers
28 // 1b) member function pointer
29 // 1c) member data pointer
30 // 1d) callable class type
31 // 1e) lambdas
32 // Categories 1a, 1b, and 1c are 'Nullable' types. Only objects of these
33 // types can be null. The other categories are not tested here.
34 // 3) '__not_null(Callable)' is well formed when the call signature includes
35 // varargs.
36 // 4) '__not_null(Callable)' works for Callable types with all arities less
37 // than or equal to 3 in C++03.
38 // 5) '__not_null(Callable)' works when 'Callable' is a member function
39 // pointer to a cv or ref qualified function type.
40 //
41 // Plan:
42 // 1 For categories 1a, 1b and 1c define a set of
43 // 'Callable' objects for this category. This set should include examples
44 // of arity 0, 1, 2 and possible 3 including versions with varargs as the
45 // last parameter.
apply_ID3(struct moduleinfostruct * m,struct ID3_t * dest)46 //
47 // 2 For each 'Callable' object in categories 1a, 1b and 1c do the following.
48 //
49 // 1 Define a type 'std::function<Sig>' as 'F' where 'Sig' is compatible with
50 // the signature of the 'Callable' object.
51 //
52 // 2 Create an object of type 'F' using a null pointer of type 'Callable'.
53 // Check that 'F.target<Callable>()' is null.
54 //
55 // 3 Create an object of type 'F' that is not null. Check that
56 // 'F.target<Callable>()' is not null and is equal to the original
57 // argument.
58
59 #include <functional>
60 #include <type_traits>
61 #include <cassert>
62
63 #include "test_macros.h"
64
65 ///////////////////////////////////////////////////////////////////////////////
66 int foo() { return 42; }
67 int foo(int) { return 42; }
68 int foo(int, int) { return 42; }
69 int foo(int, int, int) { return 42; }
70
71 int foo(...) { return 42; }
72 int foo(int, ...) { return 42; }
73 int foo(int, int, ...) { return 42; }
74 int foo(int, int, int, ...) { return 42; }
75
76 ///////////////////////////////////////////////////////////////////////////////
77 struct MemFun03 {
78 int foo() { return 42; }
79 int foo() const { return 42; }
80 int foo() volatile { return 42; }
81 int foo() const volatile { return 42; }
parseid3v1(struct moduleinfostruct * m,const uint8_t * tag)82
83 int foo(int) { return 42; }
84 int foo(int) const { return 42; }
85 int foo(int) volatile { return 42; }
86 int foo(int) const volatile { return 42; }
87
88 int foo(int, int) { return 42; }
89 int foo(int, int) const { return 42; }
90 int foo(int, int) volatile { return 42; }
91 int foo(int, int) const volatile { return 42; }
92
93 int foo(int, int, int) { return 42; }
94 int foo(int, int, int) const { return 42; }
parseid3v12(struct moduleinfostruct * m,const unsigned char * tag)95 int foo(int, int, int) volatile { return 42; }
96 int foo(int, int, int) const volatile { return 42; }
97
98 int foo(...) { return 42; }
99 int foo(...) const { return 42; }
100 int foo(...) volatile { return 42; }
101 int foo(...) const volatile { return 42; }
102
103 int foo(int, ...) { return 42; }
104 int foo(int, ...) const { return 42; }
105 int foo(int, ...) volatile { return 42; }
106 int foo(int, ...) const volatile { return 42; }
107
108 int foo(int, int, ...) { return 42; }
parseid3v2(struct moduleinfostruct * m,uint8_t * tag,uint32_t len)109 int foo(int, int, ...) const { return 42; }
110 int foo(int, int, ...) volatile { return 42; }
111 int foo(int, int, ...) const volatile { return 42; }
112
113 int foo(int, int, int, ...) { return 42; }
114 int foo(int, int, int, ...) const { return 42; }
115 int foo(int, int, int, ...) volatile { return 42; }
116 int foo(int, int, int, ...) const volatile { return 42; }
117 };
118
119 #if TEST_STD_VER >= 11
120 struct MemFun11 {
121 int foo() & { return 42; }
122 int foo() const & { return 42; }
123 int foo() volatile & { return 42; }
124 int foo() const volatile & { return 42; }
125
126 int foo(...) & { return 42; }
fetch16(const uint8_t * buf)127 int foo(...) const & { return 42; }
128 int foo(...) volatile & { return 42; }
129 int foo(...) const volatile & { return 42; }
130
131 int foo() && { return 42; }
fetch32(const uint8_t * buf)132 int foo() const && { return 42; }
133 int foo() volatile && { return 42; }
134 int foo() const volatile && { return 42; }
135
136 int foo(...) && { return 42; }
ampegpReadMemInfo(struct moduleinfostruct * m,const char * _buf,size_t len)137 int foo(...) const && { return 42; }
138 int foo(...) volatile && { return 42; }
139 int foo(...) const volatile && { return 42; }
140 };
141 #endif // TEST_STD_VER >= 11
142
143 struct MemData {
144 int foo;
145 };
146
147 // Create a non-null free function by taking the address of
148 // &static_cast<Tp&>(foo);
149 template <class Tp>
150 struct Creator {
151 static Tp create() {
152 return &foo;
153 }
154 };
155
156 // Create a non-null member pointer.
157 template <class Ret, class Class>
158 struct Creator<Ret Class::*> {
159 typedef Ret Class::*ReturnType;
160 static ReturnType create() {
161 return &Class::foo;
162 }
163 };
164
165 template <class TestFn, class Fn>
166 void test_imp() {
167 { // Check that the null value is detected
168 TestFn tf = nullptr;
169 std::function<Fn> f = tf;
170 RTTI_ASSERT(f.template target<TestFn>() == nullptr);
171 }
172 { // Check that the non-null value is detected.
173 TestFn tf = Creator<TestFn>::create();
174 assert(tf != nullptr);
175 std::function<Fn> f = tf;
176 RTTI_ASSERT(f.template target<TestFn>() != nullptr);
177 RTTI_ASSERT(*f.template target<TestFn>() == tf);
178 }
179 }
180
181 void test_func() {
182 test_imp<int(*)(), int()>();
183 test_imp<int(*)(...), int()>();
184 test_imp<int(*)(int), int(int)>();
185 test_imp<int(*)(int, ...), int(int)>();
186 test_imp<int(*)(int, int), int(int, int)>();
187 test_imp<int(*)(int, int, ...), int(int, int)>();
188 test_imp<int(*)(int, int, int), int(int, int, int)>();
189 test_imp<int(*)(int, int, int, ...), int(int, int, int)>();
190 }
191
192 void test_mf() {
193 test_imp<int(MemFun03::*)(), int(MemFun03&)>();
194 test_imp<int(MemFun03::*)(...), int(MemFun03&)>();
195 test_imp<int(MemFun03::*)() const, int(MemFun03&)>();
196 test_imp<int(MemFun03::*)(...) const, int(MemFun03&)>();
197 test_imp<int(MemFun03::*)() volatile, int(MemFun03&)>();
198 test_imp<int(MemFun03::*)(...) volatile, int(MemFun03&)>();
199 test_imp<int(MemFun03::*)() const volatile, int(MemFun03&)>();
200 test_imp<int(MemFun03::*)(...) const volatile, int(MemFun03&)>();
201
202 test_imp<int(MemFun03::*)(int), int(MemFun03&, int)>();
203 test_imp<int(MemFun03::*)(int, ...), int(MemFun03&, int)>();
204 test_imp<int(MemFun03::*)(int) const, int(MemFun03&, int)>();
205 test_imp<int(MemFun03::*)(int, ...) const, int(MemFun03&, int)>();
206 test_imp<int(MemFun03::*)(int) volatile, int(MemFun03&, int)>();
207 test_imp<int(MemFun03::*)(int, ...) volatile, int(MemFun03&, int)>();
208 test_imp<int(MemFun03::*)(int) const volatile, int(MemFun03&, int)>();
209 test_imp<int(MemFun03::*)(int, ...) const volatile, int(MemFun03&, int)>();
210
211 test_imp<int(MemFun03::*)(int, int), int(MemFun03&, int, int)>();
212 test_imp<int(MemFun03::*)(int, int, ...), int(MemFun03&, int, int)>();
213 test_imp<int(MemFun03::*)(int, int) const, int(MemFun03&, int, int)>();
214 test_imp<int(MemFun03::*)(int, int, ...) const, int(MemFun03&, int, int)>();
215 test_imp<int(MemFun03::*)(int, int) volatile, int(MemFun03&, int, int)>();
216 test_imp<int(MemFun03::*)(int, int, ...) volatile, int(MemFun03&, int, int)>();
217 test_imp<int(MemFun03::*)(int, int) const volatile, int(MemFun03&, int, int)>();
218 test_imp<int(MemFun03::*)(int, int, ...) const volatile, int(MemFun03&, int, int)>();
219
220 #if TEST_STD_VER >= 11
221 test_imp<int(MemFun11::*)() &, int(MemFun11&)>();
222 test_imp<int(MemFun11::*)(...) &, int(MemFun11&)>();
223 test_imp<int(MemFun11::*)() const &, int(MemFun11&)>();
224 test_imp<int(MemFun11::*)(...) const &, int(MemFun11&)>();
225 test_imp<int(MemFun11::*)() volatile &, int(MemFun11&)>();
226 test_imp<int(MemFun11::*)(...) volatile &, int(MemFun11&)>();
227 test_imp<int(MemFun11::*)() const volatile &, int(MemFun11&)>();
228 test_imp<int(MemFun11::*)(...) const volatile &, int(MemFun11&)>();
229
230 test_imp<int(MemFun11::*)() &&, int(MemFun11&&)>();
231 test_imp<int(MemFun11::*)(...) &&, int(MemFun11&&)>();
232 test_imp<int(MemFun11::*)() const &&, int(MemFun11&&)>();
233 test_imp<int(MemFun11::*)(...) const &&, int(MemFun11&&)>();
234 test_imp<int(MemFun11::*)() volatile &&, int(MemFun11&&)>();
235 test_imp<int(MemFun11::*)(...) volatile &&, int(MemFun11&&)>();
236 test_imp<int(MemFun11::*)() const volatile &&, int(MemFun11&&)>();
237 test_imp<int(MemFun11::*)(...) const volatile &&, int(MemFun11&&)>();
238 #endif
239 }
240
241 void test_md() {
242 test_imp<int MemData::*, int(MemData&)>();
243 }
244
245 int main(int, char**) {
246 test_func();
247 test_mf();
248 test_md();
249
250 return 0;
251 }
252