Skip to content
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

Add support for old-style function definitions #23

Closed
wants to merge 2 commits into from

Conversation

dehorsley
Copy link

I'm looking to refactor some very old code and it would be useful to parse old-style (K&R) function definitions. For example:

int main(argc, argv)
int argc;
const char* argv[];
{
return 0;
}

(I see even the Github syntax highlighting doesn't handle it)

Adding repeat($.declaration) in the function definition seems natural.

Unfortunately, the attributes cause a little problem. For example in

void foo(bar) __attribute__((noreturn)) __attribute__((format(printf,1,2)))
int bar;
{
}

it isn't clear if the __attributes__ are attached to the function of the variable (or one each way!). This causes the generator to complain about a conflict.

By changing the precedence and adding a conflict rule, I was at least able to get sensible parses in most cases, but I'm not entirely happy with it. Perhaps someone more familiar with tree-sitter parsing can take a look.

I suspect the number of C files using both K&R defs and attributes is vanishingly small, but it would be nice to be complete.

For reference Clang allows the above and attaches the attributes to the function, but throws an error with:

void foo(bar) __attribute__((noreturn))
 __attribute__((format(printf,1,2))) int bar;
{
}

(Just a single newline! Whoever said C was whitespace independent!)

I don't have GCC on hand to check this right now.

@XVilka
Copy link
Contributor

XVilka commented May 9, 2023

As C23 deprecated such syntax, I wonder if it worth the effort to support now. I understand that it will take quite some time for the old code to migrate, but these days it is usually happens faster than before, especially for projects who active and on GitHub.

@amaanq
Copy link
Member

amaanq commented Aug 13, 2023

Added now

@amaanq amaanq closed this Aug 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants