@@ -198,6 +198,7 @@ def process_file(filepath, dry_run=False, verbose=False):
198198 in_asm_block = False
199199 asm_brace_depth = 0
200200 asm_pending = False
201+ pp_depth = 0
201202
202203 for line_idx , line in enumerate (lines ):
203204 # Track block comments manually for reliability
@@ -249,6 +250,12 @@ def process_file(filepath, dry_run=False, verbose=False):
249250
250251 # Preprocessor directives - always column 0
251252 if stripped .startswith ('#' ):
253+ directive = stripped .lstrip ('#' ).lstrip ()
254+ if directive .startswith (('if ' , 'if\t ' , 'ifdef ' , 'ifdef\t ' ,
255+ 'ifndef ' , 'ifndef\t ' )):
256+ pp_depth += 1
257+ elif directive .startswith ('endif' ):
258+ pp_depth = max (0 , pp_depth - 1 )
252259 new_lines .append (line )
253260 in_macro_continuation = line_continues_macro
254261 continue
@@ -386,9 +393,16 @@ def process_file(filepath, dry_run=False, verbose=False):
386393 # Sanity check: if the line had significant indentation but we
387394 # computed depth 0, something is likely wrong (parse errors nearby,
388395 # inline assembly corrupting the AST, etc). Skip to be safe.
389- # Threshold of 4 spaces catches most misparses while allowing
390- # legitimate depth-0 code with minor (1-3 space) indentation errors.
396+ # Also skip depth-0 lines inside preprocessor blocks — the codebase
397+ # convention is to indent C++ code inside #if/#ifdef blocks, but
398+ # tree-sitter doesn't model preprocessor nesting.
391399 space_count = len (leading_ws )
400+ if depth == 0 and space_count > 0 and pp_depth > 0 :
401+ new_lines .append (line )
402+ skipped += 1
403+ if verbose :
404+ print (f" SKIP pp-indent L{ line_idx + 1 } : depth=0 but pp_depth={ pp_depth } : { line .rstrip ()[:80 ]} " )
405+ continue
392406 if depth == 0 and space_count >= 4 :
393407 new_lines .append (line )
394408 skipped += 1
0 commit comments