blob: 49c7acc2d3261ba7c40b70c1cd98f0837415cc6d [file] [log] [blame]
Richard Smith83e37bee2013-06-26 23:16:51 +00001// RUN: %clang_cc1 -fsyntax-only -verify -std=c11 -Wno-unused %s
2
3int f(int, int);
4
5typedef struct A {
6 int x, y;
7} A;
8
9void test() {
10 int a;
11 int xs[10];
12 a + ++a; // expected-warning {{unsequenced modification and access to 'a'}}
13 a = ++a; // expected-warning {{multiple unsequenced modifications to 'a'}}
14 a + a++; // expected-warning {{unsequenced modification and access to 'a'}}
15 a = a++; // expected-warning {{multiple unsequenced modifications to 'a'}}
16 (a++, a++); // ok
17 ++a + ++a; // expected-warning {{multiple unsequenced modifications}}
18 a++ + a++; // expected-warning {{multiple unsequenced modifications}}
19 a = xs[++a]; // expected-warning {{multiple unsequenced modifications}}
20 a = xs[a++]; // expected-warning {{multiple unsequenced modifications}}
21 a = (++a, ++a); // expected-warning {{multiple unsequenced modifications}}
22 a = (a++, ++a); // expected-warning {{multiple unsequenced modifications}}
23 a = (a++, a++); // expected-warning {{multiple unsequenced modifications}}
24 f(a, a); // ok
25 f(a = 0, a); // expected-warning {{unsequenced modification and access}}
26 f(a, a += 0); // expected-warning {{unsequenced modification and access}}
27 f(a = 0, a = 0); // expected-warning {{multiple unsequenced modifications}}
28
29 a = ++a; // expected-warning {{multiple unsequenced modifications}}
30 a += ++a; // expected-warning {{unsequenced modification and access}}
31
32 A agg1 = { a++, a++ }; // expected-warning {{multiple unsequenced modifications}}
33 A agg2 = { a++ + a, a++ }; // expected-warning {{unsequenced modification and access}}
34
35 (xs[2] && (a = 0)) + a; // ok
36 (0 && (a = 0)) + a; // ok
37 (1 && (a = 0)) + a; // expected-warning {{unsequenced modification and access}}
38
39 (xs[3] || (a = 0)) + a; // ok
40 (0 || (a = 0)) + a; // expected-warning {{unsequenced modification and access}}
41 (1 || (a = 0)) + a; // ok
42
43 (xs[4] ? a : ++a) + a; // ok
44 (0 ? a : ++a) + a; // expected-warning {{unsequenced modification and access}}
45 (1 ? a : ++a) + a; // ok
46 (xs[5] ? ++a : ++a) + a; // FIXME: warn here
47
48 (++a, xs[6] ? ++a : 0) + a; // FIXME: warn here
49
50 // Here, the read of the fourth 'a' might happen before or after the write to
51 // the second 'a'.
52 a += (a++, a) + a; // expected-warning {{unsequenced modification and access}}
53
54 int *p = xs;
55 a = *(a++, p); // ok
56 a = a++ && a; // ok
57
58 A *q = &agg1;
59 (q = &agg2)->y = q->x; // expected-warning {{unsequenced modification and access to 'q'}}
60
61 // This has undefined behavior if a == 0; otherwise, the side-effect of the
62 // increment is sequenced before the value computation of 'f(a, a)', which is
63 // sequenced before the value computation of the '&&', which is sequenced
64 // before the assignment. We treat the sequencing in '&&' as being
65 // unconditional.
66 a = a++ && f(a, a);
67
68 // This has undefined behavior if a != 0. FIXME: We should diagnose this.
69 (a && a++) + a;
70
71 (xs[7] && ++a) * (!xs[7] && ++a); // ok
72
73 xs[0] = (a = 1, a); // ok
74
75 xs[8] ? ++a + a++ : 0; // expected-warning {{multiple unsequenced modifications}}
76 xs[8] ? 0 : ++a + a++; // expected-warning {{multiple unsequenced modifications}}
77 xs[8] ? ++a : a++; // ok
78
79 xs[8] && (++a + a++); // expected-warning {{multiple unsequenced modifications}}
80 xs[8] || (++a + a++); // expected-warning {{multiple unsequenced modifications}}
81
82 (__builtin_classify_type(++a) ? 1 : 0) + ++a; // ok
83 (__builtin_constant_p(++a) ? 1 : 0) + ++a; // ok
84 (__builtin_expect(++a, 0) ? 1 : 0) + ++a; // FIXME: warn here
85}