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