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 
test_fallthrough(int n)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 
test_unreachable(int i)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 
always_inline(int i)31 BOOST_FORCEINLINE int always_inline(int i){ return ++i; }
never_inline(int i)32 BOOST_NOINLINE int never_inline(int i){ return ++i; }
33 
always_throw()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 
45 #define test_fallthrough(x) foobar(x)
46 
47 struct empty {};
48 struct no_unique
49 {
50    int a;
51    BOOST_ATTRIBUTE_NO_UNIQUE_ADDRESS empty b;
52 };
53 
54 template <bool b>
55 struct trait
56 {
57    enum { value = b };
58 };
59 
60 
main()61 int main()
62 {
63    typedef int unused_type BOOST_ATTRIBUTE_UNUSED;
64    try
65    {
66       int result = test_fallthrough BOOST_PREVENT_MACRO_SUBSTITUTION(0);
67       BOOST_STATIC_CONSTANT(bool, value = 0);
68       result += test_unreachable(1);
69       result += always_inline(2);
70       result += never_inline(3);
71       if(BOOST_UNLIKELY(!result))
72          always_throw();
73       nodiscard_struct s;
74       no_unique no_un;
75 
76       BOOST_IF_CONSTEXPR(trait<true>::value)
77       {
78          result += 2;
79       }
80    }
81    catch(int)
82    {
83       return 1;
84    }
85    return 0;
86 }
87 
88