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

bug: Parse error with macro-generated `enum` values

Open
#309 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)

tree-sitter 0.25.3 (2a835ee029dca1c325e6f1c01dbce40396f6123e)

Describe the bug

When parsing code that defines an enum using macro expansion, the parse tree incorrectly reports an ERROR node.
For example, given the following snippet:

#define ERROR_LIST(APPLY) \
    APPLY(ERR_OK, 0, "Success") \
    APPLY(ERR_FAIL, 1, "Failure")

enum Error {
  #define DEFINE(NAME, VALUE, DESC) NAME = VALUE,
      ERROR_LIST(DEFINE)
  #undef DEFINE
};

The following error appears in the parse tree: (ERROR [5, 2] - [6, 24].

Steps To Reproduce/Bad Parse Tree
  1. Write the code above in a C++ file.
  2. Run tree-sitter parse on the file.
  3. Observe the obtained parse tree:
(translation_unit [0, 0] - [10, 0]
  (preproc_function_def [0, 0] - [3, 0]
    name: (identifier [0, 8] - [0, 18])
    parameters: (preproc_params [0, 18] - [0, 25]
      (identifier [0, 19] - [0, 24]))
    value: (preproc_arg [1, 4] - [2, 33]))
  (enum_specifier [4, 0] - [8, 1]
    name: (type_identifier [4, 5] - [4, 10])
    body: (enumerator_list [4, 11] - [8, 1]
      (ERROR [5, 2] - [6, 24]
        (preproc_call [5, 2] - [6, 0]
          directive: (preproc_directive [5, 2] - [5, 9])
          argument: (preproc_arg [5, 10] - [5, 49]))
        (identifier [6, 6] - [6, 16])
        (identifier [6, 17] - [6, 23]))
      (preproc_call [7, 2] - [8, 0]
        directive: (preproc_directive [7, 2] - [7, 8])
        argument: (preproc_arg [7, 9] - [7, 15])))))
Expected Behavior/Parse Tree

The parse tree should not have errors.

Repro
#define ERROR_LIST(APPLY) \
    APPLY(ERR_OK, 0, "Success") \
    APPLY(ERR_FAIL, 1, "Failure")

enum Error {
  #define DEFINE(NAME, VALUE, DESC) NAME = VALUE,
      ERROR_LIST(DEFINE)
  #undef DEFINE
};

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 running tree-sitter parse on the provided C++ macro-and-enum snippet and inspect how the grammar handles preprocessor calls inside an enumerator_list. The work is done when the reproduced parse tree contains no ERROR node while preserving the expected enum structure.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.