aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/external/metalang99/tests/stmt.c
blob: cc00b1b5077474db9751e0fdf0c8bdb9d8e99220 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <metalang99/stmt.h>

#include <assert.h>

int main(void) {

    // ML99_INTRODUCE_VAR_TO_STMT
    {
        if (1)
            ML99_INTRODUCE_VAR_TO_STMT(int x = 5, y = 7) {
                assert(5 == x);
                assert(7 == y);
            }
    }

    // ML99_INTRODUCE_NON_NULL_PTR_TO_STMT
    {
        int x = 5, y = 7;

        // clang-format off
        if (1)
            ML99_INTRODUCE_NON_NULL_PTR_TO_STMT(int, x_ptr, &x)
                ML99_INTRODUCE_NON_NULL_PTR_TO_STMT(int, y_ptr, &y) {
                    assert(x == *x_ptr);
                    assert(y == *y_ptr);
                }
        // clang-format on
    }

    // ML99_CHAIN_EXPR_STMT
    {
        int x, y;

        // clang-format off
        if (1)
            ML99_CHAIN_EXPR_STMT(x = 1)
                ML99_CHAIN_EXPR_STMT(y = 2) {
                    assert(1 == x);
                    assert(2 == y);
                }
        // clang-format on

        // Test -Wunused suppression via ML99_CHAIN_EXPR_STMT.
        int z;

        if (1)
            ML99_CHAIN_EXPR_STMT((void)z);
    }

    // ML99_CHAIN_EXPR_STMT_AFTER
    {
        int x = 5, y = 7;

        if (1) {
            assert(5 == x);
            assert(7 == y);

            ML99_CHAIN_EXPR_STMT_AFTER(x = 1) {
                assert(5 == x);
                assert(7 == y);

                ML99_CHAIN_EXPR_STMT_AFTER(y = 2) {
                    assert(5 == x);
                    assert(7 == y);
                }

                assert(5 == x);
                assert(2 == y);
            }

            assert(1 == x);
            assert(2 == y);
        }
    }

    // ML99_SUPPRESS_UNUSED_BEFORE_STMT
    {
        int x, y;

        // clang-format off
        if (1)
            ML99_SUPPRESS_UNUSED_BEFORE_STMT(x)
                ML99_SUPPRESS_UNUSED_BEFORE_STMT(y)
                    ;
        // clang-format on
    }

    // Clang-Format breaks with the following sequence of statements so they are put into a macro.
    // clang-format off

#define STMT_CHAINING \
    ML99_INTRODUCE_VAR_TO_STMT(int x = 5) \
        ML99_INTRODUCE_NON_NULL_PTR_TO_STMT(int, x_ptr, &x) { \
            assert(x == *x_ptr); \
 \
            ML99_CHAIN_EXPR_STMT(x = 7) \
                ML99_INTRODUCE_VAR_TO_STMT(int y = 5) \
                    ML99_SUPPRESS_UNUSED_BEFORE_STMT(y) { \
                        ML99_CHAIN_EXPR_STMT_AFTER(x = 123) { \
                            assert(7 == x); \
                        } \
 \
                        assert(123 == x); \
                    } \
        }

    // clang-format on

    { STMT_CHAINING }

#undef STMT_CHAINING

    return 0;
}