Mac Video Capture: Avoid enumerating Blackmagic devices via AVFoundation
[chromium-blink-merge.git] / PRESUBMIT.py
blobee872e269c047ec2b64fafdb93537f3d7bc0149f
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'))):
365 continue
367 allowWString = False
368 for line_num, line in f.ChangedContents():
369 if 'presubmit: allow wstring' in line:
370 allowWString = True
371 elif not allowWString and 'wstring' in line:
372 problems.append(' %s:%d' % (f.LocalPath(), line_num))
373 allowWString = False
374 else:
375 allowWString = False
377 if not problems:
378 return []
379 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
380 ' If you are calling a cross-platform API that accepts a wstring, '
381 'fix the API.\n' +
382 '\n'.join(problems))]
385 def _CheckNoDEPSGIT(input_api, output_api):
386 """Make sure .DEPS.git is never modified manually."""
387 if any(f.LocalPath().endswith('.DEPS.git') for f in
388 input_api.AffectedFiles()):
389 return [output_api.PresubmitError(
390 'Never commit changes to .DEPS.git. This file is maintained by an\n'
391 'automated system based on what\'s in DEPS and your changes will be\n'
392 'overwritten.\n'
393 'See https://sites.google.com/a/chromium.org/dev/developers/how-tos/get-the-code#Rolling_DEPS\n'
394 'for more information')]
395 return []
398 def _CheckValidHostsInDEPS(input_api, output_api):
399 """Checks that DEPS file deps are from allowed_hosts."""
400 # Run only if DEPS file has been modified to annoy fewer bystanders.
401 if all(f.LocalPath() != 'DEPS' for f in input_api.AffectedFiles()):
402 return []
403 # Outsource work to gclient verify
404 try:
405 input_api.subprocess.check_output(['gclient', 'verify'])
406 return []
407 except input_api.subprocess.CalledProcessError, error:
408 return [output_api.PresubmitError(
409 'DEPS file must have only git dependencies.',
410 long_text=error.output)]
413 def _CheckNoBannedFunctions(input_api, output_api):
414 """Make sure that banned functions are not used."""
415 warnings = []
416 errors = []
418 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
419 for f in input_api.AffectedFiles(file_filter=file_filter):
420 for line_num, line in f.ChangedContents():
421 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
422 matched = False
423 if func_name[0:1] == '/':
424 regex = func_name[1:]
425 if input_api.re.search(regex, line):
426 matched = True
427 elif func_name in line:
428 matched = True
429 if matched:
430 problems = warnings;
431 if error:
432 problems = errors;
433 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
434 for message_line in message:
435 problems.append(' %s' % message_line)
437 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
438 for f in input_api.AffectedFiles(file_filter=file_filter):
439 for line_num, line in f.ChangedContents():
440 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
441 def IsBlacklisted(affected_file, blacklist):
442 local_path = affected_file.LocalPath()
443 for item in blacklist:
444 if input_api.re.match(item, local_path):
445 return True
446 return False
447 if IsBlacklisted(f, excluded_paths):
448 continue
449 matched = False
450 if func_name[0:1] == '/':
451 regex = func_name[1:]
452 if input_api.re.search(regex, line):
453 matched = True
454 elif func_name in line:
455 matched = True
456 if matched:
457 problems = warnings;
458 if error:
459 problems = errors;
460 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
461 for message_line in message:
462 problems.append(' %s' % message_line)
464 result = []
465 if (warnings):
466 result.append(output_api.PresubmitPromptWarning(
467 'Banned functions were used.\n' + '\n'.join(warnings)))
468 if (errors):
469 result.append(output_api.PresubmitError(
470 'Banned functions were used.\n' + '\n'.join(errors)))
471 return result
474 def _CheckNoPragmaOnce(input_api, output_api):
475 """Make sure that banned functions are not used."""
476 files = []
477 pattern = input_api.re.compile(r'^#pragma\s+once',
478 input_api.re.MULTILINE)
479 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
480 if not f.LocalPath().endswith('.h'):
481 continue
482 contents = input_api.ReadFile(f)
483 if pattern.search(contents):
484 files.append(f)
486 if files:
487 return [output_api.PresubmitError(
488 'Do not use #pragma once in header files.\n'
489 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
490 files)]
491 return []
494 def _CheckNoTrinaryTrueFalse(input_api, output_api):
495 """Checks to make sure we don't introduce use of foo ? true : false."""
496 problems = []
497 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
498 for f in input_api.AffectedFiles():
499 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
500 continue
502 for line_num, line in f.ChangedContents():
503 if pattern.match(line):
504 problems.append(' %s:%d' % (f.LocalPath(), line_num))
506 if not problems:
507 return []
508 return [output_api.PresubmitPromptWarning(
509 'Please consider avoiding the "? true : false" pattern if possible.\n' +
510 '\n'.join(problems))]
513 def _CheckUnwantedDependencies(input_api, output_api):
514 """Runs checkdeps on #include statements added in this
515 change. Breaking - rules is an error, breaking ! rules is a
516 warning.
518 import sys
519 # We need to wait until we have an input_api object and use this
520 # roundabout construct to import checkdeps because this file is
521 # eval-ed and thus doesn't have __file__.
522 original_sys_path = sys.path
523 try:
524 sys.path = sys.path + [input_api.os_path.join(
525 input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
526 import checkdeps
527 from cpp_checker import CppChecker
528 from rules import Rule
529 finally:
530 # Restore sys.path to what it was before.
531 sys.path = original_sys_path
533 added_includes = []
534 for f in input_api.AffectedFiles():
535 if not CppChecker.IsCppFile(f.LocalPath()):
536 continue
538 changed_lines = [line for line_num, line in f.ChangedContents()]
539 added_includes.append([f.LocalPath(), changed_lines])
541 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
543 error_descriptions = []
544 warning_descriptions = []
545 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
546 added_includes):
547 description_with_path = '%s\n %s' % (path, rule_description)
548 if rule_type == Rule.DISALLOW:
549 error_descriptions.append(description_with_path)
550 else:
551 warning_descriptions.append(description_with_path)
553 results = []
554 if error_descriptions:
555 results.append(output_api.PresubmitError(
556 'You added one or more #includes that violate checkdeps rules.',
557 error_descriptions))
558 if warning_descriptions:
559 results.append(output_api.PresubmitPromptOrNotify(
560 'You added one or more #includes of files that are temporarily\n'
561 'allowed but being removed. Can you avoid introducing the\n'
562 '#include? See relevant DEPS file(s) for details and contacts.',
563 warning_descriptions))
564 return results
567 def _CheckFilePermissions(input_api, output_api):
568 """Check that all files have their permissions properly set."""
569 if input_api.platform == 'win32':
570 return []
571 args = [input_api.python_executable, 'tools/checkperms/checkperms.py',
572 '--root', input_api.change.RepositoryRoot()]
573 for f in input_api.AffectedFiles():
574 args += ['--file', f.LocalPath()]
575 checkperms = input_api.subprocess.Popen(args,
576 stdout=input_api.subprocess.PIPE)
577 errors = checkperms.communicate()[0].strip()
578 if errors:
579 return [output_api.PresubmitError('checkperms.py failed.',
580 errors.splitlines())]
581 return []
584 def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
585 """Makes sure we don't include ui/aura/window_property.h
586 in header files.
588 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
589 errors = []
590 for f in input_api.AffectedFiles():
591 if not f.LocalPath().endswith('.h'):
592 continue
593 for line_num, line in f.ChangedContents():
594 if pattern.match(line):
595 errors.append(' %s:%d' % (f.LocalPath(), line_num))
597 results = []
598 if errors:
599 results.append(output_api.PresubmitError(
600 'Header files should not include ui/aura/window_property.h', errors))
601 return results
604 def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
605 """Checks that the lines in scope occur in the right order.
607 1. C system files in alphabetical order
608 2. C++ system files in alphabetical order
609 3. Project's .h files
612 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
613 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
614 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
616 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
618 state = C_SYSTEM_INCLUDES
620 previous_line = ''
621 previous_line_num = 0
622 problem_linenums = []
623 for line_num, line in scope:
624 if c_system_include_pattern.match(line):
625 if state != C_SYSTEM_INCLUDES:
626 problem_linenums.append((line_num, previous_line_num))
627 elif previous_line and previous_line > line:
628 problem_linenums.append((line_num, previous_line_num))
629 elif cpp_system_include_pattern.match(line):
630 if state == C_SYSTEM_INCLUDES:
631 state = CPP_SYSTEM_INCLUDES
632 elif state == CUSTOM_INCLUDES:
633 problem_linenums.append((line_num, previous_line_num))
634 elif previous_line and previous_line > line:
635 problem_linenums.append((line_num, previous_line_num))
636 elif custom_include_pattern.match(line):
637 if state != CUSTOM_INCLUDES:
638 state = CUSTOM_INCLUDES
639 elif previous_line and previous_line > line:
640 problem_linenums.append((line_num, previous_line_num))
641 else:
642 problem_linenums.append(line_num)
643 previous_line = line
644 previous_line_num = line_num
646 warnings = []
647 for (line_num, previous_line_num) in problem_linenums:
648 if line_num in changed_linenums or previous_line_num in changed_linenums:
649 warnings.append(' %s:%d' % (file_path, line_num))
650 return warnings
653 def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
654 """Checks the #include order for the given file f."""
656 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
657 # Exclude the following includes from the check:
658 # 1) #include <.../...>, e.g., <sys/...> includes often need to appear in a
659 # specific order.
660 # 2) <atlbase.h>, "build/build_config.h"
661 excluded_include_pattern = input_api.re.compile(
662 r'\s*#include (\<.*/.*|\<atlbase\.h\>|"build/build_config.h")')
663 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
664 # Match the final or penultimate token if it is xxxtest so we can ignore it
665 # when considering the special first include.
666 test_file_tag_pattern = input_api.re.compile(
667 r'_[a-z]+test(?=(_[a-zA-Z0-9]+)?\.)')
668 if_pattern = input_api.re.compile(
669 r'\s*#\s*(if|elif|else|endif|define|undef).*')
670 # Some files need specialized order of includes; exclude such files from this
671 # check.
672 uncheckable_includes_pattern = input_api.re.compile(
673 r'\s*#include '
674 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
676 contents = f.NewContents()
677 warnings = []
678 line_num = 0
680 # Handle the special first include. If the first include file is
681 # some/path/file.h, the corresponding including file can be some/path/file.cc,
682 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
683 # etc. It's also possible that no special first include exists.
684 # If the included file is some/path/file_platform.h the including file could
685 # also be some/path/file_xxxtest_platform.h.
686 including_file_base_name = test_file_tag_pattern.sub(
687 '', input_api.os_path.basename(f.LocalPath()))
689 for line in contents:
690 line_num += 1
691 if system_include_pattern.match(line):
692 # No special first include -> process the line again along with normal
693 # includes.
694 line_num -= 1
695 break
696 match = custom_include_pattern.match(line)
697 if match:
698 match_dict = match.groupdict()
699 header_basename = test_file_tag_pattern.sub(
700 '', input_api.os_path.basename(match_dict['FILE'])).replace('.h', '')
702 if header_basename not in including_file_base_name:
703 # No special first include -> process the line again along with normal
704 # includes.
705 line_num -= 1
706 break
708 # Split into scopes: Each region between #if and #endif is its own scope.
709 scopes = []
710 current_scope = []
711 for line in contents[line_num:]:
712 line_num += 1
713 if uncheckable_includes_pattern.match(line):
714 continue
715 if if_pattern.match(line):
716 scopes.append(current_scope)
717 current_scope = []
718 elif ((system_include_pattern.match(line) or
719 custom_include_pattern.match(line)) and
720 not excluded_include_pattern.match(line)):
721 current_scope.append((line_num, line))
722 scopes.append(current_scope)
724 for scope in scopes:
725 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
726 changed_linenums))
727 return warnings
730 def _CheckIncludeOrder(input_api, output_api):
731 """Checks that the #include order is correct.
733 1. The corresponding header for source files.
734 2. C system files in alphabetical order
735 3. C++ system files in alphabetical order
736 4. Project's .h files in alphabetical order
738 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
739 these rules separately.
741 def FileFilterIncludeOrder(affected_file):
742 black_list = (_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST)
743 return input_api.FilterSourceFile(affected_file, black_list=black_list)
745 warnings = []
746 for f in input_api.AffectedFiles(file_filter=FileFilterIncludeOrder):
747 if f.LocalPath().endswith(('.cc', '.h')):
748 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
749 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
751 results = []
752 if warnings:
753 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
754 warnings))
755 return results
758 def _CheckForVersionControlConflictsInFile(input_api, f):
759 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
760 errors = []
761 for line_num, line in f.ChangedContents():
762 if pattern.match(line):
763 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
764 return errors
767 def _CheckForVersionControlConflicts(input_api, output_api):
768 """Usually this is not intentional and will cause a compile failure."""
769 errors = []
770 for f in input_api.AffectedFiles():
771 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
773 results = []
774 if errors:
775 results.append(output_api.PresubmitError(
776 'Version control conflict markers found, please resolve.', errors))
777 return results
780 def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
781 def FilterFile(affected_file):
782 """Filter function for use with input_api.AffectedSourceFiles,
783 below. This filters out everything except non-test files from
784 top-level directories that generally speaking should not hard-code
785 service URLs (e.g. src/android_webview/, src/content/ and others).
787 return input_api.FilterSourceFile(
788 affected_file,
789 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
790 black_list=(_EXCLUDED_PATHS +
791 _TEST_CODE_EXCLUDED_PATHS +
792 input_api.DEFAULT_BLACK_LIST))
794 base_pattern = '"[^"]*google\.com[^"]*"'
795 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
796 pattern = input_api.re.compile(base_pattern)
797 problems = [] # items are (filename, line_number, line)
798 for f in input_api.AffectedSourceFiles(FilterFile):
799 for line_num, line in f.ChangedContents():
800 if not comment_pattern.search(line) and pattern.search(line):
801 problems.append((f.LocalPath(), line_num, line))
803 if problems:
804 return [output_api.PresubmitPromptOrNotify(
805 'Most layers below src/chrome/ should not hardcode service URLs.\n'
806 'Are you sure this is correct?',
807 [' %s:%d: %s' % (
808 problem[0], problem[1], problem[2]) for problem in problems])]
809 else:
810 return []
813 def _CheckNoAbbreviationInPngFileName(input_api, output_api):
814 """Makes sure there are no abbreviations in the name of PNG files.
816 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
817 errors = []
818 for f in input_api.AffectedFiles(include_deletes=False):
819 if pattern.match(f.LocalPath()):
820 errors.append(' %s' % f.LocalPath())
822 results = []
823 if errors:
824 results.append(output_api.PresubmitError(
825 'The name of PNG files should not have abbreviations. \n'
826 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
827 'Contact oshima@chromium.org if you have questions.', errors))
828 return results
831 def _FilesToCheckForIncomingDeps(re, changed_lines):
832 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
833 a set of DEPS entries that we should look up.
835 For a directory (rather than a specific filename) we fake a path to
836 a specific filename by adding /DEPS. This is chosen as a file that
837 will seldom or never be subject to per-file include_rules.
839 # We ignore deps entries on auto-generated directories.
840 AUTO_GENERATED_DIRS = ['grit', 'jni']
842 # This pattern grabs the path without basename in the first
843 # parentheses, and the basename (if present) in the second. It
844 # relies on the simple heuristic that if there is a basename it will
845 # be a header file ending in ".h".
846 pattern = re.compile(
847 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
848 results = set()
849 for changed_line in changed_lines:
850 m = pattern.match(changed_line)
851 if m:
852 path = m.group(1)
853 if path.split('/')[0] not in AUTO_GENERATED_DIRS:
854 if m.group(2):
855 results.add('%s%s' % (path, m.group(2)))
856 else:
857 results.add('%s/DEPS' % path)
858 return results
861 def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
862 """When a dependency prefixed with + is added to a DEPS file, we
863 want to make sure that the change is reviewed by an OWNER of the
864 target file or directory, to avoid layering violations from being
865 introduced. This check verifies that this happens.
867 changed_lines = set()
868 for f in input_api.AffectedFiles():
869 filename = input_api.os_path.basename(f.LocalPath())
870 if filename == 'DEPS':
871 changed_lines |= set(line.strip()
872 for line_num, line
873 in f.ChangedContents())
874 if not changed_lines:
875 return []
877 virtual_depended_on_files = _FilesToCheckForIncomingDeps(input_api.re,
878 changed_lines)
879 if not virtual_depended_on_files:
880 return []
882 if input_api.is_committing:
883 if input_api.tbr:
884 return [output_api.PresubmitNotifyResult(
885 '--tbr was specified, skipping OWNERS check for DEPS additions')]
886 if not input_api.change.issue:
887 return [output_api.PresubmitError(
888 "DEPS approval by OWNERS check failed: this change has "
889 "no Rietveld issue number, so we can't check it for approvals.")]
890 output = output_api.PresubmitError
891 else:
892 output = output_api.PresubmitNotifyResult
894 owners_db = input_api.owners_db
895 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
896 input_api,
897 owners_db.email_regexp,
898 approval_needed=input_api.is_committing)
900 owner_email = owner_email or input_api.change.author_email
902 reviewers_plus_owner = set(reviewers)
903 if owner_email:
904 reviewers_plus_owner.add(owner_email)
905 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
906 reviewers_plus_owner)
908 # We strip the /DEPS part that was added by
909 # _FilesToCheckForIncomingDeps to fake a path to a file in a
910 # directory.
911 def StripDeps(path):
912 start_deps = path.rfind('/DEPS')
913 if start_deps != -1:
914 return path[:start_deps]
915 else:
916 return path
917 unapproved_dependencies = ["'+%s'," % StripDeps(path)
918 for path in missing_files]
920 if unapproved_dependencies:
921 output_list = [
922 output('Missing LGTM from OWNERS of dependencies added to DEPS:\n %s' %
923 '\n '.join(sorted(unapproved_dependencies)))]
924 if not input_api.is_committing:
925 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
926 output_list.append(output(
927 'Suggested missing target path OWNERS:\n %s' %
928 '\n '.join(suggested_owners or [])))
929 return output_list
931 return []
934 def _CheckSpamLogging(input_api, output_api):
935 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
936 black_list = (_EXCLUDED_PATHS +
937 _TEST_CODE_EXCLUDED_PATHS +
938 input_api.DEFAULT_BLACK_LIST +
939 (r"^base[\\\/]logging\.h$",
940 r"^base[\\\/]logging\.cc$",
941 r"^chrome[\\\/]app[\\\/]chrome_main_delegate\.cc$",
942 r"^chrome[\\\/]browser[\\\/]chrome_browser_main\.cc$",
943 r"^chrome[\\\/]browser[\\\/]ui[\\\/]startup[\\\/]"
944 r"startup_browser_creator\.cc$",
945 r"^chrome[\\\/]installer[\\\/]setup[\\\/].*",
946 r"chrome[\\\/]browser[\\\/]diagnostics[\\\/]" +
947 r"diagnostics_writer\.cc$",
948 r"^chrome_elf[\\\/]dll_hash[\\\/]dll_hash_main\.cc$",
949 r"^chromecast[\\\/]",
950 r"^cloud_print[\\\/]",
951 r"^content[\\\/]common[\\\/]gpu[\\\/]client[\\\/]"
952 r"gl_helper_benchmark\.cc$",
953 r"^courgette[\\\/]courgette_tool\.cc$",
954 r"^extensions[\\\/]renderer[\\\/]logging_native_handler\.cc$",
955 r"^native_client_sdk[\\\/]",
956 r"^remoting[\\\/]base[\\\/]logging\.h$",
957 r"^remoting[\\\/]host[\\\/].*",
958 r"^sandbox[\\\/]linux[\\\/].*",
959 r"^tools[\\\/]",
960 r"^ui[\\\/]aura[\\\/]bench[\\\/]bench_main\.cc$",
961 r"^webkit[\\\/]browser[\\\/]fileapi[\\\/]" +
962 r"dump_file_system.cc$",))
963 source_file_filter = lambda x: input_api.FilterSourceFile(
964 x, white_list=(file_inclusion_pattern,), black_list=black_list)
966 log_info = []
967 printf = []
969 for f in input_api.AffectedSourceFiles(source_file_filter):
970 contents = input_api.ReadFile(f, 'rb')
971 if input_api.re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", contents):
972 log_info.append(f.LocalPath())
973 elif input_api.re.search(r"\bD?LOG_IF\s*\(\s*INFO\s*,", contents):
974 log_info.append(f.LocalPath())
976 if input_api.re.search(r"\bprintf\(", contents):
977 printf.append(f.LocalPath())
978 elif input_api.re.search(r"\bfprintf\((stdout|stderr)", contents):
979 printf.append(f.LocalPath())
981 if log_info:
982 return [output_api.PresubmitError(
983 'These files spam the console log with LOG(INFO):',
984 items=log_info)]
985 if printf:
986 return [output_api.PresubmitError(
987 'These files spam the console log with printf/fprintf:',
988 items=printf)]
989 return []
992 def _CheckForAnonymousVariables(input_api, output_api):
993 """These types are all expected to hold locks while in scope and
994 so should never be anonymous (which causes them to be immediately
995 destroyed)."""
996 they_who_must_be_named = [
997 'base::AutoLock',
998 'base::AutoReset',
999 'base::AutoUnlock',
1000 'SkAutoAlphaRestore',
1001 'SkAutoBitmapShaderInstall',
1002 'SkAutoBlitterChoose',
1003 'SkAutoBounderCommit',
1004 'SkAutoCallProc',
1005 'SkAutoCanvasRestore',
1006 'SkAutoCommentBlock',
1007 'SkAutoDescriptor',
1008 'SkAutoDisableDirectionCheck',
1009 'SkAutoDisableOvalCheck',
1010 'SkAutoFree',
1011 'SkAutoGlyphCache',
1012 'SkAutoHDC',
1013 'SkAutoLockColors',
1014 'SkAutoLockPixels',
1015 'SkAutoMalloc',
1016 'SkAutoMaskFreeImage',
1017 'SkAutoMutexAcquire',
1018 'SkAutoPathBoundsUpdate',
1019 'SkAutoPDFRelease',
1020 'SkAutoRasterClipValidate',
1021 'SkAutoRef',
1022 'SkAutoTime',
1023 'SkAutoTrace',
1024 'SkAutoUnref',
1026 anonymous = r'(%s)\s*[({]' % '|'.join(they_who_must_be_named)
1027 # bad: base::AutoLock(lock.get());
1028 # not bad: base::AutoLock lock(lock.get());
1029 bad_pattern = input_api.re.compile(anonymous)
1030 # good: new base::AutoLock(lock.get())
1031 good_pattern = input_api.re.compile(r'\bnew\s*' + anonymous)
1032 errors = []
1034 for f in input_api.AffectedFiles():
1035 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
1036 continue
1037 for linenum, line in f.ChangedContents():
1038 if bad_pattern.search(line) and not good_pattern.search(line):
1039 errors.append('%s:%d' % (f.LocalPath(), linenum))
1041 if errors:
1042 return [output_api.PresubmitError(
1043 'These lines create anonymous variables that need to be named:',
1044 items=errors)]
1045 return []
1048 def _CheckCygwinShell(input_api, output_api):
1049 source_file_filter = lambda x: input_api.FilterSourceFile(
1050 x, white_list=(r'.+\.(gyp|gypi)$',))
1051 cygwin_shell = []
1053 for f in input_api.AffectedSourceFiles(source_file_filter):
1054 for linenum, line in f.ChangedContents():
1055 if 'msvs_cygwin_shell' in line:
1056 cygwin_shell.append(f.LocalPath())
1057 break
1059 if cygwin_shell:
1060 return [output_api.PresubmitError(
1061 'These files should not use msvs_cygwin_shell (the default is 0):',
1062 items=cygwin_shell)]
1063 return []
1066 def _CheckUserActionUpdate(input_api, output_api):
1067 """Checks if any new user action has been added."""
1068 if any('actions.xml' == input_api.os_path.basename(f) for f in
1069 input_api.LocalPaths()):
1070 # If actions.xml is already included in the changelist, the PRESUBMIT
1071 # for actions.xml will do a more complete presubmit check.
1072 return []
1074 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm'))
1075 action_re = r'[^a-zA-Z]UserMetricsAction\("([^"]*)'
1076 current_actions = None
1077 for f in input_api.AffectedFiles(file_filter=file_filter):
1078 for line_num, line in f.ChangedContents():
1079 match = input_api.re.search(action_re, line)
1080 if match:
1081 # Loads contents in tools/metrics/actions/actions.xml to memory. It's
1082 # loaded only once.
1083 if not current_actions:
1084 with open('tools/metrics/actions/actions.xml') as actions_f:
1085 current_actions = actions_f.read()
1086 # Search for the matched user action name in |current_actions|.
1087 for action_name in match.groups():
1088 action = 'name="{0}"'.format(action_name)
1089 if action not in current_actions:
1090 return [output_api.PresubmitPromptWarning(
1091 'File %s line %d: %s is missing in '
1092 'tools/metrics/actions/actions.xml. Please run '
1093 'tools/metrics/actions/extract_actions.py to update.'
1094 % (f.LocalPath(), line_num, action_name))]
1095 return []
1098 def _GetJSONParseError(input_api, filename, eat_comments=True):
1099 try:
1100 contents = input_api.ReadFile(filename)
1101 if eat_comments:
1102 json_comment_eater = input_api.os_path.join(
1103 input_api.PresubmitLocalPath(),
1104 'tools', 'json_comment_eater', 'json_comment_eater.py')
1105 process = input_api.subprocess.Popen(
1106 [input_api.python_executable, json_comment_eater],
1107 stdin=input_api.subprocess.PIPE,
1108 stdout=input_api.subprocess.PIPE,
1109 universal_newlines=True)
1110 (contents, _) = process.communicate(input=contents)
1112 input_api.json.loads(contents)
1113 except ValueError as e:
1114 return e
1115 return None
1118 def _GetIDLParseError(input_api, filename):
1119 try:
1120 contents = input_api.ReadFile(filename)
1121 idl_schema = input_api.os_path.join(
1122 input_api.PresubmitLocalPath(),
1123 'tools', 'json_schema_compiler', 'idl_schema.py')
1124 process = input_api.subprocess.Popen(
1125 [input_api.python_executable, idl_schema],
1126 stdin=input_api.subprocess.PIPE,
1127 stdout=input_api.subprocess.PIPE,
1128 stderr=input_api.subprocess.PIPE,
1129 universal_newlines=True)
1130 (_, error) = process.communicate(input=contents)
1131 return error or None
1132 except ValueError as e:
1133 return e
1136 def _CheckParseErrors(input_api, output_api):
1137 """Check that IDL and JSON files do not contain syntax errors."""
1138 actions = {
1139 '.idl': _GetIDLParseError,
1140 '.json': _GetJSONParseError,
1142 # These paths contain test data and other known invalid JSON files.
1143 excluded_patterns = [
1144 r'test[\\\/]data[\\\/]',
1145 r'^components[\\\/]policy[\\\/]resources[\\\/]policy_templates\.json$',
1147 # Most JSON files are preprocessed and support comments, but these do not.
1148 json_no_comments_patterns = [
1149 r'^testing[\\\/]',
1151 # Only run IDL checker on files in these directories.
1152 idl_included_patterns = [
1153 r'^chrome[\\\/]common[\\\/]extensions[\\\/]api[\\\/]',
1154 r'^extensions[\\\/]common[\\\/]api[\\\/]',
1157 def get_action(affected_file):
1158 filename = affected_file.LocalPath()
1159 return actions.get(input_api.os_path.splitext(filename)[1])
1161 def MatchesFile(patterns, path):
1162 for pattern in patterns:
1163 if input_api.re.search(pattern, path):
1164 return True
1165 return False
1167 def FilterFile(affected_file):
1168 action = get_action(affected_file)
1169 if not action:
1170 return False
1171 path = affected_file.LocalPath()
1173 if MatchesFile(excluded_patterns, path):
1174 return False
1176 if (action == _GetIDLParseError and
1177 not MatchesFile(idl_included_patterns, path)):
1178 return False
1179 return True
1181 results = []
1182 for affected_file in input_api.AffectedFiles(
1183 file_filter=FilterFile, include_deletes=False):
1184 action = get_action(affected_file)
1185 kwargs = {}
1186 if (action == _GetJSONParseError and
1187 MatchesFile(json_no_comments_patterns, affected_file.LocalPath())):
1188 kwargs['eat_comments'] = False
1189 parse_error = action(input_api,
1190 affected_file.AbsoluteLocalPath(),
1191 **kwargs)
1192 if parse_error:
1193 results.append(output_api.PresubmitError('%s could not be parsed: %s' %
1194 (affected_file.LocalPath(), parse_error)))
1195 return results
1198 def _CheckJavaStyle(input_api, output_api):
1199 """Runs checkstyle on changed java files and returns errors if any exist."""
1200 import sys
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
1210 return checkstyle.RunCheckstyle(
1211 input_api, output_api, 'tools/android/checkstyle/chromium-style-5.0.xml')
1214 _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" ),
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" ),
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" ),
1238 def _CheckNoDeprecatedCSS(input_api, output_api):
1239 """ Make sure that we don't use deprecated CSS
1240 properties, functions or values. Our external
1241 documentation is ignored by the hooks as it
1242 needs to be consumed by WebKit. """
1243 results = []
1244 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)
1253 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 deprecated_value in 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
1263 _DEPRECATED_JS = [
1264 ( "__lookupGetter__", "Object.getOwnPropertyDescriptor" ),
1265 ( "__defineGetter__", "Object.defineProperty" ),
1266 ( "__defineSetter__", "Object.defineProperty" ),
1269 def _CheckNoDeprecatedJS(input_api, output_api):
1270 """Make sure that we don't use deprecated JS in Chrome code."""
1271 results = []
1272 file_inclusion_pattern = (r".+\.js$",) # TODO(dbeam): .html?
1273 black_list = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS +
1274 input_api.DEFAULT_BLACK_LIST)
1275 file_filter = lambda f: input_api.FilterSourceFile(
1276 f, white_list=file_inclusion_pattern, black_list=black_list)
1277 for fpath in input_api.AffectedFiles(file_filter=file_filter):
1278 for lnum, line in fpath.ChangedContents():
1279 for (deprecated, replacement) in _DEPRECATED_JS:
1280 if deprecated in line:
1281 results.append(output_api.PresubmitError(
1282 "%s:%d: Use of deprecated JS %s, use %s instead" %
1283 (fpath.LocalPath(), lnum, deprecated, replacement)))
1284 return results
1287 def _CommonChecks(input_api, output_api):
1288 """Checks common to both upload and commit."""
1289 results = []
1290 results.extend(input_api.canned_checks.PanProjectChecks(
1291 input_api, output_api,
1292 excluded_paths=_EXCLUDED_PATHS + _TESTRUNNER_PATHS))
1293 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
1294 results.extend(
1295 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
1296 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
1297 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
1298 results.extend(_CheckNoNewWStrings(input_api, output_api))
1299 results.extend(_CheckNoDEPSGIT(input_api, output_api))
1300 results.extend(_CheckNoBannedFunctions(input_api, output_api))
1301 results.extend(_CheckNoPragmaOnce(input_api, output_api))
1302 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
1303 results.extend(_CheckUnwantedDependencies(input_api, output_api))
1304 results.extend(_CheckFilePermissions(input_api, output_api))
1305 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
1306 results.extend(_CheckIncludeOrder(input_api, output_api))
1307 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
1308 results.extend(_CheckPatchFiles(input_api, output_api))
1309 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
1310 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
1311 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
1312 results.extend(_CheckForInvalidIfDefinedMacros(input_api, output_api))
1313 # TODO(danakj): Remove this when base/move.h is removed.
1314 results.extend(_CheckForUsingSideEffectsOfPass(input_api, output_api))
1315 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
1316 results.extend(
1317 input_api.canned_checks.CheckChangeHasNoTabs(
1318 input_api,
1319 output_api,
1320 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
1321 results.extend(_CheckSpamLogging(input_api, output_api))
1322 results.extend(_CheckForAnonymousVariables(input_api, output_api))
1323 results.extend(_CheckCygwinShell(input_api, output_api))
1324 results.extend(_CheckUserActionUpdate(input_api, output_api))
1325 results.extend(_CheckNoDeprecatedCSS(input_api, output_api))
1326 results.extend(_CheckNoDeprecatedJS(input_api, output_api))
1327 results.extend(_CheckParseErrors(input_api, output_api))
1328 results.extend(_CheckForIPCRules(input_api, output_api))
1330 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
1331 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
1332 input_api, output_api,
1333 input_api.PresubmitLocalPath(),
1334 whitelist=[r'^PRESUBMIT_test\.py$']))
1335 return results
1338 def _CheckAuthorizedAuthor(input_api, output_api):
1339 """For non-googler/chromites committers, verify the author's email address is
1340 in AUTHORS.
1342 # TODO(maruel): Add it to input_api?
1343 import fnmatch
1345 author = input_api.change.author_email
1346 if not author:
1347 input_api.logging.info('No author, skipping AUTHOR check')
1348 return []
1349 authors_path = input_api.os_path.join(
1350 input_api.PresubmitLocalPath(), 'AUTHORS')
1351 valid_authors = (
1352 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
1353 for line in open(authors_path))
1354 valid_authors = [item.group(1).lower() for item in valid_authors if item]
1355 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
1356 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
1357 return [output_api.PresubmitPromptWarning(
1358 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
1359 '\n'
1360 'http://www.chromium.org/developers/contributing-code and read the '
1361 '"Legal" section\n'
1362 'If you are a chromite, verify the contributor signed the CLA.') %
1363 author)]
1364 return []
1367 def _CheckPatchFiles(input_api, output_api):
1368 problems = [f.LocalPath() for f in input_api.AffectedFiles()
1369 if f.LocalPath().endswith(('.orig', '.rej'))]
1370 if problems:
1371 return [output_api.PresubmitError(
1372 "Don't commit .rej and .orig files.", problems)]
1373 else:
1374 return []
1377 def _DidYouMeanOSMacro(bad_macro):
1378 try:
1379 return {'A': 'OS_ANDROID',
1380 'B': 'OS_BSD',
1381 'C': 'OS_CHROMEOS',
1382 'F': 'OS_FREEBSD',
1383 'L': 'OS_LINUX',
1384 'M': 'OS_MACOSX',
1385 'N': 'OS_NACL',
1386 'O': 'OS_OPENBSD',
1387 'P': 'OS_POSIX',
1388 'S': 'OS_SOLARIS',
1389 'W': 'OS_WIN'}[bad_macro[3].upper()]
1390 except KeyError:
1391 return ''
1394 def _CheckForInvalidOSMacrosInFile(input_api, f):
1395 """Check for sensible looking, totally invalid OS macros."""
1396 preprocessor_statement = input_api.re.compile(r'^\s*#')
1397 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
1398 results = []
1399 for lnum, line in f.ChangedContents():
1400 if preprocessor_statement.search(line):
1401 for match in os_macro.finditer(line):
1402 if not match.group(1) in _VALID_OS_MACROS:
1403 good = _DidYouMeanOSMacro(match.group(1))
1404 did_you_mean = ' (did you mean %s?)' % good if good else ''
1405 results.append(' %s:%d %s%s' % (f.LocalPath(),
1406 lnum,
1407 match.group(1),
1408 did_you_mean))
1409 return results
1412 def _CheckForInvalidOSMacros(input_api, output_api):
1413 """Check all affected files for invalid OS macros."""
1414 bad_macros = []
1415 for f in input_api.AffectedFiles():
1416 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
1417 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
1419 if not bad_macros:
1420 return []
1422 return [output_api.PresubmitError(
1423 'Possibly invalid OS macro[s] found. Please fix your code\n'
1424 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
1427 def _CheckForInvalidIfDefinedMacrosInFile(input_api, f):
1428 """Check all affected files for invalid "if defined" macros."""
1429 ALWAYS_DEFINED_MACROS = (
1430 "TARGET_CPU_PPC",
1431 "TARGET_CPU_PPC64",
1432 "TARGET_CPU_68K",
1433 "TARGET_CPU_X86",
1434 "TARGET_CPU_ARM",
1435 "TARGET_CPU_MIPS",
1436 "TARGET_CPU_SPARC",
1437 "TARGET_CPU_ALPHA",
1438 "TARGET_IPHONE_SIMULATOR",
1439 "TARGET_OS_EMBEDDED",
1440 "TARGET_OS_IPHONE",
1441 "TARGET_OS_MAC",
1442 "TARGET_OS_UNIX",
1443 "TARGET_OS_WIN32",
1445 ifdef_macro = input_api.re.compile(r'^\s*#.*(?:ifdef\s|defined\()([^\s\)]+)')
1446 results = []
1447 for lnum, line in f.ChangedContents():
1448 for match in ifdef_macro.finditer(line):
1449 if match.group(1) in ALWAYS_DEFINED_MACROS:
1450 always_defined = ' %s is always defined. ' % match.group(1)
1451 did_you_mean = 'Did you mean \'#if %s\'?' % match.group(1)
1452 results.append(' %s:%d %s\n\t%s' % (f.LocalPath(),
1453 lnum,
1454 always_defined,
1455 did_you_mean))
1456 return results
1459 def _CheckForInvalidIfDefinedMacros(input_api, output_api):
1460 """Check all affected files for invalid "if defined" macros."""
1461 bad_macros = []
1462 for f in input_api.AffectedFiles():
1463 if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
1464 bad_macros.extend(_CheckForInvalidIfDefinedMacrosInFile(input_api, f))
1466 if not bad_macros:
1467 return []
1469 return [output_api.PresubmitError(
1470 'Found ifdef check on always-defined macro[s]. Please fix your code\n'
1471 'or check the list of ALWAYS_DEFINED_MACROS in src/PRESUBMIT.py.',
1472 bad_macros)]
1475 def _CheckForUsingSideEffectsOfPass(input_api, output_api):
1476 """Check all affected files for using side effects of Pass."""
1477 errors = []
1478 for f in input_api.AffectedFiles():
1479 if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
1480 for lnum, line in f.ChangedContents():
1481 # Disallow Foo(*my_scoped_thing.Pass()); See crbug.com/418297.
1482 if input_api.re.search(r'\*[a-zA-Z0-9_]+\.Pass\(\)', line):
1483 errors.append(output_api.PresubmitError(
1484 ('%s:%d uses *foo.Pass() to delete the contents of scoped_ptr. ' +
1485 'See crbug.com/418297.') % (f.LocalPath(), lnum)))
1486 return errors
1489 def _CheckForIPCRules(input_api, output_api):
1490 """Check for same IPC rules described in
1491 http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc
1493 base_pattern = r'IPC_ENUM_TRAITS\('
1494 inclusion_pattern = input_api.re.compile(r'(%s)' % base_pattern)
1495 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_pattern)
1497 problems = []
1498 for f in input_api.AffectedSourceFiles(None):
1499 local_path = f.LocalPath()
1500 if not local_path.endswith('.h'):
1501 continue
1502 for line_number, line in f.ChangedContents():
1503 if inclusion_pattern.search(line) and not comment_pattern.search(line):
1504 problems.append(
1505 '%s:%d\n %s' % (local_path, line_number, line.strip()))
1507 if problems:
1508 return [output_api.PresubmitPromptWarning(
1509 _IPC_ENUM_TRAITS_DEPRECATED, problems)]
1510 else:
1511 return []
1514 def CheckChangeOnUpload(input_api, output_api):
1515 results = []
1516 results.extend(_CommonChecks(input_api, output_api))
1517 results.extend(_CheckValidHostsInDEPS(input_api, output_api))
1518 results.extend(_CheckJavaStyle(input_api, output_api))
1519 return results
1522 def GetTryServerMasterForBot(bot):
1523 """Returns the Try Server master for the given bot.
1525 It tries to guess the master from the bot name, but may still fail
1526 and return None. There is no longer a default master.
1528 # Potentially ambiguous bot names are listed explicitly.
1529 master_map = {
1530 'linux_gpu': 'tryserver.chromium.gpu',
1531 'mac_gpu': 'tryserver.chromium.gpu',
1532 'win_gpu': 'tryserver.chromium.gpu',
1533 'chromium_presubmit': 'tryserver.chromium.linux',
1534 'blink_presubmit': 'tryserver.chromium.linux',
1535 'tools_build_presubmit': 'tryserver.chromium.linux',
1537 master = master_map.get(bot)
1538 if not master:
1539 if 'gpu' in bot:
1540 master = 'tryserver.chromium.gpu'
1541 elif 'linux' in bot or 'android' in bot or 'presubmit' in bot:
1542 master = 'tryserver.chromium.linux'
1543 elif 'win' in bot:
1544 master = 'tryserver.chromium.win'
1545 elif 'mac' in bot or 'ios' in bot:
1546 master = 'tryserver.chromium.mac'
1547 return master
1550 def GetDefaultTryConfigs(bots=None):
1551 """Returns a list of ('bot', set(['tests']), optionally filtered by [bots].
1553 To add tests to this list, they MUST be in the the corresponding master's
1554 gatekeeper config. For example, anything on master.chromium would be closed by
1555 tools/build/masters/master.chromium/master_gatekeeper_cfg.py.
1557 If 'bots' is specified, will only return configurations for bots in that list.
1560 standard_tests = [
1561 'base_unittests',
1562 'browser_tests',
1563 'cacheinvalidation_unittests',
1564 'check_deps',
1565 'check_deps2git',
1566 'content_browsertests',
1567 'content_unittests',
1568 'crypto_unittests',
1569 'gpu_unittests',
1570 'interactive_ui_tests',
1571 'ipc_tests',
1572 'jingle_unittests',
1573 'media_unittests',
1574 'net_unittests',
1575 'ppapi_unittests',
1576 'printing_unittests',
1577 'sql_unittests',
1578 'sync_unit_tests',
1579 'unit_tests',
1580 # Broken in release.
1581 #'url_unittests',
1582 #'webkit_unit_tests',
1585 builders_and_tests = {
1586 # TODO(maruel): Figure out a way to run 'sizes' where people can
1587 # effectively update the perf expectation correctly. This requires a
1588 # clobber=True build running 'sizes'. 'sizes' is not accurate with
1589 # incremental build. Reference:
1590 # http://chromium.org/developers/tree-sheriffs/perf-sheriffs.
1591 # TODO(maruel): An option would be to run 'sizes' but not count a failure
1592 # of this step as a try job failure.
1593 'android_aosp': ['compile'],
1594 'android_arm64_dbg_recipe': ['slave_steps'],
1595 'android_chromium_gn_compile_dbg': ['compile'],
1596 'android_chromium_gn_compile_rel': ['compile'],
1597 'android_clang_dbg': ['slave_steps'],
1598 'android_clang_dbg_recipe': ['slave_steps'],
1599 'android_dbg_tests_recipe': ['slave_steps'],
1600 'cros_x86': ['defaulttests'],
1601 'ios_dbg_simulator': [
1602 'compile',
1603 'base_unittests',
1604 'content_unittests',
1605 'crypto_unittests',
1606 'url_unittests',
1607 'net_unittests',
1608 'sql_unittests',
1609 'ui_base_unittests',
1610 'ui_unittests',
1612 'ios_rel_device': ['compile'],
1613 'ios_rel_device_ninja': ['compile'],
1614 'mac_asan': ['compile'],
1615 #TODO(stip): Change the name of this builder to reflect that it's release.
1616 'linux_gtk': standard_tests,
1617 'linux_chromeos_asan': ['compile'],
1618 'linux_chromium_chromeos_clang_dbg': ['defaulttests'],
1619 'linux_chromium_chromeos_rel': ['defaulttests'],
1620 'linux_chromium_compile_dbg': ['defaulttests'],
1621 'linux_chromium_gn_dbg': ['compile'],
1622 'linux_chromium_gn_rel': ['defaulttests'],
1623 'linux_chromium_rel': ['defaulttests'],
1624 'linux_chromium_rel_ng': ['defaulttests'],
1625 'linux_chromium_clang_dbg': ['defaulttests'],
1626 'linux_gpu': ['defaulttests'],
1627 'linux_nacl_sdk_build': ['compile'],
1628 'mac_chromium_compile_dbg': ['defaulttests'],
1629 'mac_chromium_rel': ['defaulttests'],
1630 'mac_chromium_rel_ng': ['defaulttests'],
1631 'mac_gpu': ['defaulttests'],
1632 'mac_nacl_sdk_build': ['compile'],
1633 'win_chromium_compile_dbg': ['defaulttests'],
1634 'win_chromium_dbg': ['defaulttests'],
1635 'win_chromium_rel': ['defaulttests'],
1636 'win_chromium_rel_ng': ['defaulttests'],
1637 'win_chromium_x64_rel': ['defaulttests'],
1638 'win_chromium_x64_rel_ng': ['defaulttests'],
1639 'win_gpu': ['defaulttests'],
1640 'win_nacl_sdk_build': ['compile'],
1641 'win8_chromium_rel': ['defaulttests'],
1644 if bots:
1645 filtered_builders_and_tests = dict((bot, set(builders_and_tests[bot]))
1646 for bot in bots)
1647 else:
1648 filtered_builders_and_tests = dict(
1649 (bot, set(tests))
1650 for bot, tests in builders_and_tests.iteritems())
1652 # Build up the mapping from tryserver master to bot/test.
1653 out = dict()
1654 for bot, tests in filtered_builders_and_tests.iteritems():
1655 out.setdefault(GetTryServerMasterForBot(bot), {})[bot] = tests
1656 return out
1659 def CheckChangeOnCommit(input_api, output_api):
1660 results = []
1661 results.extend(_CommonChecks(input_api, output_api))
1662 # TODO(thestig) temporarily disabled, doesn't work in third_party/
1663 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
1664 # input_api, output_api, sources))
1665 # Make sure the tree is 'open'.
1666 results.extend(input_api.canned_checks.CheckTreeIsOpen(
1667 input_api,
1668 output_api,
1669 json_url='http://chromium-status.appspot.com/current?format=json'))
1671 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1672 input_api, output_api))
1673 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1674 input_api, output_api))
1675 return results
1678 def GetPreferredTryMasters(project, change):
1679 import re
1680 files = change.LocalPaths()
1682 if not files or all(re.search(r'[\\\/]OWNERS$', f) for f in files):
1683 return {}
1685 if all(re.search(r'\.(m|mm)$|(^|[\\\/_])mac[\\\/_.]', f) for f in files):
1686 return GetDefaultTryConfigs([
1687 'mac_chromium_compile_dbg',
1688 'mac_chromium_rel_ng',
1690 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
1691 return GetDefaultTryConfigs([
1692 'win_chromium_dbg',
1693 'win_chromium_rel_ng',
1694 'win8_chromium_rel',
1696 if all(re.search(r'(^|[\\\/_])android[\\\/_.]', f) for f in files):
1697 return GetDefaultTryConfigs([
1698 'android_aosp',
1699 'android_clang_dbg',
1700 'android_dbg_tests_recipe',
1702 if all(re.search(r'[\\\/_]ios[\\\/_.]', f) for f in files):
1703 return GetDefaultTryConfigs(['ios_rel_device', 'ios_dbg_simulator'])
1705 builders = [
1706 'android_arm64_dbg_recipe',
1707 'android_chromium_gn_compile_rel',
1708 'android_chromium_gn_compile_dbg',
1709 'android_clang_dbg',
1710 'android_clang_dbg_recipe',
1711 'android_dbg_tests_recipe',
1712 'ios_dbg_simulator',
1713 'ios_rel_device',
1714 'ios_rel_device_ninja',
1715 'linux_chromium_chromeos_rel',
1716 'linux_chromium_clang_dbg',
1717 'linux_chromium_gn_dbg',
1718 'linux_chromium_gn_rel',
1719 'linux_chromium_rel_ng',
1720 'linux_gpu',
1721 'mac_chromium_compile_dbg',
1722 'mac_chromium_rel_ng',
1723 'mac_gpu',
1724 'win_chromium_compile_dbg',
1725 'win_chromium_rel_ng',
1726 'win_chromium_x64_rel_ng',
1727 'win_gpu',
1728 'win8_chromium_rel',
1731 # Match things like path/aura/file.cc and path/file_aura.cc.
1732 # Same for chromeos.
1733 if any(re.search(r'[\\\/_](aura|chromeos)', f) for f in files):
1734 builders.extend([
1735 'linux_chromeos_asan',
1736 'linux_chromium_chromeos_clang_dbg'
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)