QUIC - use size_t for kMaxInitialRoundTripTimeUs.
[chromium-blink-merge.git] / PRESUBMIT.py
blob5f2a11b5e8534f79c1f50a45aeb590c2f73e161e
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[/\\].*',
50 _TEST_ONLY_WARNING = (
51 'You might be calling functions intended only for testing from\n'
52 'production code. It is OK to ignore this warning if you know what\n'
53 'you are doing, as the heuristics used to detect the situation are\n'
54 'not perfect. The commit queue will not block on this warning.\n'
55 'Email joi@chromium.org if you have questions.')
58 _INCLUDE_ORDER_WARNING = (
59 'Your #include order seems to be broken. Send mail to\n'
60 'marja@chromium.org if this is not the case.')
63 _BANNED_OBJC_FUNCTIONS = (
65 'addTrackingRect:',
67 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is'
68 'prohibited. Please use CrTrackingArea instead.',
69 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
71 False,
74 'NSTrackingArea',
76 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea',
77 'instead.',
78 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
80 False,
83 'convertPointFromBase:',
85 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.',
86 'Please use |convertPoint:(point) fromView:nil| instead.',
87 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
89 True,
92 'convertPointToBase:',
94 'The use of -[NSView convertPointToBase:] is almost certainly wrong.',
95 'Please use |convertPoint:(point) toView:nil| instead.',
96 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
98 True,
101 'convertRectFromBase:',
103 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.',
104 'Please use |convertRect:(point) fromView:nil| instead.',
105 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
107 True,
110 'convertRectToBase:',
112 'The use of -[NSView convertRectToBase:] is almost certainly wrong.',
113 'Please use |convertRect:(point) toView:nil| instead.',
114 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
116 True,
119 'convertSizeFromBase:',
121 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.',
122 'Please use |convertSize:(point) fromView:nil| instead.',
123 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
125 True,
128 'convertSizeToBase:',
130 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.',
131 'Please use |convertSize:(point) toView:nil| instead.',
132 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts',
134 True,
139 _BANNED_CPP_FUNCTIONS = (
140 # Make sure that gtest's FRIEND_TEST() macro is not used; the
141 # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be
142 # used instead since that allows for FLAKY_ and DISABLED_ prefixes.
144 'FRIEND_TEST(',
146 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include',
147 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.',
149 False,
153 'ScopedAllowIO',
155 'New code should not use ScopedAllowIO. Post a task to the blocking',
156 'pool or the FILE thread instead.',
158 True,
160 r"^components[\\\/]breakpad[\\\/]app[\\\/]breakpad_mac\.mm$",
161 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_browser_main\.cc$",
162 r"^content[\\\/]shell[\\\/]browser[\\\/]shell_message_filter\.cc$",
163 r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$",
167 'SkRefPtr',
169 'The use of SkRefPtr is prohibited. ',
170 'Please use skia::RefPtr instead.'
172 True,
176 'SkAutoRef',
178 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ',
179 'Please use skia::RefPtr instead.'
181 True,
185 'SkAutoTUnref',
187 'The use of SkAutoTUnref is dangerous because it implicitly ',
188 'converts to a raw pointer. Please use skia::RefPtr instead.'
190 True,
194 'SkAutoUnref',
196 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ',
197 'because it implicitly converts to a raw pointer. ',
198 'Please use skia::RefPtr instead.'
200 True,
206 _VALID_OS_MACROS = (
207 # Please keep sorted.
208 'OS_ANDROID',
209 'OS_BSD',
210 'OS_CAT', # For testing.
211 'OS_CHROMEOS',
212 'OS_FREEBSD',
213 'OS_IOS',
214 'OS_LINUX',
215 'OS_MACOSX',
216 'OS_NACL',
217 'OS_OPENBSD',
218 'OS_POSIX',
219 'OS_SOLARIS',
220 'OS_WIN',
224 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
225 """Attempts to prevent use of functions intended only for testing in
226 non-testing code. For now this is just a best-effort implementation
227 that ignores header files and may have some false positives. A
228 better implementation would probably need a proper C++ parser.
230 # We only scan .cc files and the like, as the declaration of
231 # for-testing functions in header files are hard to distinguish from
232 # calls to such functions without a proper C++ parser.
233 file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS
235 base_function_pattern = r'ForTest(ing)?|for_test(ing)?'
236 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
237 comment_pattern = input_api.re.compile(r'//.*%s' % base_function_pattern)
238 exclusion_pattern = input_api.re.compile(
239 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
240 base_function_pattern, base_function_pattern))
242 def FilterFile(affected_file):
243 black_list = (_EXCLUDED_PATHS +
244 _TEST_CODE_EXCLUDED_PATHS +
245 input_api.DEFAULT_BLACK_LIST)
246 return input_api.FilterSourceFile(
247 affected_file,
248 white_list=(file_inclusion_pattern, ),
249 black_list=black_list)
251 problems = []
252 for f in input_api.AffectedSourceFiles(FilterFile):
253 local_path = f.LocalPath()
254 lines = input_api.ReadFile(f).splitlines()
255 line_number = 0
256 for line in lines:
257 if (inclusion_pattern.search(line) and
258 not comment_pattern.search(line) and
259 not exclusion_pattern.search(line)):
260 problems.append(
261 '%s:%d\n %s' % (local_path, line_number, line.strip()))
262 line_number += 1
264 if problems:
265 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
266 else:
267 return []
270 def _CheckNoIOStreamInHeaders(input_api, output_api):
271 """Checks to make sure no .h files include <iostream>."""
272 files = []
273 pattern = input_api.re.compile(r'^#include\s*<iostream>',
274 input_api.re.MULTILINE)
275 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
276 if not f.LocalPath().endswith('.h'):
277 continue
278 contents = input_api.ReadFile(f)
279 if pattern.search(contents):
280 files.append(f)
282 if len(files):
283 return [ output_api.PresubmitError(
284 'Do not #include <iostream> in header files, since it inserts static '
285 'initialization into every file including the header. Instead, '
286 '#include <ostream>. See http://crbug.com/94794',
287 files) ]
288 return []
291 def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api):
292 """Checks to make sure no source files use UNIT_TEST"""
293 problems = []
294 for f in input_api.AffectedFiles():
295 if (not f.LocalPath().endswith(('.cc', '.mm'))):
296 continue
298 for line_num, line in f.ChangedContents():
299 if 'UNIT_TEST' in line:
300 problems.append(' %s:%d' % (f.LocalPath(), line_num))
302 if not problems:
303 return []
304 return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' +
305 '\n'.join(problems))]
308 def _CheckNoNewWStrings(input_api, output_api):
309 """Checks to make sure we don't introduce use of wstrings."""
310 problems = []
311 for f in input_api.AffectedFiles():
312 if (not f.LocalPath().endswith(('.cc', '.h')) or
313 f.LocalPath().endswith(('test.cc', '_win.cc', '_win.h'))):
314 continue
316 allowWString = False
317 for line_num, line in f.ChangedContents():
318 if 'presubmit: allow wstring' in line:
319 allowWString = True
320 elif not allowWString and 'wstring' in line:
321 problems.append(' %s:%d' % (f.LocalPath(), line_num))
322 allowWString = False
323 else:
324 allowWString = False
326 if not problems:
327 return []
328 return [output_api.PresubmitPromptWarning('New code should not use wstrings.'
329 ' If you are calling a cross-platform API that accepts a wstring, '
330 'fix the API.\n' +
331 '\n'.join(problems))]
334 def _CheckNoDEPSGIT(input_api, output_api):
335 """Make sure .DEPS.git is never modified manually."""
336 if any(f.LocalPath().endswith('.DEPS.git') for f in
337 input_api.AffectedFiles()):
338 return [output_api.PresubmitError(
339 'Never commit changes to .DEPS.git. This file is maintained by an\n'
340 'automated system based on what\'s in DEPS and your changes will be\n'
341 'overwritten.\n'
342 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n'
343 'for more information')]
344 return []
347 def _CheckNoBannedFunctions(input_api, output_api):
348 """Make sure that banned functions are not used."""
349 warnings = []
350 errors = []
352 file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h'))
353 for f in input_api.AffectedFiles(file_filter=file_filter):
354 for line_num, line in f.ChangedContents():
355 for func_name, message, error in _BANNED_OBJC_FUNCTIONS:
356 if func_name in line:
357 problems = warnings;
358 if error:
359 problems = errors;
360 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
361 for message_line in message:
362 problems.append(' %s' % message_line)
364 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h'))
365 for f in input_api.AffectedFiles(file_filter=file_filter):
366 for line_num, line in f.ChangedContents():
367 for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS:
368 def IsBlacklisted(affected_file, blacklist):
369 local_path = affected_file.LocalPath()
370 for item in blacklist:
371 if input_api.re.match(item, local_path):
372 return True
373 return False
374 if IsBlacklisted(f, excluded_paths):
375 continue
376 if func_name in line:
377 problems = warnings;
378 if error:
379 problems = errors;
380 problems.append(' %s:%d:' % (f.LocalPath(), line_num))
381 for message_line in message:
382 problems.append(' %s' % message_line)
384 result = []
385 if (warnings):
386 result.append(output_api.PresubmitPromptWarning(
387 'Banned functions were used.\n' + '\n'.join(warnings)))
388 if (errors):
389 result.append(output_api.PresubmitError(
390 'Banned functions were used.\n' + '\n'.join(errors)))
391 return result
394 def _CheckNoPragmaOnce(input_api, output_api):
395 """Make sure that banned functions are not used."""
396 files = []
397 pattern = input_api.re.compile(r'^#pragma\s+once',
398 input_api.re.MULTILINE)
399 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
400 if not f.LocalPath().endswith('.h'):
401 continue
402 contents = input_api.ReadFile(f)
403 if pattern.search(contents):
404 files.append(f)
406 if files:
407 return [output_api.PresubmitError(
408 'Do not use #pragma once in header files.\n'
409 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
410 files)]
411 return []
414 def _CheckNoTrinaryTrueFalse(input_api, output_api):
415 """Checks to make sure we don't introduce use of foo ? true : false."""
416 problems = []
417 pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)')
418 for f in input_api.AffectedFiles():
419 if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')):
420 continue
422 for line_num, line in f.ChangedContents():
423 if pattern.match(line):
424 problems.append(' %s:%d' % (f.LocalPath(), line_num))
426 if not problems:
427 return []
428 return [output_api.PresubmitPromptWarning(
429 'Please consider avoiding the "? true : false" pattern if possible.\n' +
430 '\n'.join(problems))]
433 def _CheckUnwantedDependencies(input_api, output_api):
434 """Runs checkdeps on #include statements added in this
435 change. Breaking - rules is an error, breaking ! rules is a
436 warning.
438 # We need to wait until we have an input_api object and use this
439 # roundabout construct to import checkdeps because this file is
440 # eval-ed and thus doesn't have __file__.
441 original_sys_path = sys.path
442 try:
443 sys.path = sys.path + [input_api.os_path.join(
444 input_api.PresubmitLocalPath(), 'tools', 'checkdeps')]
445 import checkdeps
446 from cpp_checker import CppChecker
447 from rules import Rule
448 finally:
449 # Restore sys.path to what it was before.
450 sys.path = original_sys_path
452 added_includes = []
453 for f in input_api.AffectedFiles():
454 if not CppChecker.IsCppFile(f.LocalPath()):
455 continue
457 changed_lines = [line for line_num, line in f.ChangedContents()]
458 added_includes.append([f.LocalPath(), changed_lines])
460 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
462 error_descriptions = []
463 warning_descriptions = []
464 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
465 added_includes):
466 description_with_path = '%s\n %s' % (path, rule_description)
467 if rule_type == Rule.DISALLOW:
468 error_descriptions.append(description_with_path)
469 else:
470 warning_descriptions.append(description_with_path)
472 results = []
473 if error_descriptions:
474 results.append(output_api.PresubmitError(
475 'You added one or more #includes that violate checkdeps rules.',
476 error_descriptions))
477 if warning_descriptions:
478 results.append(output_api.PresubmitPromptOrNotify(
479 'You added one or more #includes of files that are temporarily\n'
480 'allowed but being removed. Can you avoid introducing the\n'
481 '#include? See relevant DEPS file(s) for details and contacts.',
482 warning_descriptions))
483 return results
486 def _CheckFilePermissions(input_api, output_api):
487 """Check that all files have their permissions properly set."""
488 args = [sys.executable, 'tools/checkperms/checkperms.py', '--root',
489 input_api.change.RepositoryRoot()]
490 for f in input_api.AffectedFiles():
491 args += ['--file', f.LocalPath()]
492 errors = []
493 (errors, stderrdata) = subprocess.Popen(args).communicate()
495 results = []
496 if errors:
497 results.append(output_api.PresubmitError('checkperms.py failed.',
498 errors))
499 return results
502 def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api):
503 """Makes sure we don't include ui/aura/window_property.h
504 in header files.
506 pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"')
507 errors = []
508 for f in input_api.AffectedFiles():
509 if not f.LocalPath().endswith('.h'):
510 continue
511 for line_num, line in f.ChangedContents():
512 if pattern.match(line):
513 errors.append(' %s:%d' % (f.LocalPath(), line_num))
515 results = []
516 if errors:
517 results.append(output_api.PresubmitError(
518 'Header files should not include ui/aura/window_property.h', errors))
519 return results
522 def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums):
523 """Checks that the lines in scope occur in the right order.
525 1. C system files in alphabetical order
526 2. C++ system files in alphabetical order
527 3. Project's .h files
530 c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>')
531 cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>')
532 custom_include_pattern = input_api.re.compile(r'\s*#include ".*')
534 C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3)
536 state = C_SYSTEM_INCLUDES
538 previous_line = ''
539 previous_line_num = 0
540 problem_linenums = []
541 for line_num, line in scope:
542 if c_system_include_pattern.match(line):
543 if state != C_SYSTEM_INCLUDES:
544 problem_linenums.append((line_num, previous_line_num))
545 elif previous_line and previous_line > line:
546 problem_linenums.append((line_num, previous_line_num))
547 elif cpp_system_include_pattern.match(line):
548 if state == C_SYSTEM_INCLUDES:
549 state = CPP_SYSTEM_INCLUDES
550 elif state == CUSTOM_INCLUDES:
551 problem_linenums.append((line_num, previous_line_num))
552 elif previous_line and previous_line > line:
553 problem_linenums.append((line_num, previous_line_num))
554 elif custom_include_pattern.match(line):
555 if state != CUSTOM_INCLUDES:
556 state = CUSTOM_INCLUDES
557 elif previous_line and previous_line > line:
558 problem_linenums.append((line_num, previous_line_num))
559 else:
560 problem_linenums.append(line_num)
561 previous_line = line
562 previous_line_num = line_num
564 warnings = []
565 for (line_num, previous_line_num) in problem_linenums:
566 if line_num in changed_linenums or previous_line_num in changed_linenums:
567 warnings.append(' %s:%d' % (file_path, line_num))
568 return warnings
571 def _CheckIncludeOrderInFile(input_api, f, changed_linenums):
572 """Checks the #include order for the given file f."""
574 system_include_pattern = input_api.re.compile(r'\s*#include \<.*')
575 # Exclude the following includes from the check:
576 # 1) #include <.../...>, e.g., <sys/...> includes often need to appear in a
577 # specific order.
578 # 2) <atlbase.h>, "build/build_config.h"
579 excluded_include_pattern = input_api.re.compile(
580 r'\s*#include (\<.*/.*|\<atlbase\.h\>|"build/build_config.h")')
581 custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"')
582 # Match the final or penultimate token if it is xxxtest so we can ignore it
583 # when considering the special first include.
584 test_file_tag_pattern = input_api.re.compile(
585 r'_[a-z]+test(?=(_[a-zA-Z0-9]+)?\.)')
586 if_pattern = input_api.re.compile(
587 r'\s*#\s*(if|elif|else|endif|define|undef).*')
588 # Some files need specialized order of includes; exclude such files from this
589 # check.
590 uncheckable_includes_pattern = input_api.re.compile(
591 r'\s*#include '
592 '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*')
594 contents = f.NewContents()
595 warnings = []
596 line_num = 0
598 # Handle the special first include. If the first include file is
599 # some/path/file.h, the corresponding including file can be some/path/file.cc,
600 # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h
601 # etc. It's also possible that no special first include exists.
602 # If the included file is some/path/file_platform.h the including file could
603 # also be some/path/file_xxxtest_platform.h.
604 including_file_base_name = test_file_tag_pattern.sub(
605 '', input_api.os_path.basename(f.LocalPath()))
607 for line in contents:
608 line_num += 1
609 if system_include_pattern.match(line):
610 # No special first include -> process the line again along with normal
611 # includes.
612 line_num -= 1
613 break
614 match = custom_include_pattern.match(line)
615 if match:
616 match_dict = match.groupdict()
617 header_basename = test_file_tag_pattern.sub(
618 '', input_api.os_path.basename(match_dict['FILE'])).replace('.h', '')
620 if header_basename not in including_file_base_name:
621 # No special first include -> process the line again along with normal
622 # includes.
623 line_num -= 1
624 break
626 # Split into scopes: Each region between #if and #endif is its own scope.
627 scopes = []
628 current_scope = []
629 for line in contents[line_num:]:
630 line_num += 1
631 if uncheckable_includes_pattern.match(line):
632 return []
633 if if_pattern.match(line):
634 scopes.append(current_scope)
635 current_scope = []
636 elif ((system_include_pattern.match(line) or
637 custom_include_pattern.match(line)) and
638 not excluded_include_pattern.match(line)):
639 current_scope.append((line_num, line))
640 scopes.append(current_scope)
642 for scope in scopes:
643 warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(),
644 changed_linenums))
645 return warnings
648 def _CheckIncludeOrder(input_api, output_api):
649 """Checks that the #include order is correct.
651 1. The corresponding header for source files.
652 2. C system files in alphabetical order
653 3. C++ system files in alphabetical order
654 4. Project's .h files in alphabetical order
656 Each region separated by #if, #elif, #else, #endif, #define and #undef follows
657 these rules separately.
660 warnings = []
661 for f in input_api.AffectedFiles():
662 if f.LocalPath().endswith(('.cc', '.h')):
663 changed_linenums = set(line_num for line_num, _ in f.ChangedContents())
664 warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums))
666 results = []
667 if warnings:
668 results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING,
669 warnings))
670 return results
673 def _CheckForVersionControlConflictsInFile(input_api, f):
674 pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$')
675 errors = []
676 for line_num, line in f.ChangedContents():
677 if pattern.match(line):
678 errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line))
679 return errors
682 def _CheckForVersionControlConflicts(input_api, output_api):
683 """Usually this is not intentional and will cause a compile failure."""
684 errors = []
685 for f in input_api.AffectedFiles():
686 errors.extend(_CheckForVersionControlConflictsInFile(input_api, f))
688 results = []
689 if errors:
690 results.append(output_api.PresubmitError(
691 'Version control conflict markers found, please resolve.', errors))
692 return results
695 def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api):
696 def FilterFile(affected_file):
697 """Filter function for use with input_api.AffectedSourceFiles,
698 below. This filters out everything except non-test files from
699 top-level directories that generally speaking should not hard-code
700 service URLs (e.g. src/android_webview/, src/content/ and others).
702 return input_api.FilterSourceFile(
703 affected_file,
704 white_list=(r'^(android_webview|base|content|net)[\\\/].*', ),
705 black_list=(_EXCLUDED_PATHS +
706 _TEST_CODE_EXCLUDED_PATHS +
707 input_api.DEFAULT_BLACK_LIST))
709 base_pattern = '"[^"]*google\.com[^"]*"'
710 comment_pattern = input_api.re.compile('//.*%s' % base_pattern)
711 pattern = input_api.re.compile(base_pattern)
712 problems = [] # items are (filename, line_number, line)
713 for f in input_api.AffectedSourceFiles(FilterFile):
714 for line_num, line in f.ChangedContents():
715 if not comment_pattern.search(line) and pattern.search(line):
716 problems.append((f.LocalPath(), line_num, line))
718 if problems:
719 return [output_api.PresubmitPromptOrNotify(
720 'Most layers below src/chrome/ should not hardcode service URLs.\n'
721 'Are you sure this is correct? (Contact: joi@chromium.org)',
722 [' %s:%d: %s' % (
723 problem[0], problem[1], problem[2]) for problem in problems])]
724 else:
725 return []
728 def _CheckNoAbbreviationInPngFileName(input_api, output_api):
729 """Makes sure there are no abbreviations in the name of PNG files.
731 pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$')
732 errors = []
733 for f in input_api.AffectedFiles(include_deletes=False):
734 if pattern.match(f.LocalPath()):
735 errors.append(' %s' % f.LocalPath())
737 results = []
738 if errors:
739 results.append(output_api.PresubmitError(
740 'The name of PNG files should not have abbreviations. \n'
741 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n'
742 'Contact oshima@chromium.org if you have questions.', errors))
743 return results
746 def _DepsFilesToCheck(re, changed_lines):
747 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
748 a set of DEPS entries that we should look up."""
749 results = set()
751 # This pattern grabs the path without basename in the first
752 # parentheses, and the basename (if present) in the second. It
753 # relies on the simple heuristic that if there is a basename it will
754 # be a header file ending in ".h".
755 pattern = re.compile(
756 r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""")
757 for changed_line in changed_lines:
758 m = pattern.match(changed_line)
759 if m:
760 path = m.group(1)
761 if not (path.startswith('grit/') or path == 'grit'):
762 results.add('%s/DEPS' % m.group(1))
763 return results
766 def _CheckAddedDepsHaveTargetApprovals(input_api, output_api):
767 """When a dependency prefixed with + is added to a DEPS file, we
768 want to make sure that the change is reviewed by an OWNER of the
769 target file or directory, to avoid layering violations from being
770 introduced. This check verifies that this happens.
772 changed_lines = set()
773 for f in input_api.AffectedFiles():
774 filename = input_api.os_path.basename(f.LocalPath())
775 if filename == 'DEPS':
776 changed_lines |= set(line.strip()
777 for line_num, line
778 in f.ChangedContents())
779 if not changed_lines:
780 return []
782 virtual_depended_on_files = _DepsFilesToCheck(input_api.re, changed_lines)
783 if not virtual_depended_on_files:
784 return []
786 if input_api.is_committing:
787 if input_api.tbr:
788 return [output_api.PresubmitNotifyResult(
789 '--tbr was specified, skipping OWNERS check for DEPS additions')]
790 if not input_api.change.issue:
791 return [output_api.PresubmitError(
792 "DEPS approval by OWNERS check failed: this change has "
793 "no Rietveld issue number, so we can't check it for approvals.")]
794 output = output_api.PresubmitError
795 else:
796 output = output_api.PresubmitNotifyResult
798 owners_db = input_api.owners_db
799 owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers(
800 input_api,
801 owners_db.email_regexp,
802 approval_needed=input_api.is_committing)
804 owner_email = owner_email or input_api.change.author_email
806 reviewers_plus_owner = set(reviewers)
807 if owner_email:
808 reviewers_plus_owner.add(owner_email)
809 missing_files = owners_db.files_not_covered_by(virtual_depended_on_files,
810 reviewers_plus_owner)
811 unapproved_dependencies = ["'+%s'," % path[:-len('/DEPS')]
812 for path in missing_files]
814 if unapproved_dependencies:
815 output_list = [
816 output('Missing LGTM from OWNERS of directories added to DEPS:\n %s' %
817 '\n '.join(sorted(unapproved_dependencies)))]
818 if not input_api.is_committing:
819 suggested_owners = owners_db.reviewers_for(missing_files, owner_email)
820 output_list.append(output(
821 'Suggested missing target path OWNERS:\n %s' %
822 '\n '.join(suggested_owners or [])))
823 return output_list
825 return []
828 def _CommonChecks(input_api, output_api):
829 """Checks common to both upload and commit."""
830 results = []
831 results.extend(input_api.canned_checks.PanProjectChecks(
832 input_api, output_api, excluded_paths=_EXCLUDED_PATHS))
833 results.extend(_CheckAuthorizedAuthor(input_api, output_api))
834 results.extend(
835 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
836 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
837 results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api))
838 results.extend(_CheckNoNewWStrings(input_api, output_api))
839 results.extend(_CheckNoDEPSGIT(input_api, output_api))
840 results.extend(_CheckNoBannedFunctions(input_api, output_api))
841 results.extend(_CheckNoPragmaOnce(input_api, output_api))
842 results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api))
843 results.extend(_CheckUnwantedDependencies(input_api, output_api))
844 results.extend(_CheckFilePermissions(input_api, output_api))
845 results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api))
846 results.extend(_CheckIncludeOrder(input_api, output_api))
847 results.extend(_CheckForVersionControlConflicts(input_api, output_api))
848 results.extend(_CheckPatchFiles(input_api, output_api))
849 results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api))
850 results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api))
851 results.extend(_CheckForInvalidOSMacros(input_api, output_api))
852 results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api))
853 results.extend(
854 input_api.canned_checks.CheckChangeHasNoTabs(
855 input_api,
856 output_api,
857 source_file_filter=lambda x: x.LocalPath().endswith('.grd')))
859 if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()):
860 results.extend(input_api.canned_checks.RunUnitTestsInDirectory(
861 input_api, output_api,
862 input_api.PresubmitLocalPath(),
863 whitelist=[r'^PRESUBMIT_test\.py$']))
864 return results
867 def _CheckSubversionConfig(input_api, output_api):
868 """Verifies the subversion config file is correctly setup.
870 Checks that autoprops are enabled, returns an error otherwise.
872 join = input_api.os_path.join
873 if input_api.platform == 'win32':
874 appdata = input_api.environ.get('APPDATA', '')
875 if not appdata:
876 return [output_api.PresubmitError('%APPDATA% is not configured.')]
877 path = join(appdata, 'Subversion', 'config')
878 else:
879 home = input_api.environ.get('HOME', '')
880 if not home:
881 return [output_api.PresubmitError('$HOME is not configured.')]
882 path = join(home, '.subversion', 'config')
884 error_msg = (
885 'Please look at http://dev.chromium.org/developers/coding-style to\n'
886 'configure your subversion configuration file. This enables automatic\n'
887 'properties to simplify the project maintenance.\n'
888 'Pro-tip: just download and install\n'
889 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n')
891 try:
892 lines = open(path, 'r').read().splitlines()
893 # Make sure auto-props is enabled and check for 2 Chromium standard
894 # auto-prop.
895 if (not '*.cc = svn:eol-style=LF' in lines or
896 not '*.pdf = svn:mime-type=application/pdf' in lines or
897 not 'enable-auto-props = yes' in lines):
898 return [
899 output_api.PresubmitNotifyResult(
900 'It looks like you have not configured your subversion config '
901 'file or it is not up-to-date.\n' + error_msg)
903 except (OSError, IOError):
904 return [
905 output_api.PresubmitNotifyResult(
906 'Can\'t find your subversion config file.\n' + error_msg)
908 return []
911 def _CheckAuthorizedAuthor(input_api, output_api):
912 """For non-googler/chromites committers, verify the author's email address is
913 in AUTHORS.
915 # TODO(maruel): Add it to input_api?
916 import fnmatch
918 author = input_api.change.author_email
919 if not author:
920 input_api.logging.info('No author, skipping AUTHOR check')
921 return []
922 authors_path = input_api.os_path.join(
923 input_api.PresubmitLocalPath(), 'AUTHORS')
924 valid_authors = (
925 input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line)
926 for line in open(authors_path))
927 valid_authors = [item.group(1).lower() for item in valid_authors if item]
928 if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors):
929 input_api.logging.info('Valid authors are %s', ', '.join(valid_authors))
930 return [output_api.PresubmitPromptWarning(
931 ('%s is not in AUTHORS file. If you are a new contributor, please visit'
932 '\n'
933 'http://www.chromium.org/developers/contributing-code and read the '
934 '"Legal" section\n'
935 'If you are a chromite, verify the contributor signed the CLA.') %
936 author)]
937 return []
940 def _CheckPatchFiles(input_api, output_api):
941 problems = [f.LocalPath() for f in input_api.AffectedFiles()
942 if f.LocalPath().endswith(('.orig', '.rej'))]
943 if problems:
944 return [output_api.PresubmitError(
945 "Don't commit .rej and .orig files.", problems)]
946 else:
947 return []
950 def _DidYouMeanOSMacro(bad_macro):
951 try:
952 return {'A': 'OS_ANDROID',
953 'B': 'OS_BSD',
954 'C': 'OS_CHROMEOS',
955 'F': 'OS_FREEBSD',
956 'L': 'OS_LINUX',
957 'M': 'OS_MACOSX',
958 'N': 'OS_NACL',
959 'O': 'OS_OPENBSD',
960 'P': 'OS_POSIX',
961 'S': 'OS_SOLARIS',
962 'W': 'OS_WIN'}[bad_macro[3].upper()]
963 except KeyError:
964 return ''
967 def _CheckForInvalidOSMacrosInFile(input_api, f):
968 """Check for sensible looking, totally invalid OS macros."""
969 preprocessor_statement = input_api.re.compile(r'^\s*#')
970 os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)')
971 results = []
972 for lnum, line in f.ChangedContents():
973 if preprocessor_statement.search(line):
974 for match in os_macro.finditer(line):
975 if not match.group(1) in _VALID_OS_MACROS:
976 good = _DidYouMeanOSMacro(match.group(1))
977 did_you_mean = ' (did you mean %s?)' % good if good else ''
978 results.append(' %s:%d %s%s' % (f.LocalPath(),
979 lnum,
980 match.group(1),
981 did_you_mean))
982 return results
985 def _CheckForInvalidOSMacros(input_api, output_api):
986 """Check all affected files for invalid OS macros."""
987 bad_macros = []
988 for f in input_api.AffectedFiles():
989 if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')):
990 bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f))
992 if not bad_macros:
993 return []
995 return [output_api.PresubmitError(
996 'Possibly invalid OS macro[s] found. Please fix your code\n'
997 'or add your macro to src/PRESUBMIT.py.', bad_macros)]
1000 def CheckChangeOnUpload(input_api, output_api):
1001 results = []
1002 results.extend(_CommonChecks(input_api, output_api))
1003 return results
1006 def CheckChangeOnCommit(input_api, output_api):
1007 results = []
1008 results.extend(_CommonChecks(input_api, output_api))
1009 # TODO(thestig) temporarily disabled, doesn't work in third_party/
1010 #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories(
1011 # input_api, output_api, sources))
1012 # Make sure the tree is 'open'.
1013 results.extend(input_api.canned_checks.CheckTreeIsOpen(
1014 input_api,
1015 output_api,
1016 json_url='http://chromium-status.appspot.com/current?format=json'))
1017 results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api,
1018 output_api, 'http://codereview.chromium.org',
1019 ('win_rel', 'linux_rel', 'mac_rel, win:compile'),
1020 'tryserver@chromium.org'))
1022 results.extend(input_api.canned_checks.CheckChangeHasBugField(
1023 input_api, output_api))
1024 results.extend(input_api.canned_checks.CheckChangeHasDescription(
1025 input_api, output_api))
1026 results.extend(_CheckSubversionConfig(input_api, output_api))
1027 return results
1030 def GetPreferredTrySlaves(project, change):
1031 files = change.LocalPaths()
1033 if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files):
1034 return []
1036 if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files):
1037 return ['mac_rel', 'mac:compile']
1038 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
1039 return ['win_rel', 'win:compile']
1040 if all(re.search('(^|[/_])android[/_.]', f) for f in files):
1041 return ['android_aosp', 'android_dbg', 'android_clang_dbg']
1042 if all(re.search('^native_client_sdk', f) for f in files):
1043 return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk']
1044 if all(re.search('[/_]ios[/_.]', f) for f in files):
1045 return ['ios_rel_device', 'ios_dbg_simulator']
1047 trybots = [
1048 'android_clang_dbg',
1049 'android_dbg',
1050 'ios_dbg_simulator',
1051 'ios_rel_device',
1052 'linux_asan',
1053 'linux_aura',
1054 'linux_chromeos',
1055 'linux_clang:compile',
1056 'linux_rel',
1057 'mac_rel',
1058 'mac:compile',
1059 'win_rel',
1060 'win:compile',
1061 'win_x64_rel:base_unittests',
1064 # Match things like path/aura/file.cc and path/file_aura.cc.
1065 # Same for chromeos.
1066 if any(re.search('[/_](aura|chromeos)', f) for f in files):
1067 trybots += ['linux_chromeos_clang:compile', 'linux_chromeos_asan']
1069 # If there are gyp changes to base, build, or chromeos, run a full cros build
1070 # in addition to the shorter linux_chromeos build. Changes to high level gyp
1071 # files have a much higher chance of breaking the cros build, which is
1072 # differnt from the linux_chromeos build that most chrome developers test
1073 # with.
1074 if any(re.search('^(base|build|chromeos).*\.gypi?$', f) for f in files):
1075 trybots += ['cros_x86']
1077 # The AOSP bot doesn't build the chrome/ layer, so ignore any changes to it
1078 # unless they're .gyp(i) files as changes to those files can break the gyp
1079 # step on that bot.
1080 if (not all(re.search('^chrome', f) for f in files) or
1081 any(re.search('\.gypi?$', f) for f in files)):
1082 trybots += ['android_aosp']
1084 return trybots