Repository navigation
fix(cpp): blank an export macro leading a function or variable declaration (#5550) - #5558
Deepjyoti-Sarmah wants to merge 2 commits into
Conversation
…claration tree-sitter-cpp reads `MYLIB_EXPORT uint64_t f(int);` as a declaration of type MYLIB_EXPORT with uint64_t as an ERROR. A prototype then reports a false syntax error, and a definition gains a phantom MYLIB_EXPORT node. The parse tree cannot tell which of two adjacent words is the macro (`DWORD WINAPI run()` has the same shape reversed), so match the conventional spellings only: a name ending in EXPORT or _API with a non-empty prefix, followed by another identifier. Blank it with spaces so offsets stay unchanged. Preprocessor lines and the word after class/struct/union/enum are left alone. Bump the AST cache schema to 58.
|
Thanks for the pull request, @Deepjyoti-Sarmah. 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 
1 bug · 4 possible issues to check · High impact
The C++ normalizer blanks selected uppercase names before parsing, using their spelling and nearby bytes to identify export macros.
A legitimate type name ending in
_API gets erased · extract.py:3670
4 possible issues we couldn't confirm
- Enum constants whose names end in
_APIorEXPORTlose their names when used incaselabels or?:expressions ·graphify/extract.py:3671 - Phantom-node test calls extract_cpp without importing it locally, so it can fail with NameError before checking anything ·
tests/test_cpp_nested_and_cli.py:497 - Phantom-node test compares only label counts, so it still passes if the real functions are dropped from both files ·
tests/test_cpp_nested_and_cli.py:500 - +1 more in Graphify
They don't change the score.
What this review covered
- A changed file's diff was too long to show in full, so the review may not have seen every changed line in it.
- One review pass used all of its turns for reading code, so it reported from what it had already read.
- 3 of 3 changed files inspected. This is scope, not a correctness guarantee.
Code graph · 5635 functions depend on this change
- 1 function became more coupled.
extract_cpp()(46 callers). - 406 of 642 test files are affected by this change.
- Gate, test list and every flagged function: in the Graphify check.
Reviewed 410aa91
| # the conventional spellings are matched: a name ending in ``EXPORT`` or | ||
| # ``_API`` with a non-empty prefix, followed by another identifier. | ||
| _CPP_EXPORT_DECL_MACRO_RE = re.compile( | ||
| rb"(?<![A-Za-z0-9_])[A-Z][A-Z0-9_]*(?:EXPORT|_API)(?![A-Za-z0-9_])" |
There was a problem hiding this comment.
A legitimate type name ending in
_API gets erased
A C++ type alias whose name ends in _API can be removed from a declaration, causing the declaration to be misparsed and omitted from extraction.
Refine _CPP_EXPORT_DECL_MACRO_RE in graphify/extract.py to preserve legitimate _API type aliases.
Evidence · checked against the code
Expected: The alias remains in the source passed to the parser, allowing the declaration to be parsed and extracted.
Actually: The normalizer replaces the alias name with spaces before parsing.
- Independent source review: The alias definition itself is not blanked because
=does not satisfy the regex lookahead. The later alias use is blanked; the helper's guards only preserve preprocessor-line matches and names following an elaborated type keyword, neither of which applies toFOO_API value;. Existing tests cover intended export-macro normalization and those guards, but do not check this alias-use case. - No execution of a reproducer or additional source lookups was performed.
- The precise extracted result depends on the C++ parser's recovery for the remaining declaration.
Prompt to fix with AI
In graphify/extract.py, _CPP_EXPORT_DECL_MACRO_RE (lines 3667-3670):
Problem: A legitimate type name ending in `_API` gets erased
Expected: The alias remains in the source passed to the parser, allowing the declaration to be parsed and extracted.
Actually: The normalizer replaces the alias name with spaces before parsing.
Fix it at the cause, and add a test that fails without the fix.
Graphify · Checked against the code · View in Graphify
|
Shipped in v0.9.122 (authorship-preserving cherry-pick). Byte-length-preserving and fails safe. One small follow-up worth considering: the macro blanking is a raw-byte regex that doesn't skip string literals / comments (unlike #5556's lexer approach) — harmless and consistent with the existing |
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)
What does this PR do?
Fixes #5550. An export macro in front of a free function or variable
declaration breaks the C++ parse:
tree-sitter-cpp has no rule for a bare macro before a return type. It reads
the macro as the type and the real type as an ERROR:
That one misreading gives both symptoms from the issue. A prototype reports
had syntax errors ... no symbols extracted, and a definition gains a phantomMYLIB_EXPORTnode, because the macro became the return type._normalize_cpp_export_macrosalready blanks a macro betweenclass/structand the type name (#3648/#3661). This adds a second pattern for the
declaration case and blanks the macro with spaces, so offsets, lines and
columns stay unchanged.
Why match by name
The parse tree cannot say which of two adjacent words is the macro.
DWORD WINAPI run(void*);gives the same tree, but there the second word isthe macro. So the pattern goes by the usual spelling, as the issue proposed:
an ALL-CAPS name ending in
EXPORTor_APIwith a non-empty prefix,followed by another identifier.
It is not limited to the start of a line. These shapes give the same broken
tree, and the line-start sketch in the issue missed them:
Left alone:
The
class/struct/union/enumguard keepstest_cpp_export_macro_does_not_break_variablespassing. Without it the newpattern blanked
MODULE_APIinclass MODULE_API var2{1};, where it namesthe type.
Two commits
Type of change
Verification & Invariants
The invariant: a C++ export macro the compiler would remove adds no node and
no parse error, and normalization keeps every byte offset of the file.
Merge Danger
Door: two-way.
_AST_CACHE_SCHEMAgoes from 57 to 58, so cachedextractions re-run once after upgrade.
Blast radius: C++ files (
.cpp/.cc/.cxx/.hpp) only. A definitionbehind an export macro loses its phantom macro node. A file of such
prototypes loses its false syntax-error warning.
Limitations
EXPORTor_API. A project that spells its macrodifferently (
FOO_DLL,FOO_PUBLIC) is not covered.FOO_APIbefore a variable would be blanked. Ifound no such type in practice.
.hfiles go to the C extractor, so the same declaration in a.hstillreports the syntax error. Out of scope, as the issue notes.
__declspec(dllexport),__attribute__((...))) stayunsupported, as before.
How was this tested?
The issue's reproduction script, with
PYTHONPATHset to the worktree:Three new tests in
tests/test_cpp_nested_and_cli.py: no ERROR for threedeclaration shapes, no phantom node on a definition, and offsets kept with
#define,WINAPIand a bareEXPORTuntouched.Not tested: Windows, MSVC-only headers.
Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (N/A: no skill fragments touched.)