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