Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 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 | |
| 12 | r""" |
| 13 | ClangFormat Diff Reformatter |
| 14 | ============================ |
| 15 | |
| 16 | This script reads input from a unified diff and reformats all the changed |
| 17 | lines. This is useful to reformat all the lines touched by a specific patch. |
| 18 | Example usage for git users: |
| 19 | |
Alexander Kornienko | cf207a6 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 20 | git diff -U0 HEAD^ | clang-format-diff.py -p1 -i |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 21 | |
| 22 | """ |
| 23 | |
| 24 | import argparse |
Alexander Kornienko | cf207a6 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 25 | import difflib |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 26 | import re |
Alexander Kornienko | cf207a6 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 27 | import string |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 28 | import subprocess |
Alexander Kornienko | cf207a6 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 29 | import StringIO |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 30 | import sys |
| 31 | |
| 32 | |
| 33 | # Change this to the full path if clang-format is not on the path. |
| 34 | binary = 'clang-format' |
| 35 | |
| 36 | |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 37 | def main(): |
| 38 | parser = argparse.ArgumentParser(description= |
Alexander Kornienko | cf207a6 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 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') |
Daniel Jasper | 01970ef | 2013-05-30 11:50:20 +0000 | [diff] [blame] | 44 | parser.add_argument('-p', default=0, |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 45 | help='strip the smallest prefix containing P slashes') |
Alexander Kornienko | 4e65c98 | 2013-09-02 16:39:23 +0000 | [diff] [blame] | 46 | parser.add_argument( |
| 47 | '-style', |
| 48 | help= |
| 49 | 'formatting style to apply (LLVM, Google, Chromium, Mozilla, WebKit)') |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 50 | args = parser.parse_args() |
| 51 | |
Daniel Jasper | 2e118a0 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 52 | # Extract changed lines for each file. |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 53 | filename = None |
Daniel Jasper | 2e118a0 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 54 | lines_by_file = {} |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 55 | for line in sys.stdin: |
| 56 | match = re.search('^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line) |
| 57 | if match: |
| 58 | filename = match.group(2) |
| 59 | if filename == None: |
| 60 | continue |
| 61 | |
Daniel Jasper | 2e118a0 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 62 | # FIXME: Add other types containing C++/ObjC code. |
| 63 | if not (filename.endswith(".cpp") or filename.endswith(".cc") or |
| 64 | filename.endswith(".h")): |
| 65 | continue |
| 66 | |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 67 | match = re.search('^@@.*\+(\d+)(,(\d+))?', line) |
| 68 | if match: |
Daniel Jasper | 2e118a0 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 69 | start_line = int(match.group(1)) |
Daniel Jasper | 4a966d3 | 2013-10-02 13:59:03 +0000 | [diff] [blame] | 70 | line_count = 1 |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 71 | if match.group(3): |
Daniel Jasper | 4a966d3 | 2013-10-02 13:59:03 +0000 | [diff] [blame] | 72 | line_count = int(match.group(3)) |
| 73 | if line_count == 0: |
| 74 | continue |
| 75 | end_line = start_line + line_count - 1; |
Daniel Jasper | 2e118a0 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 76 | lines_by_file.setdefault(filename, []).extend( |
| 77 | ['-lines', str(start_line) + ':' + str(end_line)]) |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 78 | |
Daniel Jasper | 2e118a0 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 79 | # Reformat files containing changes in place. |
| 80 | for filename, lines in lines_by_file.iteritems(): |
Alexander Kornienko | cf207a6 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 81 | command = [binary, filename] |
| 82 | if args.i: |
| 83 | command.append('-i') |
Daniel Jasper | 2e118a0 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 84 | command.extend(lines) |
| 85 | if args.style: |
Daniel Jasper | 68455be | 2013-09-21 10:05:02 +0000 | [diff] [blame] | 86 | command.extend(['-style', args.style]) |
Daniel Jasper | 2e118a0 | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 87 | p = subprocess.Popen(command, stdout=subprocess.PIPE, |
| 88 | stderr=subprocess.PIPE, |
| 89 | stdin=subprocess.PIPE) |
| 90 | stdout, stderr = p.communicate() |
| 91 | if stderr: |
| 92 | print stderr |
Daniel Jasper | f7d9e63 | 2013-10-08 15:54:36 +0000 | [diff] [blame] | 93 | if p.returncode != 0: |
| 94 | sys.exit(p.returncode); |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 95 | |
Alexander Kornienko | cf207a6 | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 96 | if not args.i: |
| 97 | with open(filename) as f: |
| 98 | code = f.readlines() |
| 99 | formatted_code = StringIO.StringIO(stdout).readlines() |
| 100 | diff = difflib.unified_diff(code, formatted_code, |
| 101 | filename, filename, |
| 102 | '(before formatting)', '(after formatting)') |
| 103 | diff_string = string.join(diff, '') |
| 104 | if len(diff_string) > 0: |
| 105 | print diff_string |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 106 | |
| 107 | if __name__ == '__main__': |
| 108 | main() |