-
Notifications
You must be signed in to change notification settings - Fork 273
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
Mimic GCC/Clang simplification behaviour when type checking ?: #7959
Open
tautschnig
wants to merge
4
commits into
diffblue:develop
Choose a base branch
from
tautschnig:features/gcc-clang-ite-simp
base: develop
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bab1341
Move is_compile_time_constantt to top of file
tautschnig 7d0523f
Mimic GCC/Clang simplification behaviour when type checking ?:
tautschnig 8430887
Expand documentation of is_compile_time_constantt
tautschnig 2d5adef
__builtin_constant_p: properly test, fix behaviour
tautschnig 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 was deleted.
Oops, something went wrong.
This file contains 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,23 @@ | ||
int main() | ||
{ | ||
long long i; | ||
#ifndef _MSC_VER | ||
_Static_assert(sizeof(int) == sizeof(*(1 ? ((void *)(0ll)) : (int *)1)), ""); | ||
// We are able to simplify all of the expressions involving i below to 0, but | ||
// GCC and Clang don't do so. Hence, the static asserts pass for those | ||
// compilers. | ||
_Static_assert( | ||
sizeof(int) != sizeof(*(1 ? ((void *)(i * 0)) : (int *)1)), ""); | ||
_Static_assert( | ||
sizeof(int) != sizeof(*(1 ? ((void *)(i - i)) : (int *)1)), ""); | ||
_Static_assert( | ||
sizeof(int) != sizeof(*(1 ? ((void *)(i ? 0ll : 0ll)) : (int *)1)), ""); | ||
_Static_assert( | ||
sizeof(int) != sizeof(*(1 ? ((void *)(0 ? i : 0ll)) : (int *)1)), ""); | ||
#else | ||
static_assert(sizeof(int) == sizeof(*(1 ? ((void *)(0)) : (int *)1)), ""); | ||
// Visual Studio rejects this as "illegal indirection" | ||
// static_assert( | ||
// sizeof(int) == sizeof(*(1 ? ((void *)(i * 0)) : (int *)1)), ""); | ||
#endif | ||
} |
2 changes: 1 addition & 1 deletion
2
.../ansi-c/gcc_builtin_constant_p1/test.desc → regression/ansi-c/sizeof6/test.desc
This file contains 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
CORE gcc-only broken-test-c++-front-end | ||
CORE | ||
main.c | ||
|
||
^EXIT=0$ | ||
|
This file contains 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,47 @@ | ||
#include <assert.h> | ||
|
||
#ifdef __GNUC__ | ||
enum | ||
{ | ||
E1 = 1 | ||
} var; | ||
|
||
struct whatnot | ||
{ | ||
} whatnot_var; | ||
#endif | ||
|
||
int main() | ||
{ | ||
// this is gcc only | ||
|
||
#ifdef __GNUC__ | ||
assert(__builtin_constant_p("some string")); | ||
assert(__builtin_constant_p(1.0f)); | ||
assert(__builtin_constant_p(E1)); | ||
assert(!__builtin_constant_p(var)); | ||
assert(!__builtin_constant_p(main)); | ||
assert(!__builtin_constant_p(whatnot_var)); | ||
assert(!__builtin_constant_p(&var)); | ||
assert(__builtin_constant_p(__builtin_constant_p(var))); | ||
|
||
// The following are not constant expressions in the sense of the C standard | ||
// and GCC wouldn't deem them constant expressions either, but they are | ||
// subject to GCC's constant folding. See also regression test ansi-c/sizeof6. | ||
// Clang's behaviour, however, is somewhat different. See | ||
// https://github.com/llvm/llvm-project/issues/55946 for further examples of | ||
// where they differ. | ||
int j; | ||
# ifndef __clang__ | ||
assert(__builtin_constant_p(j * 0)); | ||
assert(__builtin_constant_p(j - j)); | ||
assert(__builtin_constant_p(j ? 0ll : 0ll)); | ||
# endif | ||
assert(__builtin_constant_p(0 ? j : 0ll)); | ||
|
||
// side-effects are _not_ evaluated | ||
int i = 0; | ||
assert(!__builtin_constant_p(i++)); | ||
assert(i == 0); | ||
#endif | ||
} |
This file contains 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,9 @@ | ||
CORE | ||
main.c | ||
|
||
^VERIFICATION SUCCESSFUL$ | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
-- | ||
^warning: ignoring | ||
^CONVERSION ERROR$ |
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.
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.
These appear to assert that some part of the subexpression is a compile time constant. May I suggest to use
__builtin_constant_p
for that. The sizeof assertion is a distraction.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.
This, I'm afraid, just uncovered another challenge: the above "true" cases aren't deemed null-pointer constants by the compilers (and, therefore, the type of the
?:
isvoid*
rather thanint*
).__builtin_constant_p
, however, does return1
for the very same expressions!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 added a commit that better exercises (and extends) an existing test to also document (and test) these differences.
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.
Uhhh, sad
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.
Might be worth filing a bug report with those compilers.