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

Invalid AST for primitive type

Open genotrance opened this issue 4 years ago • 2 comments

When run on a C standard header which defines size_t:

typedef unsigned long size_t;

Output is:

translation_unit [0, 0] - [1, 0])
  type_definition [0, 0] - [0, 29])
    type: sized_type_specifier [0, 8] - [0, 28])
      type: primitive_type [0, 22] - [0, 28])
    declarator: MISSING type_identifier [0, 28] - [0, 28])

Even if you remove the unsigned:

typedef long size_t;

Output is:

translation_unit [0, 0] - [1, 0])
  type_definition [0, 0] - [0, 20])
    type: sized_type_specifier [0, 8] - [0, 19])
      type: primitive_type [0, 13] - [0, 19])
    declarator: MISSING type_identifier [0, 19] - [0, 19])

Changing size_t to non-primitive asize_t and it works fine:

typedef unsigned long asize_t;

Output is:

translation_unit [0, 0] - [1, 0])
  type_definition [0, 0] - [0, 30])
    type: sized_type_specifier [0, 8] - [0, 21])
    declarator: type_identifier [0, 22] - [0, 29])

genotrance avatar Oct 03 '20 21:10 genotrance

Seems to be caused by this: https://github.com/tree-sitter/tree-sitter-c/blob/99151b1e9293c9e025498fee7e6691e1a52e1d03/grammar.js#L463

Which is plain wrong, because there should be some distinction between built-in types and aliases (like ptrdiff_t or size_t).

ernestask avatar Nov 04 '20 20:11 ernestask

     primitive_type: $ => token(choice(
-      'bool',
+      '_Bool',
+      '_Complex',
+      '_Imaginary',
       'char',
       'int',
       'float',
       'double',
       'void',
-      'size_t',
-      'ssize_t',
-      'intptr_t',
-      'uintptr_t',
-      'charptr_t',
-      ...[8, 16, 32, 64].map(n => `int${n}_t`),
-      ...[8, 16, 32, 64].map(n => `uint${n}_t`),
-      ...[8, 16, 32, 64].map(n => `char${n}_t`)
     )),

This does fix the issue, but there’s a bit of fallout regarding highlighting, in that the removed typedefs are standardized (either in the language standard or POSIX) while not being built-in types, so not sure how to deal with that.

ernestask avatar Nov 04 '20 21:11 ernestask