Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wno-unused %s |
| 2 | |
Richard Smith | 0c0b390 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 3 | int f(int, int = 0); |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 4 | |
| 5 | struct A { |
| 6 | int x, y; |
| 7 | }; |
| 8 | struct S { |
| 9 | S(int, int); |
Richard Smith | 0c0b390 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 10 | int n; |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 11 | }; |
| 12 | |
| 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}} |
Richard Smith | 0c0b390 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 36 | a = f(++a); // ok |
| 37 | a = f(a++); // ok |
| 38 | a = f(++a, a++); // expected-warning {{multiple unsequenced modifications}} |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 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 | |
Richard Smith | 0c0b390 | 2013-06-30 10:40:20 +0000 | [diff] [blame] | 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 | |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 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 |
Richard Smith | 418dd3e | 2013-06-26 23:16:51 +0000 | [diff] [blame] | 71 | (0 ? a : a++) + a; // expected-warning {{unsequenced modification and access}} |
| 72 | (1 ? a : a++) + a; // ok |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 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 |
Richard Smith | 995e4a7 | 2013-01-17 22:06:26 +0000 | [diff] [blame] | 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}} |
Richard Smith | ba57183 | 2013-01-17 23:46:04 +0000 | [diff] [blame] | 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}} |
Richard Smith | 6c3af3d | 2013-01-17 01:17:56 +0000 | [diff] [blame] | 115 | } |