-
Notifications
You must be signed in to change notification settings - Fork 271
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
base: develop
Are you sure you want to change the base?
Mimic GCC/Clang simplification behaviour when type checking ?: #7959
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## develop #7959 +/- ##
===========================================
- Coverage 79.08% 79.07% -0.02%
===========================================
Files 1698 1698
Lines 196491 196519 +28
===========================================
+ Hits 155404 155406 +2
- Misses 41087 41113 +26 ☔ View full report in Codecov by Sentry. |
We will eventually need explicit constant folding logic in the C frontend, as opposed to relying on the simplifier. I expect that the latter will always be able to simplify more. |
This PR needs more contemplation. The PR changes the typing, but would appear to be about constant folding. |
506cd0d
to
6f82cc7
Compare
@kroening: Please take another look, I hope to have adjusted this in line with your feedback as it now uses (compile-time) constant folding rules. |
6f82cc7
to
3421bfd
Compare
_Static_assert( | ||
sizeof(int) != sizeof(*(1 ? ((void *)(i ? 0ll : 0ll)) : (int *)1)), ""); | ||
_Static_assert( | ||
sizeof(int) != sizeof(*(1 ? ((void *)(0 ? i : 0ll)) : (int *)1)), ""); |
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 ?:
is void*
rather than int*
). __builtin_constant_p
, however, does return 1
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.
433de81
to
4076a22
Compare
Apologies that the scope of this PR is getting pushed larger, by me. Does it make sense to replace the minimal case split done for |
I'll take care of this! |
This is useful in various methods and should be available to all of them.
Neither GCC nor Clang simplify expressions involving symbols even when the result would always be 0, which our simplifier figure out. Fixes: diffblue#7927
C11 Section 6.6 "Constant expressions" defines what are required to be constant expressions, and permits implementations to pick additional constant expressions.
4076a22
to
d39db8f
Compare
I believe this is now done. |
} | ||
else | ||
is_constant=tmp1.is_constant(); | ||
{ | ||
// try to produce constant |
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.
is_compile_time_constantt
is too far off in the GCC/MSVC case?
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.
__builtin_constant_p
doesn't exist with MSVC, so it'd be GCC only. For that, however, the simplifier appears to be much closer: GCC happily takes x - x
, x * 0
as constants. But then we could still do what I now (in my latest push to this PR) ended up doing: produce a variant of is_compile_time_constantt
for a specific compiler (clang_is_constant_foldedt
).
0c1b2f7
to
5d04507
Compare
5d04507
to
f86884c
Compare
We previously only compile-time tested what required a run-time check of assertions, so move this to the "cbmc" regression test suite. Also, extend it by behaviour that distinguishes it from actual C-standard specified constant expressions. Finally, fix the behaviour for string literals.
f86884c
to
2d5adef
Compare
Neither GCC nor Clang simplify expressions involving symbols even when the result would always be 0, which our simplifier figure out.
Fixes: #7927