[C2y] Claim conformance to WG14 N3505

This paper clarified that conditional inclusion in the preprocessor
requires the integer constant expression to be comprised only of
integer and character literals, punctuators, or implementation-defined
tokens.

This is something Clang has always required.
This commit is contained in:
Aaron Ballman
2025-03-11 15:02:51 -04:00
parent 381599f1fe
commit 456fa47bab
2 changed files with 33 additions and 1 deletions

32
clang/test/C/C2y/n3505.c Normal file
View File

@@ -0,0 +1,32 @@
// RUN: %clang_cc1 -verify -std=c2y -Wall -pedantic %s
// RUN: %clang_cc1 -verify -std=c23 -Wall -pedantic %s
// RUN: %clang_cc1 -verify -std=c11 -Wall -pedantic %s
// RUN: %clang_cc1 -verify -std=c99 -Wall -pedantic %s
/* WG14 N3505: Yes
* Preprocessor integer expressions, v. 2
*
* This introduces a constraint that preprocessing tokens must be an integer
* literal, character literal, punctuator, or some other implementation-defined
* sequence of tokens (to support builtins that insert odd tokens into the
* parsing stream).
*/
// This is technically an integer constant expression, but it does not match
// the new constraints and thus needs to be diagnosed.
#if 1 ? 1 : (""[0] += 5) // expected-error {{invalid token at start of a preprocessor expression}}
#endif
// But with a character literal, it is fine.
#if 1 ? 1 : ('a' + 5) // Ok
#endif
// This doesn't mean that all punctuators are fine, however.
#if 1 ? 1 : ('a' += 5) // expected-error {{token is not a valid binary operator in a preprocessor subexpression}}
#endif
// But some are.
#if 1 ? 1 : ~('a') // Ok
#endif
int x; // Needs a declaration to avoid a pedantic warning

View File

@@ -268,7 +268,7 @@ conformance.</p>
<tr>
<td>Preprocessor integer expressions, v. 2</td>
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3505.pdf">N3505</a></td>
<td class="unknown" align="center">Unknown</td>
<td class="full" align="center">Yes</td>
</tr>
<tr>
<td>Initialization of anonymous structures and unions</td>