<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>