Avi Drissman | 2497659 | 2022-09-12 15:24:31 | [diff] [blame] | 1 | # Copyright 2012 The Chromium Authors |
maruel@chromium.org | ca8d1984 | 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 | |
Daniel Cheng | d8824447 | 2022-05-16 09:08:47 | [diff] [blame] | 7 | See https://www.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. |
maruel@chromium.org | ca8d1984 | 2009-02-19 16:33:12 | [diff] [blame] | 9 | """ |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 10 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 11 | from typing import Callable |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 12 | from typing import Optional |
| 13 | from typing import Sequence |
| 14 | from dataclasses import dataclass |
| 15 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 16 | PRESUBMIT_VERSION = '2.0.0' |
joi@chromium.org | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 17 | |
Dirk Pranke | e3c9c62d | 2021-05-18 18:35:59 | [diff] [blame] | 18 | # This line is 'magic' in that git-cl looks for it to decide whether to |
| 19 | # use Python3 instead of Python2 when running the code in this file. |
| 20 | USE_PYTHON3 = True |
| 21 | |
maruel@chromium.org | 379e7dd | 2010-01-28 17:39:21 | [diff] [blame] | 22 | _EXCLUDED_PATHS = ( |
Bruce Dawson | 7f8566b | 2022-05-06 16:22:18 | [diff] [blame] | 23 | # Generated file |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 24 | (r"chrome/android/webapk/shell_apk/src/org/chromium" |
| 25 | r"/webapk/lib/runtime_library/IWebApkApi.java"), |
Mila Green | e3aa722 | 2021-09-07 16:34:08 | [diff] [blame] | 26 | # File needs to write to stdout to emulate a tool it's replacing. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 27 | r"chrome/updater/mac/keystone/ksadmin.mm", |
Ilya Sherman | e8a7d2d | 2020-07-25 04:33:47 | [diff] [blame] | 28 | # Generated file. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 29 | (r"^components/variations/proto/devtools/" |
Ilya Sherman | c167a96 | 2020-08-18 18:40:26 | [diff] [blame] | 30 | r"client_variations.js"), |
Bruce Dawson | 3bd976c | 2022-05-06 22:47:52 | [diff] [blame] | 31 | # These are video files, not typescript. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 32 | r"^media/test/data/.*.ts", |
| 33 | r"^native_client_sdksrc/build_tools/make_rules.py", |
| 34 | r"^native_client_sdk/src/build_tools/make_simple.py", |
| 35 | r"^native_client_sdk/src/tools/.*.mk", |
| 36 | r"^net/tools/spdyshark/.*", |
| 37 | r"^skia/.*", |
| 38 | r"^third_party/blink/.*", |
| 39 | r"^third_party/breakpad/.*", |
Darwin Huang | d74a9d3 | 2019-07-17 17:58:46 | [diff] [blame] | 40 | # sqlite is an imported third party dependency. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 41 | r"^third_party/sqlite/.*", |
| 42 | r"^v8/.*", |
maruel@chromium.org | 3e4eb11 | 2011-01-18 03:29:54 | [diff] [blame] | 43 | r".*MakeFile$", |
gman@chromium.org | 1084ccc | 2012-03-14 03:22:53 | [diff] [blame] | 44 | r".+_autogen\.h$", |
Yue She | cf138055 | 2022-08-23 20:59:20 | [diff] [blame] | 45 | r".+_pb2(_grpc)?\.py$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 46 | r".+/pnacl_shim\.c$", |
| 47 | r"^gpu/config/.*_list_json\.cc$", |
| 48 | r"tools/md_browser/.*\.css$", |
Kenneth Russell | 077c8d9 | 2017-12-16 02:52:14 | [diff] [blame] | 49 | # Test pages for Maps telemetry tests. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 50 | r"tools/perf/page_sets/maps_perf_test.*", |
ehmaldonado | 78eee2ed | 2017-03-28 13:16:54 | [diff] [blame] | 51 | # Test pages for WebRTC telemetry tests. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 52 | r"tools/perf/page_sets/webrtc_cases.*", |
dpapad | 2efd445 | 2023-04-06 01:43:45 | [diff] [blame] | 53 | # Test file compared with generated output. |
| 54 | r"tools/polymer/tests/html_to_wrapper/.*.html.ts$", |
maruel@chromium.org | 430641764 | 2009-06-11 00:33:40 | [diff] [blame] | 55 | ) |
maruel@chromium.org | ca8d1984 | 2009-02-19 16:33:12 | [diff] [blame] | 56 | |
John Abd-El-Malek | 759fea6 | 2021-03-13 03:41:14 | [diff] [blame] | 57 | _EXCLUDED_SET_NO_PARENT_PATHS = ( |
| 58 | # It's for historical reasons that blink isn't a top level directory, where |
| 59 | # it would be allowed to have "set noparent" to avoid top level owners |
| 60 | # accidentally +1ing changes. |
| 61 | 'third_party/blink/OWNERS', |
| 62 | ) |
| 63 | |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 64 | |
joi@chromium.org | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 65 | # Fragment of a regular expression that matches C++ and Objective-C++ |
| 66 | # implementation files. |
| 67 | _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$' |
| 68 | |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 69 | |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 70 | # Fragment of a regular expression that matches C++ and Objective-C++ |
| 71 | # header files. |
| 72 | _HEADER_EXTENSIONS = r'\.(h|hpp|hxx)$' |
| 73 | |
| 74 | |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 75 | # Paths with sources that don't use //base. |
| 76 | _NON_BASE_DEPENDENT_PATHS = ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 77 | r"^chrome/browser/browser_switcher/bho/", |
| 78 | r"^tools/win/", |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 79 | ) |
| 80 | |
| 81 | |
joi@chromium.org | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 82 | # Regular expression that matches code only used for test binaries |
| 83 | # (best effort). |
| 84 | _TEST_CODE_EXCLUDED_PATHS = ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 85 | r'.*/(fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS, |
joi@chromium.org | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 86 | r'.+_test_(base|support|util)%s' % _IMPLEMENTATION_EXTENSIONS, |
James Cook | 1b4dc13 | 2021-03-09 22:45:13 | [diff] [blame] | 87 | # Test suite files, like: |
| 88 | # foo_browsertest.cc |
| 89 | # bar_unittest_mac.cc (suffix) |
| 90 | # baz_unittests.cc (plural) |
| 91 | r'.+_(api|browser|eg|int|perf|pixel|unit|ui)?test(s)?(_[a-z]+)?%s' % |
danakj@chromium.org | e2d7e6f | 2013-04-23 12:57:12 | [diff] [blame] | 92 | _IMPLEMENTATION_EXTENSIONS, |
Matthew Denton | 63ea1e6 | 2019-03-25 20:39:18 | [diff] [blame] | 93 | r'.+_(fuzz|fuzzer)(_[a-z]+)?%s' % _IMPLEMENTATION_EXTENSIONS, |
Victor Hugo Vianna Silva | c22e020 | 2021-06-09 19:46:21 | [diff] [blame] | 94 | r'.+sync_service_impl_harness%s' % _IMPLEMENTATION_EXTENSIONS, |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 95 | r'.*/(test|tool(s)?)/.*', |
danakj | 89f4708 | 2020-09-02 17:53:43 | [diff] [blame] | 96 | # content_shell is used for running content_browsertests. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 97 | r'content/shell/.*', |
danakj | 89f4708 | 2020-09-02 17:53:43 | [diff] [blame] | 98 | # Web test harness. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 99 | r'content/web_test/.*', |
darin@chromium.org | 7b05498 | 2013-11-27 00:44:47 | [diff] [blame] | 100 | # Non-production example code. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 101 | r'mojo/examples/.*', |
lliabraa@chromium.org | 8176de1 | 2014-06-20 19:07:08 | [diff] [blame] | 102 | # Launcher for running iOS tests on the simulator. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 103 | r'testing/iossim/iossim\.mm$', |
Olivier Robin | bcea0fa | 2019-11-12 08:56:41 | [diff] [blame] | 104 | # EarlGrey app side code for tests. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 105 | r'ios/.*_app_interface\.mm$', |
Allen Bauer | 0678d77 | 2020-05-11 22:25:17 | [diff] [blame] | 106 | # Views Examples code |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 107 | r'ui/views/examples/.*', |
Austin Sullivan | 33da70a | 2020-10-07 15:39:41 | [diff] [blame] | 108 | # Chromium Codelab |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 109 | r'codelabs/*' |
joi@chromium.org | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 110 | ) |
maruel@chromium.org | ca8d1984 | 2009-02-19 16:33:12 | [diff] [blame] | 111 | |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 112 | _THIRD_PARTY_EXCEPT_BLINK = 'third_party/(?!blink/)' |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 113 | |
joi@chromium.org | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 114 | _TEST_ONLY_WARNING = ( |
| 115 | 'You might be calling functions intended only for testing from\n' |
danakj | 5f6e3b8 | 2020-09-10 13:52:55 | [diff] [blame] | 116 | 'production code. If you are doing this from inside another method\n' |
| 117 | 'named as *ForTesting(), then consider exposing things to have tests\n' |
| 118 | 'make that same call directly.\n' |
| 119 | 'If that is not possible, you may put a comment on the same line with\n' |
| 120 | ' // IN-TEST \n' |
| 121 | 'to tell the PRESUBMIT script that the code is inside a *ForTesting()\n' |
| 122 | 'method and can be ignored. Do not do this inside production code.\n' |
| 123 | 'The android-binary-size trybot will block if the method exists in the\n' |
| 124 | 'release apk.') |
joi@chromium.org | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 125 | |
| 126 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 127 | @dataclass |
| 128 | class BanRule: |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 129 | # String pattern. If the pattern begins with a slash, the pattern will be |
| 130 | # treated as a regular expression instead. |
| 131 | pattern: str |
| 132 | # Explanation as a sequence of strings. Each string in the sequence will be |
| 133 | # printed on its own line. |
| 134 | explanation: Sequence[str] |
| 135 | # Whether or not to treat this ban as a fatal error. If unspecified, |
| 136 | # defaults to true. |
| 137 | treat_as_error: Optional[bool] = None |
| 138 | # Paths that should be excluded from the ban check. Each string is a regular |
| 139 | # expression that will be matched against the path of the file being checked |
| 140 | # relative to the root of the source tree. |
| 141 | excluded_paths: Optional[Sequence[str]] = None |
marja@chromium.org | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 142 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 143 | |
Daniel Cheng | 917ce54 | 2022-03-15 20:46:57 | [diff] [blame] | 144 | _BANNED_JAVA_IMPORTS : Sequence[BanRule] = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 145 | BanRule( |
| 146 | 'import java.net.URI;', |
| 147 | ( |
| 148 | 'Use org.chromium.url.GURL instead of java.net.URI, where possible.', |
| 149 | ), |
| 150 | excluded_paths=( |
| 151 | (r'net/android/javatests/src/org/chromium/net/' |
| 152 | 'AndroidProxySelectorTest\.java'), |
| 153 | r'components/cronet/', |
| 154 | r'third_party/robolectric/local/', |
| 155 | ), |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 156 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 157 | BanRule( |
| 158 | 'import android.annotation.TargetApi;', |
| 159 | ( |
| 160 | 'Do not use TargetApi, use @androidx.annotation.RequiresApi instead. ' |
| 161 | 'RequiresApi ensures that any calls are guarded by the appropriate ' |
| 162 | 'SDK_INT check. See https://crbug.com/1116486.', |
| 163 | ), |
| 164 | ), |
| 165 | BanRule( |
Mohamed Heikal | 3d7a94c | 2023-03-28 16:55:24 | [diff] [blame] | 166 | 'import androidx.test.rule.UiThreadTestRule;', |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 167 | ( |
| 168 | 'Do not use UiThreadTestRule, just use ' |
| 169 | '@org.chromium.base.test.UiThreadTest on test methods that should run ' |
| 170 | 'on the UI thread. See https://crbug.com/1111893.', |
| 171 | ), |
| 172 | ), |
| 173 | BanRule( |
Mohamed Heikal | 3d7a94c | 2023-03-28 16:55:24 | [diff] [blame] | 174 | 'import androidx.test.annotation.UiThreadTest;', |
| 175 | ('Do not use androidx.test.annotation.UiThreadTest, use ' |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 176 | 'org.chromium.base.test.UiThreadTest instead. See ' |
| 177 | 'https://crbug.com/1111893.', |
| 178 | ), |
| 179 | ), |
| 180 | BanRule( |
Mohamed Heikal | 3d7a94c | 2023-03-28 16:55:24 | [diff] [blame] | 181 | 'import androidx.test.rule.ActivityTestRule;', |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 182 | ( |
| 183 | 'Do not use ActivityTestRule, use ' |
| 184 | 'org.chromium.base.test.BaseActivityTestRule instead.', |
| 185 | ), |
| 186 | excluded_paths=( |
| 187 | 'components/cronet/', |
| 188 | ), |
| 189 | ), |
Min Qin | bc44383c | 2023-02-22 17:25:26 | [diff] [blame] | 190 | BanRule( |
| 191 | 'import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;', |
| 192 | ( |
| 193 | 'Do not use VectorDrawableCompat, use getResources().getDrawable() to ' |
| 194 | 'avoid extra indirections. Please also add trace event as the call ' |
| 195 | 'might take more than 20 ms to complete.', |
| 196 | ), |
| 197 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 198 | ) |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 199 | |
Daniel Cheng | 917ce54 | 2022-03-15 20:46:57 | [diff] [blame] | 200 | _BANNED_JAVA_FUNCTIONS : Sequence[BanRule] = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 201 | BanRule( |
Eric Stevenson | a9a98097 | 2017-09-23 00:04:41 | [diff] [blame] | 202 | 'StrictMode.allowThreadDiskReads()', |
| 203 | ( |
| 204 | 'Prefer using StrictModeContext.allowDiskReads() to using StrictMode ' |
| 205 | 'directly.', |
| 206 | ), |
| 207 | False, |
| 208 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 209 | BanRule( |
Eric Stevenson | a9a98097 | 2017-09-23 00:04:41 | [diff] [blame] | 210 | 'StrictMode.allowThreadDiskWrites()', |
| 211 | ( |
| 212 | 'Prefer using StrictModeContext.allowDiskWrites() to using StrictMode ' |
| 213 | 'directly.', |
| 214 | ), |
| 215 | False, |
| 216 | ), |
Daniel Cheng | 917ce54 | 2022-03-15 20:46:57 | [diff] [blame] | 217 | BanRule( |
Michael Thiessen | 0f2547e | 2020-07-27 21:55:36 | [diff] [blame] | 218 | '.waitForIdleSync()', |
| 219 | ( |
| 220 | 'Do not use waitForIdleSync as it masks underlying issues. There is ' |
| 221 | 'almost always something else you should wait on instead.', |
| 222 | ), |
| 223 | False, |
| 224 | ), |
Ashley Newson | 09cbd60 | 2022-10-26 11:40:14 | [diff] [blame] | 225 | BanRule( |
Ashley Newson | eb6f5ced | 2022-10-26 14:45:42 | [diff] [blame] | 226 | r'/(?<!\bsuper\.)(?<!\bIntent )\bregisterReceiver\(', |
Ashley Newson | 09cbd60 | 2022-10-26 11:40:14 | [diff] [blame] | 227 | ( |
| 228 | 'Do not call android.content.Context.registerReceiver (or an override) ' |
| 229 | 'directly. Use one of the wrapper methods defined in ' |
| 230 | 'org.chromium.base.ContextUtils, such as ' |
| 231 | 'registerProtectedBroadcastReceiver, ' |
| 232 | 'registerExportedBroadcastReceiver, or ' |
| 233 | 'registerNonExportedBroadcastReceiver. See their documentation for ' |
| 234 | 'which one to use.', |
| 235 | ), |
| 236 | True, |
| 237 | excluded_paths=( |
Ashley Newson | 22bc26d | 2022-11-01 20:30:57 | [diff] [blame] | 238 | r'.*Test[^a-z]', |
| 239 | r'third_party/', |
Ashley Newson | 09cbd60 | 2022-10-26 11:40:14 | [diff] [blame] | 240 | 'base/android/java/src/org/chromium/base/ContextUtils.java', |
Brandon Mousseau | 7e76a9c | 2022-12-08 22:08:38 | [diff] [blame] | 241 | 'chromecast/browser/android/apk/src/org/chromium/chromecast/shell/BroadcastReceiverScope.java', |
Ashley Newson | 09cbd60 | 2022-10-26 11:40:14 | [diff] [blame] | 242 | ), |
| 243 | ), |
Ted Choc | d5b327b1 | 2022-11-05 02:13:22 | [diff] [blame] | 244 | BanRule( |
| 245 | r'/(?:extends|new)\s*(?:android.util.)?Property<[A-Za-z.]+,\s*(?:Integer|Float)>', |
| 246 | ( |
| 247 | 'Do not use Property<..., Integer|Float>, but use FloatProperty or ' |
| 248 | 'IntProperty because it will avoid unnecessary autoboxing of ' |
| 249 | 'primitives.', |
| 250 | ), |
| 251 | ), |
Peilin Wang | bba4a865 | 2022-11-10 16:33:57 | [diff] [blame] | 252 | BanRule( |
| 253 | 'requestLayout()', |
| 254 | ( |
| 255 | 'Layouts can be expensive. Prefer using ViewUtils.requestLayout(), ' |
| 256 | 'which emits a trace event with additional information to help with ' |
| 257 | 'scroll jank investigations. See http://crbug.com/1354176.', |
| 258 | ), |
| 259 | False, |
| 260 | excluded_paths=( |
| 261 | 'ui/android/java/src/org/chromium/ui/base/ViewUtils.java', |
| 262 | ), |
| 263 | ), |
Ted Choc | f40ea915 | 2023-02-14 19:02:39 | [diff] [blame] | 264 | BanRule( |
| 265 | 'Profile.getLastUsedRegularProfile()', |
| 266 | ( |
| 267 | 'Prefer passing in the Profile reference instead of relying on the ' |
| 268 | 'static getLastUsedRegularProfile() call. Only top level entry points ' |
| 269 | '(e.g. Activities) should call this method. Otherwise, the Profile ' |
| 270 | 'should either be passed in explicitly or retreived from an existing ' |
| 271 | 'entity with a reference to the Profile (e.g. WebContents).', |
| 272 | ), |
| 273 | False, |
| 274 | excluded_paths=( |
| 275 | r'.*Test[A-Z]?.*\.java', |
| 276 | ), |
| 277 | ), |
Min Qin | bc44383c | 2023-02-22 17:25:26 | [diff] [blame] | 278 | BanRule( |
| 279 | r'/(ResourcesCompat|getResources\(\))\.getDrawable\(\)', |
| 280 | ( |
| 281 | 'getDrawable() can be expensive. If you have a lot of calls to ' |
| 282 | 'GetDrawable() or your code may introduce janks, please put your calls ' |
| 283 | 'inside a trace().', |
| 284 | ), |
| 285 | False, |
| 286 | excluded_paths=( |
| 287 | r'.*Test[A-Z]?.*\.java', |
| 288 | ), |
| 289 | ), |
Henrique Nakashima | bbf2b26 | 2023-03-10 17:21:39 | [diff] [blame] | 290 | BanRule( |
| 291 | r'/RecordHistogram\.getHistogram(ValueCount|TotalCount|Samples)ForTesting\(', |
| 292 | ( |
| 293 | 'Raw histogram counts are easy to misuse; for example they don\'t reset ' |
| 294 | 'between batched tests. Use HistogramWatcher to check histogram records instead.', |
| 295 | ), |
| 296 | False, |
| 297 | excluded_paths=( |
| 298 | 'base/android/javatests/src/org/chromium/base/metrics/RecordHistogramTest.java', |
| 299 | 'base/test/android/javatests/src/org/chromium/base/test/util/HistogramWatcher.java', |
| 300 | ), |
| 301 | ), |
Eric Stevenson | a9a98097 | 2017-09-23 00:04:41 | [diff] [blame] | 302 | ) |
| 303 | |
Clement Yan | 9b330cb | 2022-11-17 05:25:29 | [diff] [blame] | 304 | _BANNED_JAVASCRIPT_FUNCTIONS : Sequence [BanRule] = ( |
| 305 | BanRule( |
| 306 | r'/\bchrome\.send\b', |
| 307 | ( |
| 308 | 'The use of chrome.send is disallowed in Chrome (context: https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/security/handling-messages-from-web-content.md).', |
| 309 | 'Please use mojo instead for new webuis. https://docs.google.com/document/d/1RF-GSUoveYa37eoyZ9EhwMtaIwoW7Z88pIgNZ9YzQi4/edit#heading=h.gkk22wgk6wff', |
| 310 | ), |
| 311 | True, |
| 312 | ( |
| 313 | r'^(?!ash\/webui).+', |
| 314 | # TODO(crbug.com/1385601): pre-existing violations still need to be |
| 315 | # cleaned up. |
Rebekah Potter | 57aa94df | 2022-12-13 20:30:58 | [diff] [blame] | 316 | 'ash/webui/common/resources/cr.m.js', |
Clement Yan | 9b330cb | 2022-11-17 05:25:29 | [diff] [blame] | 317 | 'ash/webui/common/resources/multidevice_setup/multidevice_setup_browser_proxy.js', |
| 318 | 'ash/webui/common/resources/quick_unlock/lock_screen_constants.js', |
| 319 | 'ash/webui/common/resources/smb_shares/smb_browser_proxy.js', |
| 320 | 'ash/webui/connectivity_diagnostics/resources/connectivity_diagnostics.js', |
| 321 | 'ash/webui/diagnostics_ui/resources/diagnostics_browser_proxy.ts', |
| 322 | 'ash/webui/multidevice_debug/resources/logs.js', |
| 323 | 'ash/webui/multidevice_debug/resources/webui.js', |
| 324 | 'ash/webui/projector_app/resources/annotator/trusted/annotator_browser_proxy.js', |
| 325 | 'ash/webui/projector_app/resources/app/trusted/projector_browser_proxy.js', |
| 326 | 'ash/webui/scanning/resources/scanning_browser_proxy.js', |
| 327 | ), |
| 328 | ), |
| 329 | ) |
| 330 | |
Daniel Cheng | 917ce54 | 2022-03-15 20:46:57 | [diff] [blame] | 331 | _BANNED_OBJC_FUNCTIONS : Sequence[BanRule] = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 332 | BanRule( |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 333 | 'addTrackingRect:', |
avi@chromium.org | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 334 | ( |
| 335 | 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is' |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 336 | 'prohibited. Please use CrTrackingArea instead.', |
| 337 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 338 | ), |
| 339 | False, |
| 340 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 341 | BanRule( |
leng@chromium.org | eaae197 | 2014-04-16 04:17:26 | [diff] [blame] | 342 | r'/NSTrackingArea\W', |
avi@chromium.org | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 343 | ( |
| 344 | 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea', |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 345 | 'instead.', |
| 346 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 347 | ), |
| 348 | False, |
| 349 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 350 | BanRule( |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 351 | 'convertPointFromBase:', |
avi@chromium.org | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 352 | ( |
| 353 | 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.', |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 354 | 'Please use |convertPoint:(point) fromView:nil| instead.', |
| 355 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 356 | ), |
| 357 | True, |
| 358 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 359 | BanRule( |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 360 | 'convertPointToBase:', |
avi@chromium.org | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 361 | ( |
| 362 | 'The use of -[NSView convertPointToBase:] is almost certainly wrong.', |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 363 | 'Please use |convertPoint:(point) toView:nil| instead.', |
| 364 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 365 | ), |
| 366 | True, |
| 367 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 368 | BanRule( |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 369 | 'convertRectFromBase:', |
avi@chromium.org | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 370 | ( |
| 371 | 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.', |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 372 | 'Please use |convertRect:(point) fromView:nil| instead.', |
| 373 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 374 | ), |
| 375 | True, |
| 376 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 377 | BanRule( |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 378 | 'convertRectToBase:', |
avi@chromium.org | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 379 | ( |
| 380 | 'The use of -[NSView convertRectToBase:] is almost certainly wrong.', |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 381 | 'Please use |convertRect:(point) toView:nil| instead.', |
| 382 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 383 | ), |
| 384 | True, |
| 385 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 386 | BanRule( |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 387 | 'convertSizeFromBase:', |
avi@chromium.org | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 388 | ( |
| 389 | 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.', |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 390 | 'Please use |convertSize:(point) fromView:nil| instead.', |
| 391 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 392 | ), |
| 393 | True, |
| 394 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 395 | BanRule( |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 396 | 'convertSizeToBase:', |
avi@chromium.org | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 397 | ( |
| 398 | 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.', |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 399 | 'Please use |convertSize:(point) toView:nil| instead.', |
| 400 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 401 | ), |
| 402 | True, |
| 403 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 404 | BanRule( |
jif | 6539870 | 2016-10-27 10:19:48 | [diff] [blame] | 405 | r"/\s+UTF8String\s*]", |
| 406 | ( |
| 407 | 'The use of -[NSString UTF8String] is dangerous as it can return null', |
| 408 | 'even if |canBeConvertedToEncoding:NSUTF8StringEncoding| returns YES.', |
| 409 | 'Please use |SysNSStringToUTF8| instead.', |
| 410 | ), |
| 411 | True, |
| 412 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 413 | BanRule( |
Sylvain Defresne | 4cf1d18 | 2017-09-18 14:16:34 | [diff] [blame] | 414 | r'__unsafe_unretained', |
| 415 | ( |
| 416 | 'The use of __unsafe_unretained is almost certainly wrong, unless', |
| 417 | 'when interacting with NSFastEnumeration or NSInvocation.', |
| 418 | 'Please use __weak in files build with ARC, nothing otherwise.', |
| 419 | ), |
| 420 | False, |
| 421 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 422 | BanRule( |
Avi Drissman | 7382afa0 | 2019-04-29 23:27:13 | [diff] [blame] | 423 | 'freeWhenDone:NO', |
| 424 | ( |
| 425 | 'The use of "freeWhenDone:NO" with the NoCopy creation of ', |
| 426 | 'Foundation types is prohibited.', |
| 427 | ), |
| 428 | True, |
| 429 | ), |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 430 | ) |
| 431 | |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 432 | _BANNED_IOS_OBJC_FUNCTIONS = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 433 | BanRule( |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 434 | r'/\bTEST[(]', |
| 435 | ( |
| 436 | 'TEST() macro should not be used in Objective-C++ code as it does not ', |
| 437 | 'drain the autorelease pool at the end of the test. Use TEST_F() ', |
| 438 | 'macro instead with a fixture inheriting from PlatformTest (or a ', |
| 439 | 'typedef).' |
| 440 | ), |
| 441 | True, |
| 442 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 443 | BanRule( |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 444 | r'/\btesting::Test\b', |
| 445 | ( |
| 446 | 'testing::Test should not be used in Objective-C++ code as it does ', |
| 447 | 'not drain the autorelease pool at the end of the test. Use ', |
| 448 | 'PlatformTest instead.' |
| 449 | ), |
| 450 | True, |
| 451 | ), |
Ewann | 2ecc8d7 | 2022-07-18 07:41:23 | [diff] [blame] | 452 | BanRule( |
| 453 | ' systemImageNamed:', |
| 454 | ( |
| 455 | '+[UIImage systemImageNamed:] should not be used to create symbols.', |
| 456 | 'Instead use a wrapper defined in:', |
Victor Vianna | 77a40f6 | 2023-01-31 19:04:53 | [diff] [blame] | 457 | 'ios/chrome/browser/ui/icons/symbol_helpers.h' |
Ewann | 2ecc8d7 | 2022-07-18 07:41:23 | [diff] [blame] | 458 | ), |
| 459 | True, |
Ewann | 450a2ef | 2022-07-19 14:38:23 | [diff] [blame] | 460 | excluded_paths=( |
Gauthier Ambard | 4d8756b | 2023-04-07 17:26:41 | [diff] [blame] | 461 | 'ios/chrome/browser/shared/ui/symbols/symbol_helpers.mm', |
Gauthier Ambard | d36c10b1 | 2023-03-16 08:45:03 | [diff] [blame] | 462 | 'ios/chrome/search_widget_extension/', |
Ewann | 450a2ef | 2022-07-19 14:38:23 | [diff] [blame] | 463 | ), |
Ewann | 2ecc8d7 | 2022-07-18 07:41:23 | [diff] [blame] | 464 | ), |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 465 | ) |
| 466 | |
Daniel Cheng | 917ce54 | 2022-03-15 20:46:57 | [diff] [blame] | 467 | _BANNED_IOS_EGTEST_FUNCTIONS : Sequence[BanRule] = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 468 | BanRule( |
Peter K. Lee | 6c03ccff | 2019-07-15 14:40:05 | [diff] [blame] | 469 | r'/\bEXPECT_OCMOCK_VERIFY\b', |
| 470 | ( |
| 471 | 'EXPECT_OCMOCK_VERIFY should not be used in EarlGrey tests because ', |
| 472 | 'it is meant for GTests. Use [mock verify] instead.' |
| 473 | ), |
| 474 | True, |
| 475 | ), |
| 476 | ) |
| 477 | |
Daniel Cheng | 917ce54 | 2022-03-15 20:46:57 | [diff] [blame] | 478 | _BANNED_CPP_FUNCTIONS : Sequence[BanRule] = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 479 | BanRule( |
Peter Kasting | 94a56c4 | 2019-10-25 21:54:04 | [diff] [blame] | 480 | r'/\busing namespace ', |
| 481 | ( |
| 482 | 'Using directives ("using namespace x") are banned by the Google Style', |
| 483 | 'Guide ( http://google.github.io/styleguide/cppguide.html#Namespaces ).', |
| 484 | 'Explicitly qualify symbols or use using declarations ("using x::foo").', |
| 485 | ), |
| 486 | True, |
| 487 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 488 | ), |
Antonio Gomes | 07300d0 | 2019-03-13 20:59:57 | [diff] [blame] | 489 | # Make sure that gtest's FRIEND_TEST() macro is not used; the |
| 490 | # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be |
| 491 | # used instead since that allows for FLAKY_ and DISABLED_ prefixes. |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 492 | BanRule( |
avi@chromium.org | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 493 | 'FRIEND_TEST(', |
| 494 | ( |
jam@chromium.org | e3c94550 | 2012-06-26 20:01:49 | [diff] [blame] | 495 | 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include', |
avi@chromium.org | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 496 | 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.', |
| 497 | ), |
| 498 | False, |
jochen@chromium.org | 7345da0 | 2012-11-27 14:31:49 | [diff] [blame] | 499 | (), |
avi@chromium.org | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 500 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 501 | BanRule( |
tomhudson | e2c14d55 | 2016-05-26 17:07:46 | [diff] [blame] | 502 | 'setMatrixClip', |
| 503 | ( |
| 504 | 'Overriding setMatrixClip() is prohibited; ', |
| 505 | 'the base function is deprecated. ', |
| 506 | ), |
| 507 | True, |
| 508 | (), |
| 509 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 510 | BanRule( |
enne@chromium.org | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 511 | 'SkRefPtr', |
| 512 | ( |
| 513 | 'The use of SkRefPtr is prohibited. ', |
tomhudson | 7e6e051 | 2016-04-19 19:27:22 | [diff] [blame] | 514 | 'Please use sk_sp<> instead.' |
enne@chromium.org | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 515 | ), |
| 516 | True, |
| 517 | (), |
| 518 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 519 | BanRule( |
enne@chromium.org | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 520 | 'SkAutoRef', |
| 521 | ( |
| 522 | 'The indirect use of SkRefPtr via SkAutoRef is prohibited. ', |
tomhudson | 7e6e051 | 2016-04-19 19:27:22 | [diff] [blame] | 523 | 'Please use sk_sp<> instead.' |
enne@chromium.org | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 524 | ), |
| 525 | True, |
| 526 | (), |
| 527 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 528 | BanRule( |
enne@chromium.org | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 529 | 'SkAutoTUnref', |
| 530 | ( |
| 531 | 'The use of SkAutoTUnref is dangerous because it implicitly ', |
tomhudson | 7e6e051 | 2016-04-19 19:27:22 | [diff] [blame] | 532 | 'converts to a raw pointer. Please use sk_sp<> instead.' |
enne@chromium.org | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 533 | ), |
| 534 | True, |
| 535 | (), |
| 536 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 537 | BanRule( |
enne@chromium.org | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 538 | 'SkAutoUnref', |
| 539 | ( |
| 540 | 'The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ', |
| 541 | 'because it implicitly converts to a raw pointer. ', |
tomhudson | 7e6e051 | 2016-04-19 19:27:22 | [diff] [blame] | 542 | 'Please use sk_sp<> instead.' |
enne@chromium.org | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 543 | ), |
| 544 | True, |
| 545 | (), |
| 546 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 547 | BanRule( |
mark@chromium.org | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 548 | r'/HANDLE_EINTR\(.*close', |
| 549 | ( |
| 550 | 'HANDLE_EINTR(close) is invalid. If close fails with EINTR, the file', |
| 551 | 'descriptor will be closed, and it is incorrect to retry the close.', |
| 552 | 'Either call close directly and ignore its return value, or wrap close', |
| 553 | 'in IGNORE_EINTR to use its return value. See http://crbug.com/269623' |
| 554 | ), |
| 555 | True, |
| 556 | (), |
| 557 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 558 | BanRule( |
mark@chromium.org | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 559 | r'/IGNORE_EINTR\((?!.*close)', |
| 560 | ( |
| 561 | 'IGNORE_EINTR is only valid when wrapping close. To wrap other system', |
| 562 | 'calls, use HANDLE_EINTR. See http://crbug.com/269623', |
| 563 | ), |
| 564 | True, |
| 565 | ( |
| 566 | # Files that #define IGNORE_EINTR. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 567 | r'^base/posix/eintr_wrapper\.h$', |
| 568 | r'^ppapi/tests/test_broker\.cc$', |
mark@chromium.org | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 569 | ), |
| 570 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 571 | BanRule( |
jochen@chromium.org | ec5b3f0 | 2014-04-04 18:43:43 | [diff] [blame] | 572 | r'/v8::Extension\(', |
| 573 | ( |
| 574 | 'Do not introduce new v8::Extensions into the code base, use', |
| 575 | 'gin::Wrappable instead. See http://crbug.com/334679', |
| 576 | ), |
| 577 | True, |
rockot@chromium.org | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 578 | ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 579 | r'extensions/renderer/safe_builtins\.*', |
rockot@chromium.org | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 580 | ), |
jochen@chromium.org | ec5b3f0 | 2014-04-04 18:43:43 | [diff] [blame] | 581 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 582 | BanRule( |
jam | e2d1a95 | 2016-04-02 00:27:10 | [diff] [blame] | 583 | '#pragma comment(lib,', |
| 584 | ( |
| 585 | 'Specify libraries to link with in build files and not in the source.', |
| 586 | ), |
| 587 | True, |
Mirko Bonadei | f4f0f0e | 2018-04-12 09:29:41 | [diff] [blame] | 588 | ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 589 | r'^base/third_party/symbolize/.*', |
| 590 | r'^third_party/abseil-cpp/.*', |
Mirko Bonadei | f4f0f0e | 2018-04-12 09:29:41 | [diff] [blame] | 591 | ), |
jam | e2d1a95 | 2016-04-02 00:27:10 | [diff] [blame] | 592 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 593 | BanRule( |
Gabriel Charette | 7cc6c43 | 2018-04-25 20:52:02 | [diff] [blame] | 594 | r'/base::SequenceChecker\b', |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 595 | ( |
| 596 | 'Consider using SEQUENCE_CHECKER macros instead of the class directly.', |
| 597 | ), |
| 598 | False, |
| 599 | (), |
| 600 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 601 | BanRule( |
Gabriel Charette | 7cc6c43 | 2018-04-25 20:52:02 | [diff] [blame] | 602 | r'/base::ThreadChecker\b', |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 603 | ( |
| 604 | 'Consider using THREAD_CHECKER macros instead of the class directly.', |
| 605 | ), |
| 606 | False, |
| 607 | (), |
| 608 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 609 | BanRule( |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 610 | r'/\b(?!(Sequenced|SingleThread))\w*TaskRunner::(GetCurrentDefault|CurrentDefaultHandle)', |
| 611 | ( |
| 612 | 'It is not allowed to call these methods from the subclasses ', |
| 613 | 'of Sequenced or SingleThread task runners.', |
| 614 | ), |
| 615 | True, |
| 616 | (), |
| 617 | ), |
| 618 | BanRule( |
Yuri Wiitala | 2f8de5c | 2017-07-21 00:11:06 | [diff] [blame] | 619 | r'/(Time(|Delta|Ticks)|ThreadTicks)::FromInternalValue|ToInternalValue', |
| 620 | ( |
| 621 | 'base::TimeXXX::FromInternalValue() and ToInternalValue() are', |
| 622 | 'deprecated (http://crbug.com/634507). Please avoid converting away', |
| 623 | 'from the Time types in Chromium code, especially if any math is', |
| 624 | 'being done on time values. For interfacing with platform/library', |
| 625 | 'APIs, use FromMicroseconds() or InMicroseconds(), or one of the other', |
| 626 | 'type converter methods instead. For faking TimeXXX values (for unit', |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 627 | 'testing only), use TimeXXX() + Microseconds(N). For', |
Yuri Wiitala | 2f8de5c | 2017-07-21 00:11:06 | [diff] [blame] | 628 | 'other use cases, please contact base/time/OWNERS.', |
| 629 | ), |
| 630 | False, |
| 631 | (), |
| 632 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 633 | BanRule( |
dbeam | b6f4fde | 2017-06-15 04:03:06 | [diff] [blame] | 634 | 'CallJavascriptFunctionUnsafe', |
| 635 | ( |
| 636 | "Don't use CallJavascriptFunctionUnsafe() in new code. Instead, use", |
| 637 | 'AllowJavascript(), OnJavascriptAllowed()/OnJavascriptDisallowed(),', |
| 638 | 'and CallJavascriptFunction(). See https://goo.gl/qivavq.', |
| 639 | ), |
| 640 | False, |
| 641 | ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 642 | r'^content/browser/webui/web_ui_impl\.(cc|h)$', |
| 643 | r'^content/public/browser/web_ui\.h$', |
| 644 | r'^content/public/test/test_web_ui\.(cc|h)$', |
dbeam | b6f4fde | 2017-06-15 04:03:06 | [diff] [blame] | 645 | ), |
| 646 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 647 | BanRule( |
dskiba | 1474c2bfd6 | 2017-07-20 02:19:24 | [diff] [blame] | 648 | 'leveldb::DB::Open', |
| 649 | ( |
| 650 | 'Instead of leveldb::DB::Open() use leveldb_env::OpenDB() from', |
| 651 | 'third_party/leveldatabase/env_chromium.h. It exposes databases to', |
| 652 | "Chrome's tracing, making their memory usage visible.", |
| 653 | ), |
| 654 | True, |
| 655 | ( |
| 656 | r'^third_party/leveldatabase/.*\.(cc|h)$', |
| 657 | ), |
Gabriel Charette | 0592c3a | 2017-07-26 12:02:04 | [diff] [blame] | 658 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 659 | BanRule( |
Chris Mumford | c38afb6 | 2017-10-09 17:55:08 | [diff] [blame] | 660 | 'leveldb::NewMemEnv', |
| 661 | ( |
| 662 | 'Instead of leveldb::NewMemEnv() use leveldb_chrome::NewMemEnv() from', |
Chris Mumford | 8d26d10a | 2018-04-20 17:07:58 | [diff] [blame] | 663 | 'third_party/leveldatabase/leveldb_chrome.h. It exposes environments', |
| 664 | "to Chrome's tracing, making their memory usage visible.", |
Chris Mumford | c38afb6 | 2017-10-09 17:55:08 | [diff] [blame] | 665 | ), |
| 666 | True, |
| 667 | ( |
| 668 | r'^third_party/leveldatabase/.*\.(cc|h)$', |
| 669 | ), |
| 670 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 671 | BanRule( |
Gabriel Charette | d9839bc | 2017-07-29 14:17:47 | [diff] [blame] | 672 | 'RunLoop::QuitCurrent', |
| 673 | ( |
Robert Liao | 64b7ab2 | 2017-08-04 23:03:43 | [diff] [blame] | 674 | 'Please migrate away from RunLoop::QuitCurrent*() methods. Use member', |
| 675 | 'methods of a specific RunLoop instance instead.', |
Gabriel Charette | d9839bc | 2017-07-29 14:17:47 | [diff] [blame] | 676 | ), |
Gabriel Charette | c0a8f3ee | 2018-04-25 20:49:41 | [diff] [blame] | 677 | False, |
Gabriel Charette | d9839bc | 2017-07-29 14:17:47 | [diff] [blame] | 678 | (), |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 679 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 680 | BanRule( |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 681 | 'base::ScopedMockTimeMessageLoopTaskRunner', |
| 682 | ( |
Gabriel Charette | 87cc1af | 2018-04-25 20:52:51 | [diff] [blame] | 683 | 'ScopedMockTimeMessageLoopTaskRunner is deprecated. Prefer', |
Gabriel Charette | dfa3604 | 2019-08-19 17:30:11 | [diff] [blame] | 684 | 'TaskEnvironment::TimeSource::MOCK_TIME. There are still a', |
Gabriel Charette | 87cc1af | 2018-04-25 20:52:51 | [diff] [blame] | 685 | 'few cases that may require a ScopedMockTimeMessageLoopTaskRunner', |
| 686 | '(i.e. mocking the main MessageLoopForUI in browser_tests), but check', |
| 687 | 'with gab@ first if you think you need it)', |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 688 | ), |
Gabriel Charette | 87cc1af | 2018-04-25 20:52:51 | [diff] [blame] | 689 | False, |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 690 | (), |
Eric Stevenson | 6b47b44c | 2017-08-30 20:41:57 | [diff] [blame] | 691 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 692 | BanRule( |
Dave Tapuska | 98199b61 | 2019-07-10 13:30:44 | [diff] [blame] | 693 | 'std::regex', |
Eric Stevenson | 6b47b44c | 2017-08-30 20:41:57 | [diff] [blame] | 694 | ( |
| 695 | 'Using std::regex adds unnecessary binary size to Chrome. Please use', |
Mostyn Bramley-Moore | 6b42732 | 2017-12-21 22:11:02 | [diff] [blame] | 696 | 're2::RE2 instead (crbug.com/755321)', |
Eric Stevenson | 6b47b44c | 2017-08-30 20:41:57 | [diff] [blame] | 697 | ), |
| 698 | True, |
Danil Chapovalov | 7bc42a7 | 2020-12-09 18:20:16 | [diff] [blame] | 699 | # Abseil's benchmarks never linked into chrome. |
| 700 | ['third_party/abseil-cpp/.*_benchmark.cc'], |
Francois Doray | 43670e3 | 2017-09-27 12:40:38 | [diff] [blame] | 701 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 702 | BanRule( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 703 | r'/\bstd::sto(i|l|ul|ll|ull)\b', |
Peter Kasting | 991618a6 | 2019-06-17 22:00:09 | [diff] [blame] | 704 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 705 | 'std::sto{i,l,ul,ll,ull}() use exceptions to communicate results. ', |
| 706 | 'Use base::StringTo[U]Int[64]() instead.', |
Peter Kasting | 991618a6 | 2019-06-17 22:00:09 | [diff] [blame] | 707 | ), |
| 708 | True, |
| 709 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 710 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 711 | BanRule( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 712 | r'/\bstd::sto(f|d|ld)\b', |
Peter Kasting | 991618a6 | 2019-06-17 22:00:09 | [diff] [blame] | 713 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 714 | 'std::sto{f,d,ld}() use exceptions to communicate results. ', |
Peter Kasting | 991618a6 | 2019-06-17 22:00:09 | [diff] [blame] | 715 | 'For locale-independent values, e.g. reading numbers from disk', |
| 716 | 'profiles, use base::StringToDouble().', |
| 717 | 'For user-visible values, parse using ICU.', |
| 718 | ), |
| 719 | True, |
| 720 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 721 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 722 | BanRule( |
Daniel Bratell | 69334cc | 2019-03-26 11:07:45 | [diff] [blame] | 723 | r'/\bstd::to_string\b', |
| 724 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 725 | 'std::to_string() is locale dependent and slower than alternatives.', |
Peter Kasting | 991618a6 | 2019-06-17 22:00:09 | [diff] [blame] | 726 | 'For locale-independent strings, e.g. writing numbers to disk', |
| 727 | 'profiles, use base::NumberToString().', |
Daniel Bratell | 69334cc | 2019-03-26 11:07:45 | [diff] [blame] | 728 | 'For user-visible strings, use base::FormatNumber() and', |
| 729 | 'the related functions in base/i18n/number_formatting.h.', |
| 730 | ), |
Peter Kasting | 991618a6 | 2019-06-17 22:00:09 | [diff] [blame] | 731 | False, # Only a warning since it is already used. |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 732 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Daniel Bratell | 69334cc | 2019-03-26 11:07:45 | [diff] [blame] | 733 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 734 | BanRule( |
Daniel Bratell | 69334cc | 2019-03-26 11:07:45 | [diff] [blame] | 735 | r'/\bstd::shared_ptr\b', |
| 736 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 737 | 'std::shared_ptr is banned. Use scoped_refptr instead.', |
Daniel Bratell | 69334cc | 2019-03-26 11:07:45 | [diff] [blame] | 738 | ), |
| 739 | True, |
Ulan Degenbaev | 94704388 | 2021-02-10 14:02:31 | [diff] [blame] | 740 | [ |
| 741 | # Needed for interop with third-party library. |
| 742 | '^third_party/blink/renderer/core/typed_arrays/array_buffer/' + |
Alex Chau | 9eb03cdd5 | 2020-07-13 21:04:57 | [diff] [blame] | 743 | 'array_buffer_contents\.(cc|h)', |
Ben Kelly | 39bf6bef | 2021-10-04 22:54:58 | [diff] [blame] | 744 | '^third_party/blink/renderer/bindings/core/v8/' + |
| 745 | 'v8_wasm_response_extensions.cc', |
Wez | 5f56be5 | 2021-05-04 09:30:58 | [diff] [blame] | 746 | '^gin/array_buffer\.(cc|h)', |
| 747 | '^chrome/services/sharing/nearby/', |
Stephen Nusko | e09c8ef2 | 2022-09-29 00:47:28 | [diff] [blame] | 748 | # Needed for interop with third-party library libunwindstack. |
Stephen Nusko | e51c138 | 2022-09-26 15:49:03 | [diff] [blame] | 749 | '^base/profiler/libunwindstack_unwinder_android\.(cc|h)', |
Bob Beck | 03509d28 | 2022-12-07 21:49:05 | [diff] [blame] | 750 | # Needed for interop with third-party boringssl cert verifier |
| 751 | '^third_party/boringssl/', |
| 752 | '^net/cert/', |
| 753 | '^net/tools/cert_verify_tool/', |
| 754 | '^services/cert_verifier/', |
| 755 | '^components/certificate_transparency/', |
| 756 | '^components/media_router/common/providers/cast/certificate/', |
Meilin Wang | 00efc7c | 2021-05-13 01:12:42 | [diff] [blame] | 757 | # gRPC provides some C++ libraries that use std::shared_ptr<>. |
Yeunjoo Choi | 1b64440 | 2022-08-25 02:36:10 | [diff] [blame] | 758 | '^chromeos/ash/services/libassistant/grpc/', |
Vigen Issahhanjan | fdf9de5 | 2021-12-22 21:13:59 | [diff] [blame] | 759 | '^chromecast/cast_core/grpc', |
| 760 | '^chromecast/cast_core/runtime/browser', |
Yue She | f83d9520 | 2022-09-26 20:23:45 | [diff] [blame] | 761 | '^ios/chrome/test/earl_grey/chrome_egtest_plugin_client\.(mm|h)', |
Wez | 5f56be5 | 2021-05-04 09:30:58 | [diff] [blame] | 762 | # Fuchsia provides C++ libraries that use std::shared_ptr<>. |
Wez | 6da2e41 | 2022-11-23 11:28:48 | [diff] [blame] | 763 | '^base/fuchsia/.*\.(cc|h)', |
Wez | 5f56be5 | 2021-05-04 09:30:58 | [diff] [blame] | 764 | '.*fuchsia.*test\.(cc|h)', |
mikt | b599ed1 | 2023-05-26 07:03:56 | [diff] [blame] | 765 | # Clang plugins have different build config. |
| 766 | '^tools/clang/plugins/', |
Alex Chau | 9eb03cdd5 | 2020-07-13 21:04:57 | [diff] [blame] | 767 | _THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 768 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 769 | BanRule( |
Peter Kasting | 991618a6 | 2019-06-17 22:00:09 | [diff] [blame] | 770 | r'/\bstd::weak_ptr\b', |
| 771 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 772 | 'std::weak_ptr is banned. Use base::WeakPtr instead.', |
Peter Kasting | 991618a6 | 2019-06-17 22:00:09 | [diff] [blame] | 773 | ), |
| 774 | True, |
| 775 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 776 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 777 | BanRule( |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 778 | r'/\blong long\b', |
| 779 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 780 | 'long long is banned. Use [u]int64_t instead.', |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 781 | ), |
| 782 | False, # Only a warning since it is already used. |
| 783 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 784 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 785 | BanRule( |
Daniel Cheng | 192683f | 2022-11-01 20:52:44 | [diff] [blame] | 786 | r'/\b(absl|std)::any\b', |
Daniel Cheng | c05fcc6 | 2022-01-12 16:54:29 | [diff] [blame] | 787 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 788 | '{absl,std}::any are banned due to incompatibility with the component ', |
| 789 | 'build.', |
Daniel Cheng | c05fcc6 | 2022-01-12 16:54:29 | [diff] [blame] | 790 | ), |
| 791 | True, |
| 792 | # Not an error in third party folders, though it probably should be :) |
| 793 | [_THIRD_PARTY_EXCEPT_BLINK], |
| 794 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 795 | BanRule( |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 796 | r'/\bstd::bind\b', |
| 797 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 798 | 'std::bind() is banned because of lifetime risks. Use ', |
| 799 | 'base::Bind{Once,Repeating}() instead.', |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 800 | ), |
| 801 | True, |
| 802 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 803 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 804 | BanRule( |
Daniel Cheng | 192683f | 2022-11-01 20:52:44 | [diff] [blame] | 805 | ( |
Peter Kasting | c7460d98 | 2023-03-14 21:01:42 | [diff] [blame] | 806 | r'/\bstd::(?:' |
| 807 | r'linear_congruential_engine|mersenne_twister_engine|' |
| 808 | r'subtract_with_carry_engine|discard_block_engine|' |
| 809 | r'independent_bits_engine|shuffle_order_engine|' |
| 810 | r'minstd_rand0?|mt19937(_64)?|ranlux(24|48)(_base)?|knuth_b|' |
| 811 | r'default_random_engine|' |
| 812 | r'random_device|' |
| 813 | r'seed_seq' |
Daniel Cheng | 192683f | 2022-11-01 20:52:44 | [diff] [blame] | 814 | r')\b' |
| 815 | ), |
| 816 | ( |
| 817 | 'STL random number engines and generators are banned. Use the ', |
| 818 | 'helpers in base/rand_util.h instead, e.g. base::RandBytes() or ', |
| 819 | 'base::RandomBitGenerator.' |
| 820 | ), |
| 821 | True, |
| 822 | [ |
| 823 | # Not an error in third_party folders. |
| 824 | _THIRD_PARTY_EXCEPT_BLINK, |
| 825 | # Various tools which build outside of Chrome. |
| 826 | r'testing/libfuzzer', |
| 827 | r'tools/android/io_benchmark/', |
| 828 | # Fuzzers are allowed to use standard library random number generators |
| 829 | # since fuzzing speed + reproducibility is important. |
| 830 | r'tools/ipc_fuzzer/', |
| 831 | r'.+_fuzzer\.cc$', |
| 832 | r'.+_fuzzertest\.cc$', |
| 833 | # TODO(https://crbug.com/1380528): These are all unsanctioned uses of |
| 834 | # the standard library's random number generators, and should be |
| 835 | # migrated to the //base equivalent. |
| 836 | r'ash/ambient/model/ambient_topic_queue\.cc', |
| 837 | r'base/allocator/partition_allocator/partition_alloc_unittest\.cc', |
| 838 | r'base/ranges/algorithm_unittest\.cc', |
| 839 | r'base/test/launcher/test_launcher\.cc', |
| 840 | r'cc/metrics/video_playback_roughness_reporter_unittest\.cc', |
| 841 | r'chrome/browser/apps/app_service/metrics/website_metrics\.cc', |
| 842 | r'chrome/browser/ash/power/auto_screen_brightness/monotone_cubic_spline_unittest\.cc', |
| 843 | r'chrome/browser/ash/printing/zeroconf_printer_detector_unittest\.cc', |
| 844 | r'chrome/browser/nearby_sharing/contacts/nearby_share_contact_manager_impl_unittest\.cc', |
| 845 | r'chrome/browser/nearby_sharing/contacts/nearby_share_contacts_sorter_unittest\.cc', |
| 846 | r'chrome/browser/privacy_budget/mesa_distribution_unittest\.cc', |
| 847 | r'chrome/browser/web_applications/test/web_app_test_utils\.cc', |
| 848 | r'chrome/browser/web_applications/test/web_app_test_utils\.cc', |
| 849 | r'chrome/browser/win/conflicts/module_blocklist_cache_util_unittest\.cc', |
| 850 | r'chrome/chrome_cleaner/logging/detailed_info_sampler\.cc', |
| 851 | r'chromeos/ash/components/memory/userspace_swap/swap_storage_unittest\.cc', |
| 852 | r'chromeos/ash/components/memory/userspace_swap/userspace_swap\.cc', |
| 853 | r'components/metrics/metrics_state_manager\.cc', |
| 854 | r'components/omnibox/browser/history_quick_provider_performance_unittest\.cc', |
| 855 | r'components/zucchini/disassembler_elf_unittest\.cc', |
| 856 | r'content/browser/webid/federated_auth_request_impl\.cc', |
| 857 | r'content/browser/webid/federated_auth_request_impl\.cc', |
| 858 | r'media/cast/test/utility/udp_proxy\.h', |
| 859 | r'sql/recover_module/module_unittest\.cc', |
| 860 | ], |
| 861 | ), |
| 862 | BanRule( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 863 | r'/\b(absl,std)::bind_front\b', |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 864 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 865 | '{absl,std}::bind_front() are banned. Use base::Bind{Once,Repeating}() ' |
| 866 | 'instead.', |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 867 | ), |
| 868 | True, |
| 869 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 870 | ), |
| 871 | BanRule( |
| 872 | r'/\bABSL_FLAG\b', |
| 873 | ( |
| 874 | 'ABSL_FLAG is banned. Use base::CommandLine instead.', |
| 875 | ), |
| 876 | True, |
| 877 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 878 | ), |
| 879 | BanRule( |
| 880 | r'/\babsl::c_', |
| 881 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 882 | 'Abseil container utilities are banned. Use base/ranges/algorithm.h ', |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 883 | 'instead.', |
| 884 | ), |
| 885 | True, |
| 886 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 887 | ), |
| 888 | BanRule( |
| 889 | r'/\babsl::FunctionRef\b', |
| 890 | ( |
| 891 | 'absl::FunctionRef is banned. Use base::FunctionRef instead.', |
| 892 | ), |
| 893 | True, |
| 894 | [ |
| 895 | # base::Bind{Once,Repeating} references absl::FunctionRef to disallow |
| 896 | # interoperability. |
| 897 | r'^base/functional/bind_internal\.h', |
| 898 | # base::FunctionRef is implemented on top of absl::FunctionRef. |
| 899 | r'^base/functional/function_ref.*\..+', |
| 900 | # Not an error in third_party folders. |
| 901 | _THIRD_PARTY_EXCEPT_BLINK, |
| 902 | ], |
| 903 | ), |
| 904 | BanRule( |
| 905 | r'/\babsl::(Insecure)?BitGen\b', |
| 906 | ( |
Daniel Cheng | 192683f | 2022-11-01 20:52:44 | [diff] [blame] | 907 | 'absl random number generators are banned. Use the helpers in ' |
| 908 | 'base/rand_util.h instead, e.g. base::RandBytes() or ', |
| 909 | 'base::RandomBitGenerator.' |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 910 | ), |
| 911 | True, |
| 912 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 913 | ), |
| 914 | BanRule( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 915 | r'/(\babsl::Span\b|#include <span>)', |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 916 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 917 | 'absl::Span is banned and <span> is not allowed yet ', |
| 918 | '(https://crbug.com/1414652). Use base::span instead.', |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 919 | ), |
| 920 | True, |
Victor Vasiliev | 23b9ea6a | 2023-01-05 19:42:29 | [diff] [blame] | 921 | [ |
| 922 | # Needed to use QUICHE API. |
| 923 | r'services/network/web_transport\.cc', |
Brianna Goldstein | 846d800 | 2023-05-16 19:24:30 | [diff] [blame] | 924 | r'chrome/browser/ip_protection/.*', |
Victor Vasiliev | 23b9ea6a | 2023-01-05 19:42:29 | [diff] [blame] | 925 | # Not an error in third_party folders. |
| 926 | _THIRD_PARTY_EXCEPT_BLINK |
| 927 | ], |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 928 | ), |
| 929 | BanRule( |
| 930 | r'/\babsl::StatusOr\b', |
| 931 | ( |
| 932 | 'absl::StatusOr is banned. Use base::expected instead.', |
| 933 | ), |
| 934 | True, |
Adithya Srinivasan | b204188 | 2022-10-21 19:34:20 | [diff] [blame] | 935 | [ |
| 936 | # Needed to use liburlpattern API. |
| 937 | r'third_party/blink/renderer/core/url_pattern/.*', |
Louise Brett | c6d2387 | 2023-04-11 02:48:32 | [diff] [blame] | 938 | r'third_party/blink/renderer/modules/manifest/manifest_parser\.cc', |
Brianna Goldstein | 846d800 | 2023-05-16 19:24:30 | [diff] [blame] | 939 | # Needed to use QUICHE API. |
| 940 | r'chrome/browser/ip_protection/.*', |
Adithya Srinivasan | b204188 | 2022-10-21 19:34:20 | [diff] [blame] | 941 | # Not an error in third_party folders. |
| 942 | _THIRD_PARTY_EXCEPT_BLINK |
| 943 | ], |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 944 | ), |
| 945 | BanRule( |
| 946 | r'/\babsl::StrFormat\b', |
| 947 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 948 | 'absl::StrFormat() is not allowed yet (https://crbug.com/1371963). ', |
| 949 | 'Use base::StringPrintf() instead.', |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 950 | ), |
| 951 | True, |
| 952 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 953 | ), |
| 954 | BanRule( |
David Benjamin | ea985a2 | 2023-04-18 22:05:01 | [diff] [blame] | 955 | r'/\babsl::string_view\b', |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 956 | ( |
David Benjamin | ea985a2 | 2023-04-18 22:05:01 | [diff] [blame] | 957 | 'absl::string_view is a legacy spelling of std::string_view, which is ', |
| 958 | 'not allowed yet (https://crbug.com/691162). Use base::StringPiece ', |
| 959 | 'instead, unless std::string_view is needed to use with an external ', |
| 960 | 'API.', |
| 961 | ), |
| 962 | True, |
| 963 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 964 | ), |
| 965 | BanRule( |
| 966 | r'/\bstd::(u16)?string_view\b', |
| 967 | ( |
| 968 | 'std::[u16]string_view is not yet allowed (crbug.com/691162). Use ', |
| 969 | 'base::StringPiece[16] instead, unless std::[u16]string_view is ', |
| 970 | 'needed to use an external API.', |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 971 | ), |
| 972 | True, |
Adithya Srinivasan | b204188 | 2022-10-21 19:34:20 | [diff] [blame] | 973 | [ |
David Benjamin | ea985a2 | 2023-04-18 22:05:01 | [diff] [blame] | 974 | # Needed to implement and test std::string_view interoperability. |
| 975 | r'base/strings/string_piece.*', |
Xiaochen Zhou | f19c97f | 2023-04-28 13:04:32 | [diff] [blame] | 976 | # Needed to use re2::RE2 regular expression library. |
| 977 | r'third_party/blink/common/interest_group/ad_display_size_utils.cc', |
Adithya Srinivasan | b204188 | 2022-10-21 19:34:20 | [diff] [blame] | 978 | # Needed to use liburlpattern API. |
| 979 | r'third_party/blink/renderer/core/url_pattern/.*', |
Louise Brett | c6d2387 | 2023-04-11 02:48:32 | [diff] [blame] | 980 | r'third_party/blink/renderer/modules/manifest/manifest_parser\.cc', |
David Benjamin | 3a305f1 | 2022-11-19 00:10:03 | [diff] [blame] | 981 | # Needed to use QUICHE API. |
Victor Vasiliev | a13f193 | 2022-12-02 15:27:24 | [diff] [blame] | 982 | r'net/quic/.*', |
| 983 | r'net/spdy/.*', |
David Benjamin | 3a305f1 | 2022-11-19 00:10:03 | [diff] [blame] | 984 | r'net/test/embedded_test_server/.*', |
Victor Vasiliev | a13f193 | 2022-12-02 15:27:24 | [diff] [blame] | 985 | r'net/third_party/quiche/.*', |
| 986 | r'services/network/web_transport\.cc', |
David Benjamin | ea985a2 | 2023-04-18 22:05:01 | [diff] [blame] | 987 | # This code is in the process of being extracted into an external |
| 988 | # library, where //base will be unavailable. |
| 989 | r'net/cert/pki/.*', |
| 990 | r'net/der/.*', |
| 991 | # Needed to use APIs from the above. |
| 992 | r'net/cert/.*', |
Adithya Srinivasan | b204188 | 2022-10-21 19:34:20 | [diff] [blame] | 993 | # Not an error in third_party folders. |
| 994 | _THIRD_PARTY_EXCEPT_BLINK |
| 995 | ], |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 996 | ), |
| 997 | BanRule( |
| 998 | r'/\babsl::(StrSplit|StrJoin|StrCat|StrAppend|Substitute|StrContains)\b', |
| 999 | ( |
| 1000 | 'Abseil string utilities are banned. Use base/strings instead.', |
| 1001 | ), |
| 1002 | True, |
| 1003 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 1004 | ), |
| 1005 | BanRule( |
| 1006 | r'/\babsl::(Mutex|CondVar|Notification|Barrier|BlockingCounter)\b', |
| 1007 | ( |
| 1008 | 'Abseil synchronization primitives are banned. Use', |
| 1009 | 'base/synchronization instead.', |
| 1010 | ), |
| 1011 | True, |
| 1012 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 1013 | ), |
| 1014 | BanRule( |
| 1015 | r'/\babsl::(Duration|Time|TimeZone|CivilDay)\b', |
| 1016 | ( |
| 1017 | 'Abseil\'s time library is banned. Use base/time instead.', |
| 1018 | ), |
| 1019 | True, |
| 1020 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 1021 | ), |
| 1022 | BanRule( |
Avi Drissman | 48ee39e | 2022-02-16 16:31:03 | [diff] [blame] | 1023 | r'/\bstd::optional\b', |
| 1024 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1025 | 'std::optional is not allowed yet (https://crbug.com/1373619). Use ', |
| 1026 | 'absl::optional instead.', |
Avi Drissman | 48ee39e | 2022-02-16 16:31:03 | [diff] [blame] | 1027 | ), |
| 1028 | True, |
mikt | b599ed1 | 2023-05-26 07:03:56 | [diff] [blame] | 1029 | [ |
| 1030 | # Clang plugins have different build config. |
| 1031 | '^tools/clang/plugins/', |
| 1032 | # Not an error in third_party folders. |
| 1033 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1034 | ], |
Avi Drissman | 48ee39e | 2022-02-16 16:31:03 | [diff] [blame] | 1035 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1036 | BanRule( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1037 | r'/#include <chrono>', |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 1038 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1039 | '<chrono> is banned. Use base/time instead.', |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 1040 | ), |
| 1041 | True, |
| 1042 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 1043 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1044 | BanRule( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1045 | r'/#include <exception>', |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 1046 | ( |
| 1047 | 'Exceptions are banned and disabled in Chromium.', |
| 1048 | ), |
| 1049 | True, |
| 1050 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 1051 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1052 | BanRule( |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 1053 | r'/\bstd::function\b', |
| 1054 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1055 | 'std::function is banned. Use base::{Once,Repeating}Callback instead.', |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 1056 | ), |
Daniel Cheng | e5583e3c | 2022-09-22 00:19:41 | [diff] [blame] | 1057 | True, |
Daniel Cheng | cd23b8b | 2022-09-16 17:16:24 | [diff] [blame] | 1058 | [ |
| 1059 | # Has tests that template trait helpers don't unintentionally match |
| 1060 | # std::function. |
Daniel Cheng | e5583e3c | 2022-09-22 00:19:41 | [diff] [blame] | 1061 | r'base/functional/callback_helpers_unittest\.cc', |
| 1062 | # Required to implement interfaces from the third-party perfetto |
| 1063 | # library. |
| 1064 | r'base/tracing/perfetto_task_runner\.cc', |
| 1065 | r'base/tracing/perfetto_task_runner\.h', |
| 1066 | # Needed for interop with the third-party nearby library type |
| 1067 | # location::nearby::connections::ResultCallback. |
| 1068 | 'chrome/services/sharing/nearby/nearby_connections_conversions\.cc' |
| 1069 | # Needed for interop with the internal libassistant library. |
| 1070 | 'chromeos/ash/services/libassistant/callback_utils\.h', |
| 1071 | # Needed for interop with Fuchsia fidl APIs. |
| 1072 | 'fuchsia_web/webengine/browser/context_impl_browsertest\.cc', |
| 1073 | 'fuchsia_web/webengine/browser/cookie_manager_impl_unittest\.cc', |
| 1074 | 'fuchsia_web/webengine/browser/media_player_impl_unittest\.cc', |
| 1075 | # Required to interop with interfaces from the third-party perfetto |
| 1076 | # library. |
| 1077 | 'services/tracing/public/cpp/perfetto/custom_event_recorder\.cc', |
| 1078 | 'services/tracing/public/cpp/perfetto/perfetto_traced_process\.cc', |
| 1079 | 'services/tracing/public/cpp/perfetto/perfetto_traced_process\.h', |
| 1080 | 'services/tracing/public/cpp/perfetto/perfetto_tracing_backend\.cc', |
| 1081 | 'services/tracing/public/cpp/perfetto/producer_client\.cc', |
| 1082 | 'services/tracing/public/cpp/perfetto/producer_client\.h', |
| 1083 | 'services/tracing/public/cpp/perfetto/producer_test_utils\.cc', |
| 1084 | 'services/tracing/public/cpp/perfetto/producer_test_utils\.h', |
| 1085 | # Required for interop with the third-party webrtc library. |
| 1086 | 'third_party/blink/renderer/modules/peerconnection/mock_peer_connection_impl\.cc', |
| 1087 | 'third_party/blink/renderer/modules/peerconnection/mock_peer_connection_impl\.h', |
Bob Beck | 5fc0be8 | 2022-12-12 23:32:52 | [diff] [blame] | 1088 | # This code is in the process of being extracted into a third-party library. |
| 1089 | # See https://crbug.com/1322914 |
| 1090 | '^net/cert/pki/path_builder_unittest\.cc', |
Brianna Goldstein | 846d800 | 2023-05-16 19:24:30 | [diff] [blame] | 1091 | # Needed to use QUICHE API |
| 1092 | r'chrome/browser/ip_protection/.*', |
Daniel Cheng | e5583e3c | 2022-09-22 00:19:41 | [diff] [blame] | 1093 | # TODO(https://crbug.com/1364577): Various uses that should be |
| 1094 | # migrated to something else. |
| 1095 | # Should use base::OnceCallback or base::RepeatingCallback. |
| 1096 | 'base/allocator/dispatcher/initializer_unittest\.cc', |
| 1097 | 'chrome/browser/ash/accessibility/speech_monitor\.cc', |
| 1098 | 'chrome/browser/ash/accessibility/speech_monitor\.h', |
| 1099 | 'chrome/browser/ash/login/ash_hud_login_browsertest\.cc', |
| 1100 | 'chromecast/base/observer_unittest\.cc', |
| 1101 | 'chromecast/browser/cast_web_view\.h', |
| 1102 | 'chromecast/public/cast_media_shlib\.h', |
| 1103 | 'device/bluetooth/floss/exported_callback_manager\.h', |
| 1104 | 'device/bluetooth/floss/floss_dbus_client\.h', |
| 1105 | 'device/fido/cable/v2_handshake_unittest\.cc', |
| 1106 | 'device/fido/pin\.cc', |
| 1107 | 'services/tracing/perfetto/test_utils\.h', |
| 1108 | # Should use base::FunctionRef. |
| 1109 | 'chrome/browser/media/webrtc/test_stats_dictionary\.cc', |
| 1110 | 'chrome/browser/media/webrtc/test_stats_dictionary\.h', |
| 1111 | 'chromeos/ash/services/libassistant/device_settings_controller\.cc', |
| 1112 | 'components/browser_ui/client_certificate/android/ssl_client_certificate_request\.cc', |
| 1113 | 'components/gwp_asan/client/sampling_malloc_shims_unittest\.cc', |
| 1114 | 'content/browser/font_unique_name_lookup/font_unique_name_lookup_unittest\.cc', |
| 1115 | # Does not need std::function at all. |
| 1116 | 'components/omnibox/browser/autocomplete_result\.cc', |
| 1117 | 'device/fido/win/webauthn_api\.cc', |
| 1118 | 'media/audio/alsa/alsa_util\.cc', |
| 1119 | 'media/remoting/stream_provider\.h', |
| 1120 | 'sql/vfs_wrapper\.cc', |
| 1121 | # TODO(https://crbug.com/1364585): Remove usage and exception list |
| 1122 | # entries. |
| 1123 | 'extensions/renderer/api/automation/automation_internal_custom_bindings\.cc', |
| 1124 | 'extensions/renderer/api/automation/automation_internal_custom_bindings\.h', |
| 1125 | # TODO(https://crbug.com/1364579): Remove usage and exception list |
| 1126 | # entry. |
| 1127 | 'ui/views/controls/focus_ring\.h', |
| 1128 | |
| 1129 | # Various pre-existing uses in //tools that is low-priority to fix. |
| 1130 | 'tools/binary_size/libsupersize/viewer/caspian/diff\.cc', |
| 1131 | 'tools/binary_size/libsupersize/viewer/caspian/model\.cc', |
| 1132 | 'tools/binary_size/libsupersize/viewer/caspian/model\.h', |
| 1133 | 'tools/binary_size/libsupersize/viewer/caspian/tree_builder\.h', |
| 1134 | 'tools/clang/base_bind_rewriters/BaseBindRewriters\.cpp', |
| 1135 | |
Daniel Cheng | cd23b8b | 2022-09-16 17:16:24 | [diff] [blame] | 1136 | # Not an error in third_party folders. |
| 1137 | _THIRD_PARTY_EXCEPT_BLINK |
| 1138 | ], |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 1139 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1140 | BanRule( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1141 | r'/#include <X11/', |
Tom Anderson | a95e1204 | 2020-09-09 23:08:00 | [diff] [blame] | 1142 | ( |
| 1143 | 'Do not use Xlib. Use xproto (from //ui/gfx/x:xproto) instead.', |
| 1144 | ), |
| 1145 | True, |
| 1146 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 1147 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1148 | BanRule( |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 1149 | r'/\bstd::ratio\b', |
| 1150 | ( |
| 1151 | 'std::ratio is banned by the Google Style Guide.', |
| 1152 | ), |
| 1153 | True, |
| 1154 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Daniel Bratell | 69334cc | 2019-03-26 11:07:45 | [diff] [blame] | 1155 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1156 | BanRule( |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1157 | r'/\bstd::aligned_alloc\b', |
| 1158 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1159 | 'std::aligned_alloc() is not yet allowed (crbug.com/1412818). Use ', |
| 1160 | 'base::AlignedAlloc() instead.', |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1161 | ), |
| 1162 | True, |
| 1163 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 1164 | ), |
| 1165 | BanRule( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1166 | r'/#include <(barrier|latch|semaphore|stop_token)>', |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1167 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1168 | 'The thread support library is banned. Use base/synchronization ' |
| 1169 | 'instead.', |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1170 | ), |
| 1171 | True, |
| 1172 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 1173 | ), |
| 1174 | BanRule( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1175 | r'/\bstd::(c8rtomb|mbrtoc8)\b', |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1176 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1177 | 'std::c8rtomb() and std::mbrtoc8() are banned.', |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1178 | ), |
| 1179 | True, |
| 1180 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 1181 | ), |
| 1182 | BanRule( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1183 | r'/\bchar8_t|std::u8string\b', |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1184 | ( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1185 | 'char8_t and std::u8string are not yet allowed. Can you use [unsigned]', |
| 1186 | ' char and std::string instead?', |
| 1187 | ), |
| 1188 | True, |
Daniel Cheng | 893c563f | 2023-04-21 09:54:52 | [diff] [blame] | 1189 | [ |
| 1190 | # The demangler does not use this type but needs to know about it. |
| 1191 | 'base/third_party/symbolize/demangle\.cc', |
| 1192 | # Don't warn in third_party folders. |
| 1193 | _THIRD_PARTY_EXCEPT_BLINK |
| 1194 | ], |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1195 | ), |
| 1196 | BanRule( |
| 1197 | r'/(\b(co_await|co_return|co_yield)\b|#include <coroutine>)', |
| 1198 | ( |
| 1199 | 'Coroutines are not yet allowed (https://crbug.com/1403840).', |
| 1200 | ), |
| 1201 | True, |
| 1202 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 1203 | ), |
| 1204 | BanRule( |
Peter Kasting | cc15252 | 2023-03-22 20:17:37 | [diff] [blame] | 1205 | r'/^\s*(export\s|import\s+["<:\w]|module(;|\s+[:\w]))', |
Peter Kasting | 69357dc | 2023-03-14 01:34:29 | [diff] [blame] | 1206 | ( |
| 1207 | 'Modules are disallowed for now due to lack of toolchain support.', |
| 1208 | ), |
| 1209 | True, |
| 1210 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 1211 | ), |
| 1212 | BanRule( |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1213 | r'/\[\[(un)?likely\]\]', |
| 1214 | ( |
| 1215 | '[[likely]] and [[unlikely]] are not yet allowed ', |
| 1216 | '(https://crbug.com/1414620). Use [UN]LIKELY instead.', |
| 1217 | ), |
| 1218 | True, |
| 1219 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 1220 | ), |
| 1221 | BanRule( |
| 1222 | r'/#include <format>', |
| 1223 | ( |
| 1224 | '<format> is not yet allowed. Use base::StringPrintf() instead.', |
| 1225 | ), |
| 1226 | True, |
| 1227 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 1228 | ), |
| 1229 | BanRule( |
| 1230 | r'/#include <ranges>', |
| 1231 | ( |
| 1232 | '<ranges> is not yet allowed. Use base/ranges/algorithm.h instead.', |
| 1233 | ), |
| 1234 | True, |
| 1235 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 1236 | ), |
| 1237 | BanRule( |
| 1238 | r'/#include <source_location>', |
| 1239 | ( |
| 1240 | '<source_location> is not yet allowed. Use base/location.h instead.', |
| 1241 | ), |
| 1242 | True, |
| 1243 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 1244 | ), |
| 1245 | BanRule( |
| 1246 | r'/#include <syncstream>', |
| 1247 | ( |
| 1248 | '<syncstream> is banned.', |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1249 | ), |
| 1250 | True, |
| 1251 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 1252 | ), |
| 1253 | BanRule( |
Michael Giuffrida | 7f93d692 | 2019-04-19 14:39:58 | [diff] [blame] | 1254 | r'/\bRunMessageLoop\b', |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1255 | ( |
| 1256 | 'RunMessageLoop is deprecated, use RunLoop instead.', |
| 1257 | ), |
| 1258 | False, |
| 1259 | (), |
| 1260 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1261 | BanRule( |
Dave Tapuska | 98199b61 | 2019-07-10 13:30:44 | [diff] [blame] | 1262 | 'RunAllPendingInMessageLoop()', |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1263 | ( |
| 1264 | "Prefer RunLoop over RunAllPendingInMessageLoop, please contact gab@", |
| 1265 | "if you're convinced you need this.", |
| 1266 | ), |
| 1267 | False, |
| 1268 | (), |
| 1269 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1270 | BanRule( |
Dave Tapuska | 98199b61 | 2019-07-10 13:30:44 | [diff] [blame] | 1271 | 'RunAllPendingInMessageLoop(BrowserThread', |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1272 | ( |
| 1273 | 'RunAllPendingInMessageLoop is deprecated. Use RunLoop for', |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 1274 | 'BrowserThread::UI, BrowserTaskEnvironment::RunIOThreadUntilIdle', |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1275 | 'for BrowserThread::IO, and prefer RunLoop::QuitClosure to observe', |
| 1276 | 'async events instead of flushing threads.', |
| 1277 | ), |
| 1278 | False, |
| 1279 | (), |
| 1280 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1281 | BanRule( |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1282 | r'MessageLoopRunner', |
| 1283 | ( |
| 1284 | 'MessageLoopRunner is deprecated, use RunLoop instead.', |
| 1285 | ), |
| 1286 | False, |
| 1287 | (), |
| 1288 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1289 | BanRule( |
Dave Tapuska | 98199b61 | 2019-07-10 13:30:44 | [diff] [blame] | 1290 | 'GetDeferredQuitTaskForRunLoop', |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1291 | ( |
| 1292 | "GetDeferredQuitTaskForRunLoop shouldn't be needed, please contact", |
| 1293 | "gab@ if you found a use case where this is the only solution.", |
| 1294 | ), |
| 1295 | False, |
| 1296 | (), |
| 1297 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1298 | BanRule( |
Victor Costan | e48a2e8 | 2019-03-15 22:02:34 | [diff] [blame] | 1299 | 'sqlite3_initialize(', |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 1300 | ( |
Victor Costan | e48a2e8 | 2019-03-15 22:02:34 | [diff] [blame] | 1301 | 'Instead of calling sqlite3_initialize(), depend on //sql, ', |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 1302 | '#include "sql/initialize.h" and use sql::EnsureSqliteInitialized().', |
| 1303 | ), |
| 1304 | True, |
| 1305 | ( |
| 1306 | r'^sql/initialization\.(cc|h)$', |
| 1307 | r'^third_party/sqlite/.*\.(c|cc|h)$', |
| 1308 | ), |
| 1309 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1310 | BanRule( |
Austin Sullivan | d661ab5 | 2022-11-16 08:55:15 | [diff] [blame] | 1311 | 'CREATE VIEW', |
| 1312 | ( |
| 1313 | 'SQL views are disabled in Chromium feature code', |
| 1314 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/sql#no-views', |
| 1315 | ), |
| 1316 | True, |
| 1317 | ( |
| 1318 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1319 | # sql/ itself uses views when using memory-mapped IO. |
| 1320 | r'^sql/.*', |
| 1321 | # Various performance tools that do not build as part of Chrome. |
| 1322 | r'^infra/.*', |
| 1323 | r'^tools/perf.*', |
| 1324 | r'.*perfetto.*', |
| 1325 | ), |
| 1326 | ), |
| 1327 | BanRule( |
| 1328 | 'CREATE VIRTUAL TABLE', |
| 1329 | ( |
| 1330 | 'SQL virtual tables are disabled in Chromium feature code', |
| 1331 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/sql#no-virtual-tables', |
| 1332 | ), |
| 1333 | True, |
| 1334 | ( |
| 1335 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1336 | # sql/ itself uses virtual tables in the recovery module and tests. |
| 1337 | r'^sql/.*', |
| 1338 | # TODO(https://crbug.com/695592): Remove once WebSQL is deprecated. |
| 1339 | r'third_party/blink/web_tests/storage/websql/.*' |
| 1340 | # Various performance tools that do not build as part of Chrome. |
| 1341 | r'^tools/perf.*', |
| 1342 | r'.*perfetto.*', |
| 1343 | ), |
| 1344 | ), |
| 1345 | BanRule( |
Dave Tapuska | 98199b61 | 2019-07-10 13:30:44 | [diff] [blame] | 1346 | 'std::random_shuffle', |
tzik | 5de2157f | 2018-05-08 03:42:47 | [diff] [blame] | 1347 | ( |
| 1348 | 'std::random_shuffle is deprecated in C++14, and removed in C++17. Use', |
| 1349 | 'base::RandomShuffle instead.' |
| 1350 | ), |
| 1351 | True, |
| 1352 | (), |
| 1353 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1354 | BanRule( |
Javier Ernesto Flores Robles | 749e6c2 | 2018-10-08 09:36:24 | [diff] [blame] | 1355 | 'ios/web/public/test/http_server', |
| 1356 | ( |
| 1357 | 'web::HTTPserver is deprecated use net::EmbeddedTestServer instead.', |
| 1358 | ), |
| 1359 | False, |
| 1360 | (), |
| 1361 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1362 | BanRule( |
Robert Liao | 764c949 | 2019-01-24 18:46:28 | [diff] [blame] | 1363 | 'GetAddressOf', |
| 1364 | ( |
| 1365 | 'Improper use of Microsoft::WRL::ComPtr<T>::GetAddressOf() has been ', |
Xiaohan Wang | fb31b4cd | 2020-07-08 01:18:53 | [diff] [blame] | 1366 | 'implicated in a few leaks. ReleaseAndGetAddressOf() is safe but ', |
Joshua Berenhaus | 8b972ec | 2020-09-11 20:00:11 | [diff] [blame] | 1367 | 'operator& is generally recommended. So always use operator& instead. ', |
Xiaohan Wang | fb31b4cd | 2020-07-08 01:18:53 | [diff] [blame] | 1368 | 'See http://crbug.com/914910 for more conversion guidance.' |
Robert Liao | 764c949 | 2019-01-24 18:46:28 | [diff] [blame] | 1369 | ), |
| 1370 | True, |
| 1371 | (), |
| 1372 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1373 | BanRule( |
Ben Lewis | a951460 | 2019-04-29 17:53:05 | [diff] [blame] | 1374 | 'SHFileOperation', |
| 1375 | ( |
| 1376 | 'SHFileOperation was deprecated in Windows Vista, and there are less ', |
| 1377 | 'complex functions to achieve the same goals. Use IFileOperation for ', |
| 1378 | 'any esoteric actions instead.' |
| 1379 | ), |
| 1380 | True, |
| 1381 | (), |
| 1382 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1383 | BanRule( |
Cliff Smolinsky | 8195164 | 2019-04-30 21:39:51 | [diff] [blame] | 1384 | 'StringFromGUID2', |
| 1385 | ( |
| 1386 | 'StringFromGUID2 introduces an unnecessary dependency on ole32.dll.', |
Jan Wilken Dörrie | ec81592 | 2020-07-22 07:46:24 | [diff] [blame] | 1387 | 'Use base::win::WStringFromGUID instead.' |
Cliff Smolinsky | 8195164 | 2019-04-30 21:39:51 | [diff] [blame] | 1388 | ), |
| 1389 | True, |
| 1390 | ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1391 | r'/base/win/win_util_unittest.cc', |
Cliff Smolinsky | 8195164 | 2019-04-30 21:39:51 | [diff] [blame] | 1392 | ), |
| 1393 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1394 | BanRule( |
Cliff Smolinsky | 8195164 | 2019-04-30 21:39:51 | [diff] [blame] | 1395 | 'StringFromCLSID', |
| 1396 | ( |
| 1397 | 'StringFromCLSID introduces an unnecessary dependency on ole32.dll.', |
Jan Wilken Dörrie | ec81592 | 2020-07-22 07:46:24 | [diff] [blame] | 1398 | 'Use base::win::WStringFromGUID instead.' |
Cliff Smolinsky | 8195164 | 2019-04-30 21:39:51 | [diff] [blame] | 1399 | ), |
| 1400 | True, |
| 1401 | ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1402 | r'/base/win/win_util_unittest.cc', |
Cliff Smolinsky | 8195164 | 2019-04-30 21:39:51 | [diff] [blame] | 1403 | ), |
| 1404 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1405 | BanRule( |
Avi Drissman | 7382afa0 | 2019-04-29 23:27:13 | [diff] [blame] | 1406 | 'kCFAllocatorNull', |
| 1407 | ( |
| 1408 | 'The use of kCFAllocatorNull with the NoCopy creation of ', |
| 1409 | 'CoreFoundation types is prohibited.', |
| 1410 | ), |
| 1411 | True, |
| 1412 | (), |
| 1413 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1414 | BanRule( |
Oksana Zhuravlova | fd24777 | 2019-05-16 16:57:29 | [diff] [blame] | 1415 | 'mojo::ConvertTo', |
| 1416 | ( |
| 1417 | 'mojo::ConvertTo and TypeConverter are deprecated. Please consider', |
| 1418 | 'StructTraits / UnionTraits / EnumTraits / ArrayTraits / MapTraits /', |
| 1419 | 'StringTraits if you would like to convert between custom types and', |
| 1420 | 'the wire format of mojom types.' |
| 1421 | ), |
Oksana Zhuravlova | 1d3b59de | 2019-05-17 00:08:22 | [diff] [blame] | 1422 | False, |
Oksana Zhuravlova | fd24777 | 2019-05-16 16:57:29 | [diff] [blame] | 1423 | ( |
David Dorwin | 13dc48b | 2022-06-03 21:18:42 | [diff] [blame] | 1424 | r'^fuchsia_web/webengine/browser/url_request_rewrite_rules_manager\.cc$', |
| 1425 | r'^fuchsia_web/webengine/url_request_rewrite_type_converters\.cc$', |
Oksana Zhuravlova | fd24777 | 2019-05-16 16:57:29 | [diff] [blame] | 1426 | r'^third_party/blink/.*\.(cc|h)$', |
| 1427 | r'^content/renderer/.*\.(cc|h)$', |
| 1428 | ), |
| 1429 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1430 | BanRule( |
Oksana Zhuravlova | c8222d2 | 2019-12-19 19:21:16 | [diff] [blame] | 1431 | 'GetInterfaceProvider', |
| 1432 | ( |
| 1433 | 'InterfaceProvider is deprecated.', |
| 1434 | 'Please use ExecutionContext::GetBrowserInterfaceBroker and overrides', |
| 1435 | 'or Platform::GetBrowserInterfaceBroker.' |
| 1436 | ), |
| 1437 | False, |
| 1438 | (), |
| 1439 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1440 | BanRule( |
Robert Liao | 1d78df5 | 2019-11-11 20:02:01 | [diff] [blame] | 1441 | 'CComPtr', |
| 1442 | ( |
| 1443 | 'New code should use Microsoft::WRL::ComPtr from wrl/client.h as a ', |
| 1444 | 'replacement for CComPtr from ATL. See http://crbug.com/5027 for more ', |
| 1445 | 'details.' |
| 1446 | ), |
| 1447 | False, |
| 1448 | (), |
| 1449 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1450 | BanRule( |
Xiaohan Wang | 72bd2ba | 2020-02-18 21:38:20 | [diff] [blame] | 1451 | r'/\b(IFACE|STD)METHOD_?\(', |
| 1452 | ( |
| 1453 | 'IFACEMETHOD() and STDMETHOD() make code harder to format and read.', |
| 1454 | 'Instead, always use IFACEMETHODIMP in the declaration.' |
| 1455 | ), |
| 1456 | False, |
| 1457 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 1458 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1459 | BanRule( |
Allen Bauer | 53b43fb1 | 2020-03-12 17:21:47 | [diff] [blame] | 1460 | 'set_owned_by_client', |
| 1461 | ( |
| 1462 | 'set_owned_by_client is deprecated.', |
| 1463 | 'views::View already owns the child views by default. This introduces ', |
| 1464 | 'a competing ownership model which makes the code difficult to reason ', |
| 1465 | 'about. See http://crbug.com/1044687 for more details.' |
| 1466 | ), |
| 1467 | False, |
| 1468 | (), |
| 1469 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1470 | BanRule( |
Peter Boström | 7ff4152 | 2021-07-29 03:43:27 | [diff] [blame] | 1471 | 'RemoveAllChildViewsWithoutDeleting', |
| 1472 | ( |
| 1473 | 'RemoveAllChildViewsWithoutDeleting is deprecated.', |
| 1474 | 'This method is deemed dangerous as, unless raw pointers are re-added,', |
| 1475 | 'calls to this method introduce memory leaks.' |
| 1476 | ), |
| 1477 | False, |
| 1478 | (), |
| 1479 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1480 | BanRule( |
Eric Seckler | be6f48d | 2020-05-06 18:09:12 | [diff] [blame] | 1481 | r'/\bTRACE_EVENT_ASYNC_', |
| 1482 | ( |
| 1483 | 'Please use TRACE_EVENT_NESTABLE_ASYNC_.. macros instead', |
| 1484 | 'of TRACE_EVENT_ASYNC_.. (crbug.com/1038710).', |
| 1485 | ), |
| 1486 | False, |
| 1487 | ( |
| 1488 | r'^base/trace_event/.*', |
| 1489 | r'^base/tracing/.*', |
| 1490 | ), |
| 1491 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1492 | BanRule( |
Aditya Kushwah | 5a286b7 | 2022-02-10 04:54:43 | [diff] [blame] | 1493 | r'/\bbase::debug::DumpWithoutCrashingUnthrottled[(][)]', |
| 1494 | ( |
| 1495 | 'base::debug::DumpWithoutCrashingUnthrottled() does not throttle', |
| 1496 | 'dumps and may spam crash reports. Consider if the throttled', |
| 1497 | 'variants suffice instead.', |
| 1498 | ), |
| 1499 | False, |
| 1500 | (), |
| 1501 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1502 | BanRule( |
Robert Liao | 22f66a5 | 2021-04-10 00:57:52 | [diff] [blame] | 1503 | 'RoInitialize', |
| 1504 | ( |
Robert Liao | 4801892 | 2021-04-16 23:03:02 | [diff] [blame] | 1505 | 'Improper use of [base::win]::RoInitialize() has been implicated in a ', |
Robert Liao | 22f66a5 | 2021-04-10 00:57:52 | [diff] [blame] | 1506 | 'few COM initialization leaks. Use base::win::ScopedWinrtInitializer ', |
| 1507 | 'instead. See http://crbug.com/1197722 for more information.' |
| 1508 | ), |
| 1509 | True, |
Robert Liao | 4801892 | 2021-04-16 23:03:02 | [diff] [blame] | 1510 | ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 1511 | r'^base/win/scoped_winrt_initializer\.cc$', |
Danil Chapovalov | 0769438 | 2023-05-24 17:55:21 | [diff] [blame] | 1512 | r'^third_party/abseil-cpp/absl/.*', |
Robert Liao | 4801892 | 2021-04-16 23:03:02 | [diff] [blame] | 1513 | ), |
Robert Liao | 22f66a5 | 2021-04-10 00:57:52 | [diff] [blame] | 1514 | ), |
Patrick Monette | c343bb98 | 2022-06-01 17:18:45 | [diff] [blame] | 1515 | BanRule( |
| 1516 | r'base::Watchdog', |
| 1517 | ( |
| 1518 | 'base::Watchdog is deprecated because it creates its own thread.', |
| 1519 | 'Instead, manually start a timer on a SequencedTaskRunner.', |
| 1520 | ), |
| 1521 | False, |
| 1522 | (), |
| 1523 | ), |
Andrew Rayskiy | 04a51ce | 2022-06-07 11:47:09 | [diff] [blame] | 1524 | BanRule( |
| 1525 | 'base::Passed', |
| 1526 | ( |
| 1527 | 'Do not use base::Passed. It is a legacy helper for capturing ', |
| 1528 | 'move-only types with base::BindRepeating, but invoking the ', |
| 1529 | 'resulting RepeatingCallback moves the captured value out of ', |
| 1530 | 'the callback storage, and subsequent invocations may pass the ', |
| 1531 | 'value in a valid but undefined state. Prefer base::BindOnce().', |
| 1532 | 'See http://crbug.com/1326449 for context.' |
| 1533 | ), |
| 1534 | False, |
Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 1535 | ( |
| 1536 | # False positive, but it is also fine to let bind internals reference |
| 1537 | # base::Passed. |
Daniel Cheng | cd23b8b | 2022-09-16 17:16:24 | [diff] [blame] | 1538 | r'^base[\\/]functional[\\/]bind\.h', |
Daniel Cheng | 91f6fbaf | 2022-09-16 12:07:48 | [diff] [blame] | 1539 | r'^base[\\/]functional[\\/]bind_internal\.h', |
| 1540 | ), |
Andrew Rayskiy | 04a51ce | 2022-06-07 11:47:09 | [diff] [blame] | 1541 | ), |
Daniel Cheng | 2248b33 | 2022-07-27 06:16:59 | [diff] [blame] | 1542 | BanRule( |
Daniel Cheng | ba3bc2e | 2022-10-03 02:45:43 | [diff] [blame] | 1543 | r'base::Feature k', |
| 1544 | ( |
| 1545 | 'Please use BASE_DECLARE_FEATURE() or BASE_FEATURE() instead of ', |
| 1546 | 'directly declaring/defining features.' |
| 1547 | ), |
| 1548 | True, |
| 1549 | [ |
| 1550 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1551 | ], |
| 1552 | ), |
Robert Ogden | 92101dcb | 2022-10-19 23:49:36 | [diff] [blame] | 1553 | BanRule( |
Arthur Sonzogni | 1da65fa | 2023-03-27 16:01:52 | [diff] [blame] | 1554 | r'/\bchartorune\b', |
Robert Ogden | 92101dcb | 2022-10-19 23:49:36 | [diff] [blame] | 1555 | ( |
| 1556 | 'chartorune is not memory-safe, unless you can guarantee the input ', |
| 1557 | 'string is always null-terminated. Otherwise, please use charntorune ', |
| 1558 | 'from libphonenumber instead.' |
| 1559 | ), |
| 1560 | True, |
| 1561 | [ |
| 1562 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1563 | # Exceptions to this rule should have a fuzzer. |
| 1564 | ], |
| 1565 | ), |
Arthur Sonzogni | 1da65fa | 2023-03-27 16:01:52 | [diff] [blame] | 1566 | BanRule( |
| 1567 | r'/\b#include "base/atomicops\.h"\b', |
| 1568 | ( |
| 1569 | 'Do not use base::subtle atomics, but std::atomic, which are simpler ' |
| 1570 | 'to use, have better understood, clearer and richer semantics, and are ' |
| 1571 | 'harder to mis-use. See details in base/atomicops.h.', |
| 1572 | ), |
| 1573 | False, |
| 1574 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Benoit Lize | 79cf059 | 2023-01-27 10:01:57 | [diff] [blame] | 1575 | ), |
Arthur Sonzogni | 60348572e | 2023-04-07 10:22:52 | [diff] [blame] | 1576 | BanRule( |
| 1577 | r'CrossThreadPersistent<', |
| 1578 | ( |
| 1579 | 'Do not use blink::CrossThreadPersistent, but ' |
| 1580 | 'blink::CrossThreadHandle. It is harder to mis-use.', |
| 1581 | 'More info: ' |
| 1582 | 'https://docs.google.com/document/d/1GIT0ysdQ84sGhIo1r9EscF_fFt93lmNVM_q4vvHj2FQ/edit#heading=h.3e4d6y61tgs', |
| 1583 | 'Please contact platform-architecture-dev@ before adding new instances.' |
| 1584 | ), |
| 1585 | False, |
| 1586 | [] |
| 1587 | ), |
| 1588 | BanRule( |
| 1589 | r'CrossThreadWeakPersistent<', |
| 1590 | ( |
| 1591 | 'Do not use blink::CrossThreadWeakPersistent, but ' |
| 1592 | 'blink::CrossThreadWeakHandle. It is harder to mis-use.', |
| 1593 | 'More info: ' |
| 1594 | 'https://docs.google.com/document/d/1GIT0ysdQ84sGhIo1r9EscF_fFt93lmNVM_q4vvHj2FQ/edit#heading=h.3e4d6y61tgs', |
| 1595 | 'Please contact platform-architecture-dev@ before adding new instances.' |
| 1596 | ), |
| 1597 | False, |
| 1598 | [] |
| 1599 | ), |
Avi Drissman | 491617c | 2023-04-13 17:33:15 | [diff] [blame] | 1600 | BanRule( |
| 1601 | r'objc/objc.h', |
| 1602 | ( |
| 1603 | 'Do not include <objc/objc.h>. It defines away ARC lifetime ' |
| 1604 | 'annotations, and is thus dangerous.', |
| 1605 | 'Please use the pimpl pattern; search for `ObjCStorage` for examples.', |
| 1606 | 'For further reading on how to safely mix C++ and Obj-C, see', |
| 1607 | 'https://chromium.googlesource.com/chromium/src/+/main/docs/mac/mixing_cpp_and_objc.md' |
| 1608 | ), |
| 1609 | True, |
| 1610 | [] |
| 1611 | ), |
Grace Park | 8d59b54b | 2023-04-26 17:53:35 | [diff] [blame] | 1612 | BanRule( |
| 1613 | r'/#include <filesystem>', |
| 1614 | ( |
| 1615 | 'libc++ <filesystem> is banned per the Google C++ styleguide.', |
| 1616 | ), |
| 1617 | True, |
| 1618 | # This fuzzing framework is a standalone open source project and |
| 1619 | # cannot rely on Chromium base. |
| 1620 | (r'third_party/centipede'), |
| 1621 | ), |
Daniel Cheng | 72153e0 | 2023-05-18 21:18:14 | [diff] [blame] | 1622 | BanRule( |
| 1623 | r'TopDocument()', |
| 1624 | ( |
| 1625 | 'TopDocument() does not work correctly with out-of-process iframes. ' |
| 1626 | 'Please do not introduce new uses.', |
| 1627 | ), |
| 1628 | True, |
| 1629 | ( |
| 1630 | # TODO(crbug.com/617677): Remove all remaining uses. |
| 1631 | r'^third_party/blink/renderer/core/dom/document\.cc', |
| 1632 | r'^third_party/blink/renderer/core/dom/document\.h', |
| 1633 | r'^third_party/blink/renderer/core/dom/element\.cc', |
| 1634 | r'^third_party/blink/renderer/core/exported/web_disallow_transition_scope_test\.cc', |
| 1635 | r'^third_party/blink/renderer/core/exported/web_document_test\.cc', |
Daniel Cheng | 72153e0 | 2023-05-18 21:18:14 | [diff] [blame] | 1636 | r'^third_party/blink/renderer/core/html/html_anchor_element\.cc', |
| 1637 | r'^third_party/blink/renderer/core/html/html_dialog_element\.cc', |
| 1638 | r'^third_party/blink/renderer/core/html/html_element\.cc', |
| 1639 | r'^third_party/blink/renderer/core/html/html_frame_owner_element\.cc', |
| 1640 | r'^third_party/blink/renderer/core/html/media/video_wake_lock\.cc', |
| 1641 | r'^third_party/blink/renderer/core/loader/anchor_element_interaction_tracker\.cc', |
| 1642 | r'^third_party/blink/renderer/core/page/scrolling/root_scroller_controller\.cc', |
| 1643 | r'^third_party/blink/renderer/core/page/scrolling/top_document_root_scroller_controller\.cc', |
| 1644 | r'^third_party/blink/renderer/core/page/scrolling/top_document_root_scroller_controller\.h', |
| 1645 | r'^third_party/blink/renderer/core/script/classic_pending_script\.cc', |
| 1646 | r'^third_party/blink/renderer/core/script/script_loader\.cc', |
| 1647 | ), |
| 1648 | ), |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 1649 | ) |
| 1650 | |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 1651 | _BANNED_MOJOM_PATTERNS : Sequence[BanRule] = ( |
| 1652 | BanRule( |
| 1653 | 'handle<shared_buffer>', |
| 1654 | ( |
| 1655 | 'Please use one of the more specific shared memory types instead:', |
| 1656 | ' mojo_base.mojom.ReadOnlySharedMemoryRegion', |
| 1657 | ' mojo_base.mojom.WritableSharedMemoryRegion', |
| 1658 | ' mojo_base.mojom.UnsafeSharedMemoryRegion', |
| 1659 | ), |
| 1660 | True, |
| 1661 | ), |
| 1662 | ) |
| 1663 | |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 1664 | _IPC_ENUM_TRAITS_DEPRECATED = ( |
| 1665 | 'You are using IPC_ENUM_TRAITS() in your code. It has been deprecated.\n' |
Vaclav Brozek | d5de76a | 2018-03-17 07:57:50 | [diff] [blame] | 1666 | 'See http://www.chromium.org/Home/chromium-security/education/' |
| 1667 | 'security-tips-for-ipc') |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 1668 | |
Stephen Martinis | 97a39414 | 2018-06-07 23:06:05 | [diff] [blame] | 1669 | _LONG_PATH_ERROR = ( |
| 1670 | 'Some files included in this CL have file names that are too long (> 200' |
| 1671 | ' characters). If committed, these files will cause issues on Windows. See' |
| 1672 | ' https://crbug.com/612667 for more details.' |
| 1673 | ) |
| 1674 | |
Shenghua Zhang | bfaa38b8 | 2017-11-16 21:58:02 | [diff] [blame] | 1675 | _JAVA_MULTIPLE_DEFINITION_EXCLUDED_PATHS = [ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 1676 | r".*/AppHooksImpl\.java", |
| 1677 | r".*/BuildHooksAndroidImpl\.java", |
| 1678 | r".*/LicenseContentProvider\.java", |
| 1679 | r".*/PlatformServiceBridgeImpl.java", |
| 1680 | r".*chrome/android/feed/dummy/.*\.java", |
Shenghua Zhang | bfaa38b8 | 2017-11-16 21:58:02 | [diff] [blame] | 1681 | ] |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 1682 | |
Mohamed Heikal | d048240a | 2019-11-12 16:57:37 | [diff] [blame] | 1683 | # List of image extensions that are used as resources in chromium. |
| 1684 | _IMAGE_EXTENSIONS = ['.svg', '.png', '.webp'] |
| 1685 | |
Sean Kau | 46e29bc | 2017-08-28 16:31:16 | [diff] [blame] | 1686 | # These paths contain test data and other known invalid JSON files. |
Erik Staab | 2dd72b1 | 2020-04-16 15:03:40 | [diff] [blame] | 1687 | _KNOWN_TEST_DATA_AND_INVALID_JSON_FILE_PATTERNS = [ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 1688 | r'test/data/', |
| 1689 | r'testing/buildbot/', |
| 1690 | r'^components/policy/resources/policy_templates\.json$', |
| 1691 | r'^third_party/protobuf/', |
Camillo Bruni | 1411a35 | 2023-05-24 12:39:03 | [diff] [blame] | 1692 | r'^third_party/blink/perf_tests/speedometer.*/resources/todomvc/learn\.json', |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 1693 | r'^third_party/blink/renderer/devtools/protocol\.json$', |
| 1694 | r'^third_party/blink/web_tests/external/wpt/', |
| 1695 | r'^tools/perf/', |
| 1696 | r'^tools/traceline/svgui/startup-release.json', |
Daniel Cheng | 2d4c2d19 | 2022-07-01 01:38:31 | [diff] [blame] | 1697 | # vscode configuration files allow comments |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 1698 | r'^tools/vscode/', |
Sean Kau | 46e29bc | 2017-08-28 16:31:16 | [diff] [blame] | 1699 | ] |
| 1700 | |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 1701 | # These are not checked on the public chromium-presubmit trybot. |
| 1702 | # Add files here that rely on .py files that exists only for target_os="android" |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 1703 | # checkouts. |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 1704 | _ANDROID_SPECIFIC_PYDEPS_FILES = [ |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 1705 | 'chrome/android/features/create_stripped_java_factory.pydeps', |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 1706 | ] |
| 1707 | |
| 1708 | |
| 1709 | _GENERIC_PYDEPS_FILES = [ |
Bruce Dawson | 853b739e6 | 2022-05-03 23:03:10 | [diff] [blame] | 1710 | 'android_webview/test/components/run_webview_component_smoketest.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 1711 | 'android_webview/tools/run_cts.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1712 | 'base/android/jni_generator/jni_generator.pydeps', |
| 1713 | 'base/android/jni_generator/jni_registration_generator.pydeps', |
Andrew Grieve | 4c4cede | 2020-11-20 22:09:36 | [diff] [blame] | 1714 | 'build/android/apk_operations.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 1715 | 'build/android/devil_chromium.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1716 | 'build/android/gyp/aar.pydeps', |
| 1717 | 'build/android/gyp/aidl.pydeps', |
Tibor Goldschwendt | 0bef2d7a | 2019-10-24 21:19:27 | [diff] [blame] | 1718 | 'build/android/gyp/allot_native_libraries.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1719 | 'build/android/gyp/apkbuilder.pydeps', |
Andrew Grieve | a417ad30 | 2019-02-06 19:54:38 | [diff] [blame] | 1720 | 'build/android/gyp/assert_static_initializers.pydeps', |
Mohamed Heikal | 133e1f2 | 2023-04-18 20:04:37 | [diff] [blame] | 1721 | 'build/android/gyp/binary_baseline_profile.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1722 | 'build/android/gyp/bytecode_processor.pydeps', |
Robbie McElrath | 360e54d | 2020-11-12 20:38:02 | [diff] [blame] | 1723 | 'build/android/gyp/bytecode_rewriter.pydeps', |
Mohamed Heikal | 6305bcc | 2021-03-15 15:34:22 | [diff] [blame] | 1724 | 'build/android/gyp/check_flag_expectations.pydeps', |
Andrew Grieve | 8d083ea | 2019-12-13 06:49:11 | [diff] [blame] | 1725 | 'build/android/gyp/compile_java.pydeps', |
Peter Wen | eaa963f | 2023-01-20 19:40:30 | [diff] [blame] | 1726 | 'build/android/gyp/compile_kt.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1727 | 'build/android/gyp/compile_resources.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1728 | 'build/android/gyp/copy_ex.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1729 | 'build/android/gyp/create_apk_operations_script.pydeps', |
Andrew Grieve | 8d083ea | 2019-12-13 06:49:11 | [diff] [blame] | 1730 | 'build/android/gyp/create_app_bundle.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 1731 | 'build/android/gyp/create_app_bundle_apks.pydeps', |
| 1732 | 'build/android/gyp/create_bundle_wrapper_script.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1733 | 'build/android/gyp/create_java_binary_script.pydeps', |
Mohamed Heikal | adbe4e48 | 2020-07-09 19:25:12 | [diff] [blame] | 1734 | 'build/android/gyp/create_r_java.pydeps', |
Mohamed Heikal | 8cd763a5 | 2021-02-01 23:32:09 | [diff] [blame] | 1735 | 'build/android/gyp/create_r_txt.pydeps', |
Andrew Grieve | b838d83 | 2019-02-11 16:55:22 | [diff] [blame] | 1736 | 'build/android/gyp/create_size_info_files.pydeps', |
Peter Wen | e6e017e | 2022-07-27 21:40:40 | [diff] [blame] | 1737 | 'build/android/gyp/create_test_apk_wrapper_script.pydeps', |
Andrew Grieve | 5a01ad3 | 2020-06-25 18:06:00 | [diff] [blame] | 1738 | 'build/android/gyp/create_ui_locale_resources.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1739 | 'build/android/gyp/dex.pydeps', |
| 1740 | 'build/android/gyp/dist_aar.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1741 | 'build/android/gyp/filter_zip.pydeps', |
Mohamed Heikal | 21e1994b | 2021-11-12 21:37:21 | [diff] [blame] | 1742 | 'build/android/gyp/flatc_java.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1743 | 'build/android/gyp/gcc_preprocess.pydeps', |
Christopher Grant | 99e0e2006 | 2018-11-21 21:22:36 | [diff] [blame] | 1744 | 'build/android/gyp/generate_linker_version_script.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1745 | 'build/android/gyp/ijar.pydeps', |
Yun Liu | eb4075ddf | 2019-05-13 19:47:58 | [diff] [blame] | 1746 | 'build/android/gyp/jacoco_instr.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1747 | 'build/android/gyp/java_cpp_enum.pydeps', |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 1748 | 'build/android/gyp/java_cpp_features.pydeps', |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 1749 | 'build/android/gyp/java_cpp_strings.pydeps', |
Andrew Grieve | 0945791 | 2021-04-27 15:22:47 | [diff] [blame] | 1750 | 'build/android/gyp/java_google_api_keys.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 1751 | 'build/android/gyp/jinja_template.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1752 | 'build/android/gyp/lint.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1753 | 'build/android/gyp/merge_manifest.pydeps', |
Bruce Dawson | 853b739e6 | 2022-05-03 23:03:10 | [diff] [blame] | 1754 | 'build/android/gyp/optimize_resources.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1755 | 'build/android/gyp/prepare_resources.pydeps', |
Mohamed Heikal | f85138b | 2020-10-06 15:43:22 | [diff] [blame] | 1756 | 'build/android/gyp/process_native_prebuilt.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1757 | 'build/android/gyp/proguard.pydeps', |
Andrew Grieve | e3a775ab | 2022-05-16 15:59:22 | [diff] [blame] | 1758 | 'build/android/gyp/system_image_apks.pydeps', |
Bruce Dawson | 853b739e6 | 2022-05-03 23:03:10 | [diff] [blame] | 1759 | 'build/android/gyp/trace_event_bytecode_rewriter.pydeps', |
Peter Wen | 578730b | 2020-03-19 19:55:46 | [diff] [blame] | 1760 | 'build/android/gyp/turbine.pydeps', |
Mohamed Heikal | 246710c | 2021-06-14 15:34:30 | [diff] [blame] | 1761 | 'build/android/gyp/unused_resources.pydeps', |
Eric Stevenson | a82cf608 | 2019-07-24 14:35:24 | [diff] [blame] | 1762 | 'build/android/gyp/validate_static_library_dex_references.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1763 | 'build/android/gyp/write_build_config.pydeps', |
Tibor Goldschwendt | c4caae9 | 2019-07-12 00:33:46 | [diff] [blame] | 1764 | 'build/android/gyp/write_native_libraries_java.pydeps', |
Andrew Grieve | 9ff1779 | 2018-11-30 04:55:56 | [diff] [blame] | 1765 | 'build/android/gyp/zip.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1766 | 'build/android/incremental_install/generate_android_manifest.pydeps', |
| 1767 | 'build/android/incremental_install/write_installer_json.pydeps', |
Stephanie Kim | 392913b45 | 2022-06-15 17:25:32 | [diff] [blame] | 1768 | 'build/android/pylib/results/presentation/test_results_presentation.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 1769 | 'build/android/resource_sizes.pydeps', |
| 1770 | 'build/android/test_runner.pydeps', |
| 1771 | 'build/android/test_wrapper/logdog_wrapper.pydeps', |
Samuel Huang | e65eb3f1 | 2020-08-14 19:04:36 | [diff] [blame] | 1772 | 'build/lacros/lacros_resource_sizes.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 1773 | 'build/protoc_java.pydeps', |
Peter Kotwicz | 64667b0 | 2020-10-18 06:43:32 | [diff] [blame] | 1774 | 'chrome/android/monochrome/scripts/monochrome_python_tests.pydeps', |
Peter Wen | efb56c7 | 2020-06-04 15:12:27 | [diff] [blame] | 1775 | 'chrome/test/chromedriver/log_replay/client_replay_unittest.pydeps', |
| 1776 | 'chrome/test/chromedriver/test/run_py_tests.pydeps', |
Junbo Ke | dcd3a45 | 2021-03-19 17:55:04 | [diff] [blame] | 1777 | 'chromecast/resource_sizes/chromecast_resource_sizes.pydeps', |
Andrew Grieve | 5a01ad3 | 2020-06-25 18:06:00 | [diff] [blame] | 1778 | 'components/cronet/tools/generate_javadoc.pydeps', |
| 1779 | 'components/cronet/tools/jar_src.pydeps', |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 1780 | 'components/module_installer/android/module_desc_java.pydeps', |
Andrew Grieve | 5a01ad3 | 2020-06-25 18:06:00 | [diff] [blame] | 1781 | 'content/public/android/generate_child_service.pydeps', |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 1782 | 'net/tools/testserver/testserver.pydeps', |
Peter Kotwicz | 3c339f3 | 2020-10-19 19:59:18 | [diff] [blame] | 1783 | 'testing/scripts/run_isolated_script_test.pydeps', |
Stephanie Kim | c94072c | 2022-03-22 22:31:41 | [diff] [blame] | 1784 | 'testing/merge_scripts/standard_isolated_script_merge.pydeps', |
| 1785 | 'testing/merge_scripts/standard_gtest_merge.pydeps', |
| 1786 | 'testing/merge_scripts/code_coverage/merge_results.pydeps', |
| 1787 | 'testing/merge_scripts/code_coverage/merge_steps.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 1788 | 'third_party/android_platform/development/scripts/stack.pydeps', |
Hitoshi Yoshida | 0f228c4 | 2019-08-07 09:37:42 | [diff] [blame] | 1789 | 'third_party/blink/renderer/bindings/scripts/build_web_idl_database.pydeps', |
Yuki Shiino | 38eeaad1 | 2022-08-11 06:40:25 | [diff] [blame] | 1790 | 'third_party/blink/renderer/bindings/scripts/check_generated_file_list.pydeps', |
Hitoshi Yoshida | 0f228c4 | 2019-08-07 09:37:42 | [diff] [blame] | 1791 | 'third_party/blink/renderer/bindings/scripts/collect_idl_files.pydeps', |
Yuki Shiino | e7827aa | 2019-09-13 12:26:13 | [diff] [blame] | 1792 | 'third_party/blink/renderer/bindings/scripts/generate_bindings.pydeps', |
Canon Mukai | f32f8f59 | 2021-04-23 18:56:50 | [diff] [blame] | 1793 | 'third_party/blink/renderer/bindings/scripts/validate_web_idl.pydeps', |
Stephanie Kim | c94072c | 2022-03-22 22:31:41 | [diff] [blame] | 1794 | 'third_party/blink/tools/blinkpy/web_tests/merge_results.pydeps', |
| 1795 | 'third_party/blink/tools/merge_web_test_results.pydeps', |
John Budorick | bc3571aa | 2019-04-25 02:20:06 | [diff] [blame] | 1796 | 'tools/binary_size/sizes.pydeps', |
Andrew Grieve | a7f1ee90 | 2018-05-18 16:17:22 | [diff] [blame] | 1797 | 'tools/binary_size/supersize.pydeps', |
Ben Pastene | 028104a | 2022-08-10 19:17:45 | [diff] [blame] | 1798 | 'tools/perf/process_perf_results.pydeps', |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 1799 | ] |
| 1800 | |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 1801 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 1802 | _ALL_PYDEPS_FILES = _ANDROID_SPECIFIC_PYDEPS_FILES + _GENERIC_PYDEPS_FILES |
| 1803 | |
| 1804 | |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 1805 | # Bypass the AUTHORS check for these accounts. |
| 1806 | _KNOWN_ROBOTS = set( |
Sergiy Byelozyorov | 47158a5 | 2018-06-13 22:38:59 | [diff] [blame] | 1807 | ) | set('%s@appspot.gserviceaccount.com' % s for s in ('findit-for-me',) |
Achuith Bhandarkar | 3590556 | 2018-07-25 19:28:45 | [diff] [blame] | 1808 | ) | set('%s@developer.gserviceaccount.com' % s for s in ('3su6n15k.default',) |
Sergiy Byelozyorov | 47158a5 | 2018-06-13 22:38:59 | [diff] [blame] | 1809 | ) | set('%s@chops-service-accounts.iam.gserviceaccount.com' % s |
smut | de79705 | 2019-12-04 02:03:52 | [diff] [blame] | 1810 | for s in ('bling-autoroll-builder', 'v8-ci-autoroll-builder', |
Sven Zheng | f7abd31d | 2021-08-09 19:06:23 | [diff] [blame] | 1811 | 'wpt-autoroller', 'chrome-weblayer-builder', |
Garrett Beaty | 4d4fcf6 | 2021-11-24 17:57:47 | [diff] [blame] | 1812 | 'lacros-version-skew-roller', 'skylab-test-cros-roller', |
Sven Zheng | 722960ba | 2022-07-18 16:40:46 | [diff] [blame] | 1813 | 'infra-try-recipes-tester', 'lacros-tracking-roller', |
Brian Sheedy | 1c951e6 | 2022-10-27 01:16:18 | [diff] [blame] | 1814 | 'lacros-sdk-version-roller', 'chrome-automated-expectation', |
Stephanie Kim | b49bdd24 | 2023-04-28 16:46:04 | [diff] [blame] | 1815 | 'chromium-automated-expectation', 'chrome-branch-day', |
| 1816 | 'chromium-autosharder') |
Eric Boren | 835d71f | 2018-09-07 21:09:04 | [diff] [blame] | 1817 | ) | set('%s@skia-public.iam.gserviceaccount.com' % s |
Eric Boren | 66150e5 | 2020-01-08 11:20:27 | [diff] [blame] | 1818 | for s in ('chromium-autoroll', 'chromium-release-autoroll') |
Eric Boren | 835d71f | 2018-09-07 21:09:04 | [diff] [blame] | 1819 | ) | set('%s@skia-corp.google.com.iam.gserviceaccount.com' % s |
Yulan Lin | eb0cfba | 2021-04-09 18:43:16 | [diff] [blame] | 1820 | for s in ('chromium-internal-autoroll',) |
| 1821 | ) | set('%s@owners-cleanup-prod.google.com.iam.gserviceaccount.com' % s |
Chong Gu | b277e34 | 2022-10-15 03:30:55 | [diff] [blame] | 1822 | for s in ('swarming-tasks',) |
| 1823 | ) | set('%s@fuchsia-infra.iam.gserviceaccount.com' % s |
| 1824 | for s in ('global-integration-try-builder', |
| 1825 | 'global-integration-ci-builder')) |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 1826 | |
Matt Stark | 6ef0887 | 2021-07-29 01:21:46 | [diff] [blame] | 1827 | _INVALID_GRD_FILE_LINE = [ |
| 1828 | (r'<file lang=.* path=.*', 'Path should come before lang in GRD files.') |
| 1829 | ] |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 1830 | |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 1831 | def _IsCPlusPlusFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1832 | """Returns True if this file contains C++-like code (and not Python, |
| 1833 | Go, Java, MarkDown, ...)""" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 1834 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1835 | ext = input_api.os_path.splitext(file_path)[1] |
| 1836 | # This list is compatible with CppChecker.IsCppFile but we should |
| 1837 | # consider adding ".c" to it. If we do that we can use this function |
| 1838 | # at more places in the code. |
| 1839 | return ext in ( |
| 1840 | '.h', |
| 1841 | '.cc', |
| 1842 | '.cpp', |
| 1843 | '.m', |
| 1844 | '.mm', |
| 1845 | ) |
| 1846 | |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 1847 | |
| 1848 | def _IsCPlusPlusHeaderFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1849 | return input_api.os_path.splitext(file_path)[1] == ".h" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 1850 | |
| 1851 | |
| 1852 | def _IsJavaFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1853 | return input_api.os_path.splitext(file_path)[1] == ".java" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 1854 | |
| 1855 | |
| 1856 | def _IsProtoFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1857 | return input_api.os_path.splitext(file_path)[1] == ".proto" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 1858 | |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 1859 | |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 1860 | def _IsXmlOrGrdFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1861 | ext = input_api.os_path.splitext(file_path)[1] |
| 1862 | return ext in ('.grd', '.xml') |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 1863 | |
| 1864 | |
Sven Zheng | 76a79ea | 2022-12-21 21:25:24 | [diff] [blame] | 1865 | def _IsMojomFile(input_api, file_path): |
| 1866 | return input_api.os_path.splitext(file_path)[1] == ".mojom" |
| 1867 | |
| 1868 | |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 1869 | def CheckNoUpstreamDepsOnClank(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1870 | """Prevent additions of dependencies from the upstream repo on //clank.""" |
| 1871 | # clank can depend on clank |
| 1872 | if input_api.change.RepositoryRoot().endswith('clank'): |
| 1873 | return [] |
| 1874 | build_file_patterns = [ |
| 1875 | r'(.+/)?BUILD\.gn', |
| 1876 | r'.+\.gni', |
| 1877 | ] |
| 1878 | excluded_files = [r'build[/\\]config[/\\]android[/\\]config\.gni'] |
| 1879 | bad_pattern = input_api.re.compile(r'^[^#]*//clank') |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 1880 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1881 | error_message = 'Disallowed import on //clank in an upstream build file:' |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 1882 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1883 | def FilterFile(affected_file): |
| 1884 | return input_api.FilterSourceFile(affected_file, |
| 1885 | files_to_check=build_file_patterns, |
| 1886 | files_to_skip=excluded_files) |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 1887 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1888 | problems = [] |
| 1889 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 1890 | local_path = f.LocalPath() |
| 1891 | for line_number, line in f.ChangedContents(): |
| 1892 | if (bad_pattern.search(line)): |
| 1893 | problems.append('%s:%d\n %s' % |
| 1894 | (local_path, line_number, line.strip())) |
| 1895 | if problems: |
| 1896 | return [output_api.PresubmitPromptOrNotify(error_message, problems)] |
| 1897 | else: |
| 1898 | return [] |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 1899 | |
| 1900 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 1901 | def CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1902 | """Attempts to prevent use of functions intended only for testing in |
| 1903 | non-testing code. For now this is just a best-effort implementation |
| 1904 | that ignores header files and may have some false positives. A |
| 1905 | better implementation would probably need a proper C++ parser. |
| 1906 | """ |
| 1907 | # We only scan .cc files and the like, as the declaration of |
| 1908 | # for-testing functions in header files are hard to distinguish from |
| 1909 | # calls to such functions without a proper C++ parser. |
| 1910 | file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS] |
joi@chromium.org | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 1911 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1912 | base_function_pattern = r'[ :]test::[^\s]+|ForTest(s|ing)?|for_test(s|ing)?' |
| 1913 | inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % |
| 1914 | base_function_pattern) |
| 1915 | comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern) |
| 1916 | allowlist_pattern = input_api.re.compile(r'// IN-TEST$') |
| 1917 | exclusion_pattern = input_api.re.compile( |
| 1918 | r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % |
| 1919 | (base_function_pattern, base_function_pattern)) |
| 1920 | # Avoid a false positive in this case, where the method name, the ::, and |
| 1921 | # the closing { are all on different lines due to line wrapping. |
| 1922 | # HelperClassForTesting:: |
| 1923 | # HelperClassForTesting( |
| 1924 | # args) |
| 1925 | # : member(0) {} |
| 1926 | method_defn_pattern = input_api.re.compile(r'[A-Za-z0-9_]+::$') |
joi@chromium.org | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 1927 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1928 | def FilterFile(affected_file): |
| 1929 | files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 1930 | input_api.DEFAULT_FILES_TO_SKIP) |
| 1931 | return input_api.FilterSourceFile( |
| 1932 | affected_file, |
| 1933 | files_to_check=file_inclusion_pattern, |
| 1934 | files_to_skip=files_to_skip) |
joi@chromium.org | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 1935 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1936 | problems = [] |
| 1937 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 1938 | local_path = f.LocalPath() |
| 1939 | in_method_defn = False |
| 1940 | for line_number, line in f.ChangedContents(): |
| 1941 | if (inclusion_pattern.search(line) |
| 1942 | and not comment_pattern.search(line) |
| 1943 | and not exclusion_pattern.search(line) |
| 1944 | and not allowlist_pattern.search(line) |
| 1945 | and not in_method_defn): |
| 1946 | problems.append('%s:%d\n %s' % |
| 1947 | (local_path, line_number, line.strip())) |
| 1948 | in_method_defn = method_defn_pattern.search(line) |
joi@chromium.org | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 1949 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1950 | if problems: |
| 1951 | return [ |
| 1952 | output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems) |
| 1953 | ] |
| 1954 | else: |
| 1955 | return [] |
joi@chromium.org | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 1956 | |
| 1957 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 1958 | def CheckNoProductionCodeUsingTestOnlyFunctionsJava(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1959 | """This is a simplified version of |
| 1960 | CheckNoProductionCodeUsingTestOnlyFunctions for Java files. |
| 1961 | """ |
| 1962 | javadoc_start_re = input_api.re.compile(r'^\s*/\*\*') |
| 1963 | javadoc_end_re = input_api.re.compile(r'^\s*\*/') |
| 1964 | name_pattern = r'ForTest(s|ing)?' |
| 1965 | # Describes an occurrence of "ForTest*" inside a // comment. |
| 1966 | comment_re = input_api.re.compile(r'//.*%s' % name_pattern) |
| 1967 | # Describes @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED) |
| 1968 | annotation_re = input_api.re.compile(r'@VisibleForTesting\(') |
| 1969 | # Catch calls. |
| 1970 | inclusion_re = input_api.re.compile(r'(%s)\s*\(' % name_pattern) |
| 1971 | # Ignore definitions. (Comments are ignored separately.) |
| 1972 | exclusion_re = input_api.re.compile(r'(%s)[^;]+\{' % name_pattern) |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 1973 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1974 | problems = [] |
| 1975 | sources = lambda x: input_api.FilterSourceFile( |
| 1976 | x, |
| 1977 | files_to_skip=(('(?i).*test', r'.*\/junit\/') + input_api. |
| 1978 | DEFAULT_FILES_TO_SKIP), |
| 1979 | files_to_check=[r'.*\.java$']) |
| 1980 | for f in input_api.AffectedFiles(include_deletes=False, |
| 1981 | file_filter=sources): |
| 1982 | local_path = f.LocalPath() |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 1983 | is_inside_javadoc = False |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1984 | for line_number, line in f.ChangedContents(): |
| 1985 | if is_inside_javadoc and javadoc_end_re.search(line): |
| 1986 | is_inside_javadoc = False |
| 1987 | if not is_inside_javadoc and javadoc_start_re.search(line): |
| 1988 | is_inside_javadoc = True |
| 1989 | if is_inside_javadoc: |
| 1990 | continue |
| 1991 | if (inclusion_re.search(line) and not comment_re.search(line) |
| 1992 | and not annotation_re.search(line) |
| 1993 | and not exclusion_re.search(line)): |
| 1994 | problems.append('%s:%d\n %s' % |
| 1995 | (local_path, line_number, line.strip())) |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 1996 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 1997 | if problems: |
| 1998 | return [ |
| 1999 | output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems) |
| 2000 | ] |
| 2001 | else: |
| 2002 | return [] |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 2003 | |
| 2004 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2005 | def CheckNoIOStreamInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2006 | """Checks to make sure no .h files include <iostream>.""" |
| 2007 | files = [] |
| 2008 | pattern = input_api.re.compile(r'^#include\s*<iostream>', |
| 2009 | input_api.re.MULTILINE) |
| 2010 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 2011 | if not f.LocalPath().endswith('.h'): |
| 2012 | continue |
| 2013 | contents = input_api.ReadFile(f) |
| 2014 | if pattern.search(contents): |
| 2015 | files.append(f) |
thakis@chromium.org | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 2016 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2017 | if len(files): |
| 2018 | return [ |
| 2019 | output_api.PresubmitError( |
| 2020 | 'Do not #include <iostream> in header files, since it inserts static ' |
| 2021 | 'initialization into every file including the header. Instead, ' |
| 2022 | '#include <ostream>. See http://crbug.com/94794', files) |
| 2023 | ] |
| 2024 | return [] |
| 2025 | |
thakis@chromium.org | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 2026 | |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 2027 | def CheckNoStrCatRedefines(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2028 | """Checks no windows headers with StrCat redefined are included directly.""" |
| 2029 | files = [] |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 2030 | files_to_check = (r'.+%s' % _HEADER_EXTENSIONS, |
| 2031 | r'.+%s' % _IMPLEMENTATION_EXTENSIONS) |
| 2032 | files_to_skip = (input_api.DEFAULT_FILES_TO_SKIP + |
| 2033 | _NON_BASE_DEPENDENT_PATHS) |
| 2034 | sources_filter = lambda f: input_api.FilterSourceFile( |
| 2035 | f, files_to_check=files_to_check, files_to_skip=files_to_skip) |
| 2036 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2037 | pattern_deny = input_api.re.compile( |
| 2038 | r'^#include\s*[<"](shlwapi|atlbase|propvarutil|sphelper).h[">]', |
| 2039 | input_api.re.MULTILINE) |
| 2040 | pattern_allow = input_api.re.compile( |
| 2041 | r'^#include\s"base/win/windows_defines.inc"', input_api.re.MULTILINE) |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 2042 | for f in input_api.AffectedSourceFiles(sources_filter): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2043 | contents = input_api.ReadFile(f) |
| 2044 | if pattern_deny.search( |
| 2045 | contents) and not pattern_allow.search(contents): |
| 2046 | files.append(f.LocalPath()) |
Danil Chapovalov | 3518f36 | 2018-08-11 16:13:43 | [diff] [blame] | 2047 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2048 | if len(files): |
| 2049 | return [ |
| 2050 | output_api.PresubmitError( |
| 2051 | 'Do not #include shlwapi.h, atlbase.h, propvarutil.h or sphelper.h ' |
| 2052 | 'directly since they pollute code with StrCat macro. Instead, ' |
| 2053 | 'include matching header from base/win. See http://crbug.com/856536', |
| 2054 | files) |
| 2055 | ] |
| 2056 | return [] |
Danil Chapovalov | 3518f36 | 2018-08-11 16:13:43 | [diff] [blame] | 2057 | |
thakis@chromium.org | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 2058 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2059 | def CheckNoUNIT_TESTInSourceFiles(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2060 | """Checks to make sure no source files use UNIT_TEST.""" |
| 2061 | problems = [] |
| 2062 | for f in input_api.AffectedFiles(): |
| 2063 | if (not f.LocalPath().endswith(('.cc', '.mm'))): |
| 2064 | continue |
jam@chromium.org | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 2065 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2066 | for line_num, line in f.ChangedContents(): |
| 2067 | if 'UNIT_TEST ' in line or line.endswith('UNIT_TEST'): |
| 2068 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
jam@chromium.org | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 2069 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2070 | if not problems: |
| 2071 | return [] |
| 2072 | return [ |
| 2073 | output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' + |
| 2074 | '\n'.join(problems)) |
| 2075 | ] |
| 2076 | |
jam@chromium.org | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 2077 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2078 | def CheckNoDISABLETypoInTests(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2079 | """Checks to prevent attempts to disable tests with DISABLE_ prefix. |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2080 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2081 | This test warns if somebody tries to disable a test with the DISABLE_ prefix |
| 2082 | instead of DISABLED_. To filter false positives, reports are only generated |
| 2083 | if a corresponding MAYBE_ line exists. |
| 2084 | """ |
| 2085 | problems = [] |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2086 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2087 | # The following two patterns are looked for in tandem - is a test labeled |
| 2088 | # as MAYBE_ followed by a DISABLE_ (instead of the correct DISABLED) |
| 2089 | maybe_pattern = input_api.re.compile(r'MAYBE_([a-zA-Z0-9_]+)') |
| 2090 | disable_pattern = input_api.re.compile(r'DISABLE_([a-zA-Z0-9_]+)') |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2091 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2092 | # This is for the case that a test is disabled on all platforms. |
| 2093 | full_disable_pattern = input_api.re.compile( |
| 2094 | r'^\s*TEST[^(]*\([a-zA-Z0-9_]+,\s*DISABLE_[a-zA-Z0-9_]+\)', |
| 2095 | input_api.re.MULTILINE) |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2096 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2097 | for f in input_api.AffectedFiles(False): |
| 2098 | if not 'test' in f.LocalPath() or not f.LocalPath().endswith('.cc'): |
| 2099 | continue |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2100 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2101 | # Search for MABYE_, DISABLE_ pairs. |
| 2102 | disable_lines = {} # Maps of test name to line number. |
| 2103 | maybe_lines = {} |
| 2104 | for line_num, line in f.ChangedContents(): |
| 2105 | disable_match = disable_pattern.search(line) |
| 2106 | if disable_match: |
| 2107 | disable_lines[disable_match.group(1)] = line_num |
| 2108 | maybe_match = maybe_pattern.search(line) |
| 2109 | if maybe_match: |
| 2110 | maybe_lines[maybe_match.group(1)] = line_num |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2111 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2112 | # Search for DISABLE_ occurrences within a TEST() macro. |
| 2113 | disable_tests = set(disable_lines.keys()) |
| 2114 | maybe_tests = set(maybe_lines.keys()) |
| 2115 | for test in disable_tests.intersection(maybe_tests): |
| 2116 | problems.append(' %s:%d' % (f.LocalPath(), disable_lines[test])) |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2117 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2118 | contents = input_api.ReadFile(f) |
| 2119 | full_disable_match = full_disable_pattern.search(contents) |
| 2120 | if full_disable_match: |
| 2121 | problems.append(' %s' % f.LocalPath()) |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2122 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2123 | if not problems: |
| 2124 | return [] |
| 2125 | return [ |
| 2126 | output_api.PresubmitPromptWarning( |
| 2127 | 'Attempt to disable a test with DISABLE_ instead of DISABLED_?\n' + |
| 2128 | '\n'.join(problems)) |
| 2129 | ] |
| 2130 | |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2131 | |
Nina Satragno | f766053 | 2021-09-20 18:03:35 | [diff] [blame] | 2132 | def CheckForgettingMAYBEInTests(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2133 | """Checks to make sure tests disabled conditionally are not missing a |
| 2134 | corresponding MAYBE_ prefix. |
| 2135 | """ |
| 2136 | # Expect at least a lowercase character in the test name. This helps rule out |
| 2137 | # false positives with macros wrapping the actual tests name. |
| 2138 | define_maybe_pattern = input_api.re.compile( |
| 2139 | r'^\#define MAYBE_(?P<test_name>\w*[a-z]\w*)') |
Bruce Dawson | ffc5529 | 2022-04-20 04:18:19 | [diff] [blame] | 2140 | # The test_maybe_pattern needs to handle all of these forms. The standard: |
| 2141 | # IN_PROC_TEST_F(SyncTest, MAYBE_Start) { |
| 2142 | # With a wrapper macro around the test name: |
| 2143 | # IN_PROC_TEST_F(SyncTest, E2E_ENABLED(MAYBE_Start)) { |
| 2144 | # And the odd-ball NACL_BROWSER_TEST_f format: |
| 2145 | # NACL_BROWSER_TEST_F(NaClBrowserTest, SimpleLoad, { |
| 2146 | # The optional E2E_ENABLED-style is handled with (\w*\()? |
| 2147 | # The NACL_BROWSER_TEST_F pattern is handled by allowing a trailing comma or |
| 2148 | # trailing ')'. |
| 2149 | test_maybe_pattern = ( |
| 2150 | r'^\s*\w*TEST[^(]*\(\s*\w+,\s*(\w*\()?MAYBE_{test_name}[\),]') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2151 | suite_maybe_pattern = r'^\s*\w*TEST[^(]*\(\s*MAYBE_{test_name}[\),]' |
| 2152 | warnings = [] |
Nina Satragno | f766053 | 2021-09-20 18:03:35 | [diff] [blame] | 2153 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2154 | # Read the entire files. We can't just read the affected lines, forgetting to |
| 2155 | # add MAYBE_ on a change would not show up otherwise. |
| 2156 | for f in input_api.AffectedFiles(False): |
| 2157 | if not 'test' in f.LocalPath() or not f.LocalPath().endswith('.cc'): |
| 2158 | continue |
| 2159 | contents = input_api.ReadFile(f) |
| 2160 | lines = contents.splitlines(True) |
| 2161 | current_position = 0 |
| 2162 | warning_test_names = set() |
| 2163 | for line_num, line in enumerate(lines, start=1): |
| 2164 | current_position += len(line) |
| 2165 | maybe_match = define_maybe_pattern.search(line) |
| 2166 | if maybe_match: |
| 2167 | test_name = maybe_match.group('test_name') |
| 2168 | # Do not warn twice for the same test. |
| 2169 | if (test_name in warning_test_names): |
| 2170 | continue |
| 2171 | warning_test_names.add(test_name) |
Nina Satragno | f766053 | 2021-09-20 18:03:35 | [diff] [blame] | 2172 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2173 | # Attempt to find the corresponding MAYBE_ test or suite, starting from |
| 2174 | # the current position. |
| 2175 | test_match = input_api.re.compile( |
| 2176 | test_maybe_pattern.format(test_name=test_name), |
| 2177 | input_api.re.MULTILINE).search(contents, current_position) |
| 2178 | suite_match = input_api.re.compile( |
| 2179 | suite_maybe_pattern.format(test_name=test_name), |
| 2180 | input_api.re.MULTILINE).search(contents, current_position) |
| 2181 | if not test_match and not suite_match: |
| 2182 | warnings.append( |
| 2183 | output_api.PresubmitPromptWarning( |
| 2184 | '%s:%d found MAYBE_ defined without corresponding test %s' |
| 2185 | % (f.LocalPath(), line_num, test_name))) |
| 2186 | return warnings |
| 2187 | |
jam@chromium.org | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 2188 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2189 | def CheckDCHECK_IS_ONHasBraces(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2190 | """Checks to make sure DCHECK_IS_ON() does not skip the parentheses.""" |
| 2191 | errors = [] |
Kalvin Lee | 4a3b79de | 2022-05-26 16:00:16 | [diff] [blame] | 2192 | pattern = input_api.re.compile(r'\bDCHECK_IS_ON\b(?!\(\))', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2193 | input_api.re.MULTILINE) |
| 2194 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 2195 | if (not f.LocalPath().endswith(('.cc', '.mm', '.h'))): |
| 2196 | continue |
| 2197 | for lnum, line in f.ChangedContents(): |
| 2198 | if input_api.re.search(pattern, line): |
| 2199 | errors.append( |
| 2200 | output_api.PresubmitError(( |
| 2201 | '%s:%d: Use of DCHECK_IS_ON() must be written as "#if ' |
| 2202 | + 'DCHECK_IS_ON()", not forgetting the parentheses.') % |
| 2203 | (f.LocalPath(), lnum))) |
| 2204 | return errors |
danakj | 61c1aa2 | 2015-10-26 19:55:52 | [diff] [blame] | 2205 | |
| 2206 | |
Weilun Shi | a487fad | 2020-10-28 00:10:34 | [diff] [blame] | 2207 | # TODO(crbug/1138055): Reimplement CheckUmaHistogramChangesOnUpload check in a |
| 2208 | # more reliable way. See |
| 2209 | # https://chromium-review.googlesource.com/c/chromium/src/+/2500269 |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 2210 | |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2211 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2212 | def CheckFlakyTestUsage(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2213 | """Check that FlakyTest annotation is our own instead of the android one""" |
| 2214 | pattern = input_api.re.compile(r'import android.test.FlakyTest;') |
| 2215 | files = [] |
| 2216 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 2217 | if f.LocalPath().endswith('Test.java'): |
| 2218 | if pattern.search(input_api.ReadFile(f)): |
| 2219 | files.append(f) |
| 2220 | if len(files): |
| 2221 | return [ |
| 2222 | output_api.PresubmitError( |
| 2223 | 'Use org.chromium.base.test.util.FlakyTest instead of ' |
| 2224 | 'android.test.FlakyTest', files) |
| 2225 | ] |
| 2226 | return [] |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 2227 | |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2228 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2229 | def CheckNoDEPSGIT(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2230 | """Make sure .DEPS.git is never modified manually.""" |
| 2231 | if any(f.LocalPath().endswith('.DEPS.git') |
| 2232 | for f in input_api.AffectedFiles()): |
| 2233 | return [ |
| 2234 | output_api.PresubmitError( |
| 2235 | 'Never commit changes to .DEPS.git. This file is maintained by an\n' |
| 2236 | 'automated system based on what\'s in DEPS and your changes will be\n' |
| 2237 | 'overwritten.\n' |
| 2238 | 'See https://sites.google.com/a/chromium.org/dev/developers/how-tos/' |
| 2239 | 'get-the-code#Rolling_DEPS\n' |
| 2240 | 'for more information') |
| 2241 | ] |
| 2242 | return [] |
maruel@chromium.org | 2a8ac9c | 2011-10-19 17:20:44 | [diff] [blame] | 2243 | |
| 2244 | |
Sven Zheng | 76a79ea | 2022-12-21 21:25:24 | [diff] [blame] | 2245 | def CheckCrosApiNeedBrowserTest(input_api, output_api): |
| 2246 | """Check new crosapi should add browser test.""" |
| 2247 | has_new_crosapi = False |
| 2248 | has_browser_test = False |
| 2249 | for f in input_api.AffectedFiles(): |
| 2250 | path = f.LocalPath() |
| 2251 | if (path.startswith('chromeos/crosapi/mojom') and |
| 2252 | _IsMojomFile(input_api, path) and f.Action() == 'A'): |
| 2253 | has_new_crosapi = True |
| 2254 | if path.endswith('browsertest.cc') or path.endswith('browser_test.cc'): |
| 2255 | has_browser_test = True |
| 2256 | if has_new_crosapi and not has_browser_test: |
| 2257 | return [ |
| 2258 | output_api.PresubmitPromptWarning( |
| 2259 | 'You are adding a new crosapi, but there is no file ends with ' |
| 2260 | 'browsertest.cc file being added or modified. It is important ' |
| 2261 | 'to add crosapi browser test coverage to avoid version ' |
| 2262 | ' skew issues.\n' |
| 2263 | 'Check //docs/lacros/test_instructions.md for more information.' |
| 2264 | ) |
| 2265 | ] |
| 2266 | return [] |
| 2267 | |
| 2268 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2269 | def CheckValidHostsInDEPSOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2270 | """Checks that DEPS file deps are from allowed_hosts.""" |
| 2271 | # Run only if DEPS file has been modified to annoy fewer bystanders. |
| 2272 | if all(f.LocalPath() != 'DEPS' for f in input_api.AffectedFiles()): |
| 2273 | return [] |
| 2274 | # Outsource work to gclient verify |
| 2275 | try: |
| 2276 | gclient_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 2277 | 'third_party', 'depot_tools', |
| 2278 | 'gclient.py') |
| 2279 | input_api.subprocess.check_output( |
Bruce Dawson | 8a43cf7 | 2022-05-13 17:10:32 | [diff] [blame] | 2280 | [input_api.python3_executable, gclient_path, 'verify'], |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2281 | stderr=input_api.subprocess.STDOUT) |
| 2282 | return [] |
| 2283 | except input_api.subprocess.CalledProcessError as error: |
| 2284 | return [ |
| 2285 | output_api.PresubmitError( |
| 2286 | 'DEPS file must have only git dependencies.', |
| 2287 | long_text=error.output) |
| 2288 | ] |
tandrii | ef66469 | 2014-09-23 14:51:47 | [diff] [blame] | 2289 | |
| 2290 | |
Mario Sanchez Prada | 2472cab | 2019-09-18 10:58:31 | [diff] [blame] | 2291 | def _GetMessageForMatchingType(input_api, affected_file, line_number, line, |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2292 | ban_rule): |
Allen Bauer | 8477868 | 2022-09-22 16:28:56 | [diff] [blame] | 2293 | """Helper method for checking for banned constructs. |
Mario Sanchez Prada | 2472cab | 2019-09-18 10:58:31 | [diff] [blame] | 2294 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2295 | Returns an string composed of the name of the file, the line number where the |
| 2296 | match has been found and the additional text passed as |message| in case the |
| 2297 | target type name matches the text inside the line passed as parameter. |
| 2298 | """ |
| 2299 | result = [] |
Peng Huang | 9c5949a0 | 2020-06-11 19:20:54 | [diff] [blame] | 2300 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2301 | # Ignore comments about banned types. |
| 2302 | if input_api.re.search(r"^ *//", line): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2303 | return result |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2304 | # A // nocheck comment will bypass this error. |
| 2305 | if line.endswith(" nocheck"): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2306 | return result |
| 2307 | |
| 2308 | matched = False |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2309 | if ban_rule.pattern[0:1] == '/': |
| 2310 | regex = ban_rule.pattern[1:] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2311 | if input_api.re.search(regex, line): |
| 2312 | matched = True |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2313 | elif ban_rule.pattern in line: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2314 | matched = True |
| 2315 | |
| 2316 | if matched: |
| 2317 | result.append(' %s:%d:' % (affected_file.LocalPath(), line_number)) |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2318 | for line in ban_rule.explanation: |
| 2319 | result.append(' %s' % line) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2320 | |
danakj | d18e889 | 2020-12-17 17:42:01 | [diff] [blame] | 2321 | return result |
Mario Sanchez Prada | 2472cab | 2019-09-18 10:58:31 | [diff] [blame] | 2322 | |
| 2323 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2324 | def CheckNoBannedFunctions(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2325 | """Make sure that banned functions are not used.""" |
| 2326 | warnings = [] |
| 2327 | errors = [] |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2328 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2329 | def IsExcludedFile(affected_file, excluded_paths): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2330 | if not excluded_paths: |
| 2331 | return False |
| 2332 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2333 | local_path = affected_file.LocalPath() |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2334 | # Consistently use / as path separator to simplify the writing of regex |
| 2335 | # expressions. |
| 2336 | local_path = local_path.replace(input_api.os_path.sep, '/') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2337 | for item in excluded_paths: |
| 2338 | if input_api.re.match(item, local_path): |
| 2339 | return True |
| 2340 | return False |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2341 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2342 | def IsIosObjcFile(affected_file): |
| 2343 | local_path = affected_file.LocalPath() |
| 2344 | if input_api.os_path.splitext(local_path)[-1] not in ('.mm', '.m', |
| 2345 | '.h'): |
| 2346 | return False |
| 2347 | basename = input_api.os_path.basename(local_path) |
| 2348 | if 'ios' in basename.split('_'): |
| 2349 | return True |
| 2350 | for sep in (input_api.os_path.sep, input_api.os_path.altsep): |
| 2351 | if sep and 'ios' in local_path.split(sep): |
| 2352 | return True |
| 2353 | return False |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 2354 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2355 | def CheckForMatch(affected_file, line_num: int, line: str, |
| 2356 | ban_rule: BanRule): |
| 2357 | if IsExcludedFile(affected_file, ban_rule.excluded_paths): |
| 2358 | return |
| 2359 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2360 | problems = _GetMessageForMatchingType(input_api, f, line_num, line, |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2361 | ban_rule) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2362 | if problems: |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2363 | if ban_rule.treat_as_error is not None and ban_rule.treat_as_error: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2364 | errors.extend(problems) |
| 2365 | else: |
| 2366 | warnings.extend(problems) |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2367 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2368 | file_filter = lambda f: f.LocalPath().endswith(('.java')) |
| 2369 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 2370 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2371 | for ban_rule in _BANNED_JAVA_FUNCTIONS: |
| 2372 | CheckForMatch(f, line_num, line, ban_rule) |
Eric Stevenson | a9a98097 | 2017-09-23 00:04:41 | [diff] [blame] | 2373 | |
Clement Yan | 9b330cb | 2022-11-17 05:25:29 | [diff] [blame] | 2374 | file_filter = lambda f: f.LocalPath().endswith(('.js', '.ts')) |
| 2375 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 2376 | for line_num, line in f.ChangedContents(): |
| 2377 | for ban_rule in _BANNED_JAVASCRIPT_FUNCTIONS: |
| 2378 | CheckForMatch(f, line_num, line, ban_rule) |
| 2379 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2380 | file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h')) |
| 2381 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 2382 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2383 | for ban_rule in _BANNED_OBJC_FUNCTIONS: |
| 2384 | CheckForMatch(f, line_num, line, ban_rule) |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2385 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2386 | for f in input_api.AffectedFiles(file_filter=IsIosObjcFile): |
| 2387 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2388 | for ban_rule in _BANNED_IOS_OBJC_FUNCTIONS: |
| 2389 | CheckForMatch(f, line_num, line, ban_rule) |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 2390 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2391 | egtest_filter = lambda f: f.LocalPath().endswith(('_egtest.mm')) |
| 2392 | for f in input_api.AffectedFiles(file_filter=egtest_filter): |
| 2393 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2394 | for ban_rule in _BANNED_IOS_EGTEST_FUNCTIONS: |
| 2395 | CheckForMatch(f, line_num, line, ban_rule) |
Peter K. Lee | 6c03ccff | 2019-07-15 14:40:05 | [diff] [blame] | 2396 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2397 | file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h')) |
| 2398 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 2399 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2400 | for ban_rule in _BANNED_CPP_FUNCTIONS: |
| 2401 | CheckForMatch(f, line_num, line, ban_rule) |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2402 | |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 2403 | file_filter = lambda f: f.LocalPath().endswith(('.mojom')) |
| 2404 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 2405 | for line_num, line in f.ChangedContents(): |
| 2406 | for ban_rule in _BANNED_MOJOM_PATTERNS: |
| 2407 | CheckForMatch(f, line_num, line, ban_rule) |
| 2408 | |
| 2409 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2410 | result = [] |
| 2411 | if (warnings): |
| 2412 | result.append( |
| 2413 | output_api.PresubmitPromptWarning('Banned functions were used.\n' + |
| 2414 | '\n'.join(warnings))) |
| 2415 | if (errors): |
| 2416 | result.append( |
| 2417 | output_api.PresubmitError('Banned functions were used.\n' + |
| 2418 | '\n'.join(errors))) |
| 2419 | return result |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2420 | |
Allen Bauer | 8477868 | 2022-09-22 16:28:56 | [diff] [blame] | 2421 | def CheckNoLayoutCallsInTests(input_api, output_api): |
| 2422 | """Make sure there are no explicit calls to View::Layout() in tests""" |
| 2423 | warnings = [] |
| 2424 | ban_rule = BanRule( |
| 2425 | r'/(\.|->)Layout\(\);', |
| 2426 | ( |
| 2427 | 'Direct calls to View::Layout() are not allowed in tests. ' |
| 2428 | 'If the view must be laid out here, use RunScheduledLayout(view). It ' |
| 2429 | 'is found in //ui/views/test/views_test_utils.h. ' |
| 2430 | 'See http://crbug.com/1350521 for more details.', |
| 2431 | ), |
| 2432 | False, |
| 2433 | ) |
| 2434 | file_filter = lambda f: input_api.re.search( |
| 2435 | r'_(unittest|browsertest|ui_test).*\.(cc|mm)$', f.LocalPath()) |
| 2436 | for f in input_api.AffectedFiles(file_filter = file_filter): |
| 2437 | for line_num, line in f.ChangedContents(): |
| 2438 | problems = _GetMessageForMatchingType(input_api, f, |
| 2439 | line_num, line, |
| 2440 | ban_rule) |
| 2441 | if problems: |
| 2442 | warnings.extend(problems) |
| 2443 | result = [] |
| 2444 | if (warnings): |
| 2445 | result.append( |
| 2446 | output_api.PresubmitPromptWarning( |
| 2447 | 'Banned call to View::Layout() in tests.\n\n'.join(warnings))) |
| 2448 | return result |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2449 | |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 2450 | def _CheckAndroidNoBannedImports(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2451 | """Make sure that banned java imports are not used.""" |
| 2452 | errors = [] |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 2453 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2454 | file_filter = lambda f: f.LocalPath().endswith(('.java')) |
| 2455 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 2456 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2457 | for ban_rule in _BANNED_JAVA_IMPORTS: |
| 2458 | # Consider merging this into the above function. There is no |
| 2459 | # real difference anymore other than helping with a little |
| 2460 | # bit of boilerplate text. Doing so means things like |
| 2461 | # `treat_as_error` will also be uniformly handled. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2462 | problems = _GetMessageForMatchingType(input_api, f, line_num, |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2463 | line, ban_rule) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2464 | if problems: |
| 2465 | errors.extend(problems) |
| 2466 | result = [] |
| 2467 | if (errors): |
| 2468 | result.append( |
| 2469 | output_api.PresubmitError('Banned imports were used.\n' + |
| 2470 | '\n'.join(errors))) |
| 2471 | return result |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 2472 | |
| 2473 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2474 | def CheckNoPragmaOnce(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2475 | """Make sure that banned functions are not used.""" |
| 2476 | files = [] |
| 2477 | pattern = input_api.re.compile(r'^#pragma\s+once', input_api.re.MULTILINE) |
| 2478 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 2479 | if not f.LocalPath().endswith('.h'): |
| 2480 | continue |
Bruce Dawson | 4c4c292 | 2022-05-02 18:07:33 | [diff] [blame] | 2481 | if f.LocalPath().endswith('com_imported_mstscax.h'): |
| 2482 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2483 | contents = input_api.ReadFile(f) |
| 2484 | if pattern.search(contents): |
| 2485 | files.append(f) |
dcheng@chromium.org | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 2486 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2487 | if files: |
| 2488 | return [ |
| 2489 | output_api.PresubmitError( |
| 2490 | 'Do not use #pragma once in header files.\n' |
| 2491 | 'See http://www.chromium.org/developers/coding-style#TOC-File-headers', |
| 2492 | files) |
| 2493 | ] |
| 2494 | return [] |
dcheng@chromium.org | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 2495 | |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2496 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2497 | def CheckNoTrinaryTrueFalse(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2498 | """Checks to make sure we don't introduce use of foo ? true : false.""" |
| 2499 | problems = [] |
| 2500 | pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)') |
| 2501 | for f in input_api.AffectedFiles(): |
| 2502 | if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')): |
| 2503 | continue |
thestig@chromium.org | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 2504 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2505 | for line_num, line in f.ChangedContents(): |
| 2506 | if pattern.match(line): |
| 2507 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
thestig@chromium.org | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 2508 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2509 | if not problems: |
| 2510 | return [] |
| 2511 | return [ |
| 2512 | output_api.PresubmitPromptWarning( |
| 2513 | 'Please consider avoiding the "? true : false" pattern if possible.\n' |
| 2514 | + '\n'.join(problems)) |
| 2515 | ] |
thestig@chromium.org | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 2516 | |
| 2517 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2518 | def CheckUnwantedDependencies(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2519 | """Runs checkdeps on #include and import statements added in this |
| 2520 | change. Breaking - rules is an error, breaking ! rules is a |
| 2521 | warning. |
| 2522 | """ |
| 2523 | # Return early if no relevant file types were modified. |
| 2524 | for f in input_api.AffectedFiles(): |
| 2525 | path = f.LocalPath() |
| 2526 | if (_IsCPlusPlusFile(input_api, path) or _IsProtoFile(input_api, path) |
| 2527 | or _IsJavaFile(input_api, path)): |
| 2528 | break |
joi@chromium.org | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 2529 | else: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2530 | return [] |
rhalavati | 08acd23 | 2017-04-03 07:23:28 | [diff] [blame] | 2531 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2532 | import sys |
| 2533 | # We need to wait until we have an input_api object and use this |
| 2534 | # roundabout construct to import checkdeps because this file is |
| 2535 | # eval-ed and thus doesn't have __file__. |
| 2536 | original_sys_path = sys.path |
| 2537 | try: |
| 2538 | sys.path = sys.path + [ |
| 2539 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 2540 | 'buildtools', 'checkdeps') |
| 2541 | ] |
| 2542 | import checkdeps |
| 2543 | from rules import Rule |
| 2544 | finally: |
| 2545 | # Restore sys.path to what it was before. |
| 2546 | sys.path = original_sys_path |
joi@chromium.org | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 2547 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2548 | added_includes = [] |
| 2549 | added_imports = [] |
| 2550 | added_java_imports = [] |
| 2551 | for f in input_api.AffectedFiles(): |
| 2552 | if _IsCPlusPlusFile(input_api, f.LocalPath()): |
| 2553 | changed_lines = [line for _, line in f.ChangedContents()] |
| 2554 | added_includes.append([f.AbsoluteLocalPath(), changed_lines]) |
| 2555 | elif _IsProtoFile(input_api, f.LocalPath()): |
| 2556 | changed_lines = [line for _, line in f.ChangedContents()] |
| 2557 | added_imports.append([f.AbsoluteLocalPath(), changed_lines]) |
| 2558 | elif _IsJavaFile(input_api, f.LocalPath()): |
| 2559 | changed_lines = [line for _, line in f.ChangedContents()] |
| 2560 | added_java_imports.append([f.AbsoluteLocalPath(), changed_lines]) |
Jinsuk Kim | 5a09267 | 2017-10-24 22:42:24 | [diff] [blame] | 2561 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2562 | deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath()) |
| 2563 | |
| 2564 | error_descriptions = [] |
| 2565 | warning_descriptions = [] |
| 2566 | error_subjects = set() |
| 2567 | warning_subjects = set() |
| 2568 | |
| 2569 | for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes( |
| 2570 | added_includes): |
| 2571 | path = input_api.os_path.relpath(path, input_api.PresubmitLocalPath()) |
| 2572 | description_with_path = '%s\n %s' % (path, rule_description) |
| 2573 | if rule_type == Rule.DISALLOW: |
| 2574 | error_descriptions.append(description_with_path) |
| 2575 | error_subjects.add("#includes") |
| 2576 | else: |
| 2577 | warning_descriptions.append(description_with_path) |
| 2578 | warning_subjects.add("#includes") |
| 2579 | |
| 2580 | for path, rule_type, rule_description in deps_checker.CheckAddedProtoImports( |
| 2581 | added_imports): |
| 2582 | path = input_api.os_path.relpath(path, input_api.PresubmitLocalPath()) |
| 2583 | description_with_path = '%s\n %s' % (path, rule_description) |
| 2584 | if rule_type == Rule.DISALLOW: |
| 2585 | error_descriptions.append(description_with_path) |
| 2586 | error_subjects.add("imports") |
| 2587 | else: |
| 2588 | warning_descriptions.append(description_with_path) |
| 2589 | warning_subjects.add("imports") |
| 2590 | |
| 2591 | for path, rule_type, rule_description in deps_checker.CheckAddedJavaImports( |
| 2592 | added_java_imports, _JAVA_MULTIPLE_DEFINITION_EXCLUDED_PATHS): |
| 2593 | path = input_api.os_path.relpath(path, input_api.PresubmitLocalPath()) |
| 2594 | description_with_path = '%s\n %s' % (path, rule_description) |
| 2595 | if rule_type == Rule.DISALLOW: |
| 2596 | error_descriptions.append(description_with_path) |
| 2597 | error_subjects.add("imports") |
| 2598 | else: |
| 2599 | warning_descriptions.append(description_with_path) |
| 2600 | warning_subjects.add("imports") |
| 2601 | |
| 2602 | results = [] |
| 2603 | if error_descriptions: |
| 2604 | results.append( |
| 2605 | output_api.PresubmitError( |
| 2606 | 'You added one or more %s that violate checkdeps rules.' % |
| 2607 | " and ".join(error_subjects), error_descriptions)) |
| 2608 | if warning_descriptions: |
| 2609 | results.append( |
| 2610 | output_api.PresubmitPromptOrNotify( |
| 2611 | 'You added one or more %s of files that are temporarily\n' |
| 2612 | 'allowed but being removed. Can you avoid introducing the\n' |
| 2613 | '%s? See relevant DEPS file(s) for details and contacts.' % |
| 2614 | (" and ".join(warning_subjects), "/".join(warning_subjects)), |
| 2615 | warning_descriptions)) |
| 2616 | return results |
joi@chromium.org | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 2617 | |
| 2618 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2619 | def CheckFilePermissions(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2620 | """Check that all files have their permissions properly set.""" |
| 2621 | if input_api.platform == 'win32': |
| 2622 | return [] |
| 2623 | checkperms_tool = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 2624 | 'tools', 'checkperms', |
| 2625 | 'checkperms.py') |
| 2626 | args = [ |
Bruce Dawson | 8a43cf7 | 2022-05-13 17:10:32 | [diff] [blame] | 2627 | input_api.python3_executable, checkperms_tool, '--root', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2628 | input_api.change.RepositoryRoot() |
| 2629 | ] |
| 2630 | with input_api.CreateTemporaryFile() as file_list: |
| 2631 | for f in input_api.AffectedFiles(): |
| 2632 | # checkperms.py file/directory arguments must be relative to the |
| 2633 | # repository. |
| 2634 | file_list.write((f.LocalPath() + '\n').encode('utf8')) |
| 2635 | file_list.close() |
| 2636 | args += ['--file-list', file_list.name] |
| 2637 | try: |
| 2638 | input_api.subprocess.check_output(args) |
| 2639 | return [] |
| 2640 | except input_api.subprocess.CalledProcessError as error: |
| 2641 | return [ |
| 2642 | output_api.PresubmitError('checkperms.py failed:', |
| 2643 | long_text=error.output.decode( |
| 2644 | 'utf-8', 'ignore')) |
| 2645 | ] |
csharp@chromium.org | fbcafe5a | 2012-08-08 15:31:22 | [diff] [blame] | 2646 | |
| 2647 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2648 | def CheckNoAuraWindowPropertyHInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2649 | """Makes sure we don't include ui/aura/window_property.h |
| 2650 | in header files. |
| 2651 | """ |
| 2652 | pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"') |
| 2653 | errors = [] |
| 2654 | for f in input_api.AffectedFiles(): |
| 2655 | if not f.LocalPath().endswith('.h'): |
| 2656 | continue |
| 2657 | for line_num, line in f.ChangedContents(): |
| 2658 | if pattern.match(line): |
| 2659 | errors.append(' %s:%d' % (f.LocalPath(), line_num)) |
oshima@chromium.org | c8278b3 | 2012-10-30 20:35:49 | [diff] [blame] | 2660 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2661 | results = [] |
| 2662 | if errors: |
| 2663 | results.append( |
| 2664 | output_api.PresubmitError( |
| 2665 | 'Header files should not include ui/aura/window_property.h', |
| 2666 | errors)) |
| 2667 | return results |
oshima@chromium.org | c8278b3 | 2012-10-30 20:35:49 | [diff] [blame] | 2668 | |
| 2669 | |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 2670 | def CheckNoInternalHeapIncludes(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2671 | """Makes sure we don't include any headers from |
| 2672 | third_party/blink/renderer/platform/heap/impl or |
| 2673 | third_party/blink/renderer/platform/heap/v8_wrapper from files outside of |
| 2674 | third_party/blink/renderer/platform/heap |
| 2675 | """ |
| 2676 | impl_pattern = input_api.re.compile( |
| 2677 | r'^\s*#include\s*"third_party/blink/renderer/platform/heap/impl/.*"') |
| 2678 | v8_wrapper_pattern = input_api.re.compile( |
| 2679 | r'^\s*#include\s*"third_party/blink/renderer/platform/heap/v8_wrapper/.*"' |
| 2680 | ) |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2681 | # Consistently use / as path separator to simplify the writing of regex |
| 2682 | # expressions. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2683 | file_filter = lambda f: not input_api.re.match( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2684 | r"^third_party/blink/renderer/platform/heap/.*", |
| 2685 | f.LocalPath().replace(input_api.os_path.sep, '/')) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2686 | errors = [] |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 2687 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2688 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 2689 | for line_num, line in f.ChangedContents(): |
| 2690 | if impl_pattern.match(line) or v8_wrapper_pattern.match(line): |
| 2691 | errors.append(' %s:%d' % (f.LocalPath(), line_num)) |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 2692 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2693 | results = [] |
| 2694 | if errors: |
| 2695 | results.append( |
| 2696 | output_api.PresubmitError( |
| 2697 | 'Do not include files from third_party/blink/renderer/platform/heap/impl' |
| 2698 | ' or third_party/blink/renderer/platform/heap/v8_wrapper. Use the ' |
| 2699 | 'relevant counterparts from third_party/blink/renderer/platform/heap', |
| 2700 | errors)) |
| 2701 | return results |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 2702 | |
| 2703 | |
dbeam@chromium.org | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 2704 | def _CheckForVersionControlConflictsInFile(input_api, f): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2705 | pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$') |
| 2706 | errors = [] |
| 2707 | for line_num, line in f.ChangedContents(): |
| 2708 | if f.LocalPath().endswith(('.md', '.rst', '.txt')): |
| 2709 | # First-level headers in markdown look a lot like version control |
| 2710 | # conflict markers. http://daringfireball.net/projects/markdown/basics |
| 2711 | continue |
| 2712 | if pattern.match(line): |
| 2713 | errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line)) |
| 2714 | return errors |
dbeam@chromium.org | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 2715 | |
| 2716 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2717 | def CheckForVersionControlConflicts(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2718 | """Usually this is not intentional and will cause a compile failure.""" |
| 2719 | errors = [] |
| 2720 | for f in input_api.AffectedFiles(): |
| 2721 | errors.extend(_CheckForVersionControlConflictsInFile(input_api, f)) |
dbeam@chromium.org | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 2722 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2723 | results = [] |
| 2724 | if errors: |
| 2725 | results.append( |
| 2726 | output_api.PresubmitError( |
| 2727 | 'Version control conflict markers found, please resolve.', |
| 2728 | errors)) |
| 2729 | return results |
dbeam@chromium.org | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 2730 | |
Wei-Yin Chen (陳威尹) | f799d44 | 2018-07-31 02:20:20 | [diff] [blame] | 2731 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2732 | def CheckGoogleSupportAnswerUrlOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2733 | pattern = input_api.re.compile('support\.google\.com\/chrome.*/answer') |
| 2734 | errors = [] |
| 2735 | for f in input_api.AffectedFiles(): |
| 2736 | for line_num, line in f.ChangedContents(): |
| 2737 | if pattern.search(line): |
| 2738 | errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line)) |
estade | e17314a0 | 2017-01-12 16:22:16 | [diff] [blame] | 2739 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2740 | results = [] |
| 2741 | if errors: |
| 2742 | results.append( |
| 2743 | output_api.PresubmitPromptWarning( |
| 2744 | 'Found Google support URL addressed by answer number. Please replace ' |
| 2745 | 'with a p= identifier instead. See crbug.com/679462\n', |
| 2746 | errors)) |
| 2747 | return results |
estade | e17314a0 | 2017-01-12 16:22:16 | [diff] [blame] | 2748 | |
dbeam@chromium.org | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 2749 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2750 | def CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2751 | def FilterFile(affected_file): |
| 2752 | """Filter function for use with input_api.AffectedSourceFiles, |
| 2753 | below. This filters out everything except non-test files from |
| 2754 | top-level directories that generally speaking should not hard-code |
| 2755 | service URLs (e.g. src/android_webview/, src/content/ and others). |
| 2756 | """ |
| 2757 | return input_api.FilterSourceFile( |
| 2758 | affected_file, |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2759 | files_to_check=[r'^(android_webview|base|content|net)/.*'], |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2760 | files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 2761 | input_api.DEFAULT_FILES_TO_SKIP)) |
joi@chromium.org | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 2762 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2763 | base_pattern = ('"[^"]*(google|googleapis|googlezip|googledrive|appspot)' |
| 2764 | '\.(com|net)[^"]*"') |
| 2765 | comment_pattern = input_api.re.compile('//.*%s' % base_pattern) |
| 2766 | pattern = input_api.re.compile(base_pattern) |
| 2767 | problems = [] # items are (filename, line_number, line) |
| 2768 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 2769 | for line_num, line in f.ChangedContents(): |
| 2770 | if not comment_pattern.search(line) and pattern.search(line): |
| 2771 | problems.append((f.LocalPath(), line_num, line)) |
joi@chromium.org | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 2772 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2773 | if problems: |
| 2774 | return [ |
| 2775 | output_api.PresubmitPromptOrNotify( |
| 2776 | 'Most layers below src/chrome/ should not hardcode service URLs.\n' |
| 2777 | 'Are you sure this is correct?', [ |
| 2778 | ' %s:%d: %s' % (problem[0], problem[1], problem[2]) |
| 2779 | for problem in problems |
| 2780 | ]) |
| 2781 | ] |
| 2782 | else: |
| 2783 | return [] |
joi@chromium.org | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 2784 | |
| 2785 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2786 | def CheckChromeOsSyncedPrefRegistration(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2787 | """Warns if Chrome OS C++ files register syncable prefs as browser prefs.""" |
James Cook | 6b6597c | 2019-11-06 22:05:29 | [diff] [blame] | 2788 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2789 | def FileFilter(affected_file): |
| 2790 | """Includes directories known to be Chrome OS only.""" |
| 2791 | return input_api.FilterSourceFile( |
| 2792 | affected_file, |
| 2793 | files_to_check=( |
| 2794 | '^ash/', |
| 2795 | '^chromeos/', # Top-level src/chromeos. |
| 2796 | '.*/chromeos/', # Any path component. |
| 2797 | '^components/arc', |
| 2798 | '^components/exo'), |
| 2799 | files_to_skip=(input_api.DEFAULT_FILES_TO_SKIP)) |
James Cook | 6b6597c | 2019-11-06 22:05:29 | [diff] [blame] | 2800 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2801 | prefs = [] |
| 2802 | priority_prefs = [] |
| 2803 | for f in input_api.AffectedFiles(file_filter=FileFilter): |
| 2804 | for line_num, line in f.ChangedContents(): |
| 2805 | if input_api.re.search('PrefRegistrySyncable::SYNCABLE_PREF', |
| 2806 | line): |
| 2807 | prefs.append(' %s:%d:' % (f.LocalPath(), line_num)) |
| 2808 | prefs.append(' %s' % line) |
| 2809 | if input_api.re.search( |
| 2810 | 'PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF', line): |
| 2811 | priority_prefs.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 2812 | priority_prefs.append(' %s' % line) |
| 2813 | |
| 2814 | results = [] |
| 2815 | if (prefs): |
| 2816 | results.append( |
| 2817 | output_api.PresubmitPromptWarning( |
| 2818 | 'Preferences were registered as SYNCABLE_PREF and will be controlled ' |
| 2819 | 'by browser sync settings. If these prefs should be controlled by OS ' |
| 2820 | 'sync settings use SYNCABLE_OS_PREF instead.\n' + |
| 2821 | '\n'.join(prefs))) |
| 2822 | if (priority_prefs): |
| 2823 | results.append( |
| 2824 | output_api.PresubmitPromptWarning( |
| 2825 | 'Preferences were registered as SYNCABLE_PRIORITY_PREF and will be ' |
| 2826 | 'controlled by browser sync settings. If these prefs should be ' |
| 2827 | 'controlled by OS sync settings use SYNCABLE_OS_PRIORITY_PREF ' |
| 2828 | 'instead.\n' + '\n'.join(prefs))) |
| 2829 | return results |
James Cook | 6b6597c | 2019-11-06 22:05:29 | [diff] [blame] | 2830 | |
| 2831 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2832 | def CheckNoAbbreviationInPngFileName(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2833 | """Makes sure there are no abbreviations in the name of PNG files. |
| 2834 | The native_client_sdk directory is excluded because it has auto-generated PNG |
| 2835 | files for documentation. |
| 2836 | """ |
| 2837 | errors = [] |
Yuanqing Zhu | 9eef0283 | 2022-12-04 14:42:17 | [diff] [blame] | 2838 | files_to_check = [r'.*\.png$'] |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2839 | files_to_skip = [r'^native_client_sdk/', |
| 2840 | r'^services/test/', |
| 2841 | r'^third_party/blink/web_tests/', |
Bruce Dawson | 3db45621 | 2022-05-02 05:34:18 | [diff] [blame] | 2842 | ] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2843 | file_filter = lambda f: input_api.FilterSourceFile( |
| 2844 | f, files_to_check=files_to_check, files_to_skip=files_to_skip) |
Yuanqing Zhu | 9eef0283 | 2022-12-04 14:42:17 | [diff] [blame] | 2845 | abbreviation = input_api.re.compile('.+_[a-z]\.png|.+_[a-z]_.*\.png') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2846 | for f in input_api.AffectedFiles(include_deletes=False, |
| 2847 | file_filter=file_filter): |
Yuanqing Zhu | 9eef0283 | 2022-12-04 14:42:17 | [diff] [blame] | 2848 | file_name = input_api.os_path.split(f.LocalPath())[1] |
| 2849 | if abbreviation.search(file_name): |
| 2850 | errors.append(' %s' % f.LocalPath()) |
oshima@chromium.org | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 2851 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2852 | results = [] |
| 2853 | if errors: |
| 2854 | results.append( |
| 2855 | output_api.PresubmitError( |
| 2856 | 'The name of PNG files should not have abbreviations. \n' |
| 2857 | 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n' |
| 2858 | 'Contact oshima@chromium.org if you have questions.', errors)) |
| 2859 | return results |
oshima@chromium.org | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 2860 | |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 2861 | def CheckNoProductIconsAddedToPublicRepo(input_api, output_api): |
| 2862 | """Heuristically identifies product icons based on their file name and reminds |
| 2863 | contributors not to add them to the Chromium repository. |
| 2864 | """ |
| 2865 | errors = [] |
| 2866 | files_to_check = [r'.*google.*\.png$|.*google.*\.svg$|.*google.*\.icon$'] |
| 2867 | file_filter = lambda f: input_api.FilterSourceFile( |
| 2868 | f, files_to_check=files_to_check) |
| 2869 | for f in input_api.AffectedFiles(include_deletes=False, |
| 2870 | file_filter=file_filter): |
| 2871 | errors.append(' %s' % f.LocalPath()) |
| 2872 | |
| 2873 | results = [] |
| 2874 | if errors: |
Bruce Dawson | 3bcf0c9 | 2022-08-12 00:03:08 | [diff] [blame] | 2875 | # Give warnings instead of errors on presubmit --all and presubmit |
| 2876 | # --files. |
| 2877 | message_type = (output_api.PresubmitNotifyResult if input_api.no_diffs |
| 2878 | else output_api.PresubmitError) |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 2879 | results.append( |
Bruce Dawson | 3bcf0c9 | 2022-08-12 00:03:08 | [diff] [blame] | 2880 | message_type( |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 2881 | 'Trademarked images should not be added to the public repo. ' |
| 2882 | 'See crbug.com/944754', errors)) |
| 2883 | return results |
| 2884 | |
oshima@chromium.org | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 2885 | |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 2886 | def _ExtractAddRulesFromParsedDeps(parsed_deps): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2887 | """Extract the rules that add dependencies from a parsed DEPS file. |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 2888 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2889 | Args: |
| 2890 | parsed_deps: the locals dictionary from evaluating the DEPS file.""" |
| 2891 | add_rules = set() |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 2892 | add_rules.update([ |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2893 | rule[1:] for rule in parsed_deps.get('include_rules', []) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 2894 | if rule.startswith('+') or rule.startswith('!') |
| 2895 | ]) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2896 | for _, rules in parsed_deps.get('specific_include_rules', {}).items(): |
| 2897 | add_rules.update([ |
| 2898 | rule[1:] for rule in rules |
| 2899 | if rule.startswith('+') or rule.startswith('!') |
| 2900 | ]) |
| 2901 | return add_rules |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 2902 | |
| 2903 | |
| 2904 | def _ParseDeps(contents): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2905 | """Simple helper for parsing DEPS files.""" |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 2906 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2907 | # Stubs for handling special syntax in the root DEPS file. |
| 2908 | class _VarImpl: |
| 2909 | def __init__(self, local_scope): |
| 2910 | self._local_scope = local_scope |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 2911 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2912 | def Lookup(self, var_name): |
| 2913 | """Implements the Var syntax.""" |
| 2914 | try: |
| 2915 | return self._local_scope['vars'][var_name] |
| 2916 | except KeyError: |
| 2917 | raise Exception('Var is not defined: %s' % var_name) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 2918 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2919 | local_scope = {} |
| 2920 | global_scope = { |
| 2921 | 'Var': _VarImpl(local_scope).Lookup, |
| 2922 | 'Str': str, |
| 2923 | } |
Dirk Pranke | 1b9e0638 | 2021-05-14 01:16:22 | [diff] [blame] | 2924 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2925 | exec(contents, global_scope, local_scope) |
| 2926 | return local_scope |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 2927 | |
| 2928 | |
| 2929 | def _CalculateAddedDeps(os_path, old_contents, new_contents): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2930 | """Helper method for CheckAddedDepsHaveTargetApprovals. Returns |
| 2931 | a set of DEPS entries that we should look up. |
joi@chromium.org | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 2932 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2933 | For a directory (rather than a specific filename) we fake a path to |
| 2934 | a specific filename by adding /DEPS. This is chosen as a file that |
| 2935 | will seldom or never be subject to per-file include_rules. |
| 2936 | """ |
| 2937 | # We ignore deps entries on auto-generated directories. |
| 2938 | AUTO_GENERATED_DIRS = ['grit', 'jni'] |
tony@chromium.org | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 2939 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2940 | old_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(old_contents)) |
| 2941 | new_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(new_contents)) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 2942 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2943 | added_deps = new_deps.difference(old_deps) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 2944 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2945 | results = set() |
| 2946 | for added_dep in added_deps: |
| 2947 | if added_dep.split('/')[0] in AUTO_GENERATED_DIRS: |
| 2948 | continue |
| 2949 | # Assume that a rule that ends in .h is a rule for a specific file. |
| 2950 | if added_dep.endswith('.h'): |
| 2951 | results.add(added_dep) |
| 2952 | else: |
| 2953 | results.add(os_path.join(added_dep, 'DEPS')) |
| 2954 | return results |
tony@chromium.org | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 2955 | |
| 2956 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2957 | def CheckAddedDepsHaveTargetApprovals(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2958 | """When a dependency prefixed with + is added to a DEPS file, we |
| 2959 | want to make sure that the change is reviewed by an OWNER of the |
| 2960 | target file or directory, to avoid layering violations from being |
| 2961 | introduced. This check verifies that this happens. |
| 2962 | """ |
| 2963 | # We rely on Gerrit's code-owners to check approvals. |
| 2964 | # input_api.gerrit is always set for Chromium, but other projects |
| 2965 | # might not use Gerrit. |
Bruce Dawson | 344ab26 | 2022-06-04 11:35:10 | [diff] [blame] | 2966 | if not input_api.gerrit or input_api.no_diffs: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2967 | return [] |
Bruce Dawson | b357aeb | 2022-08-09 15:38:30 | [diff] [blame] | 2968 | if 'PRESUBMIT_SKIP_NETWORK' in input_api.environ: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2969 | return [] |
Bruce Dawson | b357aeb | 2022-08-09 15:38:30 | [diff] [blame] | 2970 | try: |
| 2971 | if (input_api.change.issue and |
| 2972 | input_api.gerrit.IsOwnersOverrideApproved( |
| 2973 | input_api.change.issue)): |
| 2974 | # Skip OWNERS check when Owners-Override label is approved. This is |
| 2975 | # intended for global owners, trusted bots, and on-call sheriffs. |
| 2976 | # Review is still required for these changes. |
| 2977 | return [] |
| 2978 | except Exception as e: |
Sam Maier | 4cef924 | 2022-10-03 14:21:24 | [diff] [blame] | 2979 | return [output_api.PresubmitPromptWarning( |
| 2980 | 'Failed to retrieve owner override status - %s' % str(e))] |
Edward Lesmes | 6fba5108 | 2021-01-20 04:20:23 | [diff] [blame] | 2981 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2982 | virtual_depended_on_files = set() |
jochen | 53efcdd | 2016-01-29 05:09:24 | [diff] [blame] | 2983 | |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2984 | # Consistently use / as path separator to simplify the writing of regex |
| 2985 | # expressions. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2986 | file_filter = lambda f: not input_api.re.match( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2987 | r"^third_party/blink/.*", |
| 2988 | f.LocalPath().replace(input_api.os_path.sep, '/')) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2989 | for f in input_api.AffectedFiles(include_deletes=False, |
| 2990 | file_filter=file_filter): |
| 2991 | filename = input_api.os_path.basename(f.LocalPath()) |
| 2992 | if filename == 'DEPS': |
| 2993 | virtual_depended_on_files.update( |
| 2994 | _CalculateAddedDeps(input_api.os_path, |
| 2995 | '\n'.join(f.OldContents()), |
| 2996 | '\n'.join(f.NewContents()))) |
joi@chromium.org | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 2997 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2998 | if not virtual_depended_on_files: |
| 2999 | return [] |
joi@chromium.org | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3000 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3001 | if input_api.is_committing: |
| 3002 | if input_api.tbr: |
| 3003 | return [ |
| 3004 | output_api.PresubmitNotifyResult( |
| 3005 | '--tbr was specified, skipping OWNERS check for DEPS additions' |
| 3006 | ) |
| 3007 | ] |
Daniel Cheng | 3008dc1 | 2022-05-13 04:02:11 | [diff] [blame] | 3008 | # TODO(dcheng): Make this generate an error on dry runs if the reviewer |
| 3009 | # is not added, to prevent review serialization. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3010 | if input_api.dry_run: |
| 3011 | return [ |
| 3012 | output_api.PresubmitNotifyResult( |
| 3013 | 'This is a dry run, skipping OWNERS check for DEPS additions' |
| 3014 | ) |
| 3015 | ] |
| 3016 | if not input_api.change.issue: |
| 3017 | return [ |
| 3018 | output_api.PresubmitError( |
| 3019 | "DEPS approval by OWNERS check failed: this change has " |
| 3020 | "no change number, so we can't check it for approvals.") |
| 3021 | ] |
| 3022 | output = output_api.PresubmitError |
joi@chromium.org | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 3023 | else: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3024 | output = output_api.PresubmitNotifyResult |
joi@chromium.org | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3025 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3026 | owner_email, reviewers = ( |
| 3027 | input_api.canned_checks.GetCodereviewOwnerAndReviewers( |
| 3028 | input_api, None, approval_needed=input_api.is_committing)) |
joi@chromium.org | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3029 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3030 | owner_email = owner_email or input_api.change.author_email |
| 3031 | |
| 3032 | approval_status = input_api.owners_client.GetFilesApprovalStatus( |
| 3033 | virtual_depended_on_files, reviewers.union([owner_email]), []) |
| 3034 | missing_files = [ |
| 3035 | f for f in virtual_depended_on_files |
| 3036 | if approval_status[f] != input_api.owners_client.APPROVED |
| 3037 | ] |
| 3038 | |
| 3039 | # We strip the /DEPS part that was added by |
| 3040 | # _FilesToCheckForIncomingDeps to fake a path to a file in a |
| 3041 | # directory. |
| 3042 | def StripDeps(path): |
| 3043 | start_deps = path.rfind('/DEPS') |
| 3044 | if start_deps != -1: |
| 3045 | return path[:start_deps] |
| 3046 | else: |
| 3047 | return path |
| 3048 | |
| 3049 | unapproved_dependencies = [ |
| 3050 | "'+%s'," % StripDeps(path) for path in missing_files |
| 3051 | ] |
| 3052 | |
| 3053 | if unapproved_dependencies: |
| 3054 | output_list = [ |
| 3055 | output( |
| 3056 | 'You need LGTM from owners of depends-on paths in DEPS that were ' |
| 3057 | 'modified in this CL:\n %s' % |
| 3058 | '\n '.join(sorted(unapproved_dependencies))) |
| 3059 | ] |
| 3060 | suggested_owners = input_api.owners_client.SuggestOwners( |
| 3061 | missing_files, exclude=[owner_email]) |
| 3062 | output_list.append( |
| 3063 | output('Suggested missing target path OWNERS:\n %s' % |
| 3064 | '\n '.join(suggested_owners or []))) |
| 3065 | return output_list |
| 3066 | |
| 3067 | return [] |
joi@chromium.org | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3068 | |
| 3069 | |
Wei-Yin Chen (陳威尹) | dca729a | 2018-07-31 21:35:49 | [diff] [blame] | 3070 | # TODO: add unit tests. |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3071 | def CheckSpamLogging(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3072 | file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS] |
| 3073 | files_to_skip = ( |
| 3074 | _EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 3075 | input_api.DEFAULT_FILES_TO_SKIP + ( |
Jaewon Jung | 2f323bb | 2022-12-07 23:55:01 | [diff] [blame] | 3076 | r"^base/fuchsia/scoped_fx_logger\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3077 | r"^base/logging\.h$", |
| 3078 | r"^base/logging\.cc$", |
| 3079 | r"^base/task/thread_pool/task_tracker\.cc$", |
| 3080 | r"^chrome/app/chrome_main_delegate\.cc$", |
Yao Li | 359937b | 2023-02-15 23:43:03 | [diff] [blame] | 3081 | r"^chrome/browser/ash/arc/enterprise/cert_store/arc_cert_installer\.cc$", |
| 3082 | r"^chrome/browser/ash/policy/remote_commands/user_command_arc_job\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3083 | r"^chrome/browser/chrome_browser_main\.cc$", |
| 3084 | r"^chrome/browser/ui/startup/startup_browser_creator\.cc$", |
| 3085 | r"^chrome/browser/browser_switcher/bho/.*", |
| 3086 | r"^chrome/browser/diagnostics/diagnostics_writer\.cc$", |
| 3087 | r"^chrome/chrome_cleaner/.*", |
| 3088 | r"^chrome/chrome_elf/dll_hash/dll_hash_main\.cc$", |
| 3089 | r"^chrome/installer/setup/.*", |
| 3090 | r"^chromecast/", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3091 | r"^components/media_control/renderer/media_playback_options\.cc$", |
Salma Elmahallawy | 5297645 | 2023-01-27 17:04:49 | [diff] [blame] | 3092 | r"^components/policy/core/common/policy_logger\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3093 | r"^components/viz/service/display/" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3094 | r"overlay_strategy_underlay_cast\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3095 | r"^components/zucchini/.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3096 | # TODO(peter): Remove exception. https://crbug.com/534537 |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3097 | r"^content/browser/notifications/" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3098 | r"notification_event_dispatcher_impl\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3099 | r"^content/common/gpu/client/gl_helper_benchmark\.cc$", |
| 3100 | r"^courgette/courgette_minimal_tool\.cc$", |
| 3101 | r"^courgette/courgette_tool\.cc$", |
| 3102 | r"^extensions/renderer/logging_native_handler\.cc$", |
| 3103 | r"^fuchsia_web/common/init_logging\.cc$", |
| 3104 | r"^fuchsia_web/runners/common/web_component\.cc$", |
Caroline Liu | a705013 | 2023-02-13 22:23:15 | [diff] [blame] | 3105 | r"^fuchsia_web/shell/.*\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3106 | r"^headless/app/headless_shell\.cc$", |
| 3107 | r"^ipc/ipc_logging\.cc$", |
| 3108 | r"^native_client_sdk/", |
| 3109 | r"^remoting/base/logging\.h$", |
| 3110 | r"^remoting/host/.*", |
| 3111 | r"^sandbox/linux/.*", |
| 3112 | r"^storage/browser/file_system/dump_file_system\.cc$", |
| 3113 | r"^tools/", |
| 3114 | r"^ui/base/resource/data_pack\.cc$", |
| 3115 | r"^ui/aura/bench/bench_main\.cc$", |
| 3116 | r"^ui/ozone/platform/cast/", |
| 3117 | r"^ui/base/x/xwmstartupcheck/" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3118 | r"xwmstartupcheck\.cc$")) |
| 3119 | source_file_filter = lambda x: input_api.FilterSourceFile( |
| 3120 | x, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip) |
thakis@chromium.org | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3121 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3122 | log_info = set([]) |
| 3123 | printf = set([]) |
thakis@chromium.org | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3124 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3125 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 3126 | for _, line in f.ChangedContents(): |
| 3127 | if input_api.re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", line): |
| 3128 | log_info.add(f.LocalPath()) |
| 3129 | elif input_api.re.search(r"\bD?LOG_IF\s*\(\s*INFO\s*,", line): |
| 3130 | log_info.add(f.LocalPath()) |
jln@chromium.org | 18b466b | 2013-12-02 22:01:37 | [diff] [blame] | 3131 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3132 | if input_api.re.search(r"\bprintf\(", line): |
| 3133 | printf.add(f.LocalPath()) |
| 3134 | elif input_api.re.search(r"\bfprintf\((stdout|stderr)", line): |
| 3135 | printf.add(f.LocalPath()) |
thakis@chromium.org | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3136 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3137 | if log_info: |
| 3138 | return [ |
| 3139 | output_api.PresubmitError( |
| 3140 | 'These files spam the console log with LOG(INFO):', |
| 3141 | items=log_info) |
| 3142 | ] |
| 3143 | if printf: |
| 3144 | return [ |
| 3145 | output_api.PresubmitError( |
| 3146 | 'These files spam the console log with printf/fprintf:', |
| 3147 | items=printf) |
| 3148 | ] |
| 3149 | return [] |
thakis@chromium.org | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3150 | |
| 3151 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3152 | def CheckForAnonymousVariables(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3153 | """These types are all expected to hold locks while in scope and |
| 3154 | so should never be anonymous (which causes them to be immediately |
| 3155 | destroyed).""" |
| 3156 | they_who_must_be_named = [ |
| 3157 | 'base::AutoLock', |
| 3158 | 'base::AutoReset', |
| 3159 | 'base::AutoUnlock', |
| 3160 | 'SkAutoAlphaRestore', |
| 3161 | 'SkAutoBitmapShaderInstall', |
| 3162 | 'SkAutoBlitterChoose', |
| 3163 | 'SkAutoBounderCommit', |
| 3164 | 'SkAutoCallProc', |
| 3165 | 'SkAutoCanvasRestore', |
| 3166 | 'SkAutoCommentBlock', |
| 3167 | 'SkAutoDescriptor', |
| 3168 | 'SkAutoDisableDirectionCheck', |
| 3169 | 'SkAutoDisableOvalCheck', |
| 3170 | 'SkAutoFree', |
| 3171 | 'SkAutoGlyphCache', |
| 3172 | 'SkAutoHDC', |
| 3173 | 'SkAutoLockColors', |
| 3174 | 'SkAutoLockPixels', |
| 3175 | 'SkAutoMalloc', |
| 3176 | 'SkAutoMaskFreeImage', |
| 3177 | 'SkAutoMutexAcquire', |
| 3178 | 'SkAutoPathBoundsUpdate', |
| 3179 | 'SkAutoPDFRelease', |
| 3180 | 'SkAutoRasterClipValidate', |
| 3181 | 'SkAutoRef', |
| 3182 | 'SkAutoTime', |
| 3183 | 'SkAutoTrace', |
| 3184 | 'SkAutoUnref', |
| 3185 | ] |
| 3186 | anonymous = r'(%s)\s*[({]' % '|'.join(they_who_must_be_named) |
| 3187 | # bad: base::AutoLock(lock.get()); |
| 3188 | # not bad: base::AutoLock lock(lock.get()); |
| 3189 | bad_pattern = input_api.re.compile(anonymous) |
| 3190 | # good: new base::AutoLock(lock.get()) |
| 3191 | good_pattern = input_api.re.compile(r'\bnew\s*' + anonymous) |
| 3192 | errors = [] |
enne@chromium.org | 49aa76a | 2013-12-04 06:59:16 | [diff] [blame] | 3193 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3194 | for f in input_api.AffectedFiles(): |
| 3195 | if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')): |
| 3196 | continue |
| 3197 | for linenum, line in f.ChangedContents(): |
| 3198 | if bad_pattern.search(line) and not good_pattern.search(line): |
| 3199 | errors.append('%s:%d' % (f.LocalPath(), linenum)) |
enne@chromium.org | 49aa76a | 2013-12-04 06:59:16 | [diff] [blame] | 3200 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3201 | if errors: |
| 3202 | return [ |
| 3203 | output_api.PresubmitError( |
| 3204 | 'These lines create anonymous variables that need to be named:', |
| 3205 | items=errors) |
| 3206 | ] |
| 3207 | return [] |
enne@chromium.org | 49aa76a | 2013-12-04 06:59:16 | [diff] [blame] | 3208 | |
| 3209 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3210 | def CheckUniquePtrOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3211 | # Returns whether |template_str| is of the form <T, U...> for some types T |
| 3212 | # and U. Assumes that |template_str| is already in the form <...>. |
| 3213 | def HasMoreThanOneArg(template_str): |
| 3214 | # Level of <...> nesting. |
| 3215 | nesting = 0 |
| 3216 | for c in template_str: |
| 3217 | if c == '<': |
| 3218 | nesting += 1 |
| 3219 | elif c == '>': |
| 3220 | nesting -= 1 |
| 3221 | elif c == ',' and nesting == 1: |
| 3222 | return True |
| 3223 | return False |
Vaclav Brozek | b7fadb69 | 2018-08-30 06:39:53 | [diff] [blame] | 3224 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3225 | file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS] |
| 3226 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 3227 | affected_file, |
| 3228 | files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api. |
| 3229 | DEFAULT_FILES_TO_SKIP), |
| 3230 | files_to_check=file_inclusion_pattern) |
Vaclav Brozek | a54c528b | 2018-04-06 19:23:55 | [diff] [blame] | 3231 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3232 | # Pattern to capture a single "<...>" block of template arguments. It can |
| 3233 | # handle linearly nested blocks, such as "<std::vector<std::set<T>>>", but |
| 3234 | # cannot handle branching structures, such as "<pair<set<T>,set<U>>". The |
| 3235 | # latter would likely require counting that < and > match, which is not |
| 3236 | # expressible in regular languages. Should the need arise, one can introduce |
| 3237 | # limited counting (matching up to a total number of nesting depth), which |
| 3238 | # should cover all practical cases for already a low nesting limit. |
| 3239 | template_arg_pattern = ( |
| 3240 | r'<[^>]*' # Opening block of <. |
| 3241 | r'>([^<]*>)?') # Closing block of >. |
| 3242 | # Prefix expressing that whatever follows is not already inside a <...> |
| 3243 | # block. |
| 3244 | not_inside_template_arg_pattern = r'(^|[^<,\s]\s*)' |
| 3245 | null_construct_pattern = input_api.re.compile( |
| 3246 | not_inside_template_arg_pattern + r'\bstd::unique_ptr' + |
| 3247 | template_arg_pattern + r'\(\)') |
Vaclav Brozek | a54c528b | 2018-04-06 19:23:55 | [diff] [blame] | 3248 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3249 | # Same as template_arg_pattern, but excluding type arrays, e.g., <T[]>. |
| 3250 | template_arg_no_array_pattern = ( |
| 3251 | r'<[^>]*[^]]' # Opening block of <. |
| 3252 | r'>([^(<]*[^]]>)?') # Closing block of >. |
| 3253 | # Prefix saying that what follows is the start of an expression. |
| 3254 | start_of_expr_pattern = r'(=|\breturn|^)\s*' |
| 3255 | # Suffix saying that what follows are call parentheses with a non-empty list |
| 3256 | # of arguments. |
| 3257 | nonempty_arg_list_pattern = r'\(([^)]|$)' |
| 3258 | # Put the template argument into a capture group for deeper examination later. |
| 3259 | return_construct_pattern = input_api.re.compile( |
| 3260 | start_of_expr_pattern + r'std::unique_ptr' + '(?P<template_arg>' + |
| 3261 | template_arg_no_array_pattern + ')' + nonempty_arg_list_pattern) |
Vaclav Brozek | a54c528b | 2018-04-06 19:23:55 | [diff] [blame] | 3262 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3263 | problems_constructor = [] |
| 3264 | problems_nullptr = [] |
| 3265 | for f in input_api.AffectedSourceFiles(sources): |
| 3266 | for line_number, line in f.ChangedContents(): |
| 3267 | # Disallow: |
| 3268 | # return std::unique_ptr<T>(foo); |
| 3269 | # bar = std::unique_ptr<T>(foo); |
| 3270 | # But allow: |
| 3271 | # return std::unique_ptr<T[]>(foo); |
| 3272 | # bar = std::unique_ptr<T[]>(foo); |
| 3273 | # And also allow cases when the second template argument is present. Those |
| 3274 | # cases cannot be handled by std::make_unique: |
| 3275 | # return std::unique_ptr<T, U>(foo); |
| 3276 | # bar = std::unique_ptr<T, U>(foo); |
| 3277 | local_path = f.LocalPath() |
| 3278 | return_construct_result = return_construct_pattern.search(line) |
| 3279 | if return_construct_result and not HasMoreThanOneArg( |
| 3280 | return_construct_result.group('template_arg')): |
| 3281 | problems_constructor.append( |
| 3282 | '%s:%d\n %s' % (local_path, line_number, line.strip())) |
| 3283 | # Disallow: |
| 3284 | # std::unique_ptr<T>() |
| 3285 | if null_construct_pattern.search(line): |
| 3286 | problems_nullptr.append( |
| 3287 | '%s:%d\n %s' % (local_path, line_number, line.strip())) |
Vaclav Brozek | 851d960 | 2018-04-04 16:13:05 | [diff] [blame] | 3288 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3289 | errors = [] |
| 3290 | if problems_nullptr: |
| 3291 | errors.append( |
| 3292 | output_api.PresubmitPromptWarning( |
| 3293 | 'The following files use std::unique_ptr<T>(). Use nullptr instead.', |
| 3294 | problems_nullptr)) |
| 3295 | if problems_constructor: |
| 3296 | errors.append( |
| 3297 | output_api.PresubmitError( |
| 3298 | 'The following files use explicit std::unique_ptr constructor. ' |
| 3299 | 'Use std::make_unique<T>() instead, or use base::WrapUnique if ' |
| 3300 | 'std::make_unique is not an option.', problems_constructor)) |
| 3301 | return errors |
Peter Kasting | 4844e46e | 2018-02-23 07:27:10 | [diff] [blame] | 3302 | |
| 3303 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3304 | def CheckUserActionUpdate(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3305 | """Checks if any new user action has been added.""" |
| 3306 | if any('actions.xml' == input_api.os_path.basename(f) |
| 3307 | for f in input_api.LocalPaths()): |
| 3308 | # If actions.xml is already included in the changelist, the PRESUBMIT |
| 3309 | # for actions.xml will do a more complete presubmit check. |
| 3310 | return [] |
| 3311 | |
| 3312 | file_inclusion_pattern = [r'.*\.(cc|mm)$'] |
| 3313 | files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 3314 | input_api.DEFAULT_FILES_TO_SKIP) |
| 3315 | file_filter = lambda f: input_api.FilterSourceFile( |
| 3316 | f, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip) |
| 3317 | |
| 3318 | action_re = r'[^a-zA-Z]UserMetricsAction\("([^"]*)' |
| 3319 | current_actions = None |
| 3320 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3321 | for line_num, line in f.ChangedContents(): |
| 3322 | match = input_api.re.search(action_re, line) |
| 3323 | if match: |
| 3324 | # Loads contents in tools/metrics/actions/actions.xml to memory. It's |
| 3325 | # loaded only once. |
| 3326 | if not current_actions: |
Bruce Dawson | 6cb2d4d | 2023-03-01 21:35:09 | [diff] [blame] | 3327 | with open('tools/metrics/actions/actions.xml', |
| 3328 | encoding='utf-8') as actions_f: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3329 | current_actions = actions_f.read() |
| 3330 | # Search for the matched user action name in |current_actions|. |
| 3331 | for action_name in match.groups(): |
| 3332 | action = 'name="{0}"'.format(action_name) |
| 3333 | if action not in current_actions: |
| 3334 | return [ |
| 3335 | output_api.PresubmitPromptWarning( |
| 3336 | 'File %s line %d: %s is missing in ' |
| 3337 | 'tools/metrics/actions/actions.xml. Please run ' |
| 3338 | 'tools/metrics/actions/extract_actions.py to update.' |
| 3339 | % (f.LocalPath(), line_num, action_name)) |
| 3340 | ] |
yiyaoliu@chromium.org | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 3341 | return [] |
| 3342 | |
yiyaoliu@chromium.org | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 3343 | |
Daniel Cheng | 13ca61a88 | 2017-08-25 15:11:25 | [diff] [blame] | 3344 | def _ImportJSONCommentEater(input_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3345 | import sys |
| 3346 | sys.path = sys.path + [ |
| 3347 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 3348 | 'json_comment_eater') |
| 3349 | ] |
| 3350 | import json_comment_eater |
| 3351 | return json_comment_eater |
Daniel Cheng | 13ca61a88 | 2017-08-25 15:11:25 | [diff] [blame] | 3352 | |
| 3353 | |
yoz@chromium.org | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 3354 | def _GetJSONParseError(input_api, filename, eat_comments=True): |
dcheng | e07de81 | 2016-06-20 19:27:17 | [diff] [blame] | 3355 | try: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3356 | contents = input_api.ReadFile(filename) |
| 3357 | if eat_comments: |
| 3358 | json_comment_eater = _ImportJSONCommentEater(input_api) |
| 3359 | contents = json_comment_eater.Nom(contents) |
dcheng | e07de81 | 2016-06-20 19:27:17 | [diff] [blame] | 3360 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3361 | input_api.json.loads(contents) |
| 3362 | except ValueError as e: |
| 3363 | return e |
Andrew Grieve | 4deedb1 | 2022-02-03 21:34:50 | [diff] [blame] | 3364 | return None |
| 3365 | |
| 3366 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3367 | def _GetIDLParseError(input_api, filename): |
| 3368 | try: |
| 3369 | contents = input_api.ReadFile(filename) |
Devlin Cronin | f7582a1 | 2022-04-21 21:14:28 | [diff] [blame] | 3370 | for i, char in enumerate(contents): |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3371 | if not char.isascii(): |
| 3372 | return ( |
| 3373 | 'Non-ascii character "%s" (ord %d) found at offset %d.' % |
| 3374 | (char, ord(char), i)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3375 | idl_schema = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 3376 | 'tools', 'json_schema_compiler', |
| 3377 | 'idl_schema.py') |
| 3378 | process = input_api.subprocess.Popen( |
Bruce Dawson | 679fb08 | 2022-04-14 00:47:28 | [diff] [blame] | 3379 | [input_api.python3_executable, idl_schema], |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3380 | stdin=input_api.subprocess.PIPE, |
| 3381 | stdout=input_api.subprocess.PIPE, |
| 3382 | stderr=input_api.subprocess.PIPE, |
| 3383 | universal_newlines=True) |
| 3384 | (_, error) = process.communicate(input=contents) |
| 3385 | return error or None |
| 3386 | except ValueError as e: |
| 3387 | return e |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 3388 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 3389 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3390 | def CheckParseErrors(input_api, output_api): |
| 3391 | """Check that IDL and JSON files do not contain syntax errors.""" |
| 3392 | actions = { |
| 3393 | '.idl': _GetIDLParseError, |
| 3394 | '.json': _GetJSONParseError, |
| 3395 | } |
| 3396 | # Most JSON files are preprocessed and support comments, but these do not. |
| 3397 | json_no_comments_patterns = [ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3398 | r'^testing/', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3399 | ] |
| 3400 | # Only run IDL checker on files in these directories. |
| 3401 | idl_included_patterns = [ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3402 | r'^chrome/common/extensions/api/', |
| 3403 | r'^extensions/common/api/', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3404 | ] |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 3405 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3406 | def get_action(affected_file): |
| 3407 | filename = affected_file.LocalPath() |
| 3408 | return actions.get(input_api.os_path.splitext(filename)[1]) |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 3409 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3410 | def FilterFile(affected_file): |
| 3411 | action = get_action(affected_file) |
| 3412 | if not action: |
| 3413 | return False |
| 3414 | path = affected_file.LocalPath() |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 3415 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3416 | if _MatchesFile(input_api, |
| 3417 | _KNOWN_TEST_DATA_AND_INVALID_JSON_FILE_PATTERNS, path): |
| 3418 | return False |
| 3419 | |
| 3420 | if (action == _GetIDLParseError |
| 3421 | and not _MatchesFile(input_api, idl_included_patterns, path)): |
| 3422 | return False |
| 3423 | return True |
| 3424 | |
| 3425 | results = [] |
| 3426 | for affected_file in input_api.AffectedFiles(file_filter=FilterFile, |
| 3427 | include_deletes=False): |
| 3428 | action = get_action(affected_file) |
| 3429 | kwargs = {} |
| 3430 | if (action == _GetJSONParseError |
| 3431 | and _MatchesFile(input_api, json_no_comments_patterns, |
| 3432 | affected_file.LocalPath())): |
| 3433 | kwargs['eat_comments'] = False |
| 3434 | parse_error = action(input_api, affected_file.AbsoluteLocalPath(), |
| 3435 | **kwargs) |
| 3436 | if parse_error: |
| 3437 | results.append( |
| 3438 | output_api.PresubmitError( |
| 3439 | '%s could not be parsed: %s' % |
| 3440 | (affected_file.LocalPath(), parse_error))) |
| 3441 | return results |
| 3442 | |
| 3443 | |
| 3444 | def CheckJavaStyle(input_api, output_api): |
| 3445 | """Runs checkstyle on changed java files and returns errors if any exist.""" |
| 3446 | |
| 3447 | # Return early if no java files were modified. |
| 3448 | if not any( |
| 3449 | _IsJavaFile(input_api, f.LocalPath()) |
| 3450 | for f in input_api.AffectedFiles()): |
| 3451 | return [] |
| 3452 | |
| 3453 | import sys |
| 3454 | original_sys_path = sys.path |
| 3455 | try: |
| 3456 | sys.path = sys.path + [ |
| 3457 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 3458 | 'android', 'checkstyle') |
| 3459 | ] |
| 3460 | import checkstyle |
| 3461 | finally: |
| 3462 | # Restore sys.path to what it was before. |
| 3463 | sys.path = original_sys_path |
| 3464 | |
Andrew Grieve | 4f88e3ca | 2022-11-22 19:09:20 | [diff] [blame] | 3465 | return checkstyle.run_presubmit( |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3466 | input_api, |
| 3467 | output_api, |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3468 | files_to_skip=_EXCLUDED_PATHS + input_api.DEFAULT_FILES_TO_SKIP) |
| 3469 | |
| 3470 | |
| 3471 | def CheckPythonDevilInit(input_api, output_api): |
| 3472 | """Checks to make sure devil is initialized correctly in python scripts.""" |
| 3473 | script_common_initialize_pattern = input_api.re.compile( |
| 3474 | r'script_common\.InitializeEnvironment\(') |
| 3475 | devil_env_config_initialize = input_api.re.compile( |
| 3476 | r'devil_env\.config\.Initialize\(') |
| 3477 | |
| 3478 | errors = [] |
| 3479 | |
| 3480 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 3481 | affected_file, |
| 3482 | files_to_skip=(_EXCLUDED_PATHS + input_api.DEFAULT_FILES_TO_SKIP + ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3483 | r'^build/android/devil_chromium\.py', |
| 3484 | r'^third_party/.*', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3485 | )), |
| 3486 | files_to_check=[r'.*\.py$']) |
| 3487 | |
| 3488 | for f in input_api.AffectedSourceFiles(sources): |
| 3489 | for line_num, line in f.ChangedContents(): |
| 3490 | if (script_common_initialize_pattern.search(line) |
| 3491 | or devil_env_config_initialize.search(line)): |
| 3492 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 3493 | |
| 3494 | results = [] |
| 3495 | |
| 3496 | if errors: |
| 3497 | results.append( |
| 3498 | output_api.PresubmitError( |
| 3499 | 'Devil initialization should always be done using ' |
| 3500 | 'devil_chromium.Initialize() in the chromium project, to use better ' |
| 3501 | 'defaults for dependencies (ex. up-to-date version of adb).', |
| 3502 | errors)) |
| 3503 | |
| 3504 | return results |
| 3505 | |
| 3506 | |
| 3507 | def _MatchesFile(input_api, patterns, path): |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3508 | # Consistently use / as path separator to simplify the writing of regex |
| 3509 | # expressions. |
| 3510 | path = path.replace(input_api.os_path.sep, '/') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3511 | for pattern in patterns: |
| 3512 | if input_api.re.search(pattern, path): |
| 3513 | return True |
| 3514 | return False |
| 3515 | |
| 3516 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3517 | def _ChangeHasSecurityReviewer(input_api, owners_file): |
| 3518 | """Returns True iff the CL has a reviewer from SECURITY_OWNERS. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3519 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3520 | Args: |
| 3521 | input_api: The presubmit input API. |
| 3522 | owners_file: OWNERS file with required reviewers. Typically, this is |
| 3523 | something like ipc/SECURITY_OWNERS. |
| 3524 | |
| 3525 | Note: if the presubmit is running for commit rather than for upload, this |
| 3526 | only returns True if a security reviewer has also approved the CL. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3527 | """ |
Daniel Cheng | d8824447 | 2022-05-16 09:08:47 | [diff] [blame] | 3528 | # Owners-Override should bypass all additional OWNERS enforcement checks. |
| 3529 | # A CR+1 vote will still be required to land this change. |
| 3530 | if (input_api.change.issue and input_api.gerrit.IsOwnersOverrideApproved( |
| 3531 | input_api.change.issue)): |
| 3532 | return True |
| 3533 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3534 | owner_email, reviewers = ( |
| 3535 | input_api.canned_checks.GetCodereviewOwnerAndReviewers( |
Daniel Cheng | 3008dc1 | 2022-05-13 04:02:11 | [diff] [blame] | 3536 | input_api, |
| 3537 | None, |
| 3538 | approval_needed=input_api.is_committing and not input_api.dry_run)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3539 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3540 | security_owners = input_api.owners_client.ListOwners(owners_file) |
| 3541 | return any(owner in reviewers for owner in security_owners) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3542 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3543 | |
| 3544 | @dataclass |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3545 | class _SecurityProblemWithItems: |
| 3546 | problem: str |
| 3547 | items: Sequence[str] |
| 3548 | |
| 3549 | |
| 3550 | @dataclass |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3551 | class _MissingSecurityOwnersResult: |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3552 | owners_file_problems: Sequence[_SecurityProblemWithItems] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3553 | has_security_sensitive_files: bool |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3554 | missing_reviewer_problem: Optional[_SecurityProblemWithItems] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3555 | |
| 3556 | |
| 3557 | def _FindMissingSecurityOwners(input_api, |
| 3558 | output_api, |
| 3559 | file_patterns: Sequence[str], |
| 3560 | excluded_patterns: Sequence[str], |
| 3561 | required_owners_file: str, |
| 3562 | custom_rule_function: Optional[Callable] = None |
| 3563 | ) -> _MissingSecurityOwnersResult: |
| 3564 | """Find OWNERS files missing per-file rules for security-sensitive files. |
| 3565 | |
| 3566 | Args: |
| 3567 | input_api: the PRESUBMIT input API object. |
| 3568 | output_api: the PRESUBMIT output API object. |
| 3569 | file_patterns: basename patterns that require a corresponding per-file |
| 3570 | security restriction. |
| 3571 | excluded_patterns: path patterns that should be exempted from |
| 3572 | requiring a security restriction. |
| 3573 | required_owners_file: path to the required OWNERS file, e.g. |
| 3574 | ipc/SECURITY_OWNERS |
| 3575 | cc_alias: If not None, email that will be CCed automatically if the |
| 3576 | change contains security-sensitive files, as determined by |
| 3577 | `file_patterns` and `excluded_patterns`. |
| 3578 | custom_rule_function: If not None, will be called with `input_api` and |
| 3579 | the current file under consideration. Returning True will add an |
| 3580 | exact match per-file rule check for the current file. |
| 3581 | """ |
| 3582 | |
| 3583 | # `to_check` is a mapping of an OWNERS file path to Patterns. |
| 3584 | # |
| 3585 | # Patterns is a dictionary mapping glob patterns (suitable for use in |
| 3586 | # per-file rules) to a PatternEntry. |
| 3587 | # |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3588 | # PatternEntry is a dictionary with two keys: |
| 3589 | # - 'files': the files that are matched by this pattern |
| 3590 | # - 'rules': the per-file rules needed for this pattern |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3591 | # |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3592 | # For example, if we expect OWNERS file to contain rules for *.mojom and |
| 3593 | # *_struct_traits*.*, Patterns might look like this: |
| 3594 | # { |
| 3595 | # '*.mojom': { |
| 3596 | # 'files': ..., |
| 3597 | # 'rules': [ |
| 3598 | # 'per-file *.mojom=set noparent', |
| 3599 | # 'per-file *.mojom=file://ipc/SECURITY_OWNERS', |
| 3600 | # ], |
| 3601 | # }, |
| 3602 | # '*_struct_traits*.*': { |
| 3603 | # 'files': ..., |
| 3604 | # 'rules': [ |
| 3605 | # 'per-file *_struct_traits*.*=set noparent', |
| 3606 | # 'per-file *_struct_traits*.*=file://ipc/SECURITY_OWNERS', |
| 3607 | # ], |
| 3608 | # }, |
| 3609 | # } |
| 3610 | to_check = {} |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3611 | files_to_review = [] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3612 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3613 | def AddPatternToCheck(file, pattern): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3614 | owners_file = input_api.os_path.join( |
Daniel Cheng | d8824447 | 2022-05-16 09:08:47 | [diff] [blame] | 3615 | input_api.os_path.dirname(file.LocalPath()), 'OWNERS') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3616 | if owners_file not in to_check: |
| 3617 | to_check[owners_file] = {} |
| 3618 | if pattern not in to_check[owners_file]: |
| 3619 | to_check[owners_file][pattern] = { |
| 3620 | 'files': [], |
| 3621 | 'rules': [ |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3622 | f'per-file {pattern}=set noparent', |
| 3623 | f'per-file {pattern}=file://{required_owners_file}', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3624 | ] |
| 3625 | } |
Daniel Cheng | ed57a16 | 2022-05-25 02:56:34 | [diff] [blame] | 3626 | to_check[owners_file][pattern]['files'].append(file.LocalPath()) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3627 | files_to_review.append(file.LocalPath()) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3628 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3629 | # Only enforce security OWNERS rules for a directory if that directory has a |
| 3630 | # file that matches `file_patterns`. For example, if a directory only |
| 3631 | # contains *.mojom files and no *_messages*.h files, the check should only |
| 3632 | # ensure that rules for *.mojom files are present. |
| 3633 | for file in input_api.AffectedFiles(include_deletes=False): |
| 3634 | file_basename = input_api.os_path.basename(file.LocalPath()) |
| 3635 | if custom_rule_function is not None and custom_rule_function( |
| 3636 | input_api, file): |
| 3637 | AddPatternToCheck(file, file_basename) |
| 3638 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3639 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3640 | if any( |
| 3641 | input_api.fnmatch.fnmatch(file.LocalPath(), pattern) |
| 3642 | for pattern in excluded_patterns): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3643 | continue |
| 3644 | |
| 3645 | for pattern in file_patterns: |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3646 | # Unlike `excluded_patterns`, `file_patterns` is checked only against the |
| 3647 | # file's basename. |
| 3648 | if input_api.fnmatch.fnmatch(file_basename, pattern): |
| 3649 | AddPatternToCheck(file, pattern) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3650 | break |
| 3651 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3652 | has_security_sensitive_files = bool(to_check) |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3653 | |
| 3654 | # Check if any newly added lines in OWNERS files intersect with required |
| 3655 | # per-file OWNERS lines. If so, ensure that a security reviewer is included. |
| 3656 | # This is a hack, but is needed because the OWNERS check (by design) ignores |
| 3657 | # new OWNERS entries; otherwise, a non-owner could add someone as a new |
| 3658 | # OWNER and have that newly-added OWNER self-approve their own addition. |
| 3659 | newly_covered_files = [] |
| 3660 | for file in input_api.AffectedFiles(include_deletes=False): |
| 3661 | if not file.LocalPath() in to_check: |
| 3662 | continue |
| 3663 | for _, line in file.ChangedContents(): |
| 3664 | for _, entry in to_check[file.LocalPath()].items(): |
| 3665 | if line in entry['rules']: |
| 3666 | newly_covered_files.extend(entry['files']) |
| 3667 | |
| 3668 | missing_reviewer_problems = None |
| 3669 | if newly_covered_files and not _ChangeHasSecurityReviewer( |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3670 | input_api, required_owners_file): |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3671 | missing_reviewer_problems = _SecurityProblemWithItems( |
| 3672 | f'Review from an owner in {required_owners_file} is required for ' |
| 3673 | 'the following newly-added files:', |
| 3674 | [f'{file}' for file in sorted(set(newly_covered_files))]) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3675 | |
| 3676 | # Go through the OWNERS files to check, filtering out rules that are already |
| 3677 | # present in that OWNERS file. |
| 3678 | for owners_file, patterns in to_check.items(): |
| 3679 | try: |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3680 | lines = set( |
| 3681 | input_api.ReadFile( |
| 3682 | input_api.os_path.join(input_api.change.RepositoryRoot(), |
| 3683 | owners_file)).splitlines()) |
| 3684 | for entry in patterns.values(): |
| 3685 | entry['rules'] = [ |
| 3686 | rule for rule in entry['rules'] if rule not in lines |
| 3687 | ] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3688 | except IOError: |
| 3689 | # No OWNERS file, so all the rules are definitely missing. |
| 3690 | continue |
| 3691 | |
| 3692 | # All the remaining lines weren't found in OWNERS files, so emit an error. |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3693 | owners_file_problems = [] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3694 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3695 | for owners_file, patterns in to_check.items(): |
| 3696 | missing_lines = [] |
| 3697 | files = [] |
| 3698 | for _, entry in patterns.items(): |
Daniel Cheng | ed57a16 | 2022-05-25 02:56:34 | [diff] [blame] | 3699 | files.extend(entry['files']) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3700 | missing_lines.extend(entry['rules']) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3701 | if missing_lines: |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3702 | joined_missing_lines = '\n'.join(line for line in missing_lines) |
| 3703 | owners_file_problems.append( |
| 3704 | _SecurityProblemWithItems( |
| 3705 | 'Found missing OWNERS lines for security-sensitive files. ' |
| 3706 | f'Please add the following lines to {owners_file}:\n' |
| 3707 | f'{joined_missing_lines}\n\nTo ensure security review for:', |
| 3708 | files)) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3709 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3710 | return _MissingSecurityOwnersResult(owners_file_problems, |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3711 | has_security_sensitive_files, |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3712 | missing_reviewer_problems) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3713 | |
| 3714 | |
| 3715 | def _CheckChangeForIpcSecurityOwners(input_api, output_api): |
| 3716 | # Whether or not a file affects IPC is (mostly) determined by a simple list |
| 3717 | # of filename patterns. |
| 3718 | file_patterns = [ |
| 3719 | # Legacy IPC: |
| 3720 | '*_messages.cc', |
| 3721 | '*_messages*.h', |
| 3722 | '*_param_traits*.*', |
| 3723 | # Mojo IPC: |
| 3724 | '*.mojom', |
| 3725 | '*_mojom_traits*.*', |
| 3726 | '*_type_converter*.*', |
| 3727 | # Android native IPC: |
| 3728 | '*.aidl', |
| 3729 | ] |
| 3730 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3731 | excluded_patterns = [ |
Daniel Cheng | 518943f | 2022-05-12 22:15:46 | [diff] [blame] | 3732 | # These third_party directories do not contain IPCs, but contain files |
| 3733 | # matching the above patterns, which trigger false positives. |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3734 | 'third_party/crashpad/*', |
| 3735 | 'third_party/blink/renderer/platform/bindings/*', |
| 3736 | 'third_party/protobuf/benchmarks/python/*', |
| 3737 | 'third_party/win_build_output/*', |
Daniel Cheng | d8824447 | 2022-05-16 09:08:47 | [diff] [blame] | 3738 | # Enum-only mojoms used for web metrics, so no security review needed. |
| 3739 | 'third_party/blink/public/mojom/use_counter/metrics/*', |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3740 | # These files are just used to communicate between class loaders running |
| 3741 | # in the same process. |
| 3742 | 'weblayer/browser/java/org/chromium/weblayer_private/interfaces/*', |
| 3743 | 'weblayer/browser/java/org/chromium/weblayer_private/test_interfaces/*', |
| 3744 | ] |
| 3745 | |
| 3746 | def IsMojoServiceManifestFile(input_api, file): |
| 3747 | manifest_pattern = input_api.re.compile('manifests?\.(cc|h)$') |
| 3748 | test_manifest_pattern = input_api.re.compile('test_manifests?\.(cc|h)') |
| 3749 | if not manifest_pattern.search(file.LocalPath()): |
| 3750 | return False |
| 3751 | |
| 3752 | if test_manifest_pattern.search(file.LocalPath()): |
| 3753 | return False |
| 3754 | |
| 3755 | # All actual service manifest files should contain at least one |
| 3756 | # qualified reference to service_manager::Manifest. |
| 3757 | return any('service_manager::Manifest' in line |
| 3758 | for line in file.NewContents()) |
| 3759 | |
| 3760 | return _FindMissingSecurityOwners( |
| 3761 | input_api, |
| 3762 | output_api, |
| 3763 | file_patterns, |
| 3764 | excluded_patterns, |
| 3765 | 'ipc/SECURITY_OWNERS', |
| 3766 | custom_rule_function=IsMojoServiceManifestFile) |
| 3767 | |
| 3768 | |
| 3769 | def _CheckChangeForFuchsiaSecurityOwners(input_api, output_api): |
| 3770 | file_patterns = [ |
| 3771 | # Component specifications. |
| 3772 | '*.cml', # Component Framework v2. |
| 3773 | '*.cmx', # Component Framework v1. |
| 3774 | |
| 3775 | # Fuchsia IDL protocol specifications. |
| 3776 | '*.fidl', |
| 3777 | ] |
| 3778 | |
| 3779 | # Don't check for owners files for changes in these directories. |
| 3780 | excluded_patterns = [ |
| 3781 | 'third_party/crashpad/*', |
| 3782 | ] |
| 3783 | |
| 3784 | return _FindMissingSecurityOwners(input_api, output_api, file_patterns, |
| 3785 | excluded_patterns, |
| 3786 | 'build/fuchsia/SECURITY_OWNERS') |
| 3787 | |
| 3788 | |
| 3789 | def CheckSecurityOwners(input_api, output_api): |
| 3790 | """Checks that various security-sensitive files have an IPC OWNERS rule.""" |
| 3791 | ipc_results = _CheckChangeForIpcSecurityOwners(input_api, output_api) |
| 3792 | fuchsia_results = _CheckChangeForFuchsiaSecurityOwners( |
| 3793 | input_api, output_api) |
| 3794 | |
| 3795 | if ipc_results.has_security_sensitive_files: |
| 3796 | output_api.AppendCC('ipc-security-reviews@chromium.org') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3797 | |
| 3798 | results = [] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3799 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3800 | missing_reviewer_problems = [] |
| 3801 | if ipc_results.missing_reviewer_problem: |
| 3802 | missing_reviewer_problems.append(ipc_results.missing_reviewer_problem) |
| 3803 | if fuchsia_results.missing_reviewer_problem: |
| 3804 | missing_reviewer_problems.append( |
| 3805 | fuchsia_results.missing_reviewer_problem) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3806 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3807 | # Missing reviewers are an error unless there's no issue number |
| 3808 | # associated with this branch; in that case, the presubmit is being run |
| 3809 | # with --all or --files. |
| 3810 | # |
| 3811 | # Note that upload should never be an error; otherwise, it would be |
| 3812 | # impossible to upload changes at all. |
| 3813 | if input_api.is_committing and input_api.change.issue: |
| 3814 | make_presubmit_message = output_api.PresubmitError |
| 3815 | else: |
| 3816 | make_presubmit_message = output_api.PresubmitNotifyResult |
| 3817 | for problem in missing_reviewer_problems: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3818 | results.append( |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3819 | make_presubmit_message(problem.problem, items=problem.items)) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3820 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3821 | owners_file_problems = [] |
| 3822 | owners_file_problems.extend(ipc_results.owners_file_problems) |
| 3823 | owners_file_problems.extend(fuchsia_results.owners_file_problems) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3824 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3825 | for problem in owners_file_problems: |
Daniel Cheng | 3008dc1 | 2022-05-13 04:02:11 | [diff] [blame] | 3826 | # Missing per-file rules are always an error. While swarming and caching |
| 3827 | # means that uploading a patchset with updated OWNERS files and sending |
| 3828 | # it to the CQ again should not have a large incremental cost, it is |
| 3829 | # still frustrating to discover the error only after the change has |
| 3830 | # already been uploaded. |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3831 | results.append( |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 3832 | output_api.PresubmitError(problem.problem, items=problem.items)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3833 | |
| 3834 | return results |
| 3835 | |
| 3836 | |
| 3837 | def _GetFilesUsingSecurityCriticalFunctions(input_api): |
| 3838 | """Checks affected files for changes to security-critical calls. This |
| 3839 | function checks the full change diff, to catch both additions/changes |
| 3840 | and removals. |
| 3841 | |
| 3842 | Returns a dict keyed by file name, and the value is a set of detected |
| 3843 | functions. |
| 3844 | """ |
| 3845 | # Map of function pretty name (displayed in an error) to the pattern to |
| 3846 | # match it with. |
| 3847 | _PATTERNS_TO_CHECK = { |
| 3848 | 'content::GetServiceSandboxType<>()': 'GetServiceSandboxType\\<' |
| 3849 | } |
| 3850 | _PATTERNS_TO_CHECK = { |
| 3851 | k: input_api.re.compile(v) |
| 3852 | for k, v in _PATTERNS_TO_CHECK.items() |
| 3853 | } |
| 3854 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3855 | # We don't want to trigger on strings within this file. |
| 3856 | def presubmit_file_filter(f): |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3857 | return 'PRESUBMIT.py' != input_api.os_path.split(f.LocalPath())[1] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3858 | |
| 3859 | # Scan all affected files for changes touching _FUNCTIONS_TO_CHECK. |
| 3860 | files_to_functions = {} |
| 3861 | for f in input_api.AffectedFiles(file_filter=presubmit_file_filter): |
| 3862 | diff = f.GenerateScmDiff() |
| 3863 | for line in diff.split('\n'): |
| 3864 | # Not using just RightHandSideLines() because removing a |
| 3865 | # call to a security-critical function can be just as important |
| 3866 | # as adding or changing the arguments. |
| 3867 | if line.startswith('-') or (line.startswith('+') |
| 3868 | and not line.startswith('++')): |
| 3869 | for name, pattern in _PATTERNS_TO_CHECK.items(): |
| 3870 | if pattern.search(line): |
| 3871 | path = f.LocalPath() |
| 3872 | if not path in files_to_functions: |
| 3873 | files_to_functions[path] = set() |
| 3874 | files_to_functions[path].add(name) |
| 3875 | return files_to_functions |
| 3876 | |
| 3877 | |
| 3878 | def CheckSecurityChanges(input_api, output_api): |
| 3879 | """Checks that changes involving security-critical functions are reviewed |
| 3880 | by the security team. |
| 3881 | """ |
| 3882 | files_to_functions = _GetFilesUsingSecurityCriticalFunctions(input_api) |
| 3883 | if not len(files_to_functions): |
| 3884 | return [] |
| 3885 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3886 | owners_file = 'ipc/SECURITY_OWNERS' |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3887 | if _ChangeHasSecurityReviewer(input_api, owners_file): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3888 | return [] |
| 3889 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 3890 | msg = 'The following files change calls to security-sensitive functions\n' \ |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3891 | 'that need to be reviewed by {}.\n'.format(owners_file) |
| 3892 | for path, names in files_to_functions.items(): |
| 3893 | msg += ' {}\n'.format(path) |
| 3894 | for name in names: |
| 3895 | msg += ' {}\n'.format(name) |
| 3896 | msg += '\n' |
| 3897 | |
| 3898 | if input_api.is_committing: |
| 3899 | output = output_api.PresubmitError |
Mohamed Heikal | e217fc85 | 2020-07-06 19:44:03 | [diff] [blame] | 3900 | else: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3901 | output = output_api.PresubmitNotifyResult |
| 3902 | return [output(msg)] |
| 3903 | |
| 3904 | |
| 3905 | def CheckSetNoParent(input_api, output_api): |
| 3906 | """Checks that set noparent is only used together with an OWNERS file in |
| 3907 | //build/OWNERS.setnoparent (see also |
| 3908 | //docs/code_reviews.md#owners-files-details) |
| 3909 | """ |
| 3910 | # Return early if no OWNERS files were modified. |
| 3911 | if not any(f.LocalPath().endswith('OWNERS') |
| 3912 | for f in input_api.AffectedFiles(include_deletes=False)): |
| 3913 | return [] |
| 3914 | |
| 3915 | errors = [] |
| 3916 | |
| 3917 | allowed_owners_files_file = 'build/OWNERS.setnoparent' |
| 3918 | allowed_owners_files = set() |
Bruce Dawson | 58a45d2 | 2023-02-27 11:24:16 | [diff] [blame] | 3919 | with open(allowed_owners_files_file, 'r', encoding='utf-8') as f: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3920 | for line in f: |
| 3921 | line = line.strip() |
| 3922 | if not line or line.startswith('#'): |
| 3923 | continue |
| 3924 | allowed_owners_files.add(line) |
| 3925 | |
| 3926 | per_file_pattern = input_api.re.compile('per-file (.+)=(.+)') |
| 3927 | |
| 3928 | for f in input_api.AffectedFiles(include_deletes=False): |
| 3929 | if not f.LocalPath().endswith('OWNERS'): |
| 3930 | continue |
| 3931 | |
| 3932 | found_owners_files = set() |
| 3933 | found_set_noparent_lines = dict() |
| 3934 | |
| 3935 | # Parse the OWNERS file. |
| 3936 | for lineno, line in enumerate(f.NewContents(), 1): |
| 3937 | line = line.strip() |
| 3938 | if line.startswith('set noparent'): |
| 3939 | found_set_noparent_lines[''] = lineno |
| 3940 | if line.startswith('file://'): |
| 3941 | if line in allowed_owners_files: |
| 3942 | found_owners_files.add('') |
| 3943 | if line.startswith('per-file'): |
| 3944 | match = per_file_pattern.match(line) |
| 3945 | if match: |
| 3946 | glob = match.group(1).strip() |
| 3947 | directive = match.group(2).strip() |
| 3948 | if directive == 'set noparent': |
| 3949 | found_set_noparent_lines[glob] = lineno |
| 3950 | if directive.startswith('file://'): |
| 3951 | if directive in allowed_owners_files: |
| 3952 | found_owners_files.add(glob) |
| 3953 | |
| 3954 | # Check that every set noparent line has a corresponding file:// line |
| 3955 | # listed in build/OWNERS.setnoparent. An exception is made for top level |
| 3956 | # directories since src/OWNERS shouldn't review them. |
Bruce Dawson | 6bb0d67 | 2022-04-06 15:13:49 | [diff] [blame] | 3957 | linux_path = f.LocalPath().replace(input_api.os_path.sep, '/') |
| 3958 | if (linux_path.count('/') != 1 |
| 3959 | and (not linux_path in _EXCLUDED_SET_NO_PARENT_PATHS)): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3960 | for set_noparent_line in found_set_noparent_lines: |
| 3961 | if set_noparent_line in found_owners_files: |
| 3962 | continue |
| 3963 | errors.append(' %s:%d' % |
Bruce Dawson | 6bb0d67 | 2022-04-06 15:13:49 | [diff] [blame] | 3964 | (linux_path, |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3965 | found_set_noparent_lines[set_noparent_line])) |
| 3966 | |
| 3967 | results = [] |
| 3968 | if errors: |
| 3969 | if input_api.is_committing: |
| 3970 | output = output_api.PresubmitError |
| 3971 | else: |
| 3972 | output = output_api.PresubmitPromptWarning |
| 3973 | results.append( |
| 3974 | output( |
| 3975 | 'Found the following "set noparent" restrictions in OWNERS files that ' |
| 3976 | 'do not include owners from build/OWNERS.setnoparent:', |
| 3977 | long_text='\n\n'.join(errors))) |
| 3978 | return results |
| 3979 | |
| 3980 | |
| 3981 | def CheckUselessForwardDeclarations(input_api, output_api): |
| 3982 | """Checks that added or removed lines in non third party affected |
| 3983 | header files do not lead to new useless class or struct forward |
| 3984 | declaration. |
| 3985 | """ |
| 3986 | results = [] |
| 3987 | class_pattern = input_api.re.compile(r'^class\s+(\w+);$', |
| 3988 | input_api.re.MULTILINE) |
| 3989 | struct_pattern = input_api.re.compile(r'^struct\s+(\w+);$', |
| 3990 | input_api.re.MULTILINE) |
| 3991 | for f in input_api.AffectedFiles(include_deletes=False): |
| 3992 | if (f.LocalPath().startswith('third_party') |
| 3993 | and not f.LocalPath().startswith('third_party/blink') |
| 3994 | and not f.LocalPath().startswith('third_party\\blink')): |
| 3995 | continue |
| 3996 | |
| 3997 | if not f.LocalPath().endswith('.h'): |
| 3998 | continue |
| 3999 | |
| 4000 | contents = input_api.ReadFile(f) |
| 4001 | fwd_decls = input_api.re.findall(class_pattern, contents) |
| 4002 | fwd_decls.extend(input_api.re.findall(struct_pattern, contents)) |
| 4003 | |
| 4004 | useless_fwd_decls = [] |
| 4005 | for decl in fwd_decls: |
| 4006 | count = sum(1 for _ in input_api.re.finditer( |
| 4007 | r'\b%s\b' % input_api.re.escape(decl), contents)) |
| 4008 | if count == 1: |
| 4009 | useless_fwd_decls.append(decl) |
| 4010 | |
| 4011 | if not useless_fwd_decls: |
| 4012 | continue |
| 4013 | |
| 4014 | for line in f.GenerateScmDiff().splitlines(): |
| 4015 | if (line.startswith('-') and not line.startswith('--') |
| 4016 | or line.startswith('+') and not line.startswith('++')): |
| 4017 | for decl in useless_fwd_decls: |
| 4018 | if input_api.re.search(r'\b%s\b' % decl, line[1:]): |
| 4019 | results.append( |
| 4020 | output_api.PresubmitPromptWarning( |
| 4021 | '%s: %s forward declaration is no longer needed' |
| 4022 | % (f.LocalPath(), decl))) |
| 4023 | useless_fwd_decls.remove(decl) |
| 4024 | |
| 4025 | return results |
| 4026 | |
| 4027 | |
| 4028 | def _CheckAndroidDebuggableBuild(input_api, output_api): |
| 4029 | """Checks that code uses BuildInfo.isDebugAndroid() instead of |
| 4030 | Build.TYPE.equals('') or ''.equals(Build.TYPE) to check if |
| 4031 | this is a debuggable build of Android. |
| 4032 | """ |
| 4033 | build_type_check_pattern = input_api.re.compile( |
| 4034 | r'\bBuild\.TYPE\.equals\(|\.equals\(\s*\bBuild\.TYPE\)') |
| 4035 | |
| 4036 | errors = [] |
| 4037 | |
| 4038 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 4039 | affected_file, |
| 4040 | files_to_skip=( |
| 4041 | _EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api. |
| 4042 | DEFAULT_FILES_TO_SKIP + ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4043 | r"^android_webview/support_library/boundary_interfaces/", |
| 4044 | r"^chrome/android/webapk/.*", |
| 4045 | r'^third_party/.*', |
| 4046 | r"tools/android/customtabs_benchmark/.*", |
| 4047 | r"webview/chromium/License.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4048 | )), |
| 4049 | files_to_check=[r'.*\.java$']) |
| 4050 | |
| 4051 | for f in input_api.AffectedSourceFiles(sources): |
| 4052 | for line_num, line in f.ChangedContents(): |
| 4053 | if build_type_check_pattern.search(line): |
| 4054 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4055 | |
| 4056 | results = [] |
| 4057 | |
| 4058 | if errors: |
| 4059 | results.append( |
| 4060 | output_api.PresubmitPromptWarning( |
| 4061 | 'Build.TYPE.equals or .equals(Build.TYPE) usage is detected.' |
| 4062 | ' Please use BuildInfo.isDebugAndroid() instead.', errors)) |
| 4063 | |
| 4064 | return results |
| 4065 | |
| 4066 | # TODO: add unit tests |
| 4067 | def _CheckAndroidToastUsage(input_api, output_api): |
| 4068 | """Checks that code uses org.chromium.ui.widget.Toast instead of |
| 4069 | android.widget.Toast (Chromium Toast doesn't force hardware |
| 4070 | acceleration on low-end devices, saving memory). |
| 4071 | """ |
| 4072 | toast_import_pattern = input_api.re.compile( |
| 4073 | r'^import android\.widget\.Toast;$') |
| 4074 | |
| 4075 | errors = [] |
| 4076 | |
| 4077 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 4078 | affected_file, |
| 4079 | files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4080 | DEFAULT_FILES_TO_SKIP + (r'^chromecast/.*', |
| 4081 | r'^remoting/.*')), |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4082 | files_to_check=[r'.*\.java$']) |
| 4083 | |
| 4084 | for f in input_api.AffectedSourceFiles(sources): |
| 4085 | for line_num, line in f.ChangedContents(): |
| 4086 | if toast_import_pattern.search(line): |
| 4087 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4088 | |
| 4089 | results = [] |
| 4090 | |
| 4091 | if errors: |
| 4092 | results.append( |
| 4093 | output_api.PresubmitError( |
| 4094 | 'android.widget.Toast usage is detected. Android toasts use hardware' |
| 4095 | ' acceleration, and can be\ncostly on low-end devices. Please use' |
| 4096 | ' org.chromium.ui.widget.Toast instead.\n' |
| 4097 | 'Contact dskiba@chromium.org if you have any questions.', |
| 4098 | errors)) |
| 4099 | |
| 4100 | return results |
| 4101 | |
| 4102 | |
| 4103 | def _CheckAndroidCrLogUsage(input_api, output_api): |
| 4104 | """Checks that new logs using org.chromium.base.Log: |
| 4105 | - Are using 'TAG' as variable name for the tags (warn) |
| 4106 | - Are using a tag that is shorter than 20 characters (error) |
| 4107 | """ |
| 4108 | |
| 4109 | # Do not check format of logs in the given files |
| 4110 | cr_log_check_excluded_paths = [ |
| 4111 | # //chrome/android/webapk cannot depend on //base |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4112 | r"^chrome/android/webapk/.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4113 | # WebView license viewer code cannot depend on //base; used in stub APK. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4114 | r"^android_webview/glue/java/src/com/android/" |
| 4115 | r"webview/chromium/License.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4116 | # The customtabs_benchmark is a small app that does not depend on Chromium |
| 4117 | # java pieces. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4118 | r"tools/android/customtabs_benchmark/.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4119 | ] |
| 4120 | |
| 4121 | cr_log_import_pattern = input_api.re.compile( |
| 4122 | r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE) |
| 4123 | class_in_base_pattern = input_api.re.compile( |
| 4124 | r'^package org\.chromium\.base;$', input_api.re.MULTILINE) |
| 4125 | has_some_log_import_pattern = input_api.re.compile(r'^import .*\.Log;$', |
| 4126 | input_api.re.MULTILINE) |
| 4127 | # Extract the tag from lines like `Log.d(TAG, "*");` or `Log.d("TAG", "*");` |
| 4128 | log_call_pattern = input_api.re.compile(r'\bLog\.\w\((?P<tag>\"?\w+)') |
| 4129 | log_decl_pattern = input_api.re.compile( |
| 4130 | r'static final String TAG = "(?P<name>(.*))"') |
| 4131 | rough_log_decl_pattern = input_api.re.compile(r'\bString TAG\s*=') |
| 4132 | |
| 4133 | REF_MSG = ('See docs/android_logging.md for more info.') |
| 4134 | sources = lambda x: input_api.FilterSourceFile( |
| 4135 | x, |
| 4136 | files_to_check=[r'.*\.java$'], |
| 4137 | files_to_skip=cr_log_check_excluded_paths) |
| 4138 | |
| 4139 | tag_decl_errors = [] |
| 4140 | tag_length_errors = [] |
| 4141 | tag_errors = [] |
| 4142 | tag_with_dot_errors = [] |
| 4143 | util_log_errors = [] |
| 4144 | |
| 4145 | for f in input_api.AffectedSourceFiles(sources): |
| 4146 | file_content = input_api.ReadFile(f) |
| 4147 | has_modified_logs = False |
| 4148 | # Per line checks |
| 4149 | if (cr_log_import_pattern.search(file_content) |
| 4150 | or (class_in_base_pattern.search(file_content) |
| 4151 | and not has_some_log_import_pattern.search(file_content))): |
| 4152 | # Checks to run for files using cr log |
| 4153 | for line_num, line in f.ChangedContents(): |
| 4154 | if rough_log_decl_pattern.search(line): |
| 4155 | has_modified_logs = True |
| 4156 | |
| 4157 | # Check if the new line is doing some logging |
| 4158 | match = log_call_pattern.search(line) |
| 4159 | if match: |
| 4160 | has_modified_logs = True |
| 4161 | |
| 4162 | # Make sure it uses "TAG" |
| 4163 | if not match.group('tag') == 'TAG': |
| 4164 | tag_errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4165 | else: |
| 4166 | # Report non cr Log function calls in changed lines |
| 4167 | for line_num, line in f.ChangedContents(): |
| 4168 | if log_call_pattern.search(line): |
| 4169 | util_log_errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4170 | |
| 4171 | # Per file checks |
| 4172 | if has_modified_logs: |
| 4173 | # Make sure the tag is using the "cr" prefix and is not too long |
| 4174 | match = log_decl_pattern.search(file_content) |
| 4175 | tag_name = match.group('name') if match else None |
| 4176 | if not tag_name: |
| 4177 | tag_decl_errors.append(f.LocalPath()) |
| 4178 | elif len(tag_name) > 20: |
| 4179 | tag_length_errors.append(f.LocalPath()) |
| 4180 | elif '.' in tag_name: |
| 4181 | tag_with_dot_errors.append(f.LocalPath()) |
| 4182 | |
| 4183 | results = [] |
| 4184 | if tag_decl_errors: |
| 4185 | results.append( |
| 4186 | output_api.PresubmitPromptWarning( |
| 4187 | 'Please define your tags using the suggested format: .\n' |
| 4188 | '"private static final String TAG = "<package tag>".\n' |
| 4189 | 'They will be prepended with "cr_" automatically.\n' + REF_MSG, |
| 4190 | tag_decl_errors)) |
| 4191 | |
| 4192 | if tag_length_errors: |
| 4193 | results.append( |
| 4194 | output_api.PresubmitError( |
| 4195 | 'The tag length is restricted by the system to be at most ' |
| 4196 | '20 characters.\n' + REF_MSG, tag_length_errors)) |
| 4197 | |
| 4198 | if tag_errors: |
| 4199 | results.append( |
| 4200 | output_api.PresubmitPromptWarning( |
| 4201 | 'Please use a variable named "TAG" for your log tags.\n' + |
| 4202 | REF_MSG, tag_errors)) |
| 4203 | |
| 4204 | if util_log_errors: |
| 4205 | results.append( |
| 4206 | output_api.PresubmitPromptWarning( |
| 4207 | 'Please use org.chromium.base.Log for new logs.\n' + REF_MSG, |
| 4208 | util_log_errors)) |
| 4209 | |
| 4210 | if tag_with_dot_errors: |
| 4211 | results.append( |
| 4212 | output_api.PresubmitPromptWarning( |
| 4213 | 'Dot in log tags cause them to be elided in crash reports.\n' + |
| 4214 | REF_MSG, tag_with_dot_errors)) |
| 4215 | |
| 4216 | return results |
| 4217 | |
| 4218 | |
| 4219 | def _CheckAndroidTestJUnitFrameworkImport(input_api, output_api): |
| 4220 | """Checks that junit.framework.* is no longer used.""" |
| 4221 | deprecated_junit_framework_pattern = input_api.re.compile( |
| 4222 | r'^import junit\.framework\..*;', input_api.re.MULTILINE) |
| 4223 | sources = lambda x: input_api.FilterSourceFile( |
| 4224 | x, files_to_check=[r'.*\.java$'], files_to_skip=None) |
| 4225 | errors = [] |
| 4226 | for f in input_api.AffectedFiles(file_filter=sources): |
| 4227 | for line_num, line in f.ChangedContents(): |
| 4228 | if deprecated_junit_framework_pattern.search(line): |
| 4229 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4230 | |
| 4231 | results = [] |
| 4232 | if errors: |
| 4233 | results.append( |
| 4234 | output_api.PresubmitError( |
| 4235 | 'APIs from junit.framework.* are deprecated, please use JUnit4 framework' |
| 4236 | '(org.junit.*) from //third_party/junit. Contact yolandyan@chromium.org' |
| 4237 | ' if you have any question.', errors)) |
| 4238 | return results |
| 4239 | |
| 4240 | |
| 4241 | def _CheckAndroidTestJUnitInheritance(input_api, output_api): |
| 4242 | """Checks that if new Java test classes have inheritance. |
| 4243 | Either the new test class is JUnit3 test or it is a JUnit4 test class |
| 4244 | with a base class, either case is undesirable. |
| 4245 | """ |
| 4246 | class_declaration_pattern = input_api.re.compile(r'^public class \w*Test ') |
| 4247 | |
| 4248 | sources = lambda x: input_api.FilterSourceFile( |
| 4249 | x, files_to_check=[r'.*Test\.java$'], files_to_skip=None) |
| 4250 | errors = [] |
| 4251 | for f in input_api.AffectedFiles(file_filter=sources): |
| 4252 | if not f.OldContents(): |
| 4253 | class_declaration_start_flag = False |
| 4254 | for line_num, line in f.ChangedContents(): |
| 4255 | if class_declaration_pattern.search(line): |
| 4256 | class_declaration_start_flag = True |
| 4257 | if class_declaration_start_flag and ' extends ' in line: |
| 4258 | errors.append('%s:%d' % (f.LocalPath(), line_num)) |
| 4259 | if '{' in line: |
| 4260 | class_declaration_start_flag = False |
| 4261 | |
| 4262 | results = [] |
| 4263 | if errors: |
| 4264 | results.append( |
| 4265 | output_api.PresubmitPromptWarning( |
| 4266 | 'The newly created files include Test classes that inherits from base' |
| 4267 | ' class. Please do not use inheritance in JUnit4 tests or add new' |
| 4268 | ' JUnit3 tests. Contact yolandyan@chromium.org if you have any' |
| 4269 | ' questions.', errors)) |
| 4270 | return results |
| 4271 | |
| 4272 | |
| 4273 | def _CheckAndroidTestAnnotationUsage(input_api, output_api): |
| 4274 | """Checks that android.test.suitebuilder.annotation.* is no longer used.""" |
| 4275 | deprecated_annotation_import_pattern = input_api.re.compile( |
| 4276 | r'^import android\.test\.suitebuilder\.annotation\..*;', |
| 4277 | input_api.re.MULTILINE) |
| 4278 | sources = lambda x: input_api.FilterSourceFile( |
| 4279 | x, files_to_check=[r'.*\.java$'], files_to_skip=None) |
| 4280 | errors = [] |
| 4281 | for f in input_api.AffectedFiles(file_filter=sources): |
| 4282 | for line_num, line in f.ChangedContents(): |
| 4283 | if deprecated_annotation_import_pattern.search(line): |
| 4284 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4285 | |
| 4286 | results = [] |
| 4287 | if errors: |
| 4288 | results.append( |
| 4289 | output_api.PresubmitError( |
| 4290 | 'Annotations in android.test.suitebuilder.annotation have been' |
Mohamed Heikal | 3d7a94c | 2023-03-28 16:55:24 | [diff] [blame] | 4291 | ' deprecated since API level 24. Please use androidx.test.filters' |
| 4292 | ' from //third_party/androidx:androidx_test_runner_java instead.' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4293 | ' Contact yolandyan@chromium.org if you have any questions.', |
| 4294 | errors)) |
| 4295 | return results |
| 4296 | |
| 4297 | |
| 4298 | def _CheckAndroidNewMdpiAssetLocation(input_api, output_api): |
| 4299 | """Checks if MDPI assets are placed in a correct directory.""" |
Bruce Dawson | 6c05e85 | 2022-07-21 15:48:51 | [diff] [blame] | 4300 | file_filter = lambda f: (f.LocalPath().endswith( |
| 4301 | '.png') and ('/res/drawable/'.replace('/', input_api.os_path.sep) in f. |
| 4302 | LocalPath() or '/res/drawable-ldrtl/'.replace( |
| 4303 | '/', input_api.os_path.sep) in f.LocalPath())) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4304 | errors = [] |
| 4305 | for f in input_api.AffectedFiles(include_deletes=False, |
| 4306 | file_filter=file_filter): |
| 4307 | errors.append(' %s' % f.LocalPath()) |
| 4308 | |
| 4309 | results = [] |
| 4310 | if errors: |
| 4311 | results.append( |
| 4312 | output_api.PresubmitError( |
| 4313 | 'MDPI assets should be placed in /res/drawable-mdpi/ or ' |
| 4314 | '/res/drawable-ldrtl-mdpi/\ninstead of /res/drawable/ and' |
| 4315 | '/res/drawable-ldrtl/.\n' |
| 4316 | 'Contact newt@chromium.org if you have questions.', errors)) |
| 4317 | return results |
| 4318 | |
| 4319 | |
| 4320 | def _CheckAndroidWebkitImports(input_api, output_api): |
| 4321 | """Checks that code uses org.chromium.base.Callback instead of |
| 4322 | android.webview.ValueCallback except in the WebView glue layer |
| 4323 | and WebLayer. |
| 4324 | """ |
| 4325 | valuecallback_import_pattern = input_api.re.compile( |
| 4326 | r'^import android\.webkit\.ValueCallback;$') |
| 4327 | |
| 4328 | errors = [] |
| 4329 | |
| 4330 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 4331 | affected_file, |
| 4332 | files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api. |
| 4333 | DEFAULT_FILES_TO_SKIP + ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4334 | r'^android_webview/glue/.*', |
| 4335 | r'^weblayer/.*', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4336 | )), |
| 4337 | files_to_check=[r'.*\.java$']) |
| 4338 | |
| 4339 | for f in input_api.AffectedSourceFiles(sources): |
| 4340 | for line_num, line in f.ChangedContents(): |
| 4341 | if valuecallback_import_pattern.search(line): |
| 4342 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4343 | |
| 4344 | results = [] |
| 4345 | |
| 4346 | if errors: |
| 4347 | results.append( |
| 4348 | output_api.PresubmitError( |
| 4349 | 'android.webkit.ValueCallback usage is detected outside of the glue' |
| 4350 | ' layer. To stay compatible with the support library, android.webkit.*' |
| 4351 | ' classes should only be used inside the glue layer and' |
| 4352 | ' org.chromium.base.Callback should be used instead.', errors)) |
| 4353 | |
| 4354 | return results |
| 4355 | |
| 4356 | |
| 4357 | def _CheckAndroidXmlStyle(input_api, output_api, is_check_on_upload): |
| 4358 | """Checks Android XML styles """ |
| 4359 | |
| 4360 | # Return early if no relevant files were modified. |
| 4361 | if not any( |
| 4362 | _IsXmlOrGrdFile(input_api, f.LocalPath()) |
| 4363 | for f in input_api.AffectedFiles(include_deletes=False)): |
| 4364 | return [] |
| 4365 | |
| 4366 | import sys |
| 4367 | original_sys_path = sys.path |
| 4368 | try: |
| 4369 | sys.path = sys.path + [ |
| 4370 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 4371 | 'android', 'checkxmlstyle') |
| 4372 | ] |
| 4373 | import checkxmlstyle |
| 4374 | finally: |
| 4375 | # Restore sys.path to what it was before. |
| 4376 | sys.path = original_sys_path |
| 4377 | |
| 4378 | if is_check_on_upload: |
| 4379 | return checkxmlstyle.CheckStyleOnUpload(input_api, output_api) |
| 4380 | else: |
| 4381 | return checkxmlstyle.CheckStyleOnCommit(input_api, output_api) |
| 4382 | |
| 4383 | |
| 4384 | def _CheckAndroidInfoBarDeprecation(input_api, output_api): |
| 4385 | """Checks Android Infobar Deprecation """ |
| 4386 | |
| 4387 | import sys |
| 4388 | original_sys_path = sys.path |
| 4389 | try: |
| 4390 | sys.path = sys.path + [ |
| 4391 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 4392 | 'android', 'infobar_deprecation') |
| 4393 | ] |
| 4394 | import infobar_deprecation |
| 4395 | finally: |
| 4396 | # Restore sys.path to what it was before. |
| 4397 | sys.path = original_sys_path |
| 4398 | |
| 4399 | return infobar_deprecation.CheckDeprecationOnUpload(input_api, output_api) |
| 4400 | |
| 4401 | |
| 4402 | class _PydepsCheckerResult: |
| 4403 | def __init__(self, cmd, pydeps_path, process, old_contents): |
| 4404 | self._cmd = cmd |
| 4405 | self._pydeps_path = pydeps_path |
| 4406 | self._process = process |
| 4407 | self._old_contents = old_contents |
| 4408 | |
| 4409 | def GetError(self): |
| 4410 | """Returns an error message, or None.""" |
| 4411 | import difflib |
| 4412 | if self._process.wait() != 0: |
| 4413 | # STDERR should already be printed. |
| 4414 | return 'Command failed: ' + self._cmd |
| 4415 | new_contents = self._process.stdout.read().splitlines()[2:] |
| 4416 | if self._old_contents != new_contents: |
| 4417 | diff = '\n'.join( |
| 4418 | difflib.context_diff(self._old_contents, new_contents)) |
| 4419 | return ('File is stale: {}\n' |
| 4420 | 'Diff (apply to fix):\n' |
| 4421 | '{}\n' |
| 4422 | 'To regenerate, run:\n\n' |
| 4423 | ' {}').format(self._pydeps_path, diff, self._cmd) |
| 4424 | return None |
| 4425 | |
| 4426 | |
| 4427 | class PydepsChecker: |
| 4428 | def __init__(self, input_api, pydeps_files): |
| 4429 | self._file_cache = {} |
| 4430 | self._input_api = input_api |
| 4431 | self._pydeps_files = pydeps_files |
| 4432 | |
| 4433 | def _LoadFile(self, path): |
| 4434 | """Returns the list of paths within a .pydeps file relative to //.""" |
| 4435 | if path not in self._file_cache: |
| 4436 | with open(path, encoding='utf-8') as f: |
| 4437 | self._file_cache[path] = f.read() |
| 4438 | return self._file_cache[path] |
| 4439 | |
| 4440 | def _ComputeNormalizedPydepsEntries(self, pydeps_path): |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 4441 | """Returns an iterable of paths within the .pydep, relativized to //.""" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4442 | pydeps_data = self._LoadFile(pydeps_path) |
| 4443 | uses_gn_paths = '--gn-paths' in pydeps_data |
| 4444 | entries = (l for l in pydeps_data.splitlines() |
| 4445 | if not l.startswith('#')) |
| 4446 | if uses_gn_paths: |
| 4447 | # Paths look like: //foo/bar/baz |
| 4448 | return (e[2:] for e in entries) |
| 4449 | else: |
| 4450 | # Paths look like: path/relative/to/file.pydeps |
| 4451 | os_path = self._input_api.os_path |
| 4452 | pydeps_dir = os_path.dirname(pydeps_path) |
| 4453 | return (os_path.normpath(os_path.join(pydeps_dir, e)) |
| 4454 | for e in entries) |
| 4455 | |
| 4456 | def _CreateFilesToPydepsMap(self): |
| 4457 | """Returns a map of local_path -> list_of_pydeps.""" |
| 4458 | ret = {} |
| 4459 | for pydep_local_path in self._pydeps_files: |
| 4460 | for path in self._ComputeNormalizedPydepsEntries(pydep_local_path): |
| 4461 | ret.setdefault(path, []).append(pydep_local_path) |
| 4462 | return ret |
| 4463 | |
| 4464 | def ComputeAffectedPydeps(self): |
| 4465 | """Returns an iterable of .pydeps files that might need regenerating.""" |
| 4466 | affected_pydeps = set() |
| 4467 | file_to_pydeps_map = None |
| 4468 | for f in self._input_api.AffectedFiles(include_deletes=True): |
| 4469 | local_path = f.LocalPath() |
| 4470 | # Changes to DEPS can lead to .pydeps changes if any .py files are in |
| 4471 | # subrepositories. We can't figure out which files change, so re-check |
| 4472 | # all files. |
| 4473 | # Changes to print_python_deps.py affect all .pydeps. |
| 4474 | if local_path in ('DEPS', 'PRESUBMIT.py' |
| 4475 | ) or local_path.endswith('print_python_deps.py'): |
| 4476 | return self._pydeps_files |
| 4477 | elif local_path.endswith('.pydeps'): |
| 4478 | if local_path in self._pydeps_files: |
| 4479 | affected_pydeps.add(local_path) |
| 4480 | elif local_path.endswith('.py'): |
| 4481 | if file_to_pydeps_map is None: |
| 4482 | file_to_pydeps_map = self._CreateFilesToPydepsMap() |
| 4483 | affected_pydeps.update(file_to_pydeps_map.get(local_path, ())) |
| 4484 | return affected_pydeps |
| 4485 | |
| 4486 | def DetermineIfStaleAsync(self, pydeps_path): |
| 4487 | """Runs print_python_deps.py to see if the files is stale.""" |
| 4488 | import os |
| 4489 | |
| 4490 | old_pydeps_data = self._LoadFile(pydeps_path).splitlines() |
| 4491 | if old_pydeps_data: |
| 4492 | cmd = old_pydeps_data[1][1:].strip() |
| 4493 | if '--output' not in cmd: |
| 4494 | cmd += ' --output ' + pydeps_path |
| 4495 | old_contents = old_pydeps_data[2:] |
| 4496 | else: |
| 4497 | # A default cmd that should work in most cases (as long as pydeps filename |
| 4498 | # matches the script name) so that PRESUBMIT.py does not crash if pydeps |
| 4499 | # file is empty/new. |
| 4500 | cmd = 'build/print_python_deps.py {} --root={} --output={}'.format( |
| 4501 | pydeps_path[:-4], os.path.dirname(pydeps_path), pydeps_path) |
| 4502 | old_contents = [] |
| 4503 | env = dict(os.environ) |
| 4504 | env['PYTHONDONTWRITEBYTECODE'] = '1' |
| 4505 | process = self._input_api.subprocess.Popen( |
| 4506 | cmd + ' --output ""', |
| 4507 | shell=True, |
| 4508 | env=env, |
| 4509 | stdout=self._input_api.subprocess.PIPE, |
| 4510 | encoding='utf-8') |
| 4511 | return _PydepsCheckerResult(cmd, pydeps_path, process, old_contents) |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4512 | |
| 4513 | |
Tibor Goldschwendt | 360793f7 | 2019-06-25 18:23:49 | [diff] [blame] | 4514 | def _ParseGclientArgs(): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4515 | args = {} |
| 4516 | with open('build/config/gclient_args.gni', 'r') as f: |
| 4517 | for line in f: |
| 4518 | line = line.strip() |
| 4519 | if not line or line.startswith('#'): |
| 4520 | continue |
| 4521 | attribute, value = line.split('=') |
| 4522 | args[attribute.strip()] = value.strip() |
| 4523 | return args |
Tibor Goldschwendt | 360793f7 | 2019-06-25 18:23:49 | [diff] [blame] | 4524 | |
| 4525 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 4526 | def CheckPydepsNeedsUpdating(input_api, output_api, checker_for_tests=None): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4527 | """Checks if a .pydeps file needs to be regenerated.""" |
| 4528 | # This check is for Python dependency lists (.pydeps files), and involves |
| 4529 | # paths not only in the PRESUBMIT.py, but also in the .pydeps files. It |
| 4530 | # doesn't work on Windows and Mac, so skip it on other platforms. |
| 4531 | if not input_api.platform.startswith('linux'): |
| 4532 | return [] |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 4533 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4534 | results = [] |
| 4535 | # First, check for new / deleted .pydeps. |
| 4536 | for f in input_api.AffectedFiles(include_deletes=True): |
| 4537 | # Check whether we are running the presubmit check for a file in src. |
| 4538 | # f.LocalPath is relative to repo (src, or internal repo). |
| 4539 | # os_path.exists is relative to src repo. |
| 4540 | # Therefore if os_path.exists is true, it means f.LocalPath is relative |
| 4541 | # to src and we can conclude that the pydeps is in src. |
| 4542 | if f.LocalPath().endswith('.pydeps'): |
| 4543 | if input_api.os_path.exists(f.LocalPath()): |
| 4544 | if f.Action() == 'D' and f.LocalPath() in _ALL_PYDEPS_FILES: |
| 4545 | results.append( |
| 4546 | output_api.PresubmitError( |
| 4547 | 'Please update _ALL_PYDEPS_FILES within //PRESUBMIT.py to ' |
| 4548 | 'remove %s' % f.LocalPath())) |
| 4549 | elif f.Action() != 'D' and f.LocalPath( |
| 4550 | ) not in _ALL_PYDEPS_FILES: |
| 4551 | results.append( |
| 4552 | output_api.PresubmitError( |
| 4553 | 'Please update _ALL_PYDEPS_FILES within //PRESUBMIT.py to ' |
| 4554 | 'include %s' % f.LocalPath())) |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4555 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4556 | if results: |
| 4557 | return results |
| 4558 | |
| 4559 | is_android = _ParseGclientArgs().get('checkout_android', 'false') == 'true' |
| 4560 | checker = checker_for_tests or PydepsChecker(input_api, _ALL_PYDEPS_FILES) |
| 4561 | affected_pydeps = set(checker.ComputeAffectedPydeps()) |
| 4562 | affected_android_pydeps = affected_pydeps.intersection( |
| 4563 | set(_ANDROID_SPECIFIC_PYDEPS_FILES)) |
| 4564 | if affected_android_pydeps and not is_android: |
| 4565 | results.append( |
| 4566 | output_api.PresubmitPromptOrNotify( |
| 4567 | 'You have changed python files that may affect pydeps for android\n' |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 4568 | 'specific scripts. However, the relevant presubmit check cannot be\n' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4569 | 'run because you are not using an Android checkout. To validate that\n' |
| 4570 | 'the .pydeps are correct, re-run presubmit in an Android checkout, or\n' |
| 4571 | 'use the android-internal-presubmit optional trybot.\n' |
| 4572 | 'Possibly stale pydeps files:\n{}'.format( |
| 4573 | '\n'.join(affected_android_pydeps)))) |
| 4574 | |
| 4575 | all_pydeps = _ALL_PYDEPS_FILES if is_android else _GENERIC_PYDEPS_FILES |
| 4576 | pydeps_to_check = affected_pydeps.intersection(all_pydeps) |
| 4577 | # Process these concurrently, as each one takes 1-2 seconds. |
| 4578 | pydep_results = [checker.DetermineIfStaleAsync(p) for p in pydeps_to_check] |
| 4579 | for result in pydep_results: |
| 4580 | error_msg = result.GetError() |
| 4581 | if error_msg: |
| 4582 | results.append(output_api.PresubmitError(error_msg)) |
| 4583 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4584 | return results |
| 4585 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4586 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 4587 | def CheckSingletonInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4588 | """Checks to make sure no header files have |Singleton<|.""" |
| 4589 | |
| 4590 | def FileFilter(affected_file): |
| 4591 | # It's ok for base/memory/singleton.h to have |Singleton<|. |
| 4592 | files_to_skip = (_EXCLUDED_PATHS + input_api.DEFAULT_FILES_TO_SKIP + |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4593 | (r"^base/memory/singleton\.h$", |
| 4594 | r"^net/quic/platform/impl/quic_singleton_impl\.h$")) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4595 | return input_api.FilterSourceFile(affected_file, |
| 4596 | files_to_skip=files_to_skip) |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 4597 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4598 | pattern = input_api.re.compile(r'(?<!class\sbase::)Singleton\s*<') |
| 4599 | files = [] |
| 4600 | for f in input_api.AffectedSourceFiles(FileFilter): |
| 4601 | if (f.LocalPath().endswith('.h') or f.LocalPath().endswith('.hxx') |
| 4602 | or f.LocalPath().endswith('.hpp') |
| 4603 | or f.LocalPath().endswith('.inl')): |
| 4604 | contents = input_api.ReadFile(f) |
| 4605 | for line in contents.splitlines(False): |
| 4606 | if (not line.lstrip().startswith('//') |
| 4607 | and # Strip C++ comment. |
| 4608 | pattern.search(line)): |
| 4609 | files.append(f) |
| 4610 | break |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 4611 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4612 | if files: |
| 4613 | return [ |
| 4614 | output_api.PresubmitError( |
| 4615 | 'Found base::Singleton<T> in the following header files.\n' + |
| 4616 | 'Please move them to an appropriate source file so that the ' + |
| 4617 | 'template gets instantiated in a single compilation unit.', |
| 4618 | files) |
| 4619 | ] |
| 4620 | return [] |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 4621 | |
| 4622 | |
jchaffraix@chromium.org | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 4623 | _DEPRECATED_CSS = [ |
| 4624 | # Values |
| 4625 | ( "-webkit-box", "flex" ), |
| 4626 | ( "-webkit-inline-box", "inline-flex" ), |
| 4627 | ( "-webkit-flex", "flex" ), |
| 4628 | ( "-webkit-inline-flex", "inline-flex" ), |
| 4629 | ( "-webkit-min-content", "min-content" ), |
| 4630 | ( "-webkit-max-content", "max-content" ), |
| 4631 | |
| 4632 | # Properties |
| 4633 | ( "-webkit-background-clip", "background-clip" ), |
| 4634 | ( "-webkit-background-origin", "background-origin" ), |
| 4635 | ( "-webkit-background-size", "background-size" ), |
| 4636 | ( "-webkit-box-shadow", "box-shadow" ), |
dbeam | 6936c67f | 2017-01-19 01:51:44 | [diff] [blame] | 4637 | ( "-webkit-user-select", "user-select" ), |
jchaffraix@chromium.org | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 4638 | |
| 4639 | # Functions |
| 4640 | ( "-webkit-gradient", "gradient" ), |
| 4641 | ( "-webkit-repeating-gradient", "repeating-gradient" ), |
| 4642 | ( "-webkit-linear-gradient", "linear-gradient" ), |
| 4643 | ( "-webkit-repeating-linear-gradient", "repeating-linear-gradient" ), |
| 4644 | ( "-webkit-radial-gradient", "radial-gradient" ), |
| 4645 | ( "-webkit-repeating-radial-gradient", "repeating-radial-gradient" ), |
| 4646 | ] |
| 4647 | |
Wei-Yin Chen (陳威尹) | f799d44 | 2018-07-31 02:20:20 | [diff] [blame] | 4648 | |
Wei-Yin Chen (陳威尹) | dca729a | 2018-07-31 21:35:49 | [diff] [blame] | 4649 | # TODO: add unit tests |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 4650 | def CheckNoDeprecatedCss(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4651 | """ Make sure that we don't use deprecated CSS |
| 4652 | properties, functions or values. Our external |
| 4653 | documentation and iOS CSS for dom distiller |
| 4654 | (reader mode) are ignored by the hooks as it |
| 4655 | needs to be consumed by WebKit. """ |
| 4656 | results = [] |
| 4657 | file_inclusion_pattern = [r".+\.css$"] |
| 4658 | files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 4659 | input_api.DEFAULT_FILES_TO_SKIP + |
| 4660 | (r"^chrome/common/extensions/docs", r"^chrome/docs", |
| 4661 | r"^native_client_sdk")) |
| 4662 | file_filter = lambda f: input_api.FilterSourceFile( |
| 4663 | f, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip) |
| 4664 | for fpath in input_api.AffectedFiles(file_filter=file_filter): |
| 4665 | for line_num, line in fpath.ChangedContents(): |
| 4666 | for (deprecated_value, value) in _DEPRECATED_CSS: |
| 4667 | if deprecated_value in line: |
| 4668 | results.append( |
| 4669 | output_api.PresubmitError( |
| 4670 | "%s:%d: Use of deprecated CSS %s, use %s instead" % |
| 4671 | (fpath.LocalPath(), line_num, deprecated_value, |
| 4672 | value))) |
| 4673 | return results |
jchaffraix@chromium.org | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 4674 | |
mohan.reddy | f21db96 | 2014-10-16 12:26:47 | [diff] [blame] | 4675 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 4676 | def CheckForRelativeIncludes(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4677 | bad_files = {} |
| 4678 | for f in input_api.AffectedFiles(include_deletes=False): |
| 4679 | if (f.LocalPath().startswith('third_party') |
| 4680 | and not f.LocalPath().startswith('third_party/blink') |
| 4681 | and not f.LocalPath().startswith('third_party\\blink')): |
| 4682 | continue |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 4683 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4684 | if not _IsCPlusPlusFile(input_api, f.LocalPath()): |
| 4685 | continue |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 4686 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4687 | relative_includes = [ |
| 4688 | line for _, line in f.ChangedContents() |
| 4689 | if "#include" in line and "../" in line |
| 4690 | ] |
| 4691 | if not relative_includes: |
| 4692 | continue |
| 4693 | bad_files[f.LocalPath()] = relative_includes |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 4694 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4695 | if not bad_files: |
| 4696 | return [] |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 4697 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4698 | error_descriptions = [] |
| 4699 | for file_path, bad_lines in bad_files.items(): |
| 4700 | error_description = file_path |
| 4701 | for line in bad_lines: |
| 4702 | error_description += '\n ' + line |
| 4703 | error_descriptions.append(error_description) |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 4704 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4705 | results = [] |
| 4706 | results.append( |
| 4707 | output_api.PresubmitError( |
| 4708 | 'You added one or more relative #include paths (including "../").\n' |
| 4709 | 'These shouldn\'t be used because they can be used to include headers\n' |
| 4710 | 'from code that\'s not correctly specified as a dependency in the\n' |
| 4711 | 'relevant BUILD.gn file(s).', error_descriptions)) |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 4712 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4713 | return results |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 4714 | |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4715 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 4716 | def CheckForCcIncludes(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4717 | """Check that nobody tries to include a cc file. It's a relatively |
| 4718 | common error which results in duplicate symbols in object |
| 4719 | files. This may not always break the build until someone later gets |
| 4720 | very confusing linking errors.""" |
| 4721 | results = [] |
| 4722 | for f in input_api.AffectedFiles(include_deletes=False): |
| 4723 | # We let third_party code do whatever it wants |
| 4724 | if (f.LocalPath().startswith('third_party') |
| 4725 | and not f.LocalPath().startswith('third_party/blink') |
| 4726 | and not f.LocalPath().startswith('third_party\\blink')): |
| 4727 | continue |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 4728 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4729 | if not _IsCPlusPlusFile(input_api, f.LocalPath()): |
| 4730 | continue |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 4731 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4732 | for _, line in f.ChangedContents(): |
| 4733 | if line.startswith('#include "'): |
| 4734 | included_file = line.split('"')[1] |
| 4735 | if _IsCPlusPlusFile(input_api, included_file): |
| 4736 | # The most common naming for external files with C++ code, |
| 4737 | # apart from standard headers, is to call them foo.inc, but |
| 4738 | # Chromium sometimes uses foo-inc.cc so allow that as well. |
| 4739 | if not included_file.endswith(('.h', '-inc.cc')): |
| 4740 | results.append( |
| 4741 | output_api.PresubmitError( |
| 4742 | 'Only header files or .inc files should be included in other\n' |
| 4743 | 'C++ files. Compiling the contents of a cc file more than once\n' |
| 4744 | 'will cause duplicate information in the build which may later\n' |
| 4745 | 'result in strange link_errors.\n' + |
| 4746 | f.LocalPath() + ':\n ' + line)) |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 4747 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4748 | return results |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 4749 | |
| 4750 | |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 4751 | def _CheckWatchlistDefinitionsEntrySyntax(key, value, ast): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4752 | if not isinstance(key, ast.Str): |
| 4753 | return 'Key at line %d must be a string literal' % key.lineno |
| 4754 | if not isinstance(value, ast.Dict): |
| 4755 | return 'Value at line %d must be a dict' % value.lineno |
| 4756 | if len(value.keys) != 1: |
| 4757 | return 'Dict at line %d must have single entry' % value.lineno |
| 4758 | if not isinstance(value.keys[0], ast.Str) or value.keys[0].s != 'filepath': |
| 4759 | return ( |
| 4760 | 'Entry at line %d must have a string literal \'filepath\' as key' % |
| 4761 | value.lineno) |
| 4762 | return None |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4763 | |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4764 | |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 4765 | def _CheckWatchlistsEntrySyntax(key, value, ast, email_regex): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4766 | if not isinstance(key, ast.Str): |
| 4767 | return 'Key at line %d must be a string literal' % key.lineno |
| 4768 | if not isinstance(value, ast.List): |
| 4769 | return 'Value at line %d must be a list' % value.lineno |
| 4770 | for element in value.elts: |
| 4771 | if not isinstance(element, ast.Str): |
| 4772 | return 'Watchlist elements on line %d is not a string' % key.lineno |
| 4773 | if not email_regex.match(element.s): |
| 4774 | return ('Watchlist element on line %d doesn\'t look like a valid ' |
| 4775 | + 'email: %s') % (key.lineno, element.s) |
| 4776 | return None |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4777 | |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4778 | |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 4779 | def _CheckWATCHLISTSEntries(wd_dict, w_dict, input_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4780 | mismatch_template = ( |
| 4781 | 'Mismatch between WATCHLIST_DEFINITIONS entry (%s) and WATCHLISTS ' |
| 4782 | 'entry (%s)') |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4783 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4784 | email_regex = input_api.re.compile( |
| 4785 | r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+$") |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 4786 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4787 | ast = input_api.ast |
| 4788 | i = 0 |
| 4789 | last_key = '' |
| 4790 | while True: |
| 4791 | if i >= len(wd_dict.keys): |
| 4792 | if i >= len(w_dict.keys): |
| 4793 | return None |
| 4794 | return mismatch_template % ('missing', |
| 4795 | 'line %d' % w_dict.keys[i].lineno) |
| 4796 | elif i >= len(w_dict.keys): |
| 4797 | return (mismatch_template % |
| 4798 | ('line %d' % wd_dict.keys[i].lineno, 'missing')) |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4799 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4800 | wd_key = wd_dict.keys[i] |
| 4801 | w_key = w_dict.keys[i] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4802 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4803 | result = _CheckWatchlistDefinitionsEntrySyntax(wd_key, |
| 4804 | wd_dict.values[i], ast) |
| 4805 | if result is not None: |
| 4806 | return 'Bad entry in WATCHLIST_DEFINITIONS dict: %s' % result |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4807 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4808 | result = _CheckWatchlistsEntrySyntax(w_key, w_dict.values[i], ast, |
| 4809 | email_regex) |
| 4810 | if result is not None: |
| 4811 | return 'Bad entry in WATCHLISTS dict: %s' % result |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 4812 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4813 | if wd_key.s != w_key.s: |
| 4814 | return mismatch_template % ('%s at line %d' % |
| 4815 | (wd_key.s, wd_key.lineno), |
| 4816 | '%s at line %d' % |
| 4817 | (w_key.s, w_key.lineno)) |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 4818 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4819 | if wd_key.s < last_key: |
| 4820 | return ( |
| 4821 | 'WATCHLISTS dict is not sorted lexicographically at line %d and %d' |
| 4822 | % (wd_key.lineno, w_key.lineno)) |
| 4823 | last_key = wd_key.s |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 4824 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4825 | i = i + 1 |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 4826 | |
| 4827 | |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 4828 | def _CheckWATCHLISTSSyntax(expression, input_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4829 | ast = input_api.ast |
| 4830 | if not isinstance(expression, ast.Expression): |
| 4831 | return 'WATCHLISTS file must contain a valid expression' |
| 4832 | dictionary = expression.body |
| 4833 | if not isinstance(dictionary, ast.Dict) or len(dictionary.keys) != 2: |
| 4834 | return 'WATCHLISTS file must have single dict with exactly two entries' |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 4835 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4836 | first_key = dictionary.keys[0] |
| 4837 | first_value = dictionary.values[0] |
| 4838 | second_key = dictionary.keys[1] |
| 4839 | second_value = dictionary.values[1] |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 4840 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4841 | if (not isinstance(first_key, ast.Str) |
| 4842 | or first_key.s != 'WATCHLIST_DEFINITIONS' |
| 4843 | or not isinstance(first_value, ast.Dict)): |
| 4844 | return ('The first entry of the dict in WATCHLISTS file must be ' |
| 4845 | 'WATCHLIST_DEFINITIONS dict') |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 4846 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4847 | if (not isinstance(second_key, ast.Str) or second_key.s != 'WATCHLISTS' |
| 4848 | or not isinstance(second_value, ast.Dict)): |
| 4849 | return ('The second entry of the dict in WATCHLISTS file must be ' |
| 4850 | 'WATCHLISTS dict') |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 4851 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4852 | return _CheckWATCHLISTSEntries(first_value, second_value, input_api) |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4853 | |
| 4854 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 4855 | def CheckWATCHLISTS(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4856 | for f in input_api.AffectedFiles(include_deletes=False): |
| 4857 | if f.LocalPath() == 'WATCHLISTS': |
| 4858 | contents = input_api.ReadFile(f, 'r') |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4859 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4860 | try: |
| 4861 | # First, make sure that it can be evaluated. |
| 4862 | input_api.ast.literal_eval(contents) |
| 4863 | # Get an AST tree for it and scan the tree for detailed style checking. |
| 4864 | expression = input_api.ast.parse(contents, |
| 4865 | filename='WATCHLISTS', |
| 4866 | mode='eval') |
| 4867 | except ValueError as e: |
| 4868 | return [ |
| 4869 | output_api.PresubmitError('Cannot parse WATCHLISTS file', |
| 4870 | long_text=repr(e)) |
| 4871 | ] |
| 4872 | except SyntaxError as e: |
| 4873 | return [ |
| 4874 | output_api.PresubmitError('Cannot parse WATCHLISTS file', |
| 4875 | long_text=repr(e)) |
| 4876 | ] |
| 4877 | except TypeError as e: |
| 4878 | return [ |
| 4879 | output_api.PresubmitError('Cannot parse WATCHLISTS file', |
| 4880 | long_text=repr(e)) |
| 4881 | ] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4882 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4883 | result = _CheckWATCHLISTSSyntax(expression, input_api) |
| 4884 | if result is not None: |
| 4885 | return [output_api.PresubmitError(result)] |
| 4886 | break |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4887 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4888 | return [] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4889 | |
Sean Kau | cb7c9b3 | 2022-10-25 21:25:52 | [diff] [blame] | 4890 | def CheckGnRebasePath(input_api, output_api): |
| 4891 | """Checks that target_gen_dir is not used wtih "//" in rebase_path(). |
| 4892 | |
| 4893 | Developers should use root_build_dir instead of "//" when using target_gen_dir because |
| 4894 | Chromium is sometimes built outside of the source tree. |
| 4895 | """ |
| 4896 | |
| 4897 | def gn_files(f): |
| 4898 | return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gn', )) |
| 4899 | |
| 4900 | rebase_path_regex = input_api.re.compile(r'rebase_path\(("\$target_gen_dir"|target_gen_dir), ("/"|"//")\)') |
| 4901 | problems = [] |
| 4902 | for f in input_api.AffectedSourceFiles(gn_files): |
| 4903 | for line_num, line in f.ChangedContents(): |
| 4904 | if rebase_path_regex.search(line): |
| 4905 | problems.append( |
| 4906 | 'Absolute path in rebase_path() in %s:%d' % |
| 4907 | (f.LocalPath(), line_num)) |
| 4908 | |
| 4909 | if problems: |
| 4910 | return [ |
| 4911 | output_api.PresubmitPromptWarning( |
| 4912 | 'Using an absolute path in rebase_path()', |
| 4913 | items=sorted(problems), |
| 4914 | long_text=( |
| 4915 | 'rebase_path() should use root_build_dir instead of "/" ', |
| 4916 | 'since builds can be initiated from outside of the source ', |
| 4917 | 'root.')) |
| 4918 | ] |
| 4919 | return [] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 4920 | |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 4921 | def CheckGnGlobForward(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4922 | """Checks that forward_variables_from(invoker, "*") follows best practices. |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 4923 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4924 | As documented at //build/docs/writing_gn_templates.md |
| 4925 | """ |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 4926 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4927 | def gn_files(f): |
| 4928 | return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gni', )) |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 4929 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4930 | problems = [] |
| 4931 | for f in input_api.AffectedSourceFiles(gn_files): |
| 4932 | for line_num, line in f.ChangedContents(): |
| 4933 | if 'forward_variables_from(invoker, "*")' in line: |
| 4934 | problems.append( |
| 4935 | 'Bare forward_variables_from(invoker, "*") in %s:%d' % |
| 4936 | (f.LocalPath(), line_num)) |
| 4937 | |
| 4938 | if problems: |
| 4939 | return [ |
| 4940 | output_api.PresubmitPromptWarning( |
| 4941 | 'forward_variables_from("*") without exclusions', |
| 4942 | items=sorted(problems), |
| 4943 | long_text=( |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 4944 | 'The variables "visibility" and "test_only" should be ' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4945 | 'explicitly listed in forward_variables_from(). For more ' |
| 4946 | 'details, see:\n' |
| 4947 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/' |
| 4948 | 'build/docs/writing_gn_templates.md' |
| 4949 | '#Using-forward_variables_from')) |
| 4950 | ] |
| 4951 | return [] |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 4952 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 4953 | def CheckNewHeaderWithoutGnChangeOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4954 | """Checks that newly added header files have corresponding GN changes. |
| 4955 | Note that this is only a heuristic. To be precise, run script: |
| 4956 | build/check_gn_headers.py. |
| 4957 | """ |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 4958 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4959 | def headers(f): |
| 4960 | return input_api.FilterSourceFile( |
| 4961 | f, files_to_check=(r'.+%s' % _HEADER_EXTENSIONS, )) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 4962 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4963 | new_headers = [] |
| 4964 | for f in input_api.AffectedSourceFiles(headers): |
| 4965 | if f.Action() != 'A': |
| 4966 | continue |
| 4967 | new_headers.append(f.LocalPath()) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 4968 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4969 | def gn_files(f): |
| 4970 | return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gn', )) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 4971 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4972 | all_gn_changed_contents = '' |
| 4973 | for f in input_api.AffectedSourceFiles(gn_files): |
| 4974 | for _, line in f.ChangedContents(): |
| 4975 | all_gn_changed_contents += line |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 4976 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4977 | problems = [] |
| 4978 | for header in new_headers: |
| 4979 | basename = input_api.os_path.basename(header) |
| 4980 | if basename not in all_gn_changed_contents: |
| 4981 | problems.append(header) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 4982 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4983 | if problems: |
| 4984 | return [ |
| 4985 | output_api.PresubmitPromptWarning( |
| 4986 | 'Missing GN changes for new header files', |
| 4987 | items=sorted(problems), |
| 4988 | long_text= |
| 4989 | 'Please double check whether newly added header files need ' |
| 4990 | 'corresponding changes in gn or gni files.\nThis checking is only a ' |
| 4991 | 'heuristic. Run build/check_gn_headers.py to be precise.\n' |
| 4992 | 'Read https://crbug.com/661774 for more info.') |
| 4993 | ] |
| 4994 | return [] |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 4995 | |
| 4996 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 4997 | def CheckCorrectProductNameInMessages(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4998 | """Check that Chromium-branded strings don't include "Chrome" or vice versa. |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 4999 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5000 | This assumes we won't intentionally reference one product from the other |
| 5001 | product. |
| 5002 | """ |
| 5003 | all_problems = [] |
| 5004 | test_cases = [{ |
| 5005 | "filename_postfix": "google_chrome_strings.grd", |
| 5006 | "correct_name": "Chrome", |
| 5007 | "incorrect_name": "Chromium", |
| 5008 | }, { |
| 5009 | "filename_postfix": "chromium_strings.grd", |
| 5010 | "correct_name": "Chromium", |
| 5011 | "incorrect_name": "Chrome", |
| 5012 | }] |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5013 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5014 | for test_case in test_cases: |
| 5015 | problems = [] |
| 5016 | filename_filter = lambda x: x.LocalPath().endswith(test_case[ |
| 5017 | "filename_postfix"]) |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5018 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5019 | # Check each new line. Can yield false positives in multiline comments, but |
| 5020 | # easier than trying to parse the XML because messages can have nested |
| 5021 | # children, and associating message elements with affected lines is hard. |
| 5022 | for f in input_api.AffectedSourceFiles(filename_filter): |
| 5023 | for line_num, line in f.ChangedContents(): |
| 5024 | if "<message" in line or "<!--" in line or "-->" in line: |
| 5025 | continue |
| 5026 | if test_case["incorrect_name"] in line: |
| 5027 | problems.append("Incorrect product name in %s:%d" % |
| 5028 | (f.LocalPath(), line_num)) |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5029 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5030 | if problems: |
| 5031 | message = ( |
| 5032 | "Strings in %s-branded string files should reference \"%s\", not \"%s\"" |
| 5033 | % (test_case["correct_name"], test_case["correct_name"], |
| 5034 | test_case["incorrect_name"])) |
| 5035 | all_problems.append( |
| 5036 | output_api.PresubmitPromptWarning(message, items=problems)) |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5037 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5038 | return all_problems |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5039 | |
| 5040 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5041 | def CheckForTooLargeFiles(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5042 | """Avoid large files, especially binary files, in the repository since |
| 5043 | git doesn't scale well for those. They will be in everyone's repo |
| 5044 | clones forever, forever making Chromium slower to clone and work |
| 5045 | with.""" |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5046 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5047 | # Uploading files to cloud storage is not trivial so we don't want |
| 5048 | # to set the limit too low, but the upper limit for "normal" large |
| 5049 | # files seems to be 1-2 MB, with a handful around 5-8 MB, so |
| 5050 | # anything over 20 MB is exceptional. |
Bruce Dawson | bb414db | 2022-12-27 20:21:25 | [diff] [blame] | 5051 | TOO_LARGE_FILE_SIZE_LIMIT = 20 * 1024 * 1024 |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5052 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5053 | too_large_files = [] |
| 5054 | for f in input_api.AffectedFiles(): |
| 5055 | # Check both added and modified files (but not deleted files). |
| 5056 | if f.Action() in ('A', 'M'): |
| 5057 | size = input_api.os_path.getsize(f.AbsoluteLocalPath()) |
Joe DeBlasio | 10a832f | 2023-04-21 20:20:18 | [diff] [blame] | 5058 | if size > TOO_LARGE_FILE_SIZE_LIMIT: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5059 | too_large_files.append("%s: %d bytes" % (f.LocalPath(), size)) |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5060 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5061 | if too_large_files: |
| 5062 | message = ( |
| 5063 | 'Do not commit large files to git since git scales badly for those.\n' |
| 5064 | + |
| 5065 | 'Instead put the large files in cloud storage and use DEPS to\n' + |
| 5066 | 'fetch them.\n' + '\n'.join(too_large_files)) |
| 5067 | return [ |
| 5068 | output_api.PresubmitError('Too large files found in commit', |
| 5069 | long_text=message + '\n') |
| 5070 | ] |
| 5071 | else: |
| 5072 | return [] |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5073 | |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5074 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5075 | def CheckFuzzTargetsOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5076 | """Checks specific for fuzz target sources.""" |
| 5077 | EXPORTED_SYMBOLS = [ |
| 5078 | 'LLVMFuzzerInitialize', |
| 5079 | 'LLVMFuzzerCustomMutator', |
| 5080 | 'LLVMFuzzerCustomCrossOver', |
| 5081 | 'LLVMFuzzerMutate', |
| 5082 | ] |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5083 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5084 | REQUIRED_HEADER = '#include "testing/libfuzzer/libfuzzer_exports.h"' |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5085 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5086 | def FilterFile(affected_file): |
| 5087 | """Ignore libFuzzer source code.""" |
| 5088 | files_to_check = r'.*fuzz.*\.(h|hpp|hcc|cc|cpp|cxx)$' |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5089 | files_to_skip = r"^third_party/libFuzzer" |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5090 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5091 | return input_api.FilterSourceFile(affected_file, |
| 5092 | files_to_check=[files_to_check], |
| 5093 | files_to_skip=[files_to_skip]) |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5094 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5095 | files_with_missing_header = [] |
| 5096 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 5097 | contents = input_api.ReadFile(f, 'r') |
| 5098 | if REQUIRED_HEADER in contents: |
| 5099 | continue |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5100 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5101 | if any(symbol in contents for symbol in EXPORTED_SYMBOLS): |
| 5102 | files_with_missing_header.append(f.LocalPath()) |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5103 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5104 | if not files_with_missing_header: |
| 5105 | return [] |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5106 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5107 | long_text = ( |
| 5108 | 'If you define any of the libFuzzer optional functions (%s), it is ' |
| 5109 | 'recommended to add \'%s\' directive. Otherwise, the fuzz target may ' |
| 5110 | 'work incorrectly on Mac (crbug.com/687076).\nNote that ' |
| 5111 | 'LLVMFuzzerInitialize should not be used, unless your fuzz target needs ' |
| 5112 | 'to access command line arguments passed to the fuzzer. Instead, prefer ' |
| 5113 | 'static initialization and shared resources as documented in ' |
| 5114 | 'https://chromium.googlesource.com/chromium/src/+/main/testing/' |
| 5115 | 'libfuzzer/efficient_fuzzing.md#simplifying-initialization_cleanup.\n' |
| 5116 | % (', '.join(EXPORTED_SYMBOLS), REQUIRED_HEADER)) |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5117 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5118 | return [ |
| 5119 | output_api.PresubmitPromptWarning(message="Missing '%s' in:" % |
| 5120 | REQUIRED_HEADER, |
| 5121 | items=files_with_missing_header, |
| 5122 | long_text=long_text) |
| 5123 | ] |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5124 | |
| 5125 | |
Mohamed Heikal | d048240a | 2019-11-12 16:57:37 | [diff] [blame] | 5126 | def _CheckNewImagesWarning(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5127 | """ |
| 5128 | Warns authors who add images into the repo to make sure their images are |
| 5129 | optimized before committing. |
| 5130 | """ |
| 5131 | images_added = False |
| 5132 | image_paths = [] |
| 5133 | errors = [] |
| 5134 | filter_lambda = lambda x: input_api.FilterSourceFile( |
| 5135 | x, |
| 5136 | files_to_skip=(('(?i).*test', r'.*\/junit\/') + input_api. |
| 5137 | DEFAULT_FILES_TO_SKIP), |
| 5138 | files_to_check=[r'.*\/(drawable|mipmap)']) |
| 5139 | for f in input_api.AffectedFiles(include_deletes=False, |
| 5140 | file_filter=filter_lambda): |
| 5141 | local_path = f.LocalPath().lower() |
| 5142 | if any( |
| 5143 | local_path.endswith(extension) |
| 5144 | for extension in _IMAGE_EXTENSIONS): |
| 5145 | images_added = True |
| 5146 | image_paths.append(f) |
| 5147 | if images_added: |
| 5148 | errors.append( |
| 5149 | output_api.PresubmitPromptWarning( |
| 5150 | 'It looks like you are trying to commit some images. If these are ' |
| 5151 | 'non-test-only images, please make sure to read and apply the tips in ' |
| 5152 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/docs/speed/' |
| 5153 | 'binary_size/optimization_advice.md#optimizing-images\nThis check is ' |
| 5154 | 'FYI only and will not block your CL on the CQ.', image_paths)) |
| 5155 | return errors |
Mohamed Heikal | d048240a | 2019-11-12 16:57:37 | [diff] [blame] | 5156 | |
| 5157 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5158 | def ChecksAndroidSpecificOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5159 | """Groups upload checks that target android code.""" |
| 5160 | results = [] |
| 5161 | results.extend(_CheckAndroidCrLogUsage(input_api, output_api)) |
| 5162 | results.extend(_CheckAndroidDebuggableBuild(input_api, output_api)) |
| 5163 | results.extend(_CheckAndroidNewMdpiAssetLocation(input_api, output_api)) |
| 5164 | results.extend(_CheckAndroidToastUsage(input_api, output_api)) |
| 5165 | results.extend(_CheckAndroidTestJUnitInheritance(input_api, output_api)) |
| 5166 | results.extend(_CheckAndroidTestJUnitFrameworkImport( |
| 5167 | input_api, output_api)) |
| 5168 | results.extend(_CheckAndroidTestAnnotationUsage(input_api, output_api)) |
| 5169 | results.extend(_CheckAndroidWebkitImports(input_api, output_api)) |
| 5170 | results.extend(_CheckAndroidXmlStyle(input_api, output_api, True)) |
| 5171 | results.extend(_CheckNewImagesWarning(input_api, output_api)) |
| 5172 | results.extend(_CheckAndroidNoBannedImports(input_api, output_api)) |
| 5173 | results.extend(_CheckAndroidInfoBarDeprecation(input_api, output_api)) |
| 5174 | return results |
| 5175 | |
Becky Zhou | 7c69b5099 | 2018-12-10 19:37:57 | [diff] [blame] | 5176 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5177 | def ChecksAndroidSpecificOnCommit(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5178 | """Groups commit checks that target android code.""" |
| 5179 | results = [] |
| 5180 | results.extend(_CheckAndroidXmlStyle(input_api, output_api, False)) |
| 5181 | return results |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 5182 | |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5183 | # TODO(chrishall): could we additionally match on any path owned by |
| 5184 | # ui/accessibility/OWNERS ? |
| 5185 | _ACCESSIBILITY_PATHS = ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5186 | r"^chrome/browser.*/accessibility/", |
| 5187 | r"^chrome/browser/extensions/api/automation.*/", |
| 5188 | r"^chrome/renderer/extensions/accessibility_.*", |
| 5189 | r"^chrome/tests/data/accessibility/", |
Katie Dektar | 58ef07b | 2022-09-27 13:19:17 | [diff] [blame] | 5190 | r"^components/services/screen_ai/", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5191 | r"^content/browser/accessibility/", |
| 5192 | r"^content/renderer/accessibility/", |
| 5193 | r"^content/tests/data/accessibility/", |
| 5194 | r"^extensions/renderer/api/automation/", |
Katie Dektar | 58ef07b | 2022-09-27 13:19:17 | [diff] [blame] | 5195 | r"^services/accessibility/", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5196 | r"^ui/accessibility/", |
| 5197 | r"^ui/views/accessibility/", |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5198 | ) |
| 5199 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5200 | def CheckAccessibilityRelnotesField(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5201 | """Checks that commits to accessibility code contain an AX-Relnotes field in |
| 5202 | their commit message.""" |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5203 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5204 | def FileFilter(affected_file): |
| 5205 | paths = _ACCESSIBILITY_PATHS |
| 5206 | return input_api.FilterSourceFile(affected_file, files_to_check=paths) |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5207 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5208 | # Only consider changes affecting accessibility paths. |
| 5209 | if not any(input_api.AffectedFiles(file_filter=FileFilter)): |
| 5210 | return [] |
Akihiro Ota | 08108e54 | 2020-05-20 15:30:53 | [diff] [blame] | 5211 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5212 | # AX-Relnotes can appear in either the description or the footer. |
| 5213 | # When searching the description, require 'AX-Relnotes:' to appear at the |
| 5214 | # beginning of a line. |
| 5215 | ax_regex = input_api.re.compile('ax-relnotes[:=]') |
| 5216 | description_has_relnotes = any( |
| 5217 | ax_regex.match(line) |
| 5218 | for line in input_api.change.DescriptionText().lower().splitlines()) |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5219 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5220 | footer_relnotes = input_api.change.GitFootersFromDescription().get( |
| 5221 | 'AX-Relnotes', []) |
| 5222 | if description_has_relnotes or footer_relnotes: |
| 5223 | return [] |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5224 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5225 | # TODO(chrishall): link to Relnotes documentation in message. |
| 5226 | message = ( |
| 5227 | "Missing 'AX-Relnotes:' field required for accessibility changes" |
| 5228 | "\n please add 'AX-Relnotes: [release notes].' to describe any " |
| 5229 | "user-facing changes" |
| 5230 | "\n otherwise add 'AX-Relnotes: n/a.' if this change has no " |
| 5231 | "user-facing effects" |
| 5232 | "\n if this is confusing or annoying then please contact members " |
| 5233 | "of ui/accessibility/OWNERS.") |
| 5234 | |
| 5235 | return [output_api.PresubmitNotifyResult(message)] |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 5236 | |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5237 | |
| 5238 | _ACCESSIBILITY_EVENTS_TEST_PATH = ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5239 | r"^content/test/data/accessibility/event/.*\.html", |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5240 | ) |
| 5241 | |
| 5242 | _ACCESSIBILITY_TREE_TEST_PATH = ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5243 | r"^content/test/data/accessibility/accname/.*\.html", |
| 5244 | r"^content/test/data/accessibility/aria/.*\.html", |
| 5245 | r"^content/test/data/accessibility/css/.*\.html", |
| 5246 | r"^content/test/data/accessibility/html/.*\.html", |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5247 | ) |
| 5248 | |
| 5249 | _ACCESSIBILITY_ANDROID_EVENTS_TEST_PATH = ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5250 | r"^.*/WebContentsAccessibilityEventsTest\.java", |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5251 | ) |
| 5252 | |
| 5253 | _ACCESSIBILITY_ANDROID_TREE_TEST_PATH = ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5254 | r"^.*/WebContentsAccessibilityTreeTest\.java", |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5255 | ) |
| 5256 | |
| 5257 | def CheckAccessibilityEventsTestsAreIncludedForAndroid(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5258 | """Checks that commits that include a newly added, renamed/moved, or deleted |
| 5259 | test in the DumpAccessibilityEventsTest suite also includes a corresponding |
| 5260 | change to the Android test.""" |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5261 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5262 | def FilePathFilter(affected_file): |
| 5263 | paths = _ACCESSIBILITY_EVENTS_TEST_PATH |
| 5264 | return input_api.FilterSourceFile(affected_file, files_to_check=paths) |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5265 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5266 | def AndroidFilePathFilter(affected_file): |
| 5267 | paths = _ACCESSIBILITY_ANDROID_EVENTS_TEST_PATH |
| 5268 | return input_api.FilterSourceFile(affected_file, files_to_check=paths) |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5269 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5270 | # Only consider changes in the events test data path with html type. |
| 5271 | if not any( |
| 5272 | input_api.AffectedFiles(include_deletes=True, |
| 5273 | file_filter=FilePathFilter)): |
| 5274 | return [] |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5275 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5276 | # If the commit contains any change to the Android test file, ignore. |
| 5277 | if any( |
| 5278 | input_api.AffectedFiles(include_deletes=True, |
| 5279 | file_filter=AndroidFilePathFilter)): |
| 5280 | return [] |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5281 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5282 | # Only consider changes that are adding/renaming or deleting a file |
| 5283 | message = [] |
| 5284 | for f in input_api.AffectedFiles(include_deletes=True, |
| 5285 | file_filter=FilePathFilter): |
| 5286 | if f.Action() == 'A' or f.Action() == 'D': |
| 5287 | message = ( |
| 5288 | "It appears that you are adding, renaming or deleting" |
| 5289 | "\na dump_accessibility_events* test, but have not included" |
| 5290 | "\na corresponding change for Android." |
| 5291 | "\nPlease include (or remove) the test from:" |
| 5292 | "\n content/public/android/javatests/src/org/chromium/" |
| 5293 | "content/browser/accessibility/" |
| 5294 | "WebContentsAccessibilityEventsTest.java" |
| 5295 | "\nIf this message is confusing or annoying, please contact" |
| 5296 | "\nmembers of ui/accessibility/OWNERS.") |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5297 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5298 | # If no message was set, return empty. |
| 5299 | if not len(message): |
| 5300 | return [] |
| 5301 | |
| 5302 | return [output_api.PresubmitPromptWarning(message)] |
| 5303 | |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5304 | |
| 5305 | def CheckAccessibilityTreeTestsAreIncludedForAndroid(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5306 | """Checks that commits that include a newly added, renamed/moved, or deleted |
| 5307 | test in the DumpAccessibilityTreeTest suite also includes a corresponding |
| 5308 | change to the Android test.""" |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5309 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5310 | def FilePathFilter(affected_file): |
| 5311 | paths = _ACCESSIBILITY_TREE_TEST_PATH |
| 5312 | return input_api.FilterSourceFile(affected_file, files_to_check=paths) |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5313 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5314 | def AndroidFilePathFilter(affected_file): |
| 5315 | paths = _ACCESSIBILITY_ANDROID_TREE_TEST_PATH |
| 5316 | return input_api.FilterSourceFile(affected_file, files_to_check=paths) |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5317 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5318 | # Only consider changes in the various tree test data paths with html type. |
| 5319 | if not any( |
| 5320 | input_api.AffectedFiles(include_deletes=True, |
| 5321 | file_filter=FilePathFilter)): |
| 5322 | return [] |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5323 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5324 | # If the commit contains any change to the Android test file, ignore. |
| 5325 | if any( |
| 5326 | input_api.AffectedFiles(include_deletes=True, |
| 5327 | file_filter=AndroidFilePathFilter)): |
| 5328 | return [] |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5329 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5330 | # Only consider changes that are adding/renaming or deleting a file |
| 5331 | message = [] |
| 5332 | for f in input_api.AffectedFiles(include_deletes=True, |
| 5333 | file_filter=FilePathFilter): |
| 5334 | if f.Action() == 'A' or f.Action() == 'D': |
| 5335 | message = ( |
| 5336 | "It appears that you are adding, renaming or deleting" |
| 5337 | "\na dump_accessibility_tree* test, but have not included" |
| 5338 | "\na corresponding change for Android." |
| 5339 | "\nPlease include (or remove) the test from:" |
| 5340 | "\n content/public/android/javatests/src/org/chromium/" |
| 5341 | "content/browser/accessibility/" |
| 5342 | "WebContentsAccessibilityTreeTest.java" |
| 5343 | "\nIf this message is confusing or annoying, please contact" |
| 5344 | "\nmembers of ui/accessibility/OWNERS.") |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5345 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5346 | # If no message was set, return empty. |
| 5347 | if not len(message): |
| 5348 | return [] |
| 5349 | |
| 5350 | return [output_api.PresubmitPromptWarning(message)] |
Mark Schillaci | e5a0be2 | 2022-01-19 00:38:39 | [diff] [blame] | 5351 | |
| 5352 | |
Bruce Dawson | 3380659 | 2022-11-16 01:44:51 | [diff] [blame] | 5353 | def CheckEsLintConfigChanges(input_api, output_api): |
| 5354 | """Suggest using "git cl presubmit --files" when .eslintrc.js files are |
| 5355 | modified. This is important because enabling an error in .eslintrc.js can |
| 5356 | trigger errors in any .js or .ts files in its directory, leading to hidden |
| 5357 | presubmit errors.""" |
| 5358 | results = [] |
| 5359 | eslint_filter = lambda f: input_api.FilterSourceFile( |
| 5360 | f, files_to_check=[r'.*\.eslintrc\.js$']) |
| 5361 | for f in input_api.AffectedFiles(include_deletes=False, |
| 5362 | file_filter=eslint_filter): |
| 5363 | local_dir = input_api.os_path.dirname(f.LocalPath()) |
| 5364 | # Use / characters so that the commands printed work on any OS. |
| 5365 | local_dir = local_dir.replace(input_api.os_path.sep, '/') |
| 5366 | if local_dir: |
| 5367 | local_dir += '/' |
| 5368 | results.append( |
| 5369 | output_api.PresubmitNotifyResult( |
| 5370 | '%(file)s modified. Consider running \'git cl presubmit --files ' |
| 5371 | '"%(dir)s*.js;%(dir)s*.ts"\' in order to check and fix the affected ' |
| 5372 | 'files before landing this change.' % |
| 5373 | { 'file' : f.LocalPath(), 'dir' : local_dir})) |
| 5374 | return results |
| 5375 | |
| 5376 | |
seanmccullough | 4a935625 | 2021-04-08 19:54:09 | [diff] [blame] | 5377 | # string pattern, sequence of strings to show when pattern matches, |
| 5378 | # error flag. True if match is a presubmit error, otherwise it's a warning. |
| 5379 | _NON_INCLUSIVE_TERMS = ( |
| 5380 | ( |
| 5381 | # Note that \b pattern in python re is pretty particular. In this |
| 5382 | # regexp, 'class WhiteList ...' will match, but 'class FooWhiteList |
| 5383 | # ...' will not. This may require some tweaking to catch these cases |
| 5384 | # without triggering a lot of false positives. Leaving it naive and |
| 5385 | # less matchy for now. |
seanmccullough | 56d1e3cf | 2021-12-03 18:18:32 | [diff] [blame] | 5386 | r'/\b(?i)((black|white)list|master|slave)\b', # nocheck |
seanmccullough | 4a935625 | 2021-04-08 19:54:09 | [diff] [blame] | 5387 | ( |
| 5388 | 'Please don\'t use blacklist, whitelist, ' # nocheck |
| 5389 | 'or slave in your', # nocheck |
| 5390 | 'code and make every effort to use other terms. Using "// nocheck"', |
| 5391 | '"# nocheck" or "<!-- nocheck -->"', |
| 5392 | 'at the end of the offending line will bypass this PRESUBMIT error', |
| 5393 | 'but avoid using this whenever possible. Reach out to', |
| 5394 | 'community@chromium.org if you have questions'), |
| 5395 | True),) |
| 5396 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5397 | def ChecksCommon(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5398 | """Checks common to both upload and commit.""" |
| 5399 | results = [] |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 5400 | results.extend( |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5401 | input_api.canned_checks.PanProjectChecks( |
| 5402 | input_api, output_api, excluded_paths=_EXCLUDED_PATHS)) |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 5403 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5404 | author = input_api.change.author_email |
| 5405 | if author and author not in _KNOWN_ROBOTS: |
| 5406 | results.extend( |
| 5407 | input_api.canned_checks.CheckAuthorizedAuthor( |
| 5408 | input_api, output_api)) |
marja@chromium.org | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 5409 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5410 | results.extend( |
| 5411 | input_api.canned_checks.CheckChangeHasNoTabs( |
| 5412 | input_api, |
| 5413 | output_api, |
| 5414 | source_file_filter=lambda x: x.LocalPath().endswith('.grd'))) |
| 5415 | results.extend( |
| 5416 | input_api.RunTests( |
| 5417 | input_api.canned_checks.CheckVPythonSpec(input_api, output_api))) |
Edward Lesmes | ce51df5 | 2020-08-04 22:10:17 | [diff] [blame] | 5418 | |
Bruce Dawson | c805448 | 2022-03-28 15:33:37 | [diff] [blame] | 5419 | dirmd = 'dirmd.bat' if input_api.is_windows else 'dirmd' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5420 | dirmd_bin = input_api.os_path.join(input_api.PresubmitLocalPath(), |
Bruce Dawson | c805448 | 2022-03-28 15:33:37 | [diff] [blame] | 5421 | 'third_party', 'depot_tools', dirmd) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5422 | results.extend( |
| 5423 | input_api.RunTests( |
| 5424 | input_api.canned_checks.CheckDirMetadataFormat( |
| 5425 | input_api, output_api, dirmd_bin))) |
| 5426 | results.extend( |
| 5427 | input_api.canned_checks.CheckOwnersDirMetadataExclusive( |
| 5428 | input_api, output_api)) |
| 5429 | results.extend( |
| 5430 | input_api.canned_checks.CheckNoNewMetadataInOwners( |
| 5431 | input_api, output_api)) |
| 5432 | results.extend( |
| 5433 | input_api.canned_checks.CheckInclusiveLanguage( |
| 5434 | input_api, |
| 5435 | output_api, |
| 5436 | excluded_directories_relative_path=[ |
| 5437 | 'infra', 'inclusive_language_presubmit_exempt_dirs.txt' |
| 5438 | ], |
| 5439 | non_inclusive_terms=_NON_INCLUSIVE_TERMS)) |
Dirk Pranke | e3c9c62d | 2021-05-18 18:35:59 | [diff] [blame] | 5440 | |
Aleksey Khoroshilov | 2978c94 | 2022-06-13 16:14:12 | [diff] [blame] | 5441 | presubmit_py_filter = lambda f: input_api.FilterSourceFile( |
Bruce Dawson | 696963f | 2022-09-13 01:15:47 | [diff] [blame] | 5442 | f, files_to_check=[r'.*PRESUBMIT\.py$']) |
Aleksey Khoroshilov | 2978c94 | 2022-06-13 16:14:12 | [diff] [blame] | 5443 | for f in input_api.AffectedFiles(include_deletes=False, |
| 5444 | file_filter=presubmit_py_filter): |
| 5445 | full_path = input_api.os_path.dirname(f.AbsoluteLocalPath()) |
| 5446 | test_file = input_api.os_path.join(full_path, 'PRESUBMIT_test.py') |
| 5447 | # The PRESUBMIT.py file (and the directory containing it) might have |
| 5448 | # been affected by being moved or removed, so only try to run the tests |
| 5449 | # if they still exist. |
| 5450 | if not input_api.os_path.exists(test_file): |
| 5451 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5452 | |
Aleksey Khoroshilov | 2978c94 | 2022-06-13 16:14:12 | [diff] [blame] | 5453 | use_python3 = False |
Bruce Dawson | 58a45d2 | 2023-02-27 11:24:16 | [diff] [blame] | 5454 | with open(f.LocalPath(), encoding='utf-8') as fp: |
Aleksey Khoroshilov | 2978c94 | 2022-06-13 16:14:12 | [diff] [blame] | 5455 | use_python3 = any( |
| 5456 | line.startswith('USE_PYTHON3 = True') |
| 5457 | for line in fp.readlines()) |
| 5458 | |
| 5459 | results.extend( |
| 5460 | input_api.canned_checks.RunUnitTestsInDirectory( |
| 5461 | input_api, |
| 5462 | output_api, |
| 5463 | full_path, |
| 5464 | files_to_check=[r'^PRESUBMIT_test\.py$'], |
| 5465 | run_on_python2=not use_python3, |
| 5466 | run_on_python3=use_python3, |
| 5467 | skip_shebang_check=True)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5468 | return results |
maruel@chromium.org | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 5469 | |
maruel@chromium.org | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 5470 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5471 | def CheckPatchFiles(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5472 | problems = [ |
| 5473 | f.LocalPath() for f in input_api.AffectedFiles() |
| 5474 | if f.LocalPath().endswith(('.orig', '.rej')) |
| 5475 | ] |
| 5476 | # Cargo.toml.orig files are part of third-party crates downloaded from |
| 5477 | # crates.io and should be included. |
| 5478 | problems = [f for f in problems if not f.endswith('Cargo.toml.orig')] |
| 5479 | if problems: |
| 5480 | return [ |
| 5481 | output_api.PresubmitError("Don't commit .rej and .orig files.", |
| 5482 | problems) |
| 5483 | ] |
| 5484 | else: |
| 5485 | return [] |
enne@chromium.org | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 5486 | |
| 5487 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5488 | def CheckBuildConfigMacrosWithoutInclude(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5489 | # Excludes OS_CHROMEOS, which is not defined in build_config.h. |
| 5490 | macro_re = input_api.re.compile( |
| 5491 | r'^\s*#(el)?if.*\bdefined\(((COMPILER_|ARCH_CPU_|WCHAR_T_IS_)[^)]*)') |
| 5492 | include_re = input_api.re.compile(r'^#include\s+"build/build_config.h"', |
| 5493 | input_api.re.MULTILINE) |
| 5494 | extension_re = input_api.re.compile(r'\.[a-z]+$') |
| 5495 | errors = [] |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 5496 | config_h_file = input_api.os_path.join('build', 'build_config.h') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5497 | for f in input_api.AffectedFiles(include_deletes=False): |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 5498 | # The build-config macros are allowed to be used in build_config.h |
| 5499 | # without including itself. |
| 5500 | if f.LocalPath() == config_h_file: |
| 5501 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5502 | if not f.LocalPath().endswith( |
| 5503 | ('.h', '.c', '.cc', '.cpp', '.m', '.mm')): |
| 5504 | continue |
| 5505 | found_line_number = None |
| 5506 | found_macro = None |
| 5507 | all_lines = input_api.ReadFile(f, 'r').splitlines() |
| 5508 | for line_num, line in enumerate(all_lines): |
| 5509 | match = macro_re.search(line) |
| 5510 | if match: |
| 5511 | found_line_number = line_num |
| 5512 | found_macro = match.group(2) |
| 5513 | break |
| 5514 | if not found_line_number: |
| 5515 | continue |
Kent Tamura | 5a8755d | 2017-06-29 23:37:07 | [diff] [blame] | 5516 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5517 | found_include_line = -1 |
| 5518 | for line_num, line in enumerate(all_lines): |
| 5519 | if include_re.search(line): |
| 5520 | found_include_line = line_num |
| 5521 | break |
| 5522 | if found_include_line >= 0 and found_include_line < found_line_number: |
| 5523 | continue |
Kent Tamura | 5a8755d | 2017-06-29 23:37:07 | [diff] [blame] | 5524 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5525 | if not f.LocalPath().endswith('.h'): |
| 5526 | primary_header_path = extension_re.sub('.h', f.AbsoluteLocalPath()) |
| 5527 | try: |
| 5528 | content = input_api.ReadFile(primary_header_path, 'r') |
| 5529 | if include_re.search(content): |
| 5530 | continue |
| 5531 | except IOError: |
| 5532 | pass |
| 5533 | errors.append('%s:%d %s macro is used without first including build/' |
| 5534 | 'build_config.h.' % |
| 5535 | (f.LocalPath(), found_line_number, found_macro)) |
| 5536 | if errors: |
| 5537 | return [output_api.PresubmitPromptWarning('\n'.join(errors))] |
| 5538 | return [] |
Kent Tamura | 5a8755d | 2017-06-29 23:37:07 | [diff] [blame] | 5539 | |
| 5540 | |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 5541 | def CheckForSuperfluousStlIncludesInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5542 | stl_include_re = input_api.re.compile(r'^#include\s+<(' |
| 5543 | r'algorithm|' |
| 5544 | r'array|' |
| 5545 | r'limits|' |
| 5546 | r'list|' |
| 5547 | r'map|' |
| 5548 | r'memory|' |
| 5549 | r'queue|' |
| 5550 | r'set|' |
| 5551 | r'string|' |
| 5552 | r'unordered_map|' |
| 5553 | r'unordered_set|' |
| 5554 | r'utility|' |
| 5555 | r'vector)>') |
| 5556 | std_namespace_re = input_api.re.compile(r'std::') |
| 5557 | errors = [] |
| 5558 | for f in input_api.AffectedFiles(): |
| 5559 | if not _IsCPlusPlusHeaderFile(input_api, f.LocalPath()): |
| 5560 | continue |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 5561 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5562 | uses_std_namespace = False |
| 5563 | has_stl_include = False |
| 5564 | for line in f.NewContents(): |
| 5565 | if has_stl_include and uses_std_namespace: |
| 5566 | break |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 5567 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5568 | if not has_stl_include and stl_include_re.search(line): |
| 5569 | has_stl_include = True |
| 5570 | continue |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 5571 | |
Bruce Dawson | 4a5579a | 2022-04-08 17:11:36 | [diff] [blame] | 5572 | if not uses_std_namespace and (std_namespace_re.search(line) |
| 5573 | or 'no-std-usage-because-pch-file' in line): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5574 | uses_std_namespace = True |
| 5575 | continue |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 5576 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5577 | if has_stl_include and not uses_std_namespace: |
| 5578 | errors.append( |
| 5579 | '%s: Includes STL header(s) but does not reference std::' % |
| 5580 | f.LocalPath()) |
| 5581 | if errors: |
| 5582 | return [output_api.PresubmitPromptWarning('\n'.join(errors))] |
| 5583 | return [] |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 5584 | |
| 5585 | |
Xiaohan Wang | 42d96c2 | 2022-01-20 17:23:11 | [diff] [blame] | 5586 | def _CheckForDeprecatedOSMacrosInFile(input_api, f): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5587 | """Check for sensible looking, totally invalid OS macros.""" |
| 5588 | preprocessor_statement = input_api.re.compile(r'^\s*#') |
| 5589 | os_macro = input_api.re.compile(r'defined\(OS_([^)]+)\)') |
| 5590 | results = [] |
| 5591 | for lnum, line in f.ChangedContents(): |
| 5592 | if preprocessor_statement.search(line): |
| 5593 | for match in os_macro.finditer(line): |
| 5594 | results.append( |
| 5595 | ' %s:%d: %s' % |
| 5596 | (f.LocalPath(), lnum, 'defined(OS_' + match.group(1) + |
| 5597 | ') -> BUILDFLAG(IS_' + match.group(1) + ')')) |
| 5598 | return results |
dbeam@chromium.org | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 5599 | |
| 5600 | |
Xiaohan Wang | 42d96c2 | 2022-01-20 17:23:11 | [diff] [blame] | 5601 | def CheckForDeprecatedOSMacros(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5602 | """Check all affected files for invalid OS macros.""" |
| 5603 | bad_macros = [] |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 5604 | # The OS_ macros are allowed to be used in build/build_config.h. |
| 5605 | config_h_file = input_api.os_path.join('build', 'build_config.h') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5606 | for f in input_api.AffectedSourceFiles(None): |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 5607 | if not f.LocalPath().endswith(('.py', '.js', '.html', '.css', '.md')) \ |
| 5608 | and f.LocalPath() != config_h_file: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5609 | bad_macros.extend(_CheckForDeprecatedOSMacrosInFile(input_api, f)) |
dbeam@chromium.org | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 5610 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5611 | if not bad_macros: |
| 5612 | return [] |
dbeam@chromium.org | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 5613 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5614 | return [ |
| 5615 | output_api.PresubmitError( |
| 5616 | 'OS macros have been deprecated. Please use BUILDFLAGs instead (still ' |
| 5617 | 'defined in build_config.h):', bad_macros) |
| 5618 | ] |
dbeam@chromium.org | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 5619 | |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 5620 | |
| 5621 | def _CheckForInvalidIfDefinedMacrosInFile(input_api, f): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5622 | """Check all affected files for invalid "if defined" macros.""" |
| 5623 | ALWAYS_DEFINED_MACROS = ( |
| 5624 | "TARGET_CPU_PPC", |
| 5625 | "TARGET_CPU_PPC64", |
| 5626 | "TARGET_CPU_68K", |
| 5627 | "TARGET_CPU_X86", |
| 5628 | "TARGET_CPU_ARM", |
| 5629 | "TARGET_CPU_MIPS", |
| 5630 | "TARGET_CPU_SPARC", |
| 5631 | "TARGET_CPU_ALPHA", |
| 5632 | "TARGET_IPHONE_SIMULATOR", |
| 5633 | "TARGET_OS_EMBEDDED", |
| 5634 | "TARGET_OS_IPHONE", |
| 5635 | "TARGET_OS_MAC", |
| 5636 | "TARGET_OS_UNIX", |
| 5637 | "TARGET_OS_WIN32", |
| 5638 | ) |
| 5639 | ifdef_macro = input_api.re.compile( |
| 5640 | r'^\s*#.*(?:ifdef\s|defined\()([^\s\)]+)') |
| 5641 | results = [] |
| 5642 | for lnum, line in f.ChangedContents(): |
| 5643 | for match in ifdef_macro.finditer(line): |
| 5644 | if match.group(1) in ALWAYS_DEFINED_MACROS: |
| 5645 | always_defined = ' %s is always defined. ' % match.group(1) |
| 5646 | did_you_mean = 'Did you mean \'#if %s\'?' % match.group(1) |
| 5647 | results.append( |
| 5648 | ' %s:%d %s\n\t%s' % |
| 5649 | (f.LocalPath(), lnum, always_defined, did_you_mean)) |
| 5650 | return results |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 5651 | |
| 5652 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5653 | def CheckForInvalidIfDefinedMacros(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5654 | """Check all affected files for invalid "if defined" macros.""" |
| 5655 | bad_macros = [] |
| 5656 | skipped_paths = ['third_party/sqlite/', 'third_party/abseil-cpp/'] |
| 5657 | for f in input_api.AffectedFiles(): |
| 5658 | if any([f.LocalPath().startswith(path) for path in skipped_paths]): |
| 5659 | continue |
| 5660 | if f.LocalPath().endswith(('.h', '.c', '.cc', '.m', '.mm')): |
| 5661 | bad_macros.extend( |
| 5662 | _CheckForInvalidIfDefinedMacrosInFile(input_api, f)) |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 5663 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5664 | if not bad_macros: |
| 5665 | return [] |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 5666 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5667 | return [ |
| 5668 | output_api.PresubmitError( |
| 5669 | 'Found ifdef check on always-defined macro[s]. Please fix your code\n' |
| 5670 | 'or check the list of ALWAYS_DEFINED_MACROS in src/PRESUBMIT.py.', |
| 5671 | bad_macros) |
| 5672 | ] |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 5673 | |
| 5674 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5675 | def CheckForIPCRules(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5676 | """Check for same IPC rules described in |
| 5677 | http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc |
| 5678 | """ |
| 5679 | base_pattern = r'IPC_ENUM_TRAITS\(' |
| 5680 | inclusion_pattern = input_api.re.compile(r'(%s)' % base_pattern) |
| 5681 | comment_pattern = input_api.re.compile(r'//.*(%s)' % base_pattern) |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 5682 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5683 | problems = [] |
| 5684 | for f in input_api.AffectedSourceFiles(None): |
| 5685 | local_path = f.LocalPath() |
| 5686 | if not local_path.endswith('.h'): |
| 5687 | continue |
| 5688 | for line_number, line in f.ChangedContents(): |
| 5689 | if inclusion_pattern.search( |
| 5690 | line) and not comment_pattern.search(line): |
| 5691 | problems.append('%s:%d\n %s' % |
| 5692 | (local_path, line_number, line.strip())) |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 5693 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5694 | if problems: |
| 5695 | return [ |
| 5696 | output_api.PresubmitPromptWarning(_IPC_ENUM_TRAITS_DEPRECATED, |
| 5697 | problems) |
| 5698 | ] |
| 5699 | else: |
| 5700 | return [] |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 5701 | |
dbeam@chromium.org | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 5702 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5703 | def CheckForLongPathnames(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5704 | """Check to make sure no files being submitted have long paths. |
| 5705 | This causes issues on Windows. |
| 5706 | """ |
| 5707 | problems = [] |
| 5708 | for f in input_api.AffectedTestableFiles(): |
| 5709 | local_path = f.LocalPath() |
| 5710 | # Windows has a path limit of 260 characters. Limit path length to 200 so |
| 5711 | # that we have some extra for the prefix on dev machines and the bots. |
| 5712 | if len(local_path) > 200: |
| 5713 | problems.append(local_path) |
Stephen Martinis | 97a39414 | 2018-06-07 23:06:05 | [diff] [blame] | 5714 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5715 | if problems: |
| 5716 | return [output_api.PresubmitError(_LONG_PATH_ERROR, problems)] |
| 5717 | else: |
| 5718 | return [] |
Stephen Martinis | 97a39414 | 2018-06-07 23:06:05 | [diff] [blame] | 5719 | |
| 5720 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5721 | def CheckForIncludeGuards(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5722 | """Check that header files have proper guards against multiple inclusion. |
| 5723 | If a file should not have such guards (and it probably should) then it |
Bruce Dawson | 4a5579a | 2022-04-08 17:11:36 | [diff] [blame] | 5724 | should include the string "no-include-guard-because-multiply-included" or |
| 5725 | "no-include-guard-because-pch-file". |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5726 | """ |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5727 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5728 | def is_chromium_header_file(f): |
| 5729 | # We only check header files under the control of the Chromium |
| 5730 | # project. That is, those outside third_party apart from |
| 5731 | # third_party/blink. |
| 5732 | # We also exclude *_message_generator.h headers as they use |
| 5733 | # include guards in a special, non-typical way. |
| 5734 | file_with_path = input_api.os_path.normpath(f.LocalPath()) |
| 5735 | return (file_with_path.endswith('.h') |
| 5736 | and not file_with_path.endswith('_message_generator.h') |
Bruce Dawson | 4c4c292 | 2022-05-02 18:07:33 | [diff] [blame] | 5737 | and not file_with_path.endswith('com_imported_mstscax.h') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5738 | and (not file_with_path.startswith('third_party') |
| 5739 | or file_with_path.startswith( |
| 5740 | input_api.os_path.join('third_party', 'blink')))) |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5741 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5742 | def replace_special_with_underscore(string): |
| 5743 | return input_api.re.sub(r'[+\\/.-]', '_', string) |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5744 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5745 | errors = [] |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5746 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5747 | for f in input_api.AffectedSourceFiles(is_chromium_header_file): |
| 5748 | guard_name = None |
| 5749 | guard_line_number = None |
| 5750 | seen_guard_end = False |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5751 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5752 | file_with_path = input_api.os_path.normpath(f.LocalPath()) |
| 5753 | base_file_name = input_api.os_path.splitext( |
| 5754 | input_api.os_path.basename(file_with_path))[0] |
| 5755 | upper_base_file_name = base_file_name.upper() |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5756 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5757 | expected_guard = replace_special_with_underscore( |
| 5758 | file_with_path.upper() + '_') |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5759 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5760 | # For "path/elem/file_name.h" we should really only accept |
| 5761 | # PATH_ELEM_FILE_NAME_H_ per coding style. Unfortunately there |
| 5762 | # are too many (1000+) files with slight deviations from the |
| 5763 | # coding style. The most important part is that the include guard |
| 5764 | # is there, and that it's unique, not the name so this check is |
| 5765 | # forgiving for existing files. |
| 5766 | # |
| 5767 | # As code becomes more uniform, this could be made stricter. |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5768 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5769 | guard_name_pattern_list = [ |
| 5770 | # Anything with the right suffix (maybe with an extra _). |
| 5771 | r'\w+_H__?', |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5772 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5773 | # To cover include guards with old Blink style. |
| 5774 | r'\w+_h', |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5775 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5776 | # Anything including the uppercase name of the file. |
| 5777 | r'\w*' + input_api.re.escape( |
| 5778 | replace_special_with_underscore(upper_base_file_name)) + |
| 5779 | r'\w*', |
| 5780 | ] |
| 5781 | guard_name_pattern = '|'.join(guard_name_pattern_list) |
| 5782 | guard_pattern = input_api.re.compile(r'#ifndef\s+(' + |
| 5783 | guard_name_pattern + ')') |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5784 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5785 | for line_number, line in enumerate(f.NewContents()): |
Bruce Dawson | 4a5579a | 2022-04-08 17:11:36 | [diff] [blame] | 5786 | if ('no-include-guard-because-multiply-included' in line |
| 5787 | or 'no-include-guard-because-pch-file' in line): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5788 | guard_name = 'DUMMY' # To not trigger check outside the loop. |
| 5789 | break |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5790 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5791 | if guard_name is None: |
| 5792 | match = guard_pattern.match(line) |
| 5793 | if match: |
| 5794 | guard_name = match.group(1) |
| 5795 | guard_line_number = line_number |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5796 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5797 | # We allow existing files to use include guards whose names |
| 5798 | # don't match the chromium style guide, but new files should |
| 5799 | # get it right. |
Bruce Dawson | 6cc154e | 2022-04-12 20:39:49 | [diff] [blame] | 5800 | if guard_name != expected_guard: |
Bruce Dawson | 95eb756 | 2022-09-14 15:27:16 | [diff] [blame] | 5801 | if f.Action() == 'A': # If file was just 'A'dded |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5802 | errors.append( |
| 5803 | output_api.PresubmitPromptWarning( |
| 5804 | 'Header using the wrong include guard name %s' |
| 5805 | % guard_name, [ |
| 5806 | '%s:%d' % |
| 5807 | (f.LocalPath(), line_number + 1) |
| 5808 | ], 'Expected: %r\nFound: %r' % |
| 5809 | (expected_guard, guard_name))) |
| 5810 | else: |
| 5811 | # The line after #ifndef should have a #define of the same name. |
| 5812 | if line_number == guard_line_number + 1: |
| 5813 | expected_line = '#define %s' % guard_name |
| 5814 | if line != expected_line: |
| 5815 | errors.append( |
| 5816 | output_api.PresubmitPromptWarning( |
| 5817 | 'Missing "%s" for include guard' % |
| 5818 | expected_line, |
| 5819 | ['%s:%d' % (f.LocalPath(), line_number + 1)], |
| 5820 | 'Expected: %r\nGot: %r' % |
| 5821 | (expected_line, line))) |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5822 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5823 | if not seen_guard_end and line == '#endif // %s' % guard_name: |
| 5824 | seen_guard_end = True |
| 5825 | elif seen_guard_end: |
| 5826 | if line.strip() != '': |
| 5827 | errors.append( |
| 5828 | output_api.PresubmitPromptWarning( |
| 5829 | 'Include guard %s not covering the whole file' |
| 5830 | % (guard_name), [f.LocalPath()])) |
| 5831 | break # Nothing else to check and enough to warn once. |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5832 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5833 | if guard_name is None: |
| 5834 | errors.append( |
| 5835 | output_api.PresubmitPromptWarning( |
Bruce Dawson | 32114b6 | 2022-04-11 16:45:49 | [diff] [blame] | 5836 | 'Missing include guard in %s\n' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5837 | 'Recommended name: %s\n' |
| 5838 | 'This check can be disabled by having the string\n' |
Bruce Dawson | 4a5579a | 2022-04-08 17:11:36 | [diff] [blame] | 5839 | '"no-include-guard-because-multiply-included" or\n' |
| 5840 | '"no-include-guard-because-pch-file" in the header.' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5841 | % (f.LocalPath(), expected_guard))) |
| 5842 | |
| 5843 | return errors |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 5844 | |
| 5845 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5846 | def CheckForWindowsLineEndings(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5847 | """Check source code and known ascii text files for Windows style line |
| 5848 | endings. |
| 5849 | """ |
Bruce Dawson | 5efbdc65 | 2022-04-11 19:29:51 | [diff] [blame] | 5850 | known_text_files = r'.*\.(txt|html|htm|py|gyp|gypi|gn|isolate|icon)$' |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 5851 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5852 | file_inclusion_pattern = (known_text_files, |
| 5853 | r'.+%s' % _IMPLEMENTATION_EXTENSIONS, |
| 5854 | r'.+%s' % _HEADER_EXTENSIONS) |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 5855 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5856 | problems = [] |
| 5857 | source_file_filter = lambda f: input_api.FilterSourceFile( |
| 5858 | f, files_to_check=file_inclusion_pattern, files_to_skip=None) |
| 5859 | for f in input_api.AffectedSourceFiles(source_file_filter): |
Bruce Dawson | 5efbdc65 | 2022-04-11 19:29:51 | [diff] [blame] | 5860 | # Ignore test files that contain crlf intentionally. |
| 5861 | if f.LocalPath().endswith('crlf.txt'): |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 5862 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5863 | include_file = False |
| 5864 | for line in input_api.ReadFile(f, 'r').splitlines(True): |
| 5865 | if line.endswith('\r\n'): |
| 5866 | include_file = True |
| 5867 | if include_file: |
| 5868 | problems.append(f.LocalPath()) |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 5869 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5870 | if problems: |
| 5871 | return [ |
| 5872 | output_api.PresubmitPromptWarning( |
| 5873 | 'Are you sure that you want ' |
| 5874 | 'these files to contain Windows style line endings?\n' + |
| 5875 | '\n'.join(problems)) |
| 5876 | ] |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 5877 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5878 | return [] |
| 5879 | |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 5880 | |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 5881 | def CheckIconFilesForLicenseHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5882 | """Check that .icon files (which are fragments of C++) have license headers. |
| 5883 | """ |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 5884 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5885 | icon_files = (r'.*\.icon$', ) |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 5886 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5887 | icons = lambda x: input_api.FilterSourceFile(x, files_to_check=icon_files) |
| 5888 | return input_api.canned_checks.CheckLicense(input_api, |
| 5889 | output_api, |
| 5890 | source_file_filter=icons) |
| 5891 | |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 5892 | |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 5893 | def CheckForUseOfChromeAppsDeprecations(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5894 | """Check source code for use of Chrome App technologies being |
| 5895 | deprecated. |
| 5896 | """ |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 5897 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5898 | def _CheckForDeprecatedTech(input_api, |
| 5899 | output_api, |
| 5900 | detection_list, |
| 5901 | files_to_check=None, |
| 5902 | files_to_skip=None): |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 5903 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5904 | if (files_to_check or files_to_skip): |
| 5905 | source_file_filter = lambda f: input_api.FilterSourceFile( |
| 5906 | f, files_to_check=files_to_check, files_to_skip=files_to_skip) |
| 5907 | else: |
| 5908 | source_file_filter = None |
| 5909 | |
| 5910 | problems = [] |
| 5911 | |
| 5912 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 5913 | if f.Action() == 'D': |
| 5914 | continue |
| 5915 | for _, line in f.ChangedContents(): |
| 5916 | if any(detect in line for detect in detection_list): |
| 5917 | problems.append(f.LocalPath()) |
| 5918 | |
| 5919 | return problems |
| 5920 | |
| 5921 | # to avoid this presubmit script triggering warnings |
| 5922 | files_to_skip = ['PRESUBMIT.py', 'PRESUBMIT_test.py'] |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 5923 | |
| 5924 | problems = [] |
| 5925 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5926 | # NMF: any files with extensions .nmf or NMF |
| 5927 | _NMF_FILES = r'\.(nmf|NMF)$' |
| 5928 | problems += _CheckForDeprecatedTech( |
| 5929 | input_api, |
| 5930 | output_api, |
| 5931 | detection_list=[''], # any change to the file will trigger warning |
| 5932 | files_to_check=[r'.+%s' % _NMF_FILES]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 5933 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5934 | # MANIFEST: any manifest.json that in its diff includes "app": |
| 5935 | _MANIFEST_FILES = r'(manifest\.json)$' |
| 5936 | problems += _CheckForDeprecatedTech( |
| 5937 | input_api, |
| 5938 | output_api, |
| 5939 | detection_list=['"app":'], |
| 5940 | files_to_check=[r'.*%s' % _MANIFEST_FILES]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 5941 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5942 | # NaCl / PNaCl: any file that in its diff contains the strings in the list |
| 5943 | problems += _CheckForDeprecatedTech( |
| 5944 | input_api, |
| 5945 | output_api, |
| 5946 | detection_list=['config=nacl', 'enable-nacl', 'cpu=pnacl', 'nacl_io'], |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5947 | files_to_skip=files_to_skip + [r"^native_client_sdk/"]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 5948 | |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 5949 | # PPAPI: any C/C++ file that in its diff includes a ppapi library |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5950 | problems += _CheckForDeprecatedTech( |
| 5951 | input_api, |
| 5952 | output_api, |
| 5953 | detection_list=['#include "ppapi', '#include <ppapi'], |
| 5954 | files_to_check=(r'.+%s' % _HEADER_EXTENSIONS, |
| 5955 | r'.+%s' % _IMPLEMENTATION_EXTENSIONS), |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5956 | files_to_skip=[r"^ppapi/"]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 5957 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5958 | if problems: |
| 5959 | return [ |
| 5960 | output_api.PresubmitPromptWarning( |
| 5961 | 'You are adding/modifying code' |
| 5962 | 'related to technologies which will soon be deprecated (Chrome Apps, NaCl,' |
| 5963 | ' PNaCl, PPAPI). See this blog post for more details:\n' |
| 5964 | 'https://blog.chromium.org/2020/08/changes-to-chrome-app-support-timeline.html\n' |
| 5965 | 'and this documentation for options to replace these technologies:\n' |
| 5966 | 'https://developer.chrome.com/docs/apps/migration/\n' + |
| 5967 | '\n'.join(problems)) |
| 5968 | ] |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 5969 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5970 | return [] |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 5971 | |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 5972 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5973 | def CheckSyslogUseWarningOnUpload(input_api, output_api, src_file_filter=None): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5974 | """Checks that all source files use SYSLOG properly.""" |
| 5975 | syslog_files = [] |
| 5976 | for f in input_api.AffectedSourceFiles(src_file_filter): |
| 5977 | for line_number, line in f.ChangedContents(): |
| 5978 | if 'SYSLOG' in line: |
| 5979 | syslog_files.append(f.LocalPath() + ':' + str(line_number)) |
pastarmovj | 032ba5bc | 2017-01-12 10:41:56 | [diff] [blame] | 5980 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5981 | if syslog_files: |
| 5982 | return [ |
| 5983 | output_api.PresubmitPromptWarning( |
| 5984 | 'Please make sure there are no privacy sensitive bits of data in SYSLOG' |
| 5985 | ' calls.\nFiles to check:\n', |
| 5986 | items=syslog_files) |
| 5987 | ] |
| 5988 | return [] |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 5989 | |
| 5990 | |
maruel@chromium.org | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 5991 | def CheckChangeOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5992 | if input_api.version < [2, 0, 0]: |
| 5993 | return [ |
| 5994 | output_api.PresubmitError( |
| 5995 | "Your depot_tools is out of date. " |
| 5996 | "This PRESUBMIT.py requires at least presubmit_support version 2.0.0, " |
| 5997 | "but your version is %d.%d.%d" % tuple(input_api.version)) |
| 5998 | ] |
| 5999 | results = [] |
| 6000 | results.extend( |
| 6001 | input_api.canned_checks.CheckPatchFormatted(input_api, output_api)) |
| 6002 | return results |
maruel@chromium.org | ca8d1984 | 2009-02-19 16:33:12 | [diff] [blame] | 6003 | |
| 6004 | |
| 6005 | def CheckChangeOnCommit(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6006 | if input_api.version < [2, 0, 0]: |
| 6007 | return [ |
| 6008 | output_api.PresubmitError( |
| 6009 | "Your depot_tools is out of date. " |
| 6010 | "This PRESUBMIT.py requires at least presubmit_support version 2.0.0, " |
| 6011 | "but your version is %d.%d.%d" % tuple(input_api.version)) |
| 6012 | ] |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6013 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6014 | results = [] |
| 6015 | # Make sure the tree is 'open'. |
| 6016 | results.extend( |
| 6017 | input_api.canned_checks.CheckTreeIsOpen( |
| 6018 | input_api, |
| 6019 | output_api, |
| 6020 | json_url='http://chromium-status.appspot.com/current?format=json')) |
maruel@chromium.org | 806e98e | 2010-03-19 17:49:27 | [diff] [blame] | 6021 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6022 | results.extend( |
| 6023 | input_api.canned_checks.CheckPatchFormatted(input_api, output_api)) |
| 6024 | results.extend( |
| 6025 | input_api.canned_checks.CheckChangeHasBugField(input_api, output_api)) |
| 6026 | results.extend( |
| 6027 | input_api.canned_checks.CheckChangeHasNoUnwantedTags( |
| 6028 | input_api, output_api)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6029 | return results |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6030 | |
| 6031 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6032 | def CheckStrings(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6033 | """Check string ICU syntax validity and if translation screenshots exist.""" |
| 6034 | # Skip translation screenshots check if a SkipTranslationScreenshotsCheck |
| 6035 | # footer is set to true. |
| 6036 | git_footers = input_api.change.GitFootersFromDescription() |
| 6037 | skip_screenshot_check_footer = [ |
| 6038 | footer.lower() for footer in git_footers.get( |
| 6039 | u'Skip-Translation-Screenshots-Check', []) |
| 6040 | ] |
| 6041 | run_screenshot_check = u'true' not in skip_screenshot_check_footer |
Edward Lesmes | f7c5c6d | 2020-05-14 23:30:02 | [diff] [blame] | 6042 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6043 | import os |
| 6044 | import re |
| 6045 | import sys |
| 6046 | from io import StringIO |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6047 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6048 | new_or_added_paths = set(f.LocalPath() for f in input_api.AffectedFiles() |
| 6049 | if (f.Action() == 'A' or f.Action() == 'M')) |
| 6050 | removed_paths = set(f.LocalPath() |
| 6051 | for f in input_api.AffectedFiles(include_deletes=True) |
| 6052 | if f.Action() == 'D') |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6053 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6054 | affected_grds = [ |
| 6055 | f for f in input_api.AffectedFiles() |
| 6056 | if f.LocalPath().endswith(('.grd', '.grdp')) |
| 6057 | ] |
| 6058 | affected_grds = [ |
| 6059 | f for f in affected_grds if not 'testdata' in f.LocalPath() |
| 6060 | ] |
| 6061 | if not affected_grds: |
| 6062 | return [] |
meacer | 8c0d383 | 2019-12-26 21:46:16 | [diff] [blame] | 6063 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6064 | affected_png_paths = [ |
| 6065 | f.AbsoluteLocalPath() for f in input_api.AffectedFiles() |
| 6066 | if (f.LocalPath().endswith('.png')) |
| 6067 | ] |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6068 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6069 | # Check for screenshots. Developers can upload screenshots using |
| 6070 | # tools/translation/upload_screenshots.py which finds and uploads |
| 6071 | # images associated with .grd files (e.g. test_grd/IDS_STRING.png for the |
| 6072 | # message named IDS_STRING in test.grd) and produces a .sha1 file (e.g. |
| 6073 | # test_grd/IDS_STRING.png.sha1) for each png when the upload is successful. |
| 6074 | # |
| 6075 | # The logic here is as follows: |
| 6076 | # |
| 6077 | # - If the CL has a .png file under the screenshots directory for a grd |
| 6078 | # file, warn the developer. Actual images should never be checked into the |
| 6079 | # Chrome repo. |
| 6080 | # |
| 6081 | # - If the CL contains modified or new messages in grd files and doesn't |
| 6082 | # contain the corresponding .sha1 files, warn the developer to add images |
| 6083 | # and upload them via tools/translation/upload_screenshots.py. |
| 6084 | # |
| 6085 | # - If the CL contains modified or new messages in grd files and the |
| 6086 | # corresponding .sha1 files, everything looks good. |
| 6087 | # |
| 6088 | # - If the CL contains removed messages in grd files but the corresponding |
| 6089 | # .sha1 files aren't removed, warn the developer to remove them. |
| 6090 | unnecessary_screenshots = [] |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6091 | invalid_sha1 = [] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6092 | missing_sha1 = [] |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6093 | missing_sha1_modified = [] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6094 | unnecessary_sha1_files = [] |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6095 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6096 | # This checks verifies that the ICU syntax of messages this CL touched is |
| 6097 | # valid, and reports any found syntax errors. |
| 6098 | # Without this presubmit check, ICU syntax errors in Chromium strings can land |
| 6099 | # without developers being aware of them. Later on, such ICU syntax errors |
| 6100 | # break message extraction for translation, hence would block Chromium |
| 6101 | # translations until they are fixed. |
| 6102 | icu_syntax_errors = [] |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6103 | sha1_pattern = input_api.re.compile(r'^[a-fA-F0-9]{40}$', |
| 6104 | input_api.re.MULTILINE) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6105 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6106 | def _CheckScreenshotAdded(screenshots_dir, message_id): |
| 6107 | sha1_path = input_api.os_path.join(screenshots_dir, |
| 6108 | message_id + '.png.sha1') |
| 6109 | if sha1_path not in new_or_added_paths: |
| 6110 | missing_sha1.append(sha1_path) |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6111 | elif not _CheckValidSha1(sha1_path): |
| 6112 | invalid_sha1.append(sha1_path) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6113 | |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6114 | def _CheckScreenshotModified(screenshots_dir, message_id): |
| 6115 | sha1_path = input_api.os_path.join(screenshots_dir, |
| 6116 | message_id + '.png.sha1') |
| 6117 | if sha1_path not in new_or_added_paths: |
| 6118 | missing_sha1_modified.append(sha1_path) |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6119 | elif not _CheckValidSha1(sha1_path): |
| 6120 | invalid_sha1.append(sha1_path) |
| 6121 | |
| 6122 | def _CheckValidSha1(sha1_path): |
| 6123 | return sha1_pattern.search( |
| 6124 | next( |
| 6125 | "\n".join(f.NewContents()) |
| 6126 | for f in input_api.AffectedFiles() |
| 6127 | if f.LocalPath() == sha1_path |
| 6128 | ) |
| 6129 | ) |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6130 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6131 | def _CheckScreenshotRemoved(screenshots_dir, message_id): |
| 6132 | sha1_path = input_api.os_path.join(screenshots_dir, |
| 6133 | message_id + '.png.sha1') |
| 6134 | if input_api.os_path.exists( |
| 6135 | sha1_path) and sha1_path not in removed_paths: |
| 6136 | unnecessary_sha1_files.append(sha1_path) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6137 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6138 | def _ValidateIcuSyntax(text, level, signatures): |
| 6139 | """Validates ICU syntax of a text string. |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6140 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6141 | Check if text looks similar to ICU and checks for ICU syntax correctness |
| 6142 | in this case. Reports various issues with ICU syntax and values of |
| 6143 | variants. Supports checking of nested messages. Accumulate information of |
| 6144 | each ICU messages found in the text for further checking. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6145 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6146 | Args: |
| 6147 | text: a string to check. |
| 6148 | level: a number of current nesting level. |
| 6149 | signatures: an accumulator, a list of tuple of (level, variable, |
| 6150 | kind, variants). |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6151 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6152 | Returns: |
| 6153 | None if a string is not ICU or no issue detected. |
| 6154 | A tuple of (message, start index, end index) if an issue detected. |
| 6155 | """ |
| 6156 | valid_types = { |
| 6157 | 'plural': (frozenset( |
| 6158 | ['=0', '=1', 'zero', 'one', 'two', 'few', 'many', |
| 6159 | 'other']), frozenset(['=1', 'other'])), |
| 6160 | 'selectordinal': (frozenset( |
| 6161 | ['=0', '=1', 'zero', 'one', 'two', 'few', 'many', |
| 6162 | 'other']), frozenset(['one', 'other'])), |
| 6163 | 'select': (frozenset(), frozenset(['other'])), |
| 6164 | } |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6165 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6166 | # Check if the message looks like an attempt to use ICU |
| 6167 | # plural. If yes - check if its syntax strictly matches ICU format. |
| 6168 | like = re.match(r'^[^{]*\{[^{]*\b(plural|selectordinal|select)\b', |
| 6169 | text) |
| 6170 | if not like: |
| 6171 | signatures.append((level, None, None, None)) |
| 6172 | return |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6173 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6174 | # Check for valid prefix and suffix |
| 6175 | m = re.match( |
| 6176 | r'^([^{]*\{)([a-zA-Z0-9_]+),\s*' |
| 6177 | r'(plural|selectordinal|select),\s*' |
| 6178 | r'(?:offset:\d+)?\s*(.*)', text, re.DOTALL) |
| 6179 | if not m: |
| 6180 | return (('This message looks like an ICU plural, ' |
| 6181 | 'but does not follow ICU syntax.'), like.start(), |
| 6182 | like.end()) |
| 6183 | starting, variable, kind, variant_pairs = m.groups() |
| 6184 | variants, depth, last_pos = _ParseIcuVariants(variant_pairs, |
| 6185 | m.start(4)) |
| 6186 | if depth: |
| 6187 | return ('Invalid ICU format. Unbalanced opening bracket', last_pos, |
| 6188 | len(text)) |
| 6189 | first = text[0] |
| 6190 | ending = text[last_pos:] |
| 6191 | if not starting: |
| 6192 | return ('Invalid ICU format. No initial opening bracket', |
| 6193 | last_pos - 1, last_pos) |
| 6194 | if not ending or '}' not in ending: |
| 6195 | return ('Invalid ICU format. No final closing bracket', |
| 6196 | last_pos - 1, last_pos) |
| 6197 | elif first != '{': |
| 6198 | return (( |
| 6199 | 'Invalid ICU format. Extra characters at the start of a complex ' |
| 6200 | 'message (go/icu-message-migration): "%s"') % starting, 0, |
| 6201 | len(starting)) |
| 6202 | elif ending != '}': |
| 6203 | return (( |
| 6204 | 'Invalid ICU format. Extra characters at the end of a complex ' |
| 6205 | 'message (go/icu-message-migration): "%s"') % ending, |
| 6206 | last_pos - 1, len(text) - 1) |
| 6207 | if kind not in valid_types: |
| 6208 | return (('Unknown ICU message type %s. ' |
| 6209 | 'Valid types are: plural, select, selectordinal') % kind, |
| 6210 | 0, 0) |
| 6211 | known, required = valid_types[kind] |
| 6212 | defined_variants = set() |
| 6213 | for variant, variant_range, value, value_range in variants: |
| 6214 | start, end = variant_range |
| 6215 | if variant in defined_variants: |
| 6216 | return ('Variant "%s" is defined more than once' % variant, |
| 6217 | start, end) |
| 6218 | elif known and variant not in known: |
| 6219 | return ('Variant "%s" is not valid for %s message' % |
| 6220 | (variant, kind), start, end) |
| 6221 | defined_variants.add(variant) |
| 6222 | # Check for nested structure |
| 6223 | res = _ValidateIcuSyntax(value[1:-1], level + 1, signatures) |
| 6224 | if res: |
| 6225 | return (res[0], res[1] + value_range[0] + 1, |
| 6226 | res[2] + value_range[0] + 1) |
| 6227 | missing = required - defined_variants |
| 6228 | if missing: |
| 6229 | return ('Required variants missing: %s' % ', '.join(missing), 0, |
| 6230 | len(text)) |
| 6231 | signatures.append((level, variable, kind, defined_variants)) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6232 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6233 | def _ParseIcuVariants(text, offset=0): |
| 6234 | """Parse variants part of ICU complex message. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6235 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6236 | Builds a tuple of variant names and values, as well as |
| 6237 | their offsets in the input string. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6238 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6239 | Args: |
| 6240 | text: a string to parse |
| 6241 | offset: additional offset to add to positions in the text to get correct |
| 6242 | position in the complete ICU string. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6243 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6244 | Returns: |
| 6245 | List of tuples, each tuple consist of four fields: variant name, |
| 6246 | variant name span (tuple of two integers), variant value, value |
| 6247 | span (tuple of two integers). |
| 6248 | """ |
| 6249 | depth, start, end = 0, -1, -1 |
| 6250 | variants = [] |
| 6251 | key = None |
| 6252 | for idx, char in enumerate(text): |
| 6253 | if char == '{': |
| 6254 | if not depth: |
| 6255 | start = idx |
| 6256 | chunk = text[end + 1:start] |
| 6257 | key = chunk.strip() |
| 6258 | pos = offset + end + 1 + chunk.find(key) |
| 6259 | span = (pos, pos + len(key)) |
| 6260 | depth += 1 |
| 6261 | elif char == '}': |
| 6262 | if not depth: |
| 6263 | return variants, depth, offset + idx |
| 6264 | depth -= 1 |
| 6265 | if not depth: |
| 6266 | end = idx |
| 6267 | variants.append((key, span, text[start:end + 1], |
| 6268 | (offset + start, offset + end + 1))) |
| 6269 | return variants, depth, offset + end + 1 |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6270 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6271 | try: |
| 6272 | old_sys_path = sys.path |
| 6273 | sys.path = sys.path + [ |
| 6274 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 6275 | 'translation') |
| 6276 | ] |
| 6277 | from helper import grd_helper |
| 6278 | finally: |
| 6279 | sys.path = old_sys_path |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6280 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6281 | for f in affected_grds: |
| 6282 | file_path = f.LocalPath() |
| 6283 | old_id_to_msg_map = {} |
| 6284 | new_id_to_msg_map = {} |
| 6285 | # Note that this code doesn't check if the file has been deleted. This is |
| 6286 | # OK because it only uses the old and new file contents and doesn't load |
| 6287 | # the file via its path. |
| 6288 | # It's also possible that a file's content refers to a renamed or deleted |
| 6289 | # file via a <part> tag, such as <part file="now-deleted-file.grdp">. This |
| 6290 | # is OK as well, because grd_helper ignores <part> tags when loading .grd or |
| 6291 | # .grdp files. |
| 6292 | if file_path.endswith('.grdp'): |
| 6293 | if f.OldContents(): |
| 6294 | old_id_to_msg_map = grd_helper.GetGrdpMessagesFromString( |
| 6295 | '\n'.join(f.OldContents())) |
| 6296 | if f.NewContents(): |
| 6297 | new_id_to_msg_map = grd_helper.GetGrdpMessagesFromString( |
| 6298 | '\n'.join(f.NewContents())) |
| 6299 | else: |
| 6300 | file_dir = input_api.os_path.dirname(file_path) or '.' |
| 6301 | if f.OldContents(): |
| 6302 | old_id_to_msg_map = grd_helper.GetGrdMessages( |
| 6303 | StringIO('\n'.join(f.OldContents())), file_dir) |
| 6304 | if f.NewContents(): |
| 6305 | new_id_to_msg_map = grd_helper.GetGrdMessages( |
| 6306 | StringIO('\n'.join(f.NewContents())), file_dir) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6307 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6308 | grd_name, ext = input_api.os_path.splitext( |
| 6309 | input_api.os_path.basename(file_path)) |
| 6310 | screenshots_dir = input_api.os_path.join( |
| 6311 | input_api.os_path.dirname(file_path), |
| 6312 | grd_name + ext.replace('.', '_')) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6313 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6314 | # Compute added, removed and modified message IDs. |
| 6315 | old_ids = set(old_id_to_msg_map) |
| 6316 | new_ids = set(new_id_to_msg_map) |
| 6317 | added_ids = new_ids - old_ids |
| 6318 | removed_ids = old_ids - new_ids |
| 6319 | modified_ids = set([]) |
| 6320 | for key in old_ids.intersection(new_ids): |
| 6321 | if (old_id_to_msg_map[key].ContentsAsXml('', True) != |
| 6322 | new_id_to_msg_map[key].ContentsAsXml('', True)): |
| 6323 | # The message content itself changed. Require an updated screenshot. |
| 6324 | modified_ids.add(key) |
| 6325 | elif old_id_to_msg_map[key].attrs['meaning'] != \ |
| 6326 | new_id_to_msg_map[key].attrs['meaning']: |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6327 | # The message meaning changed. We later check for a screenshot. |
| 6328 | modified_ids.add(key) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6329 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6330 | if run_screenshot_check: |
| 6331 | # Check the screenshot directory for .png files. Warn if there is any. |
| 6332 | for png_path in affected_png_paths: |
| 6333 | if png_path.startswith(screenshots_dir): |
| 6334 | unnecessary_screenshots.append(png_path) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6335 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6336 | for added_id in added_ids: |
| 6337 | _CheckScreenshotAdded(screenshots_dir, added_id) |
Rainhard Findling | d8d0437 | 2020-08-13 13:30:09 | [diff] [blame] | 6338 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6339 | for modified_id in modified_ids: |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6340 | _CheckScreenshotModified(screenshots_dir, modified_id) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6341 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6342 | for removed_id in removed_ids: |
| 6343 | _CheckScreenshotRemoved(screenshots_dir, removed_id) |
| 6344 | |
| 6345 | # Check new and changed strings for ICU syntax errors. |
| 6346 | for key in added_ids.union(modified_ids): |
| 6347 | msg = new_id_to_msg_map[key].ContentsAsXml('', True) |
| 6348 | err = _ValidateIcuSyntax(msg, 0, []) |
| 6349 | if err is not None: |
| 6350 | icu_syntax_errors.append(str(key) + ': ' + str(err[0])) |
| 6351 | |
| 6352 | results = [] |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6353 | if run_screenshot_check: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6354 | if unnecessary_screenshots: |
| 6355 | results.append( |
| 6356 | output_api.PresubmitError( |
| 6357 | 'Do not include actual screenshots in the changelist. Run ' |
| 6358 | 'tools/translate/upload_screenshots.py to upload them instead:', |
| 6359 | sorted(unnecessary_screenshots))) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6360 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6361 | if missing_sha1: |
| 6362 | results.append( |
| 6363 | output_api.PresubmitError( |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6364 | 'You are adding UI strings.\n' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6365 | 'To ensure the best translations, take screenshots of the relevant UI ' |
| 6366 | '(https://g.co/chrome/translation) and add these files to your ' |
| 6367 | 'changelist:', sorted(missing_sha1))) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6368 | |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6369 | if invalid_sha1: |
| 6370 | results.append( |
| 6371 | output_api.PresubmitError( |
| 6372 | 'The following files do not seem to contain valid sha1 hashes. ' |
| 6373 | 'Make sure they contain hashes created by ' |
| 6374 | 'tools/translate/upload_screenshots.py:', sorted(invalid_sha1))) |
| 6375 | |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6376 | if missing_sha1_modified: |
| 6377 | results.append( |
| 6378 | output_api.PresubmitError( |
| 6379 | 'You are modifying UI strings or their meanings.\n' |
| 6380 | 'To ensure the best translations, take screenshots of the relevant UI ' |
| 6381 | '(https://g.co/chrome/translation) and add these files to your ' |
| 6382 | 'changelist:', sorted(missing_sha1_modified))) |
| 6383 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6384 | if unnecessary_sha1_files: |
| 6385 | results.append( |
| 6386 | output_api.PresubmitError( |
| 6387 | 'You removed strings associated with these files. Remove:', |
| 6388 | sorted(unnecessary_sha1_files))) |
| 6389 | else: |
| 6390 | results.append( |
| 6391 | output_api.PresubmitPromptOrNotify('Skipping translation ' |
| 6392 | 'screenshots check.')) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6393 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6394 | if icu_syntax_errors: |
| 6395 | results.append( |
| 6396 | output_api.PresubmitPromptWarning( |
| 6397 | 'ICU syntax errors were found in the following strings (problems or ' |
| 6398 | 'feedback? Contact rainhard@chromium.org):', |
| 6399 | items=icu_syntax_errors)) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6400 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6401 | return results |
Mustafa Emre Acer | 51f2f74 | 2020-03-09 19:41:12 | [diff] [blame] | 6402 | |
| 6403 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6404 | def CheckTranslationExpectations(input_api, output_api, |
Mustafa Emre Acer | 51f2f74 | 2020-03-09 19:41:12 | [diff] [blame] | 6405 | repo_root=None, |
| 6406 | translation_expectations_path=None, |
| 6407 | grd_files=None): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6408 | import sys |
| 6409 | affected_grds = [ |
| 6410 | f for f in input_api.AffectedFiles() |
| 6411 | if (f.LocalPath().endswith('.grd') or f.LocalPath().endswith('.grdp')) |
| 6412 | ] |
| 6413 | if not affected_grds: |
| 6414 | return [] |
| 6415 | |
| 6416 | try: |
| 6417 | old_sys_path = sys.path |
| 6418 | sys.path = sys.path + [ |
| 6419 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 6420 | 'translation') |
| 6421 | ] |
| 6422 | from helper import git_helper |
| 6423 | from helper import translation_helper |
| 6424 | finally: |
| 6425 | sys.path = old_sys_path |
| 6426 | |
| 6427 | # Check that translation expectations can be parsed and we can get a list of |
| 6428 | # translatable grd files. |repo_root| and |translation_expectations_path| are |
| 6429 | # only passed by tests. |
| 6430 | if not repo_root: |
| 6431 | repo_root = input_api.PresubmitLocalPath() |
| 6432 | if not translation_expectations_path: |
| 6433 | translation_expectations_path = input_api.os_path.join( |
| 6434 | repo_root, 'tools', 'gritsettings', 'translation_expectations.pyl') |
| 6435 | if not grd_files: |
| 6436 | grd_files = git_helper.list_grds_in_repository(repo_root) |
| 6437 | |
| 6438 | # Ignore bogus grd files used only for testing |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 6439 | # ui/webui/resources/tools/generate_grd.py. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6440 | ignore_path = input_api.os_path.join('ui', 'webui', 'resources', 'tools', |
| 6441 | 'tests') |
| 6442 | grd_files = [p for p in grd_files if ignore_path not in p] |
| 6443 | |
| 6444 | try: |
| 6445 | translation_helper.get_translatable_grds( |
| 6446 | repo_root, grd_files, translation_expectations_path) |
| 6447 | except Exception as e: |
| 6448 | return [ |
| 6449 | output_api.PresubmitNotifyResult( |
| 6450 | 'Failed to get a list of translatable grd files. This happens when:\n' |
| 6451 | ' - One of the modified grd or grdp files cannot be parsed or\n' |
| 6452 | ' - %s is not updated.\n' |
| 6453 | 'Stack:\n%s' % (translation_expectations_path, str(e))) |
| 6454 | ] |
Mustafa Emre Acer | 51f2f74 | 2020-03-09 19:41:12 | [diff] [blame] | 6455 | return [] |
| 6456 | |
Ken Rockot | c31f483 | 2020-05-29 18:58:51 | [diff] [blame] | 6457 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6458 | def CheckStableMojomChanges(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6459 | """Changes to [Stable] mojom types must preserve backward-compatibility.""" |
| 6460 | changed_mojoms = input_api.AffectedFiles( |
| 6461 | include_deletes=True, |
| 6462 | file_filter=lambda f: f.LocalPath().endswith(('.mojom'))) |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 6463 | |
Bruce Dawson | 344ab26 | 2022-06-04 11:35:10 | [diff] [blame] | 6464 | if not changed_mojoms or input_api.no_diffs: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6465 | return [] |
| 6466 | |
| 6467 | delta = [] |
| 6468 | for mojom in changed_mojoms: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6469 | delta.append({ |
| 6470 | 'filename': mojom.LocalPath(), |
| 6471 | 'old': '\n'.join(mojom.OldContents()) or None, |
| 6472 | 'new': '\n'.join(mojom.NewContents()) or None, |
| 6473 | }) |
| 6474 | |
| 6475 | process = input_api.subprocess.Popen([ |
Takuto Ikuta | dca1022 | 2022-04-13 02:51:21 | [diff] [blame] | 6476 | input_api.python3_executable, |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6477 | input_api.os_path.join( |
| 6478 | input_api.PresubmitLocalPath(), 'mojo', 'public', 'tools', 'mojom', |
| 6479 | 'check_stable_mojom_compatibility.py'), '--src-root', |
| 6480 | input_api.PresubmitLocalPath() |
| 6481 | ], |
| 6482 | stdin=input_api.subprocess.PIPE, |
| 6483 | stdout=input_api.subprocess.PIPE, |
| 6484 | stderr=input_api.subprocess.PIPE, |
| 6485 | universal_newlines=True) |
| 6486 | (x, error) = process.communicate(input=input_api.json.dumps(delta)) |
| 6487 | if process.returncode: |
| 6488 | return [ |
| 6489 | output_api.PresubmitError( |
| 6490 | 'One or more [Stable] mojom definitions appears to have been changed ' |
| 6491 | 'in a way that is not backward-compatible.', |
| 6492 | long_text=error) |
| 6493 | ] |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 6494 | return [] |
| 6495 | |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6496 | def CheckDeprecationOfPreferences(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6497 | """Removing a preference should come with a deprecation.""" |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6498 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6499 | def FilterFile(affected_file): |
| 6500 | """Accept only .cc files and the like.""" |
| 6501 | file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS] |
| 6502 | files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 6503 | input_api.DEFAULT_FILES_TO_SKIP) |
| 6504 | return input_api.FilterSourceFile( |
| 6505 | affected_file, |
| 6506 | files_to_check=file_inclusion_pattern, |
| 6507 | files_to_skip=files_to_skip) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6508 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6509 | def ModifiedLines(affected_file): |
| 6510 | """Returns a list of tuples (line number, line text) of added and removed |
| 6511 | lines. |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6512 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6513 | Deleted lines share the same line number as the previous line. |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6514 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6515 | This relies on the scm diff output describing each changed code section |
| 6516 | with a line of the form |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6517 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6518 | ^@@ <old line num>,<old size> <new line num>,<new size> @@$ |
| 6519 | """ |
| 6520 | line_num = 0 |
| 6521 | modified_lines = [] |
| 6522 | for line in affected_file.GenerateScmDiff().splitlines(): |
| 6523 | # Extract <new line num> of the patch fragment (see format above). |
| 6524 | m = input_api.re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', |
| 6525 | line) |
| 6526 | if m: |
| 6527 | line_num = int(m.groups(1)[0]) |
| 6528 | continue |
| 6529 | if ((line.startswith('+') and not line.startswith('++')) |
| 6530 | or (line.startswith('-') and not line.startswith('--'))): |
| 6531 | modified_lines.append((line_num, line)) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6532 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6533 | if not line.startswith('-'): |
| 6534 | line_num += 1 |
| 6535 | return modified_lines |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6536 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6537 | def FindLineWith(lines, needle): |
| 6538 | """Returns the line number (i.e. index + 1) in `lines` containing `needle`. |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6539 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6540 | If 0 or >1 lines contain `needle`, -1 is returned. |
| 6541 | """ |
| 6542 | matching_line_numbers = [ |
| 6543 | # + 1 for 1-based counting of line numbers. |
| 6544 | i + 1 for i, line in enumerate(lines) if needle in line |
| 6545 | ] |
| 6546 | return matching_line_numbers[0] if len( |
| 6547 | matching_line_numbers) == 1 else -1 |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6548 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6549 | def ModifiedPrefMigration(affected_file): |
| 6550 | """Returns whether the MigrateObsolete.*Pref functions were modified.""" |
| 6551 | # Determine first and last lines of MigrateObsolete.*Pref functions. |
| 6552 | new_contents = affected_file.NewContents() |
| 6553 | range_1 = (FindLineWith(new_contents, |
| 6554 | 'BEGIN_MIGRATE_OBSOLETE_LOCAL_STATE_PREFS'), |
| 6555 | FindLineWith(new_contents, |
| 6556 | 'END_MIGRATE_OBSOLETE_LOCAL_STATE_PREFS')) |
| 6557 | range_2 = (FindLineWith(new_contents, |
| 6558 | 'BEGIN_MIGRATE_OBSOLETE_PROFILE_PREFS'), |
| 6559 | FindLineWith(new_contents, |
| 6560 | 'END_MIGRATE_OBSOLETE_PROFILE_PREFS')) |
| 6561 | if (-1 in range_1 + range_2): |
| 6562 | raise Exception( |
| 6563 | 'Broken .*MIGRATE_OBSOLETE_.*_PREFS markers in browser_prefs.cc.' |
| 6564 | ) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6565 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6566 | # Check whether any of the modified lines are part of the |
| 6567 | # MigrateObsolete.*Pref functions. |
| 6568 | for line_nr, line in ModifiedLines(affected_file): |
| 6569 | if (range_1[0] <= line_nr <= range_1[1] |
| 6570 | or range_2[0] <= line_nr <= range_2[1]): |
| 6571 | return True |
| 6572 | return False |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6573 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6574 | register_pref_pattern = input_api.re.compile(r'Register.+Pref') |
| 6575 | browser_prefs_file_pattern = input_api.re.compile( |
| 6576 | r'chrome/browser/prefs/browser_prefs.cc') |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6577 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6578 | changes = input_api.AffectedFiles(include_deletes=True, |
| 6579 | file_filter=FilterFile) |
| 6580 | potential_problems = [] |
| 6581 | for f in changes: |
| 6582 | for line in f.GenerateScmDiff().splitlines(): |
| 6583 | # Check deleted lines for pref registrations. |
| 6584 | if (line.startswith('-') and not line.startswith('--') |
| 6585 | and register_pref_pattern.search(line)): |
| 6586 | potential_problems.append('%s: %s' % (f.LocalPath(), line)) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6587 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6588 | if browser_prefs_file_pattern.search(f.LocalPath()): |
| 6589 | # If the developer modified the MigrateObsolete.*Prefs() functions, we |
| 6590 | # assume that they knew that they have to deprecate preferences and don't |
| 6591 | # warn. |
| 6592 | try: |
| 6593 | if ModifiedPrefMigration(f): |
| 6594 | return [] |
| 6595 | except Exception as e: |
| 6596 | return [output_api.PresubmitError(str(e))] |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 6597 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6598 | if potential_problems: |
| 6599 | return [ |
| 6600 | output_api.PresubmitPromptWarning( |
| 6601 | 'Discovered possible removal of preference registrations.\n\n' |
| 6602 | 'Please make sure to properly deprecate preferences by clearing their\n' |
| 6603 | 'value for a couple of milestones before finally removing the code.\n' |
| 6604 | 'Otherwise data may stay in the preferences files forever. See\n' |
| 6605 | 'Migrate*Prefs() in chrome/browser/prefs/browser_prefs.cc and\n' |
| 6606 | 'chrome/browser/prefs/README.md for examples.\n' |
| 6607 | 'This may be a false positive warning (e.g. if you move preference\n' |
| 6608 | 'registrations to a different place).\n', potential_problems) |
| 6609 | ] |
| 6610 | return [] |
| 6611 | |
Matt Stark | 6ef0887 | 2021-07-29 01:21:46 | [diff] [blame] | 6612 | |
| 6613 | def CheckConsistentGrdChanges(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6614 | """Changes to GRD files must be consistent for tools to read them.""" |
| 6615 | changed_grds = input_api.AffectedFiles( |
| 6616 | include_deletes=False, |
| 6617 | file_filter=lambda f: f.LocalPath().endswith(('.grd'))) |
| 6618 | errors = [] |
| 6619 | invalid_file_regexes = [(input_api.re.compile(matcher), msg) |
| 6620 | for matcher, msg in _INVALID_GRD_FILE_LINE] |
| 6621 | for grd in changed_grds: |
| 6622 | for i, line in enumerate(grd.NewContents()): |
| 6623 | for matcher, msg in invalid_file_regexes: |
| 6624 | if matcher.search(line): |
| 6625 | errors.append( |
| 6626 | output_api.PresubmitError( |
| 6627 | 'Problem on {grd}:{i} - {msg}'.format( |
| 6628 | grd=grd.LocalPath(), i=i + 1, msg=msg))) |
| 6629 | return errors |
| 6630 | |
Kevin McNee | 967dd2d2 | 2021-11-15 16:09:29 | [diff] [blame] | 6631 | |
Henrique Ferreiro | 2a4b5594 | 2021-11-29 23:45:36 | [diff] [blame] | 6632 | def CheckAssertAshOnlyCode(input_api, output_api): |
| 6633 | """Errors if a BUILD.gn file in an ash/ directory doesn't include |
| 6634 | assert(is_chromeos_ash). |
| 6635 | """ |
| 6636 | |
| 6637 | def FileFilter(affected_file): |
| 6638 | """Includes directories known to be Ash only.""" |
| 6639 | return input_api.FilterSourceFile( |
| 6640 | affected_file, |
| 6641 | files_to_check=( |
| 6642 | r'^ash/.*BUILD\.gn', # Top-level src/ash/. |
| 6643 | r'.*/ash/.*BUILD\.gn'), # Any path component. |
| 6644 | files_to_skip=(input_api.DEFAULT_FILES_TO_SKIP)) |
| 6645 | |
| 6646 | errors = [] |
| 6647 | pattern = input_api.re.compile(r'assert\(is_chromeos_ash') |
Jameson Thies | 0ce669f | 2021-12-09 15:56:56 | [diff] [blame] | 6648 | for f in input_api.AffectedFiles(include_deletes=False, |
| 6649 | file_filter=FileFilter): |
Henrique Ferreiro | 2a4b5594 | 2021-11-29 23:45:36 | [diff] [blame] | 6650 | if (not pattern.search(input_api.ReadFile(f))): |
| 6651 | errors.append( |
| 6652 | output_api.PresubmitError( |
| 6653 | 'Please add assert(is_chromeos_ash) to %s. If that\'s not ' |
| 6654 | 'possible, please create and issue and add a comment such ' |
| 6655 | 'as:\n # TODO(https://crbug.com/XXX): add ' |
| 6656 | 'assert(is_chromeos_ash) when ...' % f.LocalPath())) |
| 6657 | return errors |
Lukasz Anforowicz | 7016d05e | 2021-11-30 03:56:27 | [diff] [blame] | 6658 | |
| 6659 | |
| 6660 | def _IsRendererOnlyCppFile(input_api, affected_file): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6661 | path = affected_file.LocalPath() |
| 6662 | if not _IsCPlusPlusFile(input_api, path): |
| 6663 | return False |
| 6664 | |
| 6665 | # Any code under a "renderer" subdirectory is assumed to be Renderer-only. |
| 6666 | if "/renderer/" in path: |
| 6667 | return True |
| 6668 | |
| 6669 | # Blink's public/web API is only used/included by Renderer-only code. Note |
| 6670 | # that public/platform API may be used in non-Renderer processes (e.g. there |
| 6671 | # are some includes in code used by Utility, PDF, or Plugin processes). |
| 6672 | if "/blink/public/web/" in path: |
| 6673 | return True |
| 6674 | |
| 6675 | # We assume that everything else may be used outside of Renderer processes. |
Lukasz Anforowicz | 7016d05e | 2021-11-30 03:56:27 | [diff] [blame] | 6676 | return False |
| 6677 | |
Lukasz Anforowicz | 7016d05e | 2021-11-30 03:56:27 | [diff] [blame] | 6678 | # TODO(https://crbug.com/1273182): Remove these checks, once they are replaced |
| 6679 | # by the Chromium Clang Plugin (which will be preferable because it will |
| 6680 | # 1) report errors earlier - at compile-time and 2) cover more rules). |
| 6681 | def CheckRawPtrUsage(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6682 | """Rough checks that raw_ptr<T> usage guidelines are followed.""" |
| 6683 | errors = [] |
| 6684 | # The regex below matches "raw_ptr<" following a word boundary, but not in a |
| 6685 | # C++ comment. |
| 6686 | raw_ptr_matcher = input_api.re.compile(r'^((?!//).)*\braw_ptr<') |
| 6687 | file_filter = lambda f: _IsRendererOnlyCppFile(input_api, f) |
| 6688 | for f, line_num, line in input_api.RightHandSideLines(file_filter): |
| 6689 | if raw_ptr_matcher.search(line): |
| 6690 | errors.append( |
| 6691 | output_api.PresubmitError( |
| 6692 | 'Problem on {path}:{line} - '\ |
| 6693 | 'raw_ptr<T> should not be used in Renderer-only code '\ |
| 6694 | '(as documented in the "Pointers to unprotected memory" '\ |
| 6695 | 'section in //base/memory/raw_ptr.md)'.format( |
| 6696 | path=f.LocalPath(), line=line_num))) |
| 6697 | return errors |
Henrique Ferreiro | f9819f2e3 | 2021-11-30 13:31:56 | [diff] [blame] | 6698 | |
| 6699 | |
| 6700 | def CheckPythonShebang(input_api, output_api): |
| 6701 | """Checks that python scripts use #!/usr/bin/env instead of hardcoding a |
| 6702 | system-wide python. |
| 6703 | """ |
| 6704 | errors = [] |
| 6705 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 6706 | affected_file, |
| 6707 | files_to_skip=((_THIRD_PARTY_EXCEPT_BLINK, |
| 6708 | r'third_party/blink/web_tests/external/') + input_api. |
| 6709 | DEFAULT_FILES_TO_SKIP), |
| 6710 | files_to_check=[r'.*\.py$']) |
| 6711 | for f in input_api.AffectedSourceFiles(sources): |
Takuto Ikuta | 3697651 | 2021-11-30 23:15:27 | [diff] [blame] | 6712 | for line_num, line in f.ChangedContents(): |
| 6713 | if line_num == 1 and line.startswith('#!/usr/bin/python'): |
| 6714 | errors.append(f.LocalPath()) |
| 6715 | break |
Henrique Ferreiro | f9819f2e3 | 2021-11-30 13:31:56 | [diff] [blame] | 6716 | |
| 6717 | result = [] |
| 6718 | for file in errors: |
| 6719 | result.append( |
| 6720 | output_api.PresubmitError( |
| 6721 | "Please use '#!/usr/bin/env python/2/3' as the shebang of %s" % |
| 6722 | file)) |
| 6723 | return result |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 6724 | |
| 6725 | |
| 6726 | def CheckBatchAnnotation(input_api, output_api): |
| 6727 | """Checks that tests have either @Batch or @DoNotBatch annotation. If this |
| 6728 | is not an instrumentation test, disregard.""" |
| 6729 | |
| 6730 | batch_annotation = input_api.re.compile(r'^\s*@Batch') |
| 6731 | do_not_batch_annotation = input_api.re.compile(r'^\s*@DoNotBatch') |
| 6732 | robolectric_test = input_api.re.compile(r'[rR]obolectric') |
| 6733 | test_class_declaration = input_api.re.compile(r'^\s*public\sclass.*Test') |
| 6734 | uiautomator_test = input_api.re.compile(r'[uU]i[aA]utomator') |
| 6735 | |
ckitagawa | e8fd23b | 2022-06-17 15:29:38 | [diff] [blame] | 6736 | missing_annotation_errors = [] |
| 6737 | extra_annotation_errors = [] |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 6738 | |
| 6739 | def _FilterFile(affected_file): |
| 6740 | return input_api.FilterSourceFile( |
| 6741 | affected_file, |
| 6742 | files_to_skip=input_api.DEFAULT_FILES_TO_SKIP, |
| 6743 | files_to_check=[r'.*Test\.java$']) |
| 6744 | |
| 6745 | for f in input_api.AffectedSourceFiles(_FilterFile): |
| 6746 | batch_matched = None |
| 6747 | do_not_batch_matched = None |
| 6748 | is_instrumentation_test = True |
| 6749 | for line in f.NewContents(): |
| 6750 | if robolectric_test.search(line) or uiautomator_test.search(line): |
| 6751 | # Skip Robolectric and UiAutomator tests. |
| 6752 | is_instrumentation_test = False |
| 6753 | break |
| 6754 | if not batch_matched: |
| 6755 | batch_matched = batch_annotation.search(line) |
| 6756 | if not do_not_batch_matched: |
| 6757 | do_not_batch_matched = do_not_batch_annotation.search(line) |
| 6758 | test_class_declaration_matched = test_class_declaration.search( |
| 6759 | line) |
| 6760 | if test_class_declaration_matched: |
| 6761 | break |
| 6762 | if (is_instrumentation_test and |
| 6763 | not batch_matched and |
| 6764 | not do_not_batch_matched): |
Sam Maier | 4cef924 | 2022-10-03 14:21:24 | [diff] [blame] | 6765 | missing_annotation_errors.append(str(f.LocalPath())) |
ckitagawa | e8fd23b | 2022-06-17 15:29:38 | [diff] [blame] | 6766 | if (not is_instrumentation_test and |
| 6767 | (batch_matched or |
| 6768 | do_not_batch_matched)): |
Sam Maier | 4cef924 | 2022-10-03 14:21:24 | [diff] [blame] | 6769 | extra_annotation_errors.append(str(f.LocalPath())) |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 6770 | |
| 6771 | results = [] |
| 6772 | |
ckitagawa | e8fd23b | 2022-06-17 15:29:38 | [diff] [blame] | 6773 | if missing_annotation_errors: |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 6774 | results.append( |
| 6775 | output_api.PresubmitPromptWarning( |
| 6776 | """ |
Henrique Nakashima | cb4c55a | 2023-01-30 20:09:09 | [diff] [blame] | 6777 | Instrumentation tests should use either @Batch or @DoNotBatch. Use |
| 6778 | @Batch(Batch.PER_CLASS) in most cases. Use @Batch(Batch.UNIT_TESTS) when tests |
| 6779 | have no side-effects. If the tests are not safe to run in batch, please use |
| 6780 | @DoNotBatch with reasons. |
Jens Mueller | 2085ff8 | 2023-02-27 11:54:49 | [diff] [blame] | 6781 | See https://source.chromium.org/chromium/chromium/src/+/main:docs/testing/batching_instrumentation_tests.md |
ckitagawa | e8fd23b | 2022-06-17 15:29:38 | [diff] [blame] | 6782 | """, missing_annotation_errors)) |
| 6783 | if extra_annotation_errors: |
| 6784 | results.append( |
| 6785 | output_api.PresubmitPromptWarning( |
| 6786 | """ |
| 6787 | Robolectric tests do not need a @Batch or @DoNotBatch annotations. |
| 6788 | """, extra_annotation_errors)) |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 6789 | |
| 6790 | return results |
Sam Maier | 4cef924 | 2022-10-03 14:21:24 | [diff] [blame] | 6791 | |
| 6792 | |
| 6793 | def CheckMockAnnotation(input_api, output_api): |
| 6794 | """Checks that we have annotated all Mockito.mock()-ed or Mockito.spy()-ed |
| 6795 | classes with @Mock or @Spy. If this is not an instrumentation test, |
| 6796 | disregard.""" |
| 6797 | |
| 6798 | # This is just trying to be approximately correct. We are not writing a |
| 6799 | # Java parser, so special cases like statically importing mock() then |
| 6800 | # calling an unrelated non-mockito spy() function will cause a false |
| 6801 | # positive. |
| 6802 | package_name = input_api.re.compile(r'^package\s+(\w+(?:\.\w+)+);') |
| 6803 | mock_static_import = input_api.re.compile( |
| 6804 | r'^import\s+static\s+org.mockito.Mockito.(?:mock|spy);') |
| 6805 | import_class = input_api.re.compile(r'import\s+((?:\w+\.)+)(\w+);') |
| 6806 | mock_annotation = input_api.re.compile(r'^\s*@(?:Mock|Spy)') |
| 6807 | field_type = input_api.re.compile(r'(\w+)(?:<\w+>)?\s+\w+\s*(?:;|=)') |
| 6808 | mock_or_spy_function_call = r'(?:mock|spy)\(\s*(?:new\s*)?(\w+)(?:\.class|\()' |
| 6809 | fully_qualified_mock_function = input_api.re.compile( |
| 6810 | r'Mockito\.' + mock_or_spy_function_call) |
| 6811 | statically_imported_mock_function = input_api.re.compile( |
| 6812 | r'\W' + mock_or_spy_function_call) |
| 6813 | robolectric_test = input_api.re.compile(r'[rR]obolectric') |
| 6814 | uiautomator_test = input_api.re.compile(r'[uU]i[aA]utomator') |
| 6815 | |
| 6816 | def _DoClassLookup(class_name, class_name_map, package): |
| 6817 | found = class_name_map.get(class_name) |
| 6818 | if found is not None: |
| 6819 | return found |
| 6820 | else: |
| 6821 | return package + '.' + class_name |
| 6822 | |
| 6823 | def _FilterFile(affected_file): |
| 6824 | return input_api.FilterSourceFile( |
| 6825 | affected_file, |
| 6826 | files_to_skip=input_api.DEFAULT_FILES_TO_SKIP, |
| 6827 | files_to_check=[r'.*Test\.java$']) |
| 6828 | |
| 6829 | mocked_by_function_classes = set() |
| 6830 | mocked_by_annotation_classes = set() |
| 6831 | class_to_filename = {} |
| 6832 | for f in input_api.AffectedSourceFiles(_FilterFile): |
| 6833 | mock_function_regex = fully_qualified_mock_function |
| 6834 | next_line_is_annotated = False |
| 6835 | fully_qualified_class_map = {} |
| 6836 | package = None |
| 6837 | |
| 6838 | for line in f.NewContents(): |
| 6839 | if robolectric_test.search(line) or uiautomator_test.search(line): |
| 6840 | # Skip Robolectric and UiAutomator tests. |
| 6841 | break |
| 6842 | |
| 6843 | m = package_name.search(line) |
| 6844 | if m: |
| 6845 | package = m.group(1) |
| 6846 | continue |
| 6847 | |
| 6848 | if mock_static_import.search(line): |
| 6849 | mock_function_regex = statically_imported_mock_function |
| 6850 | continue |
| 6851 | |
| 6852 | m = import_class.search(line) |
| 6853 | if m: |
| 6854 | fully_qualified_class_map[m.group(2)] = m.group(1) + m.group(2) |
| 6855 | continue |
| 6856 | |
| 6857 | if next_line_is_annotated: |
| 6858 | next_line_is_annotated = False |
| 6859 | fully_qualified_class = _DoClassLookup( |
| 6860 | field_type.search(line).group(1), fully_qualified_class_map, |
| 6861 | package) |
| 6862 | mocked_by_annotation_classes.add(fully_qualified_class) |
| 6863 | continue |
| 6864 | |
| 6865 | if mock_annotation.search(line): |
| 6866 | next_line_is_annotated = True |
| 6867 | continue |
| 6868 | |
| 6869 | m = mock_function_regex.search(line) |
| 6870 | if m: |
| 6871 | fully_qualified_class = _DoClassLookup(m.group(1), |
| 6872 | fully_qualified_class_map, package) |
| 6873 | # Skipping builtin classes, since they don't get optimized. |
| 6874 | if fully_qualified_class.startswith( |
| 6875 | 'android.') or fully_qualified_class.startswith( |
| 6876 | 'java.'): |
| 6877 | continue |
| 6878 | class_to_filename[fully_qualified_class] = str(f.LocalPath()) |
| 6879 | mocked_by_function_classes.add(fully_qualified_class) |
| 6880 | |
| 6881 | results = [] |
| 6882 | missed_classes = mocked_by_function_classes - mocked_by_annotation_classes |
| 6883 | if missed_classes: |
| 6884 | error_locations = [] |
| 6885 | for c in missed_classes: |
| 6886 | error_locations.append(c + ' in ' + class_to_filename[c]) |
| 6887 | results.append( |
| 6888 | output_api.PresubmitPromptWarning( |
| 6889 | """ |
| 6890 | Mockito.mock()/spy() cause issues with our Java optimizer. You have 3 options: |
| 6891 | 1) If the mocked variable can be a class member, annotate the member with |
| 6892 | @Mock/@Spy. |
| 6893 | 2) If the mocked variable cannot be a class member, create a dummy member |
| 6894 | variable of that type, annotated with @Mock/@Spy. This dummy does not need |
| 6895 | to be used or initialized in any way. |
| 6896 | 3) If the mocked type is definitely not going to be optimized, whether it's a |
| 6897 | builtin type which we don't ship, or a class you know R8 will treat |
| 6898 | specially, you can ignore this warning. |
| 6899 | """, error_locations)) |
| 6900 | |
| 6901 | return results |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 6902 | |
| 6903 | def CheckNoJsInIos(input_api, output_api): |
| 6904 | """Checks to make sure that JavaScript files are not used on iOS.""" |
| 6905 | |
| 6906 | def _FilterFile(affected_file): |
| 6907 | return input_api.FilterSourceFile( |
| 6908 | affected_file, |
| 6909 | files_to_skip=input_api.DEFAULT_FILES_TO_SKIP + |
Mike Dougherty | 7bc8a81 | 2023-05-02 06:55:21 | [diff] [blame] | 6910 | (r'^ios/third_party/*', r'^ios/tools/*', r'^third_party/*'), |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 6911 | files_to_check=[r'^ios/.*\.js$', r'.*/ios/.*\.js$']) |
| 6912 | |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 6913 | deleted_files = [] |
| 6914 | |
| 6915 | # Collect filenames of all removed JS files. |
| 6916 | for f in input_api.AffectedSourceFiles(_FilterFile): |
| 6917 | local_path = f.LocalPath() |
| 6918 | |
| 6919 | if input_api.os_path.splitext(local_path)[1] == '.js' and f.Action() == 'D': |
| 6920 | deleted_files.append(input_api.os_path.basename(local_path)) |
| 6921 | |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 6922 | error_paths = [] |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 6923 | moved_paths = [] |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 6924 | warning_paths = [] |
| 6925 | |
| 6926 | for f in input_api.AffectedSourceFiles(_FilterFile): |
| 6927 | local_path = f.LocalPath() |
| 6928 | |
| 6929 | if input_api.os_path.splitext(local_path)[1] == '.js': |
| 6930 | if f.Action() == 'A': |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 6931 | if input_api.os_path.basename(local_path) in deleted_files: |
| 6932 | # This script was probably moved rather than newly created. |
| 6933 | # Present a warning instead of an error for these cases. |
| 6934 | moved_paths.append(local_path) |
| 6935 | else: |
| 6936 | error_paths.append(local_path) |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 6937 | elif f.Action() != 'D': |
| 6938 | warning_paths.append(local_path) |
| 6939 | |
| 6940 | results = [] |
| 6941 | |
| 6942 | if warning_paths: |
| 6943 | results.append(output_api.PresubmitPromptWarning( |
| 6944 | 'TypeScript is now fully supported for iOS feature scripts. ' |
| 6945 | 'Consider converting JavaScript files to TypeScript. See ' |
| 6946 | '//ios/web/public/js_messaging/README.md for more details.', |
| 6947 | warning_paths)) |
| 6948 | |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 6949 | if moved_paths: |
| 6950 | results.append(output_api.PresubmitPromptWarning( |
| 6951 | 'Do not use JavaScript on iOS for new files as TypeScript is ' |
| 6952 | 'fully supported. (If this is a moved file, you may leave the ' |
| 6953 | 'script unconverted.) See //ios/web/public/js_messaging/README.md ' |
| 6954 | 'for help using scripts on iOS.', moved_paths)) |
| 6955 | |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 6956 | if error_paths: |
| 6957 | results.append(output_api.PresubmitError( |
| 6958 | 'Do not use JavaScript on iOS as TypeScript is fully supported. ' |
| 6959 | 'See //ios/web/public/js_messaging/README.md for help using ' |
| 6960 | 'scripts on iOS.', error_paths)) |
| 6961 | |
| 6962 | return results |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 6963 | |
| 6964 | def CheckLibcxxRevisionsMatch(input_api, output_api): |
| 6965 | """Check to make sure the libc++ version matches across deps files.""" |
Andrew Grieve | 21bb679 | 2023-03-27 19:06:48 | [diff] [blame] | 6966 | # Disable check for changes to sub-repositories. |
| 6967 | if input_api.PresubmitLocalPath() != input_api.change.RepositoryRoot(): |
| 6968 | return [] |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 6969 | |
| 6970 | DEPS_FILES = [ 'DEPS', 'buildtools/deps_revisions.gni' ] |
| 6971 | |
| 6972 | file_filter = lambda f: f.LocalPath().replace( |
| 6973 | input_api.os_path.sep, '/') in DEPS_FILES |
| 6974 | changed_deps_files = input_api.AffectedFiles(file_filter=file_filter) |
| 6975 | if not changed_deps_files: |
| 6976 | return [] |
| 6977 | |
| 6978 | def LibcxxRevision(file): |
| 6979 | file = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 6980 | *file.split('/')) |
| 6981 | return input_api.re.search( |
| 6982 | r'libcxx_revision.*[:=].*[\'"](\w+)[\'"]', |
| 6983 | input_api.ReadFile(file)).group(1) |
| 6984 | |
| 6985 | if len(set([LibcxxRevision(f) for f in DEPS_FILES])) == 1: |
| 6986 | return [] |
| 6987 | |
| 6988 | return [output_api.PresubmitError( |
| 6989 | 'libcxx_revision not equal across %s' % ', '.join(DEPS_FILES), |
| 6990 | changed_deps_files)] |