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

bug: typedef is not treated as a general specifier

Open
#327 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

Describe the bug

In this grammar, typedefs are considered specific standalone statements, requiring the statement to start with typedef, but in the official C++ grammar, typedef is just one of many possible decl-specifiers.

This means that while e.g. typedef int x, y; is parsed correctly, int typedef x, y; is not, despite both compiling to the same effect in gcc and clang.

This is actually used in the wild, but it seems very rare. Nonetheless, I thought I'd report it in case someone's willing to bother themselves with strange and unknown C++ quirks :)

Steps To Reproduce/Bad Parse Tree

Output for int typedef x, y;:

(translation_unit [0, 0] - [1, 0]
  (declaration [0, 0] - [0, 17]
    type: (primitive_type [0, 0] - [0, 3])
    declarator: (identifier [0, 4] - [0, 11])
    (ERROR [0, 12] - [0, 13]
      (identifier [0, 12] - [0, 13]))
    declarator: (identifier [0, 15] - [0, 16])))
Expected Behavior/Parse Tree

The expected parse tree is the same as with typedef at the start, only with different locations:

(translation_unit
  (type_definition
    type: (primitive_type)
    declarator: (type_identifier)
    declarator: (type_identifier)))

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

Reproduce the issue by parsing typedef int x, y; and int typedef x, y; with the tree-sitter-cpp grammar, then inspect the declaration and declaration-specifier rules that distinguish these forms. The work is done when both inputs produce equivalent type-definition parse trees without an ERROR node.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.