-
Notifications
You must be signed in to change notification settings - Fork 278
Mimic GCC/Clang simplification behaviour when type checking ?: #7959
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
base: develop
Are you sure you want to change the base?
Mimic GCC/Clang simplification behaviour when type checking ?: #7959
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #7959 +/- ##
===========================================
- Coverage 80.39% 80.38% -0.01%
===========================================
Files 1688 1688
Lines 207403 207435 +32
Branches 73 73
===========================================
+ Hits 166749 166756 +7
- Misses 40654 40679 +25 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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! |
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
f86884c
to
2d5adef
Compare
5772645
to
f2ed870
Compare
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.
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.
f2ed870
to
0846117
Compare
Neither GCC nor Clang simplify expressions involving symbols even when the result would always be 0, which our simplifier figure out.
Fixes: #7927