@@ -184,6 +184,7 @@ def process_file(filepath, dry_run=False, verbose=False):
184184 in_asm_block = False
185185 asm_brace_depth = 0
186186 asm_pending = False
187+ pp_depth = 0
187188
188189 for line_idx , line in enumerate (lines ):
189190 # Track block comments manually for reliability
@@ -235,6 +236,13 @@ def process_file(filepath, dry_run=False, verbose=False):
235236
236237 # Preprocessor directives - always column 0
237238 if stripped .startswith ('#' ):
239+ # Track preprocessor nesting depth
240+ directive = stripped .lstrip ('#' ).lstrip ()
241+ if directive .startswith (('if ' , 'if\t ' , 'ifdef ' , 'ifdef\t ' ,
242+ 'ifndef ' , 'ifndef\t ' )):
243+ pp_depth += 1
244+ elif directive .startswith ('endif' ):
245+ pp_depth = max (0 , pp_depth - 1 )
238246 new_lines .append (line )
239247 in_macro_continuation = line_continues_macro
240248 continue
@@ -373,9 +381,16 @@ def process_file(filepath, dry_run=False, verbose=False):
373381 # Sanity check: if the line had significant indentation but we
374382 # computed depth 0, something is likely wrong (parse errors nearby,
375383 # inline assembly corrupting the AST, etc). Skip to be safe.
376- # Threshold of 4 spaces catches most misparses while allowing
377- # legitimate depth-0 code with minor (1-3 space) indentation errors.
384+ # Also skip depth-0 lines inside preprocessor blocks — the codebase
385+ # convention is to indent C++ code inside #if/#ifdef blocks, but
386+ # tree-sitter doesn't model preprocessor nesting.
378387 space_count = len (leading_ws )
388+ if depth == 0 and space_count > 0 and pp_depth > 0 :
389+ new_lines .append (line )
390+ skipped += 1
391+ if verbose :
392+ print (f" SKIP pp-indent L{ line_idx + 1 } : depth=0 but pp_depth={ pp_depth } : { line .rstrip ()[:80 ]} " )
393+ continue
379394 if depth == 0 and space_count >= 4 :
380395 new_lines .append (line )
381396 skipped += 1
0 commit comments