Bug 1391231 - Use the list provided by .clang-format-ignore instead of only thirdpartypath.txt r?gps draft
authorSylvestre Ledru <sledru@mozilla.com>
Thu, 17 Aug 2017 15:22:06 +0200
changeset 649128 005febfcfec5b2fbce00baa2474f88599d240956
parent 648195 6df5a4a0b721eb2890bf274a05d7d17ede8edd41
child 727011 bc8b249c70e08667c6bef22db808ba8d13cde868
push id74960
push userbmo:sledru@mozilla.com
push dateFri, 18 Aug 2017 17:48:28 +0000
reviewersgps
bugs1391231
milestone57.0a1
Bug 1391231 - Use the list provided by .clang-format-ignore instead of only thirdpartypath.txt r?gps MozReview-Commit-ID: CY1fS7Fg1y
tools/mach_commands.py
--- a/tools/mach_commands.py
+++ b/tools/mach_commands.py
@@ -4,16 +4,17 @@
 
 from __future__ import absolute_import, unicode_literals
 
 import sys
 import os
 import stat
 import platform
 import errno
+import re
 
 from mach.decorators import (
     CommandArgument,
     CommandProvider,
     Command,
 )
 
 from mozbuild.base import MachCommandBase, MozbuildObject
@@ -271,39 +272,43 @@ class FormatProvider(MachCommandBase):
         args = [sys.executable, clang_format_diff, "-p1"]
         if not show:
             args.append("-i")
         cf_process = Popen(args, stdin=diff_process.stdout)
         return cf_process.communicate()[0]
 
     def generate_path_list(self, paths):
         pathToThirdparty = os.path.join(self.topsrcdir,
-                                        "tools",
-                                        "rewriting",
-                                        "ThirdPartyPaths.txt")
-        with open(pathToThirdparty) as f:
-            # Normalize the path (no trailing /)
-            ignored_dir = tuple(d.rstrip('/') for d in f.read().splitlines())
+                                        ".clang-format-ignore")
+        ignored_dir = []
+        for line in open(pathToThirdparty):
+            # Remove comments and empty lines
+            if line.startswith('#') or len(line.strip()) == 0:
+                continue
+            ignored_dir.append(line.rstrip())
 
+        # Generates the list of regexp
+        ignored_dir_re = '(%s)' % '|'.join(ignored_dir)
         extensions = ('.cpp', '.c', '.h')
 
         path_list = []
         for f in paths:
-            if f.startswith(ignored_dir):
+            if re.match(ignored_dir_re, f):
+                # Early exit if we have provided an ignored directory
                 print("clang-format: Ignored third party code '{0}'".format(f))
                 continue
 
             if os.path.isdir(f):
                 # Processing a directory, generate the file list
                 for folder, subs, files in os.walk(f):
                     subs.sort()
                     for filename in sorted(files):
                         f_in_dir = os.path.join(folder, filename)
-                        if f_in_dir.endswith(extensions):
-                            # Supported extension
+                        if f_in_dir.endswith(extensions) and not re.match(ignored_dir_re, f_in_dir):
+                            # Supported extension and accepted path
                             path_list.append(f_in_dir)
             else:
                 if f.endswith(extensions):
                     path_list.append(f)
 
         return path_list
 
     def run_clang_format_path(self, clang_format, show, paths):