Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -std=c++1z -verify %s
      2 
      3 void testIf() {
      4   int x = 0;
      5   if (x; x) ++x;
      6   if (int t = 0; t) ++t; else --t;
      7 
      8   if (int x, y = 0; y) // expected-note 2 {{previous definition is here}}
      9     int x = 0; // expected-error {{redefinition of 'x'}}
     10   else
     11     int x = 0; // expected-error {{redefinition of 'x'}}
     12 
     13   if (x; int a = 0) ++a;
     14   if (x, +x; int a = 0) // expected-note 2 {{previous definition is here}} expected-warning {{unused}}
     15     int a = 0; // expected-error {{redefinition of 'a'}}
     16   else
     17     int a = 0; // expected-error {{redefinition of 'a'}}
     18 
     19   if (int b = 0; b)
     20     ;
     21   b = 2; // expected-error {{use of undeclared identifier}}
     22 }
     23 
     24 void testSwitch() {
     25   int x = 0;
     26   switch (x; x) {
     27     case 1:
     28       ++x;
     29   }
     30 
     31   switch (int x, y = 0; y) {
     32     case 1:
     33       ++x;
     34     default:
     35       ++y;
     36   }
     37 
     38   switch (int x, y = 0; y) { // expected-note 2 {{previous definition is here}}
     39     case 0:
     40       int x = 0; // expected-error {{redefinition of 'x'}}
     41     case 1:
     42       int y = 0; // expected-error {{redefinition of 'y'}}
     43   };
     44 
     45   switch (x; int a = 0) {
     46     case 0:
     47       ++a;
     48   }
     49 
     50   switch (x, +x; int a = 0) { // expected-note {{previous definition is here}} expected-warning {{unused}}
     51     case 0:
     52       int a = 0; // expected-error {{redefinition of 'a'}} // expected-note {{previous definition is here}}
     53     case 1:
     54       int a = 0; // expected-error {{redefinition of 'a'}}
     55   }
     56 
     57   switch (int b = 0; b) {
     58     case 0:
     59       break;
     60   }
     61   b = 2; // expected-error {{use of undeclared identifier}}
     62 }
     63 
     64 constexpr bool constexpr_if_init(int n) {
     65   if (int a = n; ++a > 0)
     66     return true;
     67   else
     68     return false;
     69 }
     70 
     71 constexpr int constexpr_switch_init(int n) {
     72   switch (int p = n + 2; p) {
     73     case 0:
     74       return 0;
     75     case 1:
     76       return 1;
     77     default:
     78       return -1;
     79   }
     80 }
     81 
     82 void test_constexpr_init_stmt() {
     83   constexpr bool a = constexpr_if_init(-2);
     84   static_assert(!a, "");
     85   static_assert(constexpr_if_init(1), "");
     86 
     87   constexpr int b = constexpr_switch_init(-1);
     88   static_assert(b == 1, "");
     89   static_assert(constexpr_switch_init(-2) == 0, "");
     90   static_assert(constexpr_switch_init(-5) == -1, "");
     91 }
     92