Skip to content

Commit 5157938

Browse files
committed
build: fix PCH sanitization to actually work by using temp directory
1 parent a6c1aaf commit 5157938

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

scripts/run-clang-tidy.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import json
1313
import os
1414
import re
15+
import shutil
1516
import subprocess
1617
import sys
1718
import tempfile
@@ -129,25 +130,27 @@ def create_sanitized_compile_commands(compile_commands: List[dict],
129130
original_path: Path) -> Path:
130131
"""Create a temporary sanitized compile_commands.json file.
131132
132-
Returns the path to the temporary file that should be used with clang-tidy.
133+
Returns the path to the directory containing the sanitized compile_commands.json.
133134
"""
134-
# Create temp file in the same directory as the original for consistency
135-
temp_dir = original_path.parent
136-
temp_fd, temp_path = tempfile.mkstemp(
137-
suffix='_sanitized.json',
138-
prefix='compile_commands_',
139-
dir=temp_dir,
140-
text=True
141-
)
135+
# Create a temporary directory for the sanitized compile commands
136+
# We need the file to be named exactly "compile_commands.json" for clang-tidy -p
137+
temp_dir = Path(tempfile.mkdtemp(
138+
suffix='_clang_tidy',
139+
prefix='sanitized_',
140+
dir=original_path.parent
141+
))
142+
143+
temp_file = temp_dir / 'compile_commands.json'
142144

143145
try:
144-
with os.fdopen(temp_fd, 'w') as f:
146+
with open(temp_file, 'w') as f:
145147
json.dump(compile_commands, f, indent=2)
146-
return Path(temp_path)
148+
return temp_dir
147149
except Exception as e:
148150
# Clean up on error
149151
try:
150-
os.unlink(temp_path)
152+
import shutil
153+
shutil.rmtree(temp_dir)
151154
except:
152155
pass
153156
raise RuntimeError(f"Failed to create sanitized compile commands: {e}")
@@ -186,7 +189,7 @@ def run_clang_tidy(source_files: List[str],
186189
for batch_num, batch in enumerate(batches, 1):
187190
cmd = [
188191
'clang-tidy',
189-
f'-p={temp_compile_commands.parent}',
192+
f'-p={temp_compile_commands}',
190193
]
191194

192195
if fix:
@@ -214,12 +217,12 @@ def run_clang_tidy(source_files: List[str],
214217
return overall_returncode
215218

216219
finally:
217-
# Clean up the temporary file
220+
# Clean up the temporary directory
218221
try:
219-
temp_compile_commands.unlink()
220-
print(f"Cleaned up temporary file: {temp_compile_commands.name}")
222+
shutil.rmtree(temp_compile_commands)
223+
print(f"Cleaned up temporary directory: {temp_compile_commands.name}")
221224
except Exception as e:
222-
print(f"Warning: Could not remove temporary file {temp_compile_commands}: {e}")
225+
print(f"Warning: Could not remove temporary directory {temp_compile_commands}: {e}")
223226

224227

225228
def main():

0 commit comments

Comments
 (0)