xref: /llvm-project/clang/tools/clang-format/clang-format-diff.py (revision 2158912a4edcd3c341b7570aec4424188631f46b)
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=None,
47                      help='custom pattern selecting file paths to reformat '
48                      '(case sensitive, overrides -iregex)')
49  parser.add_argument('-iregex', metavar='PATTERN', default=
50                      r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc|js|proto'
51                      r'|protodevel)',
52                      help='custom pattern selecting file paths to reformat '
53                      '(case insensitive, overridden by -regex)')
54  parser.add_argument(
55      '-style',
56      help=
57      'formatting style to apply (LLVM, Google, Chromium, Mozilla, WebKit)')
58  args = parser.parse_args()
59
60  # Extract changed lines for each file.
61  filename = None
62  lines_by_file = {}
63  for line in sys.stdin:
64    match = re.search('^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line)
65    if match:
66      filename = match.group(2)
67    if filename == None:
68      continue
69
70    if args.regex is not None:
71      if not re.match('^%s$' % args.regex, filename):
72        continue
73    else:
74      if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE):
75        continue
76
77    match = re.search('^@@.*\+(\d+)(,(\d+))?', line)
78    if match:
79      start_line = int(match.group(1))
80      line_count = 1
81      if match.group(3):
82        line_count = int(match.group(3))
83      if line_count == 0:
84        continue
85      end_line = start_line + line_count - 1;
86      lines_by_file.setdefault(filename, []).extend(
87          ['-lines', str(start_line) + ':' + str(end_line)])
88
89  # Reformat files containing changes in place.
90  for filename, lines in lines_by_file.iteritems():
91    command = [binary, filename]
92    if args.i:
93      command.append('-i')
94    command.extend(lines)
95    if args.style:
96      command.extend(['-style', args.style])
97    p = subprocess.Popen(command, stdout=subprocess.PIPE,
98                         stderr=None, stdin=subprocess.PIPE)
99    stdout, stderr = p.communicate()
100    if p.returncode != 0:
101      sys.exit(p.returncode);
102
103    if not args.i:
104      with open(filename) as f:
105        code = f.readlines()
106      formatted_code = StringIO.StringIO(stdout).readlines()
107      diff = difflib.unified_diff(code, formatted_code,
108                                  filename, filename,
109                                  '(before formatting)', '(after formatting)')
110      diff_string = string.join(diff, '')
111      if len(diff_string) > 0:
112        sys.stdout.write(diff_string)
113
114if __name__ == '__main__':
115  main()
116