forked from cil-project/cil
-
Notifications
You must be signed in to change notification settings - Fork 25
Fix typedef/variable name conflict parsing error #219
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
Copilot
wants to merge
9
commits into
develop
Choose a base branch
from
copilot/fix-typedef-variable-name-conflict
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
9 commits
Select commit
Hold shift + click to select a range
3494d12
Initial plan
Copilot 98004a6
Fix typedef/variable name conflict: add declarator_init_start rule to…
Copilot f9a7b12
Fix comment style in cparser.mly to use /* (* TEXT *) */
Copilot d8822cc
Fix double add_identifier: skip re-registration in doDeclaration for …
Copilot 160543c
Fix add_identifier timing: register each declarator immediately after…
Copilot d977be5
Add test for NO_INIT declarator typedef shadowing in mixed declarations
Copilot 363114d
Fix scope2 regression: revert declarator_no_init, keep declarator_ini…
Copilot 5960536
Add NO_INIT middle-declarator test and re-add declarator_no_init with…
Copilot aa40855
Move add_type to declarator_no_init: all lexer registration per-decla…
Copilot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* Test for typedef and variable name conflict (issue #114). | ||
| A variable with the same name as a typedef should shadow it in its initializer, | ||
| per C11 6.2.1.7: "Any other identifier has scope that begins just after the | ||
| completion of its declarator." */ | ||
| #include "testharness.h" | ||
| #include <stdint.h> | ||
| #include <stdlib.h> | ||
|
|
||
| typedef struct raxNode { | ||
| uint32_t iskey:1; | ||
| uint32_t isnull:1; | ||
| uint32_t iscompr:1; | ||
| uint32_t size:29; | ||
| unsigned char data[]; | ||
| } raxNode; | ||
|
|
||
| typedef struct rax { | ||
| raxNode *head; | ||
| uint64_t numele; | ||
| uint64_t numnodes; | ||
| } rax; | ||
|
|
||
| typedef int mytype; | ||
|
|
||
| int main() { | ||
| rax *rax = malloc(sizeof(*rax)); /* variable rax shadows typedef rax in initializer */ | ||
| if (rax == 0) E(1); | ||
| free(rax); | ||
|
|
||
| /* NO_INIT variable in the middle of a declaration list shadows a typedef. | ||
| After "mytype a = 1, mytype", the name "mytype" is an identifier (variable). | ||
| So "b = sizeof mytype" (without parens) is only valid when mytype is a variable | ||
| (types require parens: "sizeof(type)"). This fails to parse without the fix. */ | ||
| { | ||
| mytype a = 1, mytype, b = sizeof mytype; | ||
| mytype = 5; | ||
| if (a != 1) E(2); | ||
| if (mytype != 5) E(3); | ||
| if (b != sizeof(int)) E(4); | ||
| } | ||
|
|
||
| SUCCESS; | ||
| } | ||
michael-schwarz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,48 @@ | ||
| /* Test for typedef/variable name conflict in a declaration list with multiple | ||
| initialized declarators. | ||
|
|
||
| Per C11 6.2.1.7, a declared identifier's scope begins just after the completion | ||
| of its declarator. For an initialized declarator "T = expr", scope begins just | ||
| after the declarator (before the "="), so the initializer of a LATER declarator | ||
| in the same list can refer to T as a variable. | ||
|
|
||
| Concretely, after "T T = 1", the name T is registered as a variable (identifier) | ||
| by the lexer hack, so that the next initializer "T + 1" correctly sees T as a | ||
| variable. | ||
|
|
||
| This test fails to parse (without the fix) because the initializer T + 1 would | ||
| see T as a type name (NAMED_TYPE), making "T + 1" a syntax error. */ | ||
| #include "testharness.h" | ||
|
|
||
| typedef int T; | ||
| typedef int mytype; | ||
|
|
||
| int test_init_chain(void) { | ||
| /* "T T = 1": declarator_init_start registers T as an identifier before "= 1". | ||
| "U = T + 1": T in the initializer must be the variable T (value 1), not the | ||
| typedef. If it's still a type, "T + 1" is a syntax error. */ | ||
| { | ||
| T T = 1, U = T + 1; | ||
| if (T != 1) E(1); | ||
| if (U != 2) E(2); | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| int test_typedef_varname_init(void) { | ||
| /* Original issue: rax-style conflict where a typedef and variable share a name. | ||
| The variable's initializer can use the variable name as a regular identifier. */ | ||
| { | ||
| mytype mytype = 42; | ||
| if (mytype != 42) E(3); | ||
| (void)mytype; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| int main(void) { | ||
| if (test_init_chain()) return 1; | ||
| if (test_typedef_varname_init()) return 1; | ||
| SUCCESS; | ||
| } | ||
|
|
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
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.