blob: fda7ba96e5957a5b91c98f120af4c1708ed8c9a4 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001#!/usr/bin/env python
2#
Ben Murdoch589d6972011-11-30 16:04:58 +00003# Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00004# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following
12# disclaimer in the documentation and/or other materials provided
13# with the distribution.
14# * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived
16# from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
Ben Murdochbb769b22010-08-11 14:56:33 +010030try:
31 import hashlib
32 md5er = hashlib.md5
33except ImportError, e:
34 import md5
35 md5er = md5.new
Steve Blocka7e24c12009-10-30 11:49:00 +000036
Ben Murdochbb769b22010-08-11 14:56:33 +010037
Steve Blocka7e24c12009-10-30 11:49:00 +000038import optparse
39import os
40from os.path import abspath, join, dirname, basename, exists
Steve Blockd0582a62009-12-15 09:54:21 +000041import pickle
Steve Blocka7e24c12009-10-30 11:49:00 +000042import re
43import sys
44import subprocess
Ben Murdoch589d6972011-11-30 16:04:58 +000045from subprocess import PIPE
Steve Blocka7e24c12009-10-30 11:49:00 +000046
47# Disabled LINT rules and reason.
48# build/include_what_you_use: Started giving false positives for variables
49# named "string" and "map" assuming that you needed to include STL headers.
50
51ENABLED_LINT_RULES = """
52build/class
53build/deprecated
54build/endif_comment
55build/forward_decl
56build/include_order
57build/printf_format
58build/storage_class
59legal/copyright
60readability/boost
61readability/braces
62readability/casting
63readability/check
64readability/constructors
65readability/fn_size
66readability/function
67readability/multiline_comment
68readability/multiline_string
69readability/streams
70readability/todo
71readability/utf8
72runtime/arrays
73runtime/casting
74runtime/deprecated_fn
75runtime/explicit
76runtime/int
77runtime/memset
78runtime/mutex
79runtime/nonconf
80runtime/printf
81runtime/printf_format
82runtime/references
83runtime/rtti
84runtime/sizeof
85runtime/string
86runtime/virtual
87runtime/vlog
88whitespace/blank_line
89whitespace/braces
90whitespace/comma
91whitespace/comments
Steve Blocka7e24c12009-10-30 11:49:00 +000092whitespace/ending_newline
93whitespace/indent
94whitespace/labels
95whitespace/line_length
96whitespace/newline
97whitespace/operators
98whitespace/parens
99whitespace/tab
100whitespace/todo
101""".split()
102
103
Steve Blockd0582a62009-12-15 09:54:21 +0000104class FileContentsCache(object):
105
106 def __init__(self, sums_file_name):
107 self.sums = {}
108 self.sums_file_name = sums_file_name
109
110 def Load(self):
111 try:
112 sums_file = None
113 try:
114 sums_file = open(self.sums_file_name, 'r')
115 self.sums = pickle.load(sums_file)
116 except IOError:
117 # File might not exist, this is OK.
118 pass
119 finally:
120 if sums_file:
121 sums_file.close()
122
123 def Save(self):
124 try:
125 sums_file = open(self.sums_file_name, 'w')
126 pickle.dump(self.sums, sums_file)
127 finally:
128 sums_file.close()
129
130 def FilterUnchangedFiles(self, files):
131 changed_or_new = []
132 for file in files:
133 try:
134 handle = open(file, "r")
Ben Murdochbb769b22010-08-11 14:56:33 +0100135 file_sum = md5er(handle.read()).digest()
Steve Blockd0582a62009-12-15 09:54:21 +0000136 if not file in self.sums or self.sums[file] != file_sum:
137 changed_or_new.append(file)
138 self.sums[file] = file_sum
139 finally:
140 handle.close()
141 return changed_or_new
142
143 def RemoveFile(self, file):
144 if file in self.sums:
145 self.sums.pop(file)
146
147
Steve Blocka7e24c12009-10-30 11:49:00 +0000148class SourceFileProcessor(object):
149 """
150 Utility class that can run through a directory structure, find all relevant
151 files and invoke a custom check on the files.
152 """
153
154 def Run(self, path):
155 all_files = []
156 for file in self.GetPathsToSearch():
157 all_files += self.FindFilesIn(join(path, file))
158 if not self.ProcessFiles(all_files, path):
159 return False
160 return True
161
162 def IgnoreDir(self, name):
Steve Blockd0582a62009-12-15 09:54:21 +0000163 return name.startswith('.') or name == 'data' or name == 'sputniktests'
Steve Blocka7e24c12009-10-30 11:49:00 +0000164
165 def IgnoreFile(self, name):
166 return name.startswith('.')
167
168 def FindFilesIn(self, path):
169 result = []
170 for (root, dirs, files) in os.walk(path):
171 for ignored in [x for x in dirs if self.IgnoreDir(x)]:
172 dirs.remove(ignored)
173 for file in files:
174 if not self.IgnoreFile(file) and self.IsRelevant(file):
175 result.append(join(root, file))
176 return result
177
178
179class CppLintProcessor(SourceFileProcessor):
180 """
181 Lint files to check that they follow the google code style.
182 """
183
184 def IsRelevant(self, name):
185 return name.endswith('.cc') or name.endswith('.h')
186
187 def IgnoreDir(self, name):
188 return (super(CppLintProcessor, self).IgnoreDir(name)
189 or (name == 'third_party'))
190
191 IGNORE_LINT = ['flag-definitions.h']
Steve Blockd0582a62009-12-15 09:54:21 +0000192
Steve Blocka7e24c12009-10-30 11:49:00 +0000193 def IgnoreFile(self, name):
194 return (super(CppLintProcessor, self).IgnoreFile(name)
195 or (name in CppLintProcessor.IGNORE_LINT))
196
197 def GetPathsToSearch(self):
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800198 return ['src', 'preparser', 'include', 'samples', join('test', 'cctest')]
Steve Blocka7e24c12009-10-30 11:49:00 +0000199
200 def ProcessFiles(self, files, path):
Steve Blockd0582a62009-12-15 09:54:21 +0000201 good_files_cache = FileContentsCache('.cpplint-cache')
202 good_files_cache.Load()
203 files = good_files_cache.FilterUnchangedFiles(files)
204 if len(files) == 0:
205 print 'No changes in files detected. Skipping cpplint check.'
206 return True
207
Steve Blocka7e24c12009-10-30 11:49:00 +0000208 filt = '-,' + ",".join(['+' + n for n in ENABLED_LINT_RULES])
209 command = ['cpplint.py', '--filter', filt] + join(files)
210 local_cpplint = join(path, "tools", "cpplint.py")
211 if exists(local_cpplint):
212 command = ['python', local_cpplint, '--filter', filt] + join(files)
Steve Blockd0582a62009-12-15 09:54:21 +0000213
214 process = subprocess.Popen(command, stderr=subprocess.PIPE)
215 LINT_ERROR_PATTERN = re.compile(r'^(.+)[:(]\d+[:)]')
216 while True:
217 out_line = process.stderr.readline()
218 if out_line == '' and process.poll() != None:
219 break
220 sys.stderr.write(out_line)
221 m = LINT_ERROR_PATTERN.match(out_line)
222 if m:
223 good_files_cache.RemoveFile(m.group(1))
224
225 good_files_cache.Save()
226 return process.returncode == 0
Steve Blocka7e24c12009-10-30 11:49:00 +0000227
228
229COPYRIGHT_HEADER_PATTERN = re.compile(
Leon Clarkee46be812010-01-19 14:06:41 +0000230 r'Copyright [\d-]*20[0-1][0-9] the V8 project authors. All rights reserved.')
Steve Blocka7e24c12009-10-30 11:49:00 +0000231
232class SourceProcessor(SourceFileProcessor):
233 """
Ben Murdoch589d6972011-11-30 16:04:58 +0000234 Check that all files include a copyright notice and no trailing whitespaces.
Steve Blocka7e24c12009-10-30 11:49:00 +0000235 """
236
237 RELEVANT_EXTENSIONS = ['.js', '.cc', '.h', '.py', '.c', 'SConscript',
Ben Murdoch589d6972011-11-30 16:04:58 +0000238 'SConstruct', '.status', '.gyp', '.gypi']
239
240 # Overwriting the one in the parent class.
241 def FindFilesIn(self, path):
242 if os.path.exists(path+'/.git'):
243 output = subprocess.Popen('git ls-files --full-name',
244 stdout=PIPE, cwd=path, shell=True)
245 result = []
246 for file in output.stdout.read().split():
247 for dir_part in os.path.dirname(file).split(os.sep):
248 if self.IgnoreDir(dir_part):
249 break
250 else:
251 if self.IsRelevant(file) and not self.IgnoreFile(file):
252 result.append(join(path, file))
253 if output.wait() == 0:
254 return result
255 return super(SourceProcessor, self).FindFilesIn(path)
256
Steve Blocka7e24c12009-10-30 11:49:00 +0000257 def IsRelevant(self, name):
258 for ext in SourceProcessor.RELEVANT_EXTENSIONS:
259 if name.endswith(ext):
260 return True
261 return False
262
263 def GetPathsToSearch(self):
264 return ['.']
265
266 def IgnoreDir(self, name):
267 return (super(SourceProcessor, self).IgnoreDir(name)
268 or (name == 'third_party')
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000269 or (name == 'gyp')
270 or (name == 'out')
Steve Blocka7e24c12009-10-30 11:49:00 +0000271 or (name == 'obj'))
272
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000273 IGNORE_COPYRIGHTS = ['cpplint.py',
274 'earley-boyer.js',
275 'raytrace.js',
276 'crypto.js',
277 'libraries.cc',
278 'libraries-empty.cc',
279 'jsmin.py',
280 'regexp-pcre.js']
281 IGNORE_TABS = IGNORE_COPYRIGHTS + ['unicode-test.js', 'html-comments.js']
Steve Blocka7e24c12009-10-30 11:49:00 +0000282
283 def ProcessContents(self, name, contents):
284 result = True
285 base = basename(name)
286 if not base in SourceProcessor.IGNORE_TABS:
287 if '\t' in contents:
288 print "%s contains tabs" % name
289 result = False
290 if not base in SourceProcessor.IGNORE_COPYRIGHTS:
291 if not COPYRIGHT_HEADER_PATTERN.search(contents):
292 print "%s is missing a correct copyright header." % name
293 result = False
Ben Murdoch589d6972011-11-30 16:04:58 +0000294 ext = base.split('.').pop()
295 if ' \n' in contents or contents.endswith(' '):
296 line = 0
297 lines = []
298 parts = contents.split(' \n')
299 if not contents.endswith(' '):
300 parts.pop()
301 for part in parts:
302 line += part.count('\n') + 1
303 lines.append(str(line))
304 linenumbers = ', '.join(lines)
305 if len(lines) > 1:
306 print "%s has trailing whitespaces in lines %s." % (name, linenumbers)
307 else:
308 print "%s has trailing whitespaces in line %s." % (name, linenumbers)
309 result = False
Steve Blocka7e24c12009-10-30 11:49:00 +0000310 return result
311
312 def ProcessFiles(self, files, path):
313 success = True
Ben Murdoch589d6972011-11-30 16:04:58 +0000314 violations = 0
Steve Blocka7e24c12009-10-30 11:49:00 +0000315 for file in files:
316 try:
317 handle = open(file)
318 contents = handle.read()
Ben Murdoch589d6972011-11-30 16:04:58 +0000319 if not self.ProcessContents(file, contents):
320 success = False
321 violations += 1
Steve Blocka7e24c12009-10-30 11:49:00 +0000322 finally:
323 handle.close()
Ben Murdoch589d6972011-11-30 16:04:58 +0000324 print "Total violating files: %s" % violations
Steve Blocka7e24c12009-10-30 11:49:00 +0000325 return success
326
327
328def GetOptions():
329 result = optparse.OptionParser()
330 result.add_option('--no-lint', help="Do not run cpplint", default=False,
331 action="store_true")
332 return result
333
334
335def Main():
336 workspace = abspath(join(dirname(sys.argv[0]), '..'))
337 parser = GetOptions()
338 (options, args) = parser.parse_args()
339 success = True
Ben Murdoch589d6972011-11-30 16:04:58 +0000340 print "Running C++ lint check..."
Steve Blocka7e24c12009-10-30 11:49:00 +0000341 if not options.no_lint:
342 success = CppLintProcessor().Run(workspace) and success
Ben Murdoch589d6972011-11-30 16:04:58 +0000343 print "Running copyright header and trailing whitespaces check..."
Steve Blocka7e24c12009-10-30 11:49:00 +0000344 success = SourceProcessor().Run(workspace) and success
345 if success:
346 return 0
347 else:
348 return 1
349
350
351if __name__ == '__main__':
352 sys.exit(Main())