-
Notifications
You must be signed in to change notification settings - Fork 143
Enhance function parsing #308
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
Conversation
For the second example, the improved shecc will report an error message as follows due to conflicting types for $ qemu-arm out/shecc-stage2.elf --no-libc -o example2 example2.c
Error: conflicting types for the function func.
before: void func()
after: int **func() Other examples:
|
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.
1 issue found across 2 files
Prompt for AI agents (all 1 issues)
Understand the root cause of the following 1 issues and fix them.
<file name="tests/driver.sh">
<violation number="1" location="tests/driver.sh:5590">
The new compile-error tests return values from `void`/pointer functions (e.g., `return 3;`), so they will always trigger diagnostics unrelated to the prototype mismatch, meaning the tests won’t catch the regression they target.</violation>
</file>
React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai
to give feedback, ask questions, or re-run the review.
e740cd6
to
f5df160
Compare
Add handling for
|
Since the previous parser may incorrectly handle a function with a forward declaration before implementation, the code generator produces wrong instructions for a function because the frontend provides incorrect information. For example, consider the following code: int func(int *a); int func(int *a) { return *a; } int main() { /* ... */ } After parsing the forward declaration of 'func', it is added to the function list, and its parameter 'a' is recorded with the type 'int *'. When the function implementation is later parsed, the parser processes the declaration again, but the pointer level of 'a' is accumulated, causing the type of 'a' to become 'int **'. Therefore, to resolve the above issue and enhance the function parsing, these changes improve the parser to correctly handle functions with forward declarations, and report an error message if a later declaration differs from a previous one. Close sysprog21#305
Since the previous commit enhanced function parsing so that the parser reports an error if a later function declaration differs from a previous one, this commit adds some test cases to the test suite to validate this functionality.
f5df160
to
c0d33ed
Compare
Thank @DrXiao for contributing! |
Since the previous parser incorrectly handles a function with a forward declaration before implementation, the code generator may produce wrong instructions due to the incorrect information provided by the frontend.
For example, consider the following example:
After parsing the forward declaration of
func
, it is added to the function list, and its parametera
is recorded with the typechar *
. When the function implementation is later parsed, the parser processes the declaration again, but the pointer level ofa
is accumulated, causing the type ofa
to becomechar **
.This can be observed when using shecc to compile and output the IRs:
Additionally, if a function with a forward declaration differs from the later declaration, shecc still continues to parse the source instead of reporting an error:
In the above case, shecc considers
func
to be a function of typeint **(void)
.Therefore, the proposed changes enhance the implementation of the parser to resolve the above issues.
Summary by cubic
Fix function parsing for forward-declared functions to keep parameter types correct and to error on conflicting declarations, preventing incorrect IR generation.