
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
146 lines
5.3 KiB
Python
Executable File
146 lines
5.3 KiB
Python
Executable File
#!/usr/bin/env python
|
|
#
|
|
#===- clang-tidy-diff.py - ClangTidy Diff Checker ------------*- python -*--===#
|
|
#
|
|
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
# See https://llvm.org/LICENSE.txt for license information.
|
|
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
#
|
|
#===------------------------------------------------------------------------===#
|
|
|
|
r"""
|
|
ClangTidy Diff Checker
|
|
======================
|
|
|
|
This script reads input from a unified diff, runs clang-tidy on all changed
|
|
files and outputs clang-tidy warnings in changed lines only. This is useful to
|
|
detect clang-tidy regressions in the lines touched by a specific patch.
|
|
Example usage for git/svn users:
|
|
|
|
git diff -U0 HEAD^ | clang-tidy-diff.py -p1
|
|
svn diff --diff-cmd=diff -x-U0 | \
|
|
clang-tidy-diff.py -fix -checks=-*,modernize-use-override
|
|
|
|
"""
|
|
|
|
import argparse
|
|
import json
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=
|
|
'Run clang-tidy against changed files, and '
|
|
'output diagnostics only for modified '
|
|
'lines.')
|
|
parser.add_argument('-clang-tidy-binary', metavar='PATH',
|
|
default='clang-tidy',
|
|
help='path to clang-tidy binary')
|
|
parser.add_argument('-p', metavar='NUM', default=0,
|
|
help='strip the smallest prefix containing P slashes')
|
|
parser.add_argument('-regex', metavar='PATTERN', default=None,
|
|
help='custom pattern selecting file paths to check '
|
|
'(case sensitive, overrides -iregex)')
|
|
parser.add_argument('-iregex', metavar='PATTERN', default=
|
|
r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc)',
|
|
help='custom pattern selecting file paths to check '
|
|
'(case insensitive, overridden by -regex)')
|
|
|
|
parser.add_argument('-fix', action='store_true', default=False,
|
|
help='apply suggested fixes')
|
|
parser.add_argument('-checks',
|
|
help='checks filter, when not specified, use clang-tidy '
|
|
'default',
|
|
default='')
|
|
parser.add_argument('-path', dest='build_path',
|
|
help='Path used to read a compile command database.')
|
|
parser.add_argument('-export-fixes', metavar='FILE', dest='export_fixes',
|
|
help='Create a yaml file to store suggested fixes in, '
|
|
'which can be applied with clang-apply-replacements.')
|
|
parser.add_argument('-extra-arg', dest='extra_arg',
|
|
action='append', default=[],
|
|
help='Additional argument to append to the compiler '
|
|
'command line.')
|
|
parser.add_argument('-extra-arg-before', dest='extra_arg_before',
|
|
action='append', default=[],
|
|
help='Additional argument to prepend to the compiler '
|
|
'command line.')
|
|
parser.add_argument('-quiet', action='store_true', default=False,
|
|
help='Run clang-tidy in quiet mode')
|
|
clang_tidy_args = []
|
|
argv = sys.argv[1:]
|
|
if '--' in argv:
|
|
clang_tidy_args.extend(argv[argv.index('--'):])
|
|
argv = argv[:argv.index('--')]
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
# Extract changed lines for each file.
|
|
filename = None
|
|
lines_by_file = {}
|
|
for line in sys.stdin:
|
|
match = re.search('^\+\+\+\ \"?(.*?/){%s}([^ \t\n\"]*)' % args.p, line)
|
|
if match:
|
|
filename = match.group(2)
|
|
if filename == None:
|
|
continue
|
|
|
|
if args.regex is not None:
|
|
if not re.match('^%s$' % args.regex, filename):
|
|
continue
|
|
else:
|
|
if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE):
|
|
continue
|
|
|
|
match = re.search('^@@.*\+(\d+)(,(\d+))?', line)
|
|
if match:
|
|
start_line = int(match.group(1))
|
|
line_count = 1
|
|
if match.group(3):
|
|
line_count = int(match.group(3))
|
|
if line_count == 0:
|
|
continue
|
|
end_line = start_line + line_count - 1;
|
|
lines_by_file.setdefault(filename, []).append([start_line, end_line])
|
|
|
|
if len(lines_by_file) == 0:
|
|
print("No relevant changes found.")
|
|
sys.exit(0)
|
|
|
|
line_filter_json = json.dumps(
|
|
[{"name" : name, "lines" : lines_by_file[name]} for name in lines_by_file],
|
|
separators = (',', ':'))
|
|
|
|
quote = "";
|
|
if sys.platform == 'win32':
|
|
line_filter_json=re.sub(r'"', r'"""', line_filter_json)
|
|
else:
|
|
quote = "'";
|
|
|
|
# Run clang-tidy on files containing changes.
|
|
command = [args.clang_tidy_binary]
|
|
command.append('-line-filter=' + quote + line_filter_json + quote)
|
|
if args.fix:
|
|
command.append('-fix')
|
|
if args.export_fixes:
|
|
command.append('-export-fixes=' + args.export_fixes)
|
|
if args.checks != '':
|
|
command.append('-checks=' + quote + args.checks + quote)
|
|
if args.quiet:
|
|
command.append('-quiet')
|
|
if args.build_path is not None:
|
|
command.append('-p=%s' % args.build_path)
|
|
command.extend(lines_by_file.keys())
|
|
for arg in args.extra_arg:
|
|
command.append('-extra-arg=%s' % arg)
|
|
for arg in args.extra_arg_before:
|
|
command.append('-extra-arg-before=%s' % arg)
|
|
command.extend(clang_tidy_args)
|
|
|
|
sys.exit(subprocess.call(' '.join(command), shell=True))
|
|
|
|
if __name__ == '__main__':
|
|
main()
|