[email protected] | a18130a | 2012-01-03 17:52:08 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Top-level presubmit script for Chromium. |
| 6 | |
[email protected] | f129379 | 2009-07-31 18:09:56 | [diff] [blame] | 7 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
[email protected] | 50d7d721e | 2009-11-15 17:56:18 | [diff] [blame] | 8 | for more details about the presubmit API built into gcl. |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 9 | """ |
| 10 | |
[email protected] | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 11 | |
[email protected] | 9d16ad1 | 2011-12-14 20:49:47 | [diff] [blame] | 12 | import re |
[email protected] | fbcafe5a | 2012-08-08 15:31:22 | [diff] [blame] | 13 | import subprocess |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 14 | import sys |
[email protected] | 9d16ad1 | 2011-12-14 20:49:47 | [diff] [blame] | 15 | |
| 16 | |
[email protected] | 379e7dd | 2010-01-28 17:39:21 | [diff] [blame] | 17 | _EXCLUDED_PATHS = ( |
[email protected] | 3e4eb11 | 2011-01-18 03:29:54 | [diff] [blame] | 18 | r"^breakpad[\\\/].*", |
[email protected] | 40d1dbb | 2012-10-26 07:18:00 | [diff] [blame] | 19 | r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_rules.py", |
| 20 | r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_simple.py", |
[email protected] | 8886ffcb | 2013-02-12 04:56:28 | [diff] [blame] | 21 | r"^native_client_sdk[\\\/]src[\\\/]tools[\\\/].*.mk", |
[email protected] | a18130a | 2012-01-03 17:52:08 | [diff] [blame] | 22 | r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*", |
[email protected] | 3e4eb11 | 2011-01-18 03:29:54 | [diff] [blame] | 23 | r"^skia[\\\/].*", |
| 24 | r"^v8[\\\/].*", |
| 25 | r".*MakeFile$", |
[email protected] | 1084ccc | 2012-03-14 03:22:53 | [diff] [blame] | 26 | r".+_autogen\.h$", |
[email protected] | ce145c0 | 2012-09-06 09:49:34 | [diff] [blame] | 27 | r".+[\\\/]pnacl_shim\.c$", |
[email protected] | 430641764 | 2009-06-11 00:33:40 | [diff] [blame] | 28 | ) |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 29 | |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 30 | # Fragment of a regular expression that matches C++ and Objective-C++ |
| 31 | # implementation files. |
| 32 | _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$' |
| 33 | |
| 34 | # Regular expression that matches code only used for test binaries |
| 35 | # (best effort). |
| 36 | _TEST_CODE_EXCLUDED_PATHS = ( |
| 37 | r'.*[/\\](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS, |
| 38 | r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS, |
[email protected] | 11e0608 | 2013-04-26 19:09:03 | [diff] [blame] | 39 | r'.+_(api|browser|perf|pixel|unit|ui)?test(_[a-z]+)?%s' % |
[email protected] | e2d7e6f | 2013-04-23 12:57:12 | [diff] [blame] | 40 | _IMPLEMENTATION_EXTENSIONS, |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 41 | r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS, |
| 42 | r'.*[/\\](test|tool(s)?)[/\\].*', |
[email protected] | ef070cc | 2013-05-03 11:53:05 | [diff] [blame] | 43 | # content_shell is used for running layout tests. |
| 44 | r'content[/\\]shell[/\\].*', |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 45 | # At request of folks maintaining this folder. |
| 46 | r'chrome[/\\]browser[/\\]automation[/\\].*', |
| 47 | ) |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 48 | |
[email protected] | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 49 | _TEST_ONLY_WARNING = ( |
| 50 | 'You might be calling functions intended only for testing from\n' |
| 51 | 'production code. It is OK to ignore this warning if you know what\n' |
| 52 | 'you are doing, as the heuristics used to detect the situation are\n' |
| 53 | 'not perfect. The commit queue will not block on this warning.\n' |
| 54 | 'Email [email protected] if you have questions.') |
| 55 | |
| 56 | |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 57 | _INCLUDE_ORDER_WARNING = ( |
| 58 | 'Your #include order seems to be broken. Send mail to\n' |
| 59 | '[email protected] if this is not the case.') |
| 60 | |
| 61 | |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 62 | _BANNED_OBJC_FUNCTIONS = ( |
| 63 | ( |
| 64 | 'addTrackingRect:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 65 | ( |
| 66 | 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is' |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 67 | 'prohibited. Please use CrTrackingArea instead.', |
| 68 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 69 | ), |
| 70 | False, |
| 71 | ), |
| 72 | ( |
| 73 | 'NSTrackingArea', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 74 | ( |
| 75 | 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 76 | 'instead.', |
| 77 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 78 | ), |
| 79 | False, |
| 80 | ), |
| 81 | ( |
| 82 | 'convertPointFromBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 83 | ( |
| 84 | 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 85 | 'Please use |convertPoint:(point) fromView:nil| instead.', |
| 86 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 87 | ), |
| 88 | True, |
| 89 | ), |
| 90 | ( |
| 91 | 'convertPointToBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 92 | ( |
| 93 | 'The use of -[NSView convertPointToBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 94 | 'Please use |convertPoint:(point) toView:nil| instead.', |
| 95 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 96 | ), |
| 97 | True, |
| 98 | ), |
| 99 | ( |
| 100 | 'convertRectFromBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 101 | ( |
| 102 | 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 103 | 'Please use |convertRect:(point) fromView:nil| instead.', |
| 104 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 105 | ), |
| 106 | True, |
| 107 | ), |
| 108 | ( |
| 109 | 'convertRectToBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 110 | ( |
| 111 | 'The use of -[NSView convertRectToBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 112 | 'Please use |convertRect:(point) toView:nil| instead.', |
| 113 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 114 | ), |
| 115 | True, |
| 116 | ), |
| 117 | ( |
| 118 | 'convertSizeFromBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 119 | ( |
| 120 | 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 121 | 'Please use |convertSize:(point) fromView:nil| instead.', |
| 122 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 123 | ), |
| 124 | True, |
| 125 | ), |
| 126 | ( |
| 127 | 'convertSizeToBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 128 | ( |
| 129 | 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 130 | 'Please use |convertSize:(point) toView:nil| instead.', |
| 131 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 132 | ), |
| 133 | True, |
| 134 | ), |
| 135 | ) |
| 136 | |
| 137 | |
| 138 | _BANNED_CPP_FUNCTIONS = ( |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 139 | # Make sure that gtest's FRIEND_TEST() macro is not used; the |
| 140 | # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be |
[email protected] | e00ccc9 | 2012-11-01 17:32:30 | [diff] [blame] | 141 | # used instead since that allows for FLAKY_ and DISABLED_ prefixes. |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 142 | ( |
| 143 | 'FRIEND_TEST(', |
| 144 | ( |
[email protected] | e3c94550 | 2012-06-26 20:01:49 | [diff] [blame] | 145 | 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 146 | 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.', |
| 147 | ), |
| 148 | False, |
[email protected] | 7345da0 | 2012-11-27 14:31:49 | [diff] [blame] | 149 | (), |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 150 | ), |
| 151 | ( |
| 152 | 'ScopedAllowIO', |
| 153 | ( |
[email protected] | e3c94550 | 2012-06-26 20:01:49 | [diff] [blame] | 154 | 'New code should not use ScopedAllowIO. Post a task to the blocking', |
| 155 | 'pool or the FILE thread instead.', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 156 | ), |
[email protected] | e3c94550 | 2012-06-26 20:01:49 | [diff] [blame] | 157 | True, |
[email protected] | 7345da0 | 2012-11-27 14:31:49 | [diff] [blame] | 158 | ( |
| 159 | r"^content[\\\/]shell[\\\/]shell_browser_main\.cc$", |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 160 | r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$", |
[email protected] | 7345da0 | 2012-11-27 14:31:49 | [diff] [blame] | 161 | ), |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 162 | ), |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 163 | ) |
| 164 | |
| 165 | |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 166 | _VALID_OS_MACROS = ( |
| 167 | # Please keep sorted. |
| 168 | 'OS_ANDROID', |
| 169 | 'OS_BSD', |
| 170 | 'OS_CAT', # For testing. |
| 171 | 'OS_CHROMEOS', |
| 172 | 'OS_FREEBSD', |
| 173 | 'OS_IOS', |
| 174 | 'OS_LINUX', |
| 175 | 'OS_MACOSX', |
| 176 | 'OS_NACL', |
| 177 | 'OS_OPENBSD', |
| 178 | 'OS_POSIX', |
| 179 | 'OS_SOLARIS', |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 180 | 'OS_WIN', |
| 181 | ) |
| 182 | |
| 183 | |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 184 | def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api): |
| 185 | """Attempts to prevent use of functions intended only for testing in |
| 186 | non-testing code. For now this is just a best-effort implementation |
| 187 | that ignores header files and may have some false positives. A |
| 188 | better implementation would probably need a proper C++ parser. |
| 189 | """ |
| 190 | # We only scan .cc files and the like, as the declaration of |
| 191 | # for-testing functions in header files are hard to distinguish from |
| 192 | # calls to such functions without a proper C++ parser. |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 193 | file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 194 | |
| 195 | base_function_pattern = r'ForTest(ing)?|for_test(ing)?' |
| 196 | inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern) |
| 197 | exclusion_pattern = input_api.re.compile( |
| 198 | r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % ( |
| 199 | base_function_pattern, base_function_pattern)) |
| 200 | |
| 201 | def FilterFile(affected_file): |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 202 | black_list = (_EXCLUDED_PATHS + |
| 203 | _TEST_CODE_EXCLUDED_PATHS + |
| 204 | input_api.DEFAULT_BLACK_LIST) |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 205 | return input_api.FilterSourceFile( |
| 206 | affected_file, |
| 207 | white_list=(file_inclusion_pattern, ), |
| 208 | black_list=black_list) |
| 209 | |
| 210 | problems = [] |
| 211 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 212 | local_path = f.LocalPath() |
[email protected] | 2fdd1f36 | 2013-01-16 03:56:03 | [diff] [blame] | 213 | lines = input_api.ReadFile(f).splitlines() |
| 214 | line_number = 0 |
| 215 | for line in lines: |
| 216 | if (inclusion_pattern.search(line) and |
| 217 | not exclusion_pattern.search(line)): |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 218 | problems.append( |
[email protected] | 2fdd1f36 | 2013-01-16 03:56:03 | [diff] [blame] | 219 | '%s:%d\n %s' % (local_path, line_number, line.strip())) |
| 220 | line_number += 1 |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 221 | |
| 222 | if problems: |
[email protected] | f7051d5 | 2013-04-02 18:31:42 | [diff] [blame] | 223 | return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)] |
[email protected] | 2fdd1f36 | 2013-01-16 03:56:03 | [diff] [blame] | 224 | else: |
| 225 | return [] |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 226 | |
| 227 | |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 228 | def _CheckNoIOStreamInHeaders(input_api, output_api): |
| 229 | """Checks to make sure no .h files include <iostream>.""" |
| 230 | files = [] |
| 231 | pattern = input_api.re.compile(r'^#include\s*<iostream>', |
| 232 | input_api.re.MULTILINE) |
| 233 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 234 | if not f.LocalPath().endswith('.h'): |
| 235 | continue |
| 236 | contents = input_api.ReadFile(f) |
| 237 | if pattern.search(contents): |
| 238 | files.append(f) |
| 239 | |
| 240 | if len(files): |
| 241 | return [ output_api.PresubmitError( |
[email protected] | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 242 | 'Do not #include <iostream> in header files, since it inserts static ' |
| 243 | 'initialization into every file including the header. Instead, ' |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 244 | '#include <ostream>. See http://crbug.com/94794', |
| 245 | files) ] |
| 246 | return [] |
| 247 | |
| 248 | |
[email protected] | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 249 | def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api): |
| 250 | """Checks to make sure no source files use UNIT_TEST""" |
| 251 | problems = [] |
| 252 | for f in input_api.AffectedFiles(): |
| 253 | if (not f.LocalPath().endswith(('.cc', '.mm'))): |
| 254 | continue |
| 255 | |
| 256 | for line_num, line in f.ChangedContents(): |
| 257 | if 'UNIT_TEST' in line: |
| 258 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 259 | |
| 260 | if not problems: |
| 261 | return [] |
| 262 | return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' + |
| 263 | '\n'.join(problems))] |
| 264 | |
| 265 | |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 266 | def _CheckNoNewWStrings(input_api, output_api): |
| 267 | """Checks to make sure we don't introduce use of wstrings.""" |
[email protected] | 55463aa6 | 2011-10-12 00:48:27 | [diff] [blame] | 268 | problems = [] |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 269 | for f in input_api.AffectedFiles(): |
[email protected] | b5c2429 | 2011-11-28 14:38:20 | [diff] [blame] | 270 | if (not f.LocalPath().endswith(('.cc', '.h')) or |
| 271 | f.LocalPath().endswith('test.cc')): |
| 272 | continue |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 273 | |
[email protected] | a11dbe9b | 2012-08-07 01:32:58 | [diff] [blame] | 274 | allowWString = False |
[email protected] | b5c2429 | 2011-11-28 14:38:20 | [diff] [blame] | 275 | for line_num, line in f.ChangedContents(): |
[email protected] | a11dbe9b | 2012-08-07 01:32:58 | [diff] [blame] | 276 | if 'presubmit: allow wstring' in line: |
| 277 | allowWString = True |
| 278 | elif not allowWString and 'wstring' in line: |
[email protected] | 55463aa6 | 2011-10-12 00:48:27 | [diff] [blame] | 279 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
[email protected] | a11dbe9b | 2012-08-07 01:32:58 | [diff] [blame] | 280 | allowWString = False |
| 281 | else: |
| 282 | allowWString = False |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 283 | |
[email protected] | 55463aa6 | 2011-10-12 00:48:27 | [diff] [blame] | 284 | if not problems: |
| 285 | return [] |
| 286 | return [output_api.PresubmitPromptWarning('New code should not use wstrings.' |
[email protected] | a11dbe9b | 2012-08-07 01:32:58 | [diff] [blame] | 287 | ' If you are calling a cross-platform API that accepts a wstring, ' |
| 288 | 'fix the API.\n' + |
[email protected] | 55463aa6 | 2011-10-12 00:48:27 | [diff] [blame] | 289 | '\n'.join(problems))] |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 290 | |
| 291 | |
[email protected] | 2a8ac9c | 2011-10-19 17:20:44 | [diff] [blame] | 292 | def _CheckNoDEPSGIT(input_api, output_api): |
| 293 | """Make sure .DEPS.git is never modified manually.""" |
| 294 | if any(f.LocalPath().endswith('.DEPS.git') for f in |
| 295 | input_api.AffectedFiles()): |
| 296 | return [output_api.PresubmitError( |
| 297 | 'Never commit changes to .DEPS.git. This file is maintained by an\n' |
| 298 | 'automated system based on what\'s in DEPS and your changes will be\n' |
| 299 | 'overwritten.\n' |
| 300 | 'See http://code.google.com/p/chromium/wiki/UsingNewGit#Rolling_DEPS\n' |
| 301 | 'for more information')] |
| 302 | return [] |
| 303 | |
| 304 | |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 305 | def _CheckNoBannedFunctions(input_api, output_api): |
| 306 | """Make sure that banned functions are not used.""" |
| 307 | warnings = [] |
| 308 | errors = [] |
| 309 | |
| 310 | file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h')) |
| 311 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 312 | for line_num, line in f.ChangedContents(): |
| 313 | for func_name, message, error in _BANNED_OBJC_FUNCTIONS: |
| 314 | if func_name in line: |
| 315 | problems = warnings; |
| 316 | if error: |
| 317 | problems = errors; |
| 318 | problems.append(' %s:%d:' % (f.LocalPath(), line_num)) |
| 319 | for message_line in message: |
| 320 | problems.append(' %s' % message_line) |
| 321 | |
| 322 | file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h')) |
| 323 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 324 | for line_num, line in f.ChangedContents(): |
[email protected] | 7345da0 | 2012-11-27 14:31:49 | [diff] [blame] | 325 | for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS: |
| 326 | def IsBlacklisted(affected_file, blacklist): |
| 327 | local_path = affected_file.LocalPath() |
| 328 | for item in blacklist: |
| 329 | if input_api.re.match(item, local_path): |
| 330 | return True |
| 331 | return False |
| 332 | if IsBlacklisted(f, excluded_paths): |
| 333 | continue |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 334 | if func_name in line: |
| 335 | problems = warnings; |
| 336 | if error: |
| 337 | problems = errors; |
| 338 | problems.append(' %s:%d:' % (f.LocalPath(), line_num)) |
| 339 | for message_line in message: |
| 340 | problems.append(' %s' % message_line) |
| 341 | |
| 342 | result = [] |
| 343 | if (warnings): |
| 344 | result.append(output_api.PresubmitPromptWarning( |
| 345 | 'Banned functions were used.\n' + '\n'.join(warnings))) |
| 346 | if (errors): |
| 347 | result.append(output_api.PresubmitError( |
| 348 | 'Banned functions were used.\n' + '\n'.join(errors))) |
| 349 | return result |
| 350 | |
| 351 | |
[email protected] | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 352 | def _CheckNoPragmaOnce(input_api, output_api): |
| 353 | """Make sure that banned functions are not used.""" |
| 354 | files = [] |
| 355 | pattern = input_api.re.compile(r'^#pragma\s+once', |
| 356 | input_api.re.MULTILINE) |
| 357 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 358 | if not f.LocalPath().endswith('.h'): |
| 359 | continue |
| 360 | contents = input_api.ReadFile(f) |
| 361 | if pattern.search(contents): |
| 362 | files.append(f) |
| 363 | |
| 364 | if files: |
| 365 | return [output_api.PresubmitError( |
| 366 | 'Do not use #pragma once in header files.\n' |
| 367 | 'See http://www.chromium.org/developers/coding-style#TOC-File-headers', |
| 368 | files)] |
| 369 | return [] |
| 370 | |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 371 | |
[email protected] | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 372 | def _CheckNoTrinaryTrueFalse(input_api, output_api): |
| 373 | """Checks to make sure we don't introduce use of foo ? true : false.""" |
| 374 | problems = [] |
| 375 | pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)') |
| 376 | for f in input_api.AffectedFiles(): |
| 377 | if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')): |
| 378 | continue |
| 379 | |
| 380 | for line_num, line in f.ChangedContents(): |
| 381 | if pattern.match(line): |
| 382 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 383 | |
| 384 | if not problems: |
| 385 | return [] |
| 386 | return [output_api.PresubmitPromptWarning( |
| 387 | 'Please consider avoiding the "? true : false" pattern if possible.\n' + |
| 388 | '\n'.join(problems))] |
| 389 | |
| 390 | |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 391 | def _CheckUnwantedDependencies(input_api, output_api): |
| 392 | """Runs checkdeps on #include statements added in this |
| 393 | change. Breaking - rules is an error, breaking ! rules is a |
| 394 | warning. |
| 395 | """ |
| 396 | # We need to wait until we have an input_api object and use this |
| 397 | # roundabout construct to import checkdeps because this file is |
| 398 | # eval-ed and thus doesn't have __file__. |
| 399 | original_sys_path = sys.path |
| 400 | try: |
| 401 | sys.path = sys.path + [input_api.os_path.join( |
| 402 | input_api.PresubmitLocalPath(), 'tools', 'checkdeps')] |
| 403 | import checkdeps |
| 404 | from cpp_checker import CppChecker |
| 405 | from rules import Rule |
| 406 | finally: |
| 407 | # Restore sys.path to what it was before. |
| 408 | sys.path = original_sys_path |
| 409 | |
| 410 | added_includes = [] |
| 411 | for f in input_api.AffectedFiles(): |
| 412 | if not CppChecker.IsCppFile(f.LocalPath()): |
| 413 | continue |
| 414 | |
| 415 | changed_lines = [line for line_num, line in f.ChangedContents()] |
| 416 | added_includes.append([f.LocalPath(), changed_lines]) |
| 417 | |
[email protected] | 2638517 | 2013-05-09 23:11:35 | [diff] [blame] | 418 | deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath()) |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 419 | |
| 420 | error_descriptions = [] |
| 421 | warning_descriptions = [] |
| 422 | for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes( |
| 423 | added_includes): |
| 424 | description_with_path = '%s\n %s' % (path, rule_description) |
| 425 | if rule_type == Rule.DISALLOW: |
| 426 | error_descriptions.append(description_with_path) |
| 427 | else: |
| 428 | warning_descriptions.append(description_with_path) |
| 429 | |
| 430 | results = [] |
| 431 | if error_descriptions: |
| 432 | results.append(output_api.PresubmitError( |
| 433 | 'You added one or more #includes that violate checkdeps rules.', |
| 434 | error_descriptions)) |
| 435 | if warning_descriptions: |
[email protected] | f7051d5 | 2013-04-02 18:31:42 | [diff] [blame] | 436 | results.append(output_api.PresubmitPromptOrNotify( |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 437 | 'You added one or more #includes of files that are temporarily\n' |
| 438 | 'allowed but being removed. Can you avoid introducing the\n' |
| 439 | '#include? See relevant DEPS file(s) for details and contacts.', |
| 440 | warning_descriptions)) |
| 441 | return results |
| 442 | |
| 443 | |
[email protected] | fbcafe5a | 2012-08-08 15:31:22 | [diff] [blame] | 444 | def _CheckFilePermissions(input_api, output_api): |
| 445 | """Check that all files have their permissions properly set.""" |
| 446 | args = [sys.executable, 'tools/checkperms/checkperms.py', '--root', |
| 447 | input_api.change.RepositoryRoot()] |
| 448 | for f in input_api.AffectedFiles(): |
| 449 | args += ['--file', f.LocalPath()] |
| 450 | errors = [] |
| 451 | (errors, stderrdata) = subprocess.Popen(args).communicate() |
| 452 | |
| 453 | results = [] |
| 454 | if errors: |
[email protected] | c8278b3 | 2012-10-30 20:35:49 | [diff] [blame] | 455 | results.append(output_api.PresubmitError('checkperms.py failed.', |
[email protected] | fbcafe5a | 2012-08-08 15:31:22 | [diff] [blame] | 456 | errors)) |
| 457 | return results |
| 458 | |
| 459 | |
[email protected] | c8278b3 | 2012-10-30 20:35:49 | [diff] [blame] | 460 | def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api): |
| 461 | """Makes sure we don't include ui/aura/window_property.h |
| 462 | in header files. |
| 463 | """ |
| 464 | pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"') |
| 465 | errors = [] |
| 466 | for f in input_api.AffectedFiles(): |
| 467 | if not f.LocalPath().endswith('.h'): |
| 468 | continue |
| 469 | for line_num, line in f.ChangedContents(): |
| 470 | if pattern.match(line): |
| 471 | errors.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 472 | |
| 473 | results = [] |
| 474 | if errors: |
| 475 | results.append(output_api.PresubmitError( |
| 476 | 'Header files should not include ui/aura/window_property.h', errors)) |
| 477 | return results |
| 478 | |
| 479 | |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 480 | def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums): |
| 481 | """Checks that the lines in scope occur in the right order. |
| 482 | |
| 483 | 1. C system files in alphabetical order |
| 484 | 2. C++ system files in alphabetical order |
| 485 | 3. Project's .h files |
| 486 | """ |
| 487 | |
| 488 | c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>') |
| 489 | cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>') |
| 490 | custom_include_pattern = input_api.re.compile(r'\s*#include ".*') |
| 491 | |
| 492 | C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3) |
| 493 | |
| 494 | state = C_SYSTEM_INCLUDES |
| 495 | |
| 496 | previous_line = '' |
[email protected] | 728b9bb | 2012-11-14 20:38:57 | [diff] [blame] | 497 | previous_line_num = 0 |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 498 | problem_linenums = [] |
| 499 | for line_num, line in scope: |
| 500 | if c_system_include_pattern.match(line): |
| 501 | if state != C_SYSTEM_INCLUDES: |
[email protected] | 728b9bb | 2012-11-14 20:38:57 | [diff] [blame] | 502 | problem_linenums.append((line_num, previous_line_num)) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 503 | elif previous_line and previous_line > line: |
[email protected] | 728b9bb | 2012-11-14 20:38:57 | [diff] [blame] | 504 | problem_linenums.append((line_num, previous_line_num)) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 505 | elif cpp_system_include_pattern.match(line): |
| 506 | if state == C_SYSTEM_INCLUDES: |
| 507 | state = CPP_SYSTEM_INCLUDES |
| 508 | elif state == CUSTOM_INCLUDES: |
[email protected] | 728b9bb | 2012-11-14 20:38:57 | [diff] [blame] | 509 | problem_linenums.append((line_num, previous_line_num)) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 510 | elif previous_line and previous_line > line: |
[email protected] | 728b9bb | 2012-11-14 20:38:57 | [diff] [blame] | 511 | problem_linenums.append((line_num, previous_line_num)) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 512 | elif custom_include_pattern.match(line): |
| 513 | if state != CUSTOM_INCLUDES: |
| 514 | state = CUSTOM_INCLUDES |
| 515 | elif previous_line and previous_line > line: |
[email protected] | 728b9bb | 2012-11-14 20:38:57 | [diff] [blame] | 516 | problem_linenums.append((line_num, previous_line_num)) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 517 | else: |
| 518 | problem_linenums.append(line_num) |
| 519 | previous_line = line |
[email protected] | 728b9bb | 2012-11-14 20:38:57 | [diff] [blame] | 520 | previous_line_num = line_num |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 521 | |
| 522 | warnings = [] |
[email protected] | 728b9bb | 2012-11-14 20:38:57 | [diff] [blame] | 523 | for (line_num, previous_line_num) in problem_linenums: |
| 524 | if line_num in changed_linenums or previous_line_num in changed_linenums: |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 525 | warnings.append(' %s:%d' % (file_path, line_num)) |
| 526 | return warnings |
| 527 | |
| 528 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 529 | def _CheckIncludeOrderInFile(input_api, f, changed_linenums): |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 530 | """Checks the #include order for the given file f.""" |
| 531 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 532 | system_include_pattern = input_api.re.compile(r'\s*#include \<.*') |
[email protected] | 962f117e | 2012-11-22 18:11:56 | [diff] [blame] | 533 | # Exclude #include <.../...> includes from the check; e.g., <sys/...> includes |
| 534 | # often need to appear in a specific order. |
| 535 | excluded_include_pattern = input_api.re.compile(r'\s*#include \<.*/.*') |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 536 | custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"') |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 537 | if_pattern = input_api.re.compile( |
| 538 | r'\s*#\s*(if|elif|else|endif|define|undef).*') |
| 539 | # Some files need specialized order of includes; exclude such files from this |
| 540 | # check. |
| 541 | uncheckable_includes_pattern = input_api.re.compile( |
| 542 | r'\s*#include ' |
| 543 | '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*') |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 544 | |
| 545 | contents = f.NewContents() |
| 546 | warnings = [] |
| 547 | line_num = 0 |
| 548 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 549 | # Handle the special first include. If the first include file is |
| 550 | # some/path/file.h, the corresponding including file can be some/path/file.cc, |
| 551 | # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h |
| 552 | # etc. It's also possible that no special first include exists. |
| 553 | for line in contents: |
| 554 | line_num += 1 |
| 555 | if system_include_pattern.match(line): |
| 556 | # No special first include -> process the line again along with normal |
| 557 | # includes. |
| 558 | line_num -= 1 |
| 559 | break |
| 560 | match = custom_include_pattern.match(line) |
| 561 | if match: |
| 562 | match_dict = match.groupdict() |
| 563 | header_basename = input_api.os_path.basename( |
| 564 | match_dict['FILE']).replace('.h', '') |
| 565 | if header_basename not in input_api.os_path.basename(f.LocalPath()): |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 566 | # No special first include -> process the line again along with normal |
| 567 | # includes. |
| 568 | line_num -= 1 |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 569 | break |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 570 | |
| 571 | # Split into scopes: Each region between #if and #endif is its own scope. |
| 572 | scopes = [] |
| 573 | current_scope = [] |
| 574 | for line in contents[line_num:]: |
| 575 | line_num += 1 |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 576 | if uncheckable_includes_pattern.match(line): |
| 577 | return [] |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 578 | if if_pattern.match(line): |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 579 | scopes.append(current_scope) |
| 580 | current_scope = [] |
[email protected] | 962f117e | 2012-11-22 18:11:56 | [diff] [blame] | 581 | elif ((system_include_pattern.match(line) or |
| 582 | custom_include_pattern.match(line)) and |
| 583 | not excluded_include_pattern.match(line)): |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 584 | current_scope.append((line_num, line)) |
| 585 | scopes.append(current_scope) |
| 586 | |
| 587 | for scope in scopes: |
| 588 | warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(), |
| 589 | changed_linenums)) |
| 590 | return warnings |
| 591 | |
| 592 | |
| 593 | def _CheckIncludeOrder(input_api, output_api): |
| 594 | """Checks that the #include order is correct. |
| 595 | |
| 596 | 1. The corresponding header for source files. |
| 597 | 2. C system files in alphabetical order |
| 598 | 3. C++ system files in alphabetical order |
| 599 | 4. Project's .h files in alphabetical order |
| 600 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 601 | Each region separated by #if, #elif, #else, #endif, #define and #undef follows |
| 602 | these rules separately. |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 603 | """ |
| 604 | |
| 605 | warnings = [] |
| 606 | for f in input_api.AffectedFiles(): |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 607 | if f.LocalPath().endswith(('.cc', '.h')): |
| 608 | changed_linenums = set(line_num for line_num, _ in f.ChangedContents()) |
| 609 | warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums)) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 610 | |
| 611 | results = [] |
| 612 | if warnings: |
[email protected] | f7051d5 | 2013-04-02 18:31:42 | [diff] [blame] | 613 | results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING, |
[email protected] | 120cf540d | 2012-12-10 17:55:53 | [diff] [blame] | 614 | warnings)) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 615 | return results |
| 616 | |
| 617 | |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 618 | def _CheckForVersionControlConflictsInFile(input_api, f): |
| 619 | pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$') |
| 620 | errors = [] |
| 621 | for line_num, line in f.ChangedContents(): |
| 622 | if pattern.match(line): |
| 623 | errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line)) |
| 624 | return errors |
| 625 | |
| 626 | |
| 627 | def _CheckForVersionControlConflicts(input_api, output_api): |
| 628 | """Usually this is not intentional and will cause a compile failure.""" |
| 629 | errors = [] |
| 630 | for f in input_api.AffectedFiles(): |
| 631 | errors.extend(_CheckForVersionControlConflictsInFile(input_api, f)) |
| 632 | |
| 633 | results = [] |
| 634 | if errors: |
| 635 | results.append(output_api.PresubmitError( |
| 636 | 'Version control conflict markers found, please resolve.', errors)) |
| 637 | return results |
| 638 | |
| 639 | |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 640 | def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api): |
| 641 | def FilterFile(affected_file): |
| 642 | """Filter function for use with input_api.AffectedSourceFiles, |
| 643 | below. This filters out everything except non-test files from |
| 644 | top-level directories that generally speaking should not hard-code |
| 645 | service URLs (e.g. src/android_webview/, src/content/ and others). |
| 646 | """ |
| 647 | return input_api.FilterSourceFile( |
| 648 | affected_file, |
[email protected] | 78bb39d6 | 2012-12-11 15:11:56 | [diff] [blame] | 649 | white_list=(r'^(android_webview|base|content|net)[\\\/].*', ), |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 650 | black_list=(_EXCLUDED_PATHS + |
| 651 | _TEST_CODE_EXCLUDED_PATHS + |
| 652 | input_api.DEFAULT_BLACK_LIST)) |
| 653 | |
| 654 | pattern = input_api.re.compile('"[^"]*google\.com[^"]*"') |
| 655 | problems = [] # items are (filename, line_number, line) |
| 656 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 657 | for line_num, line in f.ChangedContents(): |
| 658 | if pattern.search(line): |
| 659 | problems.append((f.LocalPath(), line_num, line)) |
| 660 | |
| 661 | if problems: |
[email protected] | f7051d5 | 2013-04-02 18:31:42 | [diff] [blame] | 662 | return [output_api.PresubmitPromptOrNotify( |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 663 | 'Most layers below src/chrome/ should not hardcode service URLs.\n' |
| 664 | 'Are you sure this is correct? (Contact: [email protected])', |
| 665 | [' %s:%d: %s' % ( |
| 666 | problem[0], problem[1], problem[2]) for problem in problems])] |
[email protected] | 2fdd1f36 | 2013-01-16 03:56:03 | [diff] [blame] | 667 | else: |
| 668 | return [] |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 669 | |
| 670 | |
[email protected] | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 671 | def _CheckNoAbbreviationInPngFileName(input_api, output_api): |
| 672 | """Makes sure there are no abbreviations in the name of PNG files. |
| 673 | """ |
[email protected] | 4053a48e | 2013-01-25 21:43:04 | [diff] [blame] | 674 | pattern = input_api.re.compile(r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$') |
[email protected] | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 675 | errors = [] |
| 676 | for f in input_api.AffectedFiles(include_deletes=False): |
| 677 | if pattern.match(f.LocalPath()): |
| 678 | errors.append(' %s' % f.LocalPath()) |
| 679 | |
| 680 | results = [] |
| 681 | if errors: |
| 682 | results.append(output_api.PresubmitError( |
| 683 | 'The name of PNG files should not have abbreviations. \n' |
| 684 | 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n' |
| 685 | 'Contact [email protected] if you have questions.', errors)) |
| 686 | return results |
| 687 | |
| 688 | |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame^] | 689 | def _CheckAddedDepsHaveTargetApprovals(input_api, output_api): |
| 690 | """When a dependency prefixed with + is added to a DEPS file, we |
| 691 | want to make sure that the change is reviewed by an OWNER of the |
| 692 | target file or directory, to avoid layering violations from being |
| 693 | introduced. This check verifies that this happens. |
| 694 | """ |
| 695 | changed_lines = set() |
| 696 | for f in input_api.AffectedFiles(): |
| 697 | filename = input_api.os_path.basename(f.LocalPath()) |
| 698 | if filename == 'DEPS': |
| 699 | changed_lines |= set(line.strip() |
| 700 | for line_num, line |
| 701 | in f.ChangedContents()) |
| 702 | if not changed_lines: |
| 703 | return [] |
| 704 | |
| 705 | virtual_depended_on_files = set() |
| 706 | # This pattern grabs the path without basename in the first |
| 707 | # parentheses, and the basename (if present) in the second. It |
| 708 | # relies on the simple heuristic that if there is a basename it will |
| 709 | # be a header file ending in ".h". |
| 710 | pattern = input_api.re.compile( |
| 711 | r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""") |
| 712 | for changed_line in changed_lines: |
| 713 | m = pattern.match(changed_line) |
| 714 | if m: |
| 715 | virtual_depended_on_files.add('%s/DEPS' % m.group(1)) |
| 716 | |
| 717 | if not virtual_depended_on_files: |
| 718 | return [] |
| 719 | |
| 720 | if input_api.is_committing: |
| 721 | if input_api.tbr: |
| 722 | return [output_api.PresubmitNotifyResult( |
| 723 | '--tbr was specified, skipping OWNERS check for DEPS additions')] |
| 724 | if not input_api.change.issue: |
| 725 | return [output_api.PresubmitError( |
| 726 | "DEPS approval by OWNERS check failed: this change has " |
| 727 | "no Rietveld issue number, so we can't check it for approvals.")] |
| 728 | output = output_api.PresubmitError |
| 729 | else: |
| 730 | output = output_api.PresubmitNotifyResult |
| 731 | |
| 732 | owners_db = input_api.owners_db |
| 733 | owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers( |
| 734 | input_api, |
| 735 | owners_db.email_regexp, |
| 736 | approval_needed=input_api.is_committing) |
| 737 | |
| 738 | owner_email = owner_email or input_api.change.author_email |
| 739 | |
| 740 | reviewers_plus_owner = set([owner_email]).union(reviewers) |
| 741 | missing_files = owners_db.files_not_covered_by(virtual_depended_on_files, |
| 742 | reviewers_plus_owner) |
| 743 | unapproved_dependencies = ["'+%s'," % path[:-len('/DEPS')] |
| 744 | for path in missing_files] |
| 745 | |
| 746 | if unapproved_dependencies: |
| 747 | output_list = [ |
| 748 | output('Missing LGTM from OWNERS of directories added to DEPS:\n %s' % |
| 749 | '\n '.join(sorted(unapproved_dependencies)))] |
| 750 | if not input_api.is_committing: |
| 751 | suggested_owners = owners_db.reviewers_for(missing_files, owner_email) |
| 752 | output_list.append(output( |
| 753 | 'Suggested missing target path OWNERS:\n %s' % |
| 754 | '\n '.join(suggested_owners or []))) |
| 755 | return output_list |
| 756 | |
| 757 | return [] |
| 758 | |
| 759 | |
[email protected] | 22c9bd7 | 2011-03-27 16:47:39 | [diff] [blame] | 760 | def _CommonChecks(input_api, output_api): |
| 761 | """Checks common to both upload and commit.""" |
| 762 | results = [] |
| 763 | results.extend(input_api.canned_checks.PanProjectChecks( |
| 764 | input_api, output_api, excluded_paths=_EXCLUDED_PATHS)) |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 765 | results.extend(_CheckAuthorizedAuthor(input_api, output_api)) |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 766 | results.extend( |
| 767 | _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api)) |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 768 | results.extend(_CheckNoIOStreamInHeaders(input_api, output_api)) |
[email protected] | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 769 | results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api)) |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 770 | results.extend(_CheckNoNewWStrings(input_api, output_api)) |
[email protected] | 2a8ac9c | 2011-10-19 17:20:44 | [diff] [blame] | 771 | results.extend(_CheckNoDEPSGIT(input_api, output_api)) |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 772 | results.extend(_CheckNoBannedFunctions(input_api, output_api)) |
[email protected] | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 773 | results.extend(_CheckNoPragmaOnce(input_api, output_api)) |
[email protected] | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 774 | results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api)) |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 775 | results.extend(_CheckUnwantedDependencies(input_api, output_api)) |
[email protected] | fbcafe5a | 2012-08-08 15:31:22 | [diff] [blame] | 776 | results.extend(_CheckFilePermissions(input_api, output_api)) |
[email protected] | c8278b3 | 2012-10-30 20:35:49 | [diff] [blame] | 777 | results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api)) |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 778 | results.extend(_CheckIncludeOrder(input_api, output_api)) |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 779 | results.extend(_CheckForVersionControlConflicts(input_api, output_api)) |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 780 | results.extend(_CheckPatchFiles(input_api, output_api)) |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 781 | results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api)) |
[email protected] | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 782 | results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api)) |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 783 | results.extend(_CheckForInvalidOSMacros(input_api, output_api)) |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame^] | 784 | results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 785 | |
| 786 | if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()): |
| 787 | results.extend(input_api.canned_checks.RunUnitTestsInDirectory( |
| 788 | input_api, output_api, |
| 789 | input_api.PresubmitLocalPath(), |
[email protected] | 6be6338 | 2013-01-21 15:42:38 | [diff] [blame] | 790 | whitelist=[r'^PRESUBMIT_test\.py$'])) |
[email protected] | 22c9bd7 | 2011-03-27 16:47:39 | [diff] [blame] | 791 | return results |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 792 | |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 793 | |
| 794 | def _CheckSubversionConfig(input_api, output_api): |
| 795 | """Verifies the subversion config file is correctly setup. |
| 796 | |
| 797 | Checks that autoprops are enabled, returns an error otherwise. |
| 798 | """ |
| 799 | join = input_api.os_path.join |
| 800 | if input_api.platform == 'win32': |
| 801 | appdata = input_api.environ.get('APPDATA', '') |
| 802 | if not appdata: |
| 803 | return [output_api.PresubmitError('%APPDATA% is not configured.')] |
| 804 | path = join(appdata, 'Subversion', 'config') |
| 805 | else: |
| 806 | home = input_api.environ.get('HOME', '') |
| 807 | if not home: |
| 808 | return [output_api.PresubmitError('$HOME is not configured.')] |
| 809 | path = join(home, '.subversion', 'config') |
| 810 | |
| 811 | error_msg = ( |
| 812 | 'Please look at http://dev.chromium.org/developers/coding-style to\n' |
| 813 | 'configure your subversion configuration file. This enables automatic\n' |
[email protected] | c6a3c10b | 2011-01-24 16:14:20 | [diff] [blame] | 814 | 'properties to simplify the project maintenance.\n' |
| 815 | 'Pro-tip: just download and install\n' |
| 816 | 'http://src.chromium.org/viewvc/chrome/trunk/tools/build/slave/config\n') |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 817 | |
| 818 | try: |
| 819 | lines = open(path, 'r').read().splitlines() |
| 820 | # Make sure auto-props is enabled and check for 2 Chromium standard |
| 821 | # auto-prop. |
| 822 | if (not '*.cc = svn:eol-style=LF' in lines or |
| 823 | not '*.pdf = svn:mime-type=application/pdf' in lines or |
| 824 | not 'enable-auto-props = yes' in lines): |
| 825 | return [ |
[email protected] | 79ed7e6 | 2011-02-21 21:08:53 | [diff] [blame] | 826 | output_api.PresubmitNotifyResult( |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 827 | 'It looks like you have not configured your subversion config ' |
[email protected] | b5359c0 | 2011-02-01 20:29:56 | [diff] [blame] | 828 | 'file or it is not up-to-date.\n' + error_msg) |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 829 | ] |
| 830 | except (OSError, IOError): |
| 831 | return [ |
[email protected] | 79ed7e6 | 2011-02-21 21:08:53 | [diff] [blame] | 832 | output_api.PresubmitNotifyResult( |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 833 | 'Can\'t find your subversion config file.\n' + error_msg) |
| 834 | ] |
| 835 | return [] |
| 836 | |
| 837 | |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 838 | def _CheckAuthorizedAuthor(input_api, output_api): |
| 839 | """For non-googler/chromites committers, verify the author's email address is |
| 840 | in AUTHORS. |
| 841 | """ |
[email protected] | 9bb9cb8 | 2011-06-13 20:43:01 | [diff] [blame] | 842 | # TODO(maruel): Add it to input_api? |
| 843 | import fnmatch |
| 844 | |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 845 | author = input_api.change.author_email |
[email protected] | 9bb9cb8 | 2011-06-13 20:43:01 | [diff] [blame] | 846 | if not author: |
| 847 | input_api.logging.info('No author, skipping AUTHOR check') |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 848 | return [] |
[email protected] | c9966329 | 2011-05-31 19:46:08 | [diff] [blame] | 849 | authors_path = input_api.os_path.join( |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 850 | input_api.PresubmitLocalPath(), 'AUTHORS') |
| 851 | valid_authors = ( |
| 852 | input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line) |
| 853 | for line in open(authors_path)) |
[email protected] | ac54b13 | 2011-06-06 18:11:18 | [diff] [blame] | 854 | valid_authors = [item.group(1).lower() for item in valid_authors if item] |
[email protected] | d8b50be | 2011-06-15 14:19:44 | [diff] [blame] | 855 | if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors): |
[email protected] | 5861efb | 2013-01-07 18:33:23 | [diff] [blame] | 856 | input_api.logging.info('Valid authors are %s', ', '.join(valid_authors)) |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 857 | return [output_api.PresubmitPromptWarning( |
| 858 | ('%s is not in AUTHORS file. If you are a new contributor, please visit' |
| 859 | '\n' |
| 860 | 'http://www.chromium.org/developers/contributing-code and read the ' |
| 861 | '"Legal" section\n' |
| 862 | 'If you are a chromite, verify the contributor signed the CLA.') % |
| 863 | author)] |
| 864 | return [] |
| 865 | |
| 866 | |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 867 | def _CheckPatchFiles(input_api, output_api): |
| 868 | problems = [f.LocalPath() for f in input_api.AffectedFiles() |
| 869 | if f.LocalPath().endswith(('.orig', '.rej'))] |
| 870 | if problems: |
| 871 | return [output_api.PresubmitError( |
| 872 | "Don't commit .rej and .orig files.", problems)] |
[email protected] | 2fdd1f36 | 2013-01-16 03:56:03 | [diff] [blame] | 873 | else: |
| 874 | return [] |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 875 | |
| 876 | |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 877 | def _DidYouMeanOSMacro(bad_macro): |
| 878 | try: |
| 879 | return {'A': 'OS_ANDROID', |
| 880 | 'B': 'OS_BSD', |
| 881 | 'C': 'OS_CHROMEOS', |
| 882 | 'F': 'OS_FREEBSD', |
| 883 | 'L': 'OS_LINUX', |
| 884 | 'M': 'OS_MACOSX', |
| 885 | 'N': 'OS_NACL', |
| 886 | 'O': 'OS_OPENBSD', |
| 887 | 'P': 'OS_POSIX', |
| 888 | 'S': 'OS_SOLARIS', |
| 889 | 'W': 'OS_WIN'}[bad_macro[3].upper()] |
| 890 | except KeyError: |
| 891 | return '' |
| 892 | |
| 893 | |
| 894 | def _CheckForInvalidOSMacrosInFile(input_api, f): |
| 895 | """Check for sensible looking, totally invalid OS macros.""" |
| 896 | preprocessor_statement = input_api.re.compile(r'^\s*#') |
| 897 | os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)') |
| 898 | results = [] |
| 899 | for lnum, line in f.ChangedContents(): |
| 900 | if preprocessor_statement.search(line): |
| 901 | for match in os_macro.finditer(line): |
| 902 | if not match.group(1) in _VALID_OS_MACROS: |
| 903 | good = _DidYouMeanOSMacro(match.group(1)) |
| 904 | did_you_mean = ' (did you mean %s?)' % good if good else '' |
| 905 | results.append(' %s:%d %s%s' % (f.LocalPath(), |
| 906 | lnum, |
| 907 | match.group(1), |
| 908 | did_you_mean)) |
| 909 | return results |
| 910 | |
| 911 | |
| 912 | def _CheckForInvalidOSMacros(input_api, output_api): |
| 913 | """Check all affected files for invalid OS macros.""" |
| 914 | bad_macros = [] |
| 915 | for f in input_api.AffectedFiles(): |
| 916 | if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')): |
| 917 | bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f)) |
| 918 | |
| 919 | if not bad_macros: |
| 920 | return [] |
| 921 | |
| 922 | return [output_api.PresubmitError( |
| 923 | 'Possibly invalid OS macro[s] found. Please fix your code\n' |
| 924 | 'or add your macro to src/PRESUBMIT.py.', bad_macros)] |
| 925 | |
| 926 | |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 927 | def CheckChangeOnUpload(input_api, output_api): |
| 928 | results = [] |
| 929 | results.extend(_CommonChecks(input_api, output_api)) |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 930 | return results |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 931 | |
| 932 | |
| 933 | def CheckChangeOnCommit(input_api, output_api): |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 934 | results = [] |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 935 | results.extend(_CommonChecks(input_api, output_api)) |
[email protected] | dd805fe | 2009-10-01 08:11:51 | [diff] [blame] | 936 | # TODO(thestig) temporarily disabled, doesn't work in third_party/ |
| 937 | #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories( |
| 938 | # input_api, output_api, sources)) |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 939 | # Make sure the tree is 'open'. |
[email protected] | 806e98e | 2010-03-19 17:49:27 | [diff] [blame] | 940 | results.extend(input_api.canned_checks.CheckTreeIsOpen( |
[email protected] | 7f23815 | 2009-08-12 19:00:34 | [diff] [blame] | 941 | input_api, |
| 942 | output_api, |
[email protected] | 2fdd1f36 | 2013-01-16 03:56:03 | [diff] [blame] | 943 | json_url='http://chromium-status.appspot.com/current?format=json')) |
[email protected] | 806e98e | 2010-03-19 17:49:27 | [diff] [blame] | 944 | results.extend(input_api.canned_checks.CheckRietveldTryJobExecution(input_api, |
[email protected] | 2fdd1f36 | 2013-01-16 03:56:03 | [diff] [blame] | 945 | output_api, 'http://codereview.chromium.org', |
[email protected] | c1ba4c5 | 2012-03-09 14:23:28 | [diff] [blame] | 946 | ('win_rel', 'linux_rel', 'mac_rel, win:compile'), |
| 947 | '[email protected]')) |
[email protected] | 806e98e | 2010-03-19 17:49:27 | [diff] [blame] | 948 | |
[email protected] | 3e4eb11 | 2011-01-18 03:29:54 | [diff] [blame] | 949 | results.extend(input_api.canned_checks.CheckChangeHasBugField( |
| 950 | input_api, output_api)) |
[email protected] | c4b4756 | 2011-12-05 23:39:41 | [diff] [blame] | 951 | results.extend(input_api.canned_checks.CheckChangeHasDescription( |
| 952 | input_api, output_api)) |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 953 | results.extend(_CheckSubversionConfig(input_api, output_api)) |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 954 | return results |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 955 | |
| 956 | |
[email protected] | 5efb2a82 | 2011-09-27 23:06:13 | [diff] [blame] | 957 | def GetPreferredTrySlaves(project, change): |
[email protected] | 4ce995ea | 2012-06-27 02:13:10 | [diff] [blame] | 958 | files = change.LocalPaths() |
| 959 | |
[email protected] | 751b05f | 2013-01-10 23:12:17 | [diff] [blame] | 960 | if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files): |
[email protected] | 3019c90 | 2012-06-29 00:09:03 | [diff] [blame] | 961 | return [] |
| 962 | |
[email protected] | d668899a | 2012-09-06 18:16:59 | [diff] [blame] | 963 | if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files): |
[email protected] | 7fab620 | 2013-02-21 17:54:35 | [diff] [blame] | 964 | return ['mac_rel', 'mac_asan', 'mac:compile'] |
[email protected] | d668899a | 2012-09-06 18:16:59 | [diff] [blame] | 965 | if all(re.search('(^|[/_])win[/_.]', f) for f in files): |
[email protected] | 7fab620 | 2013-02-21 17:54:35 | [diff] [blame] | 966 | return ['win_rel', 'win7_aura', 'win:compile'] |
[email protected] | d668899a | 2012-09-06 18:16:59 | [diff] [blame] | 967 | if all(re.search('(^|[/_])android[/_.]', f) for f in files): |
[email protected] | 3e2f040 | 2012-11-02 16:28:01 | [diff] [blame] | 968 | return ['android_dbg', 'android_clang_dbg'] |
[email protected] | 356aa54 | 2012-09-19 23:31:29 | [diff] [blame] | 969 | if all(re.search('^native_client_sdk', f) for f in files): |
| 970 | return ['linux_nacl_sdk', 'win_nacl_sdk', 'mac_nacl_sdk'] |
[email protected] | de14215 | 2012-10-03 23:02:45 | [diff] [blame] | 971 | if all(re.search('[/_]ios[/_.]', f) for f in files): |
| 972 | return ['ios_rel_device', 'ios_dbg_simulator'] |
[email protected] | 4ce995ea | 2012-06-27 02:13:10 | [diff] [blame] | 973 | |
[email protected] | 3e2f040 | 2012-11-02 16:28:01 | [diff] [blame] | 974 | trybots = [ |
| 975 | 'android_clang_dbg', |
| 976 | 'android_dbg', |
| 977 | 'ios_dbg_simulator', |
| 978 | 'ios_rel_device', |
| 979 | 'linux_asan', |
[email protected] | 95c98916 | 2012-11-29 05:58:25 | [diff] [blame] | 980 | 'linux_aura', |
[email protected] | 3e2f040 | 2012-11-02 16:28:01 | [diff] [blame] | 981 | 'linux_chromeos', |
| 982 | 'linux_clang:compile', |
| 983 | 'linux_rel', |
| 984 | 'mac_asan', |
| 985 | 'mac_rel', |
[email protected] | 7fab620 | 2013-02-21 17:54:35 | [diff] [blame] | 986 | 'mac:compile', |
[email protected] | aa85c8b | 2013-01-11 04:20:28 | [diff] [blame] | 987 | 'win7_aura', |
[email protected] | 3e2f040 | 2012-11-02 16:28:01 | [diff] [blame] | 988 | 'win_rel', |
[email protected] | 7fab620 | 2013-02-21 17:54:35 | [diff] [blame] | 989 | 'win:compile', |
[email protected] | 3e2f040 | 2012-11-02 16:28:01 | [diff] [blame] | 990 | ] |
[email protected] | 911753b | 2012-08-02 12:11:54 | [diff] [blame] | 991 | |
| 992 | # Match things like path/aura/file.cc and path/file_aura.cc. |
[email protected] | 95c98916 | 2012-11-29 05:58:25 | [diff] [blame] | 993 | # Same for chromeos. |
| 994 | if any(re.search('[/_](aura|chromeos)', f) for f in files): |
[email protected] | 3e2f040 | 2012-11-02 16:28:01 | [diff] [blame] | 995 | trybots += ['linux_chromeos_clang:compile', 'linux_chromeos_asan'] |
[email protected] | 4ce995ea | 2012-06-27 02:13:10 | [diff] [blame] | 996 | |
[email protected] | 4ce995ea | 2012-06-27 02:13:10 | [diff] [blame] | 997 | return trybots |