1#!/usr/bin/env python3 2# 3#===- clang-format-diff.py - ClangFormat Diff Reformatter ----*- python -*--===# 4# 5# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 6# See https://llvm.org/LICENSE.txt for license information. 7# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 8# 9#===------------------------------------------------------------------------===# 10 11""" 12This script reads input from a unified diff and reformats all the changed 13lines. This is useful to reformat all the lines touched by a specific patch. 14Example usage for git/svn users: 15 16 git diff -U0 --no-color --relative HEAD^ | clang-format-diff.py -p1 -i 17 svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i 18 19It should be noted that the filename contained in the diff is used unmodified 20to determine the source file to update. Users calling this script directly 21should be careful to ensure that the path in the diff is correct relative to the 22current working directory. 23""" 24from __future__ import absolute_import, division, print_function 25 26import argparse 27import difflib 28import re 29import subprocess 30import sys 31 32if sys.version_info.major >= 3: 33 from io import StringIO 34else: 35 from io import BytesIO as StringIO 36 37def process_subprocess_result(proc, args): 38 stdout, stderr = proc.communicate() 39 if proc.returncode != 0: 40 sys.exit(proc.returncode) 41 if not args.i: 42 with open(filename) as f: 43 code = f.readlines() 44 formatted_code = StringIO(stdout).readlines() 45 diff = difflib.unified_diff(code, formatted_code, 46 filename, filename, 47 '(before formatting)', 48 '(after formatting)') 49 diff_string = ''.join(diff) 50 if len(diff_string) > 0: 51 sys.stdout.write(diff_string) 52 53def main(): 54 parser = argparse.ArgumentParser(description=__doc__, 55 formatter_class= 56 argparse.RawDescriptionHelpFormatter) 57 parser.add_argument('-i', action='store_true', default=False, 58 help='apply edits to files instead of displaying a diff') 59 parser.add_argument('-p', metavar='NUM', default=0, 60 help='strip the smallest prefix containing P slashes') 61 parser.add_argument('-regex', metavar='PATTERN', default=None, 62 help='custom pattern selecting file paths to reformat ' 63 '(case sensitive, overrides -iregex)') 64 parser.add_argument('-iregex', metavar='PATTERN', default= 65 r'.*\.(cpp|cc|c\+\+|cxx|cppm|ccm|cxxm|c\+\+m|c|cl|h|hh|hpp|hxx' 66 r'|m|mm|inc|js|ts|proto|protodevel|java|cs|json)', 67 help='custom pattern selecting file paths to reformat ' 68 '(case insensitive, overridden by -regex)') 69 parser.add_argument('-sort-includes', action='store_true', default=False, 70 help='let clang-format sort include blocks') 71 parser.add_argument('-v', '--verbose', action='store_true', 72 help='be more verbose, ineffective without -i') 73 parser.add_argument('-style', 74 help='formatting style to apply (LLVM, GNU, Google, Chromium, ' 75 'Microsoft, Mozilla, WebKit)') 76 parser.add_argument('-fallback-style', 77 help='The name of the predefined style used as a' 78 'fallback in case clang-format is invoked with' 79 '-style=file, but can not find the .clang-format' 80 'file to use.') 81 parser.add_argument('-binary', default='clang-format', 82 help='location of binary to use for clang-format') 83 parser.add_argument('-j', default=1, type=int, metavar='N', 84 help='number of concurrent clang-format processes to spawn in ' 85 'parallel') 86 args = parser.parse_args() 87 88 # Extract changed lines for each file. 89 filename = None 90 lines_by_file = {} 91 for line in sys.stdin: 92 match = re.search(r'^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line) 93 if match: 94 filename = match.group(2) 95 if filename is None: 96 continue 97 98 if args.regex is not None: 99 if not re.match('^%s$' % args.regex, filename): 100 continue 101 else: 102 if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE): 103 continue 104 105 match = re.search(r'^@@.*\+(\d+)(?:,(\d+))?', line) 106 if match: 107 start_line = int(match.group(1)) 108 line_count = 1 109 if match.group(2): 110 line_count = int(match.group(2)) 111 # The input is something like 112 # 113 # @@ -1, +0,0 @@ 114 # 115 # which means no lines were added. 116 if line_count == 0: 117 continue 118 # Also format lines range if line_count is 0 in case of deleting 119 # surrounding statements. 120 end_line = start_line 121 if line_count != 0: 122 end_line += line_count - 1 123 lines_by_file.setdefault(filename, []).extend( 124 ['-lines', str(start_line) + ':' + str(end_line)]) 125 126 # Reformat files containing changes in place. 127 lbf = list(lines_by_file.items()) 128 procs = [None for i in range(args.j)] 129 while lbf: 130 spawned_one = False 131 for i, proc in enumerate(procs): 132 if not lbf: 133 break 134 if proc is not None and proc.poll() is not None: 135 process_subprocess_result(proc, args) 136 # Set to None to flag the slot as free to start a new process 137 procs[i] = None 138 proc = None 139 if proc is None: 140 filename, lines = lbf.pop() 141 spawned_one = True 142 if args.i and args.verbose: 143 print('Formatting {}'.format(filename)) 144 command = [args.binary, filename] 145 if args.i: 146 command.append('-i') 147 if args.sort_includes: 148 command.append('-sort-includes') 149 command.extend(lines) 150 if args.style: 151 command.extend(['-style', args.style]) 152 if args.fallback_style: 153 command.extend(['-fallback-style', args.fallback_style]) 154 try: 155 procs[i] = subprocess.Popen(command, 156 stdout=subprocess.PIPE, 157 stderr=None, 158 stdin=subprocess.PIPE, 159 universal_newlines=True) 160 except OSError as e: 161 # Give the user more context when clang-format isn't 162 # found/isn't executable, etc. 163 raise RuntimeError( 164 'Failed to run "%s" - %s"' % (" ".join(command), e.strerror)) 165 # If we didn't spawn a single process after iterating through the whole 166 # list, wait on one of them to finish until we iterate through again, to 167 # prevent spinning in the case where we have a small number of jobs. 168 if not spawned_one: 169 procs[0].wait() 170 # Be sure not to leave any stray processes when exiting. 171 for proc in procs: 172 if proc: 173 proc.wait() 174 process_subprocess_result(proc, args) 175 176if __name__ == '__main__': 177 main() 178