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

Seems 'function_definition' not parsed correctly when putting 'else-if' in '#ifndef'

Open Ps7ch3 opened this issue 4 years ago • 0 comments

I'm trying to extract all the function_definition in C files but seems there's a false positive for 'function_definition' query.

Minimal repro:

int foo(int a)
{
    if (0)
        return 0;
    #ifndef XXX
    else if (1){
    	return 0;
    }
    #endif
}
translation_unit [0, 0] - [11, 0]
  function_definition [0, 0] - [9, 1]
    type: primitive_type [0, 0] - [0, 3]
    declarator: function_declarator [0, 4] - [0, 14]
      declarator: identifier [0, 4] - [0, 7]
      parameters: parameter_list [0, 7] - [0, 14]
        parameter_declaration [0, 8] - [0, 13]
          type: primitive_type [0, 8] - [0, 11]
          declarator: identifier [0, 12] - [0, 13]
    body: compound_statement [1, 0] - [9, 1]
      if_statement [2, 4] - [3, 17]
        condition: parenthesized_expression [2, 7] - [2, 10]
          number_literal [2, 8] - [2, 9]
        consequence: return_statement [3, 8] - [3, 17]
          number_literal [3, 15] - [3, 16]
      preproc_ifdef [4, 4] - [8, 10]
        name: identifier [4, 12] - [4, 15]
        function_definition [5, 4] - [7, 5]  -------------------- strange result as function_definition
          type: type_identifier [5, 4] - [5, 8]
          declarator: function_declarator [5, 9] - [5, 15]
            declarator: identifier [5, 9] - [5, 11]
            parameters: parameter_list [5, 12] - [5, 15]
              ERROR [5, 13] - [5, 14]
                number_literal [5, 13] - [5, 14]
          body: compound_statement [5, 15] - [7, 5]
            return_statement [6, 5] - [6, 14]
              number_literal [6, 12] - [6, 13]

query: (function_definition (function_declarator (identifier) (parameter_list) @params) @decl)

image

I'm not sure if this behavior is expected, but what should I do to avoid this?

Ps7ch3 avatar Aug 06 '21 08:08 Ps7ch3