Roll src/third_party/WebKit 7f284eb:3a94031 (svn 186005:186017)
[chromium-blink-merge.git] / PRESUBMIT.py
blob90d1df4e286eaad7dac54b35f082b83609c7b5bc
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"^chrome[\\\/]browser[\\\/]chromeos[\\\/]"
169 "customization_document_browsertest\.cc$",
170 r"^components[\\\/]crash[\\\/]app[\\\/]breakpad_mac\.mm$",
171 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_browser_main\.cc$",
172 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_message_filter\.cc$",
173 r"^mojo[\\\/]edk[\\\/]embedder[\\\/]" +
174 r"simple_platform_shared_buffer_posix\.cc$",
175 r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
176 r"^net[\\\/]url_request[\\\/]test_url_fetcher_factory\.cc$",
180 'SkRefPtr',
182 'The use of SkRefPtr is prohibited. ',
183 'Please use skia::RefPtr instead.'
185 True,
189 'SkAutoRef',
191 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
192 'Please use skia::RefPtr instead.'
194 True,
198 'SkAutoTUnref',
200 'The use of SkAutoTUnref is dangerous because it implicitly ',
201 'converts to a raw pointer. Please use skia::RefPtr instead.'
203 True,
207 'SkAutoUnref',
209 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
210 'because it implicitly converts to a raw pointer. ',
211 'Please use skia::RefPtr instead.'
213 True,
217 r'/HANDLE_EINTR\(.*close',
219 'HANDLE_EINTR(close) is invalid. If close fails with EINTR, the file',
220 'descriptor will be closed, and it is incorrect to retry the close.',
221 'Either call close directly and ignore its return value, or wrap close',
222 'in IGNORE_EINTR to use its return value. See http://crbug.com/269623'
224 True,
228 r'/IGNORE_EINTR\((?!.*close)',
230 'IGNORE_EINTR is only valid when wrapping close. To wrap other system',
231 'calls, use HANDLE_EINTR. See http://crbug.com/269623',
233 True,
235 # Files that #define IGNORE_EINTR.
236 r'^base[\\\/]posix[\\\/]eintr_wrapper\.h$',
237 r'^ppapi[\\\/]tests[\\\/]test_broker\.cc$',
241 r'/v8::Extension\(',
243 'Do not introduce new v8::Extensions into the code base, use',
244 'gin::Wrappable instead. See http://crbug.com/334679',
246 True,
248 r'extensions[\\\/]renderer[\\\/]safe_builtins\.*',
253 _IPC_ENUM_TRAITS_DEPRECATED = (
254 'You are using IPC_ENUM_TRAITS() in your code. It has been deprecated.\n'
255 'See http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc')
258 _VALID_OS_MACROS = (
259 # Please keep sorted.
260 'OS_ANDROID',
261 'OS_ANDROID_HOST',
262 'OS_BSD',
263 'OS_CAT', # For testing.
264 'OS_CHROMEOS',
265 'OS_FREEBSD',
266 'OS_IOS',
267 'OS_LINUX',
268 'OS_MACOSX',
269 'OS_NACL',
270 'OS_NACL_NONSFI',
271 'OS_NACL_SFI',
272 'OS_OPENBSD',
273 'OS_POSIX',
274 'OS_QNX',
275 'OS_SOLARIS',
276 'OS_WIN',
280 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
281 """Attempts to prevent use of functions intended only for testing in
282 non-testing code. For now this is just a best-effort implementation
283 that ignores header files and may have some false positives. A
284 better implementation would probably need a proper C++ parser.
286 # We only scan .cc files and the like, as the declaration of
287 # for-testing functions in header files are hard to distinguish from
288 # calls to such functions without a proper C++ parser.
289 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
291 base_function_pattern = r'[ :]test::[^\s]+|ForTest(ing)?|for_test(ing)?'
292 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
293 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern)
294 exclusion_pattern = input_api.re.compile(
295 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
296 base_function_pattern, base_function_pattern))
298 def FilterFile(affected_file):
299 black_list = (_EXCLUDED_PATHS +
300 _TEST_CODE_EXCLUDED_PATHS +
301 input_api.DEFAULT_BLACK_LIST)
302 return input_api.FilterSourceFile(
303 affected_file,
304 white_list=(file_inclusion_pattern, ),
305 black_list=black_list)
307 problems = []
308 for f in input_api.AffectedSourceFiles(FilterFile):
309 local_path = f.LocalPath()
310 for line_number, line in f.ChangedContents():
311 if (inclusion_pattern.search(line) and
312 not comment_pattern.search(line) and
313 not exclusion_pattern.search(line)):
314 problems.append(
315 '%s:%d\n %s' % (local_path, line_number, line.strip()))
317 if problems:
318 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
319 else:
320 return []
323 def _CheckNoIOStreamInHeaders(input_api, output_api):
324 """Checks to make sure no .h files include <iostream>."""
325 files = []
326 pattern = input_api.re.compile(r'^#include\s*<iostream>',
327 input_api.re.MULTILINE)
328 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
329 if not f.LocalPath().endswith('.h'):
330 continue
331 contents = input_api.ReadFile(f)
332 if pattern.search(contents):
333 files.append(f)
335 if len(files):
336 return [ output_api.PresubmitError(
337 'Do not #include <iostream> in header files, since it inserts static '
338 'initialization into every file including the header. Instead, '
339 '#include <ostream>. See http://crbug.com/94794',
340 files) ]
341 return []
344 def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
345 """Checks to make sure no source files use UNIT_TEST"""
346 problems = []
347 for f in input_api.AffectedFiles():
348 if (not f.LocalPath().endswith(('.cc', '.mm'))):
349 continue
351 for line_num, line in f.ChangedContents():
352 if 'UNIT_TEST ' in line or line.endswith('UNIT_TEST'):
353 problems.append(' %s:%d' % (f.LocalPath(), line_num))
355 if not problems:
356 return []
357 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
358 '\n'.join(problems))]
361 def _CheckNoNewWStrings(input_api, output_api):
362 """Checks to make sure we don't introduce use of wstrings."""
363 problems = []
364 for f in input_api.AffectedFiles():
365 if (not f.LocalPath().endswith(('.cc', '.h')) or
366 f.LocalPath().endswith(('test.cc', '_win.cc', '_win.h')) or
367 '/win/' in f.LocalPath()):
368 continue
370 allowWString = False
371 for line_num, line in f.ChangedContents():
372 if 'presubmit: allow wstring' in line:
373 allowWString = True
374 elif not allowWString and 'wstring' in line:
375 problems.append(' %s:%d' % (f.LocalPath(), line_num))
376 allowWString = False
377 else:
378 allowWString = False
380 if not problems:
381 return []
382 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
383 ' If you are calling a cross-platform API that accepts a wstring, '
384 'fix the API.\n' +
385 '\n'.join(problems))]
388 def _CheckNoDEPSGIT(input_api, output_api):
389 """Make sure .DEPS.git is never modified manually."""
390 if any(f.LocalPath().endswith('.DEPS.git') for f in
391 input_api.AffectedFiles()):
392 return [output_api.PresubmitError(
393 'Never commit changes to .DEPS.git. This file is maintained by an\n'
394 'automated system based on what\'s in DEPS and your changes will be\n'
395 'overwritten.\n'
396 'See https://sites.google.com/a/chromium.org/dev/developers/how-tos/get-the-code#Rolling_DEPS\n'
397 'for more information')]
398 return []
401 def _CheckValidHostsInDEPS(input_api, output_api):
402 """Checks that DEPS file deps are from allowed_hosts."""
403 # Run only if DEPS file has been modified to annoy fewer bystanders.
404 if all(f.LocalPath() != 'DEPS' for f in input_api.AffectedFiles()):
405 return []
406 # Outsource work to gclient verify
407 try:
408 input_api.subprocess.check_output(['gclient', 'verify'])
409 return []
410 except input_api.subprocess.CalledProcessError, error:
411 return [output_api.PresubmitError(
412 'DEPS file must have only git dependencies.',
413 long_text=error.output)]
416 def _CheckNoBannedFunctions(input_api, output_api):
417 """Make sure that banned functions are not used."""
418 warnings = []
419 errors = []
421 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
422 for f in input_api.AffectedFiles(file_filter=file_filter):
423 for line_num, line in f.ChangedContents():
424 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
425 matched = False
426 if func_name[0:1] == '/':
427 regex = func_name[1:]
428 if input_api.re.search(regex, line):
429 matched = True
430 elif func_name in line:
431 matched = True
432 if matched:
433 problems = warnings;
434 if error:
435 problems = errors;
436 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
437 for message_line in message:
438 problems.append(' %s' % message_line)
440 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
441 for f in input_api.AffectedFiles(file_filter=file_filter):
442 for line_num, line in f.ChangedContents():
443 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
444 def IsBlacklisted(affected_file, blacklist):
445 local_path = affected_file.LocalPath()
446 for item in blacklist:
447 if input_api.re.match(item, local_path):
448 return True
449 return False
450 if IsBlacklisted(f, excluded_paths):
451 continue
452 matched = False
453 if func_name[0:1] == '/':
454 regex = func_name[1:]
455 if input_api.re.search(regex, line):
456 matched = True
457 elif func_name in line:
458 matched = True
459 if matched:
460 problems = warnings;
461 if error:
462 problems = errors;
463 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
464 for message_line in message:
465 problems.append(' %s' % message_line)
467 result = []
468 if (warnings):
469 result.append(output_api.PresubmitPromptWarning(
470 'Banned functions were used.\n' + '\n'.join(warnings)))
471 if (errors):
472 result.append(output_api.PresubmitError(
473 'Banned functions were used.\n' + '\n'.join(errors)))
474 return result
477 def _CheckNoPragmaOnce(input_api, output_api):
478 """Make sure that banned functions are not used."""
479 files = []
480 pattern = input_api.re.compile(r'^#pragma\s+once',
481 input_api.re.MULTILINE)
482 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
483 if not f.LocalPath().endswith('.h'):
484 continue
485 contents = input_api.ReadFile(f)
486 if pattern.search(contents):
487 files.append(f)
489 if files:
490 return [output_api.PresubmitError(
491 'Do not use #pragma once in header files.\n'
492 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
493 files)]
494 return []
497 def _CheckNoTrinaryTrueFalse(input_api, output_api):
498 """Checks to make sure we don't introduce use of foo ? true : false."""
499 problems = []
500 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
501 for f in input_api.AffectedFiles():
502 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
503 continue
505 for line_num, line in f.ChangedContents():
506 if pattern.match(line):
507 problems.append(' %s:%d' % (f.LocalPath(), line_num))
509 if not problems:
510 return []
511 return [output_api.PresubmitPromptWarning(
512 'Please consider avoiding the "? true : false" pattern if possible.\n' +
513 '\n'.join(problems))]
516 def _CheckUnwantedDependencies(input_api, output_api):
517 """Runs checkdeps on #include statements added in this
518 change. Breaking - rules is an error, breaking ! rules is a
519 warning.
521 import sys
522 # We need to wait until we have an input_api object and use this
523 # roundabout construct to import checkdeps because this file is
524 # eval-ed and thus doesn't have __file__.
525 original_sys_path = sys.path
526 try:
527 sys.path = sys.path + [input_api.os_path.join(
528 input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
529 import checkdeps
530 from cpp_checker import CppChecker
531 from rules import Rule
532 finally:
533 # Restore sys.path to what it was before.
534 sys.path = original_sys_path
536 added_includes = []
537 for f in input_api.AffectedFiles():
538 if not CppChecker.IsCppFile(f.LocalPath()):
539 continue
541 changed_lines = [line for line_num, line in f.ChangedContents()]
542 added_includes.append([f.LocalPath(), changed_lines])
544 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
546 error_descriptions = []
547 warning_descriptions = []
548 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
549 added_includes):
550 description_with_path = '%s\n %s' % (path, rule_description)
551 if rule_type == Rule.DISALLOW:
552 error_descriptions.append(description_with_path)
553 else:
554 warning_descriptions.append(description_with_path)
556 results = []
557 if error_descriptions:
558 results.append(output_api.PresubmitError(
559 'You added one or more #includes that violate checkdeps rules.',
560 error_descriptions))
561 if warning_descriptions:
562 results.append(output_api.PresubmitPromptOrNotify(
563 'You added one or more #includes of files that are temporarily\n'
564 'allowed but being removed. Can you avoid introducing the\n'
565 '#include? See relevant DEPS file(s) for details and contacts.',
566 warning_descriptions))
567 return results
570 def _CheckFilePermissions(input_api, output_api):
571 """Check that all files have their permissions properly set."""
572 if input_api.platform == 'win32':
573 return []
574 args = [input_api.python_executable, 'tools/checkperms/checkperms.py',
575 '--root', input_api.change.RepositoryRoot()]
576 for f in input_api.AffectedFiles():
577 args += ['--file', f.LocalPath()]
578 checkperms = input_api.subprocess.Popen(args,
579 stdout=input_api.subprocess.PIPE)
580 errors = checkperms.communicate()[0].strip()
581 if errors:
582 return [output_api.PresubmitError('checkperms.py failed.',
583 errors.splitlines())]
584 return []
587 def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
588 """Makes sure we don't include ui/aura/window_property.h
589 in header files.
591 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
592 errors = []
593 for f in input_api.AffectedFiles():
594 if not f.LocalPath().endswith('.h'):
595 continue
596 for line_num, line in f.ChangedContents():
597 if pattern.match(line):
598 errors.append(' %s:%d' % (f.LocalPath(), line_num))
600 results = []
601 if errors:
602 results.append(output_api.PresubmitError(
603 'Header files should not include ui/aura/window_property.h', errors))
604 return results
607 def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
608 """Checks that the lines in scope occur in the right order.
610 1. C system files in alphabetical order
611 2. C++ system files in alphabetical order
612 3. Project's .h files
615 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
616 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
617 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
619 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
621 state = C_SYSTEM_INCLUDES
623 previous_line = ''
624 previous_line_num = 0
625 problem_linenums = []
626 for line_num, line in scope:
627 if c_system_include_pattern.match(line):
628 if state != C_SYSTEM_INCLUDES:
629 problem_linenums.append((line_num, previous_line_num))
630 elif previous_line and previous_line > line:
631 problem_linenums.append((line_num, previous_line_num))
632 elif cpp_system_include_pattern.match(line):
633 if state == C_SYSTEM_INCLUDES:
634 state = CPP_SYSTEM_INCLUDES
635 elif state == CUSTOM_INCLUDES:
636 problem_linenums.append((line_num, previous_line_num))
637 elif previous_line and previous_line > line:
638 problem_linenums.append((line_num, previous_line_num))
639 elif custom_include_pattern.match(line):
640 if state != CUSTOM_INCLUDES:
641 state = CUSTOM_INCLUDES
642 elif previous_line and previous_line > line:
643 problem_linenums.append((line_num, previous_line_num))
644 else:
645 problem_linenums.append(line_num)
646 previous_line = line
647 previous_line_num = line_num
649 warnings = []
650 for (line_num, previous_line_num) in problem_linenums:
651 if line_num in changed_linenums or previous_line_num in changed_linenums:
652 warnings.append(' %s:%d' % (file_path, line_num))
653 return warnings
656 def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
657 """Checks the #include order for the given file f."""
659 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
660 # Exclude the following includes from the check:
661 # 1) #include <.../...>, e.g., <sys/...> includes often need to appear in a
662 # specific order.
663 # 2) <atlbase.h>, "build/build_config.h"
664 excluded_include_pattern = input_api.re.compile(
665 r'\s*#include (\<.*/.*|\<atlbase\.h\>|"build/build_config.h")')
666 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
667 # Match the final or penultimate token if it is xxxtest so we can ignore it
668 # when considering the special first include.
669 test_file_tag_pattern = input_api.re.compile(
670 r'_[a-z]+test(?=(_[a-zA-Z0-9]+)?\.)')
671 if_pattern = input_api.re.compile(
672 r'\s*#\s*(if|elif|else|endif|define|undef).*')
673 # Some files need specialized order of includes; exclude such files from this
674 # check.
675 uncheckable_includes_pattern = input_api.re.compile(
676 r'\s*#include '
677 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
679 contents = f.NewContents()
680 warnings = []
681 line_num = 0
683 # Handle the special first include. If the first include file is
684 # some/path/file.h, the corresponding including file can be some/path/file.cc,
685 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
686 # etc. It's also possible that no special first include exists.
687 # If the included file is some/path/file_platform.h the including file could
688 # also be some/path/file_xxxtest_platform.h.
689 including_file_base_name = test_file_tag_pattern.sub(
690 '', input_api.os_path.basename(f.LocalPath()))
692 for line in contents:
693 line_num += 1
694 if system_include_pattern.match(line):
695 # No special first include -> process the line again along with normal
696 # includes.
697 line_num -= 1
698 break
699 match = custom_include_pattern.match(line)
700 if match:
701 match_dict = match.groupdict()
702 header_basename = test_file_tag_pattern.sub(
703 '', input_api.os_path.basename(match_dict['FILE'])).replace('.h', '')
705 if header_basename not in including_file_base_name:
706 # No special first include -> process the line again along with normal
707 # includes.
708 line_num -= 1
709 break
711 # Split into scopes: Each region between #if and #endif is its own scope.
712 scopes = []
713 current_scope = []
714 for line in contents[line_num:]:
715 line_num += 1
716 if uncheckable_includes_pattern.match(line):
717 continue
718 if if_pattern.match(line):
719 scopes.append(current_scope)
720 current_scope = []
721 elif ((system_include_pattern.match(line) or
722 custom_include_pattern.match(line)) and
723 not excluded_include_pattern.match(line)):
724 current_scope.append((line_num, line))
725 scopes.append(current_scope)
727 for scope in scopes:
728 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
729 changed_linenums))
730 return warnings
733 def _CheckIncludeOrder(input_api, output_api):
734 """Checks that the #include order is correct.
736 1. The corresponding header for source files.
737 2. C system files in alphabetical order
738 3. C++ system files in alphabetical order
739 4. Project's .h files in alphabetical order
741 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
742 these rules separately.
744 def FileFilterIncludeOrder(affected_file):
745 black_list = (_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST)
746 return input_api.FilterSourceFile(affected_file, black_list=black_list)
748 warnings = []
749 for f in input_api.AffectedFiles(file_filter=FileFilterIncludeOrder):
750 if f.LocalPath().endswith(('.cc', '.h')):
751 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
752 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
754 results = []
755 if warnings:
756 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
757 warnings))
758 return results
761 def _CheckForVersionControlConflictsInFile(input_api, f):
762 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
763 errors = []
764 for line_num, line in f.ChangedContents():
765 if pattern.match(line):
766 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
767 return errors
770 def _CheckForVersionControlConflicts(input_api, output_api):
771 """Usually this is not intentional and will cause a compile failure."""
772 errors = []
773 for f in input_api.AffectedFiles():
774 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
776 results = []
777 if errors:
778 results.append(output_api.PresubmitError(
779 'Version control conflict markers found, please resolve.', errors))
780 return results
783 def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
784 def FilterFile(affected_file):
785 """Filter function for use with input_api.AffectedSourceFiles,
786 below. This filters out everything except non-test files from
787 top-level directories that generally speaking should not hard-code
788 service URLs (e.g. src/android_webview/, src/content/ and others).
790 return input_api.FilterSourceFile(
791 affected_file,
792 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
793 black_list=(_EXCLUDED_PATHS +
794 _TEST_CODE_EXCLUDED_PATHS +
795 input_api.DEFAULT_BLACK_LIST))
797 base_pattern = '"[^"]*google\.com[^"]*"'
798 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
799 pattern = input_api.re.compile(base_pattern)
800 problems = [] # items are (filename, line_number, line)
801 for f in input_api.AffectedSourceFiles(FilterFile):
802 for line_num, line in f.ChangedContents():
803 if not comment_pattern.search(line) and pattern.search(line):
804 problems.append((f.LocalPath(), line_num, line))
806 if problems:
807 return [output_api.PresubmitPromptOrNotify(
808 'Most layers below src/chrome/ should not hardcode service URLs.\n'
809 'Are you sure this is correct?',
810 [' %s:%d: %s' % (
811 problem[0], problem[1], problem[2]) for problem in problems])]
812 else:
813 return []
816 def _CheckNoAbbreviationInPngFileName(input_api, output_api):
817 """Makes sure there are no abbreviations in the name of PNG files.
819 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
820 errors = []
821 for f in input_api.AffectedFiles(include_deletes=False):
822 if pattern.match(f.LocalPath()):
823 errors.append(' %s' % f.LocalPath())
825 results = []
826 if errors:
827 results.append(output_api.PresubmitError(
828 'The name of PNG files should not have abbreviations. \n'
829 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
830 'Contact oshima@chromium.org if you have questions.', errors))
831 return results
834 def _FilesToCheckForIncomingDeps(re, changed_lines):
835 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
836 a set of DEPS entries that we should look up.
838 For a directory (rather than a specific filename) we fake a path to
839 a specific filename by adding /DEPS. This is chosen as a file that
840 will seldom or never be subject to per-file include_rules.
842 # We ignore deps entries on auto-generated directories.
843 AUTO_GENERATED_DIRS = ['grit', 'jni']
845 # This pattern grabs the path without basename in the first
846 # parentheses, and the basename (if present) in the second. It
847 # relies on the simple heuristic that if there is a basename it will
848 # be a header file ending in ".h".
849 pattern = re.compile(
850 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
851 results = set()
852 for changed_line in changed_lines:
853 m = pattern.match(changed_line)
854 if m:
855 path = m.group(1)
856 if path.split('/')[0] not in AUTO_GENERATED_DIRS:
857 if m.group(2):
858 results.add('%s%s' % (path, m.group(2)))
859 else:
860 results.add('%s/DEPS' % path)
861 return results
864 def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
865 """When a dependency prefixed with + is added to a DEPS file, we
866 want to make sure that the change is reviewed by an OWNER of the
867 target file or directory, to avoid layering violations from being
868 introduced. This check verifies that this happens.
870 changed_lines = set()
871 for f in input_api.AffectedFiles():
872 filename = input_api.os_path.basename(f.LocalPath())
873 if filename == 'DEPS':
874 changed_lines |= set(line.strip()
875 for line_num, line
876 in f.ChangedContents())
877 if not changed_lines:
878 return []
880 virtual_depended_on_files = _FilesToCheckForIncomingDeps(input_api.re,
881 changed_lines)
882 if not virtual_depended_on_files:
883 return []
885 if input_api.is_committing:
886 if input_api.tbr:
887 return [output_api.PresubmitNotifyResult(
888 '--tbr was specified, skipping OWNERS check for DEPS additions')]
889 if not input_api.change.issue:
890 return [output_api.PresubmitError(
891 "DEPS approval by OWNERS check failed: this change has "
892 "no Rietveld issue number, so we can't check it for approvals.")]
893 output = output_api.PresubmitError
894 else:
895 output = output_api.PresubmitNotifyResult
897 owners_db = input_api.owners_db
898 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
899 input_api,
900 owners_db.email_regexp,
901 approval_needed=input_api.is_committing)
903 owner_email = owner_email or input_api.change.author_email
905 reviewers_plus_owner = set(reviewers)
906 if owner_email:
907 reviewers_plus_owner.add(owner_email)
908 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
909 reviewers_plus_owner)
911 # We strip the /DEPS part that was added by
912 # _FilesToCheckForIncomingDeps to fake a path to a file in a
913 # directory.
914 def StripDeps(path):
915 start_deps = path.rfind('/DEPS')
916 if start_deps != -1:
917 return path[:start_deps]
918 else:
919 return path
920 unapproved_dependencies = ["'+%s'," % StripDeps(path)
921 for path in missing_files]
923 if unapproved_dependencies:
924 output_list = [
925 output('Missing LGTM from OWNERS of dependencies added to DEPS:\n %s' %
926 '\n '.join(sorted(unapproved_dependencies)))]
927 if not input_api.is_committing:
928 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
929 output_list.append(output(
930 'Suggested missing target path OWNERS:\n %s' %
931 '\n '.join(suggested_owners or [])))
932 return output_list
934 return []
937 def _CheckSpamLogging(input_api, output_api):
938 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
939 black_list = (_EXCLUDED_PATHS +
940 _TEST_CODE_EXCLUDED_PATHS +
941 input_api.DEFAULT_BLACK_LIST +
942 (r"^base[\\\/]logging\.h$",
943 r"^base[\\\/]logging\.cc$",
944 r"^chrome[\\\/]app[\\\/]chrome_main_delegate\.cc$",
945 r"^chrome[\\\/]browser[\\\/]chrome_browser_main\.cc$",
946 r"^chrome[\\\/]browser[\\\/]ui[\\\/]startup[\\\/]"
947 r"startup_browser_creator\.cc$",
948 r"^chrome[\\\/]installer[\\\/]setup[\\\/].*",
949 r"chrome[\\\/]browser[\\\/]diagnostics[\\\/]" +
950 r"diagnostics_writer\.cc$",
951 r"^chrome_elf[\\\/]dll_hash[\\\/]dll_hash_main\.cc$",
952 r"^chromecast[\\\/]",
953 r"^cloud_print[\\\/]",
954 r"^content[\\\/]common[\\\/]gpu[\\\/]client[\\\/]"
955 r"gl_helper_benchmark\.cc$",
956 r"^courgette[\\\/]courgette_tool\.cc$",
957 r"^extensions[\\\/]renderer[\\\/]logging_native_handler\.cc$",
958 r"^ipc[\\\/]ipc_logging\.cc$",
959 r"^native_client_sdk[\\\/]",
960 r"^remoting[\\\/]base[\\\/]logging\.h$",
961 r"^remoting[\\\/]host[\\\/].*",
962 r"^sandbox[\\\/]linux[\\\/].*",
963 r"^tools[\\\/]",
964 r"^ui[\\\/]aura[\\\/]bench[\\\/]bench_main\.cc$",
965 r"^webkit[\\\/]browser[\\\/]fileapi[\\\/]" +
966 r"dump_file_system.cc$",))
967 source_file_filter = lambda x: input_api.FilterSourceFile(
968 x, white_list=(file_inclusion_pattern,), black_list=black_list)
970 log_info = []
971 printf = []
973 for f in input_api.AffectedSourceFiles(source_file_filter):
974 contents = input_api.ReadFile(f, 'rb')
975 if input_api.re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", contents):
976 log_info.append(f.LocalPath())
977 elif input_api.re.search(r"\bD?LOG_IF\s*\(\s*INFO\s*,", contents):
978 log_info.append(f.LocalPath())
980 if input_api.re.search(r"\bprintf\(", contents):
981 printf.append(f.LocalPath())
982 elif input_api.re.search(r"\bfprintf\((stdout|stderr)", contents):
983 printf.append(f.LocalPath())
985 if log_info:
986 return [output_api.PresubmitError(
987 'These files spam the console log with LOG(INFO):',
988 items=log_info)]
989 if printf:
990 return [output_api.PresubmitError(
991 'These files spam the console log with printf/fprintf:',
992 items=printf)]
993 return []
996 def _CheckForAnonymousVariables(input_api, output_api):
997 """These types are all expected to hold locks while in scope and
998 so should never be anonymous (which causes them to be immediately
999 destroyed)."""
1000 they_who_must_be_named = [
1001 'base::AutoLock',
1002 'base::AutoReset',
1003 'base::AutoUnlock',
1004 'SkAutoAlphaRestore',
1005 'SkAutoBitmapShaderInstall',
1006 'SkAutoBlitterChoose',
1007 'SkAutoBounderCommit',
1008 'SkAutoCallProc',
1009 'SkAutoCanvasRestore',
1010 'SkAutoCommentBlock',
1011 'SkAutoDescriptor',
1012 'SkAutoDisableDirectionCheck',
1013 'SkAutoDisableOvalCheck',
1014 'SkAutoFree',
1015 'SkAutoGlyphCache',
1016 'SkAutoHDC',
1017 'SkAutoLockColors',
1018 'SkAutoLockPixels',
1019 'SkAutoMalloc',
1020 'SkAutoMaskFreeImage',
1021 'SkAutoMutexAcquire',
1022 'SkAutoPathBoundsUpdate',
1023 'SkAutoPDFRelease',
1024 'SkAutoRasterClipValidate',
1025 'SkAutoRef',
1026 'SkAutoTime',
1027 'SkAutoTrace',
1028 'SkAutoUnref',
1030 anonymous = r'(%s)\s*[({]' % '|'.join(they_who_must_be_named)
1031 # bad: base::AutoLock(lock.get());
1032 # not bad: base::AutoLock lock(lock.get());
1033 bad_pattern = input_api.re.compile(anonymous)
1034 # good: new base::AutoLock(lock.get())
1035 good_pattern = input_api.re.compile(r'\bnew\s*' + anonymous)
1036 errors = []
1038 for f in input_api.AffectedFiles():
1039 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
1040 continue
1041 for linenum, line in f.ChangedContents():
1042 if bad_pattern.search(line) and not good_pattern.search(line):
1043 errors.append('%s:%d' % (f.LocalPath(), linenum))
1045 if errors:
1046 return [output_api.PresubmitError(
1047 'These lines create anonymous variables that need to be named:',
1048 items=errors)]
1049 return []
1052 def _CheckCygwinShell(input_api, output_api):
1053 source_file_filter = lambda x: input_api.FilterSourceFile(
1054 x, white_list=(r'.+\.(gyp|gypi)$',))
1055 cygwin_shell = []
1057 for f in input_api.AffectedSourceFiles(source_file_filter):
1058 for linenum, line in f.ChangedContents():
1059 if 'msvs_cygwin_shell' in line:
1060 cygwin_shell.append(f.LocalPath())
1061 break
1063 if cygwin_shell:
1064 return [output_api.PresubmitError(
1065 'These files should not use msvs_cygwin_shell (the default is 0):',
1066 items=cygwin_shell)]
1067 return []
1070 def _CheckUserActionUpdate(input_api, output_api):
1071 """Checks if any new user action has been added."""
1072 if any('actions.xml' == input_api.os_path.basename(f) for f in
1073 input_api.LocalPaths()):
1074 # If actions.xml is already included in the changelist, the PRESUBMIT
1075 # for actions.xml will do a more complete presubmit check.
1076 return []
1078 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm'))
1079 action_re = r'[^a-zA-Z]UserMetricsAction\("([^"]*)'
1080 current_actions = None
1081 for f in input_api.AffectedFiles(file_filter=file_filter):
1082 for line_num, line in f.ChangedContents():
1083 match = input_api.re.search(action_re, line)
1084 if match:
1085 # Loads contents in tools/metrics/actions/actions.xml to memory. It's
1086 # loaded only once.
1087 if not current_actions:
1088 with open('tools/metrics/actions/actions.xml') as actions_f:
1089 current_actions = actions_f.read()
1090 # Search for the matched user action name in |current_actions|.
1091 for action_name in match.groups():
1092 action = 'name="{0}"'.format(action_name)
1093 if action not in current_actions:
1094 return [output_api.PresubmitPromptWarning(
1095 'File %s line %d: %s is missing in '
1096 'tools/metrics/actions/actions.xml. Please run '
1097 'tools/metrics/actions/extract_actions.py to update.'
1098 % (f.LocalPath(), line_num, action_name))]
1099 return []
1102 def _GetJSONParseError(input_api, filename, eat_comments=True):
1103 try:
1104 contents = input_api.ReadFile(filename)
1105 if eat_comments:
1106 json_comment_eater = input_api.os_path.join(
1107 input_api.PresubmitLocalPath(),
1108 'tools', 'json_comment_eater', 'json_comment_eater.py')
1109 process = input_api.subprocess.Popen(
1110 [input_api.python_executable, json_comment_eater],
1111 stdin=input_api.subprocess.PIPE,
1112 stdout=input_api.subprocess.PIPE,
1113 universal_newlines=True)
1114 (contents, _) = process.communicate(input=contents)
1116 input_api.json.loads(contents)
1117 except ValueError as e:
1118 return e
1119 return None
1122 def _GetIDLParseError(input_api, filename):
1123 try:
1124 contents = input_api.ReadFile(filename)
1125 idl_schema = input_api.os_path.join(
1126 input_api.PresubmitLocalPath(),
1127 'tools', 'json_schema_compiler', 'idl_schema.py')
1128 process = input_api.subprocess.Popen(
1129 [input_api.python_executable, idl_schema],
1130 stdin=input_api.subprocess.PIPE,
1131 stdout=input_api.subprocess.PIPE,
1132 stderr=input_api.subprocess.PIPE,
1133 universal_newlines=True)
1134 (_, error) = process.communicate(input=contents)
1135 return error or None
1136 except ValueError as e:
1137 return e
1140 def _CheckParseErrors(input_api, output_api):
1141 """Check that IDL and JSON files do not contain syntax errors."""
1142 actions = {
1143 '.idl': _GetIDLParseError,
1144 '.json': _GetJSONParseError,
1146 # These paths contain test data and other known invalid JSON files.
1147 excluded_patterns = [
1148 r'test[\\\/]data[\\\/]',
1149 r'^components[\\\/]policy[\\\/]resources[\\\/]policy_templates\.json$',
1151 # Most JSON files are preprocessed and support comments, but these do not.
1152 json_no_comments_patterns = [
1153 r'^testing[\\\/]',
1155 # Only run IDL checker on files in these directories.
1156 idl_included_patterns = [
1157 r'^chrome[\\\/]common[\\\/]extensions[\\\/]api[\\\/]',
1158 r'^extensions[\\\/]common[\\\/]api[\\\/]',
1161 def get_action(affected_file):
1162 filename = affected_file.LocalPath()
1163 return actions.get(input_api.os_path.splitext(filename)[1])
1165 def MatchesFile(patterns, path):
1166 for pattern in patterns:
1167 if input_api.re.search(pattern, path):
1168 return True
1169 return False
1171 def FilterFile(affected_file):
1172 action = get_action(affected_file)
1173 if not action:
1174 return False
1175 path = affected_file.LocalPath()
1177 if MatchesFile(excluded_patterns, path):
1178 return False
1180 if (action == _GetIDLParseError and
1181 not MatchesFile(idl_included_patterns, path)):
1182 return False
1183 return True
1185 results = []
1186 for affected_file in input_api.AffectedFiles(
1187 file_filter=FilterFile, include_deletes=False):
1188 action = get_action(affected_file)
1189 kwargs = {}
1190 if (action == _GetJSONParseError and
1191 MatchesFile(json_no_comments_patterns, affected_file.LocalPath())):
1192 kwargs['eat_comments'] = False
1193 parse_error = action(input_api,
1194 affected_file.AbsoluteLocalPath(),
1195 **kwargs)
1196 if parse_error:
1197 results.append(output_api.PresubmitError('%s could not be parsed: %s' %
1198 (affected_file.LocalPath(), parse_error)))
1199 return results
1202 def _CheckJavaStyle(input_api, output_api):
1203 """Runs checkstyle on changed java files and returns errors if any exist."""
1204 import sys
1205 original_sys_path = sys.path
1206 try:
1207 sys.path = sys.path + [input_api.os_path.join(
1208 input_api.PresubmitLocalPath(), 'tools', 'android', 'checkstyle')]
1209 import checkstyle
1210 finally:
1211 # Restore sys.path to what it was before.
1212 sys.path = original_sys_path
1214 return checkstyle.RunCheckstyle(
1215 input_api, output_api, 'tools/android/checkstyle/chromium-style-5.0.xml')
1218 _DEPRECATED_CSS = [
1219 # Values
1220 ( "-webkit-box", "flex" ),
1221 ( "-webkit-inline-box", "inline-flex" ),
1222 ( "-webkit-flex", "flex" ),
1223 ( "-webkit-inline-flex", "inline-flex" ),
1224 ( "-webkit-min-content", "min-content" ),
1225 ( "-webkit-max-content", "max-content" ),
1227 # Properties
1228 ( "-webkit-background-clip", "background-clip" ),
1229 ( "-webkit-background-origin", "background-origin" ),
1230 ( "-webkit-background-size", "background-size" ),
1231 ( "-webkit-box-shadow", "box-shadow" ),
1233 # Functions
1234 ( "-webkit-gradient", "gradient" ),
1235 ( "-webkit-repeating-gradient", "repeating-gradient" ),
1236 ( "-webkit-linear-gradient", "linear-gradient" ),
1237 ( "-webkit-repeating-linear-gradient", "repeating-linear-gradient" ),
1238 ( "-webkit-radial-gradient", "radial-gradient" ),
1239 ( "-webkit-repeating-radial-gradient", "repeating-radial-gradient" ),
1242 def _CheckNoDeprecatedCSS(input_api, output_api):
1243 """ Make sure that we don't use deprecated CSS
1244 properties, functions or values. Our external
1245 documentation is ignored by the hooks as it
1246 needs to be consumed by WebKit. """
1247 results = []
1248 file_inclusion_pattern = (r".+\.css$",)
1249 black_list = (_EXCLUDED_PATHS +
1250 _TEST_CODE_EXCLUDED_PATHS +
1251 input_api.DEFAULT_BLACK_LIST +
1252 (r"^chrome/common/extensions/docs",
1253 r"^chrome/docs",
1254 r"^native_client_sdk"))
1255 file_filter = lambda f: input_api.FilterSourceFile(
1256 f, white_list=file_inclusion_pattern, black_list=black_list)
1257 for fpath in input_api.AffectedFiles(file_filter=file_filter):
1258 for line_num, line in fpath.ChangedContents():
1259 for (deprecated_value, value) in _DEPRECATED_CSS:
1260 if deprecated_value in line:
1261 results.append(output_api.PresubmitError(
1262 "%s:%d: Use of deprecated CSS %s, use %s instead" %
1263 (fpath.LocalPath(), line_num, deprecated_value, value)))
1264 return results
1267 _DEPRECATED_JS = [
1268 ( "__lookupGetter__", "Object.getOwnPropertyDescriptor" ),
1269 ( "__defineGetter__", "Object.defineProperty" ),
1270 ( "__defineSetter__", "Object.defineProperty" ),
1273 def _CheckNoDeprecatedJS(input_api, output_api):
1274 """Make sure that we don't use deprecated JS in Chrome code."""
1275 results = []
1276 file_inclusion_pattern = (r".+\.js$",) # TODO(dbeam): .html?
1277 black_list = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS +
1278 input_api.DEFAULT_BLACK_LIST)
1279 file_filter = lambda f: input_api.FilterSourceFile(
1280 f, white_list=file_inclusion_pattern, black_list=black_list)
1281 for fpath in input_api.AffectedFiles(file_filter=file_filter):
1282 for lnum, line in fpath.ChangedContents():
1283 for (deprecated, replacement) in _DEPRECATED_JS:
1284 if deprecated in line:
1285 results.append(output_api.PresubmitError(
1286 "%s:%d: Use of deprecated JS %s, use %s instead" %
1287 (fpath.LocalPath(), lnum, deprecated, replacement)))
1288 return results
1291 def _CommonChecks(input_api, output_api):
1292 """Checks common to both upload and commit."""
1293 results = []
1294 results.extend(input_api.canned_checks.PanProjectChecks(
1295 input_api, output_api,
1296 excluded_paths=_EXCLUDED_PATHS + _TESTRUNNER_PATHS))
1297 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
1298 results.extend(
1299 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
1300 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
1301 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
1302 results.extend(_CheckNoNewWStrings(input_api, output_api))
1303 results.extend(_CheckNoDEPSGIT(input_api, output_api))
1304 results.extend(_CheckNoBannedFunctions(input_api, output_api))
1305 results.extend(_CheckNoPragmaOnce(input_api, output_api))
1306 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
1307 results.extend(_CheckUnwantedDependencies(input_api, output_api))
1308 results.extend(_CheckFilePermissions(input_api, output_api))
1309 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
1310 results.extend(_CheckIncludeOrder(input_api, output_api))
1311 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
1312 results.extend(_CheckPatchFiles(input_api, output_api))
1313 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
1314 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
1315 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
1316 results.extend(_CheckForInvalidIfDefinedMacros(input_api, output_api))
1317 # TODO(danakj): Remove this when base/move.h is removed.
1318 results.extend(_CheckForUsingSideEffectsOfPass(input_api, output_api))
1319 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
1320 results.extend(
1321 input_api.canned_checks.CheckChangeHasNoTabs(
1322 input_api,
1323 output_api,
1324 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
1325 results.extend(_CheckSpamLogging(input_api, output_api))
1326 results.extend(_CheckForAnonymousVariables(input_api, output_api))
1327 results.extend(_CheckCygwinShell(input_api, output_api))
1328 results.extend(_CheckUserActionUpdate(input_api, output_api))
1329 results.extend(_CheckNoDeprecatedCSS(input_api, output_api))
1330 results.extend(_CheckNoDeprecatedJS(input_api, output_api))
1331 results.extend(_CheckParseErrors(input_api, output_api))
1332 results.extend(_CheckForIPCRules(input_api, output_api))
1334 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
1335 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
1336 input_api, output_api,
1337 input_api.PresubmitLocalPath(),
1338 whitelist=[r'^PRESUBMIT_test\.py$']))
1339 return results
1342 def _CheckAuthorizedAuthor(input_api, output_api):
1343 """For non-googler/chromites committers, verify the author's email address is
1344 in AUTHORS.
1346 # TODO(maruel): Add it to input_api?
1347 import fnmatch
1349 author = input_api.change.author_email
1350 if not author:
1351 input_api.logging.info('No author, skipping AUTHOR check')
1352 return []
1353 authors_path = input_api.os_path.join(
1354 input_api.PresubmitLocalPath(), 'AUTHORS')
1355 valid_authors = (
1356 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
1357 for line in open(authors_path))
1358 valid_authors = [item.group(1).lower() for item in valid_authors if item]
1359 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
1360 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
1361 return [output_api.PresubmitPromptWarning(
1362 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
1363 '\n'
1364 'http://www.chromium.org/developers/contributing-code and read the '
1365 '"Legal" section\n'
1366 'If you are a chromite, verify the contributor signed the CLA.') %
1367 author)]
1368 return []
1371 def _CheckPatchFiles(input_api, output_api):
1372 problems = [f.LocalPath() for f in input_api.AffectedFiles()
1373 if f.LocalPath().endswith(('.orig', '.rej'))]
1374 if problems:
1375 return [output_api.PresubmitError(
1376 "Don't commit .rej and .orig files.", problems)]
1377 else:
1378 return []
1381 def _DidYouMeanOSMacro(bad_macro):
1382 try:
1383 return {'A': 'OS_ANDROID',
1384 'B': 'OS_BSD',
1385 'C': 'OS_CHROMEOS',
1386 'F': 'OS_FREEBSD',
1387 'L': 'OS_LINUX',
1388 'M': 'OS_MACOSX',
1389 'N': 'OS_NACL',
1390 'O': 'OS_OPENBSD',
1391 'P': 'OS_POSIX',
1392 'S': 'OS_SOLARIS',
1393 'W': 'OS_WIN'}[bad_macro[3].upper()]
1394 except KeyError:
1395 return ''
1398 def _CheckForInvalidOSMacrosInFile(input_api, f):
1399 """Check for sensible looking, totally invalid OS macros."""
1400 preprocessor_statement = input_api.re.compile(r'^\s*#')
1401 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
1402 results = []
1403 for lnum, line in f.ChangedContents():
1404 if preprocessor_statement.search(line):
1405 for match in os_macro.finditer(line):
1406 if not match.group(1) in _VALID_OS_MACROS:
1407 good = _DidYouMeanOSMacro(match.group(1))
1408 did_you_mean = ' (did you mean %s?)' % good if good else ''
1409 results.append(' %s:%d %s%s' % (f.LocalPath(),
1410 lnum,
1411 match.group(1),
1412 did_you_mean))
1413 return results
1416 def _CheckForInvalidOSMacros(input_api, output_api):
1417 """Check all affected files for invalid OS macros."""
1418 bad_macros = []
1419 for f in input_api.AffectedFiles():
1420 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
1421 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
1423 if not bad_macros:
1424 return []
1426 return [output_api.PresubmitError(
1427 'Possibly invalid OS macro[s] found. Please fix your code\n'
1428 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
1431 def _CheckForInvalidIfDefinedMacrosInFile(input_api, f):
1432 """Check all affected files for invalid "if defined" macros."""
1433 ALWAYS_DEFINED_MACROS = (
1434 "TARGET_CPU_PPC",
1435 "TARGET_CPU_PPC64",
1436 "TARGET_CPU_68K",
1437 "TARGET_CPU_X86",
1438 "TARGET_CPU_ARM",
1439 "TARGET_CPU_MIPS",
1440 "TARGET_CPU_SPARC",
1441 "TARGET_CPU_ALPHA",
1442 "TARGET_IPHONE_SIMULATOR",
1443 "TARGET_OS_EMBEDDED",
1444 "TARGET_OS_IPHONE",
1445 "TARGET_OS_MAC",
1446 "TARGET_OS_UNIX",
1447 "TARGET_OS_WIN32",
1449 ifdef_macro = input_api.re.compile(r'^\s*#.*(?:ifdef\s|defined\()([^\s\)]+)')
1450 results = []
1451 for lnum, line in f.ChangedContents():
1452 for match in ifdef_macro.finditer(line):
1453 if match.group(1) in ALWAYS_DEFINED_MACROS:
1454 always_defined = ' %s is always defined. ' % match.group(1)
1455 did_you_mean = 'Did you mean \'#if %s\'?' % match.group(1)
1456 results.append(' %s:%d %s\n\t%s' % (f.LocalPath(),
1457 lnum,
1458 always_defined,
1459 did_you_mean))
1460 return results
1463 def _CheckForInvalidIfDefinedMacros(input_api, output_api):
1464 """Check all affected files for invalid "if defined" macros."""
1465 bad_macros = []
1466 for f in input_api.AffectedFiles():
1467 if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
1468 bad_macros.extend(_CheckForInvalidIfDefinedMacrosInFile(input_api, f))
1470 if not bad_macros:
1471 return []
1473 return [output_api.PresubmitError(
1474 'Found ifdef check on always-defined macro[s]. Please fix your code\n'
1475 'or check the list of ALWAYS_DEFINED_MACROS in src/PRESUBMIT.py.',
1476 bad_macros)]
1479 def _CheckForUsingSideEffectsOfPass(input_api, output_api):
1480 """Check all affected files for using side effects of Pass."""
1481 errors = []
1482 for f in input_api.AffectedFiles():
1483 if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
1484 for lnum, line in f.ChangedContents():
1485 # Disallow Foo(*my_scoped_thing.Pass()); See crbug.com/418297.
1486 if input_api.re.search(r'\*[a-zA-Z0-9_]+\.Pass\(\)', line):
1487 errors.append(output_api.PresubmitError(
1488 ('%s:%d uses *foo.Pass() to delete the contents of scoped_ptr. ' +
1489 'See crbug.com/418297.') % (f.LocalPath(), lnum)))
1490 return errors
1493 def _CheckForIPCRules(input_api, output_api):
1494 """Check for same IPC rules described in
1495 http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc
1497 base_pattern = r'IPC_ENUM_TRAITS\('
1498 inclusion_pattern = input_api.re.compile(r'(%s)' % base_pattern)
1499 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_pattern)
1501 problems = []
1502 for f in input_api.AffectedSourceFiles(None):
1503 local_path = f.LocalPath()
1504 if not local_path.endswith('.h'):
1505 continue
1506 for line_number, line in f.ChangedContents():
1507 if inclusion_pattern.search(line) and not comment_pattern.search(line):
1508 problems.append(
1509 '%s:%d\n %s' % (local_path, line_number, line.strip()))
1511 if problems:
1512 return [output_api.PresubmitPromptWarning(
1513 _IPC_ENUM_TRAITS_DEPRECATED, problems)]
1514 else:
1515 return []
1518 def CheckChangeOnUpload(input_api, output_api):
1519 results = []
1520 results.extend(_CommonChecks(input_api, output_api))
1521 results.extend(_CheckValidHostsInDEPS(input_api, output_api))
1522 results.extend(_CheckJavaStyle(input_api, output_api))
1523 return results
1526 def GetTryServerMasterForBot(bot):
1527 """Returns the Try Server master for the given bot.
1529 It tries to guess the master from the bot name, but may still fail
1530 and return None. There is no longer a default master.
1532 # Potentially ambiguous bot names are listed explicitly.
1533 master_map = {
1534 'linux_gpu': 'tryserver.chromium.gpu',
1535 'mac_gpu': 'tryserver.chromium.gpu',
1536 'win_gpu': 'tryserver.chromium.gpu',
1537 'chromium_presubmit': 'tryserver.chromium.linux',
1538 'blink_presubmit': 'tryserver.chromium.linux',
1539 'tools_build_presubmit': 'tryserver.chromium.linux',
1541 master = master_map.get(bot)
1542 if not master:
1543 if 'gpu' in bot:
1544 master = 'tryserver.chromium.gpu'
1545 elif 'linux' in bot or 'android' in bot or 'presubmit' in bot:
1546 master = 'tryserver.chromium.linux'
1547 elif 'win' in bot:
1548 master = 'tryserver.chromium.win'
1549 elif 'mac' in bot or 'ios' in bot:
1550 master = 'tryserver.chromium.mac'
1551 return master
1554 def GetDefaultTryConfigs(bots=None):
1555 """Returns a list of ('bot', set(['tests']), optionally filtered by [bots].
1557 To add tests to this list, they MUST be in the the corresponding master's
1558 gatekeeper config. For example, anything on master.chromium would be closed by
1559 tools/build/masters/master.chromium/master_gatekeeper_cfg.py.
1561 If 'bots' is specified, will only return configurations for bots in that list.
1564 standard_tests = [
1565 'base_unittests',
1566 'browser_tests',
1567 'cacheinvalidation_unittests',
1568 'check_deps',
1569 'check_deps2git',
1570 'content_browsertests',
1571 'content_unittests',
1572 'crypto_unittests',
1573 'gpu_unittests',
1574 'interactive_ui_tests',
1575 'ipc_tests',
1576 'jingle_unittests',
1577 'media_unittests',
1578 'net_unittests',
1579 'ppapi_unittests',
1580 'printing_unittests',
1581 'sql_unittests',
1582 'sync_unit_tests',
1583 'unit_tests',
1584 # Broken in release.
1585 #'url_unittests',
1586 #'webkit_unit_tests',
1589 builders_and_tests = {
1590 # TODO(maruel): Figure out a way to run 'sizes' where people can
1591 # effectively update the perf expectation correctly. This requires a
1592 # clobber=True build running 'sizes'. 'sizes' is not accurate with
1593 # incremental build. Reference:
1594 # http://chromium.org/developers/tree-sheriffs/perf-sheriffs.
1595 # TODO(maruel): An option would be to run 'sizes' but not count a failure
1596 # of this step as a try job failure.
1597 'android_aosp': ['compile'],
1598 'android_arm64_dbg_recipe': ['slave_steps'],
1599 'android_chromium_gn_compile_dbg': ['compile'],
1600 'android_chromium_gn_compile_rel': ['compile'],
1601 'android_clang_dbg_recipe': ['slave_steps'],
1602 'android_dbg_tests_recipe': ['slave_steps'],
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',
1613 'ios_rel_device': ['compile'],
1614 'ios_rel_device_ninja': ['compile'],
1615 'mac_asan': ['compile'],
1616 #TODO(stip): Change the name of this builder to reflect that it's release.
1617 'linux_gtk': standard_tests,
1618 'linux_chromeos_asan': ['compile'],
1619 'linux_chromium_asan_rel': ['defaulttests'],
1620 'linux_chromium_chromeos_clang_dbg': ['defaulttests'],
1621 'linux_chromium_chromeos_compile_dbg_ng': ['defaulttests'],
1622 'linux_chromium_chromeos_rel': ['defaulttests'],
1623 'linux_chromium_chromeos_rel_ng': ['defaulttests'],
1624 'linux_chromium_compile_dbg': ['defaulttests'],
1625 'linux_chromium_compile_dbg_32_ng': ['compile'],
1626 'linux_chromium_gn_dbg': ['compile'],
1627 'linux_chromium_gn_rel': ['defaulttests'],
1628 'linux_chromium_rel': ['defaulttests'],
1629 'linux_chromium_rel_ng': ['defaulttests'],
1630 'linux_chromium_clang_dbg': ['defaulttests'],
1631 'linux_gpu': ['defaulttests'],
1632 'linux_nacl_sdk_build': ['compile'],
1633 'mac_chromium_compile_dbg': ['defaulttests'],
1634 'mac_chromium_compile_dbg_ng': ['defaulttests'],
1635 'mac_chromium_rel': ['defaulttests'],
1636 'mac_chromium_rel_ng': ['defaulttests'],
1637 'mac_gpu': ['defaulttests'],
1638 'mac_nacl_sdk_build': ['compile'],
1639 'win_chromium_compile_dbg': ['defaulttests'],
1640 'win_chromium_dbg': ['defaulttests'],
1641 'win_chromium_rel': ['defaulttests'],
1642 'win_chromium_rel_ng': ['defaulttests'],
1643 'win_chromium_x64_rel': ['defaulttests'],
1644 'win_chromium_x64_rel_ng': ['defaulttests'],
1645 'win_gpu': ['defaulttests'],
1646 'win_nacl_sdk_build': ['compile'],
1647 'win8_chromium_rel': ['defaulttests'],
1650 if bots:
1651 filtered_builders_and_tests = dict((bot, set(builders_and_tests[bot]))
1652 for bot in bots)
1653 else:
1654 filtered_builders_and_tests = dict(
1655 (bot, set(tests))
1656 for bot, tests in builders_and_tests.iteritems())
1658 # Build up the mapping from tryserver master to bot/test.
1659 out = dict()
1660 for bot, tests in filtered_builders_and_tests.iteritems():
1661 out.setdefault(GetTryServerMasterForBot(bot), {})[bot] = tests
1662 return out
1665 def CheckChangeOnCommit(input_api, output_api):
1666 results = []
1667 results.extend(_CommonChecks(input_api, output_api))
1668 # TODO(thestig) temporarily disabled, doesn't work in third_party/
1669 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
1670 # input_api, output_api, sources))
1671 # Make sure the tree is 'open'.
1672 results.extend(input_api.canned_checks.CheckTreeIsOpen(
1673 input_api,
1674 output_api,
1675 json_url='http://chromium-status.appspot.com/current?format=json'))
1677 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1678 input_api, output_api))
1679 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1680 input_api, output_api))
1681 return results
1684 def GetPreferredTryMasters(project, change):
1685 import re
1686 files = change.LocalPaths()
1688 if not files or all(re.search(r'[\\\/]OWNERS$', f) for f in files):
1689 return {}
1691 if all(re.search(r'\.(m|mm)$|(^|[\\\/_])mac[\\\/_.]', f) for f in files):
1692 return GetDefaultTryConfigs([
1693 'mac_chromium_compile_dbg_ng',
1694 'mac_chromium_rel_ng',
1696 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
1697 return GetDefaultTryConfigs([
1698 'win_chromium_dbg',
1699 'win_chromium_rel',
1700 'win8_chromium_rel',
1702 if all(re.search(r'(^|[\\\/_])android[\\\/_.]', f) for f in files):
1703 return GetDefaultTryConfigs([
1704 'android_aosp',
1705 'android_dbg_tests_recipe',
1707 if all(re.search(r'[\\\/_]ios[\\\/_.]', f) for f in files):
1708 return GetDefaultTryConfigs(['ios_rel_device', 'ios_dbg_simulator'])
1710 builders = [
1711 'android_aosp',
1712 'android_arm64_dbg_recipe',
1713 'android_chromium_gn_compile_rel',
1714 'android_chromium_gn_compile_dbg',
1715 'android_clang_dbg_recipe',
1716 'android_dbg_tests_recipe',
1717 'ios_dbg_simulator',
1718 'ios_rel_device',
1719 'ios_rel_device_ninja',
1720 'linux_chromium_asan_rel',
1721 'linux_chromium_chromeos_compile_dbg_ng',
1722 'linux_chromium_chromeos_rel_ng',
1723 'linux_chromium_compile_dbg_32_ng',
1724 'linux_chromium_gn_dbg',
1725 'linux_chromium_gn_rel',
1726 'linux_chromium_rel_ng',
1727 'linux_gpu',
1728 'mac_chromium_compile_dbg_ng',
1729 'mac_chromium_rel_ng',
1730 'mac_gpu',
1731 'win_chromium_compile_dbg',
1732 'win_chromium_rel',
1733 'win_chromium_x64_rel',
1734 'win_gpu',
1735 'win8_chromium_rel',
1738 # Match things like path/aura/file.cc and path/file_aura.cc.
1739 # Same for chromeos.
1740 if any(re.search(r'[\\\/_](aura|chromeos)', f) for f in files):
1741 builders.extend([
1742 'linux_chromeos_asan',
1745 return GetDefaultTryConfigs(builders)