1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Top-level presubmit script for cc.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into depot_tools.
14 CC_SOURCE_FILES
=(r
'^cc[\\/].*\.(cc|h)$',)
16 def CheckChangeLintsClean(input_api
, output_api
):
17 source_filter
= lambda x
: input_api
.FilterSourceFile(
18 x
, white_list
=CC_SOURCE_FILES
, black_list
=None)
20 return input_api
.canned_checks
.CheckChangeLintsClean(
21 input_api
, output_api
, source_filter
, lint_filters
=[], verbose_level
=1)
23 def CheckAsserts(input_api
, output_api
, white_list
=CC_SOURCE_FILES
, black_list
=None):
24 black_list
= tuple(black_list
or input_api
.DEFAULT_BLACK_LIST
)
25 source_file_filter
= lambda x
: input_api
.FilterSourceFile(x
, white_list
, black_list
)
30 for f
in input_api
.AffectedSourceFiles(source_file_filter
):
31 contents
= input_api
.ReadFile(f
, 'rb')
32 # WebKit ASSERT() is not allowed.
33 if re
.search(r
"\bASSERT\(", contents
):
34 assert_files
.append(f
.LocalPath())
35 # WebKit ASSERT_NOT_REACHED() is not allowed.
36 if re
.search(r
"ASSERT_NOT_REACHED\(", contents
):
37 notreached_files
.append(f
.LocalPath())
40 return [output_api
.PresubmitError(
41 'These files use ASSERT instead of using DCHECK:',
44 return [output_api
.PresubmitError(
45 'These files use ASSERT_NOT_REACHED instead of using NOTREACHED:',
46 items
=notreached_files
)]
49 def CheckStdAbs(input_api
, output_api
,
50 white_list
=CC_SOURCE_FILES
, black_list
=None):
51 black_list
= tuple(black_list
or input_api
.DEFAULT_BLACK_LIST
)
52 source_file_filter
= lambda x
: input_api
.FilterSourceFile(x
,
56 using_std_abs_files
= []
58 missing_std_prefix_files
= []
60 for f
in input_api
.AffectedSourceFiles(source_file_filter
):
61 contents
= input_api
.ReadFile(f
, 'rb')
62 if re
.search(r
"using std::f?abs;", contents
):
63 using_std_abs_files
.append(f
.LocalPath())
64 if re
.search(r
"\bfabsf?\(", contents
):
65 found_fabs_files
.append(f
.LocalPath());
67 no_std_prefix
= r
"(?<!std::)"
68 # Matches occurrences of abs/absf/fabs/fabsf without a "std::" prefix.
69 abs_without_prefix
= r
"%s(\babsf?\()" % no_std_prefix
70 fabs_without_prefix
= r
"%s(\bfabsf?\()" % no_std_prefix
71 # Skips matching any lines that have "// NOLINT".
72 no_nolint
= r
"(?![^\n]*//\s+NOLINT)"
74 expression
= re
.compile("(%s|%s)%s" %
75 (abs_without_prefix
, fabs_without_prefix
, no_nolint
))
76 if expression
.search(contents
):
77 missing_std_prefix_files
.append(f
.LocalPath())
80 if using_std_abs_files
:
81 result
.append(output_api
.PresubmitError(
82 'These files have "using std::abs" which is not permitted.',
83 items
=using_std_abs_files
))
85 result
.append(output_api
.PresubmitError(
86 'std::abs() should be used instead of std::fabs() for consistency.',
87 items
=found_fabs_files
))
88 if missing_std_prefix_files
:
89 result
.append(output_api
.PresubmitError(
90 'These files use abs(), absf(), fabs(), or fabsf() without qualifying '
91 'the std namespace. Please use std::abs() in all places.',
92 items
=missing_std_prefix_files
))
95 def CheckPassByValue(input_api
,
97 white_list
=CC_SOURCE_FILES
,
99 black_list
= tuple(black_list
or input_api
.DEFAULT_BLACK_LIST
)
100 source_file_filter
= lambda x
: input_api
.FilterSourceFile(x
,
106 # Well-defined simple classes containing only <= 4 ints, or <= 2 floats.
107 pass_by_value_types
= ['base::Time',
111 for f
in input_api
.AffectedSourceFiles(source_file_filter
):
112 contents
= input_api
.ReadFile(f
, 'rb')
114 r
'\bconst +' + '(?P<type>(%s))&' %
115 string
.join(pass_by_value_types
, '|'),
118 local_errors
.append(output_api
.PresubmitError(
119 '%s passes %s by const ref instead of by value.' %
120 (f
.LocalPath(), match
.group('type'))))
123 def CheckTodos(input_api
, output_api
):
126 source_file_filter
= lambda x
: x
127 for f
in input_api
.AffectedSourceFiles(source_file_filter
):
128 contents
= input_api
.ReadFile(f
, 'rb')
129 if ('FIX'+'ME') in contents
:
130 errors
.append(f
.LocalPath())
133 return [output_api
.PresubmitError(
134 'All TODO comments should be of the form TODO(name). ' +
135 'Use TODO instead of FIX' + 'ME',
139 def CheckDoubleAngles(input_api
, output_api
, white_list
=CC_SOURCE_FILES
,
143 source_file_filter
= lambda x
: input_api
.FilterSourceFile(x
,
146 for f
in input_api
.AffectedSourceFiles(source_file_filter
):
147 contents
= input_api
.ReadFile(f
, 'rb')
148 if ('> >') in contents
:
149 errors
.append(f
.LocalPath())
152 return [output_api
.PresubmitError('Use >> instead of > >:', items
=errors
)]
155 def CheckScopedPtr(input_api
, output_api
,
156 white_list
=CC_SOURCE_FILES
, black_list
=None):
157 black_list
= tuple(black_list
or input_api
.DEFAULT_BLACK_LIST
)
158 source_file_filter
= lambda x
: input_api
.FilterSourceFile(x
,
162 for f
in input_api
.AffectedSourceFiles(source_file_filter
):
163 for line_number
, line
in f
.ChangedContents():
165 # return scoped_ptr<T>(foo);
166 # bar = scoped_ptr<T>(foo);
168 # return scoped_ptr<T[]>(foo);
169 # bar = scoped_ptr<T[]>(foo);
170 if re
.search(r
'(=|\breturn)\s*scoped_ptr<.*?(?<!])>\([^)]+\)', line
):
171 errors
.append(output_api
.PresubmitError(
172 ('%s:%d uses explicit scoped_ptr constructor. ' +
173 'Use make_scoped_ptr() instead.') % (f
.LocalPath(), line_number
)))
176 if re
.search(r
'\bscoped_ptr<.*?>\(\)', line
):
177 errors
.append(output_api
.PresubmitError(
178 '%s:%d uses scoped_ptr<T>(). Use nullptr instead.' %
179 (f
.LocalPath(), line_number
)))
182 def FindUnquotedQuote(contents
, pos
):
183 match
= re
.search(r
"(?<!\\)(?P<quote>\")", contents[pos:])
184 return -1 if not match else match.start("quote
") + pos
186 def FindUselessIfdefs(input_api, output_api):
188 source_file_filter = lambda x: x
189 for f in input_api.AffectedSourceFiles(source_file_filter):
190 contents = input_api.ReadFile(f, 'rb')
191 if re.search(r'#if\s*0\s', contents):
192 errors.append(f.LocalPath())
194 return [output_api.PresubmitError(
195 'Don\'t use #if '+'0; just delete the code.',
199 def FindNamespaceInBlock(pos, namespace, contents, whitelist=[]):
206 while pos < len(contents) and brace_count > 0:
207 if open_brace < pos: open_brace = contents.find("{", pos)
208 if close_brace < pos: close_brace = contents.find("}", pos)
209 if quote < pos: quote = FindUnquotedQuote(contents, pos)
210 if name < pos: name = contents.find(("%s::" % namespace), pos)
213 return False # The namespace is not used at all.
215 open_brace = len(contents)
217 close_brace = len(contents)
219 quote = len(contents)
221 next = min(open_brace, min(close_brace, min(quote, name)))
223 if next == open_brace:
225 elif next == close_brace:
228 quote_count = 0 if quote_count else 1
229 elif next == name and not quote_count:
232 if re.match(w, contents[next:]):
240 # Checks for the use of cc:: within the cc namespace, which is usually
242 def CheckNamespace(input_api, output_api):
245 source_file_filter = lambda x: x
246 for f in input_api.AffectedSourceFiles(source_file_filter):
247 contents = input_api.ReadFile(f, 'rb')
248 match = re.search(r'namespace\s*cc\s*{', contents)
253 if FindNamespaceInBlock(match.end(), 'cc', contents, whitelist=whitelist):
254 errors.append(f.LocalPath())
257 return [output_api.PresubmitError(
258 'Do not use cc:: inside of the cc namespace.',
262 def CheckForUseOfWrongClock(input_api,
264 white_list=CC_SOURCE_FILES,
266 """Make sure new lines of code don't use a clock susceptible to skew."""
267 black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST)
268 source_file_filter = lambda x: input_api.FilterSourceFile(x,
271 # Regular expression that should detect any explicit references to the
272 # base::Time type (or base::Clock/DefaultClock), whether in using decls,
273 # typedefs, or to call static methods.
274 base_time_type_pattern = r'(^|\W)base::(Time|Clock|DefaultClock)(\W|$)'
276 # Regular expression that should detect references to the base::Time class
277 # members, such as a call to base::Time::Now.
278 base_time_member_pattern = r'(^|\W)(Time|Clock|DefaultClock)::'
280 # Regular expression to detect "using base
::Time
" declarations. We want to
281 # prevent these from triggerring a warning. For example, it's perfectly
282 # reasonable for code to be written like this:
286 # int64 foo_us = foo_s * Time::kMicrosecondsPerSecond;
287 using_base_time_decl_pattern = r'^\s*using\s+(::)?base::Time\s*;'
289 # Regular expression to detect references to the kXXX constants in the
290 # base::Time class. We want to prevent these from triggerring a warning.
291 base_time_konstant_pattern = r'(^|\W)Time::k\w+'
293 problem_re = input_api.re.compile(
294 r'(' + base_time_type_pattern + r')|(' + base_time_member_pattern + r')')
295 exception_re = input_api.re.compile(
296 r'(' + using_base_time_decl_pattern + r')|(' +
297 base_time_konstant_pattern + r')')
299 for f in input_api.AffectedSourceFiles(source_file_filter):
300 for line_number, line in f.ChangedContents():
301 if problem_re.search(line):
302 if not exception_re.search(line):
304 ' %s:%d\n %s' % (f.LocalPath(), line_number, line.strip()))
307 return [output_api.PresubmitPromptOrNotify(
308 'You added one or more references to the base::Time class and/or one\n'
309 'of its member functions (or base::Clock/DefaultClock). In cc code,\n'
310 'it is most certainly incorrect! Instead use base::TimeTicks.\n\n'
311 '\n'.join(problems))]
315 def CheckChangeOnUpload(input_api, output_api):
317 results += CheckAsserts(input_api, output_api)
318 results += CheckStdAbs(input_api, output_api)
319 results += CheckPassByValue(input_api, output_api)
320 results += CheckChangeLintsClean(input_api, output_api)
321 results += CheckTodos(input_api, output_api)
322 results += CheckDoubleAngles(input_api, output_api)
323 results += CheckScopedPtr(input_api, output_api)
324 results += CheckNamespace(input_api, output_api)
325 results += CheckForUseOfWrongClock(input_api, output_api)
326 results += FindUselessIfdefs(input_api, output_api)
327 results += input_api.canned_checks.CheckPatchFormatted(input_api, output_api)
330 def GetPreferredTryMasters(project, change):
333 'linux_blink_rel': set(['defaulttests']),
337 def PostUploadHook(cl, change, output_api):
338 """git cl upload will call this hook after the issue is created/modified.
340 This hook adds extra try bots list to the CL description in order to run
341 Blink tests in addition to CQ try bots.
343 rietveld_obj = cl.RpcServer()
345 description = rietveld_obj.get_description(issue)
346 if re.search(r'^CQ_INCLUDE_TRYBOTS=.*', description, re.M | re.I):
349 bots = GetPreferredTryMasters(None, change)
350 bots_string_bits = []
351 for master in bots.keys():
352 bots_string_bits.append("%s:%s" % (master, ','.join(bots[master].keys())))
355 new_description = description
356 new_description += '\nCQ_INCLUDE_TRYBOTS=%s' % ';'.join(bots_string_bits)
357 results.append(output_api.PresubmitNotifyResult(
358 'Automatically added Perf trybots to run Blink tests on CQ.'))
360 if new_description != description:
361 rietveld_obj.update_description(issue, new_description)