Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CXX / stmt.stmt / stmt.select / stmt.if / p4.cpp
blob8d43be6fc9047811a21d17e0b112a1eabf208d3f
1 // RUN: %clang_cc1 -std=c++23 -verify %s
3 void test_consteval() {
4 if consteval ({(void)1;}); // expected-error {{expected { after consteval}}
5 if consteval (void) 0; // expected-error {{expected { after consteval}}
6 if consteval {
7 (void)0;
8 } else (void)0; // expected-error {{expected { after else}}
10 static_assert([] {
11 if consteval {
12 return 0;
14 return 1;
15 }() == 0);
17 static_assert([] {
18 if consteval {
19 return 0;
20 } else {
21 return 1;
23 }() == 0);
25 static_assert([] {
26 if !consteval {
27 return 0;
28 } else {
29 return 1;
31 }() == 1);
33 static_assert([] {
34 if not consteval {
35 return 0;
37 return 1;
38 }() == 1);
40 if consteval [[likely]] { // expected-warning {{attribute 'likely' has no effect when annotating an 'if consteval' statement}}\
41 // expected-note 2{{annotating the 'if consteval' statement here}}
45 else [[unlikely]] { // expected-warning {{attribute 'unlikely' has no effect when annotating an 'if consteval' statement}}
51 void test_consteval_jumps() {
52 if consteval { // expected-note 4{{jump enters controlled statement of consteval if}}
53 goto a;
54 goto b; // expected-error {{cannot jump from this goto statement to its label}}
55 a:;
56 } else {
57 goto b;
58 goto a; // expected-error {{cannot jump from this goto statement to its label}}
59 b:;
61 goto a; // expected-error {{cannot jump from this goto statement to its label}}
62 goto b; // expected-error {{cannot jump from this goto statement to its label}}
65 void test_consteval_switch() {
66 int x = 42;
67 switch (x) {
68 if consteval { // expected-note 2{{jump enters controlled statement of consteval if}}
69 case 1:; // expected-error {{cannot jump from switch statement to this case label}}
70 default:; // expected-error {{cannot jump from switch statement to this case label}}
71 } else {
74 switch (x) {
75 if consteval { // expected-note 2{{jump enters controlled statement of consteval if}}
76 } else {
77 case 2:; // expected-error {{cannot jump from switch statement to this case label}}
78 default:; // expected-error {{cannot jump from switch statement to this case label}}
83 consteval int f(int i) { return i; }
84 constexpr int g(int i) {
85 if consteval {
86 return f(i);
87 } else {
88 return 42;
91 static_assert(g(10) == 10);
93 constexpr int h(int i) { // expected-note {{declared here}}
94 if !consteval {
95 return f(i); // expected-error {{call to consteval function 'f' is not a constant expression}}\
96 // expected-note {{cannot be used in a constant expression}}
98 return 0;
101 consteval void warn_in_consteval() {
102 if consteval { // expected-warning {{consteval if is always true in an immediate context}}
103 if consteval {} // expected-warning {{consteval if is always true in an immediate context}}
107 constexpr void warn_in_consteval2() {
108 if consteval {
109 if consteval {} // expected-warning {{consteval if is always true in an immediate context}}
113 auto y = []() consteval {
114 if consteval { // expected-warning {{consteval if is always true in an immediate context}}
115 if consteval {} // expected-warning {{consteval if is always true in an immediate context}}
119 namespace test_transform {
120 int f(auto n) {
121 if consteval {
122 n.foo; //expected-error {{no member named}}
124 else {
127 if !consteval {
128 n.foo; //expected-error {{no member named}}
130 else {
133 return 0;
136 constexpr int g(auto n) {
137 if consteval {
139 else {
140 n.foo; //expected-error {{no member named}}
143 if !consteval {
145 else {
146 n.foo; //expected-error {{no member named}}
149 return 0;
152 struct S {};
153 void test() {
154 f(S{}); //expected-note {{in instantiation}}
155 g(S{}); //expected-note {{in instantiation}}