Repository navigation
fix(cpp): blank block comments inside #define directives before parsing - #5556
leninworld wants to merge 1 commit into
Conversation
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]>
|
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. |
There was a problem hiding this comment.
Graphify review 
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
- Test expects a macro defined as a string literal to fail parsing, which is likely a wrong expectation ·
tests/test_cpp_nested_and_cli.py:519 - A block comment inside a continued
#ifline is left unblanked, and the test locks that in ·tests/test_cpp_nested_and_cli.py:549
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
|
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 |
Thank you @safishamsi |
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)
This PR fixes issue #5551.
What does this PR do?
Fixes #5551.
tree-sitter-cpp ends a
#definevalue (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 ashad syntax errors and may be partially extracted. In the issue's example the ERROR also swallows theinlineof 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
#definedoesn't change what the macro means. This PR adds_normalize_cpp_directive_comments, which runs inextract_cppnext to the existing export-macro normalization and overwrites closed block comments inside#definedirectives 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_tokenslexer, so#definetext 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
Verification & Invariants
Invariant: a comment inside a
#definedoesn'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#definedirectives are rewritten:#if/#eliflines 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.Tests: the issue's
RUN_ONCEreproduction (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# defineline 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:
\splice, is left unchanged._cpp_conditional_tokenslexer, 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./*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?
Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (Not applicable: no fragments changed.)