From 50f4cb63ffb34fb8ab98856646306c579f8ca976 Mon Sep 17 00:00:00 2001 From: huxin <1548973853@qq.com> Date: Wed, 5 Aug 2026 15:45:10 +0000 Subject: [PATCH] Fix v1 instrumentation breaking if-else chains. Inserting distance_instrument() before an else/else-if clause produces an 'else without a previous if' compile error; place the call inside the else block instead. Co-authored-by: Cursor --- trigfuzz/instrument.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/trigfuzz/instrument.py b/trigfuzz/instrument.py index 7b1e00f..671988d 100644 --- a/trigfuzz/instrument.py +++ b/trigfuzz/instrument.py @@ -66,10 +66,28 @@ def instrument_v1(tcus: list[TCU], source_root: pathlib.Path) -> None: # prepend, so iterate in reverse. for lineno in sorted(line_map, reverse=True): calls = [_v1_call(t) for t in line_map[lineno]] - # Preserve indentation of the target line for readability. - indent = _indent(lines[lineno - 1]) + target_line = lines[lineno - 1] + # If the target line is an else/else-if clause, inserting before + # it would break the if-else chain ("else without a previous if"). + # Instead, insert the instrument call inside the block body. + if target_line.lstrip().startswith("else"): + # Find the opening brace on this line or the next, then + # insert right after it. + insert_pos = lineno # default: insert on the line after else + brace_line = target_line + search_offset = 0 + while "{" not in brace_line: + search_offset += 1 + if lineno - 1 + search_offset >= len(lines): + break + brace_line = lines[lineno - 1 + search_offset] + insert_pos = lineno + search_offset + indent = _indent(target_line) + " " + else: + insert_pos = lineno - 1 + indent = _indent(target_line) stitched = "".join(indent + c + "\n" for c in calls) - lines.insert(lineno - 1, stitched) + lines.insert(insert_pos, stitched) # Inject the #include once at the top if missing. if '#include "distance.h"' not in "".join(lines[:20]):