Fix componene debug build failure because of src/media/blink
[chromium-blink-merge.git] / PRESUBMIT.py
blob3326d8c0d1a8b1d6ce92abcaba955e1374ed62e4
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 depot_tools.
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_recorder\.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$",
177 r"^ui[\\\/]ozone[\\\/]platform[\\\/]dri[\\\/]native_display_delegate_proxy\.cc$",
181 'SkRefPtr',
183 'The use of SkRefPtr is prohibited. ',
184 'Please use skia::RefPtr instead.'
186 True,
190 'SkAutoRef',
192 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
193 'Please use skia::RefPtr instead.'
195 True,
199 'SkAutoTUnref',
201 'The use of SkAutoTUnref is dangerous because it implicitly ',
202 'converts to a raw pointer. Please use skia::RefPtr instead.'
204 True,
208 'SkAutoUnref',
210 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
211 'because it implicitly converts to a raw pointer. ',
212 'Please use skia::RefPtr instead.'
214 True,
218 r'/HANDLE_EINTR\(.*close',
220 'HANDLE_EINTR(close) is invalid. If close fails with EINTR, the file',
221 'descriptor will be closed, and it is incorrect to retry the close.',
222 'Either call close directly and ignore its return value, or wrap close',
223 'in IGNORE_EINTR to use its return value. See http://crbug.com/269623'
225 True,
229 r'/IGNORE_EINTR\((?!.*close)',
231 'IGNORE_EINTR is only valid when wrapping close. To wrap other system',
232 'calls, use HANDLE_EINTR. See http://crbug.com/269623',
234 True,
236 # Files that #define IGNORE_EINTR.
237 r'^base[\\\/]posix[\\\/]eintr_wrapper\.h$',
238 r'^ppapi[\\\/]tests[\\\/]test_broker\.cc$',
242 r'/v8::Extension\(',
244 'Do not introduce new v8::Extensions into the code base, use',
245 'gin::Wrappable instead. See http://crbug.com/334679',
247 True,
249 r'extensions[\\\/]renderer[\\\/]safe_builtins\.*',
254 _IPC_ENUM_TRAITS_DEPRECATED = (
255 'You are using IPC_ENUM_TRAITS() in your code. It has been deprecated.\n'
256 'See http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc')
259 _VALID_OS_MACROS = (
260 # Please keep sorted.
261 'OS_ANDROID',
262 'OS_ANDROID_HOST',
263 'OS_BSD',
264 'OS_CAT', # For testing.
265 'OS_CHROMEOS',
266 'OS_FREEBSD',
267 'OS_IOS',
268 'OS_LINUX',
269 'OS_MACOSX',
270 'OS_NACL',
271 'OS_NACL_NONSFI',
272 'OS_NACL_SFI',
273 'OS_OPENBSD',
274 'OS_POSIX',
275 'OS_QNX',
276 'OS_SOLARIS',
277 'OS_WIN',
281 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
282 """Attempts to prevent use of functions intended only for testing in
283 non-testing code. For now this is just a best-effort implementation
284 that ignores header files and may have some false positives. A
285 better implementation would probably need a proper C++ parser.
287 # We only scan .cc files and the like, as the declaration of
288 # for-testing functions in header files are hard to distinguish from
289 # calls to such functions without a proper C++ parser.
290 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
292 base_function_pattern = r'[ :]test::[^\s]+|ForTest(ing)?|for_test(ing)?'
293 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
294 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern)
295 exclusion_pattern = input_api.re.compile(
296 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
297 base_function_pattern, base_function_pattern))
299 def FilterFile(affected_file):
300 black_list = (_EXCLUDED_PATHS +
301 _TEST_CODE_EXCLUDED_PATHS +
302 input_api.DEFAULT_BLACK_LIST)
303 return input_api.FilterSourceFile(
304 affected_file,
305 white_list=(file_inclusion_pattern, ),
306 black_list=black_list)
308 problems = []
309 for f in input_api.AffectedSourceFiles(FilterFile):
310 local_path = f.LocalPath()
311 for line_number, line in f.ChangedContents():
312 if (inclusion_pattern.search(line) and
313 not comment_pattern.search(line) and
314 not exclusion_pattern.search(line)):
315 problems.append(
316 '%s:%d\n %s' % (local_path, line_number, line.strip()))
318 if problems:
319 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
320 else:
321 return []
324 def _CheckNoIOStreamInHeaders(input_api, output_api):
325 """Checks to make sure no .h files include <iostream>."""
326 files = []
327 pattern = input_api.re.compile(r'^#include\s*<iostream>',
328 input_api.re.MULTILINE)
329 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
330 if not f.LocalPath().endswith('.h'):
331 continue
332 contents = input_api.ReadFile(f)
333 if pattern.search(contents):
334 files.append(f)
336 if len(files):
337 return [ output_api.PresubmitError(
338 'Do not #include <iostream> in header files, since it inserts static '
339 'initialization into every file including the header. Instead, '
340 '#include <ostream>. See http://crbug.com/94794',
341 files) ]
342 return []
345 def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
346 """Checks to make sure no source files use UNIT_TEST"""
347 problems = []
348 for f in input_api.AffectedFiles():
349 if (not f.LocalPath().endswith(('.cc', '.mm'))):
350 continue
352 for line_num, line in f.ChangedContents():
353 if 'UNIT_TEST ' in line or line.endswith('UNIT_TEST'):
354 problems.append(' %s:%d' % (f.LocalPath(), line_num))
356 if not problems:
357 return []
358 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
359 '\n'.join(problems))]
362 def _CheckNoNewWStrings(input_api, output_api):
363 """Checks to make sure we don't introduce use of wstrings."""
364 problems = []
365 for f in input_api.AffectedFiles():
366 if (not f.LocalPath().endswith(('.cc', '.h')) or
367 f.LocalPath().endswith(('test.cc', '_win.cc', '_win.h')) or
368 '/win/' in f.LocalPath()):
369 continue
371 allowWString = False
372 for line_num, line in f.ChangedContents():
373 if 'presubmit: allow wstring' in line:
374 allowWString = True
375 elif not allowWString and 'wstring' in line:
376 problems.append(' %s:%d' % (f.LocalPath(), line_num))
377 allowWString = False
378 else:
379 allowWString = False
381 if not problems:
382 return []
383 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
384 ' If you are calling a cross-platform API that accepts a wstring, '
385 'fix the API.\n' +
386 '\n'.join(problems))]
389 def _CheckNoDEPSGIT(input_api, output_api):
390 """Make sure .DEPS.git is never modified manually."""
391 if any(f.LocalPath().endswith('.DEPS.git') for f in
392 input_api.AffectedFiles()):
393 return [output_api.PresubmitError(
394 'Never commit changes to .DEPS.git. This file is maintained by an\n'
395 'automated system based on what\'s in DEPS and your changes will be\n'
396 'overwritten.\n'
397 'See https://sites.google.com/a/chromium.org/dev/developers/how-tos/get-the-code#Rolling_DEPS\n'
398 'for more information')]
399 return []
402 def _CheckValidHostsInDEPS(input_api, output_api):
403 """Checks that DEPS file deps are from allowed_hosts."""
404 # Run only if DEPS file has been modified to annoy fewer bystanders.
405 if all(f.LocalPath() != 'DEPS' for f in input_api.AffectedFiles()):
406 return []
407 # Outsource work to gclient verify
408 try:
409 input_api.subprocess.check_output(['gclient', 'verify'])
410 return []
411 except input_api.subprocess.CalledProcessError, error:
412 return [output_api.PresubmitError(
413 'DEPS file must have only git dependencies.',
414 long_text=error.output)]
417 def _CheckNoBannedFunctions(input_api, output_api):
418 """Make sure that banned functions are not used."""
419 warnings = []
420 errors = []
422 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
423 for f in input_api.AffectedFiles(file_filter=file_filter):
424 for line_num, line in f.ChangedContents():
425 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
426 matched = False
427 if func_name[0:1] == '/':
428 regex = func_name[1:]
429 if input_api.re.search(regex, line):
430 matched = True
431 elif func_name in line:
432 matched = True
433 if matched:
434 problems = warnings;
435 if error:
436 problems = errors;
437 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
438 for message_line in message:
439 problems.append(' %s' % message_line)
441 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
442 for f in input_api.AffectedFiles(file_filter=file_filter):
443 for line_num, line in f.ChangedContents():
444 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
445 def IsBlacklisted(affected_file, blacklist):
446 local_path = affected_file.LocalPath()
447 for item in blacklist:
448 if input_api.re.match(item, local_path):
449 return True
450 return False
451 if IsBlacklisted(f, excluded_paths):
452 continue
453 matched = False
454 if func_name[0:1] == '/':
455 regex = func_name[1:]
456 if input_api.re.search(regex, line):
457 matched = True
458 elif func_name in line:
459 matched = True
460 if matched:
461 problems = warnings;
462 if error:
463 problems = errors;
464 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
465 for message_line in message:
466 problems.append(' %s' % message_line)
468 result = []
469 if (warnings):
470 result.append(output_api.PresubmitPromptWarning(
471 'Banned functions were used.\n' + '\n'.join(warnings)))
472 if (errors):
473 result.append(output_api.PresubmitError(
474 'Banned functions were used.\n' + '\n'.join(errors)))
475 return result
478 def _CheckNoPragmaOnce(input_api, output_api):
479 """Make sure that banned functions are not used."""
480 files = []
481 pattern = input_api.re.compile(r'^#pragma\s+once',
482 input_api.re.MULTILINE)
483 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
484 if not f.LocalPath().endswith('.h'):
485 continue
486 contents = input_api.ReadFile(f)
487 if pattern.search(contents):
488 files.append(f)
490 if files:
491 return [output_api.PresubmitError(
492 'Do not use #pragma once in header files.\n'
493 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
494 files)]
495 return []
498 def _CheckNoTrinaryTrueFalse(input_api, output_api):
499 """Checks to make sure we don't introduce use of foo ? true : false."""
500 problems = []
501 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
502 for f in input_api.AffectedFiles():
503 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
504 continue
506 for line_num, line in f.ChangedContents():
507 if pattern.match(line):
508 problems.append(' %s:%d' % (f.LocalPath(), line_num))
510 if not problems:
511 return []
512 return [output_api.PresubmitPromptWarning(
513 'Please consider avoiding the "? true : false" pattern if possible.\n' +
514 '\n'.join(problems))]
517 def _CheckUnwantedDependencies(input_api, output_api):
518 """Runs checkdeps on #include statements added in this
519 change. Breaking - rules is an error, breaking ! rules is a
520 warning.
522 import sys
523 # We need to wait until we have an input_api object and use this
524 # roundabout construct to import checkdeps because this file is
525 # eval-ed and thus doesn't have __file__.
526 original_sys_path = sys.path
527 try:
528 sys.path = sys.path + [input_api.os_path.join(
529 input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
530 import checkdeps
531 from cpp_checker import CppChecker
532 from rules import Rule
533 finally:
534 # Restore sys.path to what it was before.
535 sys.path = original_sys_path
537 added_includes = []
538 for f in input_api.AffectedFiles():
539 if not CppChecker.IsCppFile(f.LocalPath()):
540 continue
542 changed_lines = [line for line_num, line in f.ChangedContents()]
543 added_includes.append([f.LocalPath(), changed_lines])
545 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
547 error_descriptions = []
548 warning_descriptions = []
549 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
550 added_includes):
551 description_with_path = '%s\n %s' % (path, rule_description)
552 if rule_type == Rule.DISALLOW:
553 error_descriptions.append(description_with_path)
554 else:
555 warning_descriptions.append(description_with_path)
557 results = []
558 if error_descriptions:
559 results.append(output_api.PresubmitError(
560 'You added one or more #includes that violate checkdeps rules.',
561 error_descriptions))
562 if warning_descriptions:
563 results.append(output_api.PresubmitPromptOrNotify(
564 'You added one or more #includes of files that are temporarily\n'
565 'allowed but being removed. Can you avoid introducing the\n'
566 '#include? See relevant DEPS file(s) for details and contacts.',
567 warning_descriptions))
568 return results
571 def _CheckFilePermissions(input_api, output_api):
572 """Check that all files have their permissions properly set."""
573 if input_api.platform == 'win32':
574 return []
575 args = [input_api.python_executable, 'tools/checkperms/checkperms.py',
576 '--root', input_api.change.RepositoryRoot()]
577 for f in input_api.AffectedFiles():
578 args += ['--file', f.LocalPath()]
579 checkperms = input_api.subprocess.Popen(args,
580 stdout=input_api.subprocess.PIPE)
581 errors = checkperms.communicate()[0].strip()
582 if errors:
583 return [output_api.PresubmitError('checkperms.py failed.',
584 errors.splitlines())]
585 return []
588 def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
589 """Makes sure we don't include ui/aura/window_property.h
590 in header files.
592 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
593 errors = []
594 for f in input_api.AffectedFiles():
595 if not f.LocalPath().endswith('.h'):
596 continue
597 for line_num, line in f.ChangedContents():
598 if pattern.match(line):
599 errors.append(' %s:%d' % (f.LocalPath(), line_num))
601 results = []
602 if errors:
603 results.append(output_api.PresubmitError(
604 'Header files should not include ui/aura/window_property.h', errors))
605 return results
608 def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
609 """Checks that the lines in scope occur in the right order.
611 1. C system files in alphabetical order
612 2. C++ system files in alphabetical order
613 3. Project's .h files
616 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
617 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
618 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
620 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
622 state = C_SYSTEM_INCLUDES
624 previous_line = ''
625 previous_line_num = 0
626 problem_linenums = []
627 for line_num, line in scope:
628 if c_system_include_pattern.match(line):
629 if state != C_SYSTEM_INCLUDES:
630 problem_linenums.append((line_num, previous_line_num))
631 elif previous_line and previous_line > line:
632 problem_linenums.append((line_num, previous_line_num))
633 elif cpp_system_include_pattern.match(line):
634 if state == C_SYSTEM_INCLUDES:
635 state = CPP_SYSTEM_INCLUDES
636 elif state == CUSTOM_INCLUDES:
637 problem_linenums.append((line_num, previous_line_num))
638 elif previous_line and previous_line > line:
639 problem_linenums.append((line_num, previous_line_num))
640 elif custom_include_pattern.match(line):
641 if state != CUSTOM_INCLUDES:
642 state = CUSTOM_INCLUDES
643 elif previous_line and previous_line > line:
644 problem_linenums.append((line_num, previous_line_num))
645 else:
646 problem_linenums.append(line_num)
647 previous_line = line
648 previous_line_num = line_num
650 warnings = []
651 for (line_num, previous_line_num) in problem_linenums:
652 if line_num in changed_linenums or previous_line_num in changed_linenums:
653 warnings.append(' %s:%d' % (file_path, line_num))
654 return warnings
657 def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
658 """Checks the #include order for the given file f."""
660 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
661 # Exclude the following includes from the check:
662 # 1) #include <.../...>, e.g., <sys/...> includes often need to appear in a
663 # specific order.
664 # 2) <atlbase.h>, "build/build_config.h"
665 excluded_include_pattern = input_api.re.compile(
666 r'\s*#include (\<.*/.*|\<atlbase\.h\>|"build/build_config.h")')
667 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
668 # Match the final or penultimate token if it is xxxtest so we can ignore it
669 # when considering the special first include.
670 test_file_tag_pattern = input_api.re.compile(
671 r'_[a-z]+test(?=(_[a-zA-Z0-9]+)?\.)')
672 if_pattern = input_api.re.compile(
673 r'\s*#\s*(if|elif|else|endif|define|undef).*')
674 # Some files need specialized order of includes; exclude such files from this
675 # check.
676 uncheckable_includes_pattern = input_api.re.compile(
677 r'\s*#include '
678 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
680 contents = f.NewContents()
681 warnings = []
682 line_num = 0
684 # Handle the special first include. If the first include file is
685 # some/path/file.h, the corresponding including file can be some/path/file.cc,
686 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
687 # etc. It's also possible that no special first include exists.
688 # If the included file is some/path/file_platform.h the including file could
689 # also be some/path/file_xxxtest_platform.h.
690 including_file_base_name = test_file_tag_pattern.sub(
691 '', input_api.os_path.basename(f.LocalPath()))
693 for line in contents:
694 line_num += 1
695 if system_include_pattern.match(line):
696 # No special first include -> process the line again along with normal
697 # includes.
698 line_num -= 1
699 break
700 match = custom_include_pattern.match(line)
701 if match:
702 match_dict = match.groupdict()
703 header_basename = test_file_tag_pattern.sub(
704 '', input_api.os_path.basename(match_dict['FILE'])).replace('.h', '')
706 if header_basename not in including_file_base_name:
707 # No special first include -> process the line again along with normal
708 # includes.
709 line_num -= 1
710 break
712 # Split into scopes: Each region between #if and #endif is its own scope.
713 scopes = []
714 current_scope = []
715 for line in contents[line_num:]:
716 line_num += 1
717 if uncheckable_includes_pattern.match(line):
718 continue
719 if if_pattern.match(line):
720 scopes.append(current_scope)
721 current_scope = []
722 elif ((system_include_pattern.match(line) or
723 custom_include_pattern.match(line)) and
724 not excluded_include_pattern.match(line)):
725 current_scope.append((line_num, line))
726 scopes.append(current_scope)
728 for scope in scopes:
729 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
730 changed_linenums))
731 return warnings
734 def _CheckIncludeOrder(input_api, output_api):
735 """Checks that the #include order is correct.
737 1. The corresponding header for source files.
738 2. C system files in alphabetical order
739 3. C++ system files in alphabetical order
740 4. Project's .h files in alphabetical order
742 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
743 these rules separately.
745 def FileFilterIncludeOrder(affected_file):
746 black_list = (_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST)
747 return input_api.FilterSourceFile(affected_file, black_list=black_list)
749 warnings = []
750 for f in input_api.AffectedFiles(file_filter=FileFilterIncludeOrder):
751 if f.LocalPath().endswith(('.cc', '.h')):
752 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
753 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
755 results = []
756 if warnings:
757 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
758 warnings))
759 return results
762 def _CheckForVersionControlConflictsInFile(input_api, f):
763 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
764 errors = []
765 for line_num, line in f.ChangedContents():
766 if pattern.match(line):
767 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
768 return errors
771 def _CheckForVersionControlConflicts(input_api, output_api):
772 """Usually this is not intentional and will cause a compile failure."""
773 errors = []
774 for f in input_api.AffectedFiles():
775 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
777 results = []
778 if errors:
779 results.append(output_api.PresubmitError(
780 'Version control conflict markers found, please resolve.', errors))
781 return results
784 def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
785 def FilterFile(affected_file):
786 """Filter function for use with input_api.AffectedSourceFiles,
787 below. This filters out everything except non-test files from
788 top-level directories that generally speaking should not hard-code
789 service URLs (e.g. src/android_webview/, src/content/ and others).
791 return input_api.FilterSourceFile(
792 affected_file,
793 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
794 black_list=(_EXCLUDED_PATHS +
795 _TEST_CODE_EXCLUDED_PATHS +
796 input_api.DEFAULT_BLACK_LIST))
798 base_pattern = '"[^"]*google\.com[^"]*"'
799 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
800 pattern = input_api.re.compile(base_pattern)
801 problems = [] # items are (filename, line_number, line)
802 for f in input_api.AffectedSourceFiles(FilterFile):
803 for line_num, line in f.ChangedContents():
804 if not comment_pattern.search(line) and pattern.search(line):
805 problems.append((f.LocalPath(), line_num, line))
807 if problems:
808 return [output_api.PresubmitPromptOrNotify(
809 'Most layers below src/chrome/ should not hardcode service URLs.\n'
810 'Are you sure this is correct?',
811 [' %s:%d: %s' % (
812 problem[0], problem[1], problem[2]) for problem in problems])]
813 else:
814 return []
817 def _CheckNoAbbreviationInPngFileName(input_api, output_api):
818 """Makes sure there are no abbreviations in the name of PNG files.
819 The native_client_sdk directory is excluded because it has auto-generated PNG
820 files for documentation.
822 errors = []
823 white_list = (r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$',)
824 black_list = (r'^native_client_sdk[\\\/]',)
825 file_filter = lambda f: input_api.FilterSourceFile(
826 f, white_list=white_list, black_list=black_list)
827 for f in input_api.AffectedFiles(include_deletes=False,
828 file_filter=file_filter):
829 errors.append(' %s' % f.LocalPath())
831 results = []
832 if errors:
833 results.append(output_api.PresubmitError(
834 'The name of PNG files should not have abbreviations. \n'
835 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
836 'Contact oshima@chromium.org if you have questions.', errors))
837 return results
840 def _FilesToCheckForIncomingDeps(re, changed_lines):
841 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
842 a set of DEPS entries that we should look up.
844 For a directory (rather than a specific filename) we fake a path to
845 a specific filename by adding /DEPS. This is chosen as a file that
846 will seldom or never be subject to per-file include_rules.
848 # We ignore deps entries on auto-generated directories.
849 AUTO_GENERATED_DIRS = ['grit', 'jni']
851 # This pattern grabs the path without basename in the first
852 # parentheses, and the basename (if present) in the second. It
853 # relies on the simple heuristic that if there is a basename it will
854 # be a header file ending in ".h".
855 pattern = re.compile(
856 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
857 results = set()
858 for changed_line in changed_lines:
859 m = pattern.match(changed_line)
860 if m:
861 path = m.group(1)
862 if path.split('/')[0] not in AUTO_GENERATED_DIRS:
863 if m.group(2):
864 results.add('%s%s' % (path, m.group(2)))
865 else:
866 results.add('%s/DEPS' % path)
867 return results
870 def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
871 """When a dependency prefixed with + is added to a DEPS file, we
872 want to make sure that the change is reviewed by an OWNER of the
873 target file or directory, to avoid layering violations from being
874 introduced. This check verifies that this happens.
876 changed_lines = set()
877 for f in input_api.AffectedFiles():
878 filename = input_api.os_path.basename(f.LocalPath())
879 if filename == 'DEPS':
880 changed_lines |= set(line.strip()
881 for line_num, line
882 in f.ChangedContents())
883 if not changed_lines:
884 return []
886 virtual_depended_on_files = _FilesToCheckForIncomingDeps(input_api.re,
887 changed_lines)
888 if not virtual_depended_on_files:
889 return []
891 if input_api.is_committing:
892 if input_api.tbr:
893 return [output_api.PresubmitNotifyResult(
894 '--tbr was specified, skipping OWNERS check for DEPS additions')]
895 if not input_api.change.issue:
896 return [output_api.PresubmitError(
897 "DEPS approval by OWNERS check failed: this change has "
898 "no Rietveld issue number, so we can't check it for approvals.")]
899 output = output_api.PresubmitError
900 else:
901 output = output_api.PresubmitNotifyResult
903 owners_db = input_api.owners_db
904 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
905 input_api,
906 owners_db.email_regexp,
907 approval_needed=input_api.is_committing)
909 owner_email = owner_email or input_api.change.author_email
911 reviewers_plus_owner = set(reviewers)
912 if owner_email:
913 reviewers_plus_owner.add(owner_email)
914 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
915 reviewers_plus_owner)
917 # We strip the /DEPS part that was added by
918 # _FilesToCheckForIncomingDeps to fake a path to a file in a
919 # directory.
920 def StripDeps(path):
921 start_deps = path.rfind('/DEPS')
922 if start_deps != -1:
923 return path[:start_deps]
924 else:
925 return path
926 unapproved_dependencies = ["'+%s'," % StripDeps(path)
927 for path in missing_files]
929 if unapproved_dependencies:
930 output_list = [
931 output('Missing LGTM from OWNERS of dependencies added to DEPS:\n %s' %
932 '\n '.join(sorted(unapproved_dependencies)))]
933 if not input_api.is_committing:
934 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
935 output_list.append(output(
936 'Suggested missing target path OWNERS:\n %s' %
937 '\n '.join(suggested_owners or [])))
938 return output_list
940 return []
943 def _CheckSpamLogging(input_api, output_api):
944 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
945 black_list = (_EXCLUDED_PATHS +
946 _TEST_CODE_EXCLUDED_PATHS +
947 input_api.DEFAULT_BLACK_LIST +
948 (r"^base[\\\/]logging\.h$",
949 r"^base[\\\/]logging\.cc$",
950 r"^chrome[\\\/]app[\\\/]chrome_main_delegate\.cc$",
951 r"^chrome[\\\/]browser[\\\/]chrome_browser_main\.cc$",
952 r"^chrome[\\\/]browser[\\\/]ui[\\\/]startup[\\\/]"
953 r"startup_browser_creator\.cc$",
954 r"^chrome[\\\/]installer[\\\/]setup[\\\/].*",
955 r"chrome[\\\/]browser[\\\/]diagnostics[\\\/]" +
956 r"diagnostics_writer\.cc$",
957 r"^chrome_elf[\\\/]dll_hash[\\\/]dll_hash_main\.cc$",
958 r"^chromecast[\\\/]",
959 r"^cloud_print[\\\/]",
960 r"^content[\\\/]common[\\\/]gpu[\\\/]client[\\\/]"
961 r"gl_helper_benchmark\.cc$",
962 r"^courgette[\\\/]courgette_tool\.cc$",
963 r"^extensions[\\\/]renderer[\\\/]logging_native_handler\.cc$",
964 r"^ipc[\\\/]ipc_logging\.cc$",
965 r"^native_client_sdk[\\\/]",
966 r"^remoting[\\\/]base[\\\/]logging\.h$",
967 r"^remoting[\\\/]host[\\\/].*",
968 r"^sandbox[\\\/]linux[\\\/].*",
969 r"^tools[\\\/]",
970 r"^ui[\\\/]aura[\\\/]bench[\\\/]bench_main\.cc$",
971 r"^storage[\\\/]browser[\\\/]fileapi[\\\/]" +
972 r"dump_file_system.cc$",))
973 source_file_filter = lambda x: input_api.FilterSourceFile(
974 x, white_list=(file_inclusion_pattern,), black_list=black_list)
976 log_info = []
977 printf = []
979 for f in input_api.AffectedSourceFiles(source_file_filter):
980 contents = input_api.ReadFile(f, 'rb')
981 if input_api.re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", contents):
982 log_info.append(f.LocalPath())
983 elif input_api.re.search(r"\bD?LOG_IF\s*\(\s*INFO\s*,", contents):
984 log_info.append(f.LocalPath())
986 if input_api.re.search(r"\bprintf\(", contents):
987 printf.append(f.LocalPath())
988 elif input_api.re.search(r"\bfprintf\((stdout|stderr)", contents):
989 printf.append(f.LocalPath())
991 if log_info:
992 return [output_api.PresubmitError(
993 'These files spam the console log with LOG(INFO):',
994 items=log_info)]
995 if printf:
996 return [output_api.PresubmitError(
997 'These files spam the console log with printf/fprintf:',
998 items=printf)]
999 return []
1002 def _CheckForAnonymousVariables(input_api, output_api):
1003 """These types are all expected to hold locks while in scope and
1004 so should never be anonymous (which causes them to be immediately
1005 destroyed)."""
1006 they_who_must_be_named = [
1007 'base::AutoLock',
1008 'base::AutoReset',
1009 'base::AutoUnlock',
1010 'SkAutoAlphaRestore',
1011 'SkAutoBitmapShaderInstall',
1012 'SkAutoBlitterChoose',
1013 'SkAutoBounderCommit',
1014 'SkAutoCallProc',
1015 'SkAutoCanvasRestore',
1016 'SkAutoCommentBlock',
1017 'SkAutoDescriptor',
1018 'SkAutoDisableDirectionCheck',
1019 'SkAutoDisableOvalCheck',
1020 'SkAutoFree',
1021 'SkAutoGlyphCache',
1022 'SkAutoHDC',
1023 'SkAutoLockColors',
1024 'SkAutoLockPixels',
1025 'SkAutoMalloc',
1026 'SkAutoMaskFreeImage',
1027 'SkAutoMutexAcquire',
1028 'SkAutoPathBoundsUpdate',
1029 'SkAutoPDFRelease',
1030 'SkAutoRasterClipValidate',
1031 'SkAutoRef',
1032 'SkAutoTime',
1033 'SkAutoTrace',
1034 'SkAutoUnref',
1036 anonymous = r'(%s)\s*[({]' % '|'.join(they_who_must_be_named)
1037 # bad: base::AutoLock(lock.get());
1038 # not bad: base::AutoLock lock(lock.get());
1039 bad_pattern = input_api.re.compile(anonymous)
1040 # good: new base::AutoLock(lock.get())
1041 good_pattern = input_api.re.compile(r'\bnew\s*' + anonymous)
1042 errors = []
1044 for f in input_api.AffectedFiles():
1045 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
1046 continue
1047 for linenum, line in f.ChangedContents():
1048 if bad_pattern.search(line) and not good_pattern.search(line):
1049 errors.append('%s:%d' % (f.LocalPath(), linenum))
1051 if errors:
1052 return [output_api.PresubmitError(
1053 'These lines create anonymous variables that need to be named:',
1054 items=errors)]
1055 return []
1058 def _CheckCygwinShell(input_api, output_api):
1059 source_file_filter = lambda x: input_api.FilterSourceFile(
1060 x, white_list=(r'.+\.(gyp|gypi)$',))
1061 cygwin_shell = []
1063 for f in input_api.AffectedSourceFiles(source_file_filter):
1064 for linenum, line in f.ChangedContents():
1065 if 'msvs_cygwin_shell' in line:
1066 cygwin_shell.append(f.LocalPath())
1067 break
1069 if cygwin_shell:
1070 return [output_api.PresubmitError(
1071 'These files should not use msvs_cygwin_shell (the default is 0):',
1072 items=cygwin_shell)]
1073 return []
1076 def _CheckUserActionUpdate(input_api, output_api):
1077 """Checks if any new user action has been added."""
1078 if any('actions.xml' == input_api.os_path.basename(f) for f in
1079 input_api.LocalPaths()):
1080 # If actions.xml is already included in the changelist, the PRESUBMIT
1081 # for actions.xml will do a more complete presubmit check.
1082 return []
1084 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm'))
1085 action_re = r'[^a-zA-Z]UserMetricsAction\("([^"]*)'
1086 current_actions = None
1087 for f in input_api.AffectedFiles(file_filter=file_filter):
1088 for line_num, line in f.ChangedContents():
1089 match = input_api.re.search(action_re, line)
1090 if match:
1091 # Loads contents in tools/metrics/actions/actions.xml to memory. It's
1092 # loaded only once.
1093 if not current_actions:
1094 with open('tools/metrics/actions/actions.xml') as actions_f:
1095 current_actions = actions_f.read()
1096 # Search for the matched user action name in |current_actions|.
1097 for action_name in match.groups():
1098 action = 'name="{0}"'.format(action_name)
1099 if action not in current_actions:
1100 return [output_api.PresubmitPromptWarning(
1101 'File %s line %d: %s is missing in '
1102 'tools/metrics/actions/actions.xml. Please run '
1103 'tools/metrics/actions/extract_actions.py to update.'
1104 % (f.LocalPath(), line_num, action_name))]
1105 return []
1108 def _GetJSONParseError(input_api, filename, eat_comments=True):
1109 try:
1110 contents = input_api.ReadFile(filename)
1111 if eat_comments:
1112 json_comment_eater = input_api.os_path.join(
1113 input_api.PresubmitLocalPath(),
1114 'tools', 'json_comment_eater', 'json_comment_eater.py')
1115 process = input_api.subprocess.Popen(
1116 [input_api.python_executable, json_comment_eater],
1117 stdin=input_api.subprocess.PIPE,
1118 stdout=input_api.subprocess.PIPE,
1119 universal_newlines=True)
1120 (contents, _) = process.communicate(input=contents)
1122 input_api.json.loads(contents)
1123 except ValueError as e:
1124 return e
1125 return None
1128 def _GetIDLParseError(input_api, filename):
1129 try:
1130 contents = input_api.ReadFile(filename)
1131 idl_schema = input_api.os_path.join(
1132 input_api.PresubmitLocalPath(),
1133 'tools', 'json_schema_compiler', 'idl_schema.py')
1134 process = input_api.subprocess.Popen(
1135 [input_api.python_executable, idl_schema],
1136 stdin=input_api.subprocess.PIPE,
1137 stdout=input_api.subprocess.PIPE,
1138 stderr=input_api.subprocess.PIPE,
1139 universal_newlines=True)
1140 (_, error) = process.communicate(input=contents)
1141 return error or None
1142 except ValueError as e:
1143 return e
1146 def _CheckParseErrors(input_api, output_api):
1147 """Check that IDL and JSON files do not contain syntax errors."""
1148 actions = {
1149 '.idl': _GetIDLParseError,
1150 '.json': _GetJSONParseError,
1152 # These paths contain test data and other known invalid JSON files.
1153 excluded_patterns = [
1154 r'test[\\\/]data[\\\/]',
1155 r'^components[\\\/]policy[\\\/]resources[\\\/]policy_templates\.json$',
1157 # Most JSON files are preprocessed and support comments, but these do not.
1158 json_no_comments_patterns = [
1159 r'^testing[\\\/]',
1161 # Only run IDL checker on files in these directories.
1162 idl_included_patterns = [
1163 r'^chrome[\\\/]common[\\\/]extensions[\\\/]api[\\\/]',
1164 r'^extensions[\\\/]common[\\\/]api[\\\/]',
1167 def get_action(affected_file):
1168 filename = affected_file.LocalPath()
1169 return actions.get(input_api.os_path.splitext(filename)[1])
1171 def MatchesFile(patterns, path):
1172 for pattern in patterns:
1173 if input_api.re.search(pattern, path):
1174 return True
1175 return False
1177 def FilterFile(affected_file):
1178 action = get_action(affected_file)
1179 if not action:
1180 return False
1181 path = affected_file.LocalPath()
1183 if MatchesFile(excluded_patterns, path):
1184 return False
1186 if (action == _GetIDLParseError and
1187 not MatchesFile(idl_included_patterns, path)):
1188 return False
1189 return True
1191 results = []
1192 for affected_file in input_api.AffectedFiles(
1193 file_filter=FilterFile, include_deletes=False):
1194 action = get_action(affected_file)
1195 kwargs = {}
1196 if (action == _GetJSONParseError and
1197 MatchesFile(json_no_comments_patterns, affected_file.LocalPath())):
1198 kwargs['eat_comments'] = False
1199 parse_error = action(input_api,
1200 affected_file.AbsoluteLocalPath(),
1201 **kwargs)
1202 if parse_error:
1203 results.append(output_api.PresubmitError('%s could not be parsed: %s' %
1204 (affected_file.LocalPath(), parse_error)))
1205 return results
1208 def _CheckJavaStyle(input_api, output_api):
1209 """Runs checkstyle on changed java files and returns errors if any exist."""
1210 import sys
1211 original_sys_path = sys.path
1212 try:
1213 sys.path = sys.path + [input_api.os_path.join(
1214 input_api.PresubmitLocalPath(), 'tools', 'android', 'checkstyle')]
1215 import checkstyle
1216 finally:
1217 # Restore sys.path to what it was before.
1218 sys.path = original_sys_path
1220 return checkstyle.RunCheckstyle(
1221 input_api, output_api, 'tools/android/checkstyle/chromium-style-5.0.xml',
1222 black_list=_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST)
1225 def _CheckForCopyrightedCode(input_api, output_api):
1226 """Verifies that newly added code doesn't contain copyrighted material
1227 and is properly licensed under the standard Chromium license.
1229 As there can be false positives, we maintain a whitelist file. This check
1230 also verifies that the whitelist file is up to date.
1232 import sys
1233 original_sys_path = sys.path
1234 try:
1235 sys.path = sys.path + [input_api.os_path.join(
1236 input_api.PresubmitLocalPath(), 'android_webview', 'tools')]
1237 import copyright_scanner
1238 finally:
1239 # Restore sys.path to what it was before.
1240 sys.path = original_sys_path
1242 return copyright_scanner.ScanAtPresubmit(input_api, output_api)
1245 _DEPRECATED_CSS = [
1246 # Values
1247 ( "-webkit-box", "flex" ),
1248 ( "-webkit-inline-box", "inline-flex" ),
1249 ( "-webkit-flex", "flex" ),
1250 ( "-webkit-inline-flex", "inline-flex" ),
1251 ( "-webkit-min-content", "min-content" ),
1252 ( "-webkit-max-content", "max-content" ),
1254 # Properties
1255 ( "-webkit-background-clip", "background-clip" ),
1256 ( "-webkit-background-origin", "background-origin" ),
1257 ( "-webkit-background-size", "background-size" ),
1258 ( "-webkit-box-shadow", "box-shadow" ),
1260 # Functions
1261 ( "-webkit-gradient", "gradient" ),
1262 ( "-webkit-repeating-gradient", "repeating-gradient" ),
1263 ( "-webkit-linear-gradient", "linear-gradient" ),
1264 ( "-webkit-repeating-linear-gradient", "repeating-linear-gradient" ),
1265 ( "-webkit-radial-gradient", "radial-gradient" ),
1266 ( "-webkit-repeating-radial-gradient", "repeating-radial-gradient" ),
1269 def _CheckNoDeprecatedCSS(input_api, output_api):
1270 """ Make sure that we don't use deprecated CSS
1271 properties, functions or values. Our external
1272 documentation is ignored by the hooks as it
1273 needs to be consumed by WebKit. """
1274 results = []
1275 file_inclusion_pattern = (r".+\.css$",)
1276 black_list = (_EXCLUDED_PATHS +
1277 _TEST_CODE_EXCLUDED_PATHS +
1278 input_api.DEFAULT_BLACK_LIST +
1279 (r"^chrome/common/extensions/docs",
1280 r"^chrome/docs",
1281 r"^native_client_sdk"))
1282 file_filter = lambda f: input_api.FilterSourceFile(
1283 f, white_list=file_inclusion_pattern, black_list=black_list)
1284 for fpath in input_api.AffectedFiles(file_filter=file_filter):
1285 for line_num, line in fpath.ChangedContents():
1286 for (deprecated_value, value) in _DEPRECATED_CSS:
1287 if deprecated_value in line:
1288 results.append(output_api.PresubmitError(
1289 "%s:%d: Use of deprecated CSS %s, use %s instead" %
1290 (fpath.LocalPath(), line_num, deprecated_value, value)))
1291 return results
1294 _DEPRECATED_JS = [
1295 ( "__lookupGetter__", "Object.getOwnPropertyDescriptor" ),
1296 ( "__defineGetter__", "Object.defineProperty" ),
1297 ( "__defineSetter__", "Object.defineProperty" ),
1300 def _CheckNoDeprecatedJS(input_api, output_api):
1301 """Make sure that we don't use deprecated JS in Chrome code."""
1302 results = []
1303 file_inclusion_pattern = (r".+\.js$",) # TODO(dbeam): .html?
1304 black_list = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS +
1305 input_api.DEFAULT_BLACK_LIST)
1306 file_filter = lambda f: input_api.FilterSourceFile(
1307 f, white_list=file_inclusion_pattern, black_list=black_list)
1308 for fpath in input_api.AffectedFiles(file_filter=file_filter):
1309 for lnum, line in fpath.ChangedContents():
1310 for (deprecated, replacement) in _DEPRECATED_JS:
1311 if deprecated in line:
1312 results.append(output_api.PresubmitError(
1313 "%s:%d: Use of deprecated JS %s, use %s instead" %
1314 (fpath.LocalPath(), lnum, deprecated, replacement)))
1315 return results
1318 def _CommonChecks(input_api, output_api):
1319 """Checks common to both upload and commit."""
1320 results = []
1321 results.extend(input_api.canned_checks.PanProjectChecks(
1322 input_api, output_api,
1323 excluded_paths=_EXCLUDED_PATHS + _TESTRUNNER_PATHS))
1324 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
1325 results.extend(
1326 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
1327 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
1328 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
1329 results.extend(_CheckNoNewWStrings(input_api, output_api))
1330 results.extend(_CheckNoDEPSGIT(input_api, output_api))
1331 results.extend(_CheckNoBannedFunctions(input_api, output_api))
1332 results.extend(_CheckNoPragmaOnce(input_api, output_api))
1333 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
1334 results.extend(_CheckUnwantedDependencies(input_api, output_api))
1335 results.extend(_CheckFilePermissions(input_api, output_api))
1336 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
1337 results.extend(_CheckIncludeOrder(input_api, output_api))
1338 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
1339 results.extend(_CheckPatchFiles(input_api, output_api))
1340 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
1341 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
1342 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
1343 results.extend(_CheckForInvalidIfDefinedMacros(input_api, output_api))
1344 # TODO(danakj): Remove this when base/move.h is removed.
1345 results.extend(_CheckForUsingSideEffectsOfPass(input_api, output_api))
1346 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
1347 results.extend(
1348 input_api.canned_checks.CheckChangeHasNoTabs(
1349 input_api,
1350 output_api,
1351 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
1352 results.extend(_CheckSpamLogging(input_api, output_api))
1353 results.extend(_CheckForAnonymousVariables(input_api, output_api))
1354 results.extend(_CheckCygwinShell(input_api, output_api))
1355 results.extend(_CheckUserActionUpdate(input_api, output_api))
1356 results.extend(_CheckNoDeprecatedCSS(input_api, output_api))
1357 results.extend(_CheckNoDeprecatedJS(input_api, output_api))
1358 results.extend(_CheckParseErrors(input_api, output_api))
1359 results.extend(_CheckForIPCRules(input_api, output_api))
1360 results.extend(_CheckForCopyrightedCode(input_api, output_api))
1361 results.extend(_CheckForWindowsLineEndings(input_api, output_api))
1363 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
1364 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
1365 input_api, output_api,
1366 input_api.PresubmitLocalPath(),
1367 whitelist=[r'^PRESUBMIT_test\.py$']))
1368 return results
1371 def _CheckAuthorizedAuthor(input_api, output_api):
1372 """For non-googler/chromites committers, verify the author's email address is
1373 in AUTHORS.
1375 # TODO(maruel): Add it to input_api?
1376 import fnmatch
1378 author = input_api.change.author_email
1379 if not author:
1380 input_api.logging.info('No author, skipping AUTHOR check')
1381 return []
1382 authors_path = input_api.os_path.join(
1383 input_api.PresubmitLocalPath(), 'AUTHORS')
1384 valid_authors = (
1385 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
1386 for line in open(authors_path))
1387 valid_authors = [item.group(1).lower() for item in valid_authors if item]
1388 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
1389 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
1390 return [output_api.PresubmitPromptWarning(
1391 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
1392 '\n'
1393 'http://www.chromium.org/developers/contributing-code and read the '
1394 '"Legal" section\n'
1395 'If you are a chromite, verify the contributor signed the CLA.') %
1396 author)]
1397 return []
1400 def _CheckPatchFiles(input_api, output_api):
1401 problems = [f.LocalPath() for f in input_api.AffectedFiles()
1402 if f.LocalPath().endswith(('.orig', '.rej'))]
1403 if problems:
1404 return [output_api.PresubmitError(
1405 "Don't commit .rej and .orig files.", problems)]
1406 else:
1407 return []
1410 def _DidYouMeanOSMacro(bad_macro):
1411 try:
1412 return {'A': 'OS_ANDROID',
1413 'B': 'OS_BSD',
1414 'C': 'OS_CHROMEOS',
1415 'F': 'OS_FREEBSD',
1416 'L': 'OS_LINUX',
1417 'M': 'OS_MACOSX',
1418 'N': 'OS_NACL',
1419 'O': 'OS_OPENBSD',
1420 'P': 'OS_POSIX',
1421 'S': 'OS_SOLARIS',
1422 'W': 'OS_WIN'}[bad_macro[3].upper()]
1423 except KeyError:
1424 return ''
1427 def _CheckForInvalidOSMacrosInFile(input_api, f):
1428 """Check for sensible looking, totally invalid OS macros."""
1429 preprocessor_statement = input_api.re.compile(r'^\s*#')
1430 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
1431 results = []
1432 for lnum, line in f.ChangedContents():
1433 if preprocessor_statement.search(line):
1434 for match in os_macro.finditer(line):
1435 if not match.group(1) in _VALID_OS_MACROS:
1436 good = _DidYouMeanOSMacro(match.group(1))
1437 did_you_mean = ' (did you mean %s?)' % good if good else ''
1438 results.append(' %s:%d %s%s' % (f.LocalPath(),
1439 lnum,
1440 match.group(1),
1441 did_you_mean))
1442 return results
1445 def _CheckForInvalidOSMacros(input_api, output_api):
1446 """Check all affected files for invalid OS macros."""
1447 bad_macros = []
1448 for f in input_api.AffectedFiles():
1449 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
1450 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
1452 if not bad_macros:
1453 return []
1455 return [output_api.PresubmitError(
1456 'Possibly invalid OS macro[s] found. Please fix your code\n'
1457 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
1460 def _CheckForInvalidIfDefinedMacrosInFile(input_api, f):
1461 """Check all affected files for invalid "if defined" macros."""
1462 ALWAYS_DEFINED_MACROS = (
1463 "TARGET_CPU_PPC",
1464 "TARGET_CPU_PPC64",
1465 "TARGET_CPU_68K",
1466 "TARGET_CPU_X86",
1467 "TARGET_CPU_ARM",
1468 "TARGET_CPU_MIPS",
1469 "TARGET_CPU_SPARC",
1470 "TARGET_CPU_ALPHA",
1471 "TARGET_IPHONE_SIMULATOR",
1472 "TARGET_OS_EMBEDDED",
1473 "TARGET_OS_IPHONE",
1474 "TARGET_OS_MAC",
1475 "TARGET_OS_UNIX",
1476 "TARGET_OS_WIN32",
1478 ifdef_macro = input_api.re.compile(r'^\s*#.*(?:ifdef\s|defined\()([^\s\)]+)')
1479 results = []
1480 for lnum, line in f.ChangedContents():
1481 for match in ifdef_macro.finditer(line):
1482 if match.group(1) in ALWAYS_DEFINED_MACROS:
1483 always_defined = ' %s is always defined. ' % match.group(1)
1484 did_you_mean = 'Did you mean \'#if %s\'?' % match.group(1)
1485 results.append(' %s:%d %s\n\t%s' % (f.LocalPath(),
1486 lnum,
1487 always_defined,
1488 did_you_mean))
1489 return results
1492 def _CheckForInvalidIfDefinedMacros(input_api, output_api):
1493 """Check all affected files for invalid "if defined" macros."""
1494 bad_macros = []
1495 for f in input_api.AffectedFiles():
1496 if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
1497 bad_macros.extend(_CheckForInvalidIfDefinedMacrosInFile(input_api, f))
1499 if not bad_macros:
1500 return []
1502 return [output_api.PresubmitError(
1503 'Found ifdef check on always-defined macro[s]. Please fix your code\n'
1504 'or check the list of ALWAYS_DEFINED_MACROS in src/PRESUBMIT.py.',
1505 bad_macros)]
1508 def _CheckForUsingSideEffectsOfPass(input_api, output_api):
1509 """Check all affected files for using side effects of Pass."""
1510 errors = []
1511 for f in input_api.AffectedFiles():
1512 if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
1513 for lnum, line in f.ChangedContents():
1514 # Disallow Foo(*my_scoped_thing.Pass()); See crbug.com/418297.
1515 if input_api.re.search(r'\*[a-zA-Z0-9_]+\.Pass\(\)', line):
1516 errors.append(output_api.PresubmitError(
1517 ('%s:%d uses *foo.Pass() to delete the contents of scoped_ptr. ' +
1518 'See crbug.com/418297.') % (f.LocalPath(), lnum)))
1519 return errors
1522 def _CheckForIPCRules(input_api, output_api):
1523 """Check for same IPC rules described in
1524 http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc
1526 base_pattern = r'IPC_ENUM_TRAITS\('
1527 inclusion_pattern = input_api.re.compile(r'(%s)' % base_pattern)
1528 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_pattern)
1530 problems = []
1531 for f in input_api.AffectedSourceFiles(None):
1532 local_path = f.LocalPath()
1533 if not local_path.endswith('.h'):
1534 continue
1535 for line_number, line in f.ChangedContents():
1536 if inclusion_pattern.search(line) and not comment_pattern.search(line):
1537 problems.append(
1538 '%s:%d\n %s' % (local_path, line_number, line.strip()))
1540 if problems:
1541 return [output_api.PresubmitPromptWarning(
1542 _IPC_ENUM_TRAITS_DEPRECATED, problems)]
1543 else:
1544 return []
1547 def _CheckForWindowsLineEndings(input_api, output_api):
1548 """Check source code and known ascii text files for Windows style line
1549 endings.
1551 known_text_files = r'.*\.(txt|html|htm|mhtml|py)$'
1553 file_inclusion_pattern = (
1554 known_text_files,
1555 r'.+%s' % _IMPLEMENTATION_EXTENSIONS
1558 filter = lambda f: input_api.FilterSourceFile(
1559 f, white_list=file_inclusion_pattern, black_list=None)
1560 files = [f.LocalPath() for f in
1561 input_api.AffectedSourceFiles(filter)]
1563 problems = []
1565 for file in files:
1566 fp = open(file, 'r')
1567 for line in fp:
1568 if line.endswith('\r\n'):
1569 problems.append(file)
1570 break
1571 fp.close()
1573 if problems:
1574 return [output_api.PresubmitPromptWarning('Are you sure that you want '
1575 'these files to contain Windows style line endings?\n' +
1576 '\n'.join(problems))]
1578 return []
1581 def CheckChangeOnUpload(input_api, output_api):
1582 results = []
1583 results.extend(_CommonChecks(input_api, output_api))
1584 results.extend(_CheckValidHostsInDEPS(input_api, output_api))
1585 results.extend(_CheckJavaStyle(input_api, output_api))
1586 results.extend(
1587 input_api.canned_checks.CheckGNFormatted(input_api, output_api))
1588 return results
1591 def GetTryServerMasterForBot(bot):
1592 """Returns the Try Server master for the given bot.
1594 It tries to guess the master from the bot name, but may still fail
1595 and return None. There is no longer a default master.
1597 # Potentially ambiguous bot names are listed explicitly.
1598 master_map = {
1599 'win_gpu': 'tryserver.chromium.gpu',
1600 'chromium_presubmit': 'tryserver.chromium.linux',
1601 'blink_presubmit': 'tryserver.chromium.linux',
1602 'tools_build_presubmit': 'tryserver.chromium.linux',
1604 master = master_map.get(bot)
1605 if not master:
1606 if 'gpu' in bot:
1607 master = 'tryserver.chromium.gpu'
1608 elif 'linux' in bot or 'android' in bot or 'presubmit' in bot:
1609 master = 'tryserver.chromium.linux'
1610 elif 'win' in bot:
1611 master = 'tryserver.chromium.win'
1612 elif 'mac' in bot or 'ios' in bot:
1613 master = 'tryserver.chromium.mac'
1614 return master
1617 def GetDefaultTryConfigs(bots):
1618 """Returns a list of ('bot', set(['tests']), filtered by [bots].
1621 builders_and_tests = dict((bot, set(['defaulttests'])) for bot in bots)
1623 # Build up the mapping from tryserver master to bot/test.
1624 out = dict()
1625 for bot, tests in builders_and_tests.iteritems():
1626 out.setdefault(GetTryServerMasterForBot(bot), {})[bot] = tests
1627 return out
1630 def CheckChangeOnCommit(input_api, output_api):
1631 results = []
1632 results.extend(_CommonChecks(input_api, output_api))
1633 # TODO(thestig) temporarily disabled, doesn't work in third_party/
1634 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
1635 # input_api, output_api, sources))
1636 # Make sure the tree is 'open'.
1637 results.extend(input_api.canned_checks.CheckTreeIsOpen(
1638 input_api,
1639 output_api,
1640 json_url='http://chromium-status.appspot.com/current?format=json'))
1642 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1643 input_api, output_api))
1644 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1645 input_api, output_api))
1646 return results
1649 def GetPreferredTryMasters(project, change):
1650 import re
1651 files = change.LocalPaths()
1653 if not files or all(re.search(r'[\\\/]OWNERS$', f) for f in files):
1654 return {}
1656 if all(re.search(r'\.(m|mm)$|(^|[\\\/_])mac[\\\/_.]', f) for f in files):
1657 return GetDefaultTryConfigs([
1658 'mac_chromium_compile_dbg_ng',
1659 'mac_chromium_rel_ng',
1661 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
1662 return GetDefaultTryConfigs([
1663 'win8_chromium_rel',
1664 'win_chromium_rel_ng',
1665 'win_chromium_x64_rel_ng',
1667 if all(re.search(r'(^|[\\\/_])android[\\\/_.]', f) and
1668 not re.search(r'(^|[\\\/_])devtools[\\\/_.]', f) for f in files):
1669 return GetDefaultTryConfigs([
1670 'android_aosp',
1671 'android_dbg_tests_recipe',
1673 if all(re.search(r'[\\\/_]ios[\\\/_.]', f) for f in files):
1674 return GetDefaultTryConfigs(['ios_rel_device', 'ios_dbg_simulator'])
1676 import os
1677 import json
1678 with open(os.path.join(
1679 change.RepositoryRoot(), 'testing', 'commit_queue', 'config.json')) as f:
1680 cq_config = json.load(f)
1681 cq_trybots = cq_config.get('trybots', {})
1682 builders = cq_trybots.get('launched', {})
1683 for master, master_config in cq_trybots.get('triggered', {}).iteritems():
1684 for triggered_bot in master_config:
1685 builders.get(master, {}).pop(triggered_bot, None)
1687 # Explicitly iterate over copies of dicts since we mutate them.
1688 for master in builders.keys():
1689 for builder in builders[master].keys():
1690 # Do not trigger presubmit builders, since they're likely to fail
1691 # (e.g. OWNERS checks before finished code review), and we're
1692 # running local presubmit anyway.
1693 if 'presubmit' in builder:
1694 builders[master].pop(builder)
1696 # Match things like path/aura/file.cc and path/file_aura.cc.
1697 # Same for chromeos.
1698 if any(re.search(r'[\\\/_](aura|chromeos)', f) for f in files):
1699 tryserver_linux = builders.setdefault('tryserver.chromium.linux', {})
1700 tryserver_linux['linux_chromium_chromeos_asan_rel_ng'] = ['defaulttests']
1702 return builders