|
12 | 12 | import json |
13 | 13 | import os |
14 | 14 | import re |
| 15 | +import shutil |
15 | 16 | import subprocess |
16 | 17 | import sys |
17 | 18 | import tempfile |
@@ -129,25 +130,27 @@ def create_sanitized_compile_commands(compile_commands: List[dict], |
129 | 130 | original_path: Path) -> Path: |
130 | 131 | """Create a temporary sanitized compile_commands.json file. |
131 | 132 | |
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. |
133 | 134 | """ |
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' |
142 | 144 |
|
143 | 145 | try: |
144 | | - with os.fdopen(temp_fd, 'w') as f: |
| 146 | + with open(temp_file, 'w') as f: |
145 | 147 | json.dump(compile_commands, f, indent=2) |
146 | | - return Path(temp_path) |
| 148 | + return temp_dir |
147 | 149 | except Exception as e: |
148 | 150 | # Clean up on error |
149 | 151 | try: |
150 | | - os.unlink(temp_path) |
| 152 | + import shutil |
| 153 | + shutil.rmtree(temp_dir) |
151 | 154 | except: |
152 | 155 | pass |
153 | 156 | raise RuntimeError(f"Failed to create sanitized compile commands: {e}") |
@@ -186,7 +189,7 @@ def run_clang_tidy(source_files: List[str], |
186 | 189 | for batch_num, batch in enumerate(batches, 1): |
187 | 190 | cmd = [ |
188 | 191 | 'clang-tidy', |
189 | | - f'-p={temp_compile_commands.parent}', |
| 192 | + f'-p={temp_compile_commands}', |
190 | 193 | ] |
191 | 194 |
|
192 | 195 | if fix: |
@@ -214,12 +217,12 @@ def run_clang_tidy(source_files: List[str], |
214 | 217 | return overall_returncode |
215 | 218 |
|
216 | 219 | finally: |
217 | | - # Clean up the temporary file |
| 220 | + # Clean up the temporary directory |
218 | 221 | 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}") |
221 | 224 | 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}") |
223 | 226 |
|
224 | 227 |
|
225 | 228 | def main(): |
|
0 commit comments