-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[C2y] Support WG14 N3457, the __COUNTER__ macro #162662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AaronBallman
wants to merge
7
commits into
llvm:main
Choose a base branch
from
AaronBallman:aballman-wg14-n3457
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−16
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7a5e159
[C2y] Support WG14 N3457, the __COUNTER__ macro
AaronBallman 1d0a459
Clang 22 isn't released yet
AaronBallman 7129a6a
Use a named constant; NFC
AaronBallman 1c123de
Add C++ pedantic test
AaronBallman 0720771
Update Language Extensions docs for backporting
AaronBallman 19e851e
unsigned long -> uint32_t; NFC
AaronBallman aabea7c
Use digit separators in the diagnostic
AaronBallman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// RUN: %clang_cc1 -verify=ext -std=c23 -pedantic %s | ||
// RUN: %clang_cc1 -verify=ext -pedantic -x c++ %s | ||
// RUN: %clang_cc1 -verify=pre -std=c2y -pedantic -Wpre-c2y-compat %s | ||
|
||
/* WG14 N3457: Clang 22 | ||
* The __COUNTER__ predefined macro | ||
* | ||
* This predefined macro was supported as an extension in earlier versions of | ||
* Clang, but the required diagnostics for the limits were not added until 22. | ||
AaronBallman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
|
||
// Ensure that __COUNTER__ starts from 0. | ||
static_assert(__COUNTER__ == 0); /* ext-warning {{'__COUNTER__' is a C2y extension}} | ||
pre-warning {{'__COUNTER__' is incompatible with standards before C2y}} | ||
*/ | ||
|
||
// Ensure that the produced value can be used with token concatenation. | ||
#define CAT_IMPL(a, b) a ## b | ||
#define CAT(a, b) CAT_IMPL(a, b) | ||
#define NAME_WITH_COUNTER(a) CAT(a, __COUNTER__) | ||
void test() { | ||
// Because this is the 2nd expansion, this defines test1. | ||
int NAME_WITH_COUNTER(test); /* ext-warning {{'__COUNTER__' is a C2y extension}} | ||
pre-warning {{'__COUNTER__' is incompatible with standards before C2y}} | ||
*/ | ||
int other_test = test1; // Ok | ||
} | ||
|
||
// Ensure that __COUNTER__ increments each time you mention it. | ||
static_assert(__COUNTER__ == 2); /* ext-warning {{'__COUNTER__' is a C2y extension}} | ||
pre-warning {{'__COUNTER__' is incompatible with standards before C2y}} | ||
*/ | ||
static_assert(__COUNTER__ == 3); /* ext-warning {{'__COUNTER__' is a C2y extension}} | ||
pre-warning {{'__COUNTER__' is incompatible with standards before C2y}} | ||
*/ | ||
static_assert(__COUNTER__ == 4); /* ext-warning {{'__COUNTER__' is a C2y extension}} | ||
pre-warning {{'__COUNTER__' is incompatible with standards before C2y}} | ||
*/ |
zwuis marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// RUN: %clang_cc1 -verify -std=c2y -finitial-counter-value=2147483646 %s | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if we set |
||
|
||
// The value produced needs to be a type that's representable with a signed | ||
// long. However, the actual type it expands to does *not* need to be forced to | ||
// be signed long because that would generally mean suffixing the value with L, | ||
// which would be very surprising for folks using this to generate unique ids. | ||
// We'll test this by ensuring the largest value can be expanded properly and | ||
// an assertion that signed long is always at least four bytes wide (which is | ||
// what's required to represent that maximal value). | ||
// | ||
// So we set the initial counter value to 2147483646, we'll validate that, | ||
// increment it once to get to the maximal value and ensure there's no | ||
// diagnostic, then increment again to ensure we get the constraint violation. | ||
|
||
static_assert(__COUNTER__ == 2147483646); // Test and increment | ||
static_assert(__COUNTER__ == 2147483647); // Test and increment | ||
|
||
// This one should fail. | ||
signed long i = __COUNTER__; // expected-error {{'__COUNTER__' value cannot exceed 2'147'483'647}} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// RUN: %clang_cc1 -verify=good -std=c2y -finitial-counter-value=2147483648 %s | ||
// RUN: %clang_cc1 -verify -std=c2y -finitial-counter-value=2147483648 -DEXPAND_IT %s | ||
// good-no-diagnostics | ||
|
||
// This sets the intial __COUNTER__ value to something that's too big. Setting | ||
// the value too large is fine. Expanding to a too-large value is not. | ||
#ifdef EXPAND_IT | ||
// This one should fail. | ||
signed long i = __COUNTER__; // expected-error {{'__COUNTER__' value cannot exceed 2'147'483'647}} | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.