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 -D_LIBCPP_ENABLE_CXX03_FUNCTION
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.
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 ///////////////////////////////////////////////////////////////////////////////
foo()66 int foo() { return 42; }
foo(int)67 int foo(int) { return 42; }
foo(int,int)68 int foo(int, int) { return 42; }
foo(int,int,int)69 int foo(int, int, int) { return 42; }
70
foo(...)71 int foo(...) { return 42; }
foo(int,...)72 int foo(int, ...) { return 42; }
foo(int,int,...)73 int foo(int, int, ...) { return 42; }
foo(int,int,int,...)74 int foo(int, int, int, ...) { return 42; }
75
76 ///////////////////////////////////////////////////////////////////////////////
77 struct MemFun03 {
fooMemFun0378 int foo() { return 42; }
fooMemFun0379 int foo() const { return 42; }
fooMemFun0380 int foo() volatile { return 42; }
fooMemFun0381 int foo() const volatile { return 42; }
82
fooMemFun0383 int foo(int) { return 42; }
fooMemFun0384 int foo(int) const { return 42; }
fooMemFun0385 int foo(int) volatile { return 42; }
fooMemFun0386 int foo(int) const volatile { return 42; }
87
fooMemFun0388 int foo(int, int) { return 42; }
fooMemFun0389 int foo(int, int) const { return 42; }
fooMemFun0390 int foo(int, int) volatile { return 42; }
fooMemFun0391 int foo(int, int) const volatile { return 42; }
92
fooMemFun0393 int foo(int, int, int) { return 42; }
fooMemFun0394 int foo(int, int, int) const { return 42; }
fooMemFun0395 int foo(int, int, int) volatile { return 42; }
fooMemFun0396 int foo(int, int, int) const volatile { return 42; }
97
fooMemFun0398 int foo(...) { return 42; }
fooMemFun0399 int foo(...) const { return 42; }
fooMemFun03100 int foo(...) volatile { return 42; }
fooMemFun03101 int foo(...) const volatile { return 42; }
102
fooMemFun03103 int foo(int, ...) { return 42; }
fooMemFun03104 int foo(int, ...) const { return 42; }
fooMemFun03105 int foo(int, ...) volatile { return 42; }
fooMemFun03106 int foo(int, ...) const volatile { return 42; }
107
fooMemFun03108 int foo(int, int, ...) { return 42; }
fooMemFun03109 int foo(int, int, ...) const { return 42; }
fooMemFun03110 int foo(int, int, ...) volatile { return 42; }
fooMemFun03111 int foo(int, int, ...) const volatile { return 42; }
112
fooMemFun03113 int foo(int, int, int, ...) { return 42; }
fooMemFun03114 int foo(int, int, int, ...) const { return 42; }
fooMemFun03115 int foo(int, int, int, ...) volatile { return 42; }
fooMemFun03116 int foo(int, int, int, ...) const volatile { return 42; }
117 };
118
119 #if TEST_STD_VER >= 11
120 struct MemFun11 {
fooMemFun11121 int foo() & { return 42; }
fooMemFun11122 int foo() const & { return 42; }
fooMemFun11123 int foo() volatile & { return 42; }
fooMemFun11124 int foo() const volatile & { return 42; }
125
fooMemFun11126 int foo(...) & { return 42; }
fooMemFun11127 int foo(...) const & { return 42; }
fooMemFun11128 int foo(...) volatile & { return 42; }
fooMemFun11129 int foo(...) const volatile & { return 42; }
130
fooMemFun11131 int foo() && { return 42; }
fooMemFun11132 int foo() const && { return 42; }
fooMemFun11133 int foo() volatile && { return 42; }
fooMemFun11134 int foo() const volatile && { return 42; }
135
fooMemFun11136 int foo(...) && { return 42; }
fooMemFun11137 int foo(...) const && { return 42; }
fooMemFun11138 int foo(...) volatile && { return 42; }
fooMemFun11139 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 {
createCreator151 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;
createCreator160 static ReturnType create() {
161 return &Class::foo;
162 }
163 };
164
165 template <class TestFn, class Fn>
test_imp()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
test_func()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
test_mf()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
test_md()241 void test_md() {
242 test_imp<int MemData::*, int(MemData&)>();
243 }
244
main(int,char **)245 int main(int, char**) {
246 test_func();
247 test_mf();
248 test_md();
249
250 return 0;
251 }
252