Repository navigation
fix(c): re-read a file with broken preprocessor syntax through a first-branch view (#5648) - #5657
Open
Deepjyoti-Sarmah wants to merge 2 commits into
Open
Deepjyoti-Sarmah wants to merge 2 commits into
Deepjyoti-Sarmah wants to merge 2 commits into
Conversation
|
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. |
1 task done
There was a problem hiding this comment.
Graphify review 
No confirmed issues · High impact
For C files with parse errors, the extractor retries parsing with a view that keeps the first conditional branch.
1 possible issue, see Graphify Cloud
What this review covered
- This review stopped before it finished; findings are from the part that ran.
- One review pass used all of its turns without returning its findings.
- A changed file's diff was too long to show in full.
- One review pass used all of its turns for reading code, so it reported from what it had already read.
- Some lookups asked for files or lines that do not exist; the review was told and carried on.
- Some findings could not be double-checked.
- +1 more notes in Graphify.
- 3 of 3 changed files inspected. This is scope, not a correctness guarantee.
Code graph · 5789 functions depend on this change
- 1 function became more coupled.
extract_c()(18 callers). - 422 of 666 test files are affected by this change.
- Gate, test list and every flagged function: in the Graphify check.
Review stopped early on a671857
Contributor
Author
|
Full suite results: None of the failures are caused by this change. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #5648. In C the preprocessor runs before the compiler, so the compiler
never sees
#ifdef,#elseor#definelines. tree-sitter-c parses theminline instead, so a conditional inside a construct breaks valid C:
On v8 this one drops
f()and its call tohelper()from the graphentirely. The other patterns in the issue (a
#definebetween enum members,an enum member or initializer item behind
#ifdef, a conditionalelse ifor
ifheader) setparse_errors. One of them (enum OptionalMode) alsotriggers the "had syntax errors" warning. Unnamed bit-fields
(
unsigned : 1;, valid per C11 6.7.2.1) are a separate grammar gap: thegrammar requires a field name.
The fix
When a
.c/.hfile has parse errors,extract_creads it a second timefrom a "first branch" view built by
_c_first_branch_view. That view replacesthe following with spaces, keeping every newline:
#if/#ifdef/#ifndef/#elif/#else/#endif,#defineand#undefline;Every byte keeps its line and column.
#includelines are kept, and commentsand strings are skipped, using the existing
_cpp_conditional_groupsand_cpp_conditional_tokenshelpers. No macro is evaluated.The view's result is used only if it keeps every node and edge of the
normal parse, and either parses cleanly or recovers more nodes. Otherwise
the result is exactly what v8 returns. So a function or call that exists only
in an
#elsebranch can never be lost by switching to the view.Two commits
Type of change
Verification & Invariants
The invariant: a C file that is valid after preprocessing loses no function
or call to the way directives are written. Re-reading the file never removes
anything the normal parse found, and never moves a source location.
Merge Danger
Door: two-way.
_AST_CACHE_SCHEMAgoes from 60 to 61, so cachedextractions are redone once after the upgrade.
Blast radius: files routed to the C extractor (
.c,.h) that alreadyhave parse errors. Clean files are never re-read. The result can only gain
nodes and edges or lose a parse error. Cost: one extra parse for such files.
Limitations
conditional, so a file that only parses with a later branch keeps today's
behaviour. Configurations are not enumerated and no macro is evaluated.
edge, the whole file falls back to the normal parse.
(
unsigned,signed,int,char,short,long,_Bool), not typedefssuch as
uint8_t : 3;.works around them inside Graphify.
How was this tested?
The issue's six patterns, each followed by a function, extracted with
extract_c(PYTHONPATHset to the worktree):tests/test_c_preprocessor_view.py, 9 tests: the six issue patterns parsecleanly, the conditional parameter list keeps
f()atL1with its call,a function only in
#elseis kept, and a clean file with#includestaysuntouched.
Not tested: Windows, real XC8 firmware trees.
Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (N/A: no skill fragments touched.)