Skip to content

C++: a // ... class comment makes the export-macro rule blank the next line's return type (and the macro rules rewrite comments and strings) #5581

Done

Description

@leninworld

What happened?

_normalize_cpp_export_macros blanks export macros with two raw-byte regexes (the class/struct MACRO Name rule from #3661 and the declaration rule from #5550). Neither skips comments or string literals, as noted on #5558. Most of the time this only rewrites bytes inside a comment, but the class/struct rule allows a newline between the keyword and the macro. So a // comment that ends in "class" or "struct" works as the keyword for the next line.

// Implementation of the Widget class
DWORD Widget::run() { return 0; }

The rule matches class + newline + DWORD + Widget + : and blanks DWORD. The function loses its return type: the references edge from Widget::run() to DWORD and the DWORD node are gone. Without the comment line both are there.

Win32-style code with ALL-CAPS return types (DWORD, HRESULT, BOOL) and out-of-line method definitions after a comment like this is common.

Steps to reproduce

printf '// Implementation of the Widget class\nDWORD Widget::run() { return 0; }\n' > g.cpp
printf 'DWORD Widget::run() { return 0; }\n' > h.cpp
python - <<'PY'
from pathlib import Path
from graphify.extract import extract_cpp
for n in ("g.cpp", "h.cpp"):
    r = extract_cpp(Path(n).resolve())
    print(n, sorted(x["label"] for x in r["nodes"]), sorted(e["relation"] for e in r["edges"]))
PY

Error output or graph output

g.cpp ['Widget::run()', 'g.cpp'] ['contains']
h.cpp ['DWORD', 'Widget::run()', 'h.cpp'] ['contains', 'references']

The declaration rule also changes bytes inside literals and comments ("see MYLIB_API docs" becomes "see docs", and the same happens in // MYLIB_EXPORT ... and inside a raw string), but I could not find a graph difference from that.

Graphify version

v8 at a7295bc (0.9.122).

Operating System

macOS (arm64), tree-sitter-cpp from uv.lock.

Proposed fix

As suggested on #5558: get the comment and string/char/raw-string spans from the existing _cpp_conditional_tokens lexer (#5556) and leave a match alone when it starts inside one of them. Code-level matches stay as they are. Since this changes extraction output, the AST cache schema needs a bump.

Activity

  1. github-actions commented on Nov 28, 2026

    @github-actions

    Thanks for opening this issue, @leninworld. 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
    d48507c
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