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