Skip to content

bug: declaration of function pointer not handling correctly functions without arguments #281

Open

Description

@uriyay-jfrog

Did you check existing issues?

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

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

No response

Describe the bug

Function declarator without any parameters is not parsed correctly in tree-sitter-cpp:

void (*func_t)() = NULL;

Output from the playground:

translation_unit [0, 0] - [6, 0]
  expression_statement [0, 0] - [0, 24]
    assignment_expression [0, 0] - [0, 23]
      left: call_expression [0, 0] - [0, 16]
        function: call_expression [0, 0] - [0, 14]
          function: primitive_type [0, 0] - [0, 4]
          arguments: argument_list [0, 5] - [0, 14]
            pointer_expression [0, 6] - [0, 13]
              argument: identifier [0, 7] - [0, 13]
        arguments: argument_list [0, 14] - [0, 16]
      right: null [0, 19] - [0, 23]

The parser for some reason treats it as an assignment with call expression as the left side.

While tree-sitter-c parses it correctly:

translation_unit [0, 0] - [6, 0]
  declaration [0, 0] - [0, 24]
    type: primitive_type [0, 0] - [0, 4]
    declarator: init_declarator [0, 5] - [0, 23]
      declarator: function_declarator [0, 5] - [0, 16]
        declarator: parenthesized_declarator [0, 5] - [0, 14]
          pointer_declarator [0, 6] - [0, 13]
            declarator: identifier [0, 7] - [0, 13]
        parameters: parameter_list [0, 14] - [0, 16]
      value: null [0, 19] - [0, 23]

Steps To Reproduce/Bad Parse Tree

translation_unit [0, 0] - [6, 0]
  expression_statement [0, 0] - [0, 24]
    assignment_expression [0, 0] - [0, 23]
      left: call_expression [0, 0] - [0, 16]
        function: call_expression [0, 0] - [0, 14]
          function: primitive_type [0, 0] - [0, 4]
          arguments: argument_list [0, 5] - [0, 14]
            pointer_expression [0, 6] - [0, 13]
              argument: identifier [0, 7] - [0, 13]
        arguments: argument_list [0, 14] - [0, 16]
      right: null [0, 19] - [0, 23]

Expected Behavior/Parse Tree

translation_unit [0, 0] - [6, 0]
  declaration [0, 0] - [0, 24]
    type: primitive_type [0, 0] - [0, 4]
    declarator: init_declarator [0, 5] - [0, 23]
      declarator: function_declarator [0, 5] - [0, 16]
        declarator: parenthesized_declarator [0, 5] - [0, 14]
          pointer_declarator [0, 6] - [0, 13]
            declarator: identifier [0, 7] - [0, 13]
        parameters: parameter_list [0, 14] - [0, 16]
      value: null [0, 19] - [0, 23]

Repro

void (*func_t)() = NULL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions