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 // R operator()(ArgTypes... args) const
14 
15 // This test runs in C++03, but we have deprecated using std::function in C++03.
16 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS -D_LIBCPP_ENABLE_CXX03_FUNCTION
17 
18 // Address Sanitizer doesn't instrument weak symbols on Linux. When a key
19 // function is defined for bad_function_call's vtable, its typeinfo and vtable
20 // will be defined as strong symbols in the library and weak symbols in other
21 // translation units. Only the strong symbol will be instrumented, increasing
22 // its size (due to the redzone) and leading to a serious ODR violation
23 // resulting in a crash.
24 // Some relevant bugs:
25 // https://github.com/google/sanitizers/issues/1017
26 // https://github.com/google/sanitizers/issues/619
27 // https://github.com/google/sanitizers/issues/398
28 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68016
29 // UNSUPPORTED: asan
30 
31 #include <functional>
32 #include <cassert>
33 
34 #include "test_macros.h"
35 
36 
37 int count = 0;
38 
39 
40 // 0 args, return int
41 
f_int_0()42 int f_int_0()
43 {
44     return 3;
45 }
46 
47 struct A_int_0
48 {
operator ()A_int_049     int operator()() {return 4;}
50 };
51 
test_int_0()52 void test_int_0()
53 {
54     // function
55     {
56         std::function<int ()> r1(f_int_0);
57         assert(r1() == 3);
58     }
59     // function pointer
60     {
61         int (*fp)() = f_int_0;
62         std::function<int ()> r1(fp);
63         assert(r1() == 3);
64     }
65     // functor
66     {
67         A_int_0 a0;
68         std::function<int ()> r1(a0);
69         assert(r1() == 4);
70     }
71 }
72 
73 
74 // 0 args, return void
75 
f_void_0()76 void f_void_0()
77 {
78     ++count;
79 }
80 
81 struct A_void_0
82 {
operator ()A_void_083     void operator()() {++count;}
84 };
85 
86 void
test_void_0()87 test_void_0()
88 {
89     int save_count = count;
90     // function
91     {
92         std::function<void ()> r1(f_void_0);
93         r1();
94         assert(count == save_count+1);
95         save_count = count;
96     }
97     // function pointer
98     {
99         void (*fp)() = f_void_0;
100         std::function<void ()> r1(fp);
101         r1();
102         assert(count == save_count+1);
103         save_count = count;
104     }
105     // functor
106     {
107         A_void_0 a0;
108         std::function<void ()> r1(a0);
109         r1();
110         assert(count == save_count+1);
111         save_count = count;
112     }
113 }
114 
115 // 1 arg, return void
116 
f_void_1(int i)117 void f_void_1(int i)
118 {
119     count += i;
120 }
121 
122 struct A_void_1
123 {
operator ()A_void_1124     void operator()(int i)
125     {
126         count += i;
127     }
128 
mem1A_void_1129     void mem1() {++count;}
mem2A_void_1130     void mem2() const {++count;}
131 };
132 
133 void
test_void_1()134 test_void_1()
135 {
136     int save_count = count;
137     // function
138     {
139         std::function<void (int)> r1(f_void_1);
140         int i = 2;
141         r1(i);
142         assert(count == save_count+2);
143         save_count = count;
144     }
145     // function pointer
146     {
147         void (*fp)(int) = f_void_1;
148         std::function<void (int)> r1(fp);
149         int i = 3;
150         r1(i);
151         assert(count == save_count+3);
152         save_count = count;
153     }
154     // functor
155     {
156         A_void_1 a0;
157         std::function<void (int)> r1(a0);
158         int i = 4;
159         r1(i);
160         assert(count == save_count+4);
161         save_count = count;
162     }
163     // member function pointer
164     {
165         void (A_void_1::*fp)() = &A_void_1::mem1;
166         std::function<void (A_void_1)> r1(fp);
167         A_void_1 a;
168         r1(a);
169         assert(count == save_count+1);
170         save_count = count;
171         A_void_1* ap = &a;
172         std::function<void (A_void_1*)> r2 = fp;
173         r2(ap);
174         assert(count == save_count+1);
175         save_count = count;
176     }
177     // const member function pointer
178     {
179         void (A_void_1::*fp)() const = &A_void_1::mem2;
180         std::function<void (A_void_1)> r1(fp);
181         A_void_1 a;
182         r1(a);
183         assert(count == save_count+1);
184         save_count = count;
185         std::function<void (A_void_1*)> r2(fp);
186         A_void_1* ap = &a;
187         r2(ap);
188         assert(count == save_count+1);
189         save_count = count;
190     }
191 }
192 
193 // 1 arg, return int
194 
f_int_1(int i)195 int f_int_1(int i)
196 {
197     return i + 1;
198 }
199 
200 struct A_int_1
201 {
A_int_1A_int_1202     A_int_1() : data_(5) {}
operator ()A_int_1203     int operator()(int i)
204     {
205         return i - 1;
206     }
207 
mem1A_int_1208     int mem1() {return 3;}
mem2A_int_1209     int mem2() const {return 4;}
210     int data_;
211 };
212 
213 void
test_int_1()214 test_int_1()
215 {
216     // function
217     {
218         std::function<int (int)> r1(f_int_1);
219         int i = 2;
220         assert(r1(i) == 3);
221     }
222     // function pointer
223     {
224         int (*fp)(int) = f_int_1;
225         std::function<int (int)> r1(fp);
226         int i = 3;
227         assert(r1(i) == 4);
228     }
229     // functor
230     {
231         A_int_1 a0;
232         std::function<int (int)> r1(a0);
233         int i = 4;
234         assert(r1(i) == 3);
235     }
236     // member function pointer
237     {
238         int (A_int_1::*fp)() = &A_int_1::mem1;
239         std::function<int (A_int_1)> r1(fp);
240         A_int_1 a;
241         assert(r1(a) == 3);
242         std::function<int (A_int_1*)> r2(fp);
243         A_int_1* ap = &a;
244         assert(r2(ap) == 3);
245     }
246     // const member function pointer
247     {
248         int (A_int_1::*fp)() const = &A_int_1::mem2;
249         std::function<int (A_int_1)> r1(fp);
250         A_int_1 a;
251         assert(r1(a) == 4);
252         std::function<int (A_int_1*)> r2(fp);
253         A_int_1* ap = &a;
254         assert(r2(ap) == 4);
255     }
256     // member data pointer
257     {
258         int A_int_1::*fp = &A_int_1::data_;
259         std::function<int& (A_int_1&)> r1(fp);
260         A_int_1 a;
261         assert(r1(a) == 5);
262         r1(a) = 6;
263         assert(r1(a) == 6);
264         std::function<int& (A_int_1*)> r2(fp);
265         A_int_1* ap = &a;
266         assert(r2(ap) == 6);
267         r2(ap) = 7;
268         assert(r2(ap) == 7);
269     }
270 }
271 
272 // 2 arg, return void
273 
f_void_2(int i,int j)274 void f_void_2(int i, int j)
275 {
276     count += i+j;
277 }
278 
279 struct A_void_2
280 {
operator ()A_void_2281     void operator()(int i, int j)
282     {
283         count += i+j;
284     }
285 
mem1A_void_2286     void mem1(int i) {count += i;}
mem2A_void_2287     void mem2(int i) const {count += i;}
288 };
289 
290 void
test_void_2()291 test_void_2()
292 {
293     int save_count = count;
294     // function
295     {
296         std::function<void (int, int)> r1(f_void_2);
297         int i = 2;
298         int j = 3;
299         r1(i, j);
300         assert(count == save_count+5);
301         save_count = count;
302     }
303     // function pointer
304     {
305         void (*fp)(int, int) = f_void_2;
306         std::function<void (int, int)> r1(fp);
307         int i = 3;
308         int j = 4;
309         r1(i, j);
310         assert(count == save_count+7);
311         save_count = count;
312     }
313     // functor
314     {
315         A_void_2 a0;
316         std::function<void (int, int)> r1(a0);
317         int i = 4;
318         int j = 5;
319         r1(i, j);
320         assert(count == save_count+9);
321         save_count = count;
322     }
323     // member function pointer
324     {
325         void (A_void_2::*fp)(int) = &A_void_2::mem1;
326         std::function<void (A_void_2, int)> r1(fp);
327         A_void_2 a;
328         int i = 3;
329         r1(a, i);
330         assert(count == save_count+3);
331         save_count = count;
332         std::function<void (A_void_2*, int)> r2(fp);
333         A_void_2* ap = &a;
334         r2(ap, i);
335         assert(count == save_count+3);
336         save_count = count;
337     }
338     // const member function pointer
339     {
340         void (A_void_2::*fp)(int) const = &A_void_2::mem2;
341         std::function<void (A_void_2, int)> r1(fp);
342         A_void_2 a;
343         int i = 4;
344         r1(a, i);
345         assert(count == save_count+4);
346         save_count = count;
347         std::function<void (A_void_2*, int)> r2(fp);
348         A_void_2* ap = &a;
349         r2(ap, i);
350         assert(count == save_count+4);
351         save_count = count;
352     }
353 }
354 
355 // 2 arg, return int
356 
f_int_2(int i,int j)357 int f_int_2(int i, int j)
358 {
359     return i+j;
360 }
361 
362 struct A_int_2
363 {
operator ()A_int_2364     int operator()(int i, int j)
365     {
366         return i+j;
367     }
368 
mem1A_int_2369     int mem1(int i) {return i+1;}
mem2A_int_2370     int mem2(int i) const {return i+2;}
371 };
372 
test_int_2()373 void test_int_2()
374 {
375     // function
376     {
377         std::function<int (int, int)> r1(f_int_2);
378         int i = 2;
379         int j = 3;
380         assert(r1(i, j) == i+j);
381     }
382     // function pointer
383     {
384         int (*fp)(int, int) = f_int_2;
385         std::function<int (int, int)> r1(fp);
386         int i = 3;
387         int j = 4;
388         assert(r1(i, j) == i+j);
389     }
390     // functor
391     {
392         A_int_2 a0;
393         std::function<int (int, int)> r1(a0);
394         int i = 4;
395         int j = 5;
396         assert(r1(i, j) == i+j);
397     }
398     // member function pointer
399     {
400         int(A_int_2::*fp)(int) = &A_int_2::mem1;
401         std::function<int (A_int_2, int)> r1(fp);
402         A_int_2 a;
403         int i = 3;
404         assert(r1(a, i) == i+1);
405         std::function<int (A_int_2*, int)> r2(fp);
406         A_int_2* ap = &a;
407         assert(r2(ap, i) == i+1);
408     }
409     // const member function pointer
410     {
411         int (A_int_2::*fp)(int) const = &A_int_2::mem2;
412         std::function<int (A_int_2, int)> r1(fp);
413         A_int_2 a;
414         int i = 4;
415         assert(r1(a, i) == i+2);
416         std::function<int (A_int_2*, int)> r2(fp);
417         A_int_2* ap = &a;
418         assert(r2(ap, i) == i+2);
419     }
420 }
421 
main(int,char **)422 int main(int, char**)
423 {
424     test_void_0();
425     test_int_0();
426     test_void_1();
427     test_int_1();
428     test_void_2();
429     test_int_2();
430 
431   return 0;
432 }
433