Unbreak and re-enable PolicyPrefIndicatorTest on official builds
[chromium-blink-merge.git] / PRESUBMIT.py
blob99c02609141f314e3bafe0d40bb9049826bf28db
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. Remember to use the right '
66 'collation (LC_COLLATE=C) and check https://google-styleguide.googlecode'
67 '.com/svn/trunk/cppguide.html#Names_and_Order_of_Includes')
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[\\\/]drm[\\\/]host[\\\/]"
178 "drm_native_display_delegate\.cc$",
182 'SkRefPtr',
184 'The use of SkRefPtr is prohibited. ',
185 'Please use skia::RefPtr instead.'
187 True,
191 'SkAutoRef',
193 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
194 'Please use skia::RefPtr instead.'
196 True,
200 'SkAutoTUnref',
202 'The use of SkAutoTUnref is dangerous because it implicitly ',
203 'converts to a raw pointer. Please use skia::RefPtr instead.'
205 True,
209 'SkAutoUnref',
211 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
212 'because it implicitly converts to a raw pointer. ',
213 'Please use skia::RefPtr instead.'
215 True,
219 r'/HANDLE_EINTR\(.*close',
221 'HANDLE_EINTR(close) is invalid. If close fails with EINTR, the file',
222 'descriptor will be closed, and it is incorrect to retry the close.',
223 'Either call close directly and ignore its return value, or wrap close',
224 'in IGNORE_EINTR to use its return value. See http://crbug.com/269623'
226 True,
230 r'/IGNORE_EINTR\((?!.*close)',
232 'IGNORE_EINTR is only valid when wrapping close. To wrap other system',
233 'calls, use HANDLE_EINTR. See http://crbug.com/269623',
235 True,
237 # Files that #define IGNORE_EINTR.
238 r'^base[\\\/]posix[\\\/]eintr_wrapper\.h$',
239 r'^ppapi[\\\/]tests[\\\/]test_broker\.cc$',
243 r'/v8::Extension\(',
245 'Do not introduce new v8::Extensions into the code base, use',
246 'gin::Wrappable instead. See http://crbug.com/334679',
248 True,
250 r'extensions[\\\/]renderer[\\\/]safe_builtins\.*',
254 'MessageLoopProxy',
256 'MessageLoopProxy is deprecated. ',
257 'Please use SingleThreadTaskRunner or ThreadTaskRunnerHandle instead.'
259 True,
264 _IPC_ENUM_TRAITS_DEPRECATED = (
265 'You are using IPC_ENUM_TRAITS() in your code. It has been deprecated.\n'
266 'See http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc')
269 _VALID_OS_MACROS = (
270 # Please keep sorted.
271 'OS_ANDROID',
272 'OS_ANDROID_HOST',
273 'OS_BSD',
274 'OS_CAT', # For testing.
275 'OS_CHROMEOS',
276 'OS_FREEBSD',
277 'OS_IOS',
278 'OS_LINUX',
279 'OS_MACOSX',
280 'OS_NACL',
281 'OS_NACL_NONSFI',
282 'OS_NACL_SFI',
283 'OS_OPENBSD',
284 'OS_POSIX',
285 'OS_QNX',
286 'OS_SOLARIS',
287 'OS_WIN',
291 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
292 """Attempts to prevent use of functions intended only for testing in
293 non-testing code. For now this is just a best-effort implementation
294 that ignores header files and may have some false positives. A
295 better implementation would probably need a proper C++ parser.
297 # We only scan .cc files and the like, as the declaration of
298 # for-testing functions in header files are hard to distinguish from
299 # calls to such functions without a proper C++ parser.
300 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
302 base_function_pattern = r'[ :]test::[^\s]+|ForTest(ing)?|for_test(ing)?'
303 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
304 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern)
305 exclusion_pattern = input_api.re.compile(
306 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
307 base_function_pattern, base_function_pattern))
309 def FilterFile(affected_file):
310 black_list = (_EXCLUDED_PATHS +
311 _TEST_CODE_EXCLUDED_PATHS +
312 input_api.DEFAULT_BLACK_LIST)
313 return input_api.FilterSourceFile(
314 affected_file,
315 white_list=(file_inclusion_pattern, ),
316 black_list=black_list)
318 problems = []
319 for f in input_api.AffectedSourceFiles(FilterFile):
320 local_path = f.LocalPath()
321 for line_number, line in f.ChangedContents():
322 if (inclusion_pattern.search(line) and
323 not comment_pattern.search(line) and
324 not exclusion_pattern.search(line)):
325 problems.append(
326 '%s:%d\n %s' % (local_path, line_number, line.strip()))
328 if problems:
329 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
330 else:
331 return []
334 def _CheckNoIOStreamInHeaders(input_api, output_api):
335 """Checks to make sure no .h files include <iostream>."""
336 files = []
337 pattern = input_api.re.compile(r'^#include\s*<iostream>',
338 input_api.re.MULTILINE)
339 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
340 if not f.LocalPath().endswith('.h'):
341 continue
342 contents = input_api.ReadFile(f)
343 if pattern.search(contents):
344 files.append(f)
346 if len(files):
347 return [ output_api.PresubmitError(
348 'Do not #include <iostream> in header files, since it inserts static '
349 'initialization into every file including the header. Instead, '
350 '#include <ostream>. See http://crbug.com/94794',
351 files) ]
352 return []
355 def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
356 """Checks to make sure no source files use UNIT_TEST"""
357 problems = []
358 for f in input_api.AffectedFiles():
359 if (not f.LocalPath().endswith(('.cc', '.mm'))):
360 continue
362 for line_num, line in f.ChangedContents():
363 if 'UNIT_TEST ' in line or line.endswith('UNIT_TEST'):
364 problems.append(' %s:%d' % (f.LocalPath(), line_num))
366 if not problems:
367 return []
368 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
369 '\n'.join(problems))]
372 def _FindHistogramNameInLine(histogram_name, line):
373 """Tries to find a histogram name or prefix in a line."""
374 if not "affected-histogram" in line:
375 return histogram_name in line
376 # A histogram_suffixes tag type has an affected-histogram name as a prefix of
377 # the histogram_name.
378 if not '"' in line:
379 return False
380 histogram_prefix = line.split('\"')[1]
381 return histogram_prefix in histogram_name
384 def _CheckUmaHistogramChanges(input_api, output_api):
385 """Check that UMA histogram names in touched lines can still be found in other
386 lines of the patch or in histograms.xml. Note that this check would not catch
387 the reverse: changes in histograms.xml not matched in the code itself."""
388 touched_histograms = []
389 histograms_xml_modifications = []
390 pattern = input_api.re.compile('UMA_HISTOGRAM.*\("(.*)"')
391 for f in input_api.AffectedFiles():
392 # If histograms.xml itself is modified, keep the modified lines for later.
393 if f.LocalPath().endswith(('histograms.xml')):
394 histograms_xml_modifications = f.ChangedContents()
395 continue
396 if not f.LocalPath().endswith(('cc', 'mm', 'cpp')):
397 continue
398 for line_num, line in f.ChangedContents():
399 found = pattern.search(line)
400 if found:
401 touched_histograms.append([found.group(1), f, line_num])
403 # Search for the touched histogram names in the local modifications to
404 # histograms.xml, and, if not found, on the base histograms.xml file.
405 unmatched_histograms = []
406 for histogram_info in touched_histograms:
407 histogram_name_found = False
408 for line_num, line in histograms_xml_modifications:
409 histogram_name_found = _FindHistogramNameInLine(histogram_info[0], line)
410 if histogram_name_found:
411 break
412 if not histogram_name_found:
413 unmatched_histograms.append(histogram_info)
415 histograms_xml_path = 'tools/metrics/histograms/histograms.xml'
416 problems = []
417 if unmatched_histograms:
418 with open(histograms_xml_path) as histograms_xml:
419 for histogram_name, f, line_num in unmatched_histograms:
420 histograms_xml.seek(0)
421 histogram_name_found = False
422 for line in histograms_xml:
423 histogram_name_found = _FindHistogramNameInLine(histogram_name, line)
424 if histogram_name_found:
425 break
426 if not histogram_name_found:
427 problems.append(' [%s:%d] %s' %
428 (f.LocalPath(), line_num, histogram_name))
430 if not problems:
431 return []
432 return [output_api.PresubmitPromptWarning('Some UMA_HISTOGRAM lines have '
433 'been modified and the associated histogram name has no match in either '
434 '%s or the modifications of it:' % (histograms_xml_path), problems)]
437 def _CheckNoNewWStrings(input_api, output_api):
438 """Checks to make sure we don't introduce use of wstrings."""
439 problems = []
440 for f in input_api.AffectedFiles():
441 if (not f.LocalPath().endswith(('.cc', '.h')) or
442 f.LocalPath().endswith(('test.cc', '_win.cc', '_win.h')) or
443 '/win/' in f.LocalPath()):
444 continue
446 allowWString = False
447 for line_num, line in f.ChangedContents():
448 if 'presubmit: allow wstring' in line:
449 allowWString = True
450 elif not allowWString and 'wstring' in line:
451 problems.append(' %s:%d' % (f.LocalPath(), line_num))
452 allowWString = False
453 else:
454 allowWString = False
456 if not problems:
457 return []
458 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
459 ' If you are calling a cross-platform API that accepts a wstring, '
460 'fix the API.\n' +
461 '\n'.join(problems))]
464 def _CheckNoDEPSGIT(input_api, output_api):
465 """Make sure .DEPS.git is never modified manually."""
466 if any(f.LocalPath().endswith('.DEPS.git') for f in
467 input_api.AffectedFiles()):
468 return [output_api.PresubmitError(
469 'Never commit changes to .DEPS.git. This file is maintained by an\n'
470 'automated system based on what\'s in DEPS and your changes will be\n'
471 'overwritten.\n'
472 'See https://sites.google.com/a/chromium.org/dev/developers/how-tos/get-the-code#Rolling_DEPS\n'
473 'for more information')]
474 return []
477 def _CheckValidHostsInDEPS(input_api, output_api):
478 """Checks that DEPS file deps are from allowed_hosts."""
479 # Run only if DEPS file has been modified to annoy fewer bystanders.
480 if all(f.LocalPath() != 'DEPS' for f in input_api.AffectedFiles()):
481 return []
482 # Outsource work to gclient verify
483 try:
484 input_api.subprocess.check_output(['gclient', 'verify'])
485 return []
486 except input_api.subprocess.CalledProcessError, error:
487 return [output_api.PresubmitError(
488 'DEPS file must have only git dependencies.',
489 long_text=error.output)]
492 def _CheckNoBannedFunctions(input_api, output_api):
493 """Make sure that banned functions are not used."""
494 warnings = []
495 errors = []
497 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
498 for f in input_api.AffectedFiles(file_filter=file_filter):
499 for line_num, line in f.ChangedContents():
500 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
501 matched = False
502 if func_name[0:1] == '/':
503 regex = func_name[1:]
504 if input_api.re.search(regex, line):
505 matched = True
506 elif func_name in line:
507 matched = True
508 if matched:
509 problems = warnings;
510 if error:
511 problems = errors;
512 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
513 for message_line in message:
514 problems.append(' %s' % message_line)
516 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
517 for f in input_api.AffectedFiles(file_filter=file_filter):
518 for line_num, line in f.ChangedContents():
519 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
520 def IsBlacklisted(affected_file, blacklist):
521 local_path = affected_file.LocalPath()
522 for item in blacklist:
523 if input_api.re.match(item, local_path):
524 return True
525 return False
526 if IsBlacklisted(f, excluded_paths):
527 continue
528 matched = False
529 if func_name[0:1] == '/':
530 regex = func_name[1:]
531 if input_api.re.search(regex, line):
532 matched = True
533 elif func_name in line:
534 matched = True
535 if matched:
536 problems = warnings;
537 if error:
538 problems = errors;
539 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
540 for message_line in message:
541 problems.append(' %s' % message_line)
543 result = []
544 if (warnings):
545 result.append(output_api.PresubmitPromptWarning(
546 'Banned functions were used.\n' + '\n'.join(warnings)))
547 if (errors):
548 result.append(output_api.PresubmitError(
549 'Banned functions were used.\n' + '\n'.join(errors)))
550 return result
553 def _CheckNoPragmaOnce(input_api, output_api):
554 """Make sure that banned functions are not used."""
555 files = []
556 pattern = input_api.re.compile(r'^#pragma\s+once',
557 input_api.re.MULTILINE)
558 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
559 if not f.LocalPath().endswith('.h'):
560 continue
561 contents = input_api.ReadFile(f)
562 if pattern.search(contents):
563 files.append(f)
565 if files:
566 return [output_api.PresubmitError(
567 'Do not use #pragma once in header files.\n'
568 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
569 files)]
570 return []
573 def _CheckNoTrinaryTrueFalse(input_api, output_api):
574 """Checks to make sure we don't introduce use of foo ? true : false."""
575 problems = []
576 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
577 for f in input_api.AffectedFiles():
578 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
579 continue
581 for line_num, line in f.ChangedContents():
582 if pattern.match(line):
583 problems.append(' %s:%d' % (f.LocalPath(), line_num))
585 if not problems:
586 return []
587 return [output_api.PresubmitPromptWarning(
588 'Please consider avoiding the "? true : false" pattern if possible.\n' +
589 '\n'.join(problems))]
592 def _CheckUnwantedDependencies(input_api, output_api):
593 """Runs checkdeps on #include statements added in this
594 change. Breaking - rules is an error, breaking ! rules is a
595 warning.
597 import sys
598 # We need to wait until we have an input_api object and use this
599 # roundabout construct to import checkdeps because this file is
600 # eval-ed and thus doesn't have __file__.
601 original_sys_path = sys.path
602 try:
603 sys.path = sys.path + [input_api.os_path.join(
604 input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
605 import checkdeps
606 from cpp_checker import CppChecker
607 from rules import Rule
608 finally:
609 # Restore sys.path to what it was before.
610 sys.path = original_sys_path
612 added_includes = []
613 for f in input_api.AffectedFiles():
614 if not CppChecker.IsCppFile(f.LocalPath()):
615 continue
617 changed_lines = [line for line_num, line in f.ChangedContents()]
618 added_includes.append([f.LocalPath(), changed_lines])
620 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
622 error_descriptions = []
623 warning_descriptions = []
624 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
625 added_includes):
626 description_with_path = '%s\n %s' % (path, rule_description)
627 if rule_type == Rule.DISALLOW:
628 error_descriptions.append(description_with_path)
629 else:
630 warning_descriptions.append(description_with_path)
632 results = []
633 if error_descriptions:
634 results.append(output_api.PresubmitError(
635 'You added one or more #includes that violate checkdeps rules.',
636 error_descriptions))
637 if warning_descriptions:
638 results.append(output_api.PresubmitPromptOrNotify(
639 'You added one or more #includes of files that are temporarily\n'
640 'allowed but being removed. Can you avoid introducing the\n'
641 '#include? See relevant DEPS file(s) for details and contacts.',
642 warning_descriptions))
643 return results
646 def _CheckFilePermissions(input_api, output_api):
647 """Check that all files have their permissions properly set."""
648 if input_api.platform == 'win32':
649 return []
650 args = [input_api.python_executable, 'tools/checkperms/checkperms.py',
651 '--root', input_api.change.RepositoryRoot()]
652 for f in input_api.AffectedFiles():
653 args += ['--file', f.LocalPath()]
654 checkperms = input_api.subprocess.Popen(args,
655 stdout=input_api.subprocess.PIPE)
656 errors = checkperms.communicate()[0].strip()
657 if errors:
658 return [output_api.PresubmitError('checkperms.py failed.',
659 errors.splitlines())]
660 return []
663 def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
664 """Makes sure we don't include ui/aura/window_property.h
665 in header files.
667 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
668 errors = []
669 for f in input_api.AffectedFiles():
670 if not f.LocalPath().endswith('.h'):
671 continue
672 for line_num, line in f.ChangedContents():
673 if pattern.match(line):
674 errors.append(' %s:%d' % (f.LocalPath(), line_num))
676 results = []
677 if errors:
678 results.append(output_api.PresubmitError(
679 'Header files should not include ui/aura/window_property.h', errors))
680 return results
683 def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
684 """Checks that the lines in scope occur in the right order.
686 1. C system files in alphabetical order
687 2. C++ system files in alphabetical order
688 3. Project's .h files
691 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
692 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
693 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
695 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
697 state = C_SYSTEM_INCLUDES
699 previous_line = ''
700 previous_line_num = 0
701 problem_linenums = []
702 for line_num, line in scope:
703 if c_system_include_pattern.match(line):
704 if state != C_SYSTEM_INCLUDES:
705 problem_linenums.append((line_num, previous_line_num))
706 elif previous_line and previous_line > line:
707 problem_linenums.append((line_num, previous_line_num))
708 elif cpp_system_include_pattern.match(line):
709 if state == C_SYSTEM_INCLUDES:
710 state = CPP_SYSTEM_INCLUDES
711 elif state == CUSTOM_INCLUDES:
712 problem_linenums.append((line_num, previous_line_num))
713 elif previous_line and previous_line > line:
714 problem_linenums.append((line_num, previous_line_num))
715 elif custom_include_pattern.match(line):
716 if state != CUSTOM_INCLUDES:
717 state = CUSTOM_INCLUDES
718 elif previous_line and previous_line > line:
719 problem_linenums.append((line_num, previous_line_num))
720 else:
721 problem_linenums.append(line_num)
722 previous_line = line
723 previous_line_num = line_num
725 warnings = []
726 for (line_num, previous_line_num) in problem_linenums:
727 if line_num in changed_linenums or previous_line_num in changed_linenums:
728 warnings.append(' %s:%d' % (file_path, line_num))
729 return warnings
732 def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
733 """Checks the #include order for the given file f."""
735 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
736 # Exclude the following includes from the check:
737 # 1) #include <.../...>, e.g., <sys/...> includes often need to appear in a
738 # specific order.
739 # 2) <atlbase.h>, "build/build_config.h"
740 excluded_include_pattern = input_api.re.compile(
741 r'\s*#include (\<.*/.*|\<atlbase\.h\>|"build/build_config.h")')
742 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
743 # Match the final or penultimate token if it is xxxtest so we can ignore it
744 # when considering the special first include.
745 test_file_tag_pattern = input_api.re.compile(
746 r'_[a-z]+test(?=(_[a-zA-Z0-9]+)?\.)')
747 if_pattern = input_api.re.compile(
748 r'\s*#\s*(if|elif|else|endif|define|undef).*')
749 # Some files need specialized order of includes; exclude such files from this
750 # check.
751 uncheckable_includes_pattern = input_api.re.compile(
752 r'\s*#include '
753 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
755 contents = f.NewContents()
756 warnings = []
757 line_num = 0
759 # Handle the special first include. If the first include file is
760 # some/path/file.h, the corresponding including file can be some/path/file.cc,
761 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
762 # etc. It's also possible that no special first include exists.
763 # If the included file is some/path/file_platform.h the including file could
764 # also be some/path/file_xxxtest_platform.h.
765 including_file_base_name = test_file_tag_pattern.sub(
766 '', input_api.os_path.basename(f.LocalPath()))
768 for line in contents:
769 line_num += 1
770 if system_include_pattern.match(line):
771 # No special first include -> process the line again along with normal
772 # includes.
773 line_num -= 1
774 break
775 match = custom_include_pattern.match(line)
776 if match:
777 match_dict = match.groupdict()
778 header_basename = test_file_tag_pattern.sub(
779 '', input_api.os_path.basename(match_dict['FILE'])).replace('.h', '')
781 if header_basename not in including_file_base_name:
782 # No special first include -> process the line again along with normal
783 # includes.
784 line_num -= 1
785 break
787 # Split into scopes: Each region between #if and #endif is its own scope.
788 scopes = []
789 current_scope = []
790 for line in contents[line_num:]:
791 line_num += 1
792 if uncheckable_includes_pattern.match(line):
793 continue
794 if if_pattern.match(line):
795 scopes.append(current_scope)
796 current_scope = []
797 elif ((system_include_pattern.match(line) or
798 custom_include_pattern.match(line)) and
799 not excluded_include_pattern.match(line)):
800 current_scope.append((line_num, line))
801 scopes.append(current_scope)
803 for scope in scopes:
804 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
805 changed_linenums))
806 return warnings
809 def _CheckIncludeOrder(input_api, output_api):
810 """Checks that the #include order is correct.
812 1. The corresponding header for source files.
813 2. C system files in alphabetical order
814 3. C++ system files in alphabetical order
815 4. Project's .h files in alphabetical order
817 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
818 these rules separately.
820 def FileFilterIncludeOrder(affected_file):
821 black_list = (_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST)
822 return input_api.FilterSourceFile(affected_file, black_list=black_list)
824 warnings = []
825 for f in input_api.AffectedFiles(file_filter=FileFilterIncludeOrder):
826 if f.LocalPath().endswith(('.cc', '.h')):
827 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
828 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
830 results = []
831 if warnings:
832 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
833 warnings))
834 return results
837 def _CheckForVersionControlConflictsInFile(input_api, f):
838 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
839 errors = []
840 for line_num, line in f.ChangedContents():
841 if pattern.match(line):
842 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
843 return errors
846 def _CheckForVersionControlConflicts(input_api, output_api):
847 """Usually this is not intentional and will cause a compile failure."""
848 errors = []
849 for f in input_api.AffectedFiles():
850 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
852 results = []
853 if errors:
854 results.append(output_api.PresubmitError(
855 'Version control conflict markers found, please resolve.', errors))
856 return results
859 def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
860 def FilterFile(affected_file):
861 """Filter function for use with input_api.AffectedSourceFiles,
862 below. This filters out everything except non-test files from
863 top-level directories that generally speaking should not hard-code
864 service URLs (e.g. src/android_webview/, src/content/ and others).
866 return input_api.FilterSourceFile(
867 affected_file,
868 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
869 black_list=(_EXCLUDED_PATHS +
870 _TEST_CODE_EXCLUDED_PATHS +
871 input_api.DEFAULT_BLACK_LIST))
873 base_pattern = '"[^"]*google\.com[^"]*"'
874 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
875 pattern = input_api.re.compile(base_pattern)
876 problems = [] # items are (filename, line_number, line)
877 for f in input_api.AffectedSourceFiles(FilterFile):
878 for line_num, line in f.ChangedContents():
879 if not comment_pattern.search(line) and pattern.search(line):
880 problems.append((f.LocalPath(), line_num, line))
882 if problems:
883 return [output_api.PresubmitPromptOrNotify(
884 'Most layers below src/chrome/ should not hardcode service URLs.\n'
885 'Are you sure this is correct?',
886 [' %s:%d: %s' % (
887 problem[0], problem[1], problem[2]) for problem in problems])]
888 else:
889 return []
892 def _CheckNoAbbreviationInPngFileName(input_api, output_api):
893 """Makes sure there are no abbreviations in the name of PNG files.
894 The native_client_sdk directory is excluded because it has auto-generated PNG
895 files for documentation.
897 errors = []
898 white_list = (r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$',)
899 black_list = (r'^native_client_sdk[\\\/]',)
900 file_filter = lambda f: input_api.FilterSourceFile(
901 f, white_list=white_list, black_list=black_list)
902 for f in input_api.AffectedFiles(include_deletes=False,
903 file_filter=file_filter):
904 errors.append(' %s' % f.LocalPath())
906 results = []
907 if errors:
908 results.append(output_api.PresubmitError(
909 'The name of PNG files should not have abbreviations. \n'
910 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
911 'Contact oshima@chromium.org if you have questions.', errors))
912 return results
915 def _FilesToCheckForIncomingDeps(re, changed_lines):
916 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
917 a set of DEPS entries that we should look up.
919 For a directory (rather than a specific filename) we fake a path to
920 a specific filename by adding /DEPS. This is chosen as a file that
921 will seldom or never be subject to per-file include_rules.
923 # We ignore deps entries on auto-generated directories.
924 AUTO_GENERATED_DIRS = ['grit', 'jni']
926 # This pattern grabs the path without basename in the first
927 # parentheses, and the basename (if present) in the second. It
928 # relies on the simple heuristic that if there is a basename it will
929 # be a header file ending in ".h".
930 pattern = re.compile(
931 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
932 results = set()
933 for changed_line in changed_lines:
934 m = pattern.match(changed_line)
935 if m:
936 path = m.group(1)
937 if path.split('/')[0] not in AUTO_GENERATED_DIRS:
938 if m.group(2):
939 results.add('%s%s' % (path, m.group(2)))
940 else:
941 results.add('%s/DEPS' % path)
942 return results
945 def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
946 """When a dependency prefixed with + is added to a DEPS file, we
947 want to make sure that the change is reviewed by an OWNER of the
948 target file or directory, to avoid layering violations from being
949 introduced. This check verifies that this happens.
951 changed_lines = set()
952 for f in input_api.AffectedFiles():
953 filename = input_api.os_path.basename(f.LocalPath())
954 if filename == 'DEPS':
955 changed_lines |= set(line.strip()
956 for line_num, line
957 in f.ChangedContents())
958 if not changed_lines:
959 return []
961 virtual_depended_on_files = _FilesToCheckForIncomingDeps(input_api.re,
962 changed_lines)
963 if not virtual_depended_on_files:
964 return []
966 if input_api.is_committing:
967 if input_api.tbr:
968 return [output_api.PresubmitNotifyResult(
969 '--tbr was specified, skipping OWNERS check for DEPS additions')]
970 if not input_api.change.issue:
971 return [output_api.PresubmitError(
972 "DEPS approval by OWNERS check failed: this change has "
973 "no Rietveld issue number, so we can't check it for approvals.")]
974 output = output_api.PresubmitError
975 else:
976 output = output_api.PresubmitNotifyResult
978 owners_db = input_api.owners_db
979 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
980 input_api,
981 owners_db.email_regexp,
982 approval_needed=input_api.is_committing)
984 owner_email = owner_email or input_api.change.author_email
986 reviewers_plus_owner = set(reviewers)
987 if owner_email:
988 reviewers_plus_owner.add(owner_email)
989 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
990 reviewers_plus_owner)
992 # We strip the /DEPS part that was added by
993 # _FilesToCheckForIncomingDeps to fake a path to a file in a
994 # directory.
995 def StripDeps(path):
996 start_deps = path.rfind('/DEPS')
997 if start_deps != -1:
998 return path[:start_deps]
999 else:
1000 return path
1001 unapproved_dependencies = ["'+%s'," % StripDeps(path)
1002 for path in missing_files]
1004 if unapproved_dependencies:
1005 output_list = [
1006 output('Missing LGTM from OWNERS of dependencies added to DEPS:\n %s' %
1007 '\n '.join(sorted(unapproved_dependencies)))]
1008 if not input_api.is_committing:
1009 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
1010 output_list.append(output(
1011 'Suggested missing target path OWNERS:\n %s' %
1012 '\n '.join(suggested_owners or [])))
1013 return output_list
1015 return []
1018 def _CheckSpamLogging(input_api, output_api):
1019 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
1020 black_list = (_EXCLUDED_PATHS +
1021 _TEST_CODE_EXCLUDED_PATHS +
1022 input_api.DEFAULT_BLACK_LIST +
1023 (r"^base[\\\/]logging\.h$",
1024 r"^base[\\\/]logging\.cc$",
1025 r"^chrome[\\\/]app[\\\/]chrome_main_delegate\.cc$",
1026 r"^chrome[\\\/]browser[\\\/]chrome_browser_main\.cc$",
1027 r"^chrome[\\\/]browser[\\\/]ui[\\\/]startup[\\\/]"
1028 r"startup_browser_creator\.cc$",
1029 r"^chrome[\\\/]installer[\\\/]setup[\\\/].*",
1030 r"chrome[\\\/]browser[\\\/]diagnostics[\\\/]" +
1031 r"diagnostics_writer\.cc$",
1032 r"^chrome_elf[\\\/]dll_hash[\\\/]dll_hash_main\.cc$",
1033 r"^chromecast[\\\/]",
1034 r"^cloud_print[\\\/]",
1035 r"^content[\\\/]common[\\\/]gpu[\\\/]client[\\\/]"
1036 r"gl_helper_benchmark\.cc$",
1037 r"^courgette[\\\/]courgette_tool\.cc$",
1038 r"^extensions[\\\/]renderer[\\\/]logging_native_handler\.cc$",
1039 r"^ipc[\\\/]ipc_logging\.cc$",
1040 r"^native_client_sdk[\\\/]",
1041 r"^remoting[\\\/]base[\\\/]logging\.h$",
1042 r"^remoting[\\\/]host[\\\/].*",
1043 r"^sandbox[\\\/]linux[\\\/].*",
1044 r"^tools[\\\/]",
1045 r"^ui[\\\/]aura[\\\/]bench[\\\/]bench_main\.cc$",
1046 r"^storage[\\\/]browser[\\\/]fileapi[\\\/]" +
1047 r"dump_file_system.cc$",))
1048 source_file_filter = lambda x: input_api.FilterSourceFile(
1049 x, white_list=(file_inclusion_pattern,), black_list=black_list)
1051 log_info = []
1052 printf = []
1054 for f in input_api.AffectedSourceFiles(source_file_filter):
1055 contents = input_api.ReadFile(f, 'rb')
1056 if input_api.re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", contents):
1057 log_info.append(f.LocalPath())
1058 elif input_api.re.search(r"\bD?LOG_IF\s*\(\s*INFO\s*,", contents):
1059 log_info.append(f.LocalPath())
1061 if input_api.re.search(r"\bprintf\(", contents):
1062 printf.append(f.LocalPath())
1063 elif input_api.re.search(r"\bfprintf\((stdout|stderr)", contents):
1064 printf.append(f.LocalPath())
1066 if log_info:
1067 return [output_api.PresubmitError(
1068 'These files spam the console log with LOG(INFO):',
1069 items=log_info)]
1070 if printf:
1071 return [output_api.PresubmitError(
1072 'These files spam the console log with printf/fprintf:',
1073 items=printf)]
1074 return []
1077 def _CheckForAnonymousVariables(input_api, output_api):
1078 """These types are all expected to hold locks while in scope and
1079 so should never be anonymous (which causes them to be immediately
1080 destroyed)."""
1081 they_who_must_be_named = [
1082 'base::AutoLock',
1083 'base::AutoReset',
1084 'base::AutoUnlock',
1085 'SkAutoAlphaRestore',
1086 'SkAutoBitmapShaderInstall',
1087 'SkAutoBlitterChoose',
1088 'SkAutoBounderCommit',
1089 'SkAutoCallProc',
1090 'SkAutoCanvasRestore',
1091 'SkAutoCommentBlock',
1092 'SkAutoDescriptor',
1093 'SkAutoDisableDirectionCheck',
1094 'SkAutoDisableOvalCheck',
1095 'SkAutoFree',
1096 'SkAutoGlyphCache',
1097 'SkAutoHDC',
1098 'SkAutoLockColors',
1099 'SkAutoLockPixels',
1100 'SkAutoMalloc',
1101 'SkAutoMaskFreeImage',
1102 'SkAutoMutexAcquire',
1103 'SkAutoPathBoundsUpdate',
1104 'SkAutoPDFRelease',
1105 'SkAutoRasterClipValidate',
1106 'SkAutoRef',
1107 'SkAutoTime',
1108 'SkAutoTrace',
1109 'SkAutoUnref',
1111 anonymous = r'(%s)\s*[({]' % '|'.join(they_who_must_be_named)
1112 # bad: base::AutoLock(lock.get());
1113 # not bad: base::AutoLock lock(lock.get());
1114 bad_pattern = input_api.re.compile(anonymous)
1115 # good: new base::AutoLock(lock.get())
1116 good_pattern = input_api.re.compile(r'\bnew\s*' + anonymous)
1117 errors = []
1119 for f in input_api.AffectedFiles():
1120 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
1121 continue
1122 for linenum, line in f.ChangedContents():
1123 if bad_pattern.search(line) and not good_pattern.search(line):
1124 errors.append('%s:%d' % (f.LocalPath(), linenum))
1126 if errors:
1127 return [output_api.PresubmitError(
1128 'These lines create anonymous variables that need to be named:',
1129 items=errors)]
1130 return []
1133 def _CheckCygwinShell(input_api, output_api):
1134 source_file_filter = lambda x: input_api.FilterSourceFile(
1135 x, white_list=(r'.+\.(gyp|gypi)$',))
1136 cygwin_shell = []
1138 for f in input_api.AffectedSourceFiles(source_file_filter):
1139 for linenum, line in f.ChangedContents():
1140 if 'msvs_cygwin_shell' in line:
1141 cygwin_shell.append(f.LocalPath())
1142 break
1144 if cygwin_shell:
1145 return [output_api.PresubmitError(
1146 'These files should not use msvs_cygwin_shell (the default is 0):',
1147 items=cygwin_shell)]
1148 return []
1151 def _CheckUserActionUpdate(input_api, output_api):
1152 """Checks if any new user action has been added."""
1153 if any('actions.xml' == input_api.os_path.basename(f) for f in
1154 input_api.LocalPaths()):
1155 # If actions.xml is already included in the changelist, the PRESUBMIT
1156 # for actions.xml will do a more complete presubmit check.
1157 return []
1159 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm'))
1160 action_re = r'[^a-zA-Z]UserMetricsAction\("([^"]*)'
1161 current_actions = None
1162 for f in input_api.AffectedFiles(file_filter=file_filter):
1163 for line_num, line in f.ChangedContents():
1164 match = input_api.re.search(action_re, line)
1165 if match:
1166 # Loads contents in tools/metrics/actions/actions.xml to memory. It's
1167 # loaded only once.
1168 if not current_actions:
1169 with open('tools/metrics/actions/actions.xml') as actions_f:
1170 current_actions = actions_f.read()
1171 # Search for the matched user action name in |current_actions|.
1172 for action_name in match.groups():
1173 action = 'name="{0}"'.format(action_name)
1174 if action not in current_actions:
1175 return [output_api.PresubmitPromptWarning(
1176 'File %s line %d: %s is missing in '
1177 'tools/metrics/actions/actions.xml. Please run '
1178 'tools/metrics/actions/extract_actions.py to update.'
1179 % (f.LocalPath(), line_num, action_name))]
1180 return []
1183 def _GetJSONParseError(input_api, filename, eat_comments=True):
1184 try:
1185 contents = input_api.ReadFile(filename)
1186 if eat_comments:
1187 json_comment_eater = input_api.os_path.join(
1188 input_api.PresubmitLocalPath(),
1189 'tools', 'json_comment_eater', 'json_comment_eater.py')
1190 process = input_api.subprocess.Popen(
1191 [input_api.python_executable, json_comment_eater],
1192 stdin=input_api.subprocess.PIPE,
1193 stdout=input_api.subprocess.PIPE,
1194 universal_newlines=True)
1195 (contents, _) = process.communicate(input=contents)
1197 input_api.json.loads(contents)
1198 except ValueError as e:
1199 return e
1200 return None
1203 def _GetIDLParseError(input_api, filename):
1204 try:
1205 contents = input_api.ReadFile(filename)
1206 idl_schema = input_api.os_path.join(
1207 input_api.PresubmitLocalPath(),
1208 'tools', 'json_schema_compiler', 'idl_schema.py')
1209 process = input_api.subprocess.Popen(
1210 [input_api.python_executable, idl_schema],
1211 stdin=input_api.subprocess.PIPE,
1212 stdout=input_api.subprocess.PIPE,
1213 stderr=input_api.subprocess.PIPE,
1214 universal_newlines=True)
1215 (_, error) = process.communicate(input=contents)
1216 return error or None
1217 except ValueError as e:
1218 return e
1221 def _CheckParseErrors(input_api, output_api):
1222 """Check that IDL and JSON files do not contain syntax errors."""
1223 actions = {
1224 '.idl': _GetIDLParseError,
1225 '.json': _GetJSONParseError,
1227 # These paths contain test data and other known invalid JSON files.
1228 excluded_patterns = [
1229 r'test[\\\/]data[\\\/]',
1230 r'^components[\\\/]policy[\\\/]resources[\\\/]policy_templates\.json$',
1232 # Most JSON files are preprocessed and support comments, but these do not.
1233 json_no_comments_patterns = [
1234 r'^testing[\\\/]',
1236 # Only run IDL checker on files in these directories.
1237 idl_included_patterns = [
1238 r'^chrome[\\\/]common[\\\/]extensions[\\\/]api[\\\/]',
1239 r'^extensions[\\\/]common[\\\/]api[\\\/]',
1242 def get_action(affected_file):
1243 filename = affected_file.LocalPath()
1244 return actions.get(input_api.os_path.splitext(filename)[1])
1246 def MatchesFile(patterns, path):
1247 for pattern in patterns:
1248 if input_api.re.search(pattern, path):
1249 return True
1250 return False
1252 def FilterFile(affected_file):
1253 action = get_action(affected_file)
1254 if not action:
1255 return False
1256 path = affected_file.LocalPath()
1258 if MatchesFile(excluded_patterns, path):
1259 return False
1261 if (action == _GetIDLParseError and
1262 not MatchesFile(idl_included_patterns, path)):
1263 return False
1264 return True
1266 results = []
1267 for affected_file in input_api.AffectedFiles(
1268 file_filter=FilterFile, include_deletes=False):
1269 action = get_action(affected_file)
1270 kwargs = {}
1271 if (action == _GetJSONParseError and
1272 MatchesFile(json_no_comments_patterns, affected_file.LocalPath())):
1273 kwargs['eat_comments'] = False
1274 parse_error = action(input_api,
1275 affected_file.AbsoluteLocalPath(),
1276 **kwargs)
1277 if parse_error:
1278 results.append(output_api.PresubmitError('%s could not be parsed: %s' %
1279 (affected_file.LocalPath(), parse_error)))
1280 return results
1283 def _CheckJavaStyle(input_api, output_api):
1284 """Runs checkstyle on changed java files and returns errors if any exist."""
1285 import sys
1286 original_sys_path = sys.path
1287 try:
1288 sys.path = sys.path + [input_api.os_path.join(
1289 input_api.PresubmitLocalPath(), 'tools', 'android', 'checkstyle')]
1290 import checkstyle
1291 finally:
1292 # Restore sys.path to what it was before.
1293 sys.path = original_sys_path
1295 return checkstyle.RunCheckstyle(
1296 input_api, output_api, 'tools/android/checkstyle/chromium-style-5.0.xml',
1297 black_list=_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST)
1300 def _CheckForCopyrightedCode(input_api, output_api):
1301 """Verifies that newly added code doesn't contain copyrighted material
1302 and is properly licensed under the standard Chromium license.
1304 As there can be false positives, we maintain a whitelist file. This check
1305 also verifies that the whitelist file is up to date.
1307 import sys
1308 original_sys_path = sys.path
1309 try:
1310 sys.path = sys.path + [input_api.os_path.join(
1311 input_api.PresubmitLocalPath(), 'android_webview', 'tools')]
1312 import copyright_scanner
1313 finally:
1314 # Restore sys.path to what it was before.
1315 sys.path = original_sys_path
1317 return copyright_scanner.ScanAtPresubmit(input_api, output_api)
1320 def _CheckSingletonInHeaders(input_api, output_api):
1321 """Checks to make sure no header files have |Singleton<|."""
1322 def FileFilter(affected_file):
1323 # It's ok for base/memory/singleton.h to have |Singleton<|.
1324 black_list = (_EXCLUDED_PATHS +
1325 input_api.DEFAULT_BLACK_LIST +
1326 (r"^base[\\\/]memory[\\\/]singleton\.h$",))
1327 return input_api.FilterSourceFile(affected_file, black_list=black_list)
1329 pattern = input_api.re.compile(r'(?<!class\s)Singleton\s*<')
1330 files = []
1331 for f in input_api.AffectedSourceFiles(FileFilter):
1332 if (f.LocalPath().endswith('.h') or f.LocalPath().endswith('.hxx') or
1333 f.LocalPath().endswith('.hpp') or f.LocalPath().endswith('.inl')):
1334 contents = input_api.ReadFile(f)
1335 for line in contents.splitlines(False):
1336 if (not input_api.re.match(r'//', line) and # Strip C++ comment.
1337 pattern.search(line)):
1338 files.append(f)
1339 break
1341 if files:
1342 return [ output_api.PresubmitError(
1343 'Found Singleton<T> in the following header files.\n' +
1344 'Please move them to an appropriate source file so that the ' +
1345 'template gets instantiated in a single compilation unit.',
1346 files) ]
1347 return []
1350 _DEPRECATED_CSS = [
1351 # Values
1352 ( "-webkit-box", "flex" ),
1353 ( "-webkit-inline-box", "inline-flex" ),
1354 ( "-webkit-flex", "flex" ),
1355 ( "-webkit-inline-flex", "inline-flex" ),
1356 ( "-webkit-min-content", "min-content" ),
1357 ( "-webkit-max-content", "max-content" ),
1359 # Properties
1360 ( "-webkit-background-clip", "background-clip" ),
1361 ( "-webkit-background-origin", "background-origin" ),
1362 ( "-webkit-background-size", "background-size" ),
1363 ( "-webkit-box-shadow", "box-shadow" ),
1365 # Functions
1366 ( "-webkit-gradient", "gradient" ),
1367 ( "-webkit-repeating-gradient", "repeating-gradient" ),
1368 ( "-webkit-linear-gradient", "linear-gradient" ),
1369 ( "-webkit-repeating-linear-gradient", "repeating-linear-gradient" ),
1370 ( "-webkit-radial-gradient", "radial-gradient" ),
1371 ( "-webkit-repeating-radial-gradient", "repeating-radial-gradient" ),
1374 def _CheckNoDeprecatedCSS(input_api, output_api):
1375 """ Make sure that we don't use deprecated CSS
1376 properties, functions or values. Our external
1377 documentation is ignored by the hooks as it
1378 needs to be consumed by WebKit. """
1379 results = []
1380 file_inclusion_pattern = (r".+\.css$",)
1381 black_list = (_EXCLUDED_PATHS +
1382 _TEST_CODE_EXCLUDED_PATHS +
1383 input_api.DEFAULT_BLACK_LIST +
1384 (r"^chrome/common/extensions/docs",
1385 r"^chrome/docs",
1386 r"^native_client_sdk"))
1387 file_filter = lambda f: input_api.FilterSourceFile(
1388 f, white_list=file_inclusion_pattern, black_list=black_list)
1389 for fpath in input_api.AffectedFiles(file_filter=file_filter):
1390 for line_num, line in fpath.ChangedContents():
1391 for (deprecated_value, value) in _DEPRECATED_CSS:
1392 if deprecated_value in line:
1393 results.append(output_api.PresubmitError(
1394 "%s:%d: Use of deprecated CSS %s, use %s instead" %
1395 (fpath.LocalPath(), line_num, deprecated_value, value)))
1396 return results
1399 _DEPRECATED_JS = [
1400 ( "__lookupGetter__", "Object.getOwnPropertyDescriptor" ),
1401 ( "__defineGetter__", "Object.defineProperty" ),
1402 ( "__defineSetter__", "Object.defineProperty" ),
1405 def _CheckNoDeprecatedJS(input_api, output_api):
1406 """Make sure that we don't use deprecated JS in Chrome code."""
1407 results = []
1408 file_inclusion_pattern = (r".+\.js$",) # TODO(dbeam): .html?
1409 black_list = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS +
1410 input_api.DEFAULT_BLACK_LIST)
1411 file_filter = lambda f: input_api.FilterSourceFile(
1412 f, white_list=file_inclusion_pattern, black_list=black_list)
1413 for fpath in input_api.AffectedFiles(file_filter=file_filter):
1414 for lnum, line in fpath.ChangedContents():
1415 for (deprecated, replacement) in _DEPRECATED_JS:
1416 if deprecated in line:
1417 results.append(output_api.PresubmitError(
1418 "%s:%d: Use of deprecated JS %s, use %s instead" %
1419 (fpath.LocalPath(), lnum, deprecated, replacement)))
1420 return results
1423 def _CommonChecks(input_api, output_api):
1424 """Checks common to both upload and commit."""
1425 results = []
1426 results.extend(input_api.canned_checks.PanProjectChecks(
1427 input_api, output_api,
1428 excluded_paths=_EXCLUDED_PATHS + _TESTRUNNER_PATHS))
1429 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
1430 results.extend(
1431 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
1432 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
1433 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
1434 results.extend(_CheckNoNewWStrings(input_api, output_api))
1435 results.extend(_CheckNoDEPSGIT(input_api, output_api))
1436 results.extend(_CheckNoBannedFunctions(input_api, output_api))
1437 results.extend(_CheckNoPragmaOnce(input_api, output_api))
1438 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
1439 results.extend(_CheckUnwantedDependencies(input_api, output_api))
1440 results.extend(_CheckFilePermissions(input_api, output_api))
1441 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
1442 results.extend(_CheckIncludeOrder(input_api, output_api))
1443 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
1444 results.extend(_CheckPatchFiles(input_api, output_api))
1445 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
1446 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
1447 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
1448 results.extend(_CheckForInvalidIfDefinedMacros(input_api, output_api))
1449 # TODO(danakj): Remove this when base/move.h is removed.
1450 results.extend(_CheckForUsingSideEffectsOfPass(input_api, output_api))
1451 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
1452 results.extend(
1453 input_api.canned_checks.CheckChangeHasNoTabs(
1454 input_api,
1455 output_api,
1456 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
1457 results.extend(_CheckSpamLogging(input_api, output_api))
1458 results.extend(_CheckForAnonymousVariables(input_api, output_api))
1459 results.extend(_CheckCygwinShell(input_api, output_api))
1460 results.extend(_CheckUserActionUpdate(input_api, output_api))
1461 results.extend(_CheckNoDeprecatedCSS(input_api, output_api))
1462 results.extend(_CheckNoDeprecatedJS(input_api, output_api))
1463 results.extend(_CheckParseErrors(input_api, output_api))
1464 results.extend(_CheckForIPCRules(input_api, output_api))
1465 results.extend(_CheckForCopyrightedCode(input_api, output_api))
1466 results.extend(_CheckForWindowsLineEndings(input_api, output_api))
1467 results.extend(_CheckSingletonInHeaders(input_api, output_api))
1469 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
1470 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
1471 input_api, output_api,
1472 input_api.PresubmitLocalPath(),
1473 whitelist=[r'^PRESUBMIT_test\.py$']))
1474 return results
1477 def _CheckAuthorizedAuthor(input_api, output_api):
1478 """For non-googler/chromites committers, verify the author's email address is
1479 in AUTHORS.
1481 # TODO(maruel): Add it to input_api?
1482 import fnmatch
1484 author = input_api.change.author_email
1485 if not author:
1486 input_api.logging.info('No author, skipping AUTHOR check')
1487 return []
1488 authors_path = input_api.os_path.join(
1489 input_api.PresubmitLocalPath(), 'AUTHORS')
1490 valid_authors = (
1491 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
1492 for line in open(authors_path))
1493 valid_authors = [item.group(1).lower() for item in valid_authors if item]
1494 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
1495 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
1496 return [output_api.PresubmitPromptWarning(
1497 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
1498 '\n'
1499 'http://www.chromium.org/developers/contributing-code and read the '
1500 '"Legal" section\n'
1501 'If you are a chromite, verify the contributor signed the CLA.') %
1502 author)]
1503 return []
1506 def _CheckPatchFiles(input_api, output_api):
1507 problems = [f.LocalPath() for f in input_api.AffectedFiles()
1508 if f.LocalPath().endswith(('.orig', '.rej'))]
1509 if problems:
1510 return [output_api.PresubmitError(
1511 "Don't commit .rej and .orig files.", problems)]
1512 else:
1513 return []
1516 def _DidYouMeanOSMacro(bad_macro):
1517 try:
1518 return {'A': 'OS_ANDROID',
1519 'B': 'OS_BSD',
1520 'C': 'OS_CHROMEOS',
1521 'F': 'OS_FREEBSD',
1522 'L': 'OS_LINUX',
1523 'M': 'OS_MACOSX',
1524 'N': 'OS_NACL',
1525 'O': 'OS_OPENBSD',
1526 'P': 'OS_POSIX',
1527 'S': 'OS_SOLARIS',
1528 'W': 'OS_WIN'}[bad_macro[3].upper()]
1529 except KeyError:
1530 return ''
1533 def _CheckForInvalidOSMacrosInFile(input_api, f):
1534 """Check for sensible looking, totally invalid OS macros."""
1535 preprocessor_statement = input_api.re.compile(r'^\s*#')
1536 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
1537 results = []
1538 for lnum, line in f.ChangedContents():
1539 if preprocessor_statement.search(line):
1540 for match in os_macro.finditer(line):
1541 if not match.group(1) in _VALID_OS_MACROS:
1542 good = _DidYouMeanOSMacro(match.group(1))
1543 did_you_mean = ' (did you mean %s?)' % good if good else ''
1544 results.append(' %s:%d %s%s' % (f.LocalPath(),
1545 lnum,
1546 match.group(1),
1547 did_you_mean))
1548 return results
1551 def _CheckForInvalidOSMacros(input_api, output_api):
1552 """Check all affected files for invalid OS macros."""
1553 bad_macros = []
1554 for f in input_api.AffectedFiles():
1555 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
1556 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
1558 if not bad_macros:
1559 return []
1561 return [output_api.PresubmitError(
1562 'Possibly invalid OS macro[s] found. Please fix your code\n'
1563 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
1566 def _CheckForInvalidIfDefinedMacrosInFile(input_api, f):
1567 """Check all affected files for invalid "if defined" macros."""
1568 ALWAYS_DEFINED_MACROS = (
1569 "TARGET_CPU_PPC",
1570 "TARGET_CPU_PPC64",
1571 "TARGET_CPU_68K",
1572 "TARGET_CPU_X86",
1573 "TARGET_CPU_ARM",
1574 "TARGET_CPU_MIPS",
1575 "TARGET_CPU_SPARC",
1576 "TARGET_CPU_ALPHA",
1577 "TARGET_IPHONE_SIMULATOR",
1578 "TARGET_OS_EMBEDDED",
1579 "TARGET_OS_IPHONE",
1580 "TARGET_OS_MAC",
1581 "TARGET_OS_UNIX",
1582 "TARGET_OS_WIN32",
1584 ifdef_macro = input_api.re.compile(r'^\s*#.*(?:ifdef\s|defined\()([^\s\)]+)')
1585 results = []
1586 for lnum, line in f.ChangedContents():
1587 for match in ifdef_macro.finditer(line):
1588 if match.group(1) in ALWAYS_DEFINED_MACROS:
1589 always_defined = ' %s is always defined. ' % match.group(1)
1590 did_you_mean = 'Did you mean \'#if %s\'?' % match.group(1)
1591 results.append(' %s:%d %s\n\t%s' % (f.LocalPath(),
1592 lnum,
1593 always_defined,
1594 did_you_mean))
1595 return results
1598 def _CheckForInvalidIfDefinedMacros(input_api, output_api):
1599 """Check all affected files for invalid "if defined" macros."""
1600 bad_macros = []
1601 for f in input_api.AffectedFiles():
1602 if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
1603 bad_macros.extend(_CheckForInvalidIfDefinedMacrosInFile(input_api, f))
1605 if not bad_macros:
1606 return []
1608 return [output_api.PresubmitError(
1609 'Found ifdef check on always-defined macro[s]. Please fix your code\n'
1610 'or check the list of ALWAYS_DEFINED_MACROS in src/PRESUBMIT.py.',
1611 bad_macros)]
1614 def _CheckForUsingSideEffectsOfPass(input_api, output_api):
1615 """Check all affected files for using side effects of Pass."""
1616 errors = []
1617 for f in input_api.AffectedFiles():
1618 if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')):
1619 for lnum, line in f.ChangedContents():
1620 # Disallow Foo(*my_scoped_thing.Pass()); See crbug.com/418297.
1621 if input_api.re.search(r'\*[a-zA-Z0-9_]+\.Pass\(\)', line):
1622 errors.append(output_api.PresubmitError(
1623 ('%s:%d uses *foo.Pass() to delete the contents of scoped_ptr. ' +
1624 'See crbug.com/418297.') % (f.LocalPath(), lnum)))
1625 return errors
1628 def _CheckForIPCRules(input_api, output_api):
1629 """Check for same IPC rules described in
1630 http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc
1632 base_pattern = r'IPC_ENUM_TRAITS\('
1633 inclusion_pattern = input_api.re.compile(r'(%s)' % base_pattern)
1634 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_pattern)
1636 problems = []
1637 for f in input_api.AffectedSourceFiles(None):
1638 local_path = f.LocalPath()
1639 if not local_path.endswith('.h'):
1640 continue
1641 for line_number, line in f.ChangedContents():
1642 if inclusion_pattern.search(line) and not comment_pattern.search(line):
1643 problems.append(
1644 '%s:%d\n %s' % (local_path, line_number, line.strip()))
1646 if problems:
1647 return [output_api.PresubmitPromptWarning(
1648 _IPC_ENUM_TRAITS_DEPRECATED, problems)]
1649 else:
1650 return []
1653 def _CheckForWindowsLineEndings(input_api, output_api):
1654 """Check source code and known ascii text files for Windows style line
1655 endings.
1657 known_text_files = r'.*\.(txt|html|htm|mhtml|py|gyp|gypi|gn|isolate)$'
1659 file_inclusion_pattern = (
1660 known_text_files,
1661 r'.+%s' % _IMPLEMENTATION_EXTENSIONS
1664 filter = lambda f: input_api.FilterSourceFile(
1665 f, white_list=file_inclusion_pattern, black_list=None)
1666 files = [f.LocalPath() for f in
1667 input_api.AffectedSourceFiles(filter)]
1669 problems = []
1671 for file in files:
1672 fp = open(file, 'r')
1673 for line in fp:
1674 if line.endswith('\r\n'):
1675 problems.append(file)
1676 break
1677 fp.close()
1679 if problems:
1680 return [output_api.PresubmitPromptWarning('Are you sure that you want '
1681 'these files to contain Windows style line endings?\n' +
1682 '\n'.join(problems))]
1684 return []
1687 def CheckChangeOnUpload(input_api, output_api):
1688 results = []
1689 results.extend(_CommonChecks(input_api, output_api))
1690 results.extend(_CheckValidHostsInDEPS(input_api, output_api))
1691 results.extend(_CheckJavaStyle(input_api, output_api))
1692 results.extend(
1693 input_api.canned_checks.CheckGNFormatted(input_api, output_api))
1694 results.extend(_CheckUmaHistogramChanges(input_api, output_api))
1695 return results
1698 def GetTryServerMasterForBot(bot):
1699 """Returns the Try Server master for the given bot.
1701 It tries to guess the master from the bot name, but may still fail
1702 and return None. There is no longer a default master.
1704 # Potentially ambiguous bot names are listed explicitly.
1705 master_map = {
1706 'chromium_presubmit': 'tryserver.chromium.linux',
1707 'blink_presubmit': 'tryserver.chromium.linux',
1708 'tools_build_presubmit': 'tryserver.chromium.linux',
1710 master = master_map.get(bot)
1711 if not master:
1712 if 'linux' in bot or 'android' in bot or 'presubmit' in bot:
1713 master = 'tryserver.chromium.linux'
1714 elif 'win' in bot:
1715 master = 'tryserver.chromium.win'
1716 elif 'mac' in bot or 'ios' in bot:
1717 master = 'tryserver.chromium.mac'
1718 return master
1721 def GetDefaultTryConfigs(bots):
1722 """Returns a list of ('bot', set(['tests']), filtered by [bots].
1725 builders_and_tests = dict((bot, set(['defaulttests'])) for bot in bots)
1727 # Build up the mapping from tryserver master to bot/test.
1728 out = dict()
1729 for bot, tests in builders_and_tests.iteritems():
1730 out.setdefault(GetTryServerMasterForBot(bot), {})[bot] = tests
1731 return out
1734 def CheckChangeOnCommit(input_api, output_api):
1735 results = []
1736 results.extend(_CommonChecks(input_api, output_api))
1737 # TODO(thestig) temporarily disabled, doesn't work in third_party/
1738 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
1739 # input_api, output_api, sources))
1740 # Make sure the tree is 'open'.
1741 results.extend(input_api.canned_checks.CheckTreeIsOpen(
1742 input_api,
1743 output_api,
1744 json_url='http://chromium-status.appspot.com/current?format=json'))
1746 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1747 input_api, output_api))
1748 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1749 input_api, output_api))
1750 return results
1753 def GetPreferredTryMasters(project, change):
1754 import re
1755 files = change.LocalPaths()
1757 import os
1758 import json
1759 with open(os.path.join(
1760 change.RepositoryRoot(), 'testing', 'commit_queue', 'config.json')) as f:
1761 cq_config = json.load(f)
1762 cq_verifiers = cq_config.get('verifiers_no_patch', {})
1763 cq_try_jobs = cq_verifiers.get('try_job_verifier', {})
1764 builders = cq_try_jobs.get('launched', {})
1766 for master, master_config in cq_try_jobs.get('triggered', {}).iteritems():
1767 for triggered_bot in master_config:
1768 builders.get(master, {}).pop(triggered_bot, None)
1770 # Explicitly iterate over copies of dicts since we mutate them.
1771 for master in builders.keys():
1772 for builder in builders[master].keys():
1773 # Do not trigger presubmit builders, since they're likely to fail
1774 # (e.g. OWNERS checks before finished code review), and we're
1775 # running local presubmit anyway.
1776 if 'presubmit' in builder:
1777 builders[master].pop(builder)
1779 return builders