|
11 | 11 | import argparse |
12 | 12 | import json |
13 | 13 | import os |
| 14 | +import re |
14 | 15 | import subprocess |
15 | 16 | import sys |
16 | 17 | from pathlib import Path |
@@ -56,11 +57,33 @@ def find_compile_commands(build_dir: Optional[Path] = None) -> Path: |
56 | 57 | ) |
57 | 58 |
|
58 | 59 |
|
| 60 | +def sanitize_compile_command(entry: dict) -> dict: |
| 61 | + """Remove PCH and other incompatible flags for clang-tidy.""" |
| 62 | + cmd = entry.get('command', '') |
| 63 | + |
| 64 | + # Remove MSVC precompiled header flags that clang-tidy can't handle |
| 65 | + flags_to_remove = [ |
| 66 | + r'/Yu[^\s]*', # MSVC: Use precompiled header |
| 67 | + r'/Yc[^\s]*', # MSVC: Create precompiled header |
| 68 | + r'/Fp[^\s]*', # MSVC: Precompiled header file path |
| 69 | + r'-Xclang -include-pch [^\s]+', # Clang PCH |
| 70 | + r'-include-pch [^\s]+', |
| 71 | + ] |
| 72 | + |
| 73 | + for flag_pattern in flags_to_remove: |
| 74 | + cmd = re.sub(flag_pattern, '', cmd) |
| 75 | + |
| 76 | + entry['command'] = cmd |
| 77 | + return entry |
| 78 | + |
| 79 | + |
59 | 80 | def load_compile_commands(compile_commands_path: Path) -> List[dict]: |
60 | 81 | """Load and parse the compile_commands.json file.""" |
61 | 82 | try: |
62 | 83 | with open(compile_commands_path, 'r') as f: |
63 | | - return json.load(f) |
| 84 | + commands = json.load(f) |
| 85 | + # Sanitize commands to remove PCH flags |
| 86 | + return [sanitize_compile_command(cmd) for cmd in commands] |
64 | 87 | except (json.JSONDecodeError, IOError) as e: |
65 | 88 | raise RuntimeError(f"Failed to load compile_commands.json: {e}") |
66 | 89 |
|
|
0 commit comments