r196917 - clang-format-diff.py: Support -regex filter and more filename extensions

Alp Toker alp at nuanti.com
Tue Dec 10 05:51:53 PST 2013


Author: alp
Date: Tue Dec 10 07:51:53 2013
New Revision: 196917

URL: http://llvm.org/viewvc/llvm-project?rev=196917&view=rev
Log:
clang-format-diff.py: Support -regex filter and more filename extensions

Add support for more filename extensions based on the list in the clang
plus JavaScript.

Also adds a -regex option so users can override defaults if they have unusual
file extensions or want to format everything in the diff.

Keeping with tradition the flag is modelled on Unix conventions, this time
matching the semantics of find(1).

Modified:
    cfe/trunk/docs/ClangFormat.rst
    cfe/trunk/tools/clang-format/clang-format-diff.py

Modified: cfe/trunk/docs/ClangFormat.rst
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/ClangFormat.rst?rev=196917&r1=196916&r2=196917&view=diff
==============================================================================
--- cfe/trunk/docs/ClangFormat.rst (original)
+++ cfe/trunk/docs/ClangFormat.rst Tue Dec 10 07:51:53 2013
@@ -158,17 +158,18 @@ a unified diff and reformats all contain
 
 .. code-block:: console
 
-  usage: clang-format-diff.py [-h] [-i] [-p P] [-style STYLE]
+  usage: clang-format-diff.py [-h] [-i] [-p NUM] [-regex PATTERN] [-style STYLE]
 
   Reformat changed lines in diff. Without -i option just output the diff that
   would be introduced.
 
   optional arguments:
-    -h, --help    show this help message and exit
-    -i            apply edits to files instead of displaying a diff
-    -p P          strip the smallest prefix containing P slashes
-    -style STYLE  formatting style to apply (LLVM, Google, Chromium, Mozilla,
-                  WebKit)
+    -h, --help      show this help message and exit
+    -i              apply edits to files instead of displaying a diff
+    -p NUM          strip the smallest prefix containing P slashes
+    -regex PATTERN  custom pattern selecting file paths to reformat
+    -style STYLE    formatting style to apply (LLVM, Google, Chromium, Mozilla,
+                    WebKit)
 
 So to reformat all the lines in the latest :program:`git` commit, just do:
 

Modified: cfe/trunk/tools/clang-format/clang-format-diff.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format-diff.py?rev=196917&r1=196916&r2=196917&view=diff
==============================================================================
--- cfe/trunk/tools/clang-format/clang-format-diff.py (original)
+++ cfe/trunk/tools/clang-format/clang-format-diff.py Tue Dec 10 07:51:53 2013
@@ -41,8 +41,11 @@ def main():
                                    'introduced.')
   parser.add_argument('-i', action='store_true', default=False,
                       help='apply edits to files instead of displaying a diff')
-  parser.add_argument('-p', default=0,
+  parser.add_argument('-p', metavar='NUM', default=0,
                       help='strip the smallest prefix containing P slashes')
+  parser.add_argument('-regex', metavar='PATTERN', default=
+                      r'.*\.(cpp|cc|CPP|C|c\+\+|cxx|c|h|hpp|m|mm|inc|js)',
+                      help='custom pattern selecting file paths to reformat')
   parser.add_argument(
       '-style',
       help=
@@ -59,9 +62,7 @@ def main():
     if filename == None:
       continue
 
-    # FIXME: Add other types containing C++/ObjC code.
-    if not (filename.endswith(".cpp") or filename.endswith(".cc") or
-            filename.endswith(".h")):
+    if not re.match(args.regex, filename):
       continue
 
     match = re.search('^@@.*\+(\d+)(,(\d+))?', line)





More information about the cfe-commits mailing list