forked from mongodb/mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply_clang_tidy_fixes.py
executable file
·94 lines (75 loc) · 3.41 KB
/
apply_clang_tidy_fixes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
"""Applies fixes, generated by buildscripts/clang-tidy.py, across the codebase."""
import argparse
import hashlib
import json
import os
import sys
from collections import defaultdict
def is_writeable(file) -> bool:
try:
with open(file, "a") as f: # noqa: F841
pass
return True
except OSError:
return False
def can_replacements_be_applied(replacements) -> bool:
"""Checks if the files containing replacements are unchanged since clang-tidy was run and are writeable.
For any replacement to be valid, all impacted files must be unchanged."""
for replacement in replacements:
if (
os.path.exists(replacement["FilePath"])
and is_writeable(replacement["FilePath"])
and replacement["FileContentsMD5"]
):
with open(replacement["FilePath"], "rb") as fin:
file_bytes = fin.read()
current_md5 = hashlib.md5(file_bytes).hexdigest()
if current_md5 != replacement["FileContentsMD5"]:
return False
else:
return False
return True
def get_replacements_to_apply(fixes_file) -> dict:
"""Gets a per file listing of the valid replacements to apply."""
replacements_to_apply = defaultdict(list)
with open(fixes_file) as fin:
fixes_data = json.load(fin)
for clang_tidy_check in fixes_data:
for main_source_file in fixes_data[clang_tidy_check]:
for violation_instance in fixes_data[clang_tidy_check][main_source_file]:
replacements = fixes_data[clang_tidy_check][main_source_file][
violation_instance
]["replacements"]
if can_replacements_be_applied(replacements):
for replacement in replacements:
replacements_to_apply[replacement["FilePath"]].append(replacement)
else:
print(
f"""WARNING: not applying replacements for {clang_tidy_check} in {main_source_file} at offset {violation_instance}, at least one file that is part of the automatic replacement has changed since clang-tidy was run, or is not writeable."""
)
return replacements_to_apply
def main(argv=sys.argv[1:]):
parser = argparse.ArgumentParser()
parser.add_argument(dest="fixes_file", help="Path to fixes file.")
args = parser.parse_args(argv)
replacements_to_apply = get_replacements_to_apply(args.fixes_file)
for file in replacements_to_apply:
with open(file, "rb") as fin:
file_bytes = fin.read()
# perform the swap replacement of the binary data
file_bytes = bytearray(file_bytes)
replacements_to_apply[file].sort(key=lambda r: r["Offset"])
adjustments = 0
for replacement in replacements_to_apply[file]:
file_bytes[
replacement["Offset"] + adjustments : replacement["Offset"]
+ adjustments
+ replacement["Length"]
] = replacement["ReplacementText"].encode()
if replacement["Length"] != len(replacement["ReplacementText"]):
adjustments += len(replacement["ReplacementText"]) - replacement["Length"]
with open(file, "wb") as fout:
fout.write(bytes(file_bytes))
if __name__ == "__main__":
main()