[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 |
tfarina | 78bb92f4 | 2015-01-31 00:20:48 | [diff] [blame] | 8 | for more details about the presubmit API built into depot_tools. |
[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] | 379e7dd | 2010-01-28 17:39:21 | [diff] [blame] | 12 | _EXCLUDED_PATHS = ( |
[email protected] | 3e4eb11 | 2011-01-18 03:29:54 | [diff] [blame] | 13 | r"^breakpad[\\\/].*", |
[email protected] | 40d1dbb | 2012-10-26 07:18:00 | [diff] [blame] | 14 | r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_rules.py", |
| 15 | r"^native_client_sdk[\\\/]src[\\\/]build_tools[\\\/]make_simple.py", |
[email protected] | 8886ffcb | 2013-02-12 04:56:28 | [diff] [blame] | 16 | r"^native_client_sdk[\\\/]src[\\\/]tools[\\\/].*.mk", |
[email protected] | a18130a | 2012-01-03 17:52:08 | [diff] [blame] | 17 | r"^net[\\\/]tools[\\\/]spdyshark[\\\/].*", |
[email protected] | 3e4eb11 | 2011-01-18 03:29:54 | [diff] [blame] | 18 | r"^skia[\\\/].*", |
| 19 | r"^v8[\\\/].*", |
| 20 | r".*MakeFile$", |
[email protected] | 1084ccc | 2012-03-14 03:22:53 | [diff] [blame] | 21 | r".+_autogen\.h$", |
[email protected] | ce145c0 | 2012-09-06 09:49:34 | [diff] [blame] | 22 | r".+[\\\/]pnacl_shim\.c$", |
[email protected] | e07b6ac7 | 2013-08-20 00:30:42 | [diff] [blame] | 23 | r"^gpu[\\\/]config[\\\/].*_list_json\.cc$", |
[email protected] | d260060 | 2014-02-19 00:09:19 | [diff] [blame] | 24 | r"^chrome[\\\/]browser[\\\/]resources[\\\/]pdf[\\\/]index.js" |
[email protected] | 430641764 | 2009-06-11 00:33:40 | [diff] [blame] | 25 | ) |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 26 | |
jochen | 9ea8fdbc | 2014-09-25 13:21:35 | [diff] [blame] | 27 | # The NetscapePlugIn library is excluded from pan-project as it will soon |
| 28 | # be deleted together with the rest of the NPAPI and it's not worthwhile to |
| 29 | # update the coding style until then. |
[email protected] | 3de922f | 2013-12-20 13:27:38 | [diff] [blame] | 30 | _TESTRUNNER_PATHS = ( |
[email protected] | de28fed2e | 2014-02-01 14:36:32 | [diff] [blame] | 31 | r"^content[\\\/]shell[\\\/]tools[\\\/]plugin[\\\/].*", |
[email protected] | 3de922f | 2013-12-20 13:27:38 | [diff] [blame] | 32 | ) |
| 33 | |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 34 | # Fragment of a regular expression that matches C++ and Objective-C++ |
| 35 | # implementation files. |
| 36 | _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$' |
| 37 | |
| 38 | # Regular expression that matches code only used for test binaries |
| 39 | # (best effort). |
| 40 | _TEST_CODE_EXCLUDED_PATHS = ( |
joaodasilva | 718f8767 | 2014-08-30 09:25:49 | [diff] [blame] | 41 | r'.*[\\\/](fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS, |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 42 | r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS, |
[email protected] | 6e04f8c | 2014-01-29 18:08:32 | [diff] [blame] | 43 | r'.+_(api|browser|kif|perf|pixel|unit|ui)?test(_[a-z]+)?%s' % |
[email protected] | e2d7e6f | 2013-04-23 12:57:12 | [diff] [blame] | 44 | _IMPLEMENTATION_EXTENSIONS, |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 45 | r'.+profile_sync_service_harness%s' % _IMPLEMENTATION_EXTENSIONS, |
joaodasilva | 718f8767 | 2014-08-30 09:25:49 | [diff] [blame] | 46 | r'.*[\\\/](test|tool(s)?)[\\\/].*', |
[email protected] | ef070cc | 2013-05-03 11:53:05 | [diff] [blame] | 47 | # content_shell is used for running layout tests. |
joaodasilva | 718f8767 | 2014-08-30 09:25:49 | [diff] [blame] | 48 | r'content[\\\/]shell[\\\/].*', |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 49 | # At request of folks maintaining this folder. |
joaodasilva | 718f8767 | 2014-08-30 09:25:49 | [diff] [blame] | 50 | r'chrome[\\\/]browser[\\\/]automation[\\\/].*', |
[email protected] | 7b05498 | 2013-11-27 00:44:47 | [diff] [blame] | 51 | # Non-production example code. |
joaodasilva | 718f8767 | 2014-08-30 09:25:49 | [diff] [blame] | 52 | r'mojo[\\\/]examples[\\\/].*', |
[email protected] | 8176de1 | 2014-06-20 19:07:08 | [diff] [blame] | 53 | # Launcher for running iOS tests on the simulator. |
joaodasilva | 718f8767 | 2014-08-30 09:25:49 | [diff] [blame] | 54 | r'testing[\\\/]iossim[\\\/]iossim\.mm$', |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 55 | ) |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 56 | |
[email protected] | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 57 | _TEST_ONLY_WARNING = ( |
| 58 | 'You might be calling functions intended only for testing from\n' |
| 59 | 'production code. It is OK to ignore this warning if you know what\n' |
| 60 | 'you are doing, as the heuristics used to detect the situation are\n' |
[email protected] | b014977 | 2014-03-27 16:47:58 | [diff] [blame] | 61 | 'not perfect. The commit queue will not block on this warning.') |
[email protected] | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 62 | |
| 63 | |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 64 | _INCLUDE_ORDER_WARNING = ( |
marja | a017dc48 | 2015-03-09 17:13:40 | [diff] [blame] | 65 | 'Your #include order seems to be broken. Remember to use the right ' |
brucedawson | 70fadb0 | 2015-06-30 17:47:55 | [diff] [blame] | 66 | 'collation (LC_COLLATE=C) and check\nhttps://google-styleguide.googlecode' |
marja | a017dc48 | 2015-03-09 17:13:40 | [diff] [blame] | 67 | '.com/svn/trunk/cppguide.html#Names_and_Order_of_Includes') |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 68 | |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 69 | _BANNED_OBJC_FUNCTIONS = ( |
| 70 | ( |
| 71 | 'addTrackingRect:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 72 | ( |
| 73 | 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is' |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 74 | 'prohibited. Please use CrTrackingArea instead.', |
| 75 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 76 | ), |
| 77 | False, |
| 78 | ), |
| 79 | ( |
[email protected] | eaae197 | 2014-04-16 04:17:26 | [diff] [blame] | 80 | r'/NSTrackingArea\W', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 81 | ( |
| 82 | 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 83 | 'instead.', |
| 84 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 85 | ), |
| 86 | False, |
| 87 | ), |
| 88 | ( |
| 89 | 'convertPointFromBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 90 | ( |
| 91 | 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 92 | 'Please use |convertPoint:(point) fromView:nil| instead.', |
| 93 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 94 | ), |
| 95 | True, |
| 96 | ), |
| 97 | ( |
| 98 | 'convertPointToBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 99 | ( |
| 100 | 'The use of -[NSView convertPointToBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 101 | 'Please use |convertPoint:(point) toView:nil| instead.', |
| 102 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 103 | ), |
| 104 | True, |
| 105 | ), |
| 106 | ( |
| 107 | 'convertRectFromBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 108 | ( |
| 109 | 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 110 | 'Please use |convertRect:(point) fromView:nil| instead.', |
| 111 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 112 | ), |
| 113 | True, |
| 114 | ), |
| 115 | ( |
| 116 | 'convertRectToBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 117 | ( |
| 118 | 'The use of -[NSView convertRectToBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 119 | 'Please use |convertRect:(point) toView:nil| instead.', |
| 120 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 121 | ), |
| 122 | True, |
| 123 | ), |
| 124 | ( |
| 125 | 'convertSizeFromBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 126 | ( |
| 127 | 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 128 | 'Please use |convertSize:(point) fromView:nil| instead.', |
| 129 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 130 | ), |
| 131 | True, |
| 132 | ), |
| 133 | ( |
| 134 | 'convertSizeToBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 135 | ( |
| 136 | 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 137 | 'Please use |convertSize:(point) toView:nil| instead.', |
| 138 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 139 | ), |
| 140 | True, |
| 141 | ), |
| 142 | ) |
| 143 | |
| 144 | |
| 145 | _BANNED_CPP_FUNCTIONS = ( |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 146 | # Make sure that gtest's FRIEND_TEST() macro is not used; the |
| 147 | # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be |
[email protected] | e00ccc9 | 2012-11-01 17:32:30 | [diff] [blame] | 148 | # used instead since that allows for FLAKY_ and DISABLED_ prefixes. |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 149 | ( |
| 150 | 'FRIEND_TEST(', |
| 151 | ( |
[email protected] | e3c94550 | 2012-06-26 20:01:49 | [diff] [blame] | 152 | 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 153 | 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.', |
| 154 | ), |
| 155 | False, |
[email protected] | 7345da0 | 2012-11-27 14:31:49 | [diff] [blame] | 156 | (), |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 157 | ), |
| 158 | ( |
| 159 | 'ScopedAllowIO', |
| 160 | ( |
[email protected] | e3c94550 | 2012-06-26 20:01:49 | [diff] [blame] | 161 | 'New code should not use ScopedAllowIO. Post a task to the blocking', |
| 162 | 'pool or the FILE thread instead.', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 163 | ), |
[email protected] | e3c94550 | 2012-06-26 20:01:49 | [diff] [blame] | 164 | True, |
[email protected] | 7345da0 | 2012-11-27 14:31:49 | [diff] [blame] | 165 | ( |
thestig | 75844fdb | 2014-09-09 19:47:10 | [diff] [blame] | 166 | r"^base[\\\/]process[\\\/]process_metrics_linux\.cc$", |
tfarina | 0923ac5 | 2015-01-07 03:21:22 | [diff] [blame] | 167 | r"^chrome[\\\/]browser[\\\/]chromeos[\\\/]boot_times_recorder\.cc$", |
alemate | e4016bb | 2014-11-12 17:38:51 | [diff] [blame] | 168 | r"^chrome[\\\/]browser[\\\/]chromeos[\\\/]" |
| 169 | "customization_document_browsertest\.cc$", |
philipj | 3f9d5bde | 2014-08-28 14:09:09 | [diff] [blame] | 170 | r"^components[\\\/]crash[\\\/]app[\\\/]breakpad_mac\.mm$", |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 171 | r"^content[\\\/]shell[\\\/]browser[\\\/]shell_browser_main\.cc$", |
| 172 | r"^content[\\\/]shell[\\\/]browser[\\\/]shell_message_filter\.cc$", |
jamesr | a03ae49 | 2014-10-03 04:26:48 | [diff] [blame] | 173 | r"^mojo[\\\/]edk[\\\/]embedder[\\\/]" + |
| 174 | r"simple_platform_shared_buffer_posix\.cc$", |
[email protected] | 398ad13 | 2013-04-02 15:11:01 | [diff] [blame] | 175 | r"^net[\\\/]disk_cache[\\\/]cache_util\.cc$", |
[email protected] | 1f52a57 | 2014-05-12 23:21:54 | [diff] [blame] | 176 | r"^net[\\\/]url_request[\\\/]test_url_fetcher_factory\.cc$", |
dnicoara | 171d8c8 | 2015-03-05 20:46:18 | [diff] [blame] | 177 | r"^ui[\\\/]ozone[\\\/]platform[\\\/]drm[\\\/]host[\\\/]" |
dnicoara | b29d051 | 2015-05-07 19:29:23 | [diff] [blame] | 178 | "drm_display_host_manager\.cc$", |
[email protected] | 7345da0 | 2012-11-27 14:31:49 | [diff] [blame] | 179 | ), |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 180 | ), |
[email protected] | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 181 | ( |
| 182 | 'SkRefPtr', |
| 183 | ( |
| 184 | 'The use of SkRefPtr is prohibited. ', |
| 185 | 'Please use skia::RefPtr instead.' |
| 186 | ), |
| 187 | True, |
| 188 | (), |
| 189 | ), |
| 190 | ( |
| 191 | 'SkAutoRef', |
| 192 | ( |
| 193 | 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ', |
| 194 | 'Please use skia::RefPtr instead.' |
| 195 | ), |
| 196 | True, |
| 197 | (), |
| 198 | ), |
| 199 | ( |
| 200 | 'SkAutoTUnref', |
| 201 | ( |
| 202 | 'The use of SkAutoTUnref is dangerous because it implicitly ', |
| 203 | 'converts to a raw pointer. Please use skia::RefPtr instead.' |
| 204 | ), |
| 205 | True, |
| 206 | (), |
| 207 | ), |
| 208 | ( |
| 209 | 'SkAutoUnref', |
| 210 | ( |
| 211 | 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ', |
| 212 | 'because it implicitly converts to a raw pointer. ', |
| 213 | 'Please use skia::RefPtr instead.' |
| 214 | ), |
| 215 | True, |
| 216 | (), |
| 217 | ), |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 218 | ( |
| 219 | r'/HANDLE_EINTR\(.*close', |
| 220 | ( |
| 221 | 'HANDLE_EINTR(close) is invalid. If close fails with EINTR, the file', |
| 222 | 'descriptor will be closed, and it is incorrect to retry the close.', |
| 223 | 'Either call close directly and ignore its return value, or wrap close', |
| 224 | 'in IGNORE_EINTR to use its return value. See http://crbug.com/269623' |
| 225 | ), |
| 226 | True, |
| 227 | (), |
| 228 | ), |
| 229 | ( |
| 230 | r'/IGNORE_EINTR\((?!.*close)', |
| 231 | ( |
| 232 | 'IGNORE_EINTR is only valid when wrapping close. To wrap other system', |
| 233 | 'calls, use HANDLE_EINTR. See http://crbug.com/269623', |
| 234 | ), |
| 235 | True, |
| 236 | ( |
| 237 | # Files that #define IGNORE_EINTR. |
| 238 | r'^base[\\\/]posix[\\\/]eintr_wrapper\.h$', |
| 239 | r'^ppapi[\\\/]tests[\\\/]test_broker\.cc$', |
| 240 | ), |
| 241 | ), |
[email protected] | ec5b3f0 | 2014-04-04 18:43:43 | [diff] [blame] | 242 | ( |
| 243 | r'/v8::Extension\(', |
| 244 | ( |
| 245 | 'Do not introduce new v8::Extensions into the code base, use', |
| 246 | 'gin::Wrappable instead. See http://crbug.com/334679', |
| 247 | ), |
| 248 | True, |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 249 | ( |
joaodasilva | 718f8767 | 2014-08-30 09:25:49 | [diff] [blame] | 250 | r'extensions[\\\/]renderer[\\\/]safe_builtins\.*', |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 251 | ), |
[email protected] | ec5b3f0 | 2014-04-04 18:43:43 | [diff] [blame] | 252 | ), |
skyostil | f9469f7 | 2015-04-20 10:38:52 | [diff] [blame] | 253 | ( |
sdefresne | eaeccc5 | 2015-04-22 08:18:32 | [diff] [blame] | 254 | '\<MessageLoopProxy\>', |
skyostil | f9469f7 | 2015-04-20 10:38:52 | [diff] [blame] | 255 | ( |
| 256 | 'MessageLoopProxy is deprecated. ', |
| 257 | 'Please use SingleThreadTaskRunner or ThreadTaskRunnerHandle instead.' |
| 258 | ), |
| 259 | True, |
kinuko | 59024ce | 2015-04-21 22:18:30 | [diff] [blame] | 260 | ( |
| 261 | # Internal message_loop related code may still use it. |
| 262 | r'^base[\\\/]message_loop[\\\/].*', |
| 263 | ), |
skyostil | f9469f7 | 2015-04-20 10:38:52 | [diff] [blame] | 264 | ), |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 265 | ) |
| 266 | |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 267 | _IPC_ENUM_TRAITS_DEPRECATED = ( |
| 268 | 'You are using IPC_ENUM_TRAITS() in your code. It has been deprecated.\n' |
| 269 | 'See http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc') |
| 270 | |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 271 | |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 272 | _VALID_OS_MACROS = ( |
| 273 | # Please keep sorted. |
| 274 | 'OS_ANDROID', |
[email protected] | f4440b4 | 2014-03-19 05:47:01 | [diff] [blame] | 275 | 'OS_ANDROID_HOST', |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 276 | 'OS_BSD', |
| 277 | 'OS_CAT', # For testing. |
| 278 | 'OS_CHROMEOS', |
| 279 | 'OS_FREEBSD', |
| 280 | 'OS_IOS', |
| 281 | 'OS_LINUX', |
| 282 | 'OS_MACOSX', |
| 283 | 'OS_NACL', |
hidehiko | f7295f2 | 2014-10-28 11:57:21 | [diff] [blame] | 284 | 'OS_NACL_NONSFI', |
| 285 | 'OS_NACL_SFI', |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 286 | 'OS_OPENBSD', |
| 287 | 'OS_POSIX', |
[email protected] | eda7afa1 | 2014-02-06 12:27:37 | [diff] [blame] | 288 | 'OS_QNX', |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 289 | 'OS_SOLARIS', |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 290 | 'OS_WIN', |
| 291 | ) |
| 292 | |
| 293 | |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 294 | def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api): |
| 295 | """Attempts to prevent use of functions intended only for testing in |
| 296 | non-testing code. For now this is just a best-effort implementation |
| 297 | that ignores header files and may have some false positives. A |
| 298 | better implementation would probably need a proper C++ parser. |
| 299 | """ |
| 300 | # We only scan .cc files and the like, as the declaration of |
| 301 | # for-testing functions in header files are hard to distinguish from |
| 302 | # calls to such functions without a proper C++ parser. |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 303 | file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 304 | |
[email protected] | 2350182 | 2014-05-14 02:06:09 | [diff] [blame] | 305 | base_function_pattern = r'[ :]test::[^\s]+|ForTest(ing)?|for_test(ing)?' |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 306 | inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern) |
[email protected] | 2350182 | 2014-05-14 02:06:09 | [diff] [blame] | 307 | comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern) |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 308 | exclusion_pattern = input_api.re.compile( |
| 309 | r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % ( |
| 310 | base_function_pattern, base_function_pattern)) |
| 311 | |
| 312 | def FilterFile(affected_file): |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 313 | black_list = (_EXCLUDED_PATHS + |
| 314 | _TEST_CODE_EXCLUDED_PATHS + |
| 315 | input_api.DEFAULT_BLACK_LIST) |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 316 | return input_api.FilterSourceFile( |
| 317 | affected_file, |
| 318 | white_list=(file_inclusion_pattern, ), |
| 319 | black_list=black_list) |
| 320 | |
| 321 | problems = [] |
| 322 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 323 | local_path = f.LocalPath() |
[email protected] | 825d2718 | 2014-01-02 21:24:24 | [diff] [blame] | 324 | for line_number, line in f.ChangedContents(): |
[email protected] | 2fdd1f36 | 2013-01-16 03:56:03 | [diff] [blame] | 325 | if (inclusion_pattern.search(line) and |
[email protected] | de4f7d2 | 2013-05-23 14:27:46 | [diff] [blame] | 326 | not comment_pattern.search(line) and |
[email protected] | 2fdd1f36 | 2013-01-16 03:56:03 | [diff] [blame] | 327 | not exclusion_pattern.search(line)): |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 328 | problems.append( |
[email protected] | 2fdd1f36 | 2013-01-16 03:56:03 | [diff] [blame] | 329 | '%s:%d\n %s' % (local_path, line_number, line.strip())) |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 330 | |
| 331 | if problems: |
[email protected] | f7051d5 | 2013-04-02 18:31:42 | [diff] [blame] | 332 | return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)] |
[email protected] | 2fdd1f36 | 2013-01-16 03:56:03 | [diff] [blame] | 333 | else: |
| 334 | return [] |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 335 | |
| 336 | |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 337 | def _CheckNoIOStreamInHeaders(input_api, output_api): |
| 338 | """Checks to make sure no .h files include <iostream>.""" |
| 339 | files = [] |
| 340 | pattern = input_api.re.compile(r'^#include\s*<iostream>', |
| 341 | input_api.re.MULTILINE) |
| 342 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 343 | if not f.LocalPath().endswith('.h'): |
| 344 | continue |
| 345 | contents = input_api.ReadFile(f) |
| 346 | if pattern.search(contents): |
| 347 | files.append(f) |
| 348 | |
| 349 | if len(files): |
| 350 | return [ output_api.PresubmitError( |
[email protected] | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 351 | 'Do not #include <iostream> in header files, since it inserts static ' |
| 352 | 'initialization into every file including the header. Instead, ' |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 353 | '#include <ostream>. See http://crbug.com/94794', |
| 354 | files) ] |
| 355 | return [] |
| 356 | |
| 357 | |
[email protected] | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 358 | def _CheckNoUNIT_TESTInSourceFiles(input_api, output_api): |
| 359 | """Checks to make sure no source files use UNIT_TEST""" |
| 360 | problems = [] |
| 361 | for f in input_api.AffectedFiles(): |
| 362 | if (not f.LocalPath().endswith(('.cc', '.mm'))): |
| 363 | continue |
| 364 | |
| 365 | for line_num, line in f.ChangedContents(): |
[email protected] | 549f86a | 2013-11-19 13:00:04 | [diff] [blame] | 366 | if 'UNIT_TEST ' in line or line.endswith('UNIT_TEST'): |
[email protected] | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 367 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 368 | |
| 369 | if not problems: |
| 370 | return [] |
| 371 | return [output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' + |
| 372 | '\n'.join(problems))] |
| 373 | |
| 374 | |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 375 | def _FindHistogramNameInLine(histogram_name, line): |
| 376 | """Tries to find a histogram name or prefix in a line.""" |
| 377 | if not "affected-histogram" in line: |
| 378 | return histogram_name in line |
| 379 | # A histogram_suffixes tag type has an affected-histogram name as a prefix of |
| 380 | # the histogram_name. |
| 381 | if not '"' in line: |
| 382 | return False |
| 383 | histogram_prefix = line.split('\"')[1] |
| 384 | return histogram_prefix in histogram_name |
| 385 | |
| 386 | |
| 387 | def _CheckUmaHistogramChanges(input_api, output_api): |
| 388 | """Check that UMA histogram names in touched lines can still be found in other |
| 389 | lines of the patch or in histograms.xml. Note that this check would not catch |
| 390 | the reverse: changes in histograms.xml not matched in the code itself.""" |
| 391 | touched_histograms = [] |
| 392 | histograms_xml_modifications = [] |
| 393 | pattern = input_api.re.compile('UMA_HISTOGRAM.*\("(.*)"') |
| 394 | for f in input_api.AffectedFiles(): |
| 395 | # If histograms.xml itself is modified, keep the modified lines for later. |
| 396 | if f.LocalPath().endswith(('histograms.xml')): |
| 397 | histograms_xml_modifications = f.ChangedContents() |
| 398 | continue |
| 399 | if not f.LocalPath().endswith(('cc', 'mm', 'cpp')): |
| 400 | continue |
| 401 | for line_num, line in f.ChangedContents(): |
| 402 | found = pattern.search(line) |
| 403 | if found: |
| 404 | touched_histograms.append([found.group(1), f, line_num]) |
| 405 | |
| 406 | # Search for the touched histogram names in the local modifications to |
| 407 | # histograms.xml, and, if not found, on the base histograms.xml file. |
| 408 | unmatched_histograms = [] |
| 409 | for histogram_info in touched_histograms: |
| 410 | histogram_name_found = False |
| 411 | for line_num, line in histograms_xml_modifications: |
| 412 | histogram_name_found = _FindHistogramNameInLine(histogram_info[0], line) |
| 413 | if histogram_name_found: |
| 414 | break |
| 415 | if not histogram_name_found: |
| 416 | unmatched_histograms.append(histogram_info) |
| 417 | |
eroman | b90c82e7e3 | 2015-04-01 15:13:49 | [diff] [blame] | 418 | histograms_xml_path = 'tools/metrics/histograms/histograms.xml' |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 419 | problems = [] |
| 420 | if unmatched_histograms: |
eroman | b90c82e7e3 | 2015-04-01 15:13:49 | [diff] [blame] | 421 | with open(histograms_xml_path) as histograms_xml: |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 422 | for histogram_name, f, line_num in unmatched_histograms: |
mcasas | 39c1b8b | 2015-02-25 15:33:45 | [diff] [blame] | 423 | histograms_xml.seek(0) |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 424 | histogram_name_found = False |
| 425 | for line in histograms_xml: |
| 426 | histogram_name_found = _FindHistogramNameInLine(histogram_name, line) |
| 427 | if histogram_name_found: |
| 428 | break |
| 429 | if not histogram_name_found: |
| 430 | problems.append(' [%s:%d] %s' % |
| 431 | (f.LocalPath(), line_num, histogram_name)) |
| 432 | |
| 433 | if not problems: |
| 434 | return [] |
| 435 | return [output_api.PresubmitPromptWarning('Some UMA_HISTOGRAM lines have ' |
| 436 | 'been modified and the associated histogram name has no match in either ' |
eroman | b90c82e7e3 | 2015-04-01 15:13:49 | [diff] [blame] | 437 | '%s or the modifications of it:' % (histograms_xml_path), problems)] |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 438 | |
| 439 | |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 440 | def _CheckNoNewWStrings(input_api, output_api): |
| 441 | """Checks to make sure we don't introduce use of wstrings.""" |
[email protected] | 55463aa6 | 2011-10-12 00:48:27 | [diff] [blame] | 442 | problems = [] |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 443 | for f in input_api.AffectedFiles(): |
[email protected] | b5c2429 | 2011-11-28 14:38:20 | [diff] [blame] | 444 | if (not f.LocalPath().endswith(('.cc', '.h')) or |
scottmg | e6f0440 | 2014-11-05 01:59:57 | [diff] [blame] | 445 | f.LocalPath().endswith(('test.cc', '_win.cc', '_win.h')) or |
| 446 | '/win/' in f.LocalPath()): |
[email protected] | b5c2429 | 2011-11-28 14:38:20 | [diff] [blame] | 447 | continue |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 448 | |
[email protected] | a11dbe9b | 2012-08-07 01:32:58 | [diff] [blame] | 449 | allowWString = False |
[email protected] | b5c2429 | 2011-11-28 14:38:20 | [diff] [blame] | 450 | for line_num, line in f.ChangedContents(): |
[email protected] | a11dbe9b | 2012-08-07 01:32:58 | [diff] [blame] | 451 | if 'presubmit: allow wstring' in line: |
| 452 | allowWString = True |
| 453 | elif not allowWString and 'wstring' in line: |
[email protected] | 55463aa6 | 2011-10-12 00:48:27 | [diff] [blame] | 454 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
[email protected] | a11dbe9b | 2012-08-07 01:32:58 | [diff] [blame] | 455 | allowWString = False |
| 456 | else: |
| 457 | allowWString = False |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 458 | |
[email protected] | 55463aa6 | 2011-10-12 00:48:27 | [diff] [blame] | 459 | if not problems: |
| 460 | return [] |
| 461 | return [output_api.PresubmitPromptWarning('New code should not use wstrings.' |
[email protected] | a11dbe9b | 2012-08-07 01:32:58 | [diff] [blame] | 462 | ' If you are calling a cross-platform API that accepts a wstring, ' |
| 463 | 'fix the API.\n' + |
[email protected] | 55463aa6 | 2011-10-12 00:48:27 | [diff] [blame] | 464 | '\n'.join(problems))] |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 465 | |
| 466 | |
[email protected] | 2a8ac9c | 2011-10-19 17:20:44 | [diff] [blame] | 467 | def _CheckNoDEPSGIT(input_api, output_api): |
| 468 | """Make sure .DEPS.git is never modified manually.""" |
| 469 | if any(f.LocalPath().endswith('.DEPS.git') for f in |
| 470 | input_api.AffectedFiles()): |
| 471 | return [output_api.PresubmitError( |
| 472 | 'Never commit changes to .DEPS.git. This file is maintained by an\n' |
| 473 | 'automated system based on what\'s in DEPS and your changes will be\n' |
| 474 | 'overwritten.\n' |
[email protected] | cb70691 | 2014-06-28 20:46:34 | [diff] [blame] | 475 | 'See https://sites.google.com/a/chromium.org/dev/developers/how-tos/get-the-code#Rolling_DEPS\n' |
[email protected] | 2a8ac9c | 2011-10-19 17:20:44 | [diff] [blame] | 476 | 'for more information')] |
| 477 | return [] |
| 478 | |
| 479 | |
tandrii | ef66469 | 2014-09-23 14:51:47 | [diff] [blame] | 480 | def _CheckValidHostsInDEPS(input_api, output_api): |
| 481 | """Checks that DEPS file deps are from allowed_hosts.""" |
| 482 | # Run only if DEPS file has been modified to annoy fewer bystanders. |
| 483 | if all(f.LocalPath() != 'DEPS' for f in input_api.AffectedFiles()): |
| 484 | return [] |
| 485 | # Outsource work to gclient verify |
| 486 | try: |
| 487 | input_api.subprocess.check_output(['gclient', 'verify']) |
| 488 | return [] |
| 489 | except input_api.subprocess.CalledProcessError, error: |
| 490 | return [output_api.PresubmitError( |
| 491 | 'DEPS file must have only git dependencies.', |
| 492 | long_text=error.output)] |
| 493 | |
| 494 | |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 495 | def _CheckNoBannedFunctions(input_api, output_api): |
| 496 | """Make sure that banned functions are not used.""" |
| 497 | warnings = [] |
| 498 | errors = [] |
| 499 | |
| 500 | file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h')) |
| 501 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 502 | for line_num, line in f.ChangedContents(): |
| 503 | for func_name, message, error in _BANNED_OBJC_FUNCTIONS: |
[email protected] | eaae197 | 2014-04-16 04:17:26 | [diff] [blame] | 504 | matched = False |
| 505 | if func_name[0:1] == '/': |
| 506 | regex = func_name[1:] |
| 507 | if input_api.re.search(regex, line): |
| 508 | matched = True |
| 509 | elif func_name in line: |
| 510 | matched = True |
| 511 | if matched: |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 512 | problems = warnings; |
| 513 | if error: |
| 514 | problems = errors; |
| 515 | problems.append(' %s:%d:' % (f.LocalPath(), line_num)) |
| 516 | for message_line in message: |
| 517 | problems.append(' %s' % message_line) |
| 518 | |
| 519 | file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h')) |
| 520 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 521 | for line_num, line in f.ChangedContents(): |
[email protected] | 7345da0 | 2012-11-27 14:31:49 | [diff] [blame] | 522 | for func_name, message, error, excluded_paths in _BANNED_CPP_FUNCTIONS: |
| 523 | def IsBlacklisted(affected_file, blacklist): |
| 524 | local_path = affected_file.LocalPath() |
| 525 | for item in blacklist: |
| 526 | if input_api.re.match(item, local_path): |
| 527 | return True |
| 528 | return False |
| 529 | if IsBlacklisted(f, excluded_paths): |
| 530 | continue |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 531 | matched = False |
| 532 | if func_name[0:1] == '/': |
| 533 | regex = func_name[1:] |
| 534 | if input_api.re.search(regex, line): |
| 535 | matched = True |
| 536 | elif func_name in line: |
| 537 | matched = True |
| 538 | if matched: |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 539 | problems = warnings; |
| 540 | if error: |
| 541 | problems = errors; |
| 542 | problems.append(' %s:%d:' % (f.LocalPath(), line_num)) |
| 543 | for message_line in message: |
| 544 | problems.append(' %s' % message_line) |
| 545 | |
| 546 | result = [] |
| 547 | if (warnings): |
| 548 | result.append(output_api.PresubmitPromptWarning( |
| 549 | 'Banned functions were used.\n' + '\n'.join(warnings))) |
| 550 | if (errors): |
| 551 | result.append(output_api.PresubmitError( |
| 552 | 'Banned functions were used.\n' + '\n'.join(errors))) |
| 553 | return result |
| 554 | |
| 555 | |
[email protected] | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 556 | def _CheckNoPragmaOnce(input_api, output_api): |
| 557 | """Make sure that banned functions are not used.""" |
| 558 | files = [] |
| 559 | pattern = input_api.re.compile(r'^#pragma\s+once', |
| 560 | input_api.re.MULTILINE) |
| 561 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 562 | if not f.LocalPath().endswith('.h'): |
| 563 | continue |
| 564 | contents = input_api.ReadFile(f) |
| 565 | if pattern.search(contents): |
| 566 | files.append(f) |
| 567 | |
| 568 | if files: |
| 569 | return [output_api.PresubmitError( |
| 570 | 'Do not use #pragma once in header files.\n' |
| 571 | 'See http://www.chromium.org/developers/coding-style#TOC-File-headers', |
| 572 | files)] |
| 573 | return [] |
| 574 | |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 575 | |
[email protected] | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 576 | def _CheckNoTrinaryTrueFalse(input_api, output_api): |
| 577 | """Checks to make sure we don't introduce use of foo ? true : false.""" |
| 578 | problems = [] |
| 579 | pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)') |
| 580 | for f in input_api.AffectedFiles(): |
| 581 | if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')): |
| 582 | continue |
| 583 | |
| 584 | for line_num, line in f.ChangedContents(): |
| 585 | if pattern.match(line): |
| 586 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 587 | |
| 588 | if not problems: |
| 589 | return [] |
| 590 | return [output_api.PresubmitPromptWarning( |
| 591 | 'Please consider avoiding the "? true : false" pattern if possible.\n' + |
| 592 | '\n'.join(problems))] |
| 593 | |
| 594 | |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 595 | def _CheckUnwantedDependencies(input_api, output_api): |
| 596 | """Runs checkdeps on #include statements added in this |
| 597 | change. Breaking - rules is an error, breaking ! rules is a |
| 598 | warning. |
| 599 | """ |
mohan.reddy | f21db96 | 2014-10-16 12:26:47 | [diff] [blame] | 600 | import sys |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 601 | # We need to wait until we have an input_api object and use this |
| 602 | # roundabout construct to import checkdeps because this file is |
| 603 | # eval-ed and thus doesn't have __file__. |
| 604 | original_sys_path = sys.path |
| 605 | try: |
| 606 | sys.path = sys.path + [input_api.os_path.join( |
[email protected] | 5298cc98 | 2014-05-29 20:53:47 | [diff] [blame] | 607 | input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')] |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 608 | import checkdeps |
| 609 | from cpp_checker import CppChecker |
| 610 | from rules import Rule |
| 611 | finally: |
| 612 | # Restore sys.path to what it was before. |
| 613 | sys.path = original_sys_path |
| 614 | |
| 615 | added_includes = [] |
| 616 | for f in input_api.AffectedFiles(): |
| 617 | if not CppChecker.IsCppFile(f.LocalPath()): |
| 618 | continue |
| 619 | |
| 620 | changed_lines = [line for line_num, line in f.ChangedContents()] |
| 621 | added_includes.append([f.LocalPath(), changed_lines]) |
| 622 | |
[email protected] | 2638517 | 2013-05-09 23:11:35 | [diff] [blame] | 623 | deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath()) |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 624 | |
| 625 | error_descriptions = [] |
| 626 | warning_descriptions = [] |
| 627 | for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes( |
| 628 | added_includes): |
| 629 | description_with_path = '%s\n %s' % (path, rule_description) |
| 630 | if rule_type == Rule.DISALLOW: |
| 631 | error_descriptions.append(description_with_path) |
| 632 | else: |
| 633 | warning_descriptions.append(description_with_path) |
| 634 | |
| 635 | results = [] |
| 636 | if error_descriptions: |
| 637 | results.append(output_api.PresubmitError( |
| 638 | 'You added one or more #includes that violate checkdeps rules.', |
| 639 | error_descriptions)) |
| 640 | if warning_descriptions: |
[email protected] | f7051d5 | 2013-04-02 18:31:42 | [diff] [blame] | 641 | results.append(output_api.PresubmitPromptOrNotify( |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 642 | 'You added one or more #includes of files that are temporarily\n' |
| 643 | 'allowed but being removed. Can you avoid introducing the\n' |
| 644 | '#include? See relevant DEPS file(s) for details and contacts.', |
| 645 | warning_descriptions)) |
| 646 | return results |
| 647 | |
| 648 | |
[email protected] | fbcafe5a | 2012-08-08 15:31:22 | [diff] [blame] | 649 | def _CheckFilePermissions(input_api, output_api): |
| 650 | """Check that all files have their permissions properly set.""" |
[email protected] | 79150720 | 2014-02-03 23:19:15 | [diff] [blame] | 651 | if input_api.platform == 'win32': |
| 652 | return [] |
mohan.reddy | f21db96 | 2014-10-16 12:26:47 | [diff] [blame] | 653 | args = [input_api.python_executable, 'tools/checkperms/checkperms.py', |
| 654 | '--root', input_api.change.RepositoryRoot()] |
[email protected] | fbcafe5a | 2012-08-08 15:31:22 | [diff] [blame] | 655 | for f in input_api.AffectedFiles(): |
| 656 | args += ['--file', f.LocalPath()] |
[email protected] | f0d330f | 2014-01-30 01:44:34 | [diff] [blame] | 657 | checkperms = input_api.subprocess.Popen(args, |
| 658 | stdout=input_api.subprocess.PIPE) |
| 659 | errors = checkperms.communicate()[0].strip() |
[email protected] | fbcafe5a | 2012-08-08 15:31:22 | [diff] [blame] | 660 | if errors: |
[email protected] | f0d330f | 2014-01-30 01:44:34 | [diff] [blame] | 661 | return [output_api.PresubmitError('checkperms.py failed.', |
| 662 | errors.splitlines())] |
| 663 | return [] |
[email protected] | fbcafe5a | 2012-08-08 15:31:22 | [diff] [blame] | 664 | |
| 665 | |
[email protected] | c8278b3 | 2012-10-30 20:35:49 | [diff] [blame] | 666 | def _CheckNoAuraWindowPropertyHInHeaders(input_api, output_api): |
| 667 | """Makes sure we don't include ui/aura/window_property.h |
| 668 | in header files. |
| 669 | """ |
| 670 | pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"') |
| 671 | errors = [] |
| 672 | for f in input_api.AffectedFiles(): |
| 673 | if not f.LocalPath().endswith('.h'): |
| 674 | continue |
| 675 | for line_num, line in f.ChangedContents(): |
| 676 | if pattern.match(line): |
| 677 | errors.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 678 | |
| 679 | results = [] |
| 680 | if errors: |
| 681 | results.append(output_api.PresubmitError( |
| 682 | 'Header files should not include ui/aura/window_property.h', errors)) |
| 683 | return results |
| 684 | |
| 685 | |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 686 | def _CheckIncludeOrderForScope(scope, input_api, file_path, changed_linenums): |
| 687 | """Checks that the lines in scope occur in the right order. |
| 688 | |
| 689 | 1. C system files in alphabetical order |
| 690 | 2. C++ system files in alphabetical order |
| 691 | 3. Project's .h files |
| 692 | """ |
| 693 | |
| 694 | c_system_include_pattern = input_api.re.compile(r'\s*#include <.*\.h>') |
| 695 | cpp_system_include_pattern = input_api.re.compile(r'\s*#include <.*>') |
| 696 | custom_include_pattern = input_api.re.compile(r'\s*#include ".*') |
| 697 | |
| 698 | C_SYSTEM_INCLUDES, CPP_SYSTEM_INCLUDES, CUSTOM_INCLUDES = range(3) |
| 699 | |
| 700 | state = C_SYSTEM_INCLUDES |
| 701 | |
| 702 | previous_line = '' |
[email protected] | 728b9bb | 2012-11-14 20:38:57 | [diff] [blame] | 703 | previous_line_num = 0 |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 704 | problem_linenums = [] |
brucedawson | 70fadb0 | 2015-06-30 17:47:55 | [diff] [blame] | 705 | out_of_order = " - line belongs before previous line" |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 706 | for line_num, line in scope: |
| 707 | if c_system_include_pattern.match(line): |
| 708 | if state != C_SYSTEM_INCLUDES: |
brucedawson | 70fadb0 | 2015-06-30 17:47:55 | [diff] [blame] | 709 | problem_linenums.append((line_num, previous_line_num, |
| 710 | " - C system include file in wrong block")) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 711 | elif previous_line and previous_line > line: |
brucedawson | 70fadb0 | 2015-06-30 17:47:55 | [diff] [blame] | 712 | problem_linenums.append((line_num, previous_line_num, |
| 713 | out_of_order)) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 714 | elif cpp_system_include_pattern.match(line): |
| 715 | if state == C_SYSTEM_INCLUDES: |
| 716 | state = CPP_SYSTEM_INCLUDES |
| 717 | elif state == CUSTOM_INCLUDES: |
brucedawson | 70fadb0 | 2015-06-30 17:47:55 | [diff] [blame] | 718 | problem_linenums.append((line_num, previous_line_num, |
| 719 | " - c++ system include file in wrong block")) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 720 | elif previous_line and previous_line > line: |
brucedawson | 70fadb0 | 2015-06-30 17:47:55 | [diff] [blame] | 721 | problem_linenums.append((line_num, previous_line_num, out_of_order)) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 722 | elif custom_include_pattern.match(line): |
| 723 | if state != CUSTOM_INCLUDES: |
| 724 | state = CUSTOM_INCLUDES |
| 725 | elif previous_line and previous_line > line: |
brucedawson | 70fadb0 | 2015-06-30 17:47:55 | [diff] [blame] | 726 | problem_linenums.append((line_num, previous_line_num, out_of_order)) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 727 | else: |
brucedawson | 70fadb0 | 2015-06-30 17:47:55 | [diff] [blame] | 728 | problem_linenums.append((line_num, previous_line_num, |
| 729 | "Unknown include type")) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 730 | previous_line = line |
[email protected] | 728b9bb | 2012-11-14 20:38:57 | [diff] [blame] | 731 | previous_line_num = line_num |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 732 | |
| 733 | warnings = [] |
brucedawson | 70fadb0 | 2015-06-30 17:47:55 | [diff] [blame] | 734 | for (line_num, previous_line_num, failure_type) in problem_linenums: |
[email protected] | 728b9bb | 2012-11-14 20:38:57 | [diff] [blame] | 735 | if line_num in changed_linenums or previous_line_num in changed_linenums: |
brucedawson | 70fadb0 | 2015-06-30 17:47:55 | [diff] [blame] | 736 | warnings.append(' %s:%d:%s' % (file_path, line_num, failure_type)) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 737 | return warnings |
| 738 | |
| 739 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 740 | def _CheckIncludeOrderInFile(input_api, f, changed_linenums): |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 741 | """Checks the #include order for the given file f.""" |
| 742 | |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 743 | system_include_pattern = input_api.re.compile(r'\s*#include \<.*') |
[email protected] | 23093b6 | 2013-09-20 12:16:30 | [diff] [blame] | 744 | # Exclude the following includes from the check: |
| 745 | # 1) #include <.../...>, e.g., <sys/...> includes often need to appear in a |
| 746 | # specific order. |
| 747 | # 2) <atlbase.h>, "build/build_config.h" |
| 748 | excluded_include_pattern = input_api.re.compile( |
| 749 | r'\s*#include (\<.*/.*|\<atlbase\.h\>|"build/build_config.h")') |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 750 | custom_include_pattern = input_api.re.compile(r'\s*#include "(?P<FILE>.*)"') |
[email protected] | 3e83618c | 2013-10-09 22:32:33 | [diff] [blame] | 751 | # Match the final or penultimate token if it is xxxtest so we can ignore it |
| 752 | # when considering the special first include. |
| 753 | test_file_tag_pattern = input_api.re.compile( |
| 754 | r'_[a-z]+test(?=(_[a-zA-Z0-9]+)?\.)') |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 755 | if_pattern = input_api.re.compile( |
| 756 | r'\s*#\s*(if|elif|else|endif|define|undef).*') |
| 757 | # Some files need specialized order of includes; exclude such files from this |
| 758 | # check. |
| 759 | uncheckable_includes_pattern = input_api.re.compile( |
| 760 | r'\s*#include ' |
| 761 | '("ipc/.*macros\.h"|<windows\.h>|".*gl.*autogen.h")\s*') |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 762 | |
| 763 | contents = f.NewContents() |
| 764 | warnings = [] |
| 765 | line_num = 0 |
| 766 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 767 | # Handle the special first include. If the first include file is |
| 768 | # some/path/file.h, the corresponding including file can be some/path/file.cc, |
| 769 | # some/other/path/file.cc, some/path/file_platform.cc, some/path/file-suffix.h |
| 770 | # etc. It's also possible that no special first include exists. |
[email protected] | 3e83618c | 2013-10-09 22:32:33 | [diff] [blame] | 771 | # If the included file is some/path/file_platform.h the including file could |
| 772 | # also be some/path/file_xxxtest_platform.h. |
| 773 | including_file_base_name = test_file_tag_pattern.sub( |
| 774 | '', input_api.os_path.basename(f.LocalPath())) |
| 775 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 776 | for line in contents: |
| 777 | line_num += 1 |
| 778 | if system_include_pattern.match(line): |
| 779 | # No special first include -> process the line again along with normal |
| 780 | # includes. |
| 781 | line_num -= 1 |
| 782 | break |
| 783 | match = custom_include_pattern.match(line) |
| 784 | if match: |
| 785 | match_dict = match.groupdict() |
[email protected] | 3e83618c | 2013-10-09 22:32:33 | [diff] [blame] | 786 | header_basename = test_file_tag_pattern.sub( |
| 787 | '', input_api.os_path.basename(match_dict['FILE'])).replace('.h', '') |
| 788 | |
| 789 | if header_basename not in including_file_base_name: |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 790 | # No special first include -> process the line again along with normal |
| 791 | # includes. |
| 792 | line_num -= 1 |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 793 | break |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 794 | |
| 795 | # Split into scopes: Each region between #if and #endif is its own scope. |
| 796 | scopes = [] |
| 797 | current_scope = [] |
| 798 | for line in contents[line_num:]: |
| 799 | line_num += 1 |
[email protected] | 0e5c185 | 2012-12-18 20:17:11 | [diff] [blame] | 800 | if uncheckable_includes_pattern.match(line): |
[email protected] | 4436c9e | 2014-01-07 23:19:54 | [diff] [blame] | 801 | continue |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 802 | if if_pattern.match(line): |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 803 | scopes.append(current_scope) |
| 804 | current_scope = [] |
[email protected] | 962f117e | 2012-11-22 18:11:56 | [diff] [blame] | 805 | elif ((system_include_pattern.match(line) or |
| 806 | custom_include_pattern.match(line)) and |
| 807 | not excluded_include_pattern.match(line)): |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 808 | current_scope.append((line_num, line)) |
| 809 | scopes.append(current_scope) |
| 810 | |
| 811 | for scope in scopes: |
| 812 | warnings.extend(_CheckIncludeOrderForScope(scope, input_api, f.LocalPath(), |
| 813 | changed_linenums)) |
| 814 | return warnings |
| 815 | |
| 816 | |
| 817 | def _CheckIncludeOrder(input_api, output_api): |
| 818 | """Checks that the #include order is correct. |
| 819 | |
| 820 | 1. The corresponding header for source files. |
| 821 | 2. C system files in alphabetical order |
| 822 | 3. C++ system files in alphabetical order |
| 823 | 4. Project's .h files in alphabetical order |
| 824 | |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 825 | Each region separated by #if, #elif, #else, #endif, #define and #undef follows |
| 826 | these rules separately. |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 827 | """ |
[email protected] | e120b01 | 2014-08-15 19:08:35 | [diff] [blame] | 828 | def FileFilterIncludeOrder(affected_file): |
| 829 | black_list = (_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST) |
| 830 | return input_api.FilterSourceFile(affected_file, black_list=black_list) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 831 | |
| 832 | warnings = [] |
[email protected] | e120b01 | 2014-08-15 19:08:35 | [diff] [blame] | 833 | for f in input_api.AffectedFiles(file_filter=FileFilterIncludeOrder): |
tapted | 574f09c | 2015-05-19 13:08:08 | [diff] [blame] | 834 | if f.LocalPath().endswith(('.cc', '.h', '.mm')): |
[email protected] | ac294a1 | 2012-12-06 16:38:43 | [diff] [blame] | 835 | changed_linenums = set(line_num for line_num, _ in f.ChangedContents()) |
| 836 | warnings.extend(_CheckIncludeOrderInFile(input_api, f, changed_linenums)) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 837 | |
| 838 | results = [] |
| 839 | if warnings: |
[email protected] | f7051d5 | 2013-04-02 18:31:42 | [diff] [blame] | 840 | results.append(output_api.PresubmitPromptOrNotify(_INCLUDE_ORDER_WARNING, |
[email protected] | 120cf540d | 2012-12-10 17:55:53 | [diff] [blame] | 841 | warnings)) |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 842 | return results |
| 843 | |
| 844 | |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 845 | def _CheckForVersionControlConflictsInFile(input_api, f): |
| 846 | pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$') |
| 847 | errors = [] |
| 848 | for line_num, line in f.ChangedContents(): |
dbeam | 95c35a2f | 2015-06-02 01:40:23 | [diff] [blame] | 849 | if f.LocalPath().endswith('.md'): |
| 850 | # First-level headers in markdown look a lot like version control |
| 851 | # conflict markers. http://daringfireball.net/projects/markdown/basics |
| 852 | continue |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 853 | if pattern.match(line): |
| 854 | errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line)) |
| 855 | return errors |
| 856 | |
| 857 | |
| 858 | def _CheckForVersionControlConflicts(input_api, output_api): |
| 859 | """Usually this is not intentional and will cause a compile failure.""" |
| 860 | errors = [] |
| 861 | for f in input_api.AffectedFiles(): |
| 862 | errors.extend(_CheckForVersionControlConflictsInFile(input_api, f)) |
| 863 | |
| 864 | results = [] |
| 865 | if errors: |
| 866 | results.append(output_api.PresubmitError( |
| 867 | 'Version control conflict markers found, please resolve.', errors)) |
| 868 | return results |
| 869 | |
| 870 | |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 871 | def _CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api): |
| 872 | def FilterFile(affected_file): |
| 873 | """Filter function for use with input_api.AffectedSourceFiles, |
| 874 | below. This filters out everything except non-test files from |
| 875 | top-level directories that generally speaking should not hard-code |
| 876 | service URLs (e.g. src/android_webview/, src/content/ and others). |
| 877 | """ |
| 878 | return input_api.FilterSourceFile( |
| 879 | affected_file, |
[email protected] | 78bb39d6 | 2012-12-11 15:11:56 | [diff] [blame] | 880 | white_list=(r'^(android_webview|base|content|net)[\\\/].*', ), |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 881 | black_list=(_EXCLUDED_PATHS + |
| 882 | _TEST_CODE_EXCLUDED_PATHS + |
| 883 | input_api.DEFAULT_BLACK_LIST)) |
| 884 | |
[email protected] | de4f7d2 | 2013-05-23 14:27:46 | [diff] [blame] | 885 | base_pattern = '"[^"]*google\.com[^"]*"' |
| 886 | comment_pattern = input_api.re.compile('//.*%s' % base_pattern) |
| 887 | pattern = input_api.re.compile(base_pattern) |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 888 | problems = [] # items are (filename, line_number, line) |
| 889 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 890 | for line_num, line in f.ChangedContents(): |
[email protected] | de4f7d2 | 2013-05-23 14:27:46 | [diff] [blame] | 891 | if not comment_pattern.search(line) and pattern.search(line): |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 892 | problems.append((f.LocalPath(), line_num, line)) |
| 893 | |
| 894 | if problems: |
[email protected] | f7051d5 | 2013-04-02 18:31:42 | [diff] [blame] | 895 | return [output_api.PresubmitPromptOrNotify( |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 896 | 'Most layers below src/chrome/ should not hardcode service URLs.\n' |
[email protected] | b014977 | 2014-03-27 16:47:58 | [diff] [blame] | 897 | 'Are you sure this is correct?', |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 898 | [' %s:%d: %s' % ( |
| 899 | problem[0], problem[1], problem[2]) for problem in problems])] |
[email protected] | 2fdd1f36 | 2013-01-16 03:56:03 | [diff] [blame] | 900 | else: |
| 901 | return [] |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 902 | |
| 903 | |
[email protected] | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 904 | def _CheckNoAbbreviationInPngFileName(input_api, output_api): |
| 905 | """Makes sure there are no abbreviations in the name of PNG files. |
binji | 0dcdf34 | 2014-12-12 18:32:31 | [diff] [blame] | 906 | The native_client_sdk directory is excluded because it has auto-generated PNG |
| 907 | files for documentation. |
[email protected] | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 908 | """ |
[email protected] | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 909 | errors = [] |
binji | 0dcdf34 | 2014-12-12 18:32:31 | [diff] [blame] | 910 | white_list = (r'.*_[a-z]_.*\.png$|.*_[a-z]\.png$',) |
| 911 | black_list = (r'^native_client_sdk[\\\/]',) |
| 912 | file_filter = lambda f: input_api.FilterSourceFile( |
| 913 | f, white_list=white_list, black_list=black_list) |
| 914 | for f in input_api.AffectedFiles(include_deletes=False, |
| 915 | file_filter=file_filter): |
| 916 | errors.append(' %s' % f.LocalPath()) |
[email protected] | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 917 | |
| 918 | results = [] |
| 919 | if errors: |
| 920 | results.append(output_api.PresubmitError( |
| 921 | 'The name of PNG files should not have abbreviations. \n' |
| 922 | 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n' |
| 923 | 'Contact [email protected] if you have questions.', errors)) |
| 924 | return results |
| 925 | |
| 926 | |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 927 | def _FilesToCheckForIncomingDeps(re, changed_lines): |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 928 | """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 929 | a set of DEPS entries that we should look up. |
| 930 | |
| 931 | For a directory (rather than a specific filename) we fake a path to |
| 932 | a specific filename by adding /DEPS. This is chosen as a file that |
| 933 | will seldom or never be subject to per-file include_rules. |
| 934 | """ |
[email protected] | 2b438d6 | 2013-11-14 17:54:14 | [diff] [blame] | 935 | # We ignore deps entries on auto-generated directories. |
| 936 | AUTO_GENERATED_DIRS = ['grit', 'jni'] |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 937 | |
| 938 | # This pattern grabs the path without basename in the first |
| 939 | # parentheses, and the basename (if present) in the second. It |
| 940 | # relies on the simple heuristic that if there is a basename it will |
| 941 | # be a header file ending in ".h". |
| 942 | pattern = re.compile( |
| 943 | r"""['"]\+([^'"]+?)(/[a-zA-Z0-9_]+\.h)?['"].*""") |
[email protected] | 2b438d6 | 2013-11-14 17:54:14 | [diff] [blame] | 944 | results = set() |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 945 | for changed_line in changed_lines: |
| 946 | m = pattern.match(changed_line) |
| 947 | if m: |
| 948 | path = m.group(1) |
[email protected] | 2b438d6 | 2013-11-14 17:54:14 | [diff] [blame] | 949 | if path.split('/')[0] not in AUTO_GENERATED_DIRS: |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 950 | if m.group(2): |
| 951 | results.add('%s%s' % (path, m.group(2))) |
| 952 | else: |
| 953 | results.add('%s/DEPS' % path) |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 954 | return results |
| 955 | |
| 956 | |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 957 | def _CheckAddedDepsHaveTargetApprovals(input_api, output_api): |
| 958 | """When a dependency prefixed with + is added to a DEPS file, we |
| 959 | want to make sure that the change is reviewed by an OWNER of the |
| 960 | target file or directory, to avoid layering violations from being |
| 961 | introduced. This check verifies that this happens. |
| 962 | """ |
| 963 | changed_lines = set() |
| 964 | for f in input_api.AffectedFiles(): |
| 965 | filename = input_api.os_path.basename(f.LocalPath()) |
| 966 | if filename == 'DEPS': |
| 967 | changed_lines |= set(line.strip() |
| 968 | for line_num, line |
| 969 | in f.ChangedContents()) |
| 970 | if not changed_lines: |
| 971 | return [] |
| 972 | |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 973 | virtual_depended_on_files = _FilesToCheckForIncomingDeps(input_api.re, |
| 974 | changed_lines) |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 975 | if not virtual_depended_on_files: |
| 976 | return [] |
| 977 | |
| 978 | if input_api.is_committing: |
| 979 | if input_api.tbr: |
| 980 | return [output_api.PresubmitNotifyResult( |
| 981 | '--tbr was specified, skipping OWNERS check for DEPS additions')] |
| 982 | if not input_api.change.issue: |
| 983 | return [output_api.PresubmitError( |
| 984 | "DEPS approval by OWNERS check failed: this change has " |
| 985 | "no Rietveld issue number, so we can't check it for approvals.")] |
| 986 | output = output_api.PresubmitError |
| 987 | else: |
| 988 | output = output_api.PresubmitNotifyResult |
| 989 | |
| 990 | owners_db = input_api.owners_db |
| 991 | owner_email, reviewers = input_api.canned_checks._RietveldOwnerAndReviewers( |
| 992 | input_api, |
| 993 | owners_db.email_regexp, |
| 994 | approval_needed=input_api.is_committing) |
| 995 | |
| 996 | owner_email = owner_email or input_api.change.author_email |
| 997 | |
[email protected] | de4f7d2 | 2013-05-23 14:27:46 | [diff] [blame] | 998 | reviewers_plus_owner = set(reviewers) |
[email protected] | e71c608 | 2013-05-22 02:28:51 | [diff] [blame] | 999 | if owner_email: |
[email protected] | de4f7d2 | 2013-05-23 14:27:46 | [diff] [blame] | 1000 | reviewers_plus_owner.add(owner_email) |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 1001 | missing_files = owners_db.files_not_covered_by(virtual_depended_on_files, |
| 1002 | reviewers_plus_owner) |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 1003 | |
| 1004 | # We strip the /DEPS part that was added by |
| 1005 | # _FilesToCheckForIncomingDeps to fake a path to a file in a |
| 1006 | # directory. |
| 1007 | def StripDeps(path): |
| 1008 | start_deps = path.rfind('/DEPS') |
| 1009 | if start_deps != -1: |
| 1010 | return path[:start_deps] |
| 1011 | else: |
| 1012 | return path |
| 1013 | unapproved_dependencies = ["'+%s'," % StripDeps(path) |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 1014 | for path in missing_files] |
| 1015 | |
| 1016 | if unapproved_dependencies: |
| 1017 | output_list = [ |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 1018 | output('Missing LGTM from OWNERS of dependencies added to DEPS:\n %s' % |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 1019 | '\n '.join(sorted(unapproved_dependencies)))] |
| 1020 | if not input_api.is_committing: |
| 1021 | suggested_owners = owners_db.reviewers_for(missing_files, owner_email) |
| 1022 | output_list.append(output( |
| 1023 | 'Suggested missing target path OWNERS:\n %s' % |
| 1024 | '\n '.join(suggested_owners or []))) |
| 1025 | return output_list |
| 1026 | |
| 1027 | return [] |
| 1028 | |
| 1029 | |
[email protected] | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 1030 | def _CheckSpamLogging(input_api, output_api): |
| 1031 | file_inclusion_pattern = r'.+%s' % _IMPLEMENTATION_EXTENSIONS |
| 1032 | black_list = (_EXCLUDED_PATHS + |
| 1033 | _TEST_CODE_EXCLUDED_PATHS + |
| 1034 | input_api.DEFAULT_BLACK_LIST + |
[email protected] | 6f742dd0 | 2013-11-26 23:19:50 | [diff] [blame] | 1035 | (r"^base[\\\/]logging\.h$", |
[email protected] | 80f360a | 2014-01-23 01:36:19 | [diff] [blame] | 1036 | r"^base[\\\/]logging\.cc$", |
[email protected] | 8dc338c | 2013-12-09 16:28:48 | [diff] [blame] | 1037 | r"^chrome[\\\/]app[\\\/]chrome_main_delegate\.cc$", |
[email protected] | 6e268db | 2013-12-04 01:41:46 | [diff] [blame] | 1038 | r"^chrome[\\\/]browser[\\\/]chrome_browser_main\.cc$", |
[email protected] | 4de7526 | 2013-12-18 23:16:12 | [diff] [blame] | 1039 | r"^chrome[\\\/]browser[\\\/]ui[\\\/]startup[\\\/]" |
| 1040 | r"startup_browser_creator\.cc$", |
[email protected] | fe0e6e1 | 2013-12-04 05:52:58 | [diff] [blame] | 1041 | r"^chrome[\\\/]installer[\\\/]setup[\\\/].*", |
[email protected] | 8cf6f84 | 2014-08-08 21:33:16 | [diff] [blame] | 1042 | r"chrome[\\\/]browser[\\\/]diagnostics[\\\/]" + |
[email protected] | f5b9a3f34 | 2014-08-08 22:06:03 | [diff] [blame] | 1043 | r"diagnostics_writer\.cc$", |
[email protected] | 9f13b60 | 2014-08-07 02:59:15 | [diff] [blame] | 1044 | r"^chrome_elf[\\\/]dll_hash[\\\/]dll_hash_main\.cc$", |
| 1045 | r"^chromecast[\\\/]", |
| 1046 | r"^cloud_print[\\\/]", |
jochen | 34415e5 | 2015-07-10 08:34:31 | [diff] [blame^] | 1047 | r"^components[\\\/]html_viewer[\\\/]" |
| 1048 | r"web_test_delegate_impl\.cc$", |
[email protected] | 9056e73 | 2014-01-08 06:25:25 | [diff] [blame] | 1049 | r"^content[\\\/]common[\\\/]gpu[\\\/]client[\\\/]" |
| 1050 | r"gl_helper_benchmark\.cc$", |
thestig | c9e38a2 | 2014-09-13 01:02:11 | [diff] [blame] | 1051 | r"^courgette[\\\/]courgette_tool\.cc$", |
[email protected] | 9f13b60 | 2014-08-07 02:59:15 | [diff] [blame] | 1052 | r"^extensions[\\\/]renderer[\\\/]logging_native_handler\.cc$", |
prashant.n | b0252f6 | 2014-11-08 05:02:11 | [diff] [blame] | 1053 | r"^ipc[\\\/]ipc_logging\.cc$", |
[email protected] | 9c36d92 | 2014-03-24 16:47:52 | [diff] [blame] | 1054 | r"^native_client_sdk[\\\/]", |
[email protected] | cdbdced | 2013-11-27 21:35:50 | [diff] [blame] | 1055 | r"^remoting[\\\/]base[\\\/]logging\.h$", |
[email protected] | 67c96ab | 2013-12-17 02:05:36 | [diff] [blame] | 1056 | r"^remoting[\\\/]host[\\\/].*", |
[email protected] | 8232f8fd | 2013-12-14 00:52:31 | [diff] [blame] | 1057 | r"^sandbox[\\\/]linux[\\\/].*", |
[email protected] | 0b7a21e | 2014-02-11 18:38:13 | [diff] [blame] | 1058 | r"^tools[\\\/]", |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 1059 | r"^ui[\\\/]aura[\\\/]bench[\\\/]bench_main\.cc$", |
vchigrin | 1425149 | 2015-01-12 08:09:02 | [diff] [blame] | 1060 | r"^storage[\\\/]browser[\\\/]fileapi[\\\/]" + |
thestig | 22dfc401 | 2014-09-05 08:29:44 | [diff] [blame] | 1061 | r"dump_file_system.cc$",)) |
[email protected] | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 1062 | source_file_filter = lambda x: input_api.FilterSourceFile( |
| 1063 | x, white_list=(file_inclusion_pattern,), black_list=black_list) |
| 1064 | |
| 1065 | log_info = [] |
| 1066 | printf = [] |
| 1067 | |
| 1068 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 1069 | contents = input_api.ReadFile(f, 'rb') |
mohan.reddy | f21db96 | 2014-10-16 12:26:47 | [diff] [blame] | 1070 | if input_api.re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", contents): |
[email protected] | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 1071 | log_info.append(f.LocalPath()) |
mohan.reddy | f21db96 | 2014-10-16 12:26:47 | [diff] [blame] | 1072 | elif input_api.re.search(r"\bD?LOG_IF\s*\(\s*INFO\s*,", contents): |
[email protected] | 8521065 | 2013-11-28 05:50:13 | [diff] [blame] | 1073 | log_info.append(f.LocalPath()) |
[email protected] | 18b466b | 2013-12-02 22:01:37 | [diff] [blame] | 1074 | |
mohan.reddy | f21db96 | 2014-10-16 12:26:47 | [diff] [blame] | 1075 | if input_api.re.search(r"\bprintf\(", contents): |
[email protected] | 18b466b | 2013-12-02 22:01:37 | [diff] [blame] | 1076 | printf.append(f.LocalPath()) |
mohan.reddy | f21db96 | 2014-10-16 12:26:47 | [diff] [blame] | 1077 | elif input_api.re.search(r"\bfprintf\((stdout|stderr)", contents): |
[email protected] | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 1078 | printf.append(f.LocalPath()) |
| 1079 | |
| 1080 | if log_info: |
| 1081 | return [output_api.PresubmitError( |
| 1082 | 'These files spam the console log with LOG(INFO):', |
| 1083 | items=log_info)] |
| 1084 | if printf: |
| 1085 | return [output_api.PresubmitError( |
| 1086 | 'These files spam the console log with printf/fprintf:', |
| 1087 | items=printf)] |
| 1088 | return [] |
| 1089 | |
| 1090 | |
[email protected] | 49aa76a | 2013-12-04 06:59:16 | [diff] [blame] | 1091 | def _CheckForAnonymousVariables(input_api, output_api): |
| 1092 | """These types are all expected to hold locks while in scope and |
| 1093 | so should never be anonymous (which causes them to be immediately |
| 1094 | destroyed).""" |
| 1095 | they_who_must_be_named = [ |
| 1096 | 'base::AutoLock', |
| 1097 | 'base::AutoReset', |
| 1098 | 'base::AutoUnlock', |
| 1099 | 'SkAutoAlphaRestore', |
| 1100 | 'SkAutoBitmapShaderInstall', |
| 1101 | 'SkAutoBlitterChoose', |
| 1102 | 'SkAutoBounderCommit', |
| 1103 | 'SkAutoCallProc', |
| 1104 | 'SkAutoCanvasRestore', |
| 1105 | 'SkAutoCommentBlock', |
| 1106 | 'SkAutoDescriptor', |
| 1107 | 'SkAutoDisableDirectionCheck', |
| 1108 | 'SkAutoDisableOvalCheck', |
| 1109 | 'SkAutoFree', |
| 1110 | 'SkAutoGlyphCache', |
| 1111 | 'SkAutoHDC', |
| 1112 | 'SkAutoLockColors', |
| 1113 | 'SkAutoLockPixels', |
| 1114 | 'SkAutoMalloc', |
| 1115 | 'SkAutoMaskFreeImage', |
| 1116 | 'SkAutoMutexAcquire', |
| 1117 | 'SkAutoPathBoundsUpdate', |
| 1118 | 'SkAutoPDFRelease', |
| 1119 | 'SkAutoRasterClipValidate', |
| 1120 | 'SkAutoRef', |
| 1121 | 'SkAutoTime', |
| 1122 | 'SkAutoTrace', |
| 1123 | 'SkAutoUnref', |
| 1124 | ] |
| 1125 | anonymous = r'(%s)\s*[({]' % '|'.join(they_who_must_be_named) |
| 1126 | # bad: base::AutoLock(lock.get()); |
| 1127 | # not bad: base::AutoLock lock(lock.get()); |
| 1128 | bad_pattern = input_api.re.compile(anonymous) |
| 1129 | # good: new base::AutoLock(lock.get()) |
| 1130 | good_pattern = input_api.re.compile(r'\bnew\s*' + anonymous) |
| 1131 | errors = [] |
| 1132 | |
| 1133 | for f in input_api.AffectedFiles(): |
| 1134 | if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')): |
| 1135 | continue |
| 1136 | for linenum, line in f.ChangedContents(): |
| 1137 | if bad_pattern.search(line) and not good_pattern.search(line): |
| 1138 | errors.append('%s:%d' % (f.LocalPath(), linenum)) |
| 1139 | |
| 1140 | if errors: |
| 1141 | return [output_api.PresubmitError( |
| 1142 | 'These lines create anonymous variables that need to be named:', |
| 1143 | items=errors)] |
| 1144 | return [] |
| 1145 | |
| 1146 | |
[email protected] | 5fe0f874 | 2013-11-29 01:04:59 | [diff] [blame] | 1147 | def _CheckCygwinShell(input_api, output_api): |
| 1148 | source_file_filter = lambda x: input_api.FilterSourceFile( |
| 1149 | x, white_list=(r'.+\.(gyp|gypi)$',)) |
| 1150 | cygwin_shell = [] |
| 1151 | |
| 1152 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 1153 | for linenum, line in f.ChangedContents(): |
| 1154 | if 'msvs_cygwin_shell' in line: |
| 1155 | cygwin_shell.append(f.LocalPath()) |
| 1156 | break |
| 1157 | |
| 1158 | if cygwin_shell: |
| 1159 | return [output_api.PresubmitError( |
| 1160 | 'These files should not use msvs_cygwin_shell (the default is 0):', |
| 1161 | items=cygwin_shell)] |
| 1162 | return [] |
| 1163 | |
[email protected] | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 1164 | |
[email protected] | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 1165 | def _CheckUserActionUpdate(input_api, output_api): |
| 1166 | """Checks if any new user action has been added.""" |
[email protected] | 2f92dec | 2014-03-07 19:21:52 | [diff] [blame] | 1167 | if any('actions.xml' == input_api.os_path.basename(f) for f in |
[email protected] | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 1168 | input_api.LocalPaths()): |
[email protected] | 2f92dec | 2014-03-07 19:21:52 | [diff] [blame] | 1169 | # If actions.xml is already included in the changelist, the PRESUBMIT |
| 1170 | # for actions.xml will do a more complete presubmit check. |
[email protected] | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 1171 | return [] |
| 1172 | |
[email protected] | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 1173 | file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm')) |
| 1174 | action_re = r'[^a-zA-Z]UserMetricsAction\("([^"]*)' |
[email protected] | 2f92dec | 2014-03-07 19:21:52 | [diff] [blame] | 1175 | current_actions = None |
[email protected] | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 1176 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 1177 | for line_num, line in f.ChangedContents(): |
| 1178 | match = input_api.re.search(action_re, line) |
| 1179 | if match: |
[email protected] | 2f92dec | 2014-03-07 19:21:52 | [diff] [blame] | 1180 | # Loads contents in tools/metrics/actions/actions.xml to memory. It's |
| 1181 | # loaded only once. |
| 1182 | if not current_actions: |
| 1183 | with open('tools/metrics/actions/actions.xml') as actions_f: |
| 1184 | current_actions = actions_f.read() |
| 1185 | # Search for the matched user action name in |current_actions|. |
[email protected] | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 1186 | for action_name in match.groups(): |
[email protected] | 2f92dec | 2014-03-07 19:21:52 | [diff] [blame] | 1187 | action = 'name="{0}"'.format(action_name) |
| 1188 | if action not in current_actions: |
[email protected] | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 1189 | return [output_api.PresubmitPromptWarning( |
| 1190 | 'File %s line %d: %s is missing in ' |
[email protected] | 2f92dec | 2014-03-07 19:21:52 | [diff] [blame] | 1191 | 'tools/metrics/actions/actions.xml. Please run ' |
| 1192 | 'tools/metrics/actions/extract_actions.py to update.' |
[email protected] | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 1193 | % (f.LocalPath(), line_num, action_name))] |
| 1194 | return [] |
| 1195 | |
| 1196 | |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 1197 | def _GetJSONParseError(input_api, filename, eat_comments=True): |
| 1198 | try: |
| 1199 | contents = input_api.ReadFile(filename) |
| 1200 | if eat_comments: |
| 1201 | json_comment_eater = input_api.os_path.join( |
| 1202 | input_api.PresubmitLocalPath(), |
| 1203 | 'tools', 'json_comment_eater', 'json_comment_eater.py') |
| 1204 | process = input_api.subprocess.Popen( |
| 1205 | [input_api.python_executable, json_comment_eater], |
| 1206 | stdin=input_api.subprocess.PIPE, |
| 1207 | stdout=input_api.subprocess.PIPE, |
| 1208 | universal_newlines=True) |
| 1209 | (contents, _) = process.communicate(input=contents) |
| 1210 | |
| 1211 | input_api.json.loads(contents) |
| 1212 | except ValueError as e: |
| 1213 | return e |
| 1214 | return None |
| 1215 | |
| 1216 | |
| 1217 | def _GetIDLParseError(input_api, filename): |
| 1218 | try: |
| 1219 | contents = input_api.ReadFile(filename) |
| 1220 | idl_schema = input_api.os_path.join( |
| 1221 | input_api.PresubmitLocalPath(), |
| 1222 | 'tools', 'json_schema_compiler', 'idl_schema.py') |
| 1223 | process = input_api.subprocess.Popen( |
| 1224 | [input_api.python_executable, idl_schema], |
| 1225 | stdin=input_api.subprocess.PIPE, |
| 1226 | stdout=input_api.subprocess.PIPE, |
| 1227 | stderr=input_api.subprocess.PIPE, |
| 1228 | universal_newlines=True) |
| 1229 | (_, error) = process.communicate(input=contents) |
| 1230 | return error or None |
| 1231 | except ValueError as e: |
| 1232 | return e |
| 1233 | |
| 1234 | |
| 1235 | def _CheckParseErrors(input_api, output_api): |
| 1236 | """Check that IDL and JSON files do not contain syntax errors.""" |
| 1237 | actions = { |
| 1238 | '.idl': _GetIDLParseError, |
| 1239 | '.json': _GetJSONParseError, |
| 1240 | } |
| 1241 | # These paths contain test data and other known invalid JSON files. |
| 1242 | excluded_patterns = [ |
joaodasilva | 718f8767 | 2014-08-30 09:25:49 | [diff] [blame] | 1243 | r'test[\\\/]data[\\\/]', |
| 1244 | r'^components[\\\/]policy[\\\/]resources[\\\/]policy_templates\.json$', |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 1245 | ] |
| 1246 | # Most JSON files are preprocessed and support comments, but these do not. |
| 1247 | json_no_comments_patterns = [ |
joaodasilva | 718f8767 | 2014-08-30 09:25:49 | [diff] [blame] | 1248 | r'^testing[\\\/]', |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 1249 | ] |
| 1250 | # Only run IDL checker on files in these directories. |
| 1251 | idl_included_patterns = [ |
joaodasilva | 718f8767 | 2014-08-30 09:25:49 | [diff] [blame] | 1252 | r'^chrome[\\\/]common[\\\/]extensions[\\\/]api[\\\/]', |
| 1253 | r'^extensions[\\\/]common[\\\/]api[\\\/]', |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 1254 | ] |
| 1255 | |
| 1256 | def get_action(affected_file): |
| 1257 | filename = affected_file.LocalPath() |
| 1258 | return actions.get(input_api.os_path.splitext(filename)[1]) |
| 1259 | |
| 1260 | def MatchesFile(patterns, path): |
| 1261 | for pattern in patterns: |
| 1262 | if input_api.re.search(pattern, path): |
| 1263 | return True |
| 1264 | return False |
| 1265 | |
| 1266 | def FilterFile(affected_file): |
| 1267 | action = get_action(affected_file) |
| 1268 | if not action: |
| 1269 | return False |
| 1270 | path = affected_file.LocalPath() |
| 1271 | |
| 1272 | if MatchesFile(excluded_patterns, path): |
| 1273 | return False |
| 1274 | |
| 1275 | if (action == _GetIDLParseError and |
| 1276 | not MatchesFile(idl_included_patterns, path)): |
| 1277 | return False |
| 1278 | return True |
| 1279 | |
| 1280 | results = [] |
| 1281 | for affected_file in input_api.AffectedFiles( |
| 1282 | file_filter=FilterFile, include_deletes=False): |
| 1283 | action = get_action(affected_file) |
| 1284 | kwargs = {} |
| 1285 | if (action == _GetJSONParseError and |
| 1286 | MatchesFile(json_no_comments_patterns, affected_file.LocalPath())): |
| 1287 | kwargs['eat_comments'] = False |
| 1288 | parse_error = action(input_api, |
| 1289 | affected_file.AbsoluteLocalPath(), |
| 1290 | **kwargs) |
| 1291 | if parse_error: |
| 1292 | results.append(output_api.PresubmitError('%s could not be parsed: %s' % |
| 1293 | (affected_file.LocalPath(), parse_error))) |
| 1294 | return results |
| 1295 | |
| 1296 | |
[email protected] | 760deea | 2013-12-10 19:33:49 | [diff] [blame] | 1297 | def _CheckJavaStyle(input_api, output_api): |
| 1298 | """Runs checkstyle on changed java files and returns errors if any exist.""" |
mohan.reddy | f21db96 | 2014-10-16 12:26:47 | [diff] [blame] | 1299 | import sys |
[email protected] | 760deea | 2013-12-10 19:33:49 | [diff] [blame] | 1300 | original_sys_path = sys.path |
| 1301 | try: |
| 1302 | sys.path = sys.path + [input_api.os_path.join( |
| 1303 | input_api.PresubmitLocalPath(), 'tools', 'android', 'checkstyle')] |
| 1304 | import checkstyle |
| 1305 | finally: |
| 1306 | # Restore sys.path to what it was before. |
| 1307 | sys.path = original_sys_path |
| 1308 | |
| 1309 | return checkstyle.RunCheckstyle( |
davileen | 72d7653 | 2015-01-20 22:30:09 | [diff] [blame] | 1310 | input_api, output_api, 'tools/android/checkstyle/chromium-style-5.0.xml', |
newt | d8b7d30e9 | 2015-01-23 18:10:51 | [diff] [blame] | 1311 | black_list=_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST) |
[email protected] | 760deea | 2013-12-10 19:33:49 | [diff] [blame] | 1312 | |
| 1313 | |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1314 | def _CheckAndroidCrLogUsage(input_api, output_api): |
| 1315 | """Checks that new logs using org.chromium.base.Log: |
| 1316 | - Are using 'TAG' as variable name for the tags (warn) |
| 1317 | - Are using the suggested name format for the tags: "cr.<PackageTag>" (warn) |
| 1318 | - Are using a tag that is shorter than 23 characters (error) |
| 1319 | """ |
| 1320 | cr_log_import_pattern = input_api.re.compile( |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1321 | r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE) |
| 1322 | class_in_base_pattern = input_api.re.compile( |
| 1323 | r'^package org\.chromium\.base;$', input_api.re.MULTILINE) |
| 1324 | has_some_log_import_pattern = input_api.re.compile( |
| 1325 | r'^import .*\.Log;$', input_api.re.MULTILINE) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1326 | # Extract the tag from lines like `Log.d(TAG, "*");` or `Log.d("TAG", "*");` |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1327 | log_call_pattern = input_api.re.compile(r'^\s*Log\.\w\((?P<tag>\"?\w+\"?)\,') |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1328 | log_decl_pattern = input_api.re.compile( |
| 1329 | r'^\s*private static final String TAG = "(?P<name>(.*)")', |
| 1330 | input_api.re.MULTILINE) |
| 1331 | log_name_pattern = input_api.re.compile(r'^cr[.\w]*') |
| 1332 | |
| 1333 | REF_MSG = ('See base/android/java/src/org/chromium/base/README_logging.md ' |
| 1334 | 'or contact [email protected] for more info.') |
| 1335 | sources = lambda x: input_api.FilterSourceFile(x, white_list=(r'.*\.java$',)) |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1336 | |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1337 | tag_decl_errors = [] |
| 1338 | tag_length_errors = [] |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1339 | tag_errors = [] |
| 1340 | util_log_errors = [] |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1341 | |
| 1342 | for f in input_api.AffectedSourceFiles(sources): |
| 1343 | file_content = input_api.ReadFile(f) |
| 1344 | has_modified_logs = False |
| 1345 | |
| 1346 | # Per line checks |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1347 | if (cr_log_import_pattern.search(file_content) or |
| 1348 | (class_in_base_pattern.search(file_content) and |
| 1349 | not has_some_log_import_pattern.search(file_content))): |
| 1350 | # Checks to run for files using cr log |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1351 | for line_num, line in f.ChangedContents(): |
| 1352 | |
| 1353 | # Check if the new line is doing some logging |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1354 | match = log_call_pattern.search(line) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1355 | if match: |
| 1356 | has_modified_logs = True |
| 1357 | |
| 1358 | # Make sure it uses "TAG" |
| 1359 | if not match.group('tag') == 'TAG': |
| 1360 | tag_errors.append("%s:%d" % (f.LocalPath(), line_num)) |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1361 | else: |
| 1362 | # Report non cr Log function calls in changed lines |
| 1363 | for line_num, line in f.ChangedContents(): |
| 1364 | if log_call_pattern.search(line): |
| 1365 | util_log_errors.append("%s:%d" % (f.LocalPath(), line_num)) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1366 | |
| 1367 | # Per file checks |
| 1368 | if has_modified_logs: |
| 1369 | # Make sure the tag is using the "cr" prefix and is not too long |
| 1370 | match = log_decl_pattern.search(file_content) |
| 1371 | tag_name = match.group('name') if match else '' |
| 1372 | if not log_name_pattern.search(tag_name ): |
| 1373 | tag_decl_errors.append(f.LocalPath()) |
| 1374 | if len(tag_name) > 23: |
| 1375 | tag_length_errors.append(f.LocalPath()) |
| 1376 | |
| 1377 | results = [] |
| 1378 | if tag_decl_errors: |
| 1379 | results.append(output_api.PresubmitPromptWarning( |
| 1380 | 'Please define your tags using the suggested format: .\n' |
| 1381 | '"private static final String TAG = "cr.<package tag>".\n' + REF_MSG, |
| 1382 | tag_decl_errors)) |
| 1383 | |
| 1384 | if tag_length_errors: |
| 1385 | results.append(output_api.PresubmitError( |
| 1386 | 'The tag length is restricted by the system to be at most ' |
| 1387 | '23 characters.\n' + REF_MSG, |
| 1388 | tag_length_errors)) |
| 1389 | |
| 1390 | if tag_errors: |
| 1391 | results.append(output_api.PresubmitPromptWarning( |
| 1392 | 'Please use a variable named "TAG" for your log tags.\n' + REF_MSG, |
| 1393 | tag_errors)) |
| 1394 | |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1395 | if util_log_errors: |
dgn | 4401aa5 | 2015-04-29 16:26:17 | [diff] [blame] | 1396 | results.append(output_api.PresubmitPromptWarning( |
dgn | 87d9fb6 | 2015-06-12 09:15:12 | [diff] [blame] | 1397 | 'Please use org.chromium.base.Log for new logs.\n' + REF_MSG, |
| 1398 | util_log_errors)) |
| 1399 | |
dgn | 4401aa5 | 2015-04-29 16:26:17 | [diff] [blame] | 1400 | return results |
| 1401 | |
| 1402 | |
mnaganov | 9b9b1fe8 | 2014-12-11 16:30:36 | [diff] [blame] | 1403 | def _CheckForCopyrightedCode(input_api, output_api): |
| 1404 | """Verifies that newly added code doesn't contain copyrighted material |
| 1405 | and is properly licensed under the standard Chromium license. |
| 1406 | |
| 1407 | As there can be false positives, we maintain a whitelist file. This check |
| 1408 | also verifies that the whitelist file is up to date. |
| 1409 | """ |
| 1410 | import sys |
| 1411 | original_sys_path = sys.path |
| 1412 | try: |
| 1413 | sys.path = sys.path + [input_api.os_path.join( |
mnaganov | f771be4a | 2015-06-12 18:13:22 | [diff] [blame] | 1414 | input_api.PresubmitLocalPath(), 'tools')] |
| 1415 | from copyright_scanner import copyright_scanner |
mnaganov | 9b9b1fe8 | 2014-12-11 16:30:36 | [diff] [blame] | 1416 | finally: |
| 1417 | # Restore sys.path to what it was before. |
| 1418 | sys.path = original_sys_path |
| 1419 | |
| 1420 | return copyright_scanner.ScanAtPresubmit(input_api, output_api) |
| 1421 | |
| 1422 | |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 1423 | def _CheckSingletonInHeaders(input_api, output_api): |
| 1424 | """Checks to make sure no header files have |Singleton<|.""" |
| 1425 | def FileFilter(affected_file): |
| 1426 | # It's ok for base/memory/singleton.h to have |Singleton<|. |
| 1427 | black_list = (_EXCLUDED_PATHS + |
| 1428 | input_api.DEFAULT_BLACK_LIST + |
| 1429 | (r"^base[\\\/]memory[\\\/]singleton\.h$",)) |
| 1430 | return input_api.FilterSourceFile(affected_file, black_list=black_list) |
| 1431 | |
| 1432 | pattern = input_api.re.compile(r'(?<!class\s)Singleton\s*<') |
| 1433 | files = [] |
| 1434 | for f in input_api.AffectedSourceFiles(FileFilter): |
| 1435 | if (f.LocalPath().endswith('.h') or f.LocalPath().endswith('.hxx') or |
| 1436 | f.LocalPath().endswith('.hpp') or f.LocalPath().endswith('.inl')): |
| 1437 | contents = input_api.ReadFile(f) |
| 1438 | for line in contents.splitlines(False): |
| 1439 | if (not input_api.re.match(r'//', line) and # Strip C++ comment. |
| 1440 | pattern.search(line)): |
| 1441 | files.append(f) |
| 1442 | break |
| 1443 | |
| 1444 | if files: |
| 1445 | return [ output_api.PresubmitError( |
| 1446 | 'Found Singleton<T> in the following header files.\n' + |
| 1447 | 'Please move them to an appropriate source file so that the ' + |
| 1448 | 'template gets instantiated in a single compilation unit.', |
| 1449 | files) ] |
| 1450 | return [] |
| 1451 | |
| 1452 | |
[email protected] | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 1453 | _DEPRECATED_CSS = [ |
| 1454 | # Values |
| 1455 | ( "-webkit-box", "flex" ), |
| 1456 | ( "-webkit-inline-box", "inline-flex" ), |
| 1457 | ( "-webkit-flex", "flex" ), |
| 1458 | ( "-webkit-inline-flex", "inline-flex" ), |
| 1459 | ( "-webkit-min-content", "min-content" ), |
| 1460 | ( "-webkit-max-content", "max-content" ), |
| 1461 | |
| 1462 | # Properties |
| 1463 | ( "-webkit-background-clip", "background-clip" ), |
| 1464 | ( "-webkit-background-origin", "background-origin" ), |
| 1465 | ( "-webkit-background-size", "background-size" ), |
| 1466 | ( "-webkit-box-shadow", "box-shadow" ), |
| 1467 | |
| 1468 | # Functions |
| 1469 | ( "-webkit-gradient", "gradient" ), |
| 1470 | ( "-webkit-repeating-gradient", "repeating-gradient" ), |
| 1471 | ( "-webkit-linear-gradient", "linear-gradient" ), |
| 1472 | ( "-webkit-repeating-linear-gradient", "repeating-linear-gradient" ), |
| 1473 | ( "-webkit-radial-gradient", "radial-gradient" ), |
| 1474 | ( "-webkit-repeating-radial-gradient", "repeating-radial-gradient" ), |
| 1475 | ] |
| 1476 | |
| 1477 | def _CheckNoDeprecatedCSS(input_api, output_api): |
| 1478 | """ Make sure that we don't use deprecated CSS |
[email protected] | 9a48e3f8 | 2014-05-22 00:06:25 | [diff] [blame] | 1479 | properties, functions or values. Our external |
mdjones | ae0286c3 | 2015-06-10 18:10:34 | [diff] [blame] | 1480 | documentation and iOS CSS for dom distiller |
| 1481 | (reader mode) are ignored by the hooks as it |
[email protected] | 9a48e3f8 | 2014-05-22 00:06:25 | [diff] [blame] | 1482 | needs to be consumed by WebKit. """ |
[email protected] | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 1483 | results = [] |
dbeam | 070cfe6 | 2014-10-22 06:44:02 | [diff] [blame] | 1484 | file_inclusion_pattern = (r".+\.css$",) |
[email protected] | 9a48e3f8 | 2014-05-22 00:06:25 | [diff] [blame] | 1485 | black_list = (_EXCLUDED_PATHS + |
| 1486 | _TEST_CODE_EXCLUDED_PATHS + |
| 1487 | input_api.DEFAULT_BLACK_LIST + |
| 1488 | (r"^chrome/common/extensions/docs", |
| 1489 | r"^chrome/docs", |
mdjones | ae0286c3 | 2015-06-10 18:10:34 | [diff] [blame] | 1490 | r"^components/dom_distiller/core/css/distilledpage_ios.css", |
[email protected] | 9a48e3f8 | 2014-05-22 00:06:25 | [diff] [blame] | 1491 | r"^native_client_sdk")) |
| 1492 | file_filter = lambda f: input_api.FilterSourceFile( |
| 1493 | f, white_list=file_inclusion_pattern, black_list=black_list) |
[email protected] | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 1494 | for fpath in input_api.AffectedFiles(file_filter=file_filter): |
| 1495 | for line_num, line in fpath.ChangedContents(): |
| 1496 | for (deprecated_value, value) in _DEPRECATED_CSS: |
dbeam | 070cfe6 | 2014-10-22 06:44:02 | [diff] [blame] | 1497 | if deprecated_value in line: |
[email protected] | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 1498 | results.append(output_api.PresubmitError( |
| 1499 | "%s:%d: Use of deprecated CSS %s, use %s instead" % |
| 1500 | (fpath.LocalPath(), line_num, deprecated_value, value))) |
| 1501 | return results |
| 1502 | |
mohan.reddy | f21db96 | 2014-10-16 12:26:47 | [diff] [blame] | 1503 | |
dbeam | 070cfe6 | 2014-10-22 06:44:02 | [diff] [blame] | 1504 | _DEPRECATED_JS = [ |
| 1505 | ( "__lookupGetter__", "Object.getOwnPropertyDescriptor" ), |
| 1506 | ( "__defineGetter__", "Object.defineProperty" ), |
| 1507 | ( "__defineSetter__", "Object.defineProperty" ), |
| 1508 | ] |
| 1509 | |
| 1510 | def _CheckNoDeprecatedJS(input_api, output_api): |
| 1511 | """Make sure that we don't use deprecated JS in Chrome code.""" |
| 1512 | results = [] |
| 1513 | file_inclusion_pattern = (r".+\.js$",) # TODO(dbeam): .html? |
| 1514 | black_list = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 1515 | input_api.DEFAULT_BLACK_LIST) |
| 1516 | file_filter = lambda f: input_api.FilterSourceFile( |
| 1517 | f, white_list=file_inclusion_pattern, black_list=black_list) |
| 1518 | for fpath in input_api.AffectedFiles(file_filter=file_filter): |
| 1519 | for lnum, line in fpath.ChangedContents(): |
| 1520 | for (deprecated, replacement) in _DEPRECATED_JS: |
| 1521 | if deprecated in line: |
| 1522 | results.append(output_api.PresubmitError( |
| 1523 | "%s:%d: Use of deprecated JS %s, use %s instead" % |
| 1524 | (fpath.LocalPath(), lnum, deprecated, replacement))) |
| 1525 | return results |
| 1526 | |
| 1527 | |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1528 | def _AndroidSpecificOnUploadChecks(input_api, output_api): |
| 1529 | """Groups checks that target android code.""" |
| 1530 | results = [] |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1531 | results.extend(_CheckAndroidCrLogUsage(input_api, output_api)) |
| 1532 | return results |
| 1533 | |
| 1534 | |
[email protected] | 22c9bd7 | 2011-03-27 16:47:39 | [diff] [blame] | 1535 | def _CommonChecks(input_api, output_api): |
| 1536 | """Checks common to both upload and commit.""" |
| 1537 | results = [] |
| 1538 | results.extend(input_api.canned_checks.PanProjectChecks( |
[email protected] | 3de922f | 2013-12-20 13:27:38 | [diff] [blame] | 1539 | input_api, output_api, |
| 1540 | excluded_paths=_EXCLUDED_PATHS + _TESTRUNNER_PATHS)) |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 1541 | results.extend(_CheckAuthorizedAuthor(input_api, output_api)) |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 1542 | results.extend( |
[email protected] | 760deea | 2013-12-10 19:33:49 | [diff] [blame] | 1543 | _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api)) |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 1544 | results.extend(_CheckNoIOStreamInHeaders(input_api, output_api)) |
[email protected] | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 1545 | results.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, output_api)) |
[email protected] | 8ea5d4b | 2011-09-13 21:49:22 | [diff] [blame] | 1546 | results.extend(_CheckNoNewWStrings(input_api, output_api)) |
[email protected] | 2a8ac9c | 2011-10-19 17:20:44 | [diff] [blame] | 1547 | results.extend(_CheckNoDEPSGIT(input_api, output_api)) |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 1548 | results.extend(_CheckNoBannedFunctions(input_api, output_api)) |
[email protected] | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 1549 | results.extend(_CheckNoPragmaOnce(input_api, output_api)) |
[email protected] | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 1550 | results.extend(_CheckNoTrinaryTrueFalse(input_api, output_api)) |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 1551 | results.extend(_CheckUnwantedDependencies(input_api, output_api)) |
[email protected] | fbcafe5a | 2012-08-08 15:31:22 | [diff] [blame] | 1552 | results.extend(_CheckFilePermissions(input_api, output_api)) |
[email protected] | c8278b3 | 2012-10-30 20:35:49 | [diff] [blame] | 1553 | results.extend(_CheckNoAuraWindowPropertyHInHeaders(input_api, output_api)) |
[email protected] | 2309b0fa0 | 2012-11-16 12:18:27 | [diff] [blame] | 1554 | results.extend(_CheckIncludeOrder(input_api, output_api)) |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 1555 | results.extend(_CheckForVersionControlConflicts(input_api, output_api)) |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 1556 | results.extend(_CheckPatchFiles(input_api, output_api)) |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 1557 | results.extend(_CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api)) |
[email protected] | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 1558 | results.extend(_CheckNoAbbreviationInPngFileName(input_api, output_api)) |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 1559 | results.extend(_CheckForInvalidOSMacros(input_api, output_api)) |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 1560 | results.extend(_CheckForInvalidIfDefinedMacros(input_api, output_api)) |
danakj | 3c84d0c | 2014-10-06 15:35:46 | [diff] [blame] | 1561 | # TODO(danakj): Remove this when base/move.h is removed. |
| 1562 | results.extend(_CheckForUsingSideEffectsOfPass(input_api, output_api)) |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 1563 | results.extend(_CheckAddedDepsHaveTargetApprovals(input_api, output_api)) |
[email protected] | 9f919cc | 2013-07-31 03:04:04 | [diff] [blame] | 1564 | results.extend( |
| 1565 | input_api.canned_checks.CheckChangeHasNoTabs( |
| 1566 | input_api, |
| 1567 | output_api, |
| 1568 | source_file_filter=lambda x: x.LocalPath().endswith('.grd'))) |
[email protected] | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 1569 | results.extend(_CheckSpamLogging(input_api, output_api)) |
[email protected] | 49aa76a | 2013-12-04 06:59:16 | [diff] [blame] | 1570 | results.extend(_CheckForAnonymousVariables(input_api, output_api)) |
[email protected] | 5fe0f874 | 2013-11-29 01:04:59 | [diff] [blame] | 1571 | results.extend(_CheckCygwinShell(input_api, output_api)) |
[email protected] | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 1572 | results.extend(_CheckUserActionUpdate(input_api, output_api)) |
[email protected] | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 1573 | results.extend(_CheckNoDeprecatedCSS(input_api, output_api)) |
dbeam | 070cfe6 | 2014-10-22 06:44:02 | [diff] [blame] | 1574 | results.extend(_CheckNoDeprecatedJS(input_api, output_api)) |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 1575 | results.extend(_CheckParseErrors(input_api, output_api)) |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 1576 | results.extend(_CheckForIPCRules(input_api, output_api)) |
mnaganov | 9b9b1fe8 | 2014-12-11 16:30:36 | [diff] [blame] | 1577 | results.extend(_CheckForCopyrightedCode(input_api, output_api)) |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 1578 | results.extend(_CheckForWindowsLineEndings(input_api, output_api)) |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 1579 | results.extend(_CheckSingletonInHeaders(input_api, output_api)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 1580 | |
| 1581 | if any('PRESUBMIT.py' == f.LocalPath() for f in input_api.AffectedFiles()): |
| 1582 | results.extend(input_api.canned_checks.RunUnitTestsInDirectory( |
| 1583 | input_api, output_api, |
| 1584 | input_api.PresubmitLocalPath(), |
[email protected] | 6be6338 | 2013-01-21 15:42:38 | [diff] [blame] | 1585 | whitelist=[r'^PRESUBMIT_test\.py$'])) |
[email protected] | 22c9bd7 | 2011-03-27 16:47:39 | [diff] [blame] | 1586 | return results |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 1587 | |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 1588 | |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 1589 | def _CheckAuthorizedAuthor(input_api, output_api): |
| 1590 | """For non-googler/chromites committers, verify the author's email address is |
| 1591 | in AUTHORS. |
| 1592 | """ |
[email protected] | 9bb9cb8 | 2011-06-13 20:43:01 | [diff] [blame] | 1593 | # TODO(maruel): Add it to input_api? |
| 1594 | import fnmatch |
| 1595 | |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 1596 | author = input_api.change.author_email |
[email protected] | 9bb9cb8 | 2011-06-13 20:43:01 | [diff] [blame] | 1597 | if not author: |
| 1598 | input_api.logging.info('No author, skipping AUTHOR check') |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 1599 | return [] |
[email protected] | c9966329 | 2011-05-31 19:46:08 | [diff] [blame] | 1600 | authors_path = input_api.os_path.join( |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 1601 | input_api.PresubmitLocalPath(), 'AUTHORS') |
| 1602 | valid_authors = ( |
| 1603 | input_api.re.match(r'[^#]+\s+\<(.+?)\>\s*$', line) |
| 1604 | for line in open(authors_path)) |
[email protected] | ac54b13 | 2011-06-06 18:11:18 | [diff] [blame] | 1605 | valid_authors = [item.group(1).lower() for item in valid_authors if item] |
[email protected] | d8b50be | 2011-06-15 14:19:44 | [diff] [blame] | 1606 | if not any(fnmatch.fnmatch(author.lower(), valid) for valid in valid_authors): |
[email protected] | 5861efb | 2013-01-07 18:33:23 | [diff] [blame] | 1607 | input_api.logging.info('Valid authors are %s', ', '.join(valid_authors)) |
[email protected] | 66daa70 | 2011-05-28 14:41:46 | [diff] [blame] | 1608 | return [output_api.PresubmitPromptWarning( |
| 1609 | ('%s is not in AUTHORS file. If you are a new contributor, please visit' |
| 1610 | '\n' |
| 1611 | 'http://www.chromium.org/developers/contributing-code and read the ' |
| 1612 | '"Legal" section\n' |
| 1613 | 'If you are a chromite, verify the contributor signed the CLA.') % |
| 1614 | author)] |
| 1615 | return [] |
| 1616 | |
| 1617 | |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 1618 | def _CheckPatchFiles(input_api, output_api): |
| 1619 | problems = [f.LocalPath() for f in input_api.AffectedFiles() |
| 1620 | if f.LocalPath().endswith(('.orig', '.rej'))] |
| 1621 | if problems: |
| 1622 | return [output_api.PresubmitError( |
| 1623 | "Don't commit .rej and .orig files.", problems)] |
[email protected] | 2fdd1f36 | 2013-01-16 03:56:03 | [diff] [blame] | 1624 | else: |
| 1625 | return [] |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 1626 | |
| 1627 | |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 1628 | def _DidYouMeanOSMacro(bad_macro): |
| 1629 | try: |
| 1630 | return {'A': 'OS_ANDROID', |
| 1631 | 'B': 'OS_BSD', |
| 1632 | 'C': 'OS_CHROMEOS', |
| 1633 | 'F': 'OS_FREEBSD', |
| 1634 | 'L': 'OS_LINUX', |
| 1635 | 'M': 'OS_MACOSX', |
| 1636 | 'N': 'OS_NACL', |
| 1637 | 'O': 'OS_OPENBSD', |
| 1638 | 'P': 'OS_POSIX', |
| 1639 | 'S': 'OS_SOLARIS', |
| 1640 | 'W': 'OS_WIN'}[bad_macro[3].upper()] |
| 1641 | except KeyError: |
| 1642 | return '' |
| 1643 | |
| 1644 | |
| 1645 | def _CheckForInvalidOSMacrosInFile(input_api, f): |
| 1646 | """Check for sensible looking, totally invalid OS macros.""" |
| 1647 | preprocessor_statement = input_api.re.compile(r'^\s*#') |
| 1648 | os_macro = input_api.re.compile(r'defined\((OS_[^)]+)\)') |
| 1649 | results = [] |
| 1650 | for lnum, line in f.ChangedContents(): |
| 1651 | if preprocessor_statement.search(line): |
| 1652 | for match in os_macro.finditer(line): |
| 1653 | if not match.group(1) in _VALID_OS_MACROS: |
| 1654 | good = _DidYouMeanOSMacro(match.group(1)) |
| 1655 | did_you_mean = ' (did you mean %s?)' % good if good else '' |
| 1656 | results.append(' %s:%d %s%s' % (f.LocalPath(), |
| 1657 | lnum, |
| 1658 | match.group(1), |
| 1659 | did_you_mean)) |
| 1660 | return results |
| 1661 | |
| 1662 | |
| 1663 | def _CheckForInvalidOSMacros(input_api, output_api): |
| 1664 | """Check all affected files for invalid OS macros.""" |
| 1665 | bad_macros = [] |
| 1666 | for f in input_api.AffectedFiles(): |
| 1667 | if not f.LocalPath().endswith(('.py', '.js', '.html', '.css')): |
| 1668 | bad_macros.extend(_CheckForInvalidOSMacrosInFile(input_api, f)) |
| 1669 | |
| 1670 | if not bad_macros: |
| 1671 | return [] |
| 1672 | |
| 1673 | return [output_api.PresubmitError( |
| 1674 | 'Possibly invalid OS macro[s] found. Please fix your code\n' |
| 1675 | 'or add your macro to src/PRESUBMIT.py.', bad_macros)] |
| 1676 | |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 1677 | |
| 1678 | def _CheckForInvalidIfDefinedMacrosInFile(input_api, f): |
| 1679 | """Check all affected files for invalid "if defined" macros.""" |
| 1680 | ALWAYS_DEFINED_MACROS = ( |
| 1681 | "TARGET_CPU_PPC", |
| 1682 | "TARGET_CPU_PPC64", |
| 1683 | "TARGET_CPU_68K", |
| 1684 | "TARGET_CPU_X86", |
| 1685 | "TARGET_CPU_ARM", |
| 1686 | "TARGET_CPU_MIPS", |
| 1687 | "TARGET_CPU_SPARC", |
| 1688 | "TARGET_CPU_ALPHA", |
| 1689 | "TARGET_IPHONE_SIMULATOR", |
| 1690 | "TARGET_OS_EMBEDDED", |
| 1691 | "TARGET_OS_IPHONE", |
| 1692 | "TARGET_OS_MAC", |
| 1693 | "TARGET_OS_UNIX", |
| 1694 | "TARGET_OS_WIN32", |
| 1695 | ) |
| 1696 | ifdef_macro = input_api.re.compile(r'^\s*#.*(?:ifdef\s|defined\()([^\s\)]+)') |
| 1697 | results = [] |
| 1698 | for lnum, line in f.ChangedContents(): |
| 1699 | for match in ifdef_macro.finditer(line): |
| 1700 | if match.group(1) in ALWAYS_DEFINED_MACROS: |
| 1701 | always_defined = ' %s is always defined. ' % match.group(1) |
| 1702 | did_you_mean = 'Did you mean \'#if %s\'?' % match.group(1) |
| 1703 | results.append(' %s:%d %s\n\t%s' % (f.LocalPath(), |
| 1704 | lnum, |
| 1705 | always_defined, |
| 1706 | did_you_mean)) |
| 1707 | return results |
| 1708 | |
| 1709 | |
| 1710 | def _CheckForInvalidIfDefinedMacros(input_api, output_api): |
| 1711 | """Check all affected files for invalid "if defined" macros.""" |
| 1712 | bad_macros = [] |
| 1713 | for f in input_api.AffectedFiles(): |
| 1714 | if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')): |
| 1715 | bad_macros.extend(_CheckForInvalidIfDefinedMacrosInFile(input_api, f)) |
| 1716 | |
| 1717 | if not bad_macros: |
| 1718 | return [] |
| 1719 | |
| 1720 | return [output_api.PresubmitError( |
| 1721 | 'Found ifdef check on always-defined macro[s]. Please fix your code\n' |
| 1722 | 'or check the list of ALWAYS_DEFINED_MACROS in src/PRESUBMIT.py.', |
| 1723 | bad_macros)] |
| 1724 | |
| 1725 | |
danakj | 3c84d0c | 2014-10-06 15:35:46 | [diff] [blame] | 1726 | def _CheckForUsingSideEffectsOfPass(input_api, output_api): |
| 1727 | """Check all affected files for using side effects of Pass.""" |
| 1728 | errors = [] |
| 1729 | for f in input_api.AffectedFiles(): |
| 1730 | if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')): |
| 1731 | for lnum, line in f.ChangedContents(): |
| 1732 | # Disallow Foo(*my_scoped_thing.Pass()); See crbug.com/418297. |
mohan.reddy | f21db96 | 2014-10-16 12:26:47 | [diff] [blame] | 1733 | if input_api.re.search(r'\*[a-zA-Z0-9_]+\.Pass\(\)', line): |
danakj | 3c84d0c | 2014-10-06 15:35:46 | [diff] [blame] | 1734 | errors.append(output_api.PresubmitError( |
| 1735 | ('%s:%d uses *foo.Pass() to delete the contents of scoped_ptr. ' + |
| 1736 | 'See crbug.com/418297.') % (f.LocalPath(), lnum))) |
| 1737 | return errors |
| 1738 | |
| 1739 | |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 1740 | def _CheckForIPCRules(input_api, output_api): |
| 1741 | """Check for same IPC rules described in |
| 1742 | http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc |
| 1743 | """ |
| 1744 | base_pattern = r'IPC_ENUM_TRAITS\(' |
| 1745 | inclusion_pattern = input_api.re.compile(r'(%s)' % base_pattern) |
| 1746 | comment_pattern = input_api.re.compile(r'//.*(%s)' % base_pattern) |
| 1747 | |
| 1748 | problems = [] |
| 1749 | for f in input_api.AffectedSourceFiles(None): |
| 1750 | local_path = f.LocalPath() |
| 1751 | if not local_path.endswith('.h'): |
| 1752 | continue |
| 1753 | for line_number, line in f.ChangedContents(): |
| 1754 | if inclusion_pattern.search(line) and not comment_pattern.search(line): |
| 1755 | problems.append( |
| 1756 | '%s:%d\n %s' % (local_path, line_number, line.strip())) |
| 1757 | |
| 1758 | if problems: |
| 1759 | return [output_api.PresubmitPromptWarning( |
| 1760 | _IPC_ENUM_TRAITS_DEPRECATED, problems)] |
| 1761 | else: |
| 1762 | return [] |
| 1763 | |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 1764 | |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 1765 | def _CheckForWindowsLineEndings(input_api, output_api): |
| 1766 | """Check source code and known ascii text files for Windows style line |
| 1767 | endings. |
| 1768 | """ |
earthdok | 1b5e0ee | 2015-03-10 15:19:10 | [diff] [blame] | 1769 | known_text_files = r'.*\.(txt|html|htm|mhtml|py|gyp|gypi|gn|isolate)$' |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 1770 | |
| 1771 | file_inclusion_pattern = ( |
| 1772 | known_text_files, |
| 1773 | r'.+%s' % _IMPLEMENTATION_EXTENSIONS |
| 1774 | ) |
| 1775 | |
| 1776 | filter = lambda f: input_api.FilterSourceFile( |
| 1777 | f, white_list=file_inclusion_pattern, black_list=None) |
| 1778 | files = [f.LocalPath() for f in |
| 1779 | input_api.AffectedSourceFiles(filter)] |
| 1780 | |
| 1781 | problems = [] |
| 1782 | |
| 1783 | for file in files: |
| 1784 | fp = open(file, 'r') |
| 1785 | for line in fp: |
| 1786 | if line.endswith('\r\n'): |
| 1787 | problems.append(file) |
| 1788 | break |
| 1789 | fp.close() |
| 1790 | |
| 1791 | if problems: |
| 1792 | return [output_api.PresubmitPromptWarning('Are you sure that you want ' |
| 1793 | 'these files to contain Windows style line endings?\n' + |
| 1794 | '\n'.join(problems))] |
| 1795 | |
| 1796 | return [] |
| 1797 | |
| 1798 | |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 1799 | def CheckChangeOnUpload(input_api, output_api): |
| 1800 | results = [] |
| 1801 | results.extend(_CommonChecks(input_api, output_api)) |
tandrii | ef66469 | 2014-09-23 14:51:47 | [diff] [blame] | 1802 | results.extend(_CheckValidHostsInDEPS(input_api, output_api)) |
aurimas | 8d3bc1c5 | 2014-10-15 01:02:17 | [diff] [blame] | 1803 | results.extend(_CheckJavaStyle(input_api, output_api)) |
scottmg | 39b2995 | 2014-12-08 18:31:28 | [diff] [blame] | 1804 | results.extend( |
| 1805 | input_api.canned_checks.CheckGNFormatted(input_api, output_api)) |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 1806 | results.extend(_CheckUmaHistogramChanges(input_api, output_api)) |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 1807 | results.extend(_AndroidSpecificOnUploadChecks(input_api, output_api)) |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 1808 | return results |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 1809 | |
| 1810 | |
[email protected] | 1bfb832 | 2014-04-23 01:02:41 | [diff] [blame] | 1811 | def GetTryServerMasterForBot(bot): |
| 1812 | """Returns the Try Server master for the given bot. |
| 1813 | |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 1814 | It tries to guess the master from the bot name, but may still fail |
| 1815 | and return None. There is no longer a default master. |
| 1816 | """ |
| 1817 | # Potentially ambiguous bot names are listed explicitly. |
| 1818 | master_map = { |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 1819 | 'chromium_presubmit': 'tryserver.chromium.linux', |
| 1820 | 'blink_presubmit': 'tryserver.chromium.linux', |
| 1821 | 'tools_build_presubmit': 'tryserver.chromium.linux', |
[email protected] | 1bfb832 | 2014-04-23 01:02:41 | [diff] [blame] | 1822 | } |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 1823 | master = master_map.get(bot) |
| 1824 | if not master: |
sergiyb | 37fd293f | 2015-02-26 06:55:01 | [diff] [blame] | 1825 | if 'linux' in bot or 'android' in bot or 'presubmit' in bot: |
[email protected] | 0bb11236 | 2014-07-26 04:38:32 | [diff] [blame] | 1826 | master = 'tryserver.chromium.linux' |
| 1827 | elif 'win' in bot: |
| 1828 | master = 'tryserver.chromium.win' |
| 1829 | elif 'mac' in bot or 'ios' in bot: |
| 1830 | master = 'tryserver.chromium.mac' |
| 1831 | return master |
[email protected] | 1bfb832 | 2014-04-23 01:02:41 | [diff] [blame] | 1832 | |
| 1833 | |
Paweł Hajdan, Jr | 5508378 | 2014-12-19 20:32:56 | [diff] [blame] | 1834 | def GetDefaultTryConfigs(bots): |
| 1835 | """Returns a list of ('bot', set(['tests']), filtered by [bots]. |
[email protected] | 38c6a51 | 2013-12-18 23:48:01 | [diff] [blame] | 1836 | """ |
| 1837 | |
Paweł Hajdan, Jr | 5508378 | 2014-12-19 20:32:56 | [diff] [blame] | 1838 | builders_and_tests = dict((bot, set(['defaulttests'])) for bot in bots) |
[email protected] | 1bfb832 | 2014-04-23 01:02:41 | [diff] [blame] | 1839 | |
| 1840 | # Build up the mapping from tryserver master to bot/test. |
| 1841 | out = dict() |
Paweł Hajdan, Jr | 5508378 | 2014-12-19 20:32:56 | [diff] [blame] | 1842 | for bot, tests in builders_and_tests.iteritems(): |
[email protected] | 1bfb832 | 2014-04-23 01:02:41 | [diff] [blame] | 1843 | out.setdefault(GetTryServerMasterForBot(bot), {})[bot] = tests |
| 1844 | return out |
[email protected] | 38c6a51 | 2013-12-18 23:48:01 | [diff] [blame] | 1845 | |
| 1846 | |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 1847 | def CheckChangeOnCommit(input_api, output_api): |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 1848 | results = [] |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 1849 | results.extend(_CommonChecks(input_api, output_api)) |
[email protected] | dd805fe | 2009-10-01 08:11:51 | [diff] [blame] | 1850 | # TODO(thestig) temporarily disabled, doesn't work in third_party/ |
| 1851 | #results.extend(input_api.canned_checks.CheckSvnModifiedDirectories( |
| 1852 | # input_api, output_api, sources)) |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 1853 | # Make sure the tree is 'open'. |
[email protected] | 806e98e | 2010-03-19 17:49:27 | [diff] [blame] | 1854 | results.extend(input_api.canned_checks.CheckTreeIsOpen( |
[email protected] | 7f23815 | 2009-08-12 19:00:34 | [diff] [blame] | 1855 | input_api, |
| 1856 | output_api, |
[email protected] | 2fdd1f36 | 2013-01-16 03:56:03 | [diff] [blame] | 1857 | json_url='http://chromium-status.appspot.com/current?format=json')) |
[email protected] | 806e98e | 2010-03-19 17:49:27 | [diff] [blame] | 1858 | |
[email protected] | 3e4eb11 | 2011-01-18 03:29:54 | [diff] [blame] | 1859 | results.extend(input_api.canned_checks.CheckChangeHasBugField( |
| 1860 | input_api, output_api)) |
[email protected] | c4b4756 | 2011-12-05 23:39:41 | [diff] [blame] | 1861 | results.extend(input_api.canned_checks.CheckChangeHasDescription( |
| 1862 | input_api, output_api)) |
[email protected] | fe5f57c5 | 2009-06-05 14:25:54 | [diff] [blame] | 1863 | return results |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 1864 | |
| 1865 | |
[email protected] | 7468ac52 | 2014-03-12 23:35:57 | [diff] [blame] | 1866 | def GetPreferredTryMasters(project, change): |
Paweł Hajdan, Jr | ef2afd4 | 2015-01-07 15:59:52 | [diff] [blame] | 1867 | import json |
sergiyb | 57a71e3 | 2015-06-03 18:44:00 | [diff] [blame] | 1868 | import os.path |
| 1869 | import platform |
| 1870 | import subprocess |
smut | 3ef206e1 | 2015-03-20 09:30:00 | [diff] [blame] | 1871 | |
sergiyb | 57a71e3 | 2015-06-03 18:44:00 | [diff] [blame] | 1872 | cq_config_path = os.path.join( |
| 1873 | change.RepositoryRoot(), 'infra', 'config', 'cq.cfg') |
| 1874 | # commit_queue.py below is a script in depot_tools directory, which has a |
| 1875 | # 'builders' command to retrieve a list of CQ builders from the CQ config. |
| 1876 | is_win = platform.system() == 'Windows' |
| 1877 | masters = json.loads(subprocess.check_output( |
| 1878 | ['commit_queue', 'builders', cq_config_path], shell=is_win)) |
[email protected] | 911753b | 2012-08-02 12:11:54 | [diff] [blame] | 1879 | |
sergiyb | 6092f74 | 2015-06-16 09:00:52 | [diff] [blame] | 1880 | try_config = {} |
| 1881 | for master in masters: |
| 1882 | try_config.setdefault(master, {}) |
| 1883 | for builder in masters[master]: |
sergiyb | 57a71e3 | 2015-06-03 18:44:00 | [diff] [blame] | 1884 | # Do not trigger presubmit builders, since they're likely to fail |
| 1885 | # (e.g. OWNERS checks before finished code review), and we're |
| 1886 | # running local presubmit anyway. |
sergiyb | 6092f74 | 2015-06-16 09:00:52 | [diff] [blame] | 1887 | if 'presubmit' not in builder: |
| 1888 | try_config[master][builder] = ['defaulttests'] |
Paweł Hajdan, Jr | 4026dbc | 2015-01-14 09:22:32 | [diff] [blame] | 1889 | |
sergiyb | 6092f74 | 2015-06-16 09:00:52 | [diff] [blame] | 1890 | return try_config |