================
@@ -0,0 +1,59 @@
+// RUN: %clang_cc1 -std=c2y -verify %s
+
+bool test_if() {
+ if (true) {}
+ if (bool x = true; x) {}
+ if (bool x = false) return x;
+ if ([[maybe_unused]] bool x = true) {}
+ if (bool x [[maybe_unused]] = true) {}
+ if ([[maybe_unused]] int x = 3; x > 0) {}
+ return false;
+}
+
+int test_switch() {
+ int y = 1;
+ switch (y) {}
+
+ switch (int x = 1; x) {
+ default:
+ y += x;
+ }
+
+ switch (int x [[maybe_unused]] = 1) {}
+ switch ([[maybe_unused]] int x = 1) {}
+
+ switch (int x = 1) {
+ default:
+ return y + x;
+ }
+}
+
+bool negative_test_if() {
+ if (true; true) {} /* expected-error {{first clause in condition must be a
declaration}}
+ expected-warning {{expression result unused}}*/
+ if (true; ) {} /* expected-error {{first clause in condition must be a
declaration}}
+ expected-error {{expected expression}}
+ expected-warning {{expression result unused}} */
+ if (bool x = true; bool y = x) return y; /* expected-error {{expected
expression}}
+ expected-error {{use of
undeclared identifier 'y'}} */
+ if (true; bool y = true) return y; /* expected-error {{first clause in
condition must be a declaration}}
+ expected-error {{expected expression}}
+ expected-error {{use of undeclared
identifier 'y'}}
+ expected-warning {{expression result
unused}}*/
+ return false;
+}
----------------
bassiounix wrote:
You swapped the wording of C and C++.
This is taken from the mentioned paper directly:
> The feature standardized here differs slightly from the C++ feature by not
> including the C++ grammatical construct _condition_, which allows the second
> clause of if and for to be a second, completely separate declaration:
>
> ```cc
>if (int x = 0; int y = x + 1) {
> y;
>}
>```
> **Therefore, the second clause to if is limited to being an expression only
> in this proposal.**
Also note the grammar rule for `selection-header`, it clearly states an
**expression** after a declaration
<blockquote>
<p><em>simple-declaration:</em><br>
<em>attribute-specifier-sequence</em><sub>opt</sub>
<em>declaration-specifiers</em> <em>declarator</em> <code>=</code>
<em>initializer</em></p>
<p><em>selection-statement:</em><br>
<code>if</code> <code>(</code> <strong><em>selection-header</em></strong>
<code>)</code> <em>secondary-block</em><br>
<code>if</code> <code>(</code> <strong><em>selection-header</em></strong>
<code>)</code> <em>secondary-block</em> <code>else</code>
<em>secondary-block</em><br>
<code>switch</code> <code>(</code> <strong><em>selection-header</em></strong>
<code>)</code> <em>secondary-block</em></p>
<p><strong><em>selection-header:</em></strong><br>
<strong><em>expression</em></strong><br>
<strong><em>declaration</em>
<em>expression</em></strong><br>
<strong><em>simple-declaration</em></strong></p>
</blockquote>
GCC doesn't agree with C mode: https://godbolt.org/z/eqx1T6Kr7
https://github.com/llvm/llvm-project/pull/198244
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits