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

bug: Member Variables Declarations Using Equal-Initializer Parsed As Function Definitions

Open savantes1 opened this issue 1 year ago • 0 comments

Did you check existing issues?

  • [X] 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-cpp

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

tree-sitter 0.22.3, tree-sitter-cpp 0.22.2

Describe the bug

Member variables that are initialized using the equal-initializer syntax (e.g., int x = 0;) show up in the tree as type function_definition rather than type field_declaration , as is the case when member variables are uninitialized or brace-initialized.

Steps To Reproduce/Bad Parse Tree

If you parse the following custom data type:

struct A
{
    int x = 0;
    int y{0};
    int z;

    void func() {}
};

You get the following parse tree:

(translation_unit 
    (struct_specifier 
        name: (type_identifier) 
        body: (field_declaration_list
            (function_definition 
                type: (primitive_type)
                declarator: (field_identifier)
                (pure_virtual_clause)
            )
            (field_declaration 
                type: (primitive_type)
                declarator: (field_identifier) 
                default_value: (initializer_list 
                    (number_literal)
                )
            ) 
            (field_declaration 
                type: (primitive_type) 
                declarator: (field_identifier)
            ) 
            (function_definition 
                type: (primitive_type) 
                declarator: (function_declarator 
                    declarator: (field_identifier) 
                    parameters: (parameter_list)
                ) 
                body: (compound_statement)
            )
        )
    )
)

Expected Behavior/Parse Tree

(translation_unit 
    (struct_specifier 
        name: (type_identifier) 
        body: (field_declaration_list
            (field_declaration 
                type: (primitive_type)
                declarator: (field_identifier)
                (default_value: (initializer_list
                   (number_literal)
            ) 
            (field_declaration 
                type: (primitive_type)
                declarator: (field_identifier) 
                default_value: (initializer_list 
                    (number_literal)
                )
            ) 
            (field_declaration 
                type: (primitive_type) 
                declarator: (field_identifier)
            ) 
            (function_definition 
                type: (primitive_type) 
                declarator: (function_declarator 
                    declarator: (field_identifier) 
                    parameters: (parameter_list)
                ) 
                body: (compound_statement)
            )
        )
    )
)

savantes1 avatar Jun 28 '24 04:06 savantes1