Skip to content

fix(cpp): blank block comments inside #define directives before parsing - #5556

Closed
leninworld wants to merge 1 commit into
Graphify-Labs:v8from
leninworld:fix/5551-cpp-define-block-comments
Closed

leninworld wants to merge 1 commit into
Graphify-Labs:v8from
leninworld:fix/5551-cpp-define-block-comments

Conversation

@leninworld

Copy link
Copy Markdown
Contributor

This PR fixes issue #5551.

What does this PR do?

Fixes #5551.

tree-sitter-cpp ends a #define value (preproc_arg) at a /* ... */ comment, so whatever follows the comment in the same directive, including its backslash-continued lines, is parsed as ordinary code. That produces an ERROR node, and graphify reports the file as had syntax errors and may be partially extracted. In the issue's example the ERROR also swallows the inline of the function after the macro.

The C preprocessor replaces every comment with a space before it processes directives (translation phase 3), so blanking a block comment inside a #define doesn't change what the macro means. This PR adds _normalize_cpp_directive_comments, which runs in extract_cpp next to the existing export-macro normalization and overwrites closed block comments inside #define directives with spaces. Like the other C++ rewrites it is byte-length preserving and keeps every CR/LF in place, so offsets, lines and columns don't move. A \ that splices a line inside such a comment is kept too, so the directive still continues.

Directives are found with the existing _cpp_conditional_tokens lexer, so #define text inside strings, raw strings or block comments in normal code is not touched. Inside a directive, string, char and raw string literals, // comments and numbers with digit separators (1'000) are skipped, so a /* inside them stays as it is.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

Invariant: a comment inside a #define doesn't make graphify see a syntax error or change which symbols it extracts; the rewritten source has the same length and the same line breaks as the file on disk. Only #define directives are rewritten: #if/#elif lines with comments already parse cleanly, and #include <...> header names are left alone. No generated or persisted state changes beyond the AST cache picking up the cleaner parse.

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

Tests: the issue's RUN_ONCE reproduction (no parse errors, same symbols as the control file, no partial-extraction warning), plus a parametrized test covering a comment mid-macro, object-like macros, digit separators, two comments on one line, a comment that crosses a line splice, CRLF, an indented # define, a comment at the end and before a continuation, and cases that must stay byte-identical (/* in a string, /* */ inside a // comment, fake directives in normal-code strings, raw strings and comments, a # define line that follows a splice in normal code, // comments, #if, #include <a/*b*/c.h>). A third test checks that byte length, CR/LF positions and splices are preserved.

Limitations:

  • A block comment that is not closed inside the directive, or that spans a newline without a \ splice, is left unchanged.
  • Directives are found by the existing _cpp_conditional_tokens lexer, which allows only spaces and tabs between # and the directive name, so a form feed or vertical tab there (#\fdefine) is not recognised and is left unchanged. I didn't widen that shared lexer here because the conditional-group code uses it too.
  • A /* inside a literal in a macro value (#define S "/* not */", '/*', L"a\"/*", R"tag(/*raw*/)tag") still reports a parse error. tree-sitter-cpp reads that /* as a comment start; there is no real comment to blank, so this PR leaves those literals byte-identical and doesn't change the parse (the test records the string case).

How was this tested?

# lane venv from `uv sync --all-extras` (uv.lock), commands run as .venv/bin/<tool>
python -m pytest tests/test_cpp_nested_and_cli.py -k directive          # without the fix: the RUN_ONCE test fails (parse_errors {'first_error_line': 1, 'multiline_error': True, ...})
python -m pytest tests/test_cpp_nested_and_cli.py tests/test_partial_extraction_warning.py tests/test_cpp_conditional_initializers.py   # 98 passed
python -m pytest tests -n auto        # 12109 passed, 165 skipped; 6 failures in test_terraform_modules.py::test_copied_ast_cache_preserves_terraform_ids[parallel-*], which fail the same way on v8 under -n auto and pass when that file runs serially
ruff check .                # clean
pyright graphify/extract.py # 169 errors before and after (no new ones)
pre-commit run --files graphify/extract.py tests/test_cpp_nested_and_cli.py   # passed
python check.py c.hpp c_control.hpp   # the issue's script: both give ['real_function()'] and no syntax-error warning

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (Not applicable: no fragments changed.)
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

tree-sitter-cpp ends a #define value at a /* */ comment, so the rest of
the directive and its continued lines parse as code and the file is
reported as partially extracted. The preprocessor replaces comments with
a space before handling directives, so overwrite closed block comments in
#define spans with spaces, keeping CR/LF and line-splice backslashes so
offsets and lines stay stable.

Fixes Graphify-Labs#5551

Co-Authored-By: Codex <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @leninworld. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 27, 2026

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify review View in Graphify

Nothing blocking · 2 possible issues to check · Merge confidence 5/5

2 possible issues to check · High impact

The C++ extraction path blanks block comments found inside #define directive spans before parsing, preserving byte offsets and line splices.

2 possible issues we couldn't confirm

They don't change the score.

Code graph · 5160 functions depend on this change
  • 2 functions became more coupled. extract() (1395 callers), extract_cpp() (47 callers).
  • 374 of 642 test files are affected by this change.
  • Gate, test list and every flagged function: in the Graphify check.

Reviewed 737975f

@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.122 (authorship-preserving cherry-pick). The lexer-based approach is nicely context-aware and fail-safe. One thing added at merge: the change clears false parse_errors (an extraction-output change) but the PR didn't bump the AST cache schema, so it'd be inert on a warm cache — folded into this release's schema 57→58 bump. Thanks @leninworld!

@safishamsi safishamsi closed this Nov 28, 2026
@leninworld

Copy link
Copy Markdown
Contributor Author

Shipped in v0.9.122 (authorship-preserving cherry-pick). The lexer-based approach is nicely context-aware and fail-safe. One thing added at merge: the change clears false parse_errors (an extraction-output change) but the PR didn't bump the AST cache schema, so it'd be inert on a warm cache — folded into this release's schema 57→58 bump. Thanks @leninworld!

Thank you @safishamsi

safishamsi pushed a commit that referenced this pull request Nov 28, 2026
The class/struct export-macro rule allows a newline after the keyword,
so a `// ... class` comment acted as the keyword for the next line and
`DWORD Widget::run()` lost its return type and its references edge.
Both export-macro rules also rewrote bytes inside strings and comments.

Collect comment and string/char/raw-string spans with the #5556 lexer
regexes, as suggested on #5558, and leave matches that start inside
them alone. Directive lines are scanned for their own comments, a block
comment opened on a directive line runs to its close, and a quote that
crosses an unspliced newline is not treated as a literal. Bump the AST
cache schema to 59 so cached C++ extractions re-run.

Fixes #5581

Co-Authored-By: Codex <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit a94fc47)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants