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 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 117 namespace templates { 118 119 template <typename T> 120 struct Bar { 121 T get() { return 0; } 122 }; 123 124 template <typename X> 125 struct Foo { 126 int Run(); 127 Bar<int> bar; 128 }; 129 130 enum E {e1, e2}; 131 bool operator&&(E, E); 132 133 void foo(int, int); 134 135 template <typename X> 136 int Foo<X>::Run() { 137 char num = 0; 138 139 // Before instantiation, Clang may consider the builtin operator here as 140 // unresolved function calls, and treat the arguments as unordered when 141 // the builtin operator evaluatation is well-ordered. Waiting until 142 // instantiation to check these expressions will prevent false positives. 143 if ((num = bar.get()) < 5 && num < 10) { } 144 if ((num = bar.get()) < 5 || num < 10) { } 145 if (static_cast<E>((num = bar.get()) < 5) || static_cast<E>(num < 10)) { } 146 147 if (static_cast<E>((num = bar.get()) < 5) && static_cast<E>(num < 10)) { } 148 // expected-warning@-1 {{unsequenced modification and access to 'num'}} 149 150 foo(num++, num++); 151 // expected-warning@-1 2{{multiple unsequenced modifications to 'num'}} 152 return 1; 153 } 154 155 int x = Foo<int>().Run(); 156 // expected-note@-1 {{in instantiation of member function 'templates::Foo<int>::Run'}} 157 158 159 template <typename T> 160 int Run2() { 161 T t = static_cast<T>(0); 162 return (t = static_cast<T>(1)) && t; 163 // expected-warning@-1 {{unsequenced modification and access to 't'}} 164 } 165 166 int y = Run2<bool>(); 167 int z = Run2<E>(); 168 // expected-note@-1{{in instantiation of function template specialization 'templates::Run2<templates::E>' requested here}} 169 170 } 171