Repository navigation
fix(core): blank statically-dead C preprocessor branches before counting (#2814) - #2950
Merged
Merged
Conversation
…ing -- closes #2814 The count path and the boundary path disagreed about dead branches. The #1720 macro shield in `_build_brace_safe_stream` prunes `#if 0` / dead-side `#if 1` branches for function-boundary detection, but `coding_analysis` ran every rule regex against the raw segment -- so a `strcpy` inside `#if 0` was counted at file level (`equations`), in `threat_locations`, and (via the raw `spatial_map` offsets) in each function's `hit_vector`. #2813 (raw counts) made this visible: the old test read 0 only because a same-function `strncpy` dampener was subtracted in place. Add `_blank_dead_preproc_branches`, applied C-family-gated in `coding_analysis` before the rule loop. It blanks only the *body* of statically-dead branches (length-preserving, so all offsets stay valid), leaving the branch's own `#if/#elif/#else/#endif` markers and every live directive intact -- so rules that legitimately match directives (cpp `import` on `#include`, csharp `safety_bypasses` on `#pragma`, objective-c `import` on `#import`) are unaffected. Unknown conditions (`#if FOO`, `#ifdef`, `#if defined(X)`) keep both branches counted, matching the #1720 boundary semantics. Both paths now share `_C_FAMILY_MACRO_LANGS` so they cannot drift. STREAM_CONTRACT gains corollary 4 documenting this; the dead-branch test is rewritten to pin `#if 0` -> 0, `#if defined(X)` -> 1. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01WrjQkxP2mdzRzaJvkDS8be
Contributor
…sarial test The #2814 change drops the counts of code inside statically-dead C preprocessor branches, so the golden crucible drifted on 27 C-family corpus files that carry `#if 0` / dead-`#if 1` regions (e.g. cosmopolitan/loader.c's two unused static helpers behind `#if 0`, cpython/dictobject.c, sqlite, godot, gnucobol_internals/*.c, and the embedded `.c` in scheme/racket). Every drifted value is a reduction and every drifted file is C-family (or a directory-group average that contains a C file); no `.scm/.rkt/.py/.js` etc. entry changed. Regenerated both fixtures in isolated venvs matching each CI leg (full-precision: networkx+tiktoken+pandas+xgboost; zero-dependency: PyYAML only) and confirmed the crucible test passes green against both. Also add `test_detector_c_macro_dead_branch_nesting_and_else` pinning the tricky shapes: a dead `#if 0` nested in a live `#if 1`, the dead side of `#if 1`/`#else`, and an `#ifndef` header guard (unknown -> both sides live). Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01WrjQkxP2mdzRzaJvkDS8be
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 & why
Closes #2814 (stream-layer child of #2812). A rule hit inside a statically-dead C
#if 0branch was counted at file level (equations), inthreat_locations, and in each function'shit_vector— even though the engine already treats#if 0as dead when it slices function boundaries.The two paths disagreed:
_build_brace_safe_stream's#1720macro shield blanks#if 0/ the dead side of#if 1, so a function inside a dead branch is never sliced.coding_analysis()ran every rule regex against the raw segment, so the dead-branch hit was counted anyway.#2813 (raw recorded counts) made this visible: the old test passed for the wrong reason — a same-function
strncpydampener netted the deadstrcpyto 0 in place.Change
_blank_dead_preproc_branches, applied C-family-gated incoding_analysisbefore the rule loop. It blanks only the body of statically-dead branches (length-preserving → allspatial_map/threat_locationsoffsets stay valid, so_calculate_block_metricsneeds no change).#if/#elif/#else/#endifmarkers and every live directive intact, so rules that match directives keep working: cppimport(#include), csharpsafety_bypasses(#pragma warning disable), objective-cimport(#import).#if FOO,#ifdef X,#if defined(X)) keep both branches counted, matching the#1720boundary semantics — the engine can't decide them without a macro table._C_FAMILY_MACRO_LANGSso the gate can't drift.STREAM_CONTRACTgains corollary 4 (rendered intodocs/signal_contracts.md); the dead-branch test is rewritten to pin#if 0→ 0 and#if defined(X)→ 1, plus a new nesting/#else/header-guard test.Golden master regeneration (why the crucible output changed)
Recorded counts for C-family files with dead branches now drop, so the golden crucible drifted on 27 corpus files carrying
#if 0/ dead-#if 1regions — e.g.cosmopolitan/loader.c(two unused static helpers,StrRChr/BaseName, behind#if 0),cpython/dictobject.c,sqlite,godot,gnucobol_internals/*.c, and the embedded.cfiles inscheme/racket. Every drifted value is a reduction, and every drifted file is C-family (or a directory-group average that contains a C file); no.scm/.rkt/.py/.jsetc. entry changed. Bothgolden_master_audit.jsonandgolden_master_zero_dep_audit.jsonwere regenerated in isolated venvs matching each CI leg and the crucible test passes green against both.Verification
tests/core_engine/— full suite green (incl.#1720/#1764/static-truth/silencer siblings and the new nesting test).signal_contract_audit.py --cigate green.c/cppregistry:system()in#if 0→ not counted; live#includestill counted.Opened as draft so the
pull_request-only mypy/ruff audits gate before merge.🤖 Generated with Claude Code
https://claude.ai/code/session_01WrjQkxP2mdzRzaJvkDS8be