aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/validation/integer-const-expr.c
blob: f41aa806ab4727d485c2f68d306bb9661aafe6e0 (plain)
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
extern void *malloc(unsigned long);

static inline __attribute__((__const__)) unsigned squarec(unsigned n)
{
        return n*n;
}

static inline unsigned square(unsigned n)
{
        return n*n;
}

static inline unsigned long long bignum(void)
{
        return 1000000000000ULL;
}

static inline __attribute__((__const__)) unsigned long long bignumc(void)
{
        return 1000000000000ULL;
}

// test if x is an integer constant expression [C99,C11 6.6p6]
#define ICE_P(x) \
    (__builtin_types_compatible_p(typeof(0?((void*)((long)(x)*0l)):(int*)1),int*))

#define CHX_P(X)	__builtin_choose_expr(ICE_P(X), 1, 0)
#define CST_P(X)	__builtin_constant_p(ICE_P(X))

#define TEST(R, X)	_Static_assert(ICE_P(X) == R, "ICE_P(" #X ") => " #R);	\
			_Static_assert(ICE_P(ICE_P(X)), "ICE_P2(" #X ")");	\
			_Static_assert(CHX_P(X) == R, "CHX_P(" #X ") => " #R);	\
			_Static_assert(CST_P(X) == 1, "CST_P(" #X ")")

int main(int argc, char *argv[])
{
        char fla[3];
        char vla[argc++];
        char **p, **q;
        int x = 5, y = 8;
        void *v;

        p = &argv[3];
        q = &argv[6];

        TEST(1, 4);
        TEST(1, sizeof(long));
        TEST(1, 5ull - 3u);
        TEST(1, 3.2);
        TEST(1, sizeof(fla));

        TEST(0, square(2));
        TEST(0, square(argc));
        TEST(0, squarec(2));
        TEST(0, squarec(argc));
        TEST(0, 1+argc-argc);
        TEST(0, 1+argc+argc+1-argc-argc);
        TEST(0, bignum() - 1);
        TEST(0, 0*bignum());
        TEST(0, 0*bignumc());
        TEST(0, sizeof(vla));
        TEST(0, p);
        TEST(0, p < q);
        TEST(0, p++);
        TEST(0, main);
        TEST(0, malloc(8));
        TEST(0, v = malloc(8));
        TEST(0, v);
        TEST(0, x++);
        TEST(0, y++);
        TEST(0, (3, 2, 1));
        TEST(0, ({x++; 0; }));
        TEST(0, ({square(y--); 0; }));
        TEST(0, (square(x), 3));
        TEST(0, (squarec(x), 3));
        TEST(0, ({squarec(x); 3;}));
        TEST(0, ({squarec(x);}));

        return 0;
}

/*
 * check-name: integer-const-expr
 * check-command: sparse -Wno-vla $file
 */