Skip to content

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
Graphify-Labs:v8from
Deepjyoti-Sarmah:fix/5648-c-preproc-parse-errors
Open

Deepjyoti-Sarmah wants to merge 2 commits into
Graphify-Labs:v8from
Deepjyoti-Sarmah:fix/5648-c-preproc-parse-errors

Conversation

@Deepjyoti-Sarmah

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #5648. In C the preprocessor runs before the compiler, so the compiler
never sees #ifdef, #else or #define lines. tree-sitter-c parses them
inline instead, so a conditional inside a construct breaks valid C:

void f(
#ifdef FEATURE
    int x
#else
    void
#endif
) { helper(); }

On v8 this one drops f() and its call to helper() from the graph
entirely. The other patterns in the issue (a #define between enum members,
an enum member or initializer item behind #ifdef, a conditional else if
or if header) set parse_errors. One of them (enum OptionalMode) also
triggers the "had syntax errors" warning. Unnamed bit-fields
(unsigned : 1;, valid per C11 6.7.2.1) are a separate grammar gap: the
grammar requires a field name.

The fix

When a .c/.h file has parse errors, extract_c reads it a second time
from a "first branch" view built by _c_first_branch_view. That view replaces
the following with spaces, keeping every newline:

  • every #if/#ifdef/#ifndef/#elif/#else/#endif, #define and #undef line;
  • every branch of a conditional after the first;
  • unnamed bit-field declarations, which are padding and declare no symbol.

Every byte keeps its line and column. #include lines are kept, and comments
and strings are skipped, using the existing _cpp_conditional_groups and
_cpp_conditional_tokens helpers. 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 #else branch can never be lost by switching to the view.

Two commits

a7b1ceca  fix(5648): re-read a C file with broken preprocessor syntax through a first-branch view
a6718570  test(5648): pin C preprocessor and unnamed bit-field parse recovery

Type of change

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

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.

  • 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.

Merge Danger

Door: two-way. _AST_CACHE_SCHEMA goes from 60 to 61, so cached
extractions are redone once after the upgrade.

Blast radius: files routed to the C extractor (.c, .h) that already
have 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

  • Branch-wise syntax only: the view keeps the first branch of each
    conditional, so a file that only parses with a later branch keeps today's
    behaviour. Configurations are not enumerated and no macro is evaluated.
  • The gate is all-or-nothing per file. If the view would lose any node or
    edge, the whole file falls back to the normal parse.
  • Unnamed bit-fields are recognised only with built-in integer types
    (unsigned, signed, int, char, short, long, _Bool), not typedefs
    such as uint8_t : 3;.
  • C++ is untouched; it has its own conditional recovery.
  • Upstream grammar issues (tree-sitter-c#108, hook install hardcodes python3, breaks in Nix/pipx environments #324) remain open; this only
    works around them inside Graphify.

How was this tested?

The issue's six patterns, each followed by a function, extracted with
extract_c (PYTHONPATH set to the worktree):

                       v8 (d249de36)                         this branch
params.c               f() missing, no calls edge, warning   f() L1 + f -> helper
enumopt.c              parse error, warning                  clean
bits / define_in_enum  parse error                           clean
initializers           parse error                           clean
else_if / if_header    parse error                           clean

tests/test_c_preprocessor_view.py, 9 tests: the six issue patterns parse
cleanly, the conditional parameter list keeps f() at L1 with its call,
a function only in #else is kept, and a clean file with #include stays
untouched.

.venv/bin/python -m pytest -q tests/test_c_preprocessor_view.py
    v8 code      -> 7 failed, 2 passed (the 2 are the guard tests)
    this branch  -> 9 passed
.venv/bin/python -m pytest -q tests
    still running at the time of opening; results follow in a comment
.venv/bin/python -m ruff check <changed files>
    -> All checks passed

Not tested: Windows, real XC8 firmware trees.

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (N/A: no skill fragments touched.)
  • 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. (No trailer in the commit messages.)

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

Copy link
Copy Markdown

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.

@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 · 1 possible issue to check · Merge confidence 5/5

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

@Deepjyoti-Sarmah

Copy link
Copy Markdown
Contributor Author

Full suite results:

.venv/bin/python -m pytest -q tests
    this branch -> 121 failed, 12525 passed, 123 skipped, 2 xfailed
    the failures are in 19 files, none of them C (cross-repo, export, hook/CLI, ...)
    those 19 files: this branch 120 failed, 202 passed
                    v8 code     120 failed, 202 passed
    (the one extra failure in the full run did not repeat)

None of the failures are caused by this change.

This branch has not been deployed

No deployments
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.

C extraction: valid unnamed bit-fields and preprocessing patterns trigger parse errors

1 participant