Skip to content

Commit 2a3f5b9

Browse files
committed
build: add PCH flag sanitization to fix MSVC precompiled header errors
1 parent d1f024a commit 2a3f5b9

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

scripts/run-clang-tidy.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import argparse
1212
import json
1313
import os
14+
import re
1415
import subprocess
1516
import sys
1617
from pathlib import Path
@@ -56,11 +57,33 @@ def find_compile_commands(build_dir: Optional[Path] = None) -> Path:
5657
)
5758

5859

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+
5980
def load_compile_commands(compile_commands_path: Path) -> List[dict]:
6081
"""Load and parse the compile_commands.json file."""
6182
try:
6283
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]
6487
except (json.JSONDecodeError, IOError) as e:
6588
raise RuntimeError(f"Failed to load compile_commands.json: {e}")
6689

0 commit comments

Comments
 (0)