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 p[(long long unsigned)(p = 0)]; // expected-warning {{unsequenced modification and access to 'p'}} 85 86 A *q = &agg1; 87 (q = &agg2)->y = q->x; // expected-warning {{unsequenced modification and access to 'q'}} 88 89 // This has undefined behavior if a == 0; otherwise, the side-effect of the 90 // increment is sequenced before the value computation of 'f(a, a)', which is 91 // sequenced before the value computation of the '&&', which is sequenced 92 // before the assignment. We treat the sequencing in '&&' as being 93 // unconditional. 94 a = a++ && f(a, a); 95 96 // This has undefined behavior if a != 0. FIXME: We should diagnose this. 97 (a && a++) + a; 98 99 (xs[7] && ++a) * (!xs[7] && ++a); // ok 100 101 xs[0] = (a = 1, a); // ok 102 (a -= 128) &= 128; // ok 103 ++a += 1; // ok 104 105 xs[8] ? ++a + a++ : 0; // expected-warning {{multiple unsequenced modifications}} 106 xs[8] ? 0 : ++a + a++; // expected-warning {{multiple unsequenced modifications}} 107 xs[8] ? ++a : a++; // ok 108 109 xs[8] && (++a + a++); // expected-warning {{multiple unsequenced modifications}} 110 xs[8] || (++a + a++); // expected-warning {{multiple unsequenced modifications}} 111 112 (__builtin_classify_type(++a) ? 1 : 0) + ++a; // ok 113 (__builtin_constant_p(++a) ? 1 : 0) + ++a; // ok 114 (__builtin_object_size(&(++a, a), 0) ? 1 : 0) + ++a; // ok 115 (__builtin_expect(++a, 0) ? 1 : 0) + ++a; // expected-warning {{multiple unsequenced modifications}} 116 } 117 118 namespace templates { 119 120 template <typename T> 121 struct Bar { 122 T get() { return 0; } 123 }; 124 125 template <typename X> 126 struct Foo { 127 int Run(); 128 Bar<int> bar; 129 }; 130 131 enum E {e1, e2}; 132 bool operator&&(E, E); 133 134 void foo(int, int); 135 136 template <typename X> 137 int Foo<X>::Run() { 138 char num = 0; 139 140 // Before instantiation, Clang may consider the builtin operator here as 141 // unresolved function calls, and treat the arguments as unordered when 142 // the builtin operator evaluatation is well-ordered. Waiting until 143 // instantiation to check these expressions will prevent false positives. 144 if ((num = bar.get()) < 5 && num < 10) { } 145 if ((num = bar.get()) < 5 || num < 10) { } 146 if (static_cast<E>((num = bar.get()) < 5) || static_cast<E>(num < 10)) { } 147 148 if (static_cast<E>((num = bar.get()) < 5) && static_cast<E>(num < 10)) { } 149 // expected-warning@-1 {{unsequenced modification and access to 'num'}} 150 151 foo(num++, num++); 152 // expected-warning@-1 2{{multiple unsequenced modifications to 'num'}} 153 return 1; 154 } 155 156 int x = Foo<int>().Run(); 157 // expected-note@-1 {{in instantiation of member function 'templates::Foo<int>::Run'}} 158 159 160 template <typename T> 161 int Run2() { 162 T t = static_cast<T>(0); 163 return (t = static_cast<T>(1)) && t; 164 // expected-warning@-1 {{unsequenced modification and access to 't'}} 165 } 166 167 int y = Run2<bool>(); 168 int z = Run2<E>(); 169 // expected-note@-1{{in instantiation of function template specialization 'templates::Run2<templates::E>' requested here}} 170 171 } 172