Add CanCloseDialog to WebDialogDelegate to allow blocking closing of dialog if needed.
[chromium-blink-merge.git] / PRESUBMIT.py
blob15eaf642d901384bf100de782fce465603ef4bae
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 import re
13 import subprocess
14 import sys
17 _EXCLUDED_PATHS = (
18 r"^breakpad[\\\/].*",
19 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_rules.py",
20 r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_simple.py",
21 r"^native_client_sdk[\\\/]src[\\\/]tools[\\\/].*.mk",
22 r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*",
23 r"^skia[\\\/].*",
24 r"^v8[\\\/].*",
25 r".*MakeFile$",
26 r".+_autogen\.h$",
27 r".+[\\\/]pnacl_shim\.c$",
28 r"^gpu[\\\/]config[\\\/].*_list_json\.cc$",
31 # Fragment of a regular expression that matches C++ and Objective-C++
32 # implementation files.
33 _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
35 # Regular expression that matches code only used for test binaries
36 # (best effort).
37 _TEST_CODE_EXCLUDED_PATHS = (
38 r'.*[/\\](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS,
39 r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS,
40 r'.+_(api|browser|perf|pixel|unit|ui)?test(_[a-z]+)?%s' %
41 _IMPLEMENTATION_EXTENSIONS,
42 r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS,
43 r'.*[/\\](test|tool(s)?)[/\\].*',
44 # content_shell is used for running layout tests.
45 r'content[/\\]shell[/\\].*',
46 # At request of folks maintaining this folder.
47 r'chrome[/\\]browser[/\\]automation[/\\].*',
48 # Non-production example code.
49 r'mojo[/\\]examples[/\\].*',
52 _TEST_ONLY_WARNING = (
53 'You might be calling functions intended only for testing from\n'
54 'production code. It is OK to ignore this warning if you know what\n'
55 'you are doing, as the heuristics used to detect the situation are\n'
56 'not perfect. The commit queue will not block on this warning.\n'
57 'Email joi@chromium.org if you have questions.')
60 _INCLUDE_ORDER_WARNING = (
61 'Your #include order seems to be broken. Send mail to\n'
62 'marja@chromium.org if this is not the case.')
65 _BANNED_OBJC_FUNCTIONS = (
67 'addTrackingRect:',
69 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
70 'prohibited. Please use CrTrackingArea instead.',
71 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
73 False,
76 'NSTrackingArea',
78 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
79 'instead.',
80 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
82 False,
85 'convertPointFromBase:',
87 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
88 'Please use |convertPoint:(point) fromView:nil| instead.',
89 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
91 True,
94 'convertPointToBase:',
96 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
97 'Please use |convertPoint:(point) toView:nil| instead.',
98 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
100 True,
103 'convertRectFromBase:',
105 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
106 'Please use |convertRect:(point) fromView:nil| instead.',
107 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
109 True,
112 'convertRectToBase:',
114 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
115 'Please use |convertRect:(point) toView:nil| instead.',
116 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
118 True,
121 'convertSizeFromBase:',
123 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
124 'Please use |convertSize:(point) fromView:nil| instead.',
125 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
127 True,
130 'convertSizeToBase:',
132 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
133 'Please use |convertSize:(point) toView:nil| instead.',
134 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
136 True,
141 _BANNED_CPP_FUNCTIONS = (
142 # Make sure that gtest's FRIEND_TEST() macro is not used; the
143 # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
144 # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
146 'FRIEND_TEST(',
148 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
149 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
151 False,
155 'ScopedAllowIO',
157 'New code should not use ScopedAllowIO. Post a task to the blocking',
158 'pool or the FILE thread instead.',
160 True,
162 r"^components[\\\/]breakpad[\\\/]app[\\\/]breakpad_mac\.mm$",
163 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_browser_main\.cc$",
164 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_message_filter\.cc$",
165 r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
169 'SkRefPtr',
171 'The use of SkRefPtr is prohibited. ',
172 'Please use skia::RefPtr instead.'
174 True,
178 'SkAutoRef',
180 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
181 'Please use skia::RefPtr instead.'
183 True,
187 'SkAutoTUnref',
189 'The use of SkAutoTUnref is dangerous because it implicitly ',
190 'converts to a raw pointer. Please use skia::RefPtr instead.'
192 True,
196 'SkAutoUnref',
198 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
199 'because it implicitly converts to a raw pointer. ',
200 'Please use skia::RefPtr instead.'
202 True,
208 _VALID_OS_MACROS = (
209 # Please keep sorted.
210 'OS_ANDROID',
211 'OS_BSD',
212 'OS_CAT', # For testing.
213 'OS_CHROMEOS',
214 'OS_FREEBSD',
215 'OS_IOS',
216 'OS_LINUX',
217 'OS_MACOSX',
218 'OS_NACL',
219 'OS_OPENBSD',
220 'OS_POSIX',
221 'OS_SOLARIS',
222 'OS_WIN',
226 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
227 """Attempts to prevent use of functions intended only for testing in
228 non-testing code. For now this is just a best-effort implementation
229 that ignores header files and may have some false positives. A
230 better implementation would probably need a proper C++ parser.
232 # We only scan .cc files and the like, as the declaration of
233 # for-testing functions in header files are hard to distinguish from
234 # calls to such functions without a proper C++ parser.
235 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
237 base_function_pattern = r'ForTest(ing)?|for_test(ing)?'
238 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
239 comment_pattern = input_api.re.compile(r'//.*%s' % base_function_pattern)
240 exclusion_pattern = input_api.re.compile(
241 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
242 base_function_pattern, base_function_pattern))
244 def FilterFile(affected_file):
245 black_list = (_EXCLUDED_PATHS +
246 _TEST_CODE_EXCLUDED_PATHS +
247 input_api.DEFAULT_BLACK_LIST)
248 return input_api.FilterSourceFile(
249 affected_file,
250 white_list=(file_inclusion_pattern, ),
251 black_list=black_list)
253 problems = []
254 for f in input_api.AffectedSourceFiles(FilterFile):
255 local_path = f.LocalPath()
256 lines = input_api.ReadFile(f).splitlines()
257 line_number = 0
258 for line in lines:
259 if (inclusion_pattern.search(line) and
260 not comment_pattern.search(line) and
261 not exclusion_pattern.search(line)):
262 problems.append(
263 '%s:%d\n %s' % (local_path, line_number, line.strip()))
264 line_number += 1
266 if problems:
267 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
268 else:
269 return []
272 def _CheckNoIOStreamInHeaders(input_api, output_api):
273 """Checks to make sure no .h files include <iostream>."""
274 files = []
275 pattern = input_api.re.compile(r'^#include\s*<iostream>',
276 input_api.re.MULTILINE)
277 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
278 if not f.LocalPath().endswith('.h'):
279 continue
280 contents = input_api.ReadFile(f)
281 if pattern.search(contents):
282 files.append(f)
284 if len(files):
285 return [ output_api.PresubmitError(
286 'Do not #include <iostream> in header files, since it inserts static '
287 'initialization into every file including the header. Instead, '
288 '#include <ostream>. See http://crbug.com/94794',
289 files) ]
290 return []
293 def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
294 """Checks to make sure no source files use UNIT_TEST"""
295 problems = []
296 for f in input_api.AffectedFiles():
297 if (not f.LocalPath().endswith(('.cc', '.mm'))):
298 continue
300 for line_num, line in f.ChangedContents():
301 if 'UNIT_TEST ' in line or line.endswith('UNIT_TEST'):
302 problems.append(' %s:%d' % (f.LocalPath(), line_num))
304 if not problems:
305 return []
306 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
307 '\n'.join(problems))]
310 def _CheckNoNewWStrings(input_api, output_api):
311 """Checks to make sure we don't introduce use of wstrings."""
312 problems = []
313 for f in input_api.AffectedFiles():
314 if (not f.LocalPath().endswith(('.cc', '.h')) or
315 f.LocalPath().endswith(('test.cc', '_win.cc', '_win.h'))):
316 continue
318 allowWString = False
319 for line_num, line in f.ChangedContents():
320 if 'presubmit: allow wstring' in line:
321 allowWString = True
322 elif not allowWString and 'wstring' in line:
323 problems.append(' %s:%d' % (f.LocalPath(), line_num))
324 allowWString = False
325 else:
326 allowWString = False
328 if not problems:
329 return []
330 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
331 ' If you are calling a cross-platform API that accepts a wstring, '
332 'fix the API.\n' +
333 '\n'.join(problems))]
336 def _CheckNoDEPSGIT(input_api, output_api):
337 """Make sure .DEPS.git is never modified manually."""
338 if any(f.LocalPath().endswith('.DEPS.git') for f in
339 input_api.AffectedFiles()):
340 return [output_api.PresubmitError(
341 'Never commit changes to .DEPS.git. This file is maintained by an\n'
342 'automated system based on what\'s in DEPS and your changes will be\n'
343 'overwritten.\n'
344 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
345 'for more information')]
346 return []
349 def _CheckNoBannedFunctions(input_api, output_api):
350 """Make sure that banned functions are not used."""
351 warnings = []
352 errors = []
354 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
355 for f in input_api.AffectedFiles(file_filter=file_filter):
356 for line_num, line in f.ChangedContents():
357 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
358 if func_name in line:
359 problems = warnings;
360 if error:
361 problems = errors;
362 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
363 for message_line in message:
364 problems.append(' %s' % message_line)
366 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
367 for f in input_api.AffectedFiles(file_filter=file_filter):
368 for line_num, line in f.ChangedContents():
369 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
370 def IsBlacklisted(affected_file, blacklist):
371 local_path = affected_file.LocalPath()
372 for item in blacklist:
373 if input_api.re.match(item, local_path):
374 return True
375 return False
376 if IsBlacklisted(f, excluded_paths):
377 continue
378 if func_name in line:
379 problems = warnings;
380 if error:
381 problems = errors;
382 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
383 for message_line in message:
384 problems.append(' %s' % message_line)
386 result = []
387 if (warnings):
388 result.append(output_api.PresubmitPromptWarning(
389 'Banned functions were used.\n' + '\n'.join(warnings)))
390 if (errors):
391 result.append(output_api.PresubmitError(
392 'Banned functions were used.\n' + '\n'.join(errors)))
393 return result
396 def _CheckNoPragmaOnce(input_api, output_api):
397 """Make sure that banned functions are not used."""
398 files = []
399 pattern = input_api.re.compile(r'^#pragma\s+once',
400 input_api.re.MULTILINE)
401 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
402 if not f.LocalPath().endswith('.h'):
403 continue
404 contents = input_api.ReadFile(f)
405 if pattern.search(contents):
406 files.append(f)
408 if files:
409 return [output_api.PresubmitError(
410 'Do not use #pragma once in header files.\n'
411 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
412 files)]
413 return []
416 def _CheckNoTrinaryTrueFalse(input_api, output_api):
417 """Checks to make sure we don't introduce use of foo ? true : false."""
418 problems = []
419 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
420 for f in input_api.AffectedFiles():
421 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
422 continue
424 for line_num, line in f.ChangedContents():
425 if pattern.match(line):
426 problems.append(' %s:%d' % (f.LocalPath(), line_num))
428 if not problems:
429 return []
430 return [output_api.PresubmitPromptWarning(
431 'Please consider avoiding the "? true : false" pattern if possible.\n' +
432 '\n'.join(problems))]
435 def _CheckUnwantedDependencies(input_api, output_api):
436 """Runs checkdeps on #include statements added in this
437 change. Breaking - rules is an error, breaking ! rules is a
438 warning.
440 # We need to wait until we have an input_api object and use this
441 # roundabout construct to import checkdeps because this file is
442 # eval-ed and thus doesn't have __file__.
443 original_sys_path = sys.path
444 try:
445 sys.path = sys.path + [input_api.os_path.join(
446 input_api.PresubmitLocalPath(), 'tools', 'checkdeps')]
447 import checkdeps
448 from cpp_checker import CppChecker
449 from rules import Rule
450 finally:
451 # Restore sys.path to what it was before.
452 sys.path = original_sys_path
454 added_includes = []
455 for f in input_api.AffectedFiles():
456 if not CppChecker.IsCppFile(f.LocalPath()):
457 continue
459 changed_lines = [line for line_num, line in f.ChangedContents()]
460 added_includes.append([f.LocalPath(), changed_lines])
462 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
464 error_descriptions = []
465 warning_descriptions = []
466 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
467 added_includes):
468 description_with_path = '%s\n %s' % (path, rule_description)
469 if rule_type == Rule.DISALLOW:
470 error_descriptions.append(description_with_path)
471 else:
472 warning_descriptions.append(description_with_path)
474 results = []
475 if error_descriptions:
476 results.append(output_api.PresubmitError(
477 'You added one or more #includes that violate checkdeps rules.',
478 error_descriptions))
479 if warning_descriptions:
480 results.append(output_api.PresubmitPromptOrNotify(
481 'You added one or more #includes of files that are temporarily\n'
482 'allowed but being removed. Can you avoid introducing the\n'
483 '#include? See relevant DEPS file(s) for details and contacts.',
484 warning_descriptions))
485 return results
488 def _CheckFilePermissions(input_api, output_api):
489 """Check that all files have their permissions properly set."""
490 args = [sys.executable, 'tools/checkperms/checkperms.py', '--root',
491 input_api.change.RepositoryRoot()]
492 for f in input_api.AffectedFiles():
493 args += ['--file', f.LocalPath()]
494 errors = []
495 (errors, stderrdata) = subprocess.Popen(args).communicate()
497 results = []
498 if errors:
499 results.append(output_api.PresubmitError('checkperms.py failed.',
500 errors))
501 return results
504 def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
505 """Makes sure we don't include ui/aura/window_property.h
506 in header files.
508 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
509 errors = []
510 for f in input_api.AffectedFiles():
511 if not f.LocalPath().endswith('.h'):
512 continue
513 for line_num, line in f.ChangedContents():
514 if pattern.match(line):
515 errors.append(' %s:%d' % (f.LocalPath(), line_num))
517 results = []
518 if errors:
519 results.append(output_api.PresubmitError(
520 'Header files should not include ui/aura/window_property.h', errors))
521 return results
524 def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
525 """Checks that the lines in scope occur in the right order.
527 1. C system files in alphabetical order
528 2. C++ system files in alphabetical order
529 3. Project's .h files
532 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
533 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
534 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
536 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
538 state = C_SYSTEM_INCLUDES
540 previous_line = ''
541 previous_line_num = 0
542 problem_linenums = []
543 for line_num, line in scope:
544 if c_system_include_pattern.match(line):
545 if state != C_SYSTEM_INCLUDES:
546 problem_linenums.append((line_num, previous_line_num))
547 elif previous_line and previous_line > line:
548 problem_linenums.append((line_num, previous_line_num))
549 elif cpp_system_include_pattern.match(line):
550 if state == C_SYSTEM_INCLUDES:
551 state = CPP_SYSTEM_INCLUDES
552 elif state == CUSTOM_INCLUDES:
553 problem_linenums.append((line_num, previous_line_num))
554 elif previous_line and previous_line > line:
555 problem_linenums.append((line_num, previous_line_num))
556 elif custom_include_pattern.match(line):
557 if state != CUSTOM_INCLUDES:
558 state = CUSTOM_INCLUDES
559 elif previous_line and previous_line > line:
560 problem_linenums.append((line_num, previous_line_num))
561 else:
562 problem_linenums.append(line_num)
563 previous_line = line
564 previous_line_num = line_num
566 warnings = []
567 for (line_num, previous_line_num) in problem_linenums:
568 if line_num in changed_linenums or previous_line_num in changed_linenums:
569 warnings.append(' %s:%d' % (file_path, line_num))
570 return warnings
573 def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
574 """Checks the #include order for the given file f."""
576 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
577 # Exclude the following includes from the check:
578 # 1) #include <.../...>, e.g., <sys/...> includes often need to appear in a
579 # specific order.
580 # 2) <atlbase.h>, "build/build_config.h"
581 excluded_include_pattern = input_api.re.compile(
582 r'\s*#include (\<.*/.*|\<atlbase\.h\>|"build/build_config.h")')
583 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
584 # Match the final or penultimate token if it is xxxtest so we can ignore it
585 # when considering the special first include.
586 test_file_tag_pattern = input_api.re.compile(
587 r'_[a-z]+test(?=(_[a-zA-Z0-9]+)?\.)')
588 if_pattern = input_api.re.compile(
589 r'\s*#\s*(if|elif|else|endif|define|undef).*')
590 # Some files need specialized order of includes; exclude such files from this
591 # check.
592 uncheckable_includes_pattern = input_api.re.compile(
593 r'\s*#include '
594 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
596 contents = f.NewContents()
597 warnings = []
598 line_num = 0
600 # Handle the special first include. If the first include file is
601 # some/path/file.h, the corresponding including file can be some/path/file.cc,
602 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
603 # etc. It's also possible that no special first include exists.
604 # If the included file is some/path/file_platform.h the including file could
605 # also be some/path/file_xxxtest_platform.h.
606 including_file_base_name = test_file_tag_pattern.sub(
607 '', input_api.os_path.basename(f.LocalPath()))
609 for line in contents:
610 line_num += 1
611 if system_include_pattern.match(line):
612 # No special first include -> process the line again along with normal
613 # includes.
614 line_num -= 1
615 break
616 match = custom_include_pattern.match(line)
617 if match:
618 match_dict = match.groupdict()
619 header_basename = test_file_tag_pattern.sub(
620 '', input_api.os_path.basename(match_dict['FILE'])).replace('.h', '')
622 if header_basename not in including_file_base_name:
623 # No special first include -> process the line again along with normal
624 # includes.
625 line_num -= 1
626 break
628 # Split into scopes: Each region between #if and #endif is its own scope.
629 scopes = []
630 current_scope = []
631 for line in contents[line_num:]:
632 line_num += 1
633 if uncheckable_includes_pattern.match(line):
634 return []
635 if if_pattern.match(line):
636 scopes.append(current_scope)
637 current_scope = []
638 elif ((system_include_pattern.match(line) or
639 custom_include_pattern.match(line)) and
640 not excluded_include_pattern.match(line)):
641 current_scope.append((line_num, line))
642 scopes.append(current_scope)
644 for scope in scopes:
645 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
646 changed_linenums))
647 return warnings
650 def _CheckIncludeOrder(input_api, output_api):
651 """Checks that the #include order is correct.
653 1. The corresponding header for source files.
654 2. C system files in alphabetical order
655 3. C++ system files in alphabetical order
656 4. Project's .h files in alphabetical order
658 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
659 these rules separately.
662 warnings = []
663 for f in input_api.AffectedFiles():
664 if f.LocalPath().endswith(('.cc', '.h')):
665 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
666 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
668 results = []
669 if warnings:
670 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
671 warnings))
672 return results
675 def _CheckForVersionControlConflictsInFile(input_api, f):
676 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
677 errors = []
678 for line_num, line in f.ChangedContents():
679 if pattern.match(line):
680 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
681 return errors
684 def _CheckForVersionControlConflicts(input_api, output_api):
685 """Usually this is not intentional and will cause a compile failure."""
686 errors = []
687 for f in input_api.AffectedFiles():
688 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
690 results = []
691 if errors:
692 results.append(output_api.PresubmitError(
693 'Version control conflict markers found, please resolve.', errors))
694 return results
697 def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
698 def FilterFile(affected_file):
699 """Filter function for use with input_api.AffectedSourceFiles,
700 below. This filters out everything except non-test files from
701 top-level directories that generally speaking should not hard-code
702 service URLs (e.g. src/android_webview/, src/content/ and others).
704 return input_api.FilterSourceFile(
705 affected_file,
706 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
707 black_list=(_EXCLUDED_PATHS +
708 _TEST_CODE_EXCLUDED_PATHS +
709 input_api.DEFAULT_BLACK_LIST))
711 base_pattern = '"[^"]*google\.com[^"]*"'
712 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
713 pattern = input_api.re.compile(base_pattern)
714 problems = [] # items are (filename, line_number, line)
715 for f in input_api.AffectedSourceFiles(FilterFile):
716 for line_num, line in f.ChangedContents():
717 if not comment_pattern.search(line) and pattern.search(line):
718 problems.append((f.LocalPath(), line_num, line))
720 if problems:
721 return [output_api.PresubmitPromptOrNotify(
722 'Most layers below src/chrome/ should not hardcode service URLs.\n'
723 'Are you sure this is correct? (Contact: joi@chromium.org)',
724 [' %s:%d: %s' % (
725 problem[0], problem[1], problem[2]) for problem in problems])]
726 else:
727 return []
730 def _CheckNoAbbreviationInPngFileName(input_api, output_api):
731 """Makes sure there are no abbreviations in the name of PNG files.
733 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
734 errors = []
735 for f in input_api.AffectedFiles(include_deletes=False):
736 if pattern.match(f.LocalPath()):
737 errors.append(' %s' % f.LocalPath())
739 results = []
740 if errors:
741 results.append(output_api.PresubmitError(
742 'The name of PNG files should not have abbreviations. \n'
743 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
744 'Contact oshima@chromium.org if you have questions.', errors))
745 return results
748 def _DepsFilesToCheck(re, changed_lines):
749 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
750 a set of DEPS entries that we should look up."""
751 # We ignore deps entries on auto-generated directories.
752 AUTO_GENERATED_DIRS = ['grit', 'jni']
754 # This pattern grabs the path without basename in the first
755 # parentheses, and the basename (if present) in the second. It
756 # relies on the simple heuristic that if there is a basename it will
757 # be a header file ending in ".h".
758 pattern = re.compile(
759 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
760 results = set()
761 for changed_line in changed_lines:
762 m = pattern.match(changed_line)
763 if m:
764 path = m.group(1)
765 if path.split('/')[0] not in AUTO_GENERATED_DIRS:
766 results.add('%s/DEPS' % m.group(1))
767 return results
770 def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
771 """When a dependency prefixed with + is added to a DEPS file, we
772 want to make sure that the change is reviewed by an OWNER of the
773 target file or directory, to avoid layering violations from being
774 introduced. This check verifies that this happens.
776 changed_lines = set()
777 for f in input_api.AffectedFiles():
778 filename = input_api.os_path.basename(f.LocalPath())
779 if filename == 'DEPS':
780 changed_lines |= set(line.strip()
781 for line_num, line
782 in f.ChangedContents())
783 if not changed_lines:
784 return []
786 virtual_depended_on_files = _DepsFilesToCheck(input_api.re, changed_lines)
787 if not virtual_depended_on_files:
788 return []
790 if input_api.is_committing:
791 if input_api.tbr:
792 return [output_api.PresubmitNotifyResult(
793 '--tbr was specified, skipping OWNERS check for DEPS additions')]
794 if not input_api.change.issue:
795 return [output_api.PresubmitError(
796 "DEPS approval by OWNERS check failed: this change has "
797 "no Rietveld issue number, so we can't check it for approvals.")]
798 output = output_api.PresubmitError
799 else:
800 output = output_api.PresubmitNotifyResult
802 owners_db = input_api.owners_db
803 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
804 input_api,
805 owners_db.email_regexp,
806 approval_needed=input_api.is_committing)
808 owner_email = owner_email or input_api.change.author_email
810 reviewers_plus_owner = set(reviewers)
811 if owner_email:
812 reviewers_plus_owner.add(owner_email)
813 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
814 reviewers_plus_owner)
815 unapproved_dependencies = ["'+%s'," % path[:-len('/DEPS')]
816 for path in missing_files]
818 if unapproved_dependencies:
819 output_list = [
820 output('Missing LGTM from OWNERS of directories added to DEPS:\n %s' %
821 '\n '.join(sorted(unapproved_dependencies)))]
822 if not input_api.is_committing:
823 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
824 output_list.append(output(
825 'Suggested missing target path OWNERS:\n %s' %
826 '\n '.join(suggested_owners or [])))
827 return output_list
829 return []
832 def _CheckSpamLogging(input_api, output_api):
833 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
834 black_list = (_EXCLUDED_PATHS +
835 _TEST_CODE_EXCLUDED_PATHS +
836 input_api.DEFAULT_BLACK_LIST +
837 (r"^base[\\\/]logging\.h$",
838 r"^chrome[\\\/]renderer[\\\/]extensions[\\\/]"
839 r"logging_native_handler\.cc",
840 r"^remoting[\\\/]base[\\\/]logging\.h$",
841 r"^sandbox[\\\/]linux[\\\/].*",))
842 source_file_filter = lambda x: input_api.FilterSourceFile(
843 x, white_list=(file_inclusion_pattern,), black_list=black_list)
845 log_info = []
846 printf = []
848 for f in input_api.AffectedSourceFiles(source_file_filter):
849 contents = input_api.ReadFile(f, 'rb')
850 if re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", contents):
851 log_info.append(f.LocalPath())
852 if re.search(r"\bD?LOG_IF\s*\(\s*INFO\s*,", contents):
853 log_info.append(f.LocalPath())
854 if re.search(r"\bf?printf\((stdout|stderr)", contents):
855 printf.append(f.LocalPath())
857 if log_info:
858 return [output_api.PresubmitError(
859 'These files spam the console log with LOG(INFO):',
860 items=log_info)]
861 if printf:
862 return [output_api.PresubmitError(
863 'These files spam the console log with printf/fprintf:',
864 items=printf)]
865 return []
868 def _CheckCygwinShell(input_api, output_api):
869 source_file_filter = lambda x: input_api.FilterSourceFile(
870 x, white_list=(r'.+\.(gyp|gypi)$',))
871 cygwin_shell = []
873 for f in input_api.AffectedSourceFiles(source_file_filter):
874 for linenum, line in f.ChangedContents():
875 if 'msvs_cygwin_shell' in line:
876 cygwin_shell.append(f.LocalPath())
877 break
879 if cygwin_shell:
880 return [output_api.PresubmitError(
881 'These files should not use msvs_cygwin_shell (the default is 0):',
882 items=cygwin_shell)]
883 return []
886 def _CommonChecks(input_api, output_api):
887 """Checks common to both upload and commit."""
888 results = []
889 results.extend(input_api.canned_checks.PanProjectChecks(
890 input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
891 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
892 results.extend(
893 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
894 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
895 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
896 results.extend(_CheckNoNewWStrings(input_api, output_api))
897 results.extend(_CheckNoDEPSGIT(input_api, output_api))
898 results.extend(_CheckNoBannedFunctions(input_api, output_api))
899 results.extend(_CheckNoPragmaOnce(input_api, output_api))
900 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
901 results.extend(_CheckUnwantedDependencies(input_api, output_api))
902 results.extend(_CheckFilePermissions(input_api, output_api))
903 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
904 results.extend(_CheckIncludeOrder(input_api, output_api))
905 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
906 results.extend(_CheckPatchFiles(input_api, output_api))
907 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
908 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
909 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
910 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
911 results.extend(
912 input_api.canned_checks.CheckChangeHasNoTabs(
913 input_api,
914 output_api,
915 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
916 results.extend(_CheckSpamLogging(input_api, output_api))
917 results.extend(_CheckCygwinShell(input_api, output_api))
919 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
920 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
921 input_api, output_api,
922 input_api.PresubmitLocalPath(),
923 whitelist=[r'^PRESUBMIT_test\.py$']))
924 return results
927 def _CheckSubversionConfig(input_api, output_api):
928 """Verifies the subversion config file is correctly setup.
930 Checks that autoprops are enabled, returns an error otherwise.
932 join = input_api.os_path.join
933 if input_api.platform == 'win32':
934 appdata = input_api.environ.get('APPDATA', '')
935 if not appdata:
936 return [output_api.PresubmitError('%APPDATA% is not configured.')]
937 path = join(appdata, 'Subversion', 'config')
938 else:
939 home = input_api.environ.get('HOME', '')
940 if not home:
941 return [output_api.PresubmitError('$HOME is not configured.')]
942 path = join(home, '.subversion', 'config')
944 error_msg = (
945 'Please look at http://dev.chromium.org/developers/coding-style to\n'
946 'configure your subversion configuration file. This enables automatic\n'
947 'properties to simplify the project maintenance.\n'
948 'Pro-tip: just download and install\n'
949 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
951 try:
952 lines = open(path, 'r').read().splitlines()
953 # Make sure auto-props is enabled and check for 2 Chromium standard
954 # auto-prop.
955 if (not '*.cc = svn:eol-style=LF' in lines or
956 not '*.pdf = svn:mime-type=application/pdf' in lines or
957 not 'enable-auto-props = yes' in lines):
958 return [
959 output_api.PresubmitNotifyResult(
960 'It looks like you have not configured your subversion config '
961 'file or it is not up-to-date.\n' + error_msg)
963 except (OSError, IOError):
964 return [
965 output_api.PresubmitNotifyResult(
966 'Can\'t find your subversion config file.\n' + error_msg)
968 return []
971 def _CheckAuthorizedAuthor(input_api, output_api):
972 """For non-googler/chromites committers, verify the author's email address is
973 in AUTHORS.
975 # TODO(maruel): Add it to input_api?
976 import fnmatch
978 author = input_api.change.author_email
979 if not author:
980 input_api.logging.info('No author, skipping AUTHOR check')
981 return []
982 authors_path = input_api.os_path.join(
983 input_api.PresubmitLocalPath(), 'AUTHORS')
984 valid_authors = (
985 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
986 for line in open(authors_path))
987 valid_authors = [item.group(1).lower() for item in valid_authors if item]
988 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
989 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
990 return [output_api.PresubmitPromptWarning(
991 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
992 '\n'
993 'http://www.chromium.org/developers/contributing-code and read the '
994 '"Legal" section\n'
995 'If you are a chromite, verify the contributor signed the CLA.') %
996 author)]
997 return []
1000 def _CheckPatchFiles(input_api, output_api):
1001 problems = [f.LocalPath() for f in input_api.AffectedFiles()
1002 if f.LocalPath().endswith(('.orig', '.rej'))]
1003 if problems:
1004 return [output_api.PresubmitError(
1005 "Don't commit .rej and .orig files.", problems)]
1006 else:
1007 return []
1010 def _DidYouMeanOSMacro(bad_macro):
1011 try:
1012 return {'A': 'OS_ANDROID',
1013 'B': 'OS_BSD',
1014 'C': 'OS_CHROMEOS',
1015 'F': 'OS_FREEBSD',
1016 'L': 'OS_LINUX',
1017 'M': 'OS_MACOSX',
1018 'N': 'OS_NACL',
1019 'O': 'OS_OPENBSD',
1020 'P': 'OS_POSIX',
1021 'S': 'OS_SOLARIS',
1022 'W': 'OS_WIN'}[bad_macro[3].upper()]
1023 except KeyError:
1024 return ''
1027 def _CheckForInvalidOSMacrosInFile(input_api, f):
1028 """Check for sensible looking, totally invalid OS macros."""
1029 preprocessor_statement = input_api.re.compile(r'^\s*#')
1030 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
1031 results = []
1032 for lnum, line in f.ChangedContents():
1033 if preprocessor_statement.search(line):
1034 for match in os_macro.finditer(line):
1035 if not match.group(1) in _VALID_OS_MACROS:
1036 good = _DidYouMeanOSMacro(match.group(1))
1037 did_you_mean = ' (did you mean %s?)' % good if good else ''
1038 results.append(' %s:%d %s%s' % (f.LocalPath(),
1039 lnum,
1040 match.group(1),
1041 did_you_mean))
1042 return results
1045 def _CheckForInvalidOSMacros(input_api, output_api):
1046 """Check all affected files for invalid OS macros."""
1047 bad_macros = []
1048 for f in input_api.AffectedFiles():
1049 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
1050 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
1052 if not bad_macros:
1053 return []
1055 return [output_api.PresubmitError(
1056 'Possibly invalid OS macro[s] found. Please fix your code\n'
1057 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
1060 def CheckChangeOnUpload(input_api, output_api):
1061 results = []
1062 results.extend(_CommonChecks(input_api, output_api))
1063 return results
1066 def CheckChangeOnCommit(input_api, output_api):
1067 results = []
1068 results.extend(_CommonChecks(input_api, output_api))
1069 # TODO(thestig) temporarily disabled, doesn't work in third_party/
1070 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
1071 # input_api, output_api, sources))
1072 # Make sure the tree is 'open'.
1073 results.extend(input_api.canned_checks.CheckTreeIsOpen(
1074 input_api,
1075 output_api,
1076 json_url='http://chromium-status.appspot.com/current?format=json'))
1077 results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api,
1078 output_api, 'http://codereview.chromium.org',
1079 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
1080 'tryserver@chromium.org'))
1082 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1083 input_api, output_api))
1084 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1085 input_api, output_api))
1086 results.extend(_CheckSubversionConfig(input_api, output_api))
1087 return results
1090 def GetPreferredTrySlaves(project, change):
1091 files = change.LocalPaths()
1093 if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files):
1094 return []
1096 if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files):
1097 return ['mac_rel', 'mac:compile']
1098 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
1099 return ['win_rel', 'win:compile']
1100 if all(re.search('(^|[/_])android[/_.]', f) for f in files):
1101 return ['android_aosp', 'android_dbg', 'android_clang_dbg']
1102 if all(re.search('^native_client_sdk', f) for f in files):
1103 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
1104 if all(re.search('[/_]ios[/_.]', f) for f in files):
1105 return ['ios_rel_device', 'ios_dbg_simulator']
1107 trybots = [
1108 'android_clang_dbg',
1109 'android_dbg',
1110 'ios_dbg_simulator',
1111 'ios_rel_device',
1112 'linux_asan',
1113 'linux_aura',
1114 'linux_chromeos',
1115 'linux_clang:compile',
1116 'linux_rel',
1117 'mac_rel',
1118 'mac:compile',
1119 'win_rel',
1120 'win:compile',
1121 'win_x64_rel:base_unittests',
1124 # Match things like path/aura/file.cc and path/file_aura.cc.
1125 # Same for chromeos.
1126 if any(re.search('[/_](aura|chromeos)', f) for f in files):
1127 trybots += ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
1129 # If there are gyp changes to base, build, or chromeos, run a full cros build
1130 # in addition to the shorter linux_chromeos build. Changes to high level gyp
1131 # files have a much higher chance of breaking the cros build, which is
1132 # differnt from the linux_chromeos build that most chrome developers test
1133 # with.
1134 if any(re.search('^(base|build|chromeos).*\.gypi?$', f) for f in files):
1135 trybots += ['cros_x86']
1137 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1138 # unless they're .gyp(i) files as changes to those files can break the gyp
1139 # step on that bot.
1140 if (not all(re.search('^chrome', f) for f in files) or
1141 any(re.search('\.gypi?$', f) for f in files)):
1142 trybots += ['android_aosp']
1144 return trybots