Files
codegraph/__tests__
danusha2345andClaude Opus 5.5 cbd9817f7c fix(extraction): a damaged C #if group no longer turns into functions named if
tree-sitter-c parses every branch of a `#if … #elif … #else … #endif`
group as a run of block items, so a group is harmless only when each
branch is complete on its own. Three shapes in real C are not: a branch
that begins with `else` (an arm of an if-chain kept behind `#ifdef`), a
branch that ends with a bare `if (…)` header whose body follows the
`#endif` (ST's HAL latency tables), and a branch whose braces don't
balance (a function head that differs per build). Each leaves a
K&R-shaped `if (…) { … }` that the grammar reads as a function named
`if`, filed inside the enclosing function, and the functions after it
were nested under it or lost. Separately, a block macro in capitals in
statement position (`ATOMIC_BLOCK(NVIC_PRIO_MAX) {`) was skipped by the
statement-macro blank, which matched lowercase iterator macros only.

blankCUnbalancedConditionalBranches runs at the end of preParseCSource,
after the directive restore, since it edits directive lines. A group
where a branch shows one of those shapes, or is a fragment of an
expression or a list (`|| (h4 == NULL)` inside an `if (…)`), loses its
directive lines, offsets kept. When a branch can't stand next to the
others, one branch is kept verbatim: the `#else` of a positive test
(`#ifdef X`, `#if defined(X)`), the first of `#ifndef`, the live one of
`#if 0`. The statement-macro blank also takes ALL_CAPS names (PascalCase
still excluded, for C++ constructors). Both engines see it: the kernel
gets the pre-parsed bytes from the TS route point.

Ours on top:
- A group on a flag whose branches each balance (braces and parens)
  keeps every branch, only its directive lines blanked: read in a row
  they are still C (`} else if (a) {…} else if (b) {…}`, list entries,
  `|| x || y`). Dropping one lost real code of builds the project uses:
  betaflight's failsafe.c arms under `#if ENABLE_RESCUE_PLAN` (default-on
  in common_post.h), and adc_stm32h7xx.c's adcTagMap, whose inner MCU
  chain kept its `#else #error` branch. Unbalanced and literal groups
  keep one branch, as above. The PR's first unit test now expects both
  arms of its balanced else-led group.
- The group is read on a copy with comments and literals masked. A brace
  inside a block comment over several lines counted as code, so Lua's
  `** {======` banners made loadlib.c's platform group look damaged, and
  its dlopen, Windows and dyld implementations were dropped for the stub;
  on betaflight it dropped 19 CMSIS-DSP MVE functions.
- A line splice is read on the raw line: a `\` at the end of a comment
  line inside a #define body still continues the #define (masking hid
  it, which made jemalloc's tsd.h include guard look damaged).
- Only a directive's own `\` continuations are blanked with it. The PR
  blanked every directive line right under a collapsed group's `#if` or
  `#else`, so a kept branch lost a leading `#include` or `#define`.
- `#if 1` keeps its first branch; it kept the dead `#else`.
- Unit tests for each, mutation-checked; CHANGELOG bullet rewritten
  without numbers.
- Tried and dropped: keeping the feature branch of a flag the file
  #defines itself (the same-file evidence PR #2615 uses). Applied per
  group, it broke dtoa's coordinated groups in jq (`#ifdef
  Avoid_Underflow` kept its branch while a later `#ifndef
  Avoid_Underflow` still kept its `}`), cutting jvp_strtod in half.

Validation, main b6379e6d vs this:
- Phantom functions (named if or (…)) removed / real functions added:
  betaflight 49679ccd 257 / 40 (spiInternalStartDMA, gyroDetect,
  processSmartPortTelemetry, esc4wayProcess, the CMSIS matrix
  routines), stm32h7xx_hal_driver 7e541d92 68 / 6 (LL_SetFlashLatency,
  HAL_ADC_AnalogWDGConfig), stm32g4xx_hal_driver a6001282 30 / 2, redis 7f440372 8 / 16 (tre_parse, je_mallocx,
  lua_cmsgpack), jq 49fbca12 1 / 11 (strptime, gethex, jvp_dtoa). No
  real function lost except jq's WIN32-only wmain. Enum members added:
  betaflight 701, stm32h7 8.
- Calls move to the real function: on betaflight, 746 calls that sat on
  a phantom or on the file node are gone, and 770 now sit on real
  functions (369 on restored ones, 401 on functions that existed but
  had lost them to a phantom). Lost: references from `#if`
  conditions inside an initializer (betaflight TIM_N x12), test-macro
  references (redis TEST x10), and a few calls in feature branches of
  unbalanced groups.
- leveldb (C++) and gin (Go) byte-identical.
- kernel-parity (betaflight, stm32h7, redis, jq): 0 files with diffs on
  main and here; fewer files defer to wasm (betaflight 1216 -> 1095,
  redis 169 -> 150, jq 21 -> 15, stm32h7 156 -> 146).
- Cost: C preParse CPU over betaflight 5.7 -> 8.6 s (the PR as written
  8.4 s), redis 1.2 -> 1.6 s.
  Whole-index CPU, median of 3 interleaved: redis 41.2 vs 40.0 s,
  stm32h7xx_hal_driver 21.4 vs 21.9 s (noise).

Original PR #1736 by @danusha2345.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 22:25:21 +00:00
..