Skip to content

[Bug]: C++: a /* ... */ comment on a backslash-continued #define line is reported as a syntax error #5551

Done

Description

@z103cb

What happened?

A block comment on a line of a multi-line macro definition makes tree-sitter-cpp produce an ERROR node, and graphify then reports the file as had syntax errors and may be partially extracted:

#define RUN_ONCE(x)                 \
    do                              \
    {                               \
        run(x); /* the comment */   \
    } while(0)

The same macro without the comment parses cleanly. Trailing explanatory comments on macro lines are common in real code.

In every case I tried, the symbols after the macro were still extracted, so I could not find a case where this loses symbols. The effect is a false syntax errors ... may be partially extracted warning. In a large corpus those warnings are the signal to look for files that really are partially extracted, so false ones hide them.

Steps to reproduce

mkdir graphify-define-repro && cd graphify-define-repro

cat > c.hpp <<'SRC'
#pragma once

#define RUN_ONCE(x)                 \
    do                              \
    {                               \
        run(x); /* the comment */   \
    } while(0)

inline void real_function() {}
SRC
sed 's#/\* the comment \*/#                #' c.hpp > c_control.hpp

cat > check.py <<'SRC'
import sys
from pathlib import Path
from graphify.extract import extract

for name in sys.argv[1:]:
    out = extract([Path(name).resolve()], cache_root=Path(".cache_" + name), root=Path("."), parallel=False)
    print(name, len(out["nodes"]), "nodes", sorted(n["label"] for n in out["nodes"]))
SRC

python check.py c.hpp c_control.hpp

Error output or graph output

On 0.9.111:

warning: 1 file(s) had syntax errors and may be partially extracted: c.hpp (error region at line 3, 1 symbol(s) extracted)
c.hpp 2 nodes ['c.hpp', 'real_function()']
c_control.hpp 2 nodes ['c_control.hpp', 'real_function()']

The control file produces no warning. The extracted nodes are identical, so only the warning differs. On a real ~450-line header with a macro like the one above, the output was the same shape: error region at line 419, 42 symbol(s) extracted before, no warning after, and the same 43 nodes both ways.

Graphify version

Reproduced on current upstream/v8 (0.9.121) and on 0.9.111.

Operating System

macOS (arm64). Python 3.15.2, tree-sitter 0.25.2, tree-sitter-cpp 0.23.4.

Proposed fix

In the same byte-length-preserving normalization hook, overwrite single-line /* ... */ comments that sit inside a backslash-continued #define with spaces (newlines are kept, so every offset, line and column is unchanged):

+# A multi-line ``#define`` (backslash continuations). tree-sitter-cpp fails on a
+# ``/* ... */`` comment inside one, so the comments are blanked (offset-preserving).
+_CPP_DEFINE_RE = re.compile(rb"^[ \t]*#[ \t]*define\b(?:[^\n]*\\\r?\n)+[^\n]*", re.MULTILINE)
+_CPP_BLOCK_COMMENT_RE = re.compile(rb"/\*[^\r\n]*?\*/")
...
+    source = _CPP_DEFINE_RE.sub(
+        lambda m: _CPP_BLOCK_COMMENT_RE.sub(
+            lambda c: re.sub(rb"[^\r\n]", b" ", c.group(0)), m.group(0)
+        ),
+        source,
+    )

Limits of this sketch, for discussion:

  • It handles comments that open and close on one line. A block comment that spans several continued lines is not handled (I did not test it).
  • Only comments inside a #define are touched, so a /* ... */ that is part of a string or character literal on a macro line would be blanked too; I did not test whether that matters in practice.
  • I did not test // comments on continued lines.

I am happy to turn this into a PR with a regression test (the two-file pair above).

Activity

  1. github-actions commented on Nov 27, 2026

    @github-actions

    Thanks for opening this issue, @z103cb. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

  2. added a commit that references this issue on Nov 28, 2026
    57100ed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions