1#!/usr/bin/python 2# 3#===- clang-format-diff.py - ClangFormat Diff Reformatter ----*- python -*--===# 4# 5# The LLVM Compiler Infrastructure 6# 7# This file is distributed under the University of Illinois Open Source 8# License. See LICENSE.TXT for details. 9# 10#===------------------------------------------------------------------------===# 11 12r""" 13ClangFormat Diff Reformatter 14============================ 15 16This script reads input from a unified diff and reformats all the changed 17lines. This is useful to reformat all the lines touched by a specific patch. 18Example usage for git users: 19 20 git diff -U0 HEAD^ | clang-format-diff.py -p1 -i 21 22""" 23 24import argparse 25import difflib 26import re 27import string 28import subprocess 29import StringIO 30import sys 31 32 33# Change this to the full path if clang-format is not on the path. 34binary = 'clang-format' 35 36 37def main(): 38 parser = argparse.ArgumentParser(description= 39 'Reformat changed lines in diff. Without -i ' 40 'option just output the diff that would be ' 41 'introduced.') 42 parser.add_argument('-i', action='store_true', default=False, 43 help='apply edits to files instead of displaying a diff') 44 parser.add_argument('-p', metavar='NUM', default=0, 45 help='strip the smallest prefix containing P slashes') 46 parser.add_argument('-regex', metavar='PATTERN', default= 47 r'.*\.(cpp|cc|CPP|C|c\+\+|cxx|c|h|hpp|m|mm|inc|js)', 48 help='custom pattern selecting file paths to reformat') 49 parser.add_argument( 50 '-style', 51 help= 52 'formatting style to apply (LLVM, Google, Chromium, Mozilla, WebKit)') 53 args = parser.parse_args() 54 55 # Extract changed lines for each file. 56 filename = None 57 lines_by_file = {} 58 for line in sys.stdin: 59 match = re.search('^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line) 60 if match: 61 filename = match.group(2) 62 if filename == None: 63 continue 64 65 if not re.match(args.regex, filename): 66 continue 67 68 match = re.search('^@@.*\+(\d+)(,(\d+))?', line) 69 if match: 70 start_line = int(match.group(1)) 71 line_count = 1 72 if match.group(3): 73 line_count = int(match.group(3)) 74 if line_count == 0: 75 continue 76 end_line = start_line + line_count - 1; 77 lines_by_file.setdefault(filename, []).extend( 78 ['-lines', str(start_line) + ':' + str(end_line)]) 79 80 # Reformat files containing changes in place. 81 for filename, lines in lines_by_file.iteritems(): 82 command = [binary, filename] 83 if args.i: 84 command.append('-i') 85 command.extend(lines) 86 if args.style: 87 command.extend(['-style', args.style]) 88 p = subprocess.Popen(command, stdout=subprocess.PIPE, 89 stderr=None, stdin=subprocess.PIPE) 90 stdout, stderr = p.communicate() 91 if p.returncode != 0: 92 sys.exit(p.returncode); 93 94 if not args.i: 95 with open(filename) as f: 96 code = f.readlines() 97 formatted_code = StringIO.StringIO(stdout).readlines() 98 diff = difflib.unified_diff(code, formatted_code, 99 filename, filename, 100 '(before formatting)', '(after formatting)') 101 diff_string = string.join(diff, '') 102 if len(diff_string) > 0: 103 sys.stdout.write(diff_string) 104 105if __name__ == '__main__': 106 main() 107