Clang Project

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