blob: 6751f06d18b4676e92e14bba1c44d6b3379c4ed3 [file] [log] [blame]
[email protected]a18130a2012-01-03 17:52:081# Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ca8d1982009-02-19 16:33:122# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Top-level presubmit script for Chromium.
6
[email protected]f1293792009-07-31 18:09:567See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
[email protected]50d7d721e2009-11-15 17:56:188for more details about the presubmit API built into gcl.
[email protected]ca8d1982009-02-19 16:33:129"""
10
[email protected]eea609a2011-11-18 13:10:1211
[email protected]9d16ad12011-12-14 20:49:4712import re
[email protected]55f9f382012-07-31 11:02:1813import sys
[email protected]9d16ad12011-12-14 20:49:4714
15
[email protected]379e7dd2010-01-28 17:39:2116_EXCLUDED_PATHS = (
[email protected]3e4eb112011-01-18 03:29:5417 r"^breakpad[\\\/].*",
[email protected]40d1dbb2012-10-26 07:18:0018 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_rules.py",
19 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_simple.py",
[email protected]8886ffcb2013-02-12 04:56:2820 r"^native_client_sdk[\\\/]src[\\\/]tools[\\\/].*.mk",
[email protected]a18130a2012-01-03 17:52:0821 r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*",
[email protected]3e4eb112011-01-18 03:29:5422 r"^skia[\\\/].*",
23 r"^v8[\\\/].*",
24 r".*MakeFile$",
[email protected]1084ccc2012-03-14 03:22:5325 r".+_autogen\.h$",
[email protected]ce145c02012-09-06 09:49:3426 r".+[\\\/]pnacl_shim\.c$",
[email protected]e07b6ac72013-08-20 00:30:4227 r"^gpu[\\\/]config[\\\/].*_list_json\.cc$",
[email protected]d2600602014-02-19 00:09:1928 r"^chrome[\\\/]browser[\\\/]resources[\\\/]pdf[\\\/]index.js"
[email protected]4306417642009-06-11 00:33:4029)
[email protected]ca8d1982009-02-19 16:33:1230
jochen9ea8fdbc2014-09-25 13:21:3531# The NetscapePlugIn library is excluded from pan-project as it will soon
32# be deleted together with the rest of the NPAPI and it's not worthwhile to
33# update the coding style until then.
[email protected]3de922f2013-12-20 13:27:3834_TESTRUNNER_PATHS = (
[email protected]de28fed2e2014-02-01 14:36:3235 r"^content[\\\/]shell[\\\/]tools[\\\/]plugin[\\\/].*",
[email protected]3de922f2013-12-20 13:27:3836)
37
[email protected]06e6d0ff2012-12-11 01:36:4438# Fragment of a regular expression that matches C++ and Objective-C++
39# implementation files.
40_IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
41
42# Regular expression that matches code only used for test binaries
43# (best effort).
44_TEST_CODE_EXCLUDED_PATHS = (
joaodasilva718f87672014-08-30 09:25:4945 r'.*[\\\/](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS,
[email protected]06e6d0ff2012-12-11 01:36:4446 r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS,
[email protected]6e04f8c2014-01-29 18:08:3247 r'.+_(api|browser|kif|perf|pixel|unit|ui)?test(_[a-z]+)?%s' %
[email protected]e2d7e6f2013-04-23 12:57:1248 _IMPLEMENTATION_EXTENSIONS,
[email protected]06e6d0ff2012-12-11 01:36:4449 r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS,
joaodasilva718f87672014-08-30 09:25:4950 r'.*[\\\/](test|tool(s)?)[\\\/].*',
[email protected]ef070cc2013-05-03 11:53:0551 # content_shell is used for running layout tests.
joaodasilva718f87672014-08-30 09:25:4952 r'content[\\\/]shell[\\\/].*',
[email protected]06e6d0ff2012-12-11 01:36:4453 # At request of folks maintaining this folder.
joaodasilva718f87672014-08-30 09:25:4954 r'chrome[\\\/]browser[\\\/]automation[\\\/].*',
[email protected]7b054982013-11-27 00:44:4755 # Non-production example code.
joaodasilva718f87672014-08-30 09:25:4956 r'mojo[\\\/]examples[\\\/].*',
[email protected]8176de12014-06-20 19:07:0857 # Launcher for running iOS tests on the simulator.
joaodasilva718f87672014-08-30 09:25:4958 r'testing[\\\/]iossim[\\\/]iossim\.mm$',
[email protected]06e6d0ff2012-12-11 01:36:4459)
[email protected]ca8d1982009-02-19 16:33:1260
[email protected]eea609a2011-11-18 13:10:1261_TEST_ONLY_WARNING = (
62 'You might be calling functions intended only for testing from\n'
63 'production code. It is OK to ignore this warning if you know what\n'
64 'you are doing, as the heuristics used to detect the situation are\n'
[email protected]b0149772014-03-27 16:47:5865 'not perfect. The commit queue will not block on this warning.')
[email protected]eea609a2011-11-18 13:10:1266
67
[email protected]cf9b78f2012-11-14 11:40:2868_INCLUDE_ORDER_WARNING = (
69 'Your #include order seems to be broken. Send mail to\n'
70 '[email protected] if this is not the case.')
71
72
[email protected]127f18ec2012-06-16 05:05:5973_BANNED_OBJC_FUNCTIONS = (
74 (
75 'addTrackingRect:',
[email protected]23e6cbc2012-06-16 18:51:2076 (
77 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
[email protected]127f18ec2012-06-16 05:05:5978 'prohibited. Please use CrTrackingArea instead.',
79 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
80 ),
81 False,
82 ),
83 (
[email protected]eaae1972014-04-16 04:17:2684 r'/NSTrackingArea\W',
[email protected]23e6cbc2012-06-16 18:51:2085 (
86 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
[email protected]127f18ec2012-06-16 05:05:5987 'instead.',
88 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
89 ),
90 False,
91 ),
92 (
93 'convertPointFromBase:',
[email protected]23e6cbc2012-06-16 18:51:2094 (
95 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:5996 'Please use |convertPoint:(point) fromView:nil| instead.',
97 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
98 ),
99 True,
100 ),
101 (
102 'convertPointToBase:',
[email protected]23e6cbc2012-06-16 18:51:20103 (
104 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59105 'Please use |convertPoint:(point) toView:nil| instead.',
106 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
107 ),
108 True,
109 ),
110 (
111 'convertRectFromBase:',
[email protected]23e6cbc2012-06-16 18:51:20112 (
113 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59114 'Please use |convertRect:(point) fromView:nil| instead.',
115 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
116 ),
117 True,
118 ),
119 (
120 'convertRectToBase:',
[email protected]23e6cbc2012-06-16 18:51:20121 (
122 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59123 'Please use |convertRect:(point) toView:nil| instead.',
124 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
125 ),
126 True,
127 ),
128 (
129 'convertSizeFromBase:',
[email protected]23e6cbc2012-06-16 18:51:20130 (
131 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59132 'Please use |convertSize:(point) fromView:nil| instead.',
133 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
134 ),
135 True,
136 ),
137 (
138 'convertSizeToBase:',
[email protected]23e6cbc2012-06-16 18:51:20139 (
140 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
[email protected]127f18ec2012-06-16 05:05:59141 'Please use |convertSize:(point) toView:nil| instead.',
142 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
143 ),
144 True,
145 ),
146)
147
148
149_BANNED_CPP_FUNCTIONS = (
[email protected]23e6cbc2012-06-16 18:51:20150 # Make sure that gtest's FRIEND_TEST() macro is not used; the
151 # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
[email protected]e00ccc92012-11-01 17:32:30152 # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
[email protected]23e6cbc2012-06-16 18:51:20153 (
154 'FRIEND_TEST(',
155 (
[email protected]e3c945502012-06-26 20:01:49156 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
[email protected]23e6cbc2012-06-16 18:51:20157 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
158 ),
159 False,
[email protected]7345da02012-11-27 14:31:49160 (),
[email protected]23e6cbc2012-06-16 18:51:20161 ),
162 (
163 'ScopedAllowIO',
164 (
[email protected]e3c945502012-06-26 20:01:49165 'New code should not use ScopedAllowIO. Post a task to the blocking',
166 'pool or the FILE thread instead.',
[email protected]23e6cbc2012-06-16 18:51:20167 ),
[email protected]e3c945502012-06-26 20:01:49168 True,
[email protected]7345da02012-11-27 14:31:49169 (
thestig75844fdb2014-09-09 19:47:10170 r"^base[\\\/]process[\\\/]process_metrics_linux\.cc$",
[email protected]b01b9e22014-06-03 22:20:19171 r"^chrome[\\\/]browser[\\\/]chromeos[\\\/]boot_times_loader\.cc$",
philipj3f9d5bde2014-08-28 14:09:09172 r"^components[\\\/]crash[\\\/]app[\\\/]breakpad_mac\.mm$",
[email protected]de7d61ff2013-08-20 11:30:41173 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_browser_main\.cc$",
174 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_message_filter\.cc$",
jamesra03ae492014-10-03 04:26:48175 r"^mojo[\\\/]edk[\\\/]embedder[\\\/]" +
176 r"simple_platform_shared_buffer_posix\.cc$",
[email protected]398ad132013-04-02 15:11:01177 r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
[email protected]1f52a572014-05-12 23:21:54178 r"^net[\\\/]url_request[\\\/]test_url_fetcher_factory\.cc$",
[email protected]7345da02012-11-27 14:31:49179 ),
[email protected]23e6cbc2012-06-16 18:51:20180 ),
[email protected]52657f62013-05-20 05:30:31181 (
182 'SkRefPtr',
183 (
184 'The use of SkRefPtr is prohibited. ',
185 'Please use skia::RefPtr instead.'
186 ),
187 True,
188 (),
189 ),
190 (
191 'SkAutoRef',
192 (
193 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
194 'Please use skia::RefPtr instead.'
195 ),
196 True,
197 (),
198 ),
199 (
200 'SkAutoTUnref',
201 (
202 'The use of SkAutoTUnref is dangerous because it implicitly ',
203 'converts to a raw pointer. Please use skia::RefPtr instead.'
204 ),
205 True,
206 (),
207 ),
208 (
209 'SkAutoUnref',
210 (
211 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
212 'because it implicitly converts to a raw pointer. ',
213 'Please use skia::RefPtr instead.'
214 ),
215 True,
216 (),
217 ),
[email protected]d89eec82013-12-03 14:10:59218 (
219 r'/HANDLE_EINTR\(.*close',
220 (
221 'HANDLE_EINTR(close) is invalid. If close fails with EINTR, the file',
222 'descriptor will be closed, and it is incorrect to retry the close.',
223 'Either call close directly and ignore its return value, or wrap close',
224 'in IGNORE_EINTR to use its return value. See http://crbug.com/269623'
225 ),
226 True,
227 (),
228 ),
229 (
230 r'/IGNORE_EINTR\((?!.*close)',
231 (
232 'IGNORE_EINTR is only valid when wrapping close. To wrap other system',
233 'calls, use HANDLE_EINTR. See http://crbug.com/269623',
234 ),
235 True,
236 (
237 # Files that #define IGNORE_EINTR.
238 r'^base[\\\/]posix[\\\/]eintr_wrapper\.h$',
239 r'^ppapi[\\\/]tests[\\\/]test_broker\.cc$',
240 ),
241 ),
[email protected]ec5b3f02014-04-04 18:43:43242 (
243 r'/v8::Extension\(',
244 (
245 'Do not introduce new v8::Extensions into the code base, use',
246 'gin::Wrappable instead. See http://crbug.com/334679',
247 ),
248 True,
[email protected]f55c90ee62014-04-12 00:50:03249 (
joaodasilva718f87672014-08-30 09:25:49250 r'extensions[\\\/]renderer[\\\/]safe_builtins\.*',
[email protected]f55c90ee62014-04-12 00:50:03251 ),
[email protected]ec5b3f02014-04-04 18:43:43252 ),
[email protected]127f18ec2012-06-16 05:05:59253)
254
mlamouria82272622014-09-16 18:45:04255_IPC_ENUM_TRAITS_DEPRECATED = (
256 'You are using IPC_ENUM_TRAITS() in your code. It has been deprecated.\n'
257 'See http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc')
258
[email protected]127f18ec2012-06-16 05:05:59259
[email protected]b00342e7f2013-03-26 16:21:54260_VALID_OS_MACROS = (
261 # Please keep sorted.
262 'OS_ANDROID',
[email protected]f4440b42014-03-19 05:47:01263 'OS_ANDROID_HOST',
[email protected]b00342e7f2013-03-26 16:21:54264 'OS_BSD',
265 'OS_CAT', # For testing.
266 'OS_CHROMEOS',
267 'OS_FREEBSD',
268 'OS_IOS',
269 'OS_LINUX',
270 'OS_MACOSX',
271 'OS_NACL',
272 'OS_OPENBSD',
273 'OS_POSIX',
[email protected]eda7afa12014-02-06 12:27:37274 'OS_QNX',
[email protected]b00342e7f2013-03-26 16:21:54275 'OS_SOLARIS',
[email protected]b00342e7f2013-03-26 16:21:54276 'OS_WIN',
277)
278
279
[email protected]55459852011-08-10 15:17:19280def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
281 """Attempts to prevent use of functions intended only for testing in
282 non-testing code. For now this is just a best-effort implementation
283 that ignores header files and may have some false positives. A
284 better implementation would probably need a proper C++ parser.
285 """
286 # We only scan .cc files and the like, as the declaration of
287 # for-testing functions in header files are hard to distinguish from
288 # calls to such functions without a proper C++ parser.
[email protected]06e6d0ff2012-12-11 01:36:44289 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
[email protected]55459852011-08-10 15:17:19290
[email protected]23501822014-05-14 02:06:09291 base_function_pattern = r'[ :]test::[^\s]+|ForTest(ing)?|for_test(ing)?'
[email protected]55459852011-08-10 15:17:19292 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
[email protected]23501822014-05-14 02:06:09293 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern)
[email protected]55459852011-08-10 15:17:19294 exclusion_pattern = input_api.re.compile(
295 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
296 base_function_pattern, base_function_pattern))
297
298 def FilterFile(affected_file):
[email protected]06e6d0ff2012-12-11 01:36:44299 black_list = (_EXCLUDED_PATHS +
300 _TEST_CODE_EXCLUDED_PATHS +
301 input_api.DEFAULT_BLACK_LIST)
[email protected]55459852011-08-10 15:17:19302 return input_api.FilterSourceFile(
303 affected_file,
304 white_list=(file_inclusion_pattern, ),
305 black_list=black_list)
306
307 problems = []
308 for f in input_api.AffectedSourceFiles(FilterFile):
309 local_path = f.LocalPath()
[email protected]825d27182014-01-02 21:24:24310 for line_number, line in f.ChangedContents():
[email protected]2fdd1f362013-01-16 03:56:03311 if (inclusion_pattern.search(line) and
[email protected]de4f7d22013-05-23 14:27:46312 not comment_pattern.search(line) and
[email protected]2fdd1f362013-01-16 03:56:03313 not exclusion_pattern.search(line)):
[email protected]55459852011-08-10 15:17:19314 problems.append(
[email protected]2fdd1f362013-01-16 03:56:03315 '%s:%d\n %s' % (local_path, line_number, line.strip()))
[email protected]55459852011-08-10 15:17:19316
317 if problems:
[email protected]f7051d52013-04-02 18:31:42318 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
[email protected]2fdd1f362013-01-16 03:56:03319 else:
320 return []
[email protected]55459852011-08-10 15:17:19321
322
[email protected]10689ca2011-09-02 02:31:54323def _CheckNoIOStreamInHeaders(input_api, output_api):
324 """Checks to make sure no .h files include <iostream>."""
325 files = []
326 pattern = input_api.re.compile(r'^#include\s*<iostream>',
327 input_api.re.MULTILINE)
328 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
329 if not f.LocalPath().endswith('.h'):
330 continue
331 contents = input_api.ReadFile(f)
332 if pattern.search(contents):
333 files.append(f)
334
335 if len(files):
336 return [ output_api.PresubmitError(
[email protected]6c063c62012-07-11 19:11:06337 'Do not #include <iostream> in header files, since it inserts static '
338 'initialization into every file including the header. Instead, '
[email protected]10689ca2011-09-02 02:31:54339 '#include <ostream>. See http://crbug.com/94794',
340 files) ]
341 return []
342
343
[email protected]72df4e782012-06-21 16:28:18344def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
345 """Checks to make sure no source files use UNIT_TEST"""
346 problems = []
347 for f in input_api.AffectedFiles():
348 if (not f.LocalPath().endswith(('.cc', '.mm'))):
349 continue
350
351 for line_num, line in f.ChangedContents():
[email protected]549f86a2013-11-19 13:00:04352 if 'UNIT_TEST ' in line or line.endswith('UNIT_TEST'):
[email protected]72df4e782012-06-21 16:28:18353 problems.append(' %s:%d' % (f.LocalPath(), line_num))
354
355 if not problems:
356 return []
357 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
358 '\n'.join(problems))]
359
360
[email protected]8ea5d4b2011-09-13 21:49:22361def _CheckNoNewWStrings(input_api, output_api):
362 """Checks to make sure we don't introduce use of wstrings."""
[email protected]55463aa62011-10-12 00:48:27363 problems = []
[email protected]8ea5d4b2011-09-13 21:49:22364 for f in input_api.AffectedFiles():
[email protected]b5c24292011-11-28 14:38:20365 if (not f.LocalPath().endswith(('.cc', '.h')) or
[email protected]24be83c2013-08-29 23:01:23366 f.LocalPath().endswith(('test.cc', '_win.cc', '_win.h'))):
[email protected]b5c24292011-11-28 14:38:20367 continue
[email protected]8ea5d4b2011-09-13 21:49:22368
[email protected]a11dbe9b2012-08-07 01:32:58369 allowWString = False
[email protected]b5c24292011-11-28 14:38:20370 for line_num, line in f.ChangedContents():
[email protected]a11dbe9b2012-08-07 01:32:58371 if 'presubmit: allow wstring' in line:
372 allowWString = True
373 elif not allowWString and 'wstring' in line:
[email protected]55463aa62011-10-12 00:48:27374 problems.append(' %s:%d' % (f.LocalPath(), line_num))
[email protected]a11dbe9b2012-08-07 01:32:58375 allowWString = False
376 else:
377 allowWString = False
[email protected]8ea5d4b2011-09-13 21:49:22378
[email protected]55463aa62011-10-12 00:48:27379 if not problems:
380 return []
381 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
[email protected]a11dbe9b2012-08-07 01:32:58382 ' If you are calling a cross-platform API that accepts a wstring, '
383 'fix the API.\n' +
[email protected]55463aa62011-10-12 00:48:27384 '\n'.join(problems))]
[email protected]8ea5d4b2011-09-13 21:49:22385
386
[email protected]2a8ac9c2011-10-19 17:20:44387def _CheckNoDEPSGIT(input_api, output_api):
388 """Make sure .DEPS.git is never modified manually."""
389 if any(f.LocalPath().endswith('.DEPS.git') for f in
390 input_api.AffectedFiles()):
391 return [output_api.PresubmitError(
392 'Never commit changes to .DEPS.git. This file is maintained by an\n'
393 'automated system based on what\'s in DEPS and your changes will be\n'
394 'overwritten.\n'
[email protected]cb706912014-06-28 20:46:34395 'See https://sites.google.com/a/chromium.org/dev/developers/how-tos/get-the-code#Rolling_DEPS\n'
[email protected]2a8ac9c2011-10-19 17:20:44396 'for more information')]
397 return []
398
399
tandriief664692014-09-23 14:51:47400def _CheckValidHostsInDEPS(input_api, output_api):
401 """Checks that DEPS file deps are from allowed_hosts."""
402 # Run only if DEPS file has been modified to annoy fewer bystanders.
403 if all(f.LocalPath() != 'DEPS' for f in input_api.AffectedFiles()):
404 return []
405 # Outsource work to gclient verify
406 try:
407 input_api.subprocess.check_output(['gclient', 'verify'])
408 return []
409 except input_api.subprocess.CalledProcessError, error:
410 return [output_api.PresubmitError(
411 'DEPS file must have only git dependencies.',
412 long_text=error.output)]
413
414
[email protected]127f18ec2012-06-16 05:05:59415def _CheckNoBannedFunctions(input_api, output_api):
416 """Make sure that banned functions are not used."""
417 warnings = []
418 errors = []
419
420 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
421 for f in input_api.AffectedFiles(file_filter=file_filter):
422 for line_num, line in f.ChangedContents():
423 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
[email protected]eaae1972014-04-16 04:17:26424 matched = False
425 if func_name[0:1] == '/':
426 regex = func_name[1:]
427 if input_api.re.search(regex, line):
428 matched = True
429 elif func_name in line:
430 matched = True
431 if matched:
[email protected]127f18ec2012-06-16 05:05:59432 problems = warnings;
433 if error:
434 problems = errors;
435 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
436 for message_line in message:
437 problems.append(' %s' % message_line)
438
439 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
440 for f in input_api.AffectedFiles(file_filter=file_filter):
441 for line_num, line in f.ChangedContents():
[email protected]7345da02012-11-27 14:31:49442 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
443 def IsBlacklisted(affected_file, blacklist):
444 local_path = affected_file.LocalPath()
445 for item in blacklist:
446 if input_api.re.match(item, local_path):
447 return True
448 return False
449 if IsBlacklisted(f, excluded_paths):
450 continue
[email protected]d89eec82013-12-03 14:10:59451 matched = False
452 if func_name[0:1] == '/':
453 regex = func_name[1:]
454 if input_api.re.search(regex, line):
455 matched = True
456 elif func_name in line:
457 matched = True
458 if matched:
[email protected]127f18ec2012-06-16 05:05:59459 problems = warnings;
460 if error:
461 problems = errors;
462 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
463 for message_line in message:
464 problems.append(' %s' % message_line)
465
466 result = []
467 if (warnings):
468 result.append(output_api.PresubmitPromptWarning(
469 'Banned functions were used.\n' + '\n'.join(warnings)))
470 if (errors):
471 result.append(output_api.PresubmitError(
472 'Banned functions were used.\n' + '\n'.join(errors)))
473 return result
474
475
[email protected]6c063c62012-07-11 19:11:06476def _CheckNoPragmaOnce(input_api, output_api):
477 """Make sure that banned functions are not used."""
478 files = []
479 pattern = input_api.re.compile(r'^#pragma\s+once',
480 input_api.re.MULTILINE)
481 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
482 if not f.LocalPath().endswith('.h'):
483 continue
484 contents = input_api.ReadFile(f)
485 if pattern.search(contents):
486 files.append(f)
487
488 if files:
489 return [output_api.PresubmitError(
490 'Do not use #pragma once in header files.\n'
491 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
492 files)]
493 return []
494
[email protected]127f18ec2012-06-16 05:05:59495
[email protected]e7479052012-09-19 00:26:12496def _CheckNoTrinaryTrueFalse(input_api, output_api):
497 """Checks to make sure we don't introduce use of foo ? true : false."""
498 problems = []
499 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
500 for f in input_api.AffectedFiles():
501 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
502 continue
503
504 for line_num, line in f.ChangedContents():
505 if pattern.match(line):
506 problems.append(' %s:%d' % (f.LocalPath(), line_num))
507
508 if not problems:
509 return []
510 return [output_api.PresubmitPromptWarning(
511 'Please consider avoiding the "? true : false" pattern if possible.\n' +
512 '\n'.join(problems))]
513
514
[email protected]55f9f382012-07-31 11:02:18515def _CheckUnwantedDependencies(input_api, output_api):
516 """Runs checkdeps on #include statements added in this
517 change. Breaking - rules is an error, breaking ! rules is a
518 warning.
519 """
520 # We need to wait until we have an input_api object and use this
521 # roundabout construct to import checkdeps because this file is
522 # eval-ed and thus doesn't have __file__.
523 original_sys_path = sys.path
524 try:
525 sys.path = sys.path + [input_api.os_path.join(
[email protected]5298cc982014-05-29 20:53:47526 input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
[email protected]55f9f382012-07-31 11:02:18527 import checkdeps
528 from cpp_checker import CppChecker
529 from rules import Rule
530 finally:
531 # Restore sys.path to what it was before.
532 sys.path = original_sys_path
533
534 added_includes = []
535 for f in input_api.AffectedFiles():
536 if not CppChecker.IsCppFile(f.LocalPath()):
537 continue
538
539 changed_lines = [line for line_num, line in f.ChangedContents()]
540 added_includes.append([f.LocalPath(), changed_lines])
541
[email protected]26385172013-05-09 23:11:35542 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
[email protected]55f9f382012-07-31 11:02:18543
544 error_descriptions = []
545 warning_descriptions = []
546 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
547 added_includes):
548 description_with_path = '%s\n %s' % (path, rule_description)
549 if rule_type == Rule.DISALLOW:
550 error_descriptions.append(description_with_path)
551 else:
552 warning_descriptions.append(description_with_path)
553
554 results = []
555 if error_descriptions:
556 results.append(output_api.PresubmitError(
557 'You added one or more #includes that violate checkdeps rules.',
558 error_descriptions))
559 if warning_descriptions:
[email protected]f7051d52013-04-02 18:31:42560 results.append(output_api.PresubmitPromptOrNotify(
[email protected]55f9f382012-07-31 11:02:18561 'You added one or more #includes of files that are temporarily\n'
562 'allowed but being removed. Can you avoid introducing the\n'
563 '#include? See relevant DEPS file(s) for details and contacts.',
564 warning_descriptions))
565 return results
566
567
[email protected]fbcafe5a2012-08-08 15:31:22568def _CheckFilePermissions(input_api, output_api):
569 """Check that all files have their permissions properly set."""
[email protected]791507202014-02-03 23:19:15570 if input_api.platform == 'win32':
571 return []
[email protected]fbcafe5a2012-08-08 15:31:22572 args = [sys.executable, 'tools/checkperms/checkperms.py', '--root',
573 input_api.change.RepositoryRoot()]
574 for f in input_api.AffectedFiles():
575 args += ['--file', f.LocalPath()]
[email protected]f0d330f2014-01-30 01:44:34576 checkperms = input_api.subprocess.Popen(args,
577 stdout=input_api.subprocess.PIPE)
578 errors = checkperms.communicate()[0].strip()
[email protected]fbcafe5a2012-08-08 15:31:22579 if errors:
[email protected]f0d330f2014-01-30 01:44:34580 return [output_api.PresubmitError('checkperms.py failed.',
581 errors.splitlines())]
582 return []
[email protected]fbcafe5a2012-08-08 15:31:22583
584
[email protected]c8278b32012-10-30 20:35:49585def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
586 """Makes sure we don't include ui/aura/window_property.h
587 in header files.
588 """
589 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
590 errors = []
591 for f in input_api.AffectedFiles():
592 if not f.LocalPath().endswith('.h'):
593 continue
594 for line_num, line in f.ChangedContents():
595 if pattern.match(line):
596 errors.append(' %s:%d' % (f.LocalPath(), line_num))
597
598 results = []
599 if errors:
600 results.append(output_api.PresubmitError(
601 'Header files should not include ui/aura/window_property.h', errors))
602 return results
603
604
[email protected]cf9b78f2012-11-14 11:40:28605def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
606 """Checks that the lines in scope occur in the right order.
607
608 1. C system files in alphabetical order
609 2. C++ system files in alphabetical order
610 3. Project's .h files
611 """
612
613 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
614 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
615 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
616
617 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
618
619 state = C_SYSTEM_INCLUDES
620
621 previous_line = ''
[email protected]728b9bb2012-11-14 20:38:57622 previous_line_num = 0
[email protected]cf9b78f2012-11-14 11:40:28623 problem_linenums = []
624 for line_num, line in scope:
625 if c_system_include_pattern.match(line):
626 if state != C_SYSTEM_INCLUDES:
[email protected]728b9bb2012-11-14 20:38:57627 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28628 elif previous_line and previous_line > line:
[email protected]728b9bb2012-11-14 20:38:57629 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28630 elif cpp_system_include_pattern.match(line):
631 if state == C_SYSTEM_INCLUDES:
632 state = CPP_SYSTEM_INCLUDES
633 elif state == CUSTOM_INCLUDES:
[email protected]728b9bb2012-11-14 20:38:57634 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28635 elif previous_line and previous_line > line:
[email protected]728b9bb2012-11-14 20:38:57636 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28637 elif custom_include_pattern.match(line):
638 if state != CUSTOM_INCLUDES:
639 state = CUSTOM_INCLUDES
640 elif previous_line and previous_line > line:
[email protected]728b9bb2012-11-14 20:38:57641 problem_linenums.append((line_num, previous_line_num))
[email protected]cf9b78f2012-11-14 11:40:28642 else:
643 problem_linenums.append(line_num)
644 previous_line = line
[email protected]728b9bb2012-11-14 20:38:57645 previous_line_num = line_num
[email protected]cf9b78f2012-11-14 11:40:28646
647 warnings = []
[email protected]728b9bb2012-11-14 20:38:57648 for (line_num, previous_line_num) in problem_linenums:
649 if line_num in changed_linenums or previous_line_num in changed_linenums:
[email protected]cf9b78f2012-11-14 11:40:28650 warnings.append(' %s:%d' % (file_path, line_num))
651 return warnings
652
653
[email protected]ac294a12012-12-06 16:38:43654def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
[email protected]cf9b78f2012-11-14 11:40:28655 """Checks the #include order for the given file f."""
656
[email protected]2299dcf2012-11-15 19:56:24657 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
[email protected]23093b62013-09-20 12:16:30658 # Exclude the following includes from the check:
659 # 1) #include <.../...>, e.g., <sys/...> includes often need to appear in a
660 # specific order.
661 # 2) <atlbase.h>, "build/build_config.h"
662 excluded_include_pattern = input_api.re.compile(
663 r'\s*#include (\<.*/.*|\<atlbase\.h\>|"build/build_config.h")')
[email protected]2299dcf2012-11-15 19:56:24664 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
[email protected]3e83618c2013-10-09 22:32:33665 # Match the final or penultimate token if it is xxxtest so we can ignore it
666 # when considering the special first include.
667 test_file_tag_pattern = input_api.re.compile(
668 r'_[a-z]+test(?=(_[a-zA-Z0-9]+)?\.)')
[email protected]0e5c1852012-12-18 20:17:11669 if_pattern = input_api.re.compile(
670 r'\s*#\s*(if|elif|else|endif|define|undef).*')
671 # Some files need specialized order of includes; exclude such files from this
672 # check.
673 uncheckable_includes_pattern = input_api.re.compile(
674 r'\s*#include '
675 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
[email protected]cf9b78f2012-11-14 11:40:28676
677 contents = f.NewContents()
678 warnings = []
679 line_num = 0
680
[email protected]ac294a12012-12-06 16:38:43681 # Handle the special first include. If the first include file is
682 # some/path/file.h, the corresponding including file can be some/path/file.cc,
683 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
684 # etc. It's also possible that no special first include exists.
[email protected]3e83618c2013-10-09 22:32:33685 # If the included file is some/path/file_platform.h the including file could
686 # also be some/path/file_xxxtest_platform.h.
687 including_file_base_name = test_file_tag_pattern.sub(
688 '', input_api.os_path.basename(f.LocalPath()))
689
[email protected]ac294a12012-12-06 16:38:43690 for line in contents:
691 line_num += 1
692 if system_include_pattern.match(line):
693 # No special first include -> process the line again along with normal
694 # includes.
695 line_num -= 1
696 break
697 match = custom_include_pattern.match(line)
698 if match:
699 match_dict = match.groupdict()
[email protected]3e83618c2013-10-09 22:32:33700 header_basename = test_file_tag_pattern.sub(
701 '', input_api.os_path.basename(match_dict['FILE'])).replace('.h', '')
702
703 if header_basename not in including_file_base_name:
[email protected]2299dcf2012-11-15 19:56:24704 # No special first include -> process the line again along with normal
705 # includes.
706 line_num -= 1
[email protected]ac294a12012-12-06 16:38:43707 break
[email protected]cf9b78f2012-11-14 11:40:28708
709 # Split into scopes: Each region between #if and #endif is its own scope.
710 scopes = []
711 current_scope = []
712 for line in contents[line_num:]:
713 line_num += 1
[email protected]0e5c1852012-12-18 20:17:11714 if uncheckable_includes_pattern.match(line):
[email protected]4436c9e2014-01-07 23:19:54715 continue
[email protected]2309b0fa02012-11-16 12:18:27716 if if_pattern.match(line):
[email protected]cf9b78f2012-11-14 11:40:28717 scopes.append(current_scope)
718 current_scope = []
[email protected]962f117e2012-11-22 18:11:56719 elif ((system_include_pattern.match(line) or
720 custom_include_pattern.match(line)) and
721 not excluded_include_pattern.match(line)):
[email protected]cf9b78f2012-11-14 11:40:28722 current_scope.append((line_num, line))
723 scopes.append(current_scope)
724
725 for scope in scopes:
726 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
727 changed_linenums))
728 return warnings
729
730
731def _CheckIncludeOrder(input_api, output_api):
732 """Checks that the #include order is correct.
733
734 1. The corresponding header for source files.
735 2. C system files in alphabetical order
736 3. C++ system files in alphabetical order
737 4. Project's .h files in alphabetical order
738
[email protected]ac294a12012-12-06 16:38:43739 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
740 these rules separately.
[email protected]cf9b78f2012-11-14 11:40:28741 """
[email protected]e120b012014-08-15 19:08:35742 def FileFilterIncludeOrder(affected_file):
743 black_list = (_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST)
744 return input_api.FilterSourceFile(affected_file, black_list=black_list)
[email protected]cf9b78f2012-11-14 11:40:28745
746 warnings = []
[email protected]e120b012014-08-15 19:08:35747 for f in input_api.AffectedFiles(file_filter=FileFilterIncludeOrder):
[email protected]ac294a12012-12-06 16:38:43748 if f.LocalPath().endswith(('.cc', '.h')):
749 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
750 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
[email protected]cf9b78f2012-11-14 11:40:28751
752 results = []
753 if warnings:
[email protected]f7051d52013-04-02 18:31:42754 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
[email protected]120cf540d2012-12-10 17:55:53755 warnings))
[email protected]cf9b78f2012-11-14 11:40:28756 return results
757
758
[email protected]70ca77752012-11-20 03:45:03759def _CheckForVersionControlConflictsInFile(input_api, f):
760 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
761 errors = []
762 for line_num, line in f.ChangedContents():
763 if pattern.match(line):
764 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
765 return errors
766
767
768def _CheckForVersionControlConflicts(input_api, output_api):
769 """Usually this is not intentional and will cause a compile failure."""
770 errors = []
771 for f in input_api.AffectedFiles():
772 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
773
774 results = []
775 if errors:
776 results.append(output_api.PresubmitError(
777 'Version control conflict markers found, please resolve.', errors))
778 return results
779
780
[email protected]06e6d0ff2012-12-11 01:36:44781def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
782 def FilterFile(affected_file):
783 """Filter function for use with input_api.AffectedSourceFiles,
784 below. This filters out everything except non-test files from
785 top-level directories that generally speaking should not hard-code
786 service URLs (e.g. src/android_webview/, src/content/ and others).
787 """
788 return input_api.FilterSourceFile(
789 affected_file,
[email protected]78bb39d62012-12-11 15:11:56790 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
[email protected]06e6d0ff2012-12-11 01:36:44791 black_list=(_EXCLUDED_PATHS +
792 _TEST_CODE_EXCLUDED_PATHS +
793 input_api.DEFAULT_BLACK_LIST))
794
[email protected]de4f7d22013-05-23 14:27:46795 base_pattern = '"[^"]*google\.com[^"]*"'
796 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
797 pattern = input_api.re.compile(base_pattern)
[email protected]06e6d0ff2012-12-11 01:36:44798 problems = [] # items are (filename, line_number, line)
799 for f in input_api.AffectedSourceFiles(FilterFile):
800 for line_num, line in f.ChangedContents():
[email protected]de4f7d22013-05-23 14:27:46801 if not comment_pattern.search(line) and pattern.search(line):
[email protected]06e6d0ff2012-12-11 01:36:44802 problems.append((f.LocalPath(), line_num, line))
803
804 if problems:
[email protected]f7051d52013-04-02 18:31:42805 return [output_api.PresubmitPromptOrNotify(
[email protected]06e6d0ff2012-12-11 01:36:44806 'Most layers below src/chrome/ should not hardcode service URLs.\n'
[email protected]b0149772014-03-27 16:47:58807 'Are you sure this is correct?',
[email protected]06e6d0ff2012-12-11 01:36:44808 [' %s:%d: %s' % (
809 problem[0], problem[1], problem[2]) for problem in problems])]
[email protected]2fdd1f362013-01-16 03:56:03810 else:
811 return []
[email protected]06e6d0ff2012-12-11 01:36:44812
813
[email protected]d2530012013-01-25 16:39:27814def _CheckNoAbbreviationInPngFileName(input_api, output_api):
815 """Makes sure there are no abbreviations in the name of PNG files.
816 """
[email protected]4053a48e2013-01-25 21:43:04817 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
[email protected]d2530012013-01-25 16:39:27818 errors = []
819 for f in input_api.AffectedFiles(include_deletes=False):
820 if pattern.match(f.LocalPath()):
821 errors.append(' %s' % f.LocalPath())
822
823 results = []
824 if errors:
825 results.append(output_api.PresubmitError(
826 'The name of PNG files should not have abbreviations. \n'
827 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
828 'Contact [email protected] if you have questions.', errors))
829 return results
830
831
[email protected]14a6131c2014-01-08 01:15:41832def _FilesToCheckForIncomingDeps(re, changed_lines):
[email protected]f32e2d1e2013-07-26 21:39:08833 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
[email protected]14a6131c2014-01-08 01:15:41834 a set of DEPS entries that we should look up.
835
836 For a directory (rather than a specific filename) we fake a path to
837 a specific filename by adding /DEPS. This is chosen as a file that
838 will seldom or never be subject to per-file include_rules.
839 """
[email protected]2b438d62013-11-14 17:54:14840 # We ignore deps entries on auto-generated directories.
841 AUTO_GENERATED_DIRS = ['grit', 'jni']
[email protected]f32e2d1e2013-07-26 21:39:08842
843 # This pattern grabs the path without basename in the first
844 # parentheses, and the basename (if present) in the second. It
845 # relies on the simple heuristic that if there is a basename it will
846 # be a header file ending in ".h".
847 pattern = re.compile(
848 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
[email protected]2b438d62013-11-14 17:54:14849 results = set()
[email protected]f32e2d1e2013-07-26 21:39:08850 for changed_line in changed_lines:
851 m = pattern.match(changed_line)
852 if m:
853 path = m.group(1)
[email protected]2b438d62013-11-14 17:54:14854 if path.split('/')[0] not in AUTO_GENERATED_DIRS:
[email protected]14a6131c2014-01-08 01:15:41855 if m.group(2):
856 results.add('%s%s' % (path, m.group(2)))
857 else:
858 results.add('%s/DEPS' % path)
[email protected]f32e2d1e2013-07-26 21:39:08859 return results
860
861
[email protected]e871964c2013-05-13 14:14:55862def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
863 """When a dependency prefixed with + is added to a DEPS file, we
864 want to make sure that the change is reviewed by an OWNER of the
865 target file or directory, to avoid layering violations from being
866 introduced. This check verifies that this happens.
867 """
868 changed_lines = set()
869 for f in input_api.AffectedFiles():
870 filename = input_api.os_path.basename(f.LocalPath())
871 if filename == 'DEPS':
872 changed_lines |= set(line.strip()
873 for line_num, line
874 in f.ChangedContents())
875 if not changed_lines:
876 return []
877
[email protected]14a6131c2014-01-08 01:15:41878 virtual_depended_on_files = _FilesToCheckForIncomingDeps(input_api.re,
879 changed_lines)
[email protected]e871964c2013-05-13 14:14:55880 if not virtual_depended_on_files:
881 return []
882
883 if input_api.is_committing:
884 if input_api.tbr:
885 return [output_api.PresubmitNotifyResult(
886 '--tbr was specified, skipping OWNERS check for DEPS additions')]
887 if not input_api.change.issue:
888 return [output_api.PresubmitError(
889 "DEPS approval by OWNERS check failed: this change has "
890 "no Rietveld issue number, so we can't check it for approvals.")]
891 output = output_api.PresubmitError
892 else:
893 output = output_api.PresubmitNotifyResult
894
895 owners_db = input_api.owners_db
896 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
897 input_api,
898 owners_db.email_regexp,
899 approval_needed=input_api.is_committing)
900
901 owner_email = owner_email or input_api.change.author_email
902
[email protected]de4f7d22013-05-23 14:27:46903 reviewers_plus_owner = set(reviewers)
[email protected]e71c6082013-05-22 02:28:51904 if owner_email:
[email protected]de4f7d22013-05-23 14:27:46905 reviewers_plus_owner.add(owner_email)
[email protected]e871964c2013-05-13 14:14:55906 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
907 reviewers_plus_owner)
[email protected]14a6131c2014-01-08 01:15:41908
909 # We strip the /DEPS part that was added by
910 # _FilesToCheckForIncomingDeps to fake a path to a file in a
911 # directory.
912 def StripDeps(path):
913 start_deps = path.rfind('/DEPS')
914 if start_deps != -1:
915 return path[:start_deps]
916 else:
917 return path
918 unapproved_dependencies = ["'+%s'," % StripDeps(path)
[email protected]e871964c2013-05-13 14:14:55919 for path in missing_files]
920
921 if unapproved_dependencies:
922 output_list = [
[email protected]14a6131c2014-01-08 01:15:41923 output('Missing LGTM from OWNERS of dependencies added to DEPS:\n %s' %
[email protected]e871964c2013-05-13 14:14:55924 '\n '.join(sorted(unapproved_dependencies)))]
925 if not input_api.is_committing:
926 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
927 output_list.append(output(
928 'Suggested missing target path OWNERS:\n %s' %
929 '\n '.join(suggested_owners or [])))
930 return output_list
931
932 return []
933
934
[email protected]85218562013-11-22 07:41:40935def _CheckSpamLogging(input_api, output_api):
936 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
937 black_list = (_EXCLUDED_PATHS +
938 _TEST_CODE_EXCLUDED_PATHS +
939 input_api.DEFAULT_BLACK_LIST +
[email protected]6f742dd02013-11-26 23:19:50940 (r"^base[\\\/]logging\.h$",
[email protected]80f360a2014-01-23 01:36:19941 r"^base[\\\/]logging\.cc$",
[email protected]8dc338c2013-12-09 16:28:48942 r"^chrome[\\\/]app[\\\/]chrome_main_delegate\.cc$",
[email protected]6e268db2013-12-04 01:41:46943 r"^chrome[\\\/]browser[\\\/]chrome_browser_main\.cc$",
[email protected]4de75262013-12-18 23:16:12944 r"^chrome[\\\/]browser[\\\/]ui[\\\/]startup[\\\/]"
945 r"startup_browser_creator\.cc$",
[email protected]fe0e6e12013-12-04 05:52:58946 r"^chrome[\\\/]installer[\\\/]setup[\\\/].*",
[email protected]8cf6f842014-08-08 21:33:16947 r"chrome[\\\/]browser[\\\/]diagnostics[\\\/]" +
[email protected]f5b9a3f342014-08-08 22:06:03948 r"diagnostics_writer\.cc$",
[email protected]9f13b602014-08-07 02:59:15949 r"^chrome_elf[\\\/]dll_hash[\\\/]dll_hash_main\.cc$",
950 r"^chromecast[\\\/]",
951 r"^cloud_print[\\\/]",
[email protected]9056e732014-01-08 06:25:25952 r"^content[\\\/]common[\\\/]gpu[\\\/]client[\\\/]"
953 r"gl_helper_benchmark\.cc$",
thestigc9e38a22014-09-13 01:02:11954 r"^courgette[\\\/]courgette_tool\.cc$",
[email protected]9f13b602014-08-07 02:59:15955 r"^extensions[\\\/]renderer[\\\/]logging_native_handler\.cc$",
[email protected]9c36d922014-03-24 16:47:52956 r"^native_client_sdk[\\\/]",
[email protected]cdbdced2013-11-27 21:35:50957 r"^remoting[\\\/]base[\\\/]logging\.h$",
[email protected]67c96ab2013-12-17 02:05:36958 r"^remoting[\\\/]host[\\\/].*",
[email protected]8232f8fd2013-12-14 00:52:31959 r"^sandbox[\\\/]linux[\\\/].*",
[email protected]0b7a21e2014-02-11 18:38:13960 r"^tools[\\\/]",
thestig22dfc4012014-09-05 08:29:44961 r"^ui[\\\/]aura[\\\/]bench[\\\/]bench_main\.cc$",
962 r"^webkit[\\\/]browser[\\\/]fileapi[\\\/]" +
963 r"dump_file_system.cc$",))
[email protected]85218562013-11-22 07:41:40964 source_file_filter = lambda x: input_api.FilterSourceFile(
965 x, white_list=(file_inclusion_pattern,), black_list=black_list)
966
967 log_info = []
968 printf = []
969
970 for f in input_api.AffectedSourceFiles(source_file_filter):
971 contents = input_api.ReadFile(f, 'rb')
972 if re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", contents):
973 log_info.append(f.LocalPath())
[email protected]18b466b2013-12-02 22:01:37974 elif re.search(r"\bD?LOG_IF\s*\(\s*INFO\s*,", contents):
[email protected]85210652013-11-28 05:50:13975 log_info.append(f.LocalPath())
[email protected]18b466b2013-12-02 22:01:37976
977 if re.search(r"\bprintf\(", contents):
978 printf.append(f.LocalPath())
979 elif re.search(r"\bfprintf\((stdout|stderr)", contents):
[email protected]85218562013-11-22 07:41:40980 printf.append(f.LocalPath())
981
982 if log_info:
983 return [output_api.PresubmitError(
984 'These files spam the console log with LOG(INFO):',
985 items=log_info)]
986 if printf:
987 return [output_api.PresubmitError(
988 'These files spam the console log with printf/fprintf:',
989 items=printf)]
990 return []
991
992
[email protected]49aa76a2013-12-04 06:59:16993def _CheckForAnonymousVariables(input_api, output_api):
994 """These types are all expected to hold locks while in scope and
995 so should never be anonymous (which causes them to be immediately
996 destroyed)."""
997 they_who_must_be_named = [
998 'base::AutoLock',
999 'base::AutoReset',
1000 'base::AutoUnlock',
1001 'SkAutoAlphaRestore',
1002 'SkAutoBitmapShaderInstall',
1003 'SkAutoBlitterChoose',
1004 'SkAutoBounderCommit',
1005 'SkAutoCallProc',
1006 'SkAutoCanvasRestore',
1007 'SkAutoCommentBlock',
1008 'SkAutoDescriptor',
1009 'SkAutoDisableDirectionCheck',
1010 'SkAutoDisableOvalCheck',
1011 'SkAutoFree',
1012 'SkAutoGlyphCache',
1013 'SkAutoHDC',
1014 'SkAutoLockColors',
1015 'SkAutoLockPixels',
1016 'SkAutoMalloc',
1017 'SkAutoMaskFreeImage',
1018 'SkAutoMutexAcquire',
1019 'SkAutoPathBoundsUpdate',
1020 'SkAutoPDFRelease',
1021 'SkAutoRasterClipValidate',
1022 'SkAutoRef',
1023 'SkAutoTime',
1024 'SkAutoTrace',
1025 'SkAutoUnref',
1026 ]
1027 anonymous = r'(%s)\s*[({]' % '|'.join(they_who_must_be_named)
1028 # bad: base::AutoLock(lock.get());
1029 # not bad: base::AutoLock lock(lock.get());
1030 bad_pattern = input_api.re.compile(anonymous)
1031 # good: new base::AutoLock(lock.get())
1032 good_pattern = input_api.re.compile(r'\bnew\s*' + anonymous)
1033 errors = []
1034
1035 for f in input_api.AffectedFiles():
1036 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
1037 continue
1038 for linenum, line in f.ChangedContents():
1039 if bad_pattern.search(line) and not good_pattern.search(line):
1040 errors.append('%s:%d' % (f.LocalPath(), linenum))
1041
1042 if errors:
1043 return [output_api.PresubmitError(
1044 'These lines create anonymous variables that need to be named:',
1045 items=errors)]
1046 return []
1047
1048
[email protected]5fe0f8742013-11-29 01:04:591049def _CheckCygwinShell(input_api, output_api):
1050 source_file_filter = lambda x: input_api.FilterSourceFile(
1051 x, white_list=(r'.+\.(gyp|gypi)$',))
1052 cygwin_shell = []
1053
1054 for f in input_api.AffectedSourceFiles(source_file_filter):
1055 for linenum, line in f.ChangedContents():
1056 if 'msvs_cygwin_shell' in line:
1057 cygwin_shell.append(f.LocalPath())
1058 break
1059
1060 if cygwin_shell:
1061 return [output_api.PresubmitError(
1062 'These files should not use msvs_cygwin_shell (the default is 0):',
1063 items=cygwin_shell)]
1064 return []
1065
[email protected]85218562013-11-22 07:41:401066
[email protected]999261d2014-03-03 20:08:081067def _CheckUserActionUpdate(input_api, output_api):
1068 """Checks if any new user action has been added."""
[email protected]2f92dec2014-03-07 19:21:521069 if any('actions.xml' == input_api.os_path.basename(f) for f in
[email protected]999261d2014-03-03 20:08:081070 input_api.LocalPaths()):
[email protected]2f92dec2014-03-07 19:21:521071 # If actions.xml is already included in the changelist, the PRESUBMIT
1072 # for actions.xml will do a more complete presubmit check.
[email protected]999261d2014-03-03 20:08:081073 return []
1074
[email protected]999261d2014-03-03 20:08:081075 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm'))
1076 action_re = r'[^a-zA-Z]UserMetricsAction\("([^"]*)'
[email protected]2f92dec2014-03-07 19:21:521077 current_actions = None
[email protected]999261d2014-03-03 20:08:081078 for f in input_api.AffectedFiles(file_filter=file_filter):
1079 for line_num, line in f.ChangedContents():
1080 match = input_api.re.search(action_re, line)
1081 if match:
[email protected]2f92dec2014-03-07 19:21:521082 # Loads contents in tools/metrics/actions/actions.xml to memory. It's
1083 # loaded only once.
1084 if not current_actions:
1085 with open('tools/metrics/actions/actions.xml') as actions_f:
1086 current_actions = actions_f.read()
1087 # Search for the matched user action name in |current_actions|.
[email protected]999261d2014-03-03 20:08:081088 for action_name in match.groups():
[email protected]2f92dec2014-03-07 19:21:521089 action = 'name="{0}"'.format(action_name)
1090 if action not in current_actions:
[email protected]999261d2014-03-03 20:08:081091 return [output_api.PresubmitPromptWarning(
1092 'File %s line %d: %s is missing in '
[email protected]2f92dec2014-03-07 19:21:521093 'tools/metrics/actions/actions.xml. Please run '
1094 'tools/metrics/actions/extract_actions.py to update.'
[email protected]999261d2014-03-03 20:08:081095 % (f.LocalPath(), line_num, action_name))]
1096 return []
1097
1098
[email protected]99171a92014-06-03 08:44:471099def _GetJSONParseError(input_api, filename, eat_comments=True):
1100 try:
1101 contents = input_api.ReadFile(filename)
1102 if eat_comments:
1103 json_comment_eater = input_api.os_path.join(
1104 input_api.PresubmitLocalPath(),
1105 'tools', 'json_comment_eater', 'json_comment_eater.py')
1106 process = input_api.subprocess.Popen(
1107 [input_api.python_executable, json_comment_eater],
1108 stdin=input_api.subprocess.PIPE,
1109 stdout=input_api.subprocess.PIPE,
1110 universal_newlines=True)
1111 (contents, _) = process.communicate(input=contents)
1112
1113 input_api.json.loads(contents)
1114 except ValueError as e:
1115 return e
1116 return None
1117
1118
1119def _GetIDLParseError(input_api, filename):
1120 try:
1121 contents = input_api.ReadFile(filename)
1122 idl_schema = input_api.os_path.join(
1123 input_api.PresubmitLocalPath(),
1124 'tools', 'json_schema_compiler', 'idl_schema.py')
1125 process = input_api.subprocess.Popen(
1126 [input_api.python_executable, idl_schema],
1127 stdin=input_api.subprocess.PIPE,
1128 stdout=input_api.subprocess.PIPE,
1129 stderr=input_api.subprocess.PIPE,
1130 universal_newlines=True)
1131 (_, error) = process.communicate(input=contents)
1132 return error or None
1133 except ValueError as e:
1134 return e
1135
1136
1137def _CheckParseErrors(input_api, output_api):
1138 """Check that IDL and JSON files do not contain syntax errors."""
1139 actions = {
1140 '.idl': _GetIDLParseError,
1141 '.json': _GetJSONParseError,
1142 }
1143 # These paths contain test data and other known invalid JSON files.
1144 excluded_patterns = [
joaodasilva718f87672014-08-30 09:25:491145 r'test[\\\/]data[\\\/]',
1146 r'^components[\\\/]policy[\\\/]resources[\\\/]policy_templates\.json$',
[email protected]99171a92014-06-03 08:44:471147 ]
1148 # Most JSON files are preprocessed and support comments, but these do not.
1149 json_no_comments_patterns = [
joaodasilva718f87672014-08-30 09:25:491150 r'^testing[\\\/]',
[email protected]99171a92014-06-03 08:44:471151 ]
1152 # Only run IDL checker on files in these directories.
1153 idl_included_patterns = [
joaodasilva718f87672014-08-30 09:25:491154 r'^chrome[\\\/]common[\\\/]extensions[\\\/]api[\\\/]',
1155 r'^extensions[\\\/]common[\\\/]api[\\\/]',
[email protected]99171a92014-06-03 08:44:471156 ]
1157
1158 def get_action(affected_file):
1159 filename = affected_file.LocalPath()
1160 return actions.get(input_api.os_path.splitext(filename)[1])
1161
1162 def MatchesFile(patterns, path):
1163 for pattern in patterns:
1164 if input_api.re.search(pattern, path):
1165 return True
1166 return False
1167
1168 def FilterFile(affected_file):
1169 action = get_action(affected_file)
1170 if not action:
1171 return False
1172 path = affected_file.LocalPath()
1173
1174 if MatchesFile(excluded_patterns, path):
1175 return False
1176
1177 if (action == _GetIDLParseError and
1178 not MatchesFile(idl_included_patterns, path)):
1179 return False
1180 return True
1181
1182 results = []
1183 for affected_file in input_api.AffectedFiles(
1184 file_filter=FilterFile, include_deletes=False):
1185 action = get_action(affected_file)
1186 kwargs = {}
1187 if (action == _GetJSONParseError and
1188 MatchesFile(json_no_comments_patterns, affected_file.LocalPath())):
1189 kwargs['eat_comments'] = False
1190 parse_error = action(input_api,
1191 affected_file.AbsoluteLocalPath(),
1192 **kwargs)
1193 if parse_error:
1194 results.append(output_api.PresubmitError('%s could not be parsed: %s' %
1195 (affected_file.LocalPath(), parse_error)))
1196 return results
1197
1198
[email protected]760deea2013-12-10 19:33:491199def _CheckJavaStyle(input_api, output_api):
1200 """Runs checkstyle on changed java files and returns errors if any exist."""
1201 original_sys_path = sys.path
1202 try:
1203 sys.path = sys.path + [input_api.os_path.join(
1204 input_api.PresubmitLocalPath(), 'tools', 'android', 'checkstyle')]
1205 import checkstyle
1206 finally:
1207 # Restore sys.path to what it was before.
1208 sys.path = original_sys_path
1209
1210 return checkstyle.RunCheckstyle(
1211 input_api, output_api, 'tools/android/checkstyle/chromium-style-5.0.xml')
1212
1213
[email protected]fd20b902014-05-09 02:14:531214_DEPRECATED_CSS = [
1215 # Values
1216 ( "-webkit-box", "flex" ),
1217 ( "-webkit-inline-box", "inline-flex" ),
1218 ( "-webkit-flex", "flex" ),
1219 ( "-webkit-inline-flex", "inline-flex" ),
1220 ( "-webkit-min-content", "min-content" ),
1221 ( "-webkit-max-content", "max-content" ),
1222
1223 # Properties
1224 ( "-webkit-background-clip", "background-clip" ),
1225 ( "-webkit-background-origin", "background-origin" ),
1226 ( "-webkit-background-size", "background-size" ),
1227 ( "-webkit-box-shadow", "box-shadow" ),
1228
1229 # Functions
1230 ( "-webkit-gradient", "gradient" ),
1231 ( "-webkit-repeating-gradient", "repeating-gradient" ),
1232 ( "-webkit-linear-gradient", "linear-gradient" ),
1233 ( "-webkit-repeating-linear-gradient", "repeating-linear-gradient" ),
1234 ( "-webkit-radial-gradient", "radial-gradient" ),
1235 ( "-webkit-repeating-radial-gradient", "repeating-radial-gradient" ),
1236]
1237
1238def _CheckNoDeprecatedCSS(input_api, output_api):
1239 """ Make sure that we don't use deprecated CSS
[email protected]9a48e3f82014-05-22 00:06:251240 properties, functions or values. Our external
1241 documentation is ignored by the hooks as it
1242 needs to be consumed by WebKit. """
[email protected]fd20b902014-05-09 02:14:531243 results = []
[email protected]9a48e3f82014-05-22 00:06:251244 file_inclusion_pattern = (r".+\.css$")
1245 black_list = (_EXCLUDED_PATHS +
1246 _TEST_CODE_EXCLUDED_PATHS +
1247 input_api.DEFAULT_BLACK_LIST +
1248 (r"^chrome/common/extensions/docs",
1249 r"^chrome/docs",
1250 r"^native_client_sdk"))
1251 file_filter = lambda f: input_api.FilterSourceFile(
1252 f, white_list=file_inclusion_pattern, black_list=black_list)
[email protected]fd20b902014-05-09 02:14:531253 for fpath in input_api.AffectedFiles(file_filter=file_filter):
1254 for line_num, line in fpath.ChangedContents():
1255 for (deprecated_value, value) in _DEPRECATED_CSS:
1256 if input_api.re.search(deprecated_value, line):
1257 results.append(output_api.PresubmitError(
1258 "%s:%d: Use of deprecated CSS %s, use %s instead" %
1259 (fpath.LocalPath(), line_num, deprecated_value, value)))
1260 return results
1261
[email protected]22c9bd72011-03-27 16:47:391262def _CommonChecks(input_api, output_api):
1263 """Checks common to both upload and commit."""
1264 results = []
1265 results.extend(input_api.canned_checks.PanProjectChecks(
[email protected]3de922f2013-12-20 13:27:381266 input_api, output_api,
1267 excluded_paths=_EXCLUDED_PATHS + _TESTRUNNER_PATHS))
[email protected]66daa702011-05-28 14:41:461268 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
[email protected]55459852011-08-10 15:17:191269 results.extend(
[email protected]760deea2013-12-10 19:33:491270 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
[email protected]10689ca2011-09-02 02:31:541271 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
[email protected]72df4e782012-06-21 16:28:181272 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
[email protected]8ea5d4b2011-09-13 21:49:221273 results.extend(_CheckNoNewWStrings(input_api, output_api))
[email protected]2a8ac9c2011-10-19 17:20:441274 results.extend(_CheckNoDEPSGIT(input_api, output_api))
[email protected]127f18ec2012-06-16 05:05:591275 results.extend(_CheckNoBannedFunctions(input_api, output_api))
[email protected]6c063c62012-07-11 19:11:061276 results.extend(_CheckNoPragmaOnce(input_api, output_api))
[email protected]e7479052012-09-19 00:26:121277 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
[email protected]55f9f382012-07-31 11:02:181278 results.extend(_CheckUnwantedDependencies(input_api, output_api))
[email protected]fbcafe5a2012-08-08 15:31:221279 results.extend(_CheckFilePermissions(input_api, output_api))
[email protected]c8278b32012-10-30 20:35:491280 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
[email protected]2309b0fa02012-11-16 12:18:271281 results.extend(_CheckIncludeOrder(input_api, output_api))
[email protected]70ca77752012-11-20 03:45:031282 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
[email protected]b8079ae4a2012-12-05 19:56:491283 results.extend(_CheckPatchFiles(input_api, output_api))
[email protected]06e6d0ff2012-12-11 01:36:441284 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
[email protected]d2530012013-01-25 16:39:271285 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
[email protected]b00342e7f2013-03-26 16:21:541286 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
lliabraa35bab3932014-10-01 12:16:441287 results.extend(_CheckForInvalidIfDefinedMacros(input_api, output_api))
danakj3c84d0c2014-10-06 15:35:461288 # TODO(danakj): Remove this when base/move.h is removed.
1289 results.extend(_CheckForUsingSideEffectsOfPass(input_api, output_api))
[email protected]e871964c2013-05-13 14:14:551290 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
[email protected]9f919cc2013-07-31 03:04:041291 results.extend(
1292 input_api.canned_checks.CheckChangeHasNoTabs(
1293 input_api,
1294 output_api,
1295 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
[email protected]85218562013-11-22 07:41:401296 results.extend(_CheckSpamLogging(input_api, output_api))
[email protected]49aa76a2013-12-04 06:59:161297 results.extend(_CheckForAnonymousVariables(input_api, output_api))
[email protected]5fe0f8742013-11-29 01:04:591298 results.extend(_CheckCygwinShell(input_api, output_api))
[email protected]999261d2014-03-03 20:08:081299 results.extend(_CheckUserActionUpdate(input_api, output_api))
[email protected]fd20b902014-05-09 02:14:531300 results.extend(_CheckNoDeprecatedCSS(input_api, output_api))
[email protected]99171a92014-06-03 08:44:471301 results.extend(_CheckParseErrors(input_api, output_api))
mlamouria82272622014-09-16 18:45:041302 results.extend(_CheckForIPCRules(input_api, output_api))
[email protected]2299dcf2012-11-15 19:56:241303
1304 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
1305 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
1306 input_api, output_api,
1307 input_api.PresubmitLocalPath(),
[email protected]6be63382013-01-21 15:42:381308 whitelist=[r'^PRESUBMIT_test\.py$']))
[email protected]22c9bd72011-03-27 16:47:391309 return results
[email protected]1f7b4172010-01-28 01:17:341310
[email protected]b337cb5b2011-01-23 21:24:051311
[email protected]66daa702011-05-28 14:41:461312def _CheckAuthorizedAuthor(input_api, output_api):
1313 """For non-googler/chromites committers, verify the author's email address is
1314 in AUTHORS.
1315 """
[email protected]9bb9cb82011-06-13 20:43:011316 # TODO(maruel): Add it to input_api?
1317 import fnmatch
1318
[email protected]66daa702011-05-28 14:41:461319 author = input_api.change.author_email
[email protected]9bb9cb82011-06-13 20:43:011320 if not author:
1321 input_api.logging.info('No author, skipping AUTHOR check')
[email protected]66daa702011-05-28 14:41:461322 return []
[email protected]c99663292011-05-31 19:46:081323 authors_path = input_api.os_path.join(
[email protected]66daa702011-05-28 14:41:461324 input_api.PresubmitLocalPath(), 'AUTHORS')
1325 valid_authors = (
1326 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
1327 for line in open(authors_path))
[email protected]ac54b132011-06-06 18:11:181328 valid_authors = [item.group(1).lower() for item in valid_authors if item]
[email protected]d8b50be2011-06-15 14:19:441329 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
[email protected]5861efb2013-01-07 18:33:231330 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
[email protected]66daa702011-05-28 14:41:461331 return [output_api.PresubmitPromptWarning(
1332 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
1333 '\n'
1334 'http://www.chromium.org/developers/contributing-code and read the '
1335 '"Legal" section\n'
1336 'If you are a chromite, verify the contributor signed the CLA.') %
1337 author)]
1338 return []
1339
1340
[email protected]b8079ae4a2012-12-05 19:56:491341def _CheckPatchFiles(input_api, output_api):
1342 problems = [f.LocalPath() for f in input_api.AffectedFiles()
1343 if f.LocalPath().endswith(('.orig', '.rej'))]
1344 if problems:
1345 return [output_api.PresubmitError(
1346 "Don't commit .rej and .orig files.", problems)]
[email protected]2fdd1f362013-01-16 03:56:031347 else:
1348 return []
[email protected]b8079ae4a2012-12-05 19:56:491349
1350
[email protected]b00342e7f2013-03-26 16:21:541351def _DidYouMeanOSMacro(bad_macro):
1352 try:
1353 return {'A': 'OS_ANDROID',
1354 'B': 'OS_BSD',
1355 'C': 'OS_CHROMEOS',
1356 'F': 'OS_FREEBSD',
1357 'L': 'OS_LINUX',
1358 'M': 'OS_MACOSX',
1359 'N': 'OS_NACL',
1360 'O': 'OS_OPENBSD',
1361 'P': 'OS_POSIX',
1362 'S': 'OS_SOLARIS',
1363 'W': 'OS_WIN'}[bad_macro[3].upper()]
1364 except KeyError:
1365 return ''
1366
1367
1368def _CheckForInvalidOSMacrosInFile(input_api, f):
1369 """Check for sensible looking, totally invalid OS macros."""
1370 preprocessor_statement = input_api.re.compile(r'^\s*#')
1371 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
1372 results = []
1373 for lnum, line in f.ChangedContents():
1374 if preprocessor_statement.search(line):
1375 for match in os_macro.finditer(line):
1376 if not match.group(1) in _VALID_OS_MACROS:
1377 good = _DidYouMeanOSMacro(match.group(1))
1378 did_you_mean = ' (did you mean %s?)' % good if good else ''
1379 results.append(' %s:%d %s%s' % (f.LocalPath(),
1380 lnum,
1381 match.group(1),
1382 did_you_mean))
1383 return results
1384
1385
1386def _CheckForInvalidOSMacros(input_api, output_api):
1387 """Check all affected files for invalid OS macros."""
1388 bad_macros = []
1389 for f in input_api.AffectedFiles():
1390 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
1391 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
1392
1393 if not bad_macros:
1394 return []
1395
1396 return [output_api.PresubmitError(
1397 'Possibly invalid OS macro[s] found. Please fix your code\n'
1398 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
1399
lliabraa35bab3932014-10-01 12:16:441400
1401def _CheckForInvalidIfDefinedMacrosInFile(input_api, f):
1402 """Check all affected files for invalid "if defined" macros."""
1403 ALWAYS_DEFINED_MACROS = (
1404 "TARGET_CPU_PPC",
1405 "TARGET_CPU_PPC64",
1406 "TARGET_CPU_68K",
1407 "TARGET_CPU_X86",
1408 "TARGET_CPU_ARM",
1409 "TARGET_CPU_MIPS",
1410 "TARGET_CPU_SPARC",
1411 "TARGET_CPU_ALPHA",
1412 "TARGET_IPHONE_SIMULATOR",
1413 "TARGET_OS_EMBEDDED",
1414 "TARGET_OS_IPHONE",
1415 "TARGET_OS_MAC",
1416 "TARGET_OS_UNIX",
1417 "TARGET_OS_WIN32",
1418 )
1419 ifdef_macro = input_api.re.compile(r'^\s*#.*(?:ifdef\s|defined\()([^\s\)]+)')
1420 results = []
1421 for lnum, line in f.ChangedContents():
1422 for match in ifdef_macro.finditer(line):
1423 if match.group(1) in ALWAYS_DEFINED_MACROS:
1424 always_defined = ' %s is always defined. ' % match.group(1)
1425 did_you_mean = 'Did you mean \'#if %s\'?' % match.group(1)
1426 results.append(' %s:%d %s\n\t%s' % (f.LocalPath(),
1427 lnum,
1428 always_defined,
1429 did_you_mean))
1430 return results
1431
1432
1433def _CheckForInvalidIfDefinedMacros(input_api, output_api):
1434 """Check all affected files for invalid "if defined" macros."""
1435 bad_macros = []
1436 for f in input_api.AffectedFiles():
1437 if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
1438 bad_macros.extend(_CheckForInvalidIfDefinedMacrosInFile(input_api, f))
1439
1440 if not bad_macros:
1441 return []
1442
1443 return [output_api.PresubmitError(
1444 'Found ifdef check on always-defined macro[s]. Please fix your code\n'
1445 'or check the list of ALWAYS_DEFINED_MACROS in src/PRESUBMIT.py.',
1446 bad_macros)]
1447
1448
danakj3c84d0c2014-10-06 15:35:461449def _CheckForUsingSideEffectsOfPass(input_api, output_api):
1450 """Check all affected files for using side effects of Pass."""
1451 errors = []
1452 for f in input_api.AffectedFiles():
1453 if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
1454 for lnum, line in f.ChangedContents():
1455 # Disallow Foo(*my_scoped_thing.Pass()); See crbug.com/418297.
1456 if re.search(r'\*[a-zA-Z0-9_]+\.Pass\(\)', line):
1457 errors.append(output_api.PresubmitError(
1458 ('%s:%d uses *foo.Pass() to delete the contents of scoped_ptr. ' +
1459 'See crbug.com/418297.') % (f.LocalPath(), lnum)))
1460 return errors
1461
1462
mlamouria82272622014-09-16 18:45:041463def _CheckForIPCRules(input_api, output_api):
1464 """Check for same IPC rules described in
1465 http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc
1466 """
1467 base_pattern = r'IPC_ENUM_TRAITS\('
1468 inclusion_pattern = input_api.re.compile(r'(%s)' % base_pattern)
1469 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_pattern)
1470
1471 problems = []
1472 for f in input_api.AffectedSourceFiles(None):
1473 local_path = f.LocalPath()
1474 if not local_path.endswith('.h'):
1475 continue
1476 for line_number, line in f.ChangedContents():
1477 if inclusion_pattern.search(line) and not comment_pattern.search(line):
1478 problems.append(
1479 '%s:%d\n %s' % (local_path, line_number, line.strip()))
1480
1481 if problems:
1482 return [output_api.PresubmitPromptWarning(
1483 _IPC_ENUM_TRAITS_DEPRECATED, problems)]
1484 else:
1485 return []
1486
[email protected]b00342e7f2013-03-26 16:21:541487
[email protected]1f7b4172010-01-28 01:17:341488def CheckChangeOnUpload(input_api, output_api):
1489 results = []
1490 results.extend(_CommonChecks(input_api, output_api))
tandriief664692014-09-23 14:51:471491 results.extend(_CheckValidHostsInDEPS(input_api, output_api))
aurimas8d3bc1c52014-10-15 01:02:171492 results.extend(_CheckJavaStyle(input_api, output_api))
[email protected]fe5f57c52009-06-05 14:25:541493 return results
[email protected]ca8d1982009-02-19 16:33:121494
1495
[email protected]1bfb8322014-04-23 01:02:411496def GetTryServerMasterForBot(bot):
1497 """Returns the Try Server master for the given bot.
1498
[email protected]0bb112362014-07-26 04:38:321499 It tries to guess the master from the bot name, but may still fail
1500 and return None. There is no longer a default master.
1501 """
1502 # Potentially ambiguous bot names are listed explicitly.
1503 master_map = {
[email protected]1bfb8322014-04-23 01:02:411504 'linux_gpu': 'tryserver.chromium.gpu',
[email protected]5c5f13042014-05-09 21:28:301505 'mac_gpu': 'tryserver.chromium.gpu',
[email protected]d263d5b2014-04-30 01:15:551506 'win_gpu': 'tryserver.chromium.gpu',
[email protected]0bb112362014-07-26 04:38:321507 'chromium_presubmit': 'tryserver.chromium.linux',
1508 'blink_presubmit': 'tryserver.chromium.linux',
1509 'tools_build_presubmit': 'tryserver.chromium.linux',
[email protected]1bfb8322014-04-23 01:02:411510 }
[email protected]0bb112362014-07-26 04:38:321511 master = master_map.get(bot)
1512 if not master:
1513 if 'gpu' in bot:
1514 master = 'tryserver.chromium.gpu'
1515 elif 'linux' in bot or 'android' in bot or 'presubmit' in bot:
1516 master = 'tryserver.chromium.linux'
1517 elif 'win' in bot:
1518 master = 'tryserver.chromium.win'
1519 elif 'mac' in bot or 'ios' in bot:
1520 master = 'tryserver.chromium.mac'
1521 return master
[email protected]1bfb8322014-04-23 01:02:411522
1523
[email protected]38c6a512013-12-18 23:48:011524def GetDefaultTryConfigs(bots=None):
1525 """Returns a list of ('bot', set(['tests']), optionally filtered by [bots].
1526
1527 To add tests to this list, they MUST be in the the corresponding master's
1528 gatekeeper config. For example, anything on master.chromium would be closed by
1529 tools/build/masters/master.chromium/master_gatekeeper_cfg.py.
1530
1531 If 'bots' is specified, will only return configurations for bots in that list.
1532 """
1533
1534 standard_tests = [
1535 'base_unittests',
1536 'browser_tests',
1537 'cacheinvalidation_unittests',
1538 'check_deps',
1539 'check_deps2git',
1540 'content_browsertests',
1541 'content_unittests',
1542 'crypto_unittests',
[email protected]38c6a512013-12-18 23:48:011543 'gpu_unittests',
1544 'interactive_ui_tests',
1545 'ipc_tests',
1546 'jingle_unittests',
1547 'media_unittests',
1548 'net_unittests',
1549 'ppapi_unittests',
1550 'printing_unittests',
1551 'sql_unittests',
1552 'sync_unit_tests',
1553 'unit_tests',
1554 # Broken in release.
1555 #'url_unittests',
1556 #'webkit_unit_tests',
1557 ]
1558
[email protected]38c6a512013-12-18 23:48:011559 builders_and_tests = {
1560 # TODO(maruel): Figure out a way to run 'sizes' where people can
1561 # effectively update the perf expectation correctly. This requires a
1562 # clobber=True build running 'sizes'. 'sizes' is not accurate with
1563 # incremental build. Reference:
1564 # http://chromium.org/developers/tree-sheriffs/perf-sheriffs.
1565 # TODO(maruel): An option would be to run 'sizes' but not count a failure
1566 # of this step as a try job failure.
1567 'android_aosp': ['compile'],
scottmgd4724062014-09-20 00:27:091568 'android_arm64_dbg_recipe': ['slave_steps'],
1569 'android_chromium_gn_compile_dbg': ['compile'],
[email protected]5a65d3042014-05-07 14:26:011570 'android_chromium_gn_compile_rel': ['compile'],
[email protected]38c6a512013-12-18 23:48:011571 'android_clang_dbg': ['slave_steps'],
scottmgd4724062014-09-20 00:27:091572 'android_clang_dbg_recipe': ['slave_steps'],
[email protected]5bd4f0cd2014-08-22 21:59:291573 'android_dbg_tests_recipe': ['slave_steps'],
[email protected]38c6a512013-12-18 23:48:011574 'cros_x86': ['defaulttests'],
1575 'ios_dbg_simulator': [
1576 'compile',
1577 'base_unittests',
1578 'content_unittests',
1579 'crypto_unittests',
1580 'url_unittests',
1581 'net_unittests',
1582 'sql_unittests',
tfarina201471d2014-10-02 18:12:111583 'ui_base_unittests',
[email protected]38c6a512013-12-18 23:48:011584 'ui_unittests',
1585 ],
1586 'ios_rel_device': ['compile'],
scottmgd4724062014-09-20 00:27:091587 'ios_rel_device_ninja': ['compile'],
[email protected]971b08ce2014-03-19 22:03:561588 'linux_asan': ['compile'],
1589 'mac_asan': ['compile'],
[email protected]38c6a512013-12-18 23:48:011590 #TODO(stip): Change the name of this builder to reflect that it's release.
[email protected]71afb4ec2014-02-07 02:45:131591 'linux_gtk': standard_tests,
[email protected]971b08ce2014-03-19 22:03:561592 'linux_chromeos_asan': ['compile'],
[email protected]d910b6082014-02-27 18:15:431593 'linux_chromium_chromeos_clang_dbg': ['defaulttests'],
[email protected]ce51953982014-08-07 15:43:241594 'linux_chromium_chromeos_rel_swarming': ['defaulttests'],
[email protected]d910b6082014-02-27 18:15:431595 'linux_chromium_compile_dbg': ['defaulttests'],
scottmgd4724062014-09-20 00:27:091596 'linux_chromium_gn_dbg': ['compile'],
[email protected]5a65d3042014-05-07 14:26:011597 'linux_chromium_gn_rel': ['defaulttests'],
[email protected]0bb112362014-07-26 04:38:321598 'linux_chromium_rel_swarming': ['defaulttests'],
[email protected]d910b6082014-02-27 18:15:431599 'linux_chromium_clang_dbg': ['defaulttests'],
[email protected]1bfb8322014-04-23 01:02:411600 'linux_gpu': ['defaulttests'],
[email protected]9780bac2014-01-11 00:12:021601 'linux_nacl_sdk_build': ['compile'],
[email protected]d910b6082014-02-27 18:15:431602 'mac_chromium_compile_dbg': ['defaulttests'],
[email protected]ce51953982014-08-07 15:43:241603 'mac_chromium_rel_swarming': ['defaulttests'],
[email protected]5c5f13042014-05-09 21:28:301604 'mac_gpu': ['defaulttests'],
[email protected]9780bac2014-01-11 00:12:021605 'mac_nacl_sdk_build': ['compile'],
[email protected]0094fa12014-03-13 03:18:281606 'win_chromium_compile_dbg': ['defaulttests'],
[email protected]c17144e42014-04-15 09:32:431607 'win_chromium_dbg': ['defaulttests'],
[email protected]02a7f6362014-08-13 02:04:161608 'win_chromium_rel_swarming': ['defaulttests'],
[email protected]c0f4e82b2014-08-15 23:35:341609 'win_chromium_x64_rel_swarming': ['defaulttests'],
[email protected]d263d5b2014-04-30 01:15:551610 'win_gpu': ['defaulttests'],
[email protected]9780bac2014-01-11 00:12:021611 'win_nacl_sdk_build': ['compile'],
danakjc89745a2014-09-16 01:33:031612 'win8_chromium_rel': ['defaulttests'],
[email protected]38c6a512013-12-18 23:48:011613 }
1614
[email protected]38c6a512013-12-18 23:48:011615 if bots:
[email protected]1bfb8322014-04-23 01:02:411616 filtered_builders_and_tests = dict((bot, set(builders_and_tests[bot]))
1617 for bot in bots)
[email protected]38c6a512013-12-18 23:48:011618 else:
[email protected]1bfb8322014-04-23 01:02:411619 filtered_builders_and_tests = dict(
1620 (bot, set(tests))
1621 for bot, tests in builders_and_tests.iteritems())
1622
1623 # Build up the mapping from tryserver master to bot/test.
1624 out = dict()
1625 for bot, tests in filtered_builders_and_tests.iteritems():
1626 out.setdefault(GetTryServerMasterForBot(bot), {})[bot] = tests
1627 return out
[email protected]38c6a512013-12-18 23:48:011628
1629
[email protected]ca8d1982009-02-19 16:33:121630def CheckChangeOnCommit(input_api, output_api):
[email protected]fe5f57c52009-06-05 14:25:541631 results = []
[email protected]1f7b4172010-01-28 01:17:341632 results.extend(_CommonChecks(input_api, output_api))
[email protected]dd805fe2009-10-01 08:11:511633 # TODO(thestig) temporarily disabled, doesn't work in third_party/
1634 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
1635 # input_api, output_api, sources))
[email protected]fe5f57c52009-06-05 14:25:541636 # Make sure the tree is 'open'.
[email protected]806e98e2010-03-19 17:49:271637 results.extend(input_api.canned_checks.CheckTreeIsOpen(
[email protected]7f238152009-08-12 19:00:341638 input_api,
1639 output_api,
[email protected]2fdd1f362013-01-16 03:56:031640 json_url='http://chromium-status.appspot.com/current?format=json'))
[email protected]806e98e2010-03-19 17:49:271641
[email protected]3e4eb112011-01-18 03:29:541642 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1643 input_api, output_api))
[email protected]c4b47562011-12-05 23:39:411644 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1645 input_api, output_api))
[email protected]fe5f57c52009-06-05 14:25:541646 return results
[email protected]ca8d1982009-02-19 16:33:121647
1648
[email protected]7468ac522014-03-12 23:35:571649def GetPreferredTryMasters(project, change):
[email protected]4ce995ea2012-06-27 02:13:101650 files = change.LocalPaths()
1651
joaodasilva718f87672014-08-30 09:25:491652 if not files or all(re.search(r'[\\\/]OWNERS$', f) for f in files):
[email protected]7468ac522014-03-12 23:35:571653 return {}
[email protected]3019c902012-06-29 00:09:031654
joaodasilva718f87672014-08-30 09:25:491655 if all(re.search(r'\.(m|mm)$|(^|[\\\/_])mac[\\\/_.]', f) for f in files):
[email protected]d96b1f42014-02-27 19:17:521656 return GetDefaultTryConfigs([
1657 'mac_chromium_compile_dbg',
[email protected]e6890ec2014-08-07 16:59:421658 'mac_chromium_rel_swarming',
[email protected]d96b1f42014-02-27 19:17:521659 ])
[email protected]d668899a2012-09-06 18:16:591660 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
[email protected]02a7f6362014-08-13 02:04:161661 return GetDefaultTryConfigs([
1662 'win_chromium_dbg',
1663 'win_chromium_rel_swarming',
danakjc89745a2014-09-16 01:33:031664 'win8_chromium_rel',
[email protected]02a7f6362014-08-13 02:04:161665 ])
joaodasilva718f87672014-08-30 09:25:491666 if all(re.search(r'(^|[\\\/_])android[\\\/_.]', f) for f in files):
[email protected]38c6a512013-12-18 23:48:011667 return GetDefaultTryConfigs([
1668 'android_aosp',
1669 'android_clang_dbg',
[email protected]5bd4f0cd2014-08-22 21:59:291670 'android_dbg_tests_recipe',
[email protected]38c6a512013-12-18 23:48:011671 ])
joaodasilva718f87672014-08-30 09:25:491672 if all(re.search(r'[\\\/_]ios[\\\/_.]', f) for f in files):
[email protected]38c6a512013-12-18 23:48:011673 return GetDefaultTryConfigs(['ios_rel_device', 'ios_dbg_simulator'])
[email protected]4ce995ea2012-06-27 02:13:101674
[email protected]7468ac522014-03-12 23:35:571675 builders = [
scottmgd4724062014-09-20 00:27:091676 'android_arm64_dbg_recipe',
[email protected]5a65d3042014-05-07 14:26:011677 'android_chromium_gn_compile_rel',
scottmgd4724062014-09-20 00:27:091678 'android_chromium_gn_compile_dbg',
[email protected]3e2f0402012-11-02 16:28:011679 'android_clang_dbg',
scottmgd4724062014-09-20 00:27:091680 'android_clang_dbg_recipe',
[email protected]5bd4f0cd2014-08-22 21:59:291681 'android_dbg_tests_recipe',
[email protected]3e2f0402012-11-02 16:28:011682 'ios_dbg_simulator',
1683 'ios_rel_device',
scottmgd4724062014-09-20 00:27:091684 'ios_rel_device_ninja',
[email protected]e6890ec2014-08-07 16:59:421685 'linux_chromium_chromeos_rel_swarming',
[email protected]d96b1f42014-02-27 19:17:521686 'linux_chromium_clang_dbg',
scottmgd4724062014-09-20 00:27:091687 'linux_chromium_gn_dbg',
[email protected]5a65d3042014-05-07 14:26:011688 'linux_chromium_gn_rel',
[email protected]0bb112362014-07-26 04:38:321689 'linux_chromium_rel_swarming',
[email protected]1bfb8322014-04-23 01:02:411690 'linux_gpu',
[email protected]d96b1f42014-02-27 19:17:521691 'mac_chromium_compile_dbg',
[email protected]e6890ec2014-08-07 16:59:421692 'mac_chromium_rel_swarming',
[email protected]5c5f13042014-05-09 21:28:301693 'mac_gpu',
[email protected]0094fa12014-03-13 03:18:281694 'win_chromium_compile_dbg',
[email protected]02a7f6362014-08-13 02:04:161695 'win_chromium_rel_swarming',
[email protected]c0f4e82b2014-08-15 23:35:341696 'win_chromium_x64_rel_swarming',
[email protected]d263d5b2014-04-30 01:15:551697 'win_gpu',
danakjc89745a2014-09-16 01:33:031698 'win8_chromium_rel',
[email protected]7468ac522014-03-12 23:35:571699 ]
[email protected]911753b2012-08-02 12:11:541700
1701 # Match things like path/aura/file.cc and path/file_aura.cc.
[email protected]95c989162012-11-29 05:58:251702 # Same for chromeos.
joaodasilva718f87672014-08-30 09:25:491703 if any(re.search(r'[\\\/_](aura|chromeos)', f) for f in files):
[email protected]7468ac522014-03-12 23:35:571704 builders.extend([
1705 'linux_chromeos_asan',
1706 'linux_chromium_chromeos_clang_dbg'
1707 ])
[email protected]4ce995ea2012-06-27 02:13:101708
[email protected]e8df48f2013-09-30 20:07:541709 # If there are gyp changes to base, build, or chromeos, run a full cros build
1710 # in addition to the shorter linux_chromeos build. Changes to high level gyp
1711 # files have a much higher chance of breaking the cros build, which is
1712 # differnt from the linux_chromeos build that most chrome developers test
1713 # with.
1714 if any(re.search('^(base|build|chromeos).*\.gypi?$', f) for f in files):
[email protected]7468ac522014-03-12 23:35:571715 builders.extend(['cros_x86'])
[email protected]e8df48f2013-09-30 20:07:541716
[email protected]d95948ef2013-07-02 10:51:001717 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1718 # unless they're .gyp(i) files as changes to those files can break the gyp
1719 # step on that bot.
1720 if (not all(re.search('^chrome', f) for f in files) or
1721 any(re.search('\.gypi?$', f) for f in files)):
[email protected]7468ac522014-03-12 23:35:571722 builders.extend(['android_aosp'])
[email protected]d95948ef2013-07-02 10:51:001723
[email protected]7468ac522014-03-12 23:35:571724 return GetDefaultTryConfigs(builders)