-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Better diagnostics when assertion fails in consteval
#130458
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
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// RUN: %clang_cc1 -std=c++23 -verify=expected,cxx20_plus -DTEST_LINUX %s | ||
// RUN: %clang_cc1 -std=c++23 -verify=expected,cxx20_plus -DTEST_WINDOWS %s | ||
// RUN: %clang_cc1 -std=c++23 -verify=expected,cxx20_plus -DTEST_DARWIN %s | ||
|
||
#ifdef __ASSERT_FUNCTION | ||
#undef __ASSERT_FUNCTION | ||
#endif | ||
|
||
#if defined(TEST_LINUX) | ||
extern "C" void __assert_fail(const char*, const char*, unsigned, const char*); | ||
#define assert(cond) \ | ||
((cond) ? (void)0 : __assert_fail(#cond, __FILE__, __LINE__, __func__)) | ||
#elif defined(TEST_DARWIN) | ||
void __assert_rtn(const char *, const char *, int, const char *); | ||
#define assert(cond) \ | ||
(__builtin_expect(!(cond), 0) ? __assert_rtn(__func__, __FILE__, __LINE__, #cond) : (void)0) | ||
#elif defined(TEST_WINDOWS) | ||
void /*__cdecl*/ _wassert(const wchar_t*, const wchar_t*, unsigned); | ||
#define _CRT_WIDE_(s) L ## s | ||
#define _CRT_WIDE(s) _CRT_WIDE_(s) | ||
#define assert(cond) \ | ||
(void)((!!(cond)) || (_wassert(_CRT_WIDE(#cond), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0)) | ||
#endif | ||
|
||
consteval int square(int x) { | ||
int result = x * x; | ||
assert(result == 42); // expected-note {{assertion failed during evaluation of constant expression}} | ||
return result; | ||
} | ||
|
||
void test() { | ||
auto val = square(2); // expected-note {{in call to 'square(2)'}} \ | ||
// expected-error {{call to consteval function 'square' is not a constant expression}} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why you are not doing that in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea how to access the preprocessor from this method and I am too inexperienced to know where to start.
If you can give me a hint on where to look I would appreciate that. Do I just do a big refactor and pass it in as a parameter to the function? Or is there some method on an object I already have?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right, we would need access to Sema, which we currently don't. Good answer! (@Endilll !)
Given that, I am satisfied with the change as is, thanks