Repository navigation
Evaluate GNU C typeof operands of variably modified type [GH #516] #457
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
71iq
wants to merge
1
commit into
edgcpp:main
Choose a base branch
from
71iq:fix/edg-28891-typeof-evaluation
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.
+277
−3
Open
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
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,17 @@ | ||
| [GH #516] | ||
| GNU C: typeof operands of variably modified type were not evaluated | ||
|
|
||
| When the operand of a GNU C typeof (or C23 typeof) operator has a variably | ||
| modified type, the operand must be evaluated. The front end treated it as | ||
| unevaluated, so its side effects were lost. The operand is now evaluated | ||
| once where the declaration, cast, compound literal, va_arg, or sizeof of a | ||
| variable-length array type appears. As in GCC 17, it is evaluated once per | ||
| declaration rather than once per declarator, and it is not evaluated again | ||
| when a typedef for the type is used. If the type's array bounds also have | ||
| side effects, side effects in the operand outside those bounds can still be | ||
| lost. For example, with --gnu_version: | ||
|
|
||
| int n = 1, a[8]; | ||
| void f(void) { | ||
| typeof (++n, (int (*)[n])a) p, q; // n is now 2. | ||
| } |
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 @@ | ||
| eccp -DTEST_NUMBER=1 --c --c11 --no_strict_gnu --gnu_version=130300 Test_name.c |
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 @@ | ||
| ./a.out |
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 @@ | ||
| eccp -DTEST_NUMBER=1 --c --c11 --no_strict_gnu --gnu_version=130300 Test_name.c |
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 @@ | ||
| ./a.out |
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 @@ | ||
| eccp -DTEST_NUMBER=1 --c --c11 --no_strict_gnu --gnu_version=170000 Test_name.c |
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 @@ | ||
| ./a.out |
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,12 @@ | ||
| //type:rp | ||
| //options_all:--c --c11 --no_strict_gnu --gnu_version=130300 | ||
| //remark:GNU C typeof operand in a block-scope prototype is not evaluated | ||
| // A typeof operand of variably modified type in a block-scope function | ||
| // prototype is not evaluated, and must not be inserted as a statement. | ||
| int a[8]; | ||
| int main(void) { | ||
| int m = 2; | ||
| void local_proto(typeof (++m, (int (*)[m])a) p); | ||
| void local_proto2(int n, typeof (++m, (int (*)[m])a) *p, int q[m]); | ||
| return m != 2; | ||
| } |
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,22 @@ | ||
| //type:rp | ||
| //options_all:--c --c11 --no_strict_gnu --gnu_version=130300 | ||
| //remark:GNU C typeof operand of variably modified type is evaluated once | ||
| // Each typeof operand of variably modified type is evaluated exactly once | ||
| // (with any back end): declarations with one declarator, a typedef, and a | ||
| // cast. | ||
| int main(void) { | ||
| int k = 1, a[8], fails = 0; | ||
| typeof (++k, (int (*)[k])a) p; | ||
| (void)sizeof(p); | ||
| fails += k != 2; | ||
| typedef typeof (++k, (int (*)[k])a) T; | ||
| T t; | ||
| (void)sizeof(t); | ||
| fails += k != 3; | ||
| (void)(typeof (++k, (int (*)[k])a))a; | ||
| fails += k != 4; | ||
| typeof ((({ ++k; 0; }), (int (*)[k])a)) s; | ||
| (void)sizeof(s); | ||
| fails += k != 5; | ||
| return fails; | ||
| } |
Oops, something went wrong.
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.
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.
Preserve operand side effects when bounds also have side effects.
For this block declaration:
The bound has side effects, so this guard excludes the entire operand from capture. Bound evaluation does not perform the separate
++k. GNUtypeofrequires evaluation of this variably modified operand. (gcc.gnu.org)The PR documents this limitation, but the limitation leaves the reported side-effect loss unresolved for this input. Separate bound evaluation from the remaining operand evaluation so that both increments occur once.
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.
Confirmed. The example still loses
++k; this is the remaining limitation already described in the PR and the Changes entry.Removing the guard would evaluate the bound both in the retained operand's cast and in the resulting type, which is the double evaluation caught by
gnu/c/vla-12. Evaluating the bound earlier can also change its order relative to other operand side effects. A complete fix needs to save the bound at its original evaluation point and reuse that value wherever the type appears.That requires further VLA lowering work beyond this patch. I'm leaving this limitation explicit and this thread open for maintainer review.