Skip to content

bug: Partial linkage specification in #ifdef-#else block breaks #endif #288

Description

@NattyNarwhal

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-c

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

tree-sitter 0.25.10 (da6fe9beb4f7f67beb75914ca8e0d48ae48d6406)

Describe the bug

I'm interested in using tree-sitter to do some analysis of headers for FFI, but there's some constructs in them which trip up the C grammar. A partial linkage specification intended to be carried out from a possible ifdef branch will confuse the grammar, preventing it from reading the #endif and in the context of the full file, make it believe the ifdefs are unbalanced.

Steps To Reproduce/Bad Parse Tree

(translation_unit [0, 0] - [15, 0]
  (preproc_ifdef [0, 0] - [14, 71]
    name: (identifier [0, 7] - [0, 18])
    (preproc_call [1, 5] - [2, 0]
      directive: (preproc_directive [1, 5] - [1, 12])
      argument: (preproc_arg [1, 13] - [1, 42]))
    alternative: (preproc_else [2, 0] - [12, 19]
      (linkage_specification [3, 5] - [12, 19]
        value: (string_literal [3, 12] - [3, 16]
          (string_content [3, 13] - [3, 15]))
        (ERROR [4, 0] - [4, 6])
        body: (declaration [6, 0] - [12, 19]
          type: (primitive_type [6, 0] - [6, 4])
          declarator: (function_declarator [6, 5] - [12, 18]
            declarator: (identifier [6, 5] - [6, 13])
            parameters: (parameter_list [6, 13] - [12, 18]
              (parameter_declaration [6, 14] - [6, 20]
                type: (primitive_type [6, 14] - [6, 18])
                declarator: (abstract_pointer_declarator [6, 19] - [6, 20]))
              (comment [6, 36] - [6, 71])
              (parameter_declaration [7, 14] - [7, 18]
                type: (sized_type_specifier [7, 14] - [7, 18]))
              (comment [7, 36] - [7, 71])
              (parameter_declaration [8, 14] - [8, 20]
                type: (primitive_type [8, 14] - [8, 18])
                declarator: (abstract_pointer_declarator [8, 19] - [8, 20]))
              (comment [8, 36] - [8, 71])
              (parameter_declaration [9, 14] - [9, 20]
                type: (primitive_type [9, 14] - [9, 18])
                declarator: (abstract_pointer_declarator [9, 19] - [9, 20]))
              (comment [9, 36] - [9, 71])
              (parameter_declaration [10, 14] - [10, 20]
                type: (primitive_type [10, 14] - [10, 18])
                declarator: (abstract_pointer_declarator [10, 19] - [10, 20]))
              (comment [10, 36] - [10, 71])
              (parameter_declaration [11, 14] - [11, 20]
                type: (primitive_type [11, 14] - [11, 18])
                declarator: (abstract_pointer_declarator [11, 19] - [11, 20]))
              (comment [11, 36] - [11, 71])
              (variadic_parameter [12, 14] - [12, 17]))))))
    (comment [12, 36] - [14, 71])))
simple.c	Parse:    1.82 ms	   388 bytes/ms	(ERROR [4, 0] - [4, 6])

Expected Behavior/Parse Tree

The partial linkage specification would be its own thing, not messing up the parse of the rest of the source.

Repro

#ifdef __ILEC400__
     #pragma linkage (QUSRMBRD,OS,nowiden)
#else
     extern "OS"
#endif

void QUSRMBRD(void *,               /* Receiver Variable             */
              long,                 /* Receiver Variable Length      */
              char *,               /* Format name                   */
              void *,               /* File and library name         */
              char *,               /* Member name                   */
              char *,               /* Override processing           */
              ...);                 /* Optional Parameters:
                                         Error code
                                         Find member processing      */

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