Skip to content

Commit 456fa47

Browse files
committed
[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.
1 parent 381599f commit 456fa47

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

clang/test/C/C2y/n3505.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %clang_cc1 -verify -std=c2y -Wall -pedantic %s
2+
// RUN: %clang_cc1 -verify -std=c23 -Wall -pedantic %s
3+
// RUN: %clang_cc1 -verify -std=c11 -Wall -pedantic %s
4+
// RUN: %clang_cc1 -verify -std=c99 -Wall -pedantic %s
5+
6+
/* WG14 N3505: Yes
7+
* Preprocessor integer expressions, v. 2
8+
*
9+
* This introduces a constraint that preprocessing tokens must be an integer
10+
* literal, character literal, punctuator, or some other implementation-defined
11+
* sequence of tokens (to support builtins that insert odd tokens into the
12+
* parsing stream).
13+
*/
14+
15+
// This is technically an integer constant expression, but it does not match
16+
// the new constraints and thus needs to be diagnosed.
17+
#if 1 ? 1 : (""[0] += 5) // expected-error {{invalid token at start of a preprocessor expression}}
18+
#endif
19+
20+
// But with a character literal, it is fine.
21+
#if 1 ? 1 : ('a' + 5) // Ok
22+
#endif
23+
24+
// This doesn't mean that all punctuators are fine, however.
25+
#if 1 ? 1 : ('a' += 5) // expected-error {{token is not a valid binary operator in a preprocessor subexpression}}
26+
#endif
27+
28+
// But some are.
29+
#if 1 ? 1 : ~('a') // Ok
30+
#endif
31+
32+
int x; // Needs a declaration to avoid a pedantic warning

clang/www/c_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ <h2 id="c2y">C2y implementation status</h2>
268268
<tr>
269269
<td>Preprocessor integer expressions, v. 2</td>
270270
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3505.pdf">N3505</a></td>
271-
<td class="unknown" align="center">Unknown</td>
271+
<td class="full" align="center">Yes</td>
272272
</tr>
273273
<tr>
274274
<td>Initialization of anonymous structures and unions</td>

0 commit comments

Comments
 (0)