This script is no good.
This commit is contained in:
@@ -1,73 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
"""
|
|
||||||
Trim unnecessary #include lines from a C++ header file.
|
|
||||||
|
|
||||||
Usage: python3 tools/trim-handler.py <file>
|
|
||||||
|
|
||||||
For each #include line, temporarily removes it and runs clangd diagnostics.
|
|
||||||
If no errors result, the include is marked unnecessary. At the end, rewrites
|
|
||||||
the file with all unnecessary includes removed.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import subprocess
|
|
||||||
import os
|
|
||||||
|
|
||||||
def read_lines(path):
|
|
||||||
with open(path, 'r') as f:
|
|
||||||
return f.readlines()
|
|
||||||
|
|
||||||
def write_lines(path, lines):
|
|
||||||
with open(path, 'w') as f:
|
|
||||||
f.writelines(lines)
|
|
||||||
|
|
||||||
def has_errors(path):
|
|
||||||
result = subprocess.run(
|
|
||||||
['python3', 'tools/clangd-query.py', 'diagnostics', path],
|
|
||||||
capture_output=True, text=True
|
|
||||||
)
|
|
||||||
output = result.stdout.strip()
|
|
||||||
return output != "No problems found."
|
|
||||||
|
|
||||||
def main():
|
|
||||||
if len(sys.argv) != 2:
|
|
||||||
print("Usage: python3 tools/trim-handler.py <file>")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
path = os.path.abspath(sys.argv[1])
|
|
||||||
original_lines = read_lines(path)
|
|
||||||
|
|
||||||
# First, verify the file starts with no errors.
|
|
||||||
if has_errors(path):
|
|
||||||
print(f"ERROR: {path} already has diagnostics errors. Fix those first.")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
unnecessary = set()
|
|
||||||
|
|
||||||
for i, line in enumerate(original_lines):
|
|
||||||
if not line.strip().startswith('#include'):
|
|
||||||
continue
|
|
||||||
# Skip the .generated.h include — always required by UHT.
|
|
||||||
if '.generated.h' in line:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Rewrite the file without this line.
|
|
||||||
test_lines = original_lines[:i] + original_lines[i+1:]
|
|
||||||
write_lines(path, test_lines)
|
|
||||||
|
|
||||||
if has_errors(path):
|
|
||||||
print(f" KEEP: {line.strip()}")
|
|
||||||
else:
|
|
||||||
print(f" DROP: {line.strip()}")
|
|
||||||
unnecessary.add(i)
|
|
||||||
|
|
||||||
# Restore the original file before testing the next include.
|
|
||||||
write_lines(path, original_lines)
|
|
||||||
|
|
||||||
# Final rewrite with unnecessary includes removed.
|
|
||||||
final_lines = [line for i, line in enumerate(original_lines) if i not in unnecessary]
|
|
||||||
write_lines(path, final_lines)
|
|
||||||
print(f"\nRemoved {len(unnecessary)} unnecessary include(s) from {path}")
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
||||||
Reference in New Issue
Block a user