seanptmaher updated this revision to Diff 487407.
seanptmaher added a comment.

Possibly fix the patchset? Sorry I've never used phabricator berfore.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D141230/new/

https://reviews.llvm.org/D141230

Files:
  clang/tools/clang-format/clang-format-diff.py

Index: clang/tools/clang-format/clang-format-diff.py
===================================================================
--- clang/tools/clang-format/clang-format-diff.py
+++ clang/tools/clang-format/clang-format-diff.py
@@ -25,6 +25,7 @@
 
 import argparse
 import difflib
+import os
 import re
 import subprocess
 import sys
@@ -99,46 +100,63 @@
           ['-lines', str(start_line) + ':' + str(end_line)])
 
   # Reformat files containing changes in place.
-  for filename, lines in lines_by_file.items():
-    if args.i and args.verbose:
-      print('Formatting {}'.format(filename))
-    command = [args.binary, filename]
-    if args.i:
-      command.append('-i')
-    if args.sort_includes:
-      command.append('-sort-includes')
-    command.extend(lines)
-    if args.style:
-      command.extend(['-style', args.style])
-    if args.fallback_style:
-      command.extend(['-fallback-style', args.fallback_style])
-
+  lbf = list(lines_by_file.items())
+  procs = None
+  try:
+    procs = [None for i in range(len(os.sched_getaffinity(0)) * 8)]
+  except AttributeError as e:
+    # os.sched_getaffinity isn't defined on all platforms.
+    import multiprocessing
     try:
-      p = subprocess.Popen(command,
-                           stdout=subprocess.PIPE,
-                           stderr=None,
-                           stdin=subprocess.PIPE,
-                           universal_newlines=True)
-    except OSError as e:
-      # Give the user more context when clang-format isn't
-      # found/isn't executable, etc.
-      raise RuntimeError(
-        'Failed to run "%s" - %s"' % (" ".join(command), e.strerror))
-
-    stdout, stderr = p.communicate()
-    if p.returncode != 0:
-      sys.exit(p.returncode)
+      procs = [None for i in range(multiprocessing.cpu_count())]
+    except NotImplementedError as e:
+      # Fallback to 8 concurrent processes in the case that CPU count cannot be
+      # determined.
+      procs = [None for i in range(8)]
 
-    if not args.i:
-      with open(filename) as f:
-        code = f.readlines()
-      formatted_code = StringIO(stdout).readlines()
-      diff = difflib.unified_diff(code, formatted_code,
-                                  filename, filename,
-                                  '(before formatting)', '(after formatting)')
-      diff_string = ''.join(diff)
-      if len(diff_string) > 0:
-        sys.stdout.write(diff_string)
+  while lbf:
+    for i, proc in enumerate(procs):
+      if not lbf:
+        break
+      if proc is None or proc.poll() is not None:
+        if proc is not None:
+          stdout, stderr = proc.communicate()
+          if proc.returncode != 0:
+            sys.exit(proc.returncode)
+          if not args.i:
+            with open(filename) as f:
+              code = f.readlines()
+            formatted_code = StringIO(stdout).readlines()
+            diff = difflib.unified_diff(code, formatted_code,
+                                        filename, filename,
+                                        '(before formatting)', '(after formatting)')
+            diff_string = ''.join(diff)
+            if len(diff_string) > 0:
+              sys.stdout.write(diff_string)
+        filename, lines = lbf.pop()
+        if args.i and args.verbose:
+          print('Formatting {}'.format(filename))
+        command = [args.binary, filename]
+        if args.i:
+          command.append('-i')
+        if args.sort_includes:
+          command.append('-sort-includes')
+        command.extend(lines)
+        if args.style:
+          command.extend(['-style', args.style])
+        if args.fallback_style:
+          command.extend(['-fallback-style', args.fallback_style])
+        try:
+          procs[i] = subprocess.Popen(command,
+                                      stdout=subprocess.PIPE,
+                                      stderr=None,
+                                      stdin=subprocess.PIPE,
+                                      universal_newlines=True)
+        except OSError as e:
+          # Give the user more context when clang-format isn't
+          # found/isn't executable, etc.
+          raise RuntimeError(
+              'Failed to run "%s" - %s"' % (" ".join(command), e.strerror))
 
 if __name__ == '__main__':
   main()
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to