Roll src/third_party/skia 6a070dc:b103d0c
[chromium-blink-merge.git] / PRESUBMIT.py
blob7c8d58e35a1bf00d995f381d618696bf2be1b534
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 Chromium.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into gcl.
9 """
12 _EXCLUDED_PATHS = (
13 r"^breakpad[\\\/].*",
14 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_rules.py",
15 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_simple.py",
16 r"^native_client_sdk[\\\/]src[\\\/]tools[\\\/].*.mk",
17 r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*",
18 r"^skia[\\\/].*",
19 r"^v8[\\\/].*",
20 r".*MakeFile$",
21 r".+_autogen\.h$",
22 r".+[\\\/]pnacl_shim\.c$",
23 r"^gpu[\\\/]config[\\\/].*_list_json\.cc$",
24 r"^chrome[\\\/]browser[\\\/]resources[\\\/]pdf[\\\/]index.js"
27 # The NetscapePlugIn library is excluded from pan-project as it will soon
28 # be deleted together with the rest of the NPAPI and it's not worthwhile to
29 # update the coding style until then.
30 _TESTRUNNER_PATHS = (
31 r"^content[\\\/]shell[\\\/]tools[\\\/]plugin[\\\/].*",
34 # Fragment of a regular expression that matches C++ and Objective-C++
35 # implementation files.
36 _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
38 # Regular expression that matches code only used for test binaries
39 # (best effort).
40 _TEST_CODE_EXCLUDED_PATHS = (
41 r'.*[\\\/](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS,
42 r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS,
43 r'.+_(api|browser|kif|perf|pixel|unit|ui)?test(_[a-z]+)?%s' %
44 _IMPLEMENTATION_EXTENSIONS,
45 r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS,
46 r'.*[\\\/](test|tool(s)?)[\\\/].*',
47 # content_shell is used for running layout tests.
48 r'content[\\\/]shell[\\\/].*',
49 # At request of folks maintaining this folder.
50 r'chrome[\\\/]browser[\\\/]automation[\\\/].*',
51 # Non-production example code.
52 r'mojo[\\\/]examples[\\\/].*',
53 # Launcher for running iOS tests on the simulator.
54 r'testing[\\\/]iossim[\\\/]iossim\.mm$',
57 _TEST_ONLY_WARNING = (
58 'You might be calling functions intended only for testing from\n'
59 'production code. It is OK to ignore this warning if you know what\n'
60 'you are doing, as the heuristics used to detect the situation are\n'
61 'not perfect. The commit queue will not block on this warning.')
64 _INCLUDE_ORDER_WARNING = (
65 'Your #include order seems to be broken. Send mail to\n'
66 'marja@chromium.org if this is not the case.')
69 _BANNED_OBJC_FUNCTIONS = (
71 'addTrackingRect:',
73 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
74 'prohibited. Please use CrTrackingArea instead.',
75 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
77 False,
80 r'/NSTrackingArea\W',
82 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
83 'instead.',
84 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
86 False,
89 'convertPointFromBase:',
91 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
92 'Please use |convertPoint:(point) fromView:nil| instead.',
93 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
95 True,
98 'convertPointToBase:',
100 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
101 'Please use |convertPoint:(point) toView:nil| instead.',
102 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
104 True,
107 'convertRectFromBase:',
109 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
110 'Please use |convertRect:(point) fromView:nil| instead.',
111 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
113 True,
116 'convertRectToBase:',
118 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
119 'Please use |convertRect:(point) toView:nil| instead.',
120 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
122 True,
125 'convertSizeFromBase:',
127 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
128 'Please use |convertSize:(point) fromView:nil| instead.',
129 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
131 True,
134 'convertSizeToBase:',
136 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
137 'Please use |convertSize:(point) toView:nil| instead.',
138 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
140 True,
145 _BANNED_CPP_FUNCTIONS = (
146 # Make sure that gtest's FRIEND_TEST() macro is not used; the
147 # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
148 # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
150 'FRIEND_TEST(',
152 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
153 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
155 False,
159 'ScopedAllowIO',
161 'New code should not use ScopedAllowIO. Post a task to the blocking',
162 'pool or the FILE thread instead.',
164 True,
166 r"^base[\\\/]process[\\\/]process_metrics_linux\.cc$",
167 r"^chrome[\\\/]browser[\\\/]chromeos[\\\/]boot_times_loader\.cc$",
168 r"^components[\\\/]crash[\\\/]app[\\\/]breakpad_mac\.mm$",
169 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_browser_main\.cc$",
170 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_message_filter\.cc$",
171 r"^mojo[\\\/]edk[\\\/]embedder[\\\/]" +
172 r"simple_platform_shared_buffer_posix\.cc$",
173 r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
174 r"^net[\\\/]url_request[\\\/]test_url_fetcher_factory\.cc$",
178 'SkRefPtr',
180 'The use of SkRefPtr is prohibited. ',
181 'Please use skia::RefPtr instead.'
183 True,
187 'SkAutoRef',
189 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
190 'Please use skia::RefPtr instead.'
192 True,
196 'SkAutoTUnref',
198 'The use of SkAutoTUnref is dangerous because it implicitly ',
199 'converts to a raw pointer. Please use skia::RefPtr instead.'
201 True,
205 'SkAutoUnref',
207 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
208 'because it implicitly converts to a raw pointer. ',
209 'Please use skia::RefPtr instead.'
211 True,
215 r'/HANDLE_EINTR\(.*close',
217 'HANDLE_EINTR(close) is invalid. If close fails with EINTR, the file',
218 'descriptor will be closed, and it is incorrect to retry the close.',
219 'Either call close directly and ignore its return value, or wrap close',
220 'in IGNORE_EINTR to use its return value. See http://crbug.com/269623'
222 True,
226 r'/IGNORE_EINTR\((?!.*close)',
228 'IGNORE_EINTR is only valid when wrapping close. To wrap other system',
229 'calls, use HANDLE_EINTR. See http://crbug.com/269623',
231 True,
233 # Files that #define IGNORE_EINTR.
234 r'^base[\\\/]posix[\\\/]eintr_wrapper\.h$',
235 r'^ppapi[\\\/]tests[\\\/]test_broker\.cc$',
239 r'/v8::Extension\(',
241 'Do not introduce new v8::Extensions into the code base, use',
242 'gin::Wrappable instead. See http://crbug.com/334679',
244 True,
246 r'extensions[\\\/]renderer[\\\/]safe_builtins\.*',
251 _IPC_ENUM_TRAITS_DEPRECATED = (
252 'You are using IPC_ENUM_TRAITS() in your code. It has been deprecated.\n'
253 'See http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc')
256 _VALID_OS_MACROS = (
257 # Please keep sorted.
258 'OS_ANDROID',
259 'OS_ANDROID_HOST',
260 'OS_BSD',
261 'OS_CAT', # For testing.
262 'OS_CHROMEOS',
263 'OS_FREEBSD',
264 'OS_IOS',
265 'OS_LINUX',
266 'OS_MACOSX',
267 'OS_NACL',
268 'OS_NACL_NONSFI',
269 'OS_NACL_SFI',
270 'OS_OPENBSD',
271 'OS_POSIX',
272 'OS_QNX',
273 'OS_SOLARIS',
274 'OS_WIN',
278 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
279 """Attempts to prevent use of functions intended only for testing in
280 non-testing code. For now this is just a best-effort implementation
281 that ignores header files and may have some false positives. A
282 better implementation would probably need a proper C++ parser.
284 # We only scan .cc files and the like, as the declaration of
285 # for-testing functions in header files are hard to distinguish from
286 # calls to such functions without a proper C++ parser.
287 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
289 base_function_pattern = r'[ :]test::[^\s]+|ForTest(ing)?|for_test(ing)?'
290 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
291 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern)
292 exclusion_pattern = input_api.re.compile(
293 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
294 base_function_pattern, base_function_pattern))
296 def FilterFile(affected_file):
297 black_list = (_EXCLUDED_PATHS +
298 _TEST_CODE_EXCLUDED_PATHS +
299 input_api.DEFAULT_BLACK_LIST)
300 return input_api.FilterSourceFile(
301 affected_file,
302 white_list=(file_inclusion_pattern, ),
303 black_list=black_list)
305 problems = []
306 for f in input_api.AffectedSourceFiles(FilterFile):
307 local_path = f.LocalPath()
308 for line_number, line in f.ChangedContents():
309 if (inclusion_pattern.search(line) and
310 not comment_pattern.search(line) and
311 not exclusion_pattern.search(line)):
312 problems.append(
313 '%s:%d\n %s' % (local_path, line_number, line.strip()))
315 if problems:
316 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
317 else:
318 return []
321 def _CheckNoIOStreamInHeaders(input_api, output_api):
322 """Checks to make sure no .h files include <iostream>."""
323 files = []
324 pattern = input_api.re.compile(r'^#include\s*<iostream>',
325 input_api.re.MULTILINE)
326 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
327 if not f.LocalPath().endswith('.h'):
328 continue
329 contents = input_api.ReadFile(f)
330 if pattern.search(contents):
331 files.append(f)
333 if len(files):
334 return [ output_api.PresubmitError(
335 'Do not #include <iostream> in header files, since it inserts static '
336 'initialization into every file including the header. Instead, '
337 '#include <ostream>. See http://crbug.com/94794',
338 files) ]
339 return []
342 def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
343 """Checks to make sure no source files use UNIT_TEST"""
344 problems = []
345 for f in input_api.AffectedFiles():
346 if (not f.LocalPath().endswith(('.cc', '.mm'))):
347 continue
349 for line_num, line in f.ChangedContents():
350 if 'UNIT_TEST ' in line or line.endswith('UNIT_TEST'):
351 problems.append(' %s:%d' % (f.LocalPath(), line_num))
353 if not problems:
354 return []
355 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
356 '\n'.join(problems))]
359 def _CheckNoNewWStrings(input_api, output_api):
360 """Checks to make sure we don't introduce use of wstrings."""
361 problems = []
362 for f in input_api.AffectedFiles():
363 if (not f.LocalPath().endswith(('.cc', '.h')) or
364 f.LocalPath().endswith(('test.cc', '_win.cc', '_win.h')) or
365 '/win/' in f.LocalPath()):
366 continue
368 allowWString = False
369 for line_num, line in f.ChangedContents():
370 if 'presubmit: allow wstring' in line:
371 allowWString = True
372 elif not allowWString and 'wstring' in line:
373 problems.append(' %s:%d' % (f.LocalPath(), line_num))
374 allowWString = False
375 else:
376 allowWString = False
378 if not problems:
379 return []
380 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
381 ' If you are calling a cross-platform API that accepts a wstring, '
382 'fix the API.\n' +
383 '\n'.join(problems))]
386 def _CheckNoDEPSGIT(input_api, output_api):
387 """Make sure .DEPS.git is never modified manually."""
388 if any(f.LocalPath().endswith('.DEPS.git') for f in
389 input_api.AffectedFiles()):
390 return [output_api.PresubmitError(
391 'Never commit changes to .DEPS.git. This file is maintained by an\n'
392 'automated system based on what\'s in DEPS and your changes will be\n'
393 'overwritten.\n'
394 'See https://sites.google.com/a/chromium.org/dev/developers/how-tos/get-the-code#Rolling_DEPS\n'
395 'for more information')]
396 return []
399 def _CheckValidHostsInDEPS(input_api, output_api):
400 """Checks that DEPS file deps are from allowed_hosts."""
401 # Run only if DEPS file has been modified to annoy fewer bystanders.
402 if all(f.LocalPath() != 'DEPS' for f in input_api.AffectedFiles()):
403 return []
404 # Outsource work to gclient verify
405 try:
406 input_api.subprocess.check_output(['gclient', 'verify'])
407 return []
408 except input_api.subprocess.CalledProcessError, error:
409 return [output_api.PresubmitError(
410 'DEPS file must have only git dependencies.',
411 long_text=error.output)]
414 def _CheckNoBannedFunctions(input_api, output_api):
415 """Make sure that banned functions are not used."""
416 warnings = []
417 errors = []
419 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
420 for f in input_api.AffectedFiles(file_filter=file_filter):
421 for line_num, line in f.ChangedContents():
422 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
423 matched = False
424 if func_name[0:1] == '/':
425 regex = func_name[1:]
426 if input_api.re.search(regex, line):
427 matched = True
428 elif func_name in line:
429 matched = True
430 if matched:
431 problems = warnings;
432 if error:
433 problems = errors;
434 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
435 for message_line in message:
436 problems.append(' %s' % message_line)
438 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
439 for f in input_api.AffectedFiles(file_filter=file_filter):
440 for line_num, line in f.ChangedContents():
441 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
442 def IsBlacklisted(affected_file, blacklist):
443 local_path = affected_file.LocalPath()
444 for item in blacklist:
445 if input_api.re.match(item, local_path):
446 return True
447 return False
448 if IsBlacklisted(f, excluded_paths):
449 continue
450 matched = False
451 if func_name[0:1] == '/':
452 regex = func_name[1:]
453 if input_api.re.search(regex, line):
454 matched = True
455 elif func_name in line:
456 matched = True
457 if matched:
458 problems = warnings;
459 if error:
460 problems = errors;
461 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
462 for message_line in message:
463 problems.append(' %s' % message_line)
465 result = []
466 if (warnings):
467 result.append(output_api.PresubmitPromptWarning(
468 'Banned functions were used.\n' + '\n'.join(warnings)))
469 if (errors):
470 result.append(output_api.PresubmitError(
471 'Banned functions were used.\n' + '\n'.join(errors)))
472 return result
475 def _CheckNoPragmaOnce(input_api, output_api):
476 """Make sure that banned functions are not used."""
477 files = []
478 pattern = input_api.re.compile(r'^#pragma\s+once',
479 input_api.re.MULTILINE)
480 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
481 if not f.LocalPath().endswith('.h'):
482 continue
483 contents = input_api.ReadFile(f)
484 if pattern.search(contents):
485 files.append(f)
487 if files:
488 return [output_api.PresubmitError(
489 'Do not use #pragma once in header files.\n'
490 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
491 files)]
492 return []
495 def _CheckNoTrinaryTrueFalse(input_api, output_api):
496 """Checks to make sure we don't introduce use of foo ? true : false."""
497 problems = []
498 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
499 for f in input_api.AffectedFiles():
500 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
501 continue
503 for line_num, line in f.ChangedContents():
504 if pattern.match(line):
505 problems.append(' %s:%d' % (f.LocalPath(), line_num))
507 if not problems:
508 return []
509 return [output_api.PresubmitPromptWarning(
510 'Please consider avoiding the "? true : false" pattern if possible.\n' +
511 '\n'.join(problems))]
514 def _CheckUnwantedDependencies(input_api, output_api):
515 """Runs checkdeps on #include statements added in this
516 change. Breaking - rules is an error, breaking ! rules is a
517 warning.
519 import sys
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(
526 input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
527 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
534 added_includes = []
535 for f in input_api.AffectedFiles():
536 if not CppChecker.IsCppFile(f.LocalPath()):
537 continue
539 changed_lines = [line for line_num, line in f.ChangedContents()]
540 added_includes.append([f.LocalPath(), changed_lines])
542 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
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)
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:
560 results.append(output_api.PresubmitPromptOrNotify(
561 '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
568 def _CheckFilePermissions(input_api, output_api):
569 """Check that all files have their permissions properly set."""
570 if input_api.platform == 'win32':
571 return []
572 args = [input_api.python_executable, 'tools/checkperms/checkperms.py',
573 '--root', input_api.change.RepositoryRoot()]
574 for f in input_api.AffectedFiles():
575 args += ['--file', f.LocalPath()]
576 checkperms = input_api.subprocess.Popen(args,
577 stdout=input_api.subprocess.PIPE)
578 errors = checkperms.communicate()[0].strip()
579 if errors:
580 return [output_api.PresubmitError('checkperms.py failed.',
581 errors.splitlines())]
582 return []
585 def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
586 """Makes sure we don't include ui/aura/window_property.h
587 in header files.
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))
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
605 def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
606 """Checks that the lines in scope occur in the right order.
608 1. C system files in alphabetical order
609 2. C++ system files in alphabetical order
610 3. Project's .h files
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 ".*')
617 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
619 state = C_SYSTEM_INCLUDES
621 previous_line = ''
622 previous_line_num = 0
623 problem_linenums = []
624 for line_num, line in scope:
625 if c_system_include_pattern.match(line):
626 if state != C_SYSTEM_INCLUDES:
627 problem_linenums.append((line_num, previous_line_num))
628 elif previous_line and previous_line > line:
629 problem_linenums.append((line_num, previous_line_num))
630 elif cpp_system_include_pattern.match(line):
631 if state == C_SYSTEM_INCLUDES:
632 state = CPP_SYSTEM_INCLUDES
633 elif state == CUSTOM_INCLUDES:
634 problem_linenums.append((line_num, previous_line_num))
635 elif previous_line and previous_line > line:
636 problem_linenums.append((line_num, previous_line_num))
637 elif custom_include_pattern.match(line):
638 if state != CUSTOM_INCLUDES:
639 state = CUSTOM_INCLUDES
640 elif previous_line and previous_line > line:
641 problem_linenums.append((line_num, previous_line_num))
642 else:
643 problem_linenums.append(line_num)
644 previous_line = line
645 previous_line_num = line_num
647 warnings = []
648 for (line_num, previous_line_num) in problem_linenums:
649 if line_num in changed_linenums or previous_line_num in changed_linenums:
650 warnings.append(' %s:%d' % (file_path, line_num))
651 return warnings
654 def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
655 """Checks the #include order for the given file f."""
657 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
658 # 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")')
664 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
665 # 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]+)?\.)')
669 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*')
677 contents = f.NewContents()
678 warnings = []
679 line_num = 0
681 # 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.
685 # 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()))
690 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()
700 header_basename = test_file_tag_pattern.sub(
701 '', input_api.os_path.basename(match_dict['FILE'])).replace('.h', '')
703 if header_basename not in including_file_base_name:
704 # No special first include -> process the line again along with normal
705 # includes.
706 line_num -= 1
707 break
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
714 if uncheckable_includes_pattern.match(line):
715 continue
716 if if_pattern.match(line):
717 scopes.append(current_scope)
718 current_scope = []
719 elif ((system_include_pattern.match(line) or
720 custom_include_pattern.match(line)) and
721 not excluded_include_pattern.match(line)):
722 current_scope.append((line_num, line))
723 scopes.append(current_scope)
725 for scope in scopes:
726 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
727 changed_linenums))
728 return warnings
731 def _CheckIncludeOrder(input_api, output_api):
732 """Checks that the #include order is correct.
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
739 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
740 these rules separately.
742 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)
746 warnings = []
747 for f in input_api.AffectedFiles(file_filter=FileFilterIncludeOrder):
748 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))
752 results = []
753 if warnings:
754 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
755 warnings))
756 return results
759 def _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
768 def _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))
774 results = []
775 if errors:
776 results.append(output_api.PresubmitError(
777 'Version control conflict markers found, please resolve.', errors))
778 return results
781 def _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).
788 return input_api.FilterSourceFile(
789 affected_file,
790 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
791 black_list=(_EXCLUDED_PATHS +
792 _TEST_CODE_EXCLUDED_PATHS +
793 input_api.DEFAULT_BLACK_LIST))
795 base_pattern = '"[^"]*google\.com[^"]*"'
796 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
797 pattern = input_api.re.compile(base_pattern)
798 problems = [] # items are (filename, line_number, line)
799 for f in input_api.AffectedSourceFiles(FilterFile):
800 for line_num, line in f.ChangedContents():
801 if not comment_pattern.search(line) and pattern.search(line):
802 problems.append((f.LocalPath(), line_num, line))
804 if problems:
805 return [output_api.PresubmitPromptOrNotify(
806 'Most layers below src/chrome/ should not hardcode service URLs.\n'
807 'Are you sure this is correct?',
808 [' %s:%d: %s' % (
809 problem[0], problem[1], problem[2]) for problem in problems])]
810 else:
811 return []
814 def _CheckNoAbbreviationInPngFileName(input_api, output_api):
815 """Makes sure there are no abbreviations in the name of PNG files.
817 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
818 errors = []
819 for f in input_api.AffectedFiles(include_deletes=False):
820 if pattern.match(f.LocalPath()):
821 errors.append(' %s' % f.LocalPath())
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 oshima@chromium.org if you have questions.', errors))
829 return results
832 def _FilesToCheckForIncomingDeps(re, changed_lines):
833 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
834 a set of DEPS entries that we should look up.
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.
840 # We ignore deps entries on auto-generated directories.
841 AUTO_GENERATED_DIRS = ['grit', 'jni']
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)?['"].*""")
849 results = set()
850 for changed_line in changed_lines:
851 m = pattern.match(changed_line)
852 if m:
853 path = m.group(1)
854 if path.split('/')[0] not in AUTO_GENERATED_DIRS:
855 if m.group(2):
856 results.add('%s%s' % (path, m.group(2)))
857 else:
858 results.add('%s/DEPS' % path)
859 return results
862 def _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.
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 []
878 virtual_depended_on_files = _FilesToCheckForIncomingDeps(input_api.re,
879 changed_lines)
880 if not virtual_depended_on_files:
881 return []
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
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)
901 owner_email = owner_email or input_api.change.author_email
903 reviewers_plus_owner = set(reviewers)
904 if owner_email:
905 reviewers_plus_owner.add(owner_email)
906 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
907 reviewers_plus_owner)
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)
919 for path in missing_files]
921 if unapproved_dependencies:
922 output_list = [
923 output('Missing LGTM from OWNERS of dependencies added to DEPS:\n %s' %
924 '\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
932 return []
935 def _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 +
940 (r"^base[\\\/]logging\.h$",
941 r"^base[\\\/]logging\.cc$",
942 r"^chrome[\\\/]app[\\\/]chrome_main_delegate\.cc$",
943 r"^chrome[\\\/]browser[\\\/]chrome_browser_main\.cc$",
944 r"^chrome[\\\/]browser[\\\/]ui[\\\/]startup[\\\/]"
945 r"startup_browser_creator\.cc$",
946 r"^chrome[\\\/]installer[\\\/]setup[\\\/].*",
947 r"chrome[\\\/]browser[\\\/]diagnostics[\\\/]" +
948 r"diagnostics_writer\.cc$",
949 r"^chrome_elf[\\\/]dll_hash[\\\/]dll_hash_main\.cc$",
950 r"^chromecast[\\\/]",
951 r"^cloud_print[\\\/]",
952 r"^content[\\\/]common[\\\/]gpu[\\\/]client[\\\/]"
953 r"gl_helper_benchmark\.cc$",
954 r"^courgette[\\\/]courgette_tool\.cc$",
955 r"^extensions[\\\/]renderer[\\\/]logging_native_handler\.cc$",
956 r"^ipc[\\\/]ipc_logging\.cc$",
957 r"^native_client_sdk[\\\/]",
958 r"^remoting[\\\/]base[\\\/]logging\.h$",
959 r"^remoting[\\\/]host[\\\/].*",
960 r"^sandbox[\\\/]linux[\\\/].*",
961 r"^tools[\\\/]",
962 r"^ui[\\\/]aura[\\\/]bench[\\\/]bench_main\.cc$",
963 r"^webkit[\\\/]browser[\\\/]fileapi[\\\/]" +
964 r"dump_file_system.cc$",))
965 source_file_filter = lambda x: input_api.FilterSourceFile(
966 x, white_list=(file_inclusion_pattern,), black_list=black_list)
968 log_info = []
969 printf = []
971 for f in input_api.AffectedSourceFiles(source_file_filter):
972 contents = input_api.ReadFile(f, 'rb')
973 if input_api.re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", contents):
974 log_info.append(f.LocalPath())
975 elif input_api.re.search(r"\bD?LOG_IF\s*\(\s*INFO\s*,", contents):
976 log_info.append(f.LocalPath())
978 if input_api.re.search(r"\bprintf\(", contents):
979 printf.append(f.LocalPath())
980 elif input_api.re.search(r"\bfprintf\((stdout|stderr)", contents):
981 printf.append(f.LocalPath())
983 if log_info:
984 return [output_api.PresubmitError(
985 'These files spam the console log with LOG(INFO):',
986 items=log_info)]
987 if printf:
988 return [output_api.PresubmitError(
989 'These files spam the console log with printf/fprintf:',
990 items=printf)]
991 return []
994 def _CheckForAnonymousVariables(input_api, output_api):
995 """These types are all expected to hold locks while in scope and
996 so should never be anonymous (which causes them to be immediately
997 destroyed)."""
998 they_who_must_be_named = [
999 'base::AutoLock',
1000 'base::AutoReset',
1001 'base::AutoUnlock',
1002 'SkAutoAlphaRestore',
1003 'SkAutoBitmapShaderInstall',
1004 'SkAutoBlitterChoose',
1005 'SkAutoBounderCommit',
1006 'SkAutoCallProc',
1007 'SkAutoCanvasRestore',
1008 'SkAutoCommentBlock',
1009 'SkAutoDescriptor',
1010 'SkAutoDisableDirectionCheck',
1011 'SkAutoDisableOvalCheck',
1012 'SkAutoFree',
1013 'SkAutoGlyphCache',
1014 'SkAutoHDC',
1015 'SkAutoLockColors',
1016 'SkAutoLockPixels',
1017 'SkAutoMalloc',
1018 'SkAutoMaskFreeImage',
1019 'SkAutoMutexAcquire',
1020 'SkAutoPathBoundsUpdate',
1021 'SkAutoPDFRelease',
1022 'SkAutoRasterClipValidate',
1023 'SkAutoRef',
1024 'SkAutoTime',
1025 'SkAutoTrace',
1026 'SkAutoUnref',
1028 anonymous = r'(%s)\s*[({]' % '|'.join(they_who_must_be_named)
1029 # bad: base::AutoLock(lock.get());
1030 # not bad: base::AutoLock lock(lock.get());
1031 bad_pattern = input_api.re.compile(anonymous)
1032 # good: new base::AutoLock(lock.get())
1033 good_pattern = input_api.re.compile(r'\bnew\s*' + anonymous)
1034 errors = []
1036 for f in input_api.AffectedFiles():
1037 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
1038 continue
1039 for linenum, line in f.ChangedContents():
1040 if bad_pattern.search(line) and not good_pattern.search(line):
1041 errors.append('%s:%d' % (f.LocalPath(), linenum))
1043 if errors:
1044 return [output_api.PresubmitError(
1045 'These lines create anonymous variables that need to be named:',
1046 items=errors)]
1047 return []
1050 def _CheckCygwinShell(input_api, output_api):
1051 source_file_filter = lambda x: input_api.FilterSourceFile(
1052 x, white_list=(r'.+\.(gyp|gypi)$',))
1053 cygwin_shell = []
1055 for f in input_api.AffectedSourceFiles(source_file_filter):
1056 for linenum, line in f.ChangedContents():
1057 if 'msvs_cygwin_shell' in line:
1058 cygwin_shell.append(f.LocalPath())
1059 break
1061 if cygwin_shell:
1062 return [output_api.PresubmitError(
1063 'These files should not use msvs_cygwin_shell (the default is 0):',
1064 items=cygwin_shell)]
1065 return []
1068 def _CheckUserActionUpdate(input_api, output_api):
1069 """Checks if any new user action has been added."""
1070 if any('actions.xml' == input_api.os_path.basename(f) for f in
1071 input_api.LocalPaths()):
1072 # If actions.xml is already included in the changelist, the PRESUBMIT
1073 # for actions.xml will do a more complete presubmit check.
1074 return []
1076 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm'))
1077 action_re = r'[^a-zA-Z]UserMetricsAction\("([^"]*)'
1078 current_actions = None
1079 for f in input_api.AffectedFiles(file_filter=file_filter):
1080 for line_num, line in f.ChangedContents():
1081 match = input_api.re.search(action_re, line)
1082 if match:
1083 # Loads contents in tools/metrics/actions/actions.xml to memory. It's
1084 # loaded only once.
1085 if not current_actions:
1086 with open('tools/metrics/actions/actions.xml') as actions_f:
1087 current_actions = actions_f.read()
1088 # Search for the matched user action name in |current_actions|.
1089 for action_name in match.groups():
1090 action = 'name="{0}"'.format(action_name)
1091 if action not in current_actions:
1092 return [output_api.PresubmitPromptWarning(
1093 'File %s line %d: %s is missing in '
1094 'tools/metrics/actions/actions.xml. Please run '
1095 'tools/metrics/actions/extract_actions.py to update.'
1096 % (f.LocalPath(), line_num, action_name))]
1097 return []
1100 def _GetJSONParseError(input_api, filename, eat_comments=True):
1101 try:
1102 contents = input_api.ReadFile(filename)
1103 if eat_comments:
1104 json_comment_eater = input_api.os_path.join(
1105 input_api.PresubmitLocalPath(),
1106 'tools', 'json_comment_eater', 'json_comment_eater.py')
1107 process = input_api.subprocess.Popen(
1108 [input_api.python_executable, json_comment_eater],
1109 stdin=input_api.subprocess.PIPE,
1110 stdout=input_api.subprocess.PIPE,
1111 universal_newlines=True)
1112 (contents, _) = process.communicate(input=contents)
1114 input_api.json.loads(contents)
1115 except ValueError as e:
1116 return e
1117 return None
1120 def _GetIDLParseError(input_api, filename):
1121 try:
1122 contents = input_api.ReadFile(filename)
1123 idl_schema = input_api.os_path.join(
1124 input_api.PresubmitLocalPath(),
1125 'tools', 'json_schema_compiler', 'idl_schema.py')
1126 process = input_api.subprocess.Popen(
1127 [input_api.python_executable, idl_schema],
1128 stdin=input_api.subprocess.PIPE,
1129 stdout=input_api.subprocess.PIPE,
1130 stderr=input_api.subprocess.PIPE,
1131 universal_newlines=True)
1132 (_, error) = process.communicate(input=contents)
1133 return error or None
1134 except ValueError as e:
1135 return e
1138 def _CheckParseErrors(input_api, output_api):
1139 """Check that IDL and JSON files do not contain syntax errors."""
1140 actions = {
1141 '.idl': _GetIDLParseError,
1142 '.json': _GetJSONParseError,
1144 # These paths contain test data and other known invalid JSON files.
1145 excluded_patterns = [
1146 r'test[\\\/]data[\\\/]',
1147 r'^components[\\\/]policy[\\\/]resources[\\\/]policy_templates\.json$',
1149 # Most JSON files are preprocessed and support comments, but these do not.
1150 json_no_comments_patterns = [
1151 r'^testing[\\\/]',
1153 # Only run IDL checker on files in these directories.
1154 idl_included_patterns = [
1155 r'^chrome[\\\/]common[\\\/]extensions[\\\/]api[\\\/]',
1156 r'^extensions[\\\/]common[\\\/]api[\\\/]',
1159 def get_action(affected_file):
1160 filename = affected_file.LocalPath()
1161 return actions.get(input_api.os_path.splitext(filename)[1])
1163 def MatchesFile(patterns, path):
1164 for pattern in patterns:
1165 if input_api.re.search(pattern, path):
1166 return True
1167 return False
1169 def FilterFile(affected_file):
1170 action = get_action(affected_file)
1171 if not action:
1172 return False
1173 path = affected_file.LocalPath()
1175 if MatchesFile(excluded_patterns, path):
1176 return False
1178 if (action == _GetIDLParseError and
1179 not MatchesFile(idl_included_patterns, path)):
1180 return False
1181 return True
1183 results = []
1184 for affected_file in input_api.AffectedFiles(
1185 file_filter=FilterFile, include_deletes=False):
1186 action = get_action(affected_file)
1187 kwargs = {}
1188 if (action == _GetJSONParseError and
1189 MatchesFile(json_no_comments_patterns, affected_file.LocalPath())):
1190 kwargs['eat_comments'] = False
1191 parse_error = action(input_api,
1192 affected_file.AbsoluteLocalPath(),
1193 **kwargs)
1194 if parse_error:
1195 results.append(output_api.PresubmitError('%s could not be parsed: %s' %
1196 (affected_file.LocalPath(), parse_error)))
1197 return results
1200 def _CheckJavaStyle(input_api, output_api):
1201 """Runs checkstyle on changed java files and returns errors if any exist."""
1202 import sys
1203 original_sys_path = sys.path
1204 try:
1205 sys.path = sys.path + [input_api.os_path.join(
1206 input_api.PresubmitLocalPath(), 'tools', 'android', 'checkstyle')]
1207 import checkstyle
1208 finally:
1209 # Restore sys.path to what it was before.
1210 sys.path = original_sys_path
1212 return checkstyle.RunCheckstyle(
1213 input_api, output_api, 'tools/android/checkstyle/chromium-style-5.0.xml')
1216 _DEPRECATED_CSS = [
1217 # Values
1218 ( "-webkit-box", "flex" ),
1219 ( "-webkit-inline-box", "inline-flex" ),
1220 ( "-webkit-flex", "flex" ),
1221 ( "-webkit-inline-flex", "inline-flex" ),
1222 ( "-webkit-min-content", "min-content" ),
1223 ( "-webkit-max-content", "max-content" ),
1225 # Properties
1226 ( "-webkit-background-clip", "background-clip" ),
1227 ( "-webkit-background-origin", "background-origin" ),
1228 ( "-webkit-background-size", "background-size" ),
1229 ( "-webkit-box-shadow", "box-shadow" ),
1231 # Functions
1232 ( "-webkit-gradient", "gradient" ),
1233 ( "-webkit-repeating-gradient", "repeating-gradient" ),
1234 ( "-webkit-linear-gradient", "linear-gradient" ),
1235 ( "-webkit-repeating-linear-gradient", "repeating-linear-gradient" ),
1236 ( "-webkit-radial-gradient", "radial-gradient" ),
1237 ( "-webkit-repeating-radial-gradient", "repeating-radial-gradient" ),
1240 def _CheckNoDeprecatedCSS(input_api, output_api):
1241 """ Make sure that we don't use deprecated CSS
1242 properties, functions or values. Our external
1243 documentation is ignored by the hooks as it
1244 needs to be consumed by WebKit. """
1245 results = []
1246 file_inclusion_pattern = (r".+\.css$",)
1247 black_list = (_EXCLUDED_PATHS +
1248 _TEST_CODE_EXCLUDED_PATHS +
1249 input_api.DEFAULT_BLACK_LIST +
1250 (r"^chrome/common/extensions/docs",
1251 r"^chrome/docs",
1252 r"^native_client_sdk"))
1253 file_filter = lambda f: input_api.FilterSourceFile(
1254 f, white_list=file_inclusion_pattern, black_list=black_list)
1255 for fpath in input_api.AffectedFiles(file_filter=file_filter):
1256 for line_num, line in fpath.ChangedContents():
1257 for (deprecated_value, value) in _DEPRECATED_CSS:
1258 if deprecated_value in line:
1259 results.append(output_api.PresubmitError(
1260 "%s:%d: Use of deprecated CSS %s, use %s instead" %
1261 (fpath.LocalPath(), line_num, deprecated_value, value)))
1262 return results
1265 _DEPRECATED_JS = [
1266 ( "__lookupGetter__", "Object.getOwnPropertyDescriptor" ),
1267 ( "__defineGetter__", "Object.defineProperty" ),
1268 ( "__defineSetter__", "Object.defineProperty" ),
1271 def _CheckNoDeprecatedJS(input_api, output_api):
1272 """Make sure that we don't use deprecated JS in Chrome code."""
1273 results = []
1274 file_inclusion_pattern = (r".+\.js$",) # TODO(dbeam): .html?
1275 black_list = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS +
1276 input_api.DEFAULT_BLACK_LIST)
1277 file_filter = lambda f: input_api.FilterSourceFile(
1278 f, white_list=file_inclusion_pattern, black_list=black_list)
1279 for fpath in input_api.AffectedFiles(file_filter=file_filter):
1280 for lnum, line in fpath.ChangedContents():
1281 for (deprecated, replacement) in _DEPRECATED_JS:
1282 if deprecated in line:
1283 results.append(output_api.PresubmitError(
1284 "%s:%d: Use of deprecated JS %s, use %s instead" %
1285 (fpath.LocalPath(), lnum, deprecated, replacement)))
1286 return results
1289 def _CommonChecks(input_api, output_api):
1290 """Checks common to both upload and commit."""
1291 results = []
1292 results.extend(input_api.canned_checks.PanProjectChecks(
1293 input_api, output_api,
1294 excluded_paths=_EXCLUDED_PATHS + _TESTRUNNER_PATHS))
1295 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
1296 results.extend(
1297 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
1298 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
1299 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
1300 results.extend(_CheckNoNewWStrings(input_api, output_api))
1301 results.extend(_CheckNoDEPSGIT(input_api, output_api))
1302 results.extend(_CheckNoBannedFunctions(input_api, output_api))
1303 results.extend(_CheckNoPragmaOnce(input_api, output_api))
1304 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
1305 results.extend(_CheckUnwantedDependencies(input_api, output_api))
1306 results.extend(_CheckFilePermissions(input_api, output_api))
1307 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
1308 results.extend(_CheckIncludeOrder(input_api, output_api))
1309 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
1310 results.extend(_CheckPatchFiles(input_api, output_api))
1311 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
1312 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
1313 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
1314 results.extend(_CheckForInvalidIfDefinedMacros(input_api, output_api))
1315 # TODO(danakj): Remove this when base/move.h is removed.
1316 results.extend(_CheckForUsingSideEffectsOfPass(input_api, output_api))
1317 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
1318 results.extend(
1319 input_api.canned_checks.CheckChangeHasNoTabs(
1320 input_api,
1321 output_api,
1322 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
1323 results.extend(_CheckSpamLogging(input_api, output_api))
1324 results.extend(_CheckForAnonymousVariables(input_api, output_api))
1325 results.extend(_CheckCygwinShell(input_api, output_api))
1326 results.extend(_CheckUserActionUpdate(input_api, output_api))
1327 results.extend(_CheckNoDeprecatedCSS(input_api, output_api))
1328 results.extend(_CheckNoDeprecatedJS(input_api, output_api))
1329 results.extend(_CheckParseErrors(input_api, output_api))
1330 results.extend(_CheckForIPCRules(input_api, output_api))
1332 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
1333 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
1334 input_api, output_api,
1335 input_api.PresubmitLocalPath(),
1336 whitelist=[r'^PRESUBMIT_test\.py$']))
1337 return results
1340 def _CheckAuthorizedAuthor(input_api, output_api):
1341 """For non-googler/chromites committers, verify the author's email address is
1342 in AUTHORS.
1344 # TODO(maruel): Add it to input_api?
1345 import fnmatch
1347 author = input_api.change.author_email
1348 if not author:
1349 input_api.logging.info('No author, skipping AUTHOR check')
1350 return []
1351 authors_path = input_api.os_path.join(
1352 input_api.PresubmitLocalPath(), 'AUTHORS')
1353 valid_authors = (
1354 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
1355 for line in open(authors_path))
1356 valid_authors = [item.group(1).lower() for item in valid_authors if item]
1357 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
1358 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
1359 return [output_api.PresubmitPromptWarning(
1360 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
1361 '\n'
1362 'http://www.chromium.org/developers/contributing-code and read the '
1363 '"Legal" section\n'
1364 'If you are a chromite, verify the contributor signed the CLA.') %
1365 author)]
1366 return []
1369 def _CheckPatchFiles(input_api, output_api):
1370 problems = [f.LocalPath() for f in input_api.AffectedFiles()
1371 if f.LocalPath().endswith(('.orig', '.rej'))]
1372 if problems:
1373 return [output_api.PresubmitError(
1374 "Don't commit .rej and .orig files.", problems)]
1375 else:
1376 return []
1379 def _DidYouMeanOSMacro(bad_macro):
1380 try:
1381 return {'A': 'OS_ANDROID',
1382 'B': 'OS_BSD',
1383 'C': 'OS_CHROMEOS',
1384 'F': 'OS_FREEBSD',
1385 'L': 'OS_LINUX',
1386 'M': 'OS_MACOSX',
1387 'N': 'OS_NACL',
1388 'O': 'OS_OPENBSD',
1389 'P': 'OS_POSIX',
1390 'S': 'OS_SOLARIS',
1391 'W': 'OS_WIN'}[bad_macro[3].upper()]
1392 except KeyError:
1393 return ''
1396 def _CheckForInvalidOSMacrosInFile(input_api, f):
1397 """Check for sensible looking, totally invalid OS macros."""
1398 preprocessor_statement = input_api.re.compile(r'^\s*#')
1399 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
1400 results = []
1401 for lnum, line in f.ChangedContents():
1402 if preprocessor_statement.search(line):
1403 for match in os_macro.finditer(line):
1404 if not match.group(1) in _VALID_OS_MACROS:
1405 good = _DidYouMeanOSMacro(match.group(1))
1406 did_you_mean = ' (did you mean %s?)' % good if good else ''
1407 results.append(' %s:%d %s%s' % (f.LocalPath(),
1408 lnum,
1409 match.group(1),
1410 did_you_mean))
1411 return results
1414 def _CheckForInvalidOSMacros(input_api, output_api):
1415 """Check all affected files for invalid OS macros."""
1416 bad_macros = []
1417 for f in input_api.AffectedFiles():
1418 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
1419 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
1421 if not bad_macros:
1422 return []
1424 return [output_api.PresubmitError(
1425 'Possibly invalid OS macro[s] found. Please fix your code\n'
1426 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
1429 def _CheckForInvalidIfDefinedMacrosInFile(input_api, f):
1430 """Check all affected files for invalid "if defined" macros."""
1431 ALWAYS_DEFINED_MACROS = (
1432 "TARGET_CPU_PPC",
1433 "TARGET_CPU_PPC64",
1434 "TARGET_CPU_68K",
1435 "TARGET_CPU_X86",
1436 "TARGET_CPU_ARM",
1437 "TARGET_CPU_MIPS",
1438 "TARGET_CPU_SPARC",
1439 "TARGET_CPU_ALPHA",
1440 "TARGET_IPHONE_SIMULATOR",
1441 "TARGET_OS_EMBEDDED",
1442 "TARGET_OS_IPHONE",
1443 "TARGET_OS_MAC",
1444 "TARGET_OS_UNIX",
1445 "TARGET_OS_WIN32",
1447 ifdef_macro = input_api.re.compile(r'^\s*#.*(?:ifdef\s|defined\()([^\s\)]+)')
1448 results = []
1449 for lnum, line in f.ChangedContents():
1450 for match in ifdef_macro.finditer(line):
1451 if match.group(1) in ALWAYS_DEFINED_MACROS:
1452 always_defined = ' %s is always defined. ' % match.group(1)
1453 did_you_mean = 'Did you mean \'#if %s\'?' % match.group(1)
1454 results.append(' %s:%d %s\n\t%s' % (f.LocalPath(),
1455 lnum,
1456 always_defined,
1457 did_you_mean))
1458 return results
1461 def _CheckForInvalidIfDefinedMacros(input_api, output_api):
1462 """Check all affected files for invalid "if defined" macros."""
1463 bad_macros = []
1464 for f in input_api.AffectedFiles():
1465 if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
1466 bad_macros.extend(_CheckForInvalidIfDefinedMacrosInFile(input_api, f))
1468 if not bad_macros:
1469 return []
1471 return [output_api.PresubmitError(
1472 'Found ifdef check on always-defined macro[s]. Please fix your code\n'
1473 'or check the list of ALWAYS_DEFINED_MACROS in src/PRESUBMIT.py.',
1474 bad_macros)]
1477 def _CheckForUsingSideEffectsOfPass(input_api, output_api):
1478 """Check all affected files for using side effects of Pass."""
1479 errors = []
1480 for f in input_api.AffectedFiles():
1481 if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
1482 for lnum, line in f.ChangedContents():
1483 # Disallow Foo(*my_scoped_thing.Pass()); See crbug.com/418297.
1484 if input_api.re.search(r'\*[a-zA-Z0-9_]+\.Pass\(\)', line):
1485 errors.append(output_api.PresubmitError(
1486 ('%s:%d uses *foo.Pass() to delete the contents of scoped_ptr. ' +
1487 'See crbug.com/418297.') % (f.LocalPath(), lnum)))
1488 return errors
1491 def _CheckForIPCRules(input_api, output_api):
1492 """Check for same IPC rules described in
1493 http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc
1495 base_pattern = r'IPC_ENUM_TRAITS\('
1496 inclusion_pattern = input_api.re.compile(r'(%s)' % base_pattern)
1497 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_pattern)
1499 problems = []
1500 for f in input_api.AffectedSourceFiles(None):
1501 local_path = f.LocalPath()
1502 if not local_path.endswith('.h'):
1503 continue
1504 for line_number, line in f.ChangedContents():
1505 if inclusion_pattern.search(line) and not comment_pattern.search(line):
1506 problems.append(
1507 '%s:%d\n %s' % (local_path, line_number, line.strip()))
1509 if problems:
1510 return [output_api.PresubmitPromptWarning(
1511 _IPC_ENUM_TRAITS_DEPRECATED, problems)]
1512 else:
1513 return []
1516 def CheckChangeOnUpload(input_api, output_api):
1517 results = []
1518 results.extend(_CommonChecks(input_api, output_api))
1519 results.extend(_CheckValidHostsInDEPS(input_api, output_api))
1520 results.extend(_CheckJavaStyle(input_api, output_api))
1521 return results
1524 def GetTryServerMasterForBot(bot):
1525 """Returns the Try Server master for the given bot.
1527 It tries to guess the master from the bot name, but may still fail
1528 and return None. There is no longer a default master.
1530 # Potentially ambiguous bot names are listed explicitly.
1531 master_map = {
1532 'linux_gpu': 'tryserver.chromium.gpu',
1533 'mac_gpu': 'tryserver.chromium.gpu',
1534 'win_gpu': 'tryserver.chromium.gpu',
1535 'chromium_presubmit': 'tryserver.chromium.linux',
1536 'blink_presubmit': 'tryserver.chromium.linux',
1537 'tools_build_presubmit': 'tryserver.chromium.linux',
1539 master = master_map.get(bot)
1540 if not master:
1541 if 'gpu' in bot:
1542 master = 'tryserver.chromium.gpu'
1543 elif 'linux' in bot or 'android' in bot or 'presubmit' in bot:
1544 master = 'tryserver.chromium.linux'
1545 elif 'win' in bot:
1546 master = 'tryserver.chromium.win'
1547 elif 'mac' in bot or 'ios' in bot:
1548 master = 'tryserver.chromium.mac'
1549 return master
1552 def GetDefaultTryConfigs(bots=None):
1553 """Returns a list of ('bot', set(['tests']), optionally filtered by [bots].
1555 To add tests to this list, they MUST be in the the corresponding master's
1556 gatekeeper config. For example, anything on master.chromium would be closed by
1557 tools/build/masters/master.chromium/master_gatekeeper_cfg.py.
1559 If 'bots' is specified, will only return configurations for bots in that list.
1562 standard_tests = [
1563 'base_unittests',
1564 'browser_tests',
1565 'cacheinvalidation_unittests',
1566 'check_deps',
1567 'check_deps2git',
1568 'content_browsertests',
1569 'content_unittests',
1570 'crypto_unittests',
1571 'gpu_unittests',
1572 'interactive_ui_tests',
1573 'ipc_tests',
1574 'jingle_unittests',
1575 'media_unittests',
1576 'net_unittests',
1577 'ppapi_unittests',
1578 'printing_unittests',
1579 'sql_unittests',
1580 'sync_unit_tests',
1581 'unit_tests',
1582 # Broken in release.
1583 #'url_unittests',
1584 #'webkit_unit_tests',
1587 builders_and_tests = {
1588 # TODO(maruel): Figure out a way to run 'sizes' where people can
1589 # effectively update the perf expectation correctly. This requires a
1590 # clobber=True build running 'sizes'. 'sizes' is not accurate with
1591 # incremental build. Reference:
1592 # http://chromium.org/developers/tree-sheriffs/perf-sheriffs.
1593 # TODO(maruel): An option would be to run 'sizes' but not count a failure
1594 # of this step as a try job failure.
1595 'android_aosp': ['compile'],
1596 'android_arm64_dbg_recipe': ['slave_steps'],
1597 'android_chromium_gn_compile_dbg': ['compile'],
1598 'android_chromium_gn_compile_rel': ['compile'],
1599 'android_clang_dbg': ['slave_steps'],
1600 'android_clang_dbg_recipe': ['slave_steps'],
1601 'android_dbg_tests_recipe': ['slave_steps'],
1602 'cros_x86': ['defaulttests'],
1603 'ios_dbg_simulator': [
1604 'compile',
1605 'base_unittests',
1606 'content_unittests',
1607 'crypto_unittests',
1608 'url_unittests',
1609 'net_unittests',
1610 'sql_unittests',
1611 'ui_base_unittests',
1612 'ui_unittests',
1614 'ios_rel_device': ['compile'],
1615 'ios_rel_device_ninja': ['compile'],
1616 'mac_asan': ['compile'],
1617 #TODO(stip): Change the name of this builder to reflect that it's release.
1618 'linux_gtk': standard_tests,
1619 'linux_chromeos_asan': ['compile'],
1620 'linux_chromium_chromeos_clang_dbg': ['defaulttests'],
1621 'linux_chromium_chromeos_rel': ['defaulttests'],
1622 'linux_chromium_compile_dbg': ['defaulttests'],
1623 'linux_chromium_gn_dbg': ['compile'],
1624 'linux_chromium_gn_rel': ['defaulttests'],
1625 'linux_chromium_rel': ['defaulttests'],
1626 'linux_chromium_rel_ng': ['defaulttests'],
1627 'linux_chromium_clang_dbg': ['defaulttests'],
1628 'linux_gpu': ['defaulttests'],
1629 'linux_nacl_sdk_build': ['compile'],
1630 'mac_chromium_compile_dbg': ['defaulttests'],
1631 'mac_chromium_rel': ['defaulttests'],
1632 'mac_chromium_rel_ng': ['defaulttests'],
1633 'mac_gpu': ['defaulttests'],
1634 'mac_nacl_sdk_build': ['compile'],
1635 'win_chromium_compile_dbg': ['defaulttests'],
1636 'win_chromium_dbg': ['defaulttests'],
1637 'win_chromium_rel': ['defaulttests'],
1638 'win_chromium_rel_ng': ['defaulttests'],
1639 'win_chromium_x64_rel': ['defaulttests'],
1640 'win_chromium_x64_rel_ng': ['defaulttests'],
1641 'win_gpu': ['defaulttests'],
1642 'win_nacl_sdk_build': ['compile'],
1643 'win8_chromium_rel': ['defaulttests'],
1646 if bots:
1647 filtered_builders_and_tests = dict((bot, set(builders_and_tests[bot]))
1648 for bot in bots)
1649 else:
1650 filtered_builders_and_tests = dict(
1651 (bot, set(tests))
1652 for bot, tests in builders_and_tests.iteritems())
1654 # Build up the mapping from tryserver master to bot/test.
1655 out = dict()
1656 for bot, tests in filtered_builders_and_tests.iteritems():
1657 out.setdefault(GetTryServerMasterForBot(bot), {})[bot] = tests
1658 return out
1661 def CheckChangeOnCommit(input_api, output_api):
1662 results = []
1663 results.extend(_CommonChecks(input_api, output_api))
1664 # TODO(thestig) temporarily disabled, doesn't work in third_party/
1665 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
1666 # input_api, output_api, sources))
1667 # Make sure the tree is 'open'.
1668 results.extend(input_api.canned_checks.CheckTreeIsOpen(
1669 input_api,
1670 output_api,
1671 json_url='http://chromium-status.appspot.com/current?format=json'))
1673 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1674 input_api, output_api))
1675 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1676 input_api, output_api))
1677 return results
1680 def GetPreferredTryMasters(project, change):
1681 import re
1682 files = change.LocalPaths()
1684 if not files or all(re.search(r'[\\\/]OWNERS$', f) for f in files):
1685 return {}
1687 if all(re.search(r'\.(m|mm)$|(^|[\\\/_])mac[\\\/_.]', f) for f in files):
1688 return GetDefaultTryConfigs([
1689 'mac_chromium_compile_dbg',
1690 'mac_chromium_rel_ng',
1692 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
1693 return GetDefaultTryConfigs([
1694 'win_chromium_dbg',
1695 'win_chromium_rel_ng',
1696 'win8_chromium_rel',
1698 if all(re.search(r'(^|[\\\/_])android[\\\/_.]', f) for f in files):
1699 return GetDefaultTryConfigs([
1700 'android_aosp',
1701 'android_clang_dbg',
1702 'android_dbg_tests_recipe',
1704 if all(re.search(r'[\\\/_]ios[\\\/_.]', f) for f in files):
1705 return GetDefaultTryConfigs(['ios_rel_device', 'ios_dbg_simulator'])
1707 builders = [
1708 'android_arm64_dbg_recipe',
1709 'android_chromium_gn_compile_rel',
1710 'android_chromium_gn_compile_dbg',
1711 'android_clang_dbg',
1712 'android_clang_dbg_recipe',
1713 'android_dbg_tests_recipe',
1714 'ios_dbg_simulator',
1715 'ios_rel_device',
1716 'ios_rel_device_ninja',
1717 'linux_chromium_chromeos_rel',
1718 'linux_chromium_gn_dbg',
1719 'linux_chromium_gn_rel',
1720 'linux_chromium_rel_ng',
1721 'linux_gpu',
1722 'mac_chromium_compile_dbg',
1723 'mac_chromium_rel_ng',
1724 'mac_gpu',
1725 'win_chromium_compile_dbg',
1726 'win_chromium_rel_ng',
1727 'win_chromium_x64_rel_ng',
1728 'win_gpu',
1729 'win8_chromium_rel',
1732 # Match things like path/aura/file.cc and path/file_aura.cc.
1733 # Same for chromeos.
1734 if any(re.search(r'[\\\/_](aura|chromeos)', f) for f in files):
1735 builders.extend([
1736 'linux_chromeos_asan',
1739 # If there are gyp changes to base, build, or chromeos, run a full cros build
1740 # in addition to the shorter linux_chromeos build. Changes to high level gyp
1741 # files have a much higher chance of breaking the cros build, which is
1742 # differnt from the linux_chromeos build that most chrome developers test
1743 # with.
1744 if any(re.search('^(base|build|chromeos).*\.gypi?$', f) for f in files):
1745 builders.extend(['cros_x86'])
1747 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1748 # unless they're .gyp(i) files as changes to those files can break the gyp
1749 # step on that bot.
1750 if (not all(re.search('^chrome', f) for f in files) or
1751 any(re.search('\.gypi?$', f) for f in files)):
1752 builders.extend(['android_aosp'])
1754 return GetDefaultTryConfigs(builders)