1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wno-unused %s
2 
3 int f(int, int = 0);
4 
5 struct A {
6   int x, y;
7 };
8 struct S {
9   S(int, int);
10   int n;
11 };
12 
test()13 void test() {
14   int a;
15   int xs[10];
16   ++a = 0; // ok
17   a + ++a; // expected-warning {{unsequenced modification and access to 'a'}}
18   a = ++a; // ok
19   a + a++; // expected-warning {{unsequenced modification and access to 'a'}}
20   a = a++; // expected-warning {{multiple unsequenced modifications to 'a'}}
21   ++ ++a; // ok
22   (a++, a++); // ok
23   ++a + ++a; // expected-warning {{multiple unsequenced modifications to 'a'}}
24   a++ + a++; // expected-warning {{multiple unsequenced modifications}}
25   (a++, a) = 0; // ok, increment is sequenced before value computation of LHS
26   a = xs[++a]; // ok
27   a = xs[a++]; // expected-warning {{multiple unsequenced modifications}}
28   (a ? xs[0] : xs[1]) = ++a; // expected-warning {{unsequenced modification and access}}
29   a = (++a, ++a); // ok
30   a = (a++, ++a); // ok
31   a = (a++, a++); // expected-warning {{multiple unsequenced modifications}}
32   f(a, a); // ok
33   f(a = 0, a); // expected-warning {{unsequenced modification and access}}
34   f(a, a += 0); // expected-warning {{unsequenced modification and access}}
35   f(a = 0, a = 0); // expected-warning {{multiple unsequenced modifications}}
36   a = f(++a); // ok
37   a = f(a++); // ok
38   a = f(++a, a++); // expected-warning {{multiple unsequenced modifications}}
39 
40   // Compound assignment "A OP= B" is equivalent to "A = A OP B" except that A
41   // is evaluated only once.
42   (++a, a) = 1; // ok
43   (++a, a) += 1; // ok
44   a = ++a; // ok
45   a += ++a; // expected-warning {{unsequenced modification and access}}
46 
47   A agg1 = { a++, a++ }; // ok
48   A agg2 = { a++ + a, a++ }; // expected-warning {{unsequenced modification and access}}
49 
50   S str1(a++, a++); // expected-warning {{multiple unsequenced modifications}}
51   S str2 = { a++, a++ }; // ok
52   S str3 = { a++ + a, a++ }; // expected-warning {{unsequenced modification and access}}
53 
54   struct Z { A a; S s; } z = { { ++a, ++a }, { ++a, ++a } }; // ok
55   a = S { ++a, a++ }.n; // ok
56   A { ++a, a++ }.x; // ok
57   a = A { ++a, a++ }.x; // expected-warning {{unsequenced modifications}}
58   A { ++a, a++ }.x + A { ++a, a++ }.y; // expected-warning {{unsequenced modifications}}
59 
60   (xs[2] && (a = 0)) + a; // ok
61   (0 && (a = 0)) + a; // ok
62   (1 && (a = 0)) + a; // expected-warning {{unsequenced modification and access}}
63 
64   (xs[3] || (a = 0)) + a; // ok
65   (0 || (a = 0)) + a; // expected-warning {{unsequenced modification and access}}
66   (1 || (a = 0)) + a; // ok
67 
68   (xs[4] ? a : ++a) + a; // ok
69   (0 ? a : ++a) + a; // expected-warning {{unsequenced modification and access}}
70   (1 ? a : ++a) + a; // ok
71   (0 ? a : a++) + a; // expected-warning {{unsequenced modification and access}}
72   (1 ? a : a++) + a; // ok
73   (xs[5] ? ++a : ++a) + a; // FIXME: warn here
74 
75   (++a, xs[6] ? ++a : 0) + a; // expected-warning {{unsequenced modification and access}}
76 
77   // Here, the read of the fourth 'a' might happen before or after the write to
78   // the second 'a'.
79   a += (a++, a) + a; // expected-warning {{unsequenced modification and access}}
80 
81   int *p = xs;
82   a = *(a++, p); // ok
83   a = a++ && a; // ok
84 
85   A *q = &agg1;
86   (q = &agg2)->y = q->x; // expected-warning {{unsequenced modification and access to 'q'}}
87 
88   // This has undefined behavior if a == 0; otherwise, the side-effect of the
89   // increment is sequenced before the value computation of 'f(a, a)', which is
90   // sequenced before the value computation of the '&&', which is sequenced
91   // before the assignment. We treat the sequencing in '&&' as being
92   // unconditional.
93   a = a++ && f(a, a);
94 
95   // This has undefined behavior if a != 0. FIXME: We should diagnose this.
96   (a && a++) + a;
97 
98   (xs[7] && ++a) * (!xs[7] && ++a); // ok
99 
100   xs[0] = (a = 1, a); // ok
101   (a -= 128) &= 128; // ok
102   ++a += 1; // ok
103 
104   xs[8] ? ++a + a++ : 0; // expected-warning {{multiple unsequenced modifications}}
105   xs[8] ? 0 : ++a + a++; // expected-warning {{multiple unsequenced modifications}}
106   xs[8] ? ++a : a++; // ok
107 
108   xs[8] && (++a + a++); // expected-warning {{multiple unsequenced modifications}}
109   xs[8] || (++a + a++); // expected-warning {{multiple unsequenced modifications}}
110 
111   (__builtin_classify_type(++a) ? 1 : 0) + ++a; // ok
112   (__builtin_constant_p(++a) ? 1 : 0) + ++a; // ok
113   (__builtin_object_size(&(++a, a), 0) ? 1 : 0) + ++a; // ok
114   (__builtin_expect(++a, 0) ? 1 : 0) + ++a; // expected-warning {{multiple unsequenced modifications}}
115 }
116