fix: scan #else branches of unknown #if/#ifdef blocks in the macro shield (#1720) - #1764
Conversation
…ield (squid-protocol#1720) The preprocessor shield in _build_brace_safe_stream assumed the first branch of every #if/#ifdef was the active one and blindly blanked the #else branch. Any real implementation living in #else was silently dropped from extraction, even though tree-sitter ground truth parses both branches. Replace the in_dead_branch flag with a per-open-#if policy stack: - #if 1 / #if true -> first branch alive, #else dead - #if 0 / #if false -> first branch dead, #else alive - anything else (macro name, defined(X), expression) -> scan BOTH branches Nested blocks honor the outer branch's liveness via any() over the stack.
…quid-protocol#1720) Re-running the pinned language-crucible corpus in both dependency modes after the shield change shifts the c/cpp snapshots: implementations that were previously blanked out of #else branches are now extracted (godot main.cpp's Main::test_setup/test_cleanup, rendering_server_default.h's redraw_request, gdscript_vm.cpp's OPCODE/OPCODE_WHILE/OPCODE_SWITCH, cpython frameobject.c, and others). Both golden crucible modes pass against the updated fixtures.
|
Closes #1720 |
|
Nice fix for #1720, well-evidenced and correctly reblessed via the sanctioned Bug: no-space
|
squid-protocol
left a comment
There was a problem hiding this comment.
LGTM! I added the \b word-boundary fix for the #if( spacing issue as requested. Approving!
…-branch-1720 # Conflicts: # tests/golden_master_audit.json # tests/golden_master_zero_dep_audit.json
squid-protocol
left a comment
There was a problem hiding this comment.
LGTM! I added the \b word-boundary fix for the #if( spacing issue as requested. Approving!
Summary
Closes #1720.
The macro shield in
_build_brace_safe_stream(gitgalaxy/core/detector.py) assumed the first branch of every#if/#ifdefwas the active one and blindly blanked the#elsebranch:That's wrong for the common case where the condition is a macro name the engine can't evaluate (
#if PLATFORM_WINDOWS,#ifdef DEBUG): the#elsebranch is just as likely to hold the real implementation, and it was scrubbed before the regex engine ever saw it. Tree-sitter ground truth parses both branches.The fix
Replaced the single
in_dead_branchflag with a per-open-#ifpolicy stack, where each entry is(static_truth, side):#if 1/#if true→ first branch alive,#elsedead#if 0/#if false→ first branch dead,#elsealivedefined(X), expressions) → both branches scannedA new
_classify_preproc_conditionhelper returns the static truth value (stripping C comments first). Nested blocks stay dead when any enclosing#ifis statically dead (any()over the stack), and#endifpops restore prior state.#elifstarts a fresh condition on the else side.Evidence on the pinned corpus
godot/main.cpp—Main::test_setup(130-line body) andMain::test_cleanupwere being truncated to stubsgodot/rendering_server_default.h— theredraw_requestdeclared in an#elseblock is now extractedgodot/gdscript_vm.cpp—OPCODE/switch handlers behind unknown#ifs now show upcpython/frameobject.c— same class of recovery on the C sideTests
test_detector_c_macro_else_branch_is_scanned_issue_1720— regression test for the exact cpp: func_recall_pct - Naive #else Macro Shielding #1720 shapetest_detector_c_macro_static_truth_prunes_branches— pins#if 0/#if 1pruning in both directions plus nested livenesstests/tools/update_golden_master.py(the sanctioned path, showing the full diff);pytest -m golden_cruciblepasses in full-precision and zero-dependency modesaudit_check.py: ruff clean, dead-key OK, ast-accuracy OKtree_sitter_accuracy_audit --lang cpp: no regressions