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