tree-sitter / tree-sitter/tree-sitter-cpp

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

Open
#281 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
453
Forks
185
PR merge metrics
No merged PRs in 30d

Description

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;

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce void (*func_t)() = NULL; in the tree-sitter-cpp playground and inspect the declarator grammar entry points that produce the expression tree. Compare the result with tree-sitter-c's parse tree; done means the C++ grammar produces a declaration with a function declarator, parenthesized pointer declarator, empty parameter list, and initializer.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.