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

bug: #ifdef causes "else" to be treated as error

Open
#298 0 comments 0 reactions 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)

Playground (0.23.4?)

Describe the bug

When "interrupting" an if/else with an #ifdef then the else is treated as an error. This might be tricky to turn into a proper tree, as the "ifdef" can separate the "else" branch content from the "if".

Steps To Reproduce/Bad Parse Tree
translation_unit [0, 0] - [6, 0]
  preproc_ifdef [0, 0] - [4, 6]
    name: identifier [0, 7] - [0, 20]
    if_statement [1, 1] - [2, 8]
      condition: condition_clause [1, 4] - [1, 9]
        value: identifier [1, 5] - [1, 8]
      consequence: expression_statement [2, 2] - [2, 8]
        call_expression [2, 2] - [2, 7]
          function: identifier [2, 2] - [2, 5]
          arguments: argument_list [2, 5] - [2, 7]
    ERROR [3, 1] - [3, 5]
  expression_statement [5, 2] - [5, 8]
    call_expression [5, 2] - [5, 7]
      function: identifier [5, 2] - [5, 5]
      arguments: argument_list [5, 5] - [5, 7]
Expected Behavior/Parse Tree

A solution might be difficult to do with the current structure. Here's a workaround that would preserve the logic and could be supported with the current structure. Maybe it could be treated like that automatically?

#ifdef DO_THIS_CHECK
	if (bar)
		baz();
	else
		qux();
#else
 	qux();
#endif
Repro
#ifdef DO_THIS_CHECK
	if (bar)
		baz();
	else
#endif
		qux();

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

Start by loading the provided C++ reproduction in the Tree-sitter Playground (0.23.4?) and compare its bad parse tree with the expected behavior. Trace how preproc_ifdef and if_statement interact around the conditional else; done means the reproduction no longer marks else as ERROR and the resulting tree preserves the intended branches.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.