xref: /llvm-project/clang/test/C/C99/block-scopes.c (revision 5901d4005f015a46185ddc080038c1a3db3fa2c7)
1edfa97a8SAaron Ballman // RUN: %clang_cc1 -std=c89 -verify %s
2edfa97a8SAaron Ballman // RUN: %clang_cc1 -std=c99 -verify %s
3edfa97a8SAaron Ballman // RUN: %clang_cc1 -std=c11 -verify %s
4edfa97a8SAaron Ballman // RUN: %clang_cc1 -std=c17 -verify %s
5edfa97a8SAaron Ballman // RUN: %clang_cc1 -std=c23 -verify %s
6edfa97a8SAaron Ballman 
7edfa97a8SAaron Ballman // expected-no-diagnostics
8edfa97a8SAaron Ballman 
9edfa97a8SAaron Ballman /* WG14 ???: yes
10edfa97a8SAaron Ballman  * new block scopes for selection and iteration statements
11edfa97a8SAaron Ballman  *
12edfa97a8SAaron Ballman  * This is referenced in the C99 front matter as new changes to C99, but it is
13edfa97a8SAaron Ballman  * not clear which document number introduced the changes. It's possible this
14edfa97a8SAaron Ballman  * is WG14 N759, based on discussion in the C99 rationale document that claims
15edfa97a8SAaron Ballman  * these changes were made in response to surprising issues with the lifetime
16edfa97a8SAaron Ballman  * of compound literals in compound statements vs non-compound statements.
17edfa97a8SAaron Ballman  */
18edfa97a8SAaron Ballman 
19edfa97a8SAaron Ballman enum {a, b};
different(void)20edfa97a8SAaron Ballman void different(void) {
21*5901d400SAaron Ballman   if (sizeof(enum {b, a}) != sizeof(int)) {
22edfa97a8SAaron Ballman     _Static_assert(a == 1, "");
23*5901d400SAaron Ballman   }
24edfa97a8SAaron Ballman   /* In C89, the 'b' found here would have been from the enum declaration in
25edfa97a8SAaron Ballman    * the controlling expression of the selection statement, not from the global
26edfa97a8SAaron Ballman    * declaration. In C99 and later, that enumeration is scoped to the 'if'
27edfa97a8SAaron Ballman    * statement and the global declaration is what's found.
28edfa97a8SAaron Ballman    */
29edfa97a8SAaron Ballman   #if __STDC_VERSION__ >= 199901L
30edfa97a8SAaron Ballman     _Static_assert(b == 1, "");
31edfa97a8SAaron Ballman   #else
32edfa97a8SAaron Ballman     _Static_assert(b == 0, "");
33edfa97a8SAaron Ballman   #endif
34edfa97a8SAaron Ballman }
35edfa97a8SAaron Ballman 
36