<div dir="ltr"><div dir="ltr">Hello Zinovy,<br><br>This commit broke test on the next builder:<br><a href="http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/builds/45557">http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/builds/45557</a><br>. . .<br>Failing Tests (1):<br>Â Â Â Clang Tools :: clang-tidy/clang-tidy-diff.cpp<br><br>Please have a look?<br><br>Thanks<br><br>Galina</div><div dir="ltr"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 20, 2019 at 8:49 AM Zinovy Nis via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Author: zinovy.nis<br>
Date: Wed Mar 20 08:50:26 2019<br>
New Revision: 356565<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=356565&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=356565&view=rev</a><br>
Log:<br>
Reland r356547 after fixing the tests for Linux.<br>
<br>
[clang-tidy] Parallelize clang-tidy-diff.py<br>
<br>
This patch has 2 rationales:<br>
<br>
- large patches lead to long command lines and often cause max command line length restrictions imposed by OS;<br>
- clang-tidy runs on modified files are independent and can be done in parallel, the same as done for run-clang-tidy.<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D57662" rel="noreferrer" target="_blank">https://reviews.llvm.org/D57662</a><br>
<br>
<br>
Modified:<br>
  clang-tools-extra/trunk/clang-tidy/tool/clang-tidy-diff.py<br>
<br>
Modified: clang-tools-extra/trunk/clang-tidy/tool/clang-tidy-diff.py<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/tool/clang-tidy-diff.py?rev=356565&r1=356564&r2=356565&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/tool/clang-tidy-diff.py?rev=356565&r1=356564&r2=356565&view=diff</a><br>
==============================================================================<br>
--- clang-tools-extra/trunk/clang-tidy/tool/clang-tidy-diff.py (original)<br>
+++ clang-tools-extra/trunk/clang-tidy/tool/clang-tidy-diff.py Wed Mar 20 08:50:26 2019<br>
@@ -24,10 +24,90 @@ Example usage for git/svn users:<br>
 """<br>
<br>
 import argparse<br>
+import glob<br>
 import json<br>
+import multiprocessing<br>
+import os<br>
 import re<br>
+import shutil<br>
 import subprocess<br>
 import sys<br>
+import tempfile<br>
+import threading<br>
+import traceback<br>
+import yaml<br>
+<br>
+is_py2 = sys.version[0] == '2'<br>
+<br>
+if is_py2:<br>
+Â Â import Queue as queue<br>
+else:<br>
+Â Â import queue as queue<br>
+<br>
+<br>
+def run_tidy(task_queue, lock, timeout):<br>
+Â watchdog = None<br>
+Â while True:<br>
+Â Â command = task_queue.get()<br>
+Â Â try:<br>
+Â Â Â proc = subprocess.Popen(command,<br>
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â stdout=subprocess.PIPE,<br>
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â stderr=subprocess.PIPE)<br>
+<br>
+Â Â Â if timeout is not None:<br>
+Â Â Â Â watchdog = threading.Timer(timeout, proc.kill)<br>
+Â Â Â Â watchdog.start()<br>
+<br>
+Â Â Â stdout, stderr = proc.communicate()<br>
+<br>
+Â Â Â with lock:<br>
+Â Â Â Â sys.stdout.write(stdout.decode('utf-8') + '\n')<br>
+Â Â Â Â sys.stdout.flush()<br>
+Â Â Â Â if stderr:<br>
+Â Â Â Â Â sys.stderr.write(stderr.decode('utf-8') + '\n')<br>
+Â Â Â Â Â sys.stderr.flush()<br>
+Â Â except Exception as e:<br>
+Â Â Â with lock:<br>
+Â Â Â Â sys.stderr.write('Failed: ' + str(e) + ': '.join(command) + '\n')<br>
+Â Â finally:<br>
+Â Â Â with lock:<br>
+Â Â Â Â if (not timeout is None) and (not watchdog is None):<br>
+Â Â Â Â Â if not watchdog.is_alive():<br>
+Â Â Â Â Â Â Â sys.stderr.write('Terminated by timeout: ' +<br>
+Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ' '.join(command) + '\n')<br>
+Â Â Â Â Â watchdog.cancel()<br>
+Â Â Â task_queue.task_done()<br>
+<br>
+<br>
+def start_workers(max_tasks, tidy_caller, task_queue, lock, timeout):<br>
+Â for _ in range(max_tasks):<br>
+Â Â t = threading.Thread(target=tidy_caller, args=(task_queue, lock, timeout))<br>
+Â Â t.daemon = True<br>
+Â Â t.start()<br>
+<br>
+def merge_replacement_files(tmpdir, mergefile):<br>
+Â """Merge all replacement files in a directory into a single file"""<br>
+Â # The fixes suggested by clang-tidy >= 4.0.0 are given under<br>
+Â # the top level key 'Diagnostics' in the output yaml files<br>
+Â mergekey = "Diagnostics"<br>
+Â merged = []<br>
+Â for replacefile in glob.iglob(os.path.join(tmpdir, '*.yaml')):<br>
+Â Â content = yaml.safe_load(open(replacefile, 'r'))<br>
+Â Â if not content:<br>
+Â Â Â continue # Skip empty files.<br>
+Â Â merged.extend(content.get(mergekey, []))<br>
+<br>
+Â if merged:<br>
+Â Â # MainSourceFile: The key is required by the definition inside<br>
+Â Â # include/clang/Tooling/ReplacementsYaml.h, but the value<br>
+Â Â # is actually never used inside clang-apply-replacements,<br>
+Â Â # so we set it to '' here.<br>
+Â Â output = { 'MainSourceFile': '', mergekey: merged }<br>
+Â Â with open(mergefile, 'w') as out:<br>
+Â Â Â yaml.safe_dump(output, out)<br>
+Â else:<br>
+Â Â # Empty the file:<br>
+Â Â open(mergefile, 'w').close()<br>
<br>
<br>
 def main():<br>
@@ -47,7 +127,10 @@ def main():<br>
            r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc)',<br>
            help='custom pattern selecting file paths to check '<br>
            '(case insensitive, overridden by -regex)')<br>
-<br>
+Â parser.add_argument('-j', type=int, default=1,<br>
+Â Â Â Â Â Â Â Â Â Â Â help='number of tidy instances to be run in parallel.')<br>
+Â parser.add_argument('-timeout', type=int, default=None,<br>
+Â Â Â Â Â Â Â Â Â Â Â help='timeout per each file in seconds.')<br>
  parser.add_argument('-fix', action='store_true', default=False,<br>
            help='apply suggested fixes')<br>
  parser.add_argument('-checks',<br>
@@ -84,7 +167,7 @@ def main():<br>
   match = re.search('^\+\+\+\ \"?(.*?/){%s}([^ \t\n\"]*)' % args.p, line)<br>
   if match:<br>
    filename = match.group(2)<br>
-Â Â if filename == None:<br>
+Â Â if filename is None:<br>
    continue<br>
<br>
   if args.regex is not None:<br>
@@ -102,44 +185,79 @@ def main():<br>
     line_count = int(match.group(3))<br>
    if line_count == 0:<br>
     continue<br>
-Â Â Â end_line = start_line + line_count - 1;<br>
+Â Â Â end_line = start_line + line_count - 1<br>
    lines_by_file.setdefault(filename, []).append([start_line, end_line])<br>
<br>
-Â if len(lines_by_file) == 0:<br>
+Â if not any(lines_by_file):<br>
   print("No relevant changes found.")<br>
   sys.exit(0)<br>
<br>
-Â line_filter_json = json.dumps(<br>
-Â Â [{"name" : name, "lines" : lines_by_file[name]} for name in lines_by_file],<br>
-Â Â separators = (',', ':'))<br>
-<br>
-Â quote = "";<br>
-Â if sys.platform == 'win32':<br>
-Â Â line_filter_json=re.sub(r'"', r'"""', line_filter_json)<br>
-Â else:<br>
-Â Â quote = "'";<br>
+Â max_task_count = args.j<br>
+Â if max_task_count == 0:<br>
+Â Â Â max_task_count = multiprocessing.cpu_count()<br>
+Â max_task_count = min(len(lines_by_file), max_task_count)<br>
<br>
-Â # Run clang-tidy on files containing changes.<br>
-Â command = [args.clang_tidy_binary]<br>
-Â command.append('-line-filter=' + quote + line_filter_json + quote)<br>
-Â if args.fix:<br>
-Â Â command.append('-fix')<br>
+Â tmpdir = None<br>
  if args.export_fixes:<br>
-Â Â command.append('-export-fixes=' + args.export_fixes)<br>
+Â Â tmpdir = tempfile.mkdtemp()<br>
+<br>
+Â # Tasks for clang-tidy.<br>
+Â task_queue = queue.Queue(max_task_count)<br>
+Â # A lock for console output.<br>
+Â lock = threading.Lock()<br>
+<br>
+Â # Run a pool of clang-tidy workers.<br>
+Â start_workers(max_task_count, run_tidy, task_queue, lock, args.timeout)<br>
+<br>
+Â # Form the common args list.<br>
+Â common_clang_tidy_args = []<br>
+Â if args.fix:<br>
+Â Â common_clang_tidy_args.append('-fix')<br>
  if args.checks != '':<br>
-Â Â command.append('-checks=' + quote + args.checks + quote)<br>
+Â Â common_clang_tidy_args.append('-checks=' + args.checks)<br>
  if args.quiet:<br>
-Â Â command.append('-quiet')<br>
+Â Â common_clang_tidy_args.append('-quiet')<br>
  if args.build_path is not None:<br>
-Â Â command.append('-p=%s' % args.build_path)<br>
-Â command.extend(lines_by_file.keys())<br>
+Â Â common_clang_tidy_args.append('-p=%s' % args.build_path)<br>
  for arg in args.extra_arg:<br>
-Â Â Â command.append('-extra-arg=%s' % arg)<br>
+Â Â common_clang_tidy_args.append('-extra-arg=%s' % arg)<br>
  for arg in args.extra_arg_before:<br>
-Â Â Â command.append('-extra-arg-before=%s' % arg)<br>
-Â command.extend(clang_tidy_args)<br>
+Â Â common_clang_tidy_args.append('-extra-arg-before=%s' % arg)<br>
+<br>
+Â for name in lines_by_file:<br>
+Â Â line_filter_json = json.dumps(<br>
+Â Â Â [{"name": name, "lines": lines_by_file[name]}],<br>
+Â Â Â separators=(',', ':'))<br>
+<br>
+Â Â # Run clang-tidy on files containing changes.<br>
+Â Â command = [args.clang_tidy_binary]<br>
+Â Â command.append('-line-filter=' + line_filter_json)<br>
+Â Â if args.export_fixes:<br>
+Â Â Â # Get a temporary file. We immediately close the handle so clang-tidy can<br>
+Â Â Â # overwrite it.<br>
+Â Â Â (handle, tmp_name) = tempfile.mkstemp(suffix='.yaml', dir=tmpdir)<br>
+Â Â Â os.close(handle)<br>
+Â Â Â command.append('-export-fixes=' + tmp_name)<br>
+Â Â command.extend(common_clang_tidy_args)<br>
+Â Â command.append(name)<br>
+Â Â command.extend(clang_tidy_args)<br>
+<br>
+Â Â task_queue.put(command)<br>
+<br>
+Â # Wait for all threads to be done.<br>
+Â task_queue.join()<br>
+<br>
+Â if args.export_fixes:<br>
+Â Â print('Writing fixes to ' + args.export_fixes + ' ...')<br>
+Â Â try:<br>
+Â Â Â merge_replacement_files(tmpdir, args.export_fixes)<br>
+Â Â except:<br>
+Â Â Â sys.stderr.write('Error exporting fixes.\n')<br>
+Â Â Â traceback.print_exc()<br>
+<br>
+Â if tmpdir:<br>
+Â Â shutil.rmtree(tmpdir)<br>
<br>
-Â sys.exit(subprocess.call(' '.join(command), shell=True))<br>
<br>
 if __name__ == '__main__':<br>
  main()<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div>