tree-sitter-c icon indicating copy to clipboard operation
tree-sitter-c copied to clipboard

bug: Incorrect parsing on function typedef

Open NathanSnail opened this issue 11 months ago • 3 comments

Did you check existing issues?

  • [ ] I have read all the tree-sitter docs if it relates to using the parser
  • [X] I have searched the existing issues of tree-sitter-c

Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

No response

Describe the bug

Using

int typedef fn_typdef(const char *args);

like syntax to define a function typedef results in the typedef keyword being highlighted as a variable and an error in the syntax tree. :Inspect in neovim image :InspectTree image This syntax is relatively obscure but is valid and so should be highlighted correctly.

Steps To Reproduce/Bad Parse Tree

  1. Make a file which uses function typedef syntax ReturnT typdef TypeName(args);
  2. Now the typedef will by incorrectly represented in the tree.

Expected Behavior/Parse Tree

The tree should probably use keyword.type.c like it usually does for typedefs.

Repro

// Example code that causes the issue
int typedef fn_typdef(const char *args);

NathanSnail avatar Jan 15 '25 04:01 NathanSnail

This does not only affect function types but all typedefs where the typedef token is not the first token in the declaration:

$ cat t.c
int typedef U;
$ tree-sitter parse t.c
(translation_unit [0, 0] - [1, 0]
  (declaration [0, 0] - [0, 14]
    type: (primitive_type [0, 0] - [0, 3])
    declarator: (identifier [0, 4] - [0, 11])
    (ERROR [0, 12] - [0, 13]
      (identifier [0, 12] - [0, 13]))))
t.c     Parse:    0.16 ms           93 bytes/ms (ERROR [0, 12] - [0, 13])

narpfel avatar Apr 13 '25 16:04 narpfel

Woah, this is definitely a TIL! Never seen this syntax in 10 years of C programming. Is there any benefit/difference to it?

gr1mpatr0n avatar Apr 14 '25 01:04 gr1mpatr0n

Is there any benefit/difference to it?

No, it’s just another case where C’s grammar is unnecessarily weird and complex. Apart from writing test cases for a C compiler, this should IMHO never be used.

narpfel avatar Apr 15 '25 17:04 narpfel