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