16 if os.path.isabs(file):
18 return os.path.normpath(os.path.join(path, file))
22 parser = argparse.ArgumentParser(description=
"Runs clang-tidy over a compilation database and applies the fixes")
23 parser.add_argument(
"compile_db", metavar=
'PATH')
24 parser.add_argument(
"--tidy", help=
"Path to clang-tidy", default=
"clang-tidy-14")
25 parser.add_argument(
"--apply-tool", help=
'Path to clang-apply-replacements', default=
"clang-apply-replacements-14")
26 parser.add_argument(
"--git", help=
"Path to git", default=
"git")
27 parser.add_argument(
"--source", help=
"Path to top-level source folder", default=
".")
28 parser.add_argument(
"--exclude", help=
"Regex of files to exclude", default=
None)
30 args = parser.parse_args()
32 apply_version = subprocess.run([args.apply_tool,
"--version"], capture_output=
True,
33 encoding=
"latin1").stdout.strip()
35 version_regex = re.compile(
r"ersion\s*(\d+)\.(\d+)\.(\d+)")
36 match = version_regex.search(apply_version)
37 has_ignore_insert_conflict = int(match.group(1)) > 14
42 database = json.load(open(args.compile_db))
43 files = set([
make_absolute(entry[
'file'], entry[
'directory'])
for entry
in database])
44 except FileNotFoundError:
45 print(f
"Failed to open {args.compile_db}: Not found")
46 except json.decoder.JSONDecodeError:
47 print(f
"Failed to open {args.compile_db}: Not valid json")
53 tmpdir = tempfile.mkdtemp()
54 source_folder = os.path.abspath(args.source)
56 exclude_re = re.compile(args.exclude)
if args.exclude
else None
59 if exclude_re
and exclude_re.search(file):
61 tidy = [args.tidy,
"-header-filter=.*",
"-export-fixes"]
62 (handle, name) = tempfile.mkstemp(suffix=
'.yaml', dir=tmpdir)
65 tidy.append(
"-p=" + os.path.dirname(os.path.normpath(args.compile_db)))
68 print(f
"Linting {file}...")
69 subprocess.run(tidy, check=
True)
70 except subprocess.CalledProcessError:
71 print(
"Linting failed. Usually that means the previously applied fix has introduced a compiler error. Aborting.")
72 print(
"Most likely it converted a conversion operator to explicit")
75 fixer = [args.apply_tool,
"--format",
"--style=file",
"--remove-change-desc-files"]
76 if has_ignore_insert_conflict:
77 fixer.append(
'--ignore-insert-conflict')
79 if subprocess.call(fixer) == 0:
80 git_update = [args.git,
'add',
'-u']
81 subprocess.call(git_update)
82 git_commit = [args.git,
'commit',
'-m', f
'clang-tidy: {os.path.relpath(file, start=source_folder)}']
83 subprocess.call(git_commit)
85 print(
"Error applying fixes, not committing")
89 if __name__ ==
"__main__":