xref: /llvm-project/clang/test/CXX/stmt.stmt/stmt.select/p3.cpp (revision 83ea47acd7116bf50274534ba9b3bd3035c01da6)
1 // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify %std_cxx98-14 %s
2 // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -Wc++14-compat -verify %std_cxx17- %s -DCPP17
3 
4 int f();
5 
g()6 void g() {
7   if (int x = f()) { // expected-note 2{{previous definition}}
8     int x; // expected-error{{redefinition of 'x'}}
9   } else {
10     int x; // expected-error{{redefinition of 'x'}}
11   }
12 }
13 
h()14 void h() {
15   if (int x = f()) // expected-note 2{{previous definition}}
16     int x; // expected-error{{redefinition of 'x'}}
17   else
18     int x; // expected-error{{redefinition of 'x'}}
19 }
20 
ifInitStatement()21 void ifInitStatement() {
22   int Var = 0;
23 
24   if (int I = 0; true) {}
25   if (Var + Var; true) {}
26   if (; true) {}
27 #ifdef CPP17
28   // expected-warning@-4 {{if initialization statements are incompatible with C++ standards before C++17}}
29   // expected-warning@-4 {{if initialization statements are incompatible with C++ standards before C++17}}
30   // expected-warning@-4 {{if initialization statements are incompatible with C++ standards before C++17}}
31 #else
32   // expected-warning@-8 {{'if' initialization statements are a C++17 extension}}
33   // expected-warning@-8 {{'if' initialization statements are a C++17 extension}}
34   // expected-warning@-8 {{'if' initialization statements are a C++17 extension}}
35 #endif
36 }
37 
switchInitStatement()38 void switchInitStatement() {
39   int Var = 0;
40 
41   switch (int I = 0; Var) {}
42   switch (Var + Var; Var) {}
43   switch (; Var) {}
44 #ifdef CPP17
45   // expected-warning@-4 {{switch initialization statements are incompatible with C++ standards before C++17}}
46   // expected-warning@-4 {{switch initialization statements are incompatible with C++ standards before C++17}}
47   // expected-warning@-4 {{switch initialization statements are incompatible with C++ standards before C++17}}
48 #else
49   // expected-warning@-8 {{'switch' initialization statements are a C++17 extension}}
50   // expected-warning@-8 {{'switch' initialization statements are a C++17 extension}}
51   // expected-warning@-8 {{'switch' initialization statements are a C++17 extension}}
52 #endif
53 }
54 
55 // TODO: Better diagnostics for while init statements.
whileInitStatement()56 void whileInitStatement() {
57   while (int I = 10; I--); // expected-error {{expected ')'}}
58   // expected-note@-1 {{to match this '('}}
59   // expected-error@-2 {{use of undeclared identifier 'I'}}
60 
61   int Var = 10;
62   while (Var + Var; Var--) {} // expected-error {{expected ')'}}
63   // expected-note@-1 {{to match this '('}}
64   // expected-error@-2 {{expected ';' after expression}}
65   // expected-error@-3 {{expected expression}}
66 }
67 
68 // TODO: This is needed because clang can't seem to diagnose invalid syntax after the
69 // last loop above. It would be nice to remove this.
whileInitStatement2()70 void whileInitStatement2() {
71   while (; false) {} // expected-error {{expected expression}}
72   // expected-error@-1 {{expected ';' after expression}}
73   // expected-error@-2 {{expected expression}}
74 }
75