zinovy.nis updated this revision to Diff 187421.
zinovy.nis added a comment.
- `-j` is `1` by default;
- fixed minor remarks;
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D57662/new/
https://reviews.llvm.org/D57662
Files:
clang-tidy/tool/clang-tidy-diff.py
Index: clang-tidy/tool/clang-tidy-diff.py
===================================================================
--- clang-tidy/tool/clang-tidy-diff.py
+++ clang-tidy/tool/clang-tidy-diff.py
@@ -25,9 +25,56 @@
import argparse
import json
+import multiprocessing
+import os
import re
import subprocess
import sys
+import threading
+
+is_py2 = sys.version[0] == '2'
+
+if is_py2:
+ import Queue as queue
+else:
+ import queue as queue
+
+def run_tidy(task_queue, lock, timeout):
+ watchdog = None
+ while True:
+ command = task_queue.get()
+ try:
+ proc = subprocess.Popen(command,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+
+ if timeout is not None:
+ watchdog = threading.Timer(timeout, proc.kill)
+ watchdog.start()
+
+ stdout, stderr = proc.communicate()
+
+ with lock:
+ sys.stdout.write((' '.join(command)).decode('utf-8') + '\n' + stdout.decode('utf-8') + '\n')
+ if stderr:
+ sys.stderr.write(stderr.decode('utf-8') + '\n')
+ except Exception as e:
+ with lock:
+ sys.stderr.write('Failed: ' + str(e) + ' '.join(command) + '\n')
+ finally:
+ with lock:
+ if (not timeout is None) and (not watchdog is None):
+ if not watchdog.is_alive():
+ sys.stderr.write('Terminated by timeout: ' + ' '.join(command) + '\n')
+ watchdog.cancel()
+ task_queue.task_done()
+
+
+def run_workers(max_tasks, tidy_caller, task_queue, lock, timeout):
+ for _ in range(max_tasks):
+ t = threading.Thread(target=tidy_caller, args=(task_queue, lock, timeout))
+ t.daemon = True
+ t.start()
def main():
@@ -48,6 +95,10 @@
help='custom pattern selecting file paths to check '
'(case insensitive, overridden by -regex)')
+ parser.add_argument('-j', type=int, default=1,
+ help='number of tidy instances to be run in parallel.')
+ parser.add_argument('-timeout', type=int, default=None,
+ help='timeout per each file in seconds.')
parser.add_argument('-fix', action='store_true', default=False,
help='apply suggested fixes')
parser.add_argument('-checks',
@@ -77,6 +128,11 @@
args = parser.parse_args(argv)
+ if args.j == 0 or args.j > 1:
+ if args.export_fixes:
+ print("error: -export-fixes and -j are mutually exclusive.")
+ sys.exit(1)
+
# Extract changed lines for each file.
filename = None
lines_by_file = {}
@@ -84,7 +140,7 @@
match = re.search('^\+\+\+\ \"?(.*?/){%s}([^ \t\n\"]*)' % args.p, line)
if match:
filename = match.group(2)
- if filename == None:
+ if filename is None:
continue
if args.regex is not None:
@@ -102,44 +158,64 @@
line_count = int(match.group(3))
if line_count == 0:
continue
- end_line = start_line + line_count - 1;
+ end_line = start_line + line_count - 1
lines_by_file.setdefault(filename, []).append([start_line, end_line])
- if len(lines_by_file) == 0:
+ if not any(lines_by_file):
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 = (',', ':'))
+ max_task = args.j
+ if max_task == 0:
+ max_task = multiprocessing.cpu_count()
+ max_task = min(len(lines_by_file), max_task)
+
+ # Tasks for clang-tidy.
+ task_queue = queue.Queue(max_task)
+ # A lock for console output.
+ lock = threading.Lock()
- quote = "";
- if sys.platform == 'win32':
- line_filter_json=re.sub(r'"', r'"""', line_filter_json)
- else:
- quote = "'";
+ # Run a pool of clang-tidy workers.
+ run_workers(max_task, run_tidy, task_queue, lock, args.timeout)
- # Run clang-tidy on files containing changes.
- command = [args.clang_tidy_binary]
- command.append('-line-filter=' + quote + line_filter_json + quote)
+ quote = ""
+ if sys.platform != 'win32':
+ quote = "'"
+
+ # Form the common args list.
+ common_clang_tidy_args = []
if args.fix:
- command.append('-fix')
+ common_clang_tidy_args.append('-fix')
if args.export_fixes:
- command.append('-export-fixes=' + args.export_fixes)
+ common_clang_tidy_args.append('-export-fixes=' + args.export_fixes)
if args.checks != '':
- command.append('-checks=' + quote + args.checks + quote)
+ common_clang_tidy_args.append('-checks=' + quote + args.checks + quote)
if args.quiet:
- command.append('-quiet')
+ common_clang_tidy_args.append('-quiet')
if args.build_path is not None:
- command.append('-p=%s' % args.build_path)
- command.extend(lines_by_file.keys())
+ common_clang_tidy_args.append('-p=%s' % args.build_path)
for arg in args.extra_arg:
- command.append('-extra-arg=%s' % arg)
+ common_clang_tidy_args.append('-extra-arg=%s' % arg)
for arg in args.extra_arg_before:
- command.append('-extra-arg-before=%s' % arg)
- command.extend(clang_tidy_args)
+ common_clang_tidy_args.append('-extra-arg-before=%s' % arg)
+
+ for name in lines_by_file:
+ line_filter_json = json.dumps(
+ [{"name": name, "lines": lines_by_file[name]}],
+ separators=(',', ':'))
+
+ # Run clang-tidy on files containing changes.
+ command = [args.clang_tidy_binary]
+ command.append('-line-filter=' + quote + line_filter_json + quote)
+ command.extend(common_clang_tidy_args)
+ command.append(name)
+ command.extend(clang_tidy_args)
+
+ task_queue.put(command)
+
+ # Wait for all threads to be done.
+ task_queue.join()
- sys.exit(subprocess.call(' '.join(command), shell=True))
if __name__ == '__main__':
main()
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits