]>
Commit | Line | Data |
---|---|---|
1 | // (C) Copyright John Maddock 2014-9. | |
2 | // (C) Copyright Andrey Semashev 2017. | |
3 | // Use, modification and distribution are subject to the | |
4 | // Boost Software License, Version 1.0. (See accompanying file | |
5 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | ||
7 | #include <boost/config.hpp> | |
8 | ||
9 | int test_fallthrough(int n) | |
10 | { | |
11 | switch (n) | |
12 | { | |
13 | case 0: | |
14 | n++; | |
15 | BOOST_FALLTHROUGH; | |
16 | case 1: | |
17 | n++; | |
18 | break; | |
19 | } | |
20 | return n; | |
21 | } | |
22 | ||
23 | int test_unreachable(int i) | |
24 | { | |
25 | if(BOOST_LIKELY(i)) return i; | |
26 | ||
27 | throw i; | |
28 | BOOST_UNREACHABLE_RETURN(0) // NOTE: no semicolon afterwards!! | |
29 | } | |
30 | ||
31 | BOOST_FORCEINLINE int always_inline(int i){ return ++i; } | |
32 | BOOST_NOINLINE int never_inline(int i){ return ++i; } | |
33 | ||
34 | BOOST_NORETURN void always_throw() | |
35 | { | |
36 | throw 0; | |
37 | } | |
38 | ||
39 | struct BOOST_MAY_ALIAS aliasing_struct {}; | |
40 | typedef unsigned int BOOST_MAY_ALIAS aliasing_uint; | |
41 | ||
42 | struct BOOST_ATTRIBUTE_NODISCARD nodiscard_struct {}; | |
43 | ||
44 | BOOST_ATTRIBUTE_NODISCARD int nodiscard_proc(int i) | |
45 | { | |
46 | return i * i; | |
47 | } | |
48 | ||
49 | ||
50 | #define test_fallthrough(x) foobar(x) | |
51 | ||
52 | struct empty {}; | |
53 | struct no_unique | |
54 | { | |
55 | int a; | |
56 | BOOST_ATTRIBUTE_NO_UNIQUE_ADDRESS empty b; | |
57 | }; | |
58 | ||
59 | template <bool b> | |
60 | struct trait | |
61 | { | |
62 | enum { value = b }; | |
63 | }; | |
64 | ||
65 | ||
66 | int main() | |
67 | { | |
68 | typedef int unused_type BOOST_ATTRIBUTE_UNUSED; | |
69 | try | |
70 | { | |
71 | int result = test_fallthrough BOOST_PREVENT_MACRO_SUBSTITUTION(0); | |
72 | BOOST_STATIC_CONSTANT(bool, value = 0); | |
73 | result += test_unreachable(1); | |
74 | result += always_inline(2); | |
75 | result += never_inline(3); | |
76 | if(BOOST_UNLIKELY(!result)) | |
77 | always_throw(); | |
78 | nodiscard_struct s; | |
79 | no_unique no_un; | |
80 | ||
81 | BOOST_IF_CONSTEXPR(trait<true>::value) | |
82 | { | |
83 | result += 2; | |
84 | } | |
85 | } | |
86 | catch(int) | |
87 | { | |
88 | return 1; | |
89 | } | |
90 | return 0; | |
91 | } | |
92 |