with open(r'd:\20_AI\LSPi\libs\thirdparty\rt-thread\include\rtdef.h', 'r') as f: lines = f.readlines() stack = [] for i, line in enumerate(lines, 1): stripped = line.strip() if stripped.startswith('#if ') or stripped.startswith('#ifdef ') or stripped.startswith('#ifndef '): stack.append((i, stripped)) print(f' + Line {i}: {stripped} (depth={len(stack)})') elif stripped.startswith('#elif') or stripped.startswith('#else'): if stack: print(f' ~ Line {i}: {stripped} (replaces line {stack[-1][0]}: {stack[-1][1]})') else: print(f' ! Line {i}: {stripped} (no matching #if!)') elif stripped.startswith('#endif'): if not stack: print(f' ! Line {i}: EXTRA #endif (no matching #if)') else: start_line, start_directive = stack.pop() print(f' - Line {i}: #endif closes line {start_line}: {start_directive} (depth={len(stack)})') if stack: print(f'\nERROR: {len(stack)} unmatched #if/#ifdef/#ifndef:') for line_no, directive in stack: print(f' Line {line_no}: {directive}') else: print('\nAll #if directives are properly closed.')