Avi Drissman | 2497659 | 2022-09-12 15:24:31 | [diff] [blame] | 1 | # Copyright 2012 The Chromium Authors |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Top-level presubmit script for Chromium. |
| 6 | |
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. |
[email protected] | ca8d198 | 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 |
mikt | 19226ff2 | 2024-08-27 05:28:21 | [diff] [blame] | 14 | from typing import Tuple |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 15 | from dataclasses import dataclass |
| 16 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 17 | PRESUBMIT_VERSION = '2.0.0' |
[email protected] | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 18 | |
Dirk Pranke | e3c9c62d | 2021-05-18 18:35:59 | [diff] [blame] | 19 | |
[email protected] | 379e7dd | 2010-01-28 17:39:21 | [diff] [blame] | 20 | _EXCLUDED_PATHS = ( |
Bruce Dawson | 7f8566b | 2022-05-06 16:22:18 | [diff] [blame] | 21 | # Generated file |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 22 | (r"chrome/android/webapk/shell_apk/src/org/chromium" |
| 23 | r"/webapk/lib/runtime_library/IWebApkApi.java"), |
Mila Green | e3aa722 | 2021-09-07 16:34:08 | [diff] [blame] | 24 | # File needs to write to stdout to emulate a tool it's replacing. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 25 | r"chrome/updater/mac/keystone/ksadmin.mm", |
Ilya Sherman | e8a7d2d | 2020-07-25 04:33:47 | [diff] [blame] | 26 | # Generated file. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 27 | (r"^components/variations/proto/devtools/" |
Ilya Sherman | c167a96 | 2020-08-18 18:40:26 | [diff] [blame] | 28 | r"client_variations.js"), |
Bruce Dawson | 3bd976c | 2022-05-06 22:47:52 | [diff] [blame] | 29 | # These are video files, not typescript. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 30 | r"^media/test/data/.*.ts", |
| 31 | r"^native_client_sdksrc/build_tools/make_rules.py", |
| 32 | r"^native_client_sdk/src/build_tools/make_simple.py", |
| 33 | r"^native_client_sdk/src/tools/.*.mk", |
| 34 | r"^net/tools/spdyshark/.*", |
| 35 | r"^skia/.*", |
| 36 | r"^third_party/blink/.*", |
| 37 | r"^third_party/breakpad/.*", |
Darwin Huang | d74a9d3 | 2019-07-17 17:58:46 | [diff] [blame] | 38 | # sqlite is an imported third party dependency. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 39 | r"^third_party/sqlite/.*", |
| 40 | r"^v8/.*", |
[email protected] | 3e4eb11 | 2011-01-18 03:29:54 | [diff] [blame] | 41 | r".*MakeFile$", |
[email protected] | 1084ccc | 2012-03-14 03:22:53 | [diff] [blame] | 42 | r".+_autogen\.h$", |
Yue She | cf138055 | 2022-08-23 20:59:20 | [diff] [blame] | 43 | r".+_pb2(_grpc)?\.py$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 44 | r".+/pnacl_shim\.c$", |
| 45 | r"^gpu/config/.*_list_json\.cc$", |
| 46 | r"tools/md_browser/.*\.css$", |
Kenneth Russell | 077c8d9 | 2017-12-16 02:52:14 | [diff] [blame] | 47 | # Test pages for Maps telemetry tests. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 48 | r"tools/perf/page_sets/maps_perf_test.*", |
ehmaldonado | 78eee2ed | 2017-03-28 13:16:54 | [diff] [blame] | 49 | # Test pages for WebRTC telemetry tests. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 50 | r"tools/perf/page_sets/webrtc_cases.*", |
dpapad | 2efd445 | 2023-04-06 01:43:45 | [diff] [blame] | 51 | # Test file compared with generated output. |
| 52 | r"tools/polymer/tests/html_to_wrapper/.*.html.ts$", |
dpapad | a45be36c | 2024-08-07 20:19:35 | [diff] [blame] | 53 | # Third-party dependency frozen at a fixed version. |
| 54 | r"chrome/test/data/webui/chromeos/chai_v4.js$", |
[email protected] | 430641764 | 2009-06-11 00:33:40 | [diff] [blame] | 55 | ) |
[email protected] | ca8d198 | 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 | |
[email protected] | 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 | |
[email protected] | 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, |
Marijn Kruisselbrink | 2a2d5fc | 2024-05-15 15:23:49 | [diff] [blame] | 86 | # Test support files, like: |
| 87 | # foo_test_support.cc |
| 88 | # bar_test_util_linux.cc (suffix) |
| 89 | # baz_test_base.cc |
| 90 | r'.+_test_(base|support|util)(_[a-z]+)?%s' % _IMPLEMENTATION_EXTENSIONS, |
James Cook | 1b4dc13 | 2021-03-09 22:45:13 | [diff] [blame] | 91 | # Test suite files, like: |
| 92 | # foo_browsertest.cc |
| 93 | # bar_unittest_mac.cc (suffix) |
| 94 | # baz_unittests.cc (plural) |
| 95 | r'.+_(api|browser|eg|int|perf|pixel|unit|ui)?test(s)?(_[a-z]+)?%s' % |
[email protected] | e2d7e6f | 2013-04-23 12:57:12 | [diff] [blame] | 96 | _IMPLEMENTATION_EXTENSIONS, |
Matthew Denton | 63ea1e6 | 2019-03-25 20:39:18 | [diff] [blame] | 97 | r'.+_(fuzz|fuzzer)(_[a-z]+)?%s' % _IMPLEMENTATION_EXTENSIONS, |
Victor Hugo Vianna Silva | c22e020 | 2021-06-09 19:46:21 | [diff] [blame] | 98 | r'.+sync_service_impl_harness%s' % _IMPLEMENTATION_EXTENSIONS, |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 99 | r'.*/(test|tool(s)?)/.*', |
danakj | 89f4708 | 2020-09-02 17:53:43 | [diff] [blame] | 100 | # content_shell is used for running content_browsertests. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 101 | r'content/shell/.*', |
danakj | 89f4708 | 2020-09-02 17:53:43 | [diff] [blame] | 102 | # Web test harness. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 103 | r'content/web_test/.*', |
[email protected] | 7b05498 | 2013-11-27 00:44:47 | [diff] [blame] | 104 | # Non-production example code. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 105 | r'mojo/examples/.*', |
[email protected] | 8176de1 | 2014-06-20 19:07:08 | [diff] [blame] | 106 | # Launcher for running iOS tests on the simulator. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 107 | r'testing/iossim/iossim\.mm$', |
Olivier Robin | bcea0fa | 2019-11-12 08:56:41 | [diff] [blame] | 108 | # EarlGrey app side code for tests. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 109 | r'ios/.*_app_interface\.mm$', |
Allen Bauer | 0678d77 | 2020-05-11 22:25:17 | [diff] [blame] | 110 | # Views Examples code |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 111 | r'ui/views/examples/.*', |
Austin Sullivan | 33da70a | 2020-10-07 15:39:41 | [diff] [blame] | 112 | # Chromium Codelab |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 113 | r'codelabs/*' |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 114 | ) |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 115 | |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 116 | _THIRD_PARTY_EXCEPT_BLINK = 'third_party/(?!blink/)' |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 117 | |
[email protected] | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 118 | _TEST_ONLY_WARNING = ( |
| 119 | 'You might be calling functions intended only for testing from\n' |
danakj | 5f6e3b8 | 2020-09-10 13:52:55 | [diff] [blame] | 120 | 'production code. If you are doing this from inside another method\n' |
| 121 | 'named as *ForTesting(), then consider exposing things to have tests\n' |
| 122 | 'make that same call directly.\n' |
| 123 | 'If that is not possible, you may put a comment on the same line with\n' |
| 124 | ' // IN-TEST \n' |
| 125 | 'to tell the PRESUBMIT script that the code is inside a *ForTesting()\n' |
| 126 | 'method and can be ignored. Do not do this inside production code.\n' |
| 127 | 'The android-binary-size trybot will block if the method exists in the\n' |
Yulun Zeng | 08d7d8c | 2024-02-01 18:46:54 | [diff] [blame] | 128 | 'release apk.\n' |
| 129 | 'Note: this warning might be a false positive (crbug.com/1196548).') |
[email protected] | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 130 | |
| 131 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 132 | @dataclass |
| 133 | class BanRule: |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 134 | # String pattern. If the pattern begins with a slash, the pattern will be |
| 135 | # treated as a regular expression instead. |
| 136 | pattern: str |
| 137 | # Explanation as a sequence of strings. Each string in the sequence will be |
| 138 | # printed on its own line. |
mikt | 19226ff2 | 2024-08-27 05:28:21 | [diff] [blame] | 139 | explanation: Tuple[str, ...] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 140 | # Whether or not to treat this ban as a fatal error. If unspecified, |
| 141 | # defaults to true. |
| 142 | treat_as_error: Optional[bool] = None |
| 143 | # Paths that should be excluded from the ban check. Each string is a regular |
| 144 | # expression that will be matched against the path of the file being checked |
| 145 | # relative to the root of the source tree. |
| 146 | excluded_paths: Optional[Sequence[str]] = None |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 147 | # If True, surfaces any violation as a Gerrit comment on the CL after |
| 148 | # running the CQ. |
| 149 | surface_as_gerrit_lint: Optional[bool] = None |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 150 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 151 | |
Daniel Cheng | 917ce54 | 2022-03-15 20:46:57 | [diff] [blame] | 152 | _BANNED_JAVA_IMPORTS : Sequence[BanRule] = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 153 | BanRule( |
| 154 | 'import java.net.URI;', |
| 155 | ( |
| 156 | 'Use org.chromium.url.GURL instead of java.net.URI, where possible.', |
| 157 | ), |
| 158 | excluded_paths=( |
| 159 | (r'net/android/javatests/src/org/chromium/net/' |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 160 | r'AndroidProxySelectorTest\.java'), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 161 | r'components/cronet/', |
| 162 | r'third_party/robolectric/local/', |
| 163 | ), |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 164 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 165 | BanRule( |
| 166 | 'import android.annotation.TargetApi;', |
| 167 | ( |
| 168 | 'Do not use TargetApi, use @androidx.annotation.RequiresApi instead. ' |
| 169 | 'RequiresApi ensures that any calls are guarded by the appropriate ' |
| 170 | 'SDK_INT check. See https://crbug.com/1116486.', |
| 171 | ), |
| 172 | ), |
| 173 | BanRule( |
Mohamed Heikal | 3d7a94c | 2023-03-28 16:55:24 | [diff] [blame] | 174 | 'import androidx.test.rule.ActivityTestRule;', |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 175 | ( |
| 176 | 'Do not use ActivityTestRule, use ' |
| 177 | 'org.chromium.base.test.BaseActivityTestRule instead.', |
| 178 | ), |
| 179 | excluded_paths=( |
| 180 | 'components/cronet/', |
| 181 | ), |
| 182 | ), |
Min Qin | bc44383c | 2023-02-22 17:25:26 | [diff] [blame] | 183 | BanRule( |
| 184 | 'import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;', |
| 185 | ( |
| 186 | 'Do not use VectorDrawableCompat, use getResources().getDrawable() to ' |
| 187 | 'avoid extra indirections. Please also add trace event as the call ' |
| 188 | 'might take more than 20 ms to complete.', |
| 189 | ), |
| 190 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 191 | ) |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 192 | |
Daniel Cheng | 917ce54 | 2022-03-15 20:46:57 | [diff] [blame] | 193 | _BANNED_JAVA_FUNCTIONS : Sequence[BanRule] = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 194 | BanRule( |
Eric Stevenson | a9a98097 | 2017-09-23 00:04:41 | [diff] [blame] | 195 | 'StrictMode.allowThreadDiskReads()', |
| 196 | ( |
| 197 | 'Prefer using StrictModeContext.allowDiskReads() to using StrictMode ' |
| 198 | 'directly.', |
| 199 | ), |
| 200 | False, |
| 201 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 202 | BanRule( |
Eric Stevenson | a9a98097 | 2017-09-23 00:04:41 | [diff] [blame] | 203 | 'StrictMode.allowThreadDiskWrites()', |
| 204 | ( |
| 205 | 'Prefer using StrictModeContext.allowDiskWrites() to using StrictMode ' |
| 206 | 'directly.', |
| 207 | ), |
| 208 | False, |
| 209 | ), |
Daniel Cheng | 917ce54 | 2022-03-15 20:46:57 | [diff] [blame] | 210 | BanRule( |
Michael Thiessen | 0f2547e | 2020-07-27 21:55:36 | [diff] [blame] | 211 | '.waitForIdleSync()', |
| 212 | ( |
| 213 | 'Do not use waitForIdleSync as it masks underlying issues. There is ' |
| 214 | 'almost always something else you should wait on instead.', |
| 215 | ), |
| 216 | False, |
| 217 | ), |
Ashley Newson | 09cbd60 | 2022-10-26 11:40:14 | [diff] [blame] | 218 | BanRule( |
Ashley Newson | eb6f5ce | 2022-10-26 14:45:42 | [diff] [blame] | 219 | r'/(?<!\bsuper\.)(?<!\bIntent )\bregisterReceiver\(', |
Ashley Newson | 09cbd60 | 2022-10-26 11:40:14 | [diff] [blame] | 220 | ( |
| 221 | 'Do not call android.content.Context.registerReceiver (or an override) ' |
| 222 | 'directly. Use one of the wrapper methods defined in ' |
| 223 | 'org.chromium.base.ContextUtils, such as ' |
| 224 | 'registerProtectedBroadcastReceiver, ' |
| 225 | 'registerExportedBroadcastReceiver, or ' |
| 226 | 'registerNonExportedBroadcastReceiver. See their documentation for ' |
| 227 | 'which one to use.', |
| 228 | ), |
| 229 | True, |
| 230 | excluded_paths=( |
Ashley Newson | 22bc26d | 2022-11-01 20:30:57 | [diff] [blame] | 231 | r'.*Test[^a-z]', |
| 232 | r'third_party/', |
Ashley Newson | 09cbd60 | 2022-10-26 11:40:14 | [diff] [blame] | 233 | 'base/android/java/src/org/chromium/base/ContextUtils.java', |
Brandon Mousseau | 7e76a9c | 2022-12-08 22:08:38 | [diff] [blame] | 234 | 'chromecast/browser/android/apk/src/org/chromium/chromecast/shell/BroadcastReceiverScope.java', |
Ashley Newson | 09cbd60 | 2022-10-26 11:40:14 | [diff] [blame] | 235 | ), |
| 236 | ), |
Ted Choc | d5b327b1 | 2022-11-05 02:13:22 | [diff] [blame] | 237 | BanRule( |
| 238 | r'/(?:extends|new)\s*(?:android.util.)?Property<[A-Za-z.]+,\s*(?:Integer|Float)>', |
| 239 | ( |
| 240 | 'Do not use Property<..., Integer|Float>, but use FloatProperty or ' |
| 241 | 'IntProperty because it will avoid unnecessary autoboxing of ' |
| 242 | 'primitives.', |
| 243 | ), |
| 244 | ), |
Peilin Wang | bba4a865 | 2022-11-10 16:33:57 | [diff] [blame] | 245 | BanRule( |
| 246 | 'requestLayout()', |
| 247 | ( |
| 248 | 'Layouts can be expensive. Prefer using ViewUtils.requestLayout(), ' |
| 249 | 'which emits a trace event with additional information to help with ' |
| 250 | 'scroll jank investigations. See http://crbug.com/1354176.', |
| 251 | ), |
| 252 | False, |
| 253 | excluded_paths=( |
| 254 | 'ui/android/java/src/org/chromium/ui/base/ViewUtils.java', |
| 255 | ), |
| 256 | ), |
Ted Choc | f40ea915 | 2023-02-14 19:02:39 | [diff] [blame] | 257 | BanRule( |
Ted Choc | f486e3f | 2024-02-17 05:37:03 | [diff] [blame] | 258 | 'ProfileManager.getLastUsedRegularProfile()', |
Ted Choc | f40ea915 | 2023-02-14 19:02:39 | [diff] [blame] | 259 | ( |
| 260 | 'Prefer passing in the Profile reference instead of relying on the ' |
| 261 | 'static getLastUsedRegularProfile() call. Only top level entry points ' |
| 262 | '(e.g. Activities) should call this method. Otherwise, the Profile ' |
| 263 | 'should either be passed in explicitly or retreived from an existing ' |
| 264 | 'entity with a reference to the Profile (e.g. WebContents).', |
| 265 | ), |
| 266 | False, |
| 267 | excluded_paths=( |
| 268 | r'.*Test[A-Z]?.*\.java', |
| 269 | ), |
| 270 | ), |
Min Qin | bc44383c | 2023-02-22 17:25:26 | [diff] [blame] | 271 | BanRule( |
| 272 | r'/(ResourcesCompat|getResources\(\))\.getDrawable\(\)', |
| 273 | ( |
| 274 | 'getDrawable() can be expensive. If you have a lot of calls to ' |
| 275 | 'GetDrawable() or your code may introduce janks, please put your calls ' |
| 276 | 'inside a trace().', |
| 277 | ), |
| 278 | False, |
| 279 | excluded_paths=( |
| 280 | r'.*Test[A-Z]?.*\.java', |
| 281 | ), |
| 282 | ), |
Henrique Nakashima | bbf2b26 | 2023-03-10 17:21:39 | [diff] [blame] | 283 | BanRule( |
| 284 | r'/RecordHistogram\.getHistogram(ValueCount|TotalCount|Samples)ForTesting\(', |
| 285 | ( |
| 286 | 'Raw histogram counts are easy to misuse; for example they don\'t reset ' |
Thiago Perrotta | 099034f | 2023-06-05 18:10:20 | [diff] [blame] | 287 | 'between batched tests. Use HistogramWatcher to check histogram records ' |
| 288 | 'instead.', |
Henrique Nakashima | bbf2b26 | 2023-03-10 17:21:39 | [diff] [blame] | 289 | ), |
| 290 | False, |
| 291 | excluded_paths=( |
| 292 | 'base/android/javatests/src/org/chromium/base/metrics/RecordHistogramTest.java', |
| 293 | 'base/test/android/javatests/src/org/chromium/base/test/util/HistogramWatcher.java', |
| 294 | ), |
| 295 | ), |
Jenna Himawan | 859865d | 2025-02-25 22:22:31 | [diff] [blame] | 296 | BanRule( |
| 297 | r'/((announceForAccessibility\()|TYPE_ANNOUNCEMENT)', |
| 298 | ('Android 16 deprecates accessibility announcements, characterized by ' |
| 299 | 'the use of announceForAccessibility or the dispatch of ' |
| 300 | 'TYPE_ANNOUNCEMENT accessibility events. See ' |
| 301 | 'https://developer.android.com/about/versions/16/behavior-changes-all#disruptive-a11y' |
| 302 | ' for more details and suggested replacements.', ), |
| 303 | False, |
| 304 | ), |
Nate Fischer | d541ff8 | 2025-03-11 21:34:19 | [diff] [blame] | 305 | BanRule( |
| 306 | pattern=(r'IS_DESKTOP_ANDROID'), |
| 307 | explanation=( |
| 308 | 'Features which depend on IS_DESKTOP_ANDROID should only exist in ' |
| 309 | 'chrome/ layer and similar layers. Lower layers such as content/ ' |
| 310 | 'should not have features which are only designed for ' |
| 311 | 'desktop-android builds. See https://crbug.com/401628399.', ), |
| 312 | treat_as_error=False, |
| 313 | excluded_paths=[ |
| 314 | _THIRD_PARTY_EXCEPT_BLINK, # Don't warn in third_party folders. |
| 315 | r'^build/', # This is permitted in build/ folder. |
| 316 | r'^chrome/', # This is permitted in chrome/ folder. |
| 317 | r'^components/', # This is permitted only for components/ that are not shared by WebView. |
| 318 | r'^extensions/', # This is permitted in chrome/ folder. |
| 319 | r'^infra/', # This is permitted in infra/ folder. |
| 320 | r'^tools/', # This is permitted in tools/ folder. |
| 321 | ], |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 322 | surface_as_gerrit_lint=True, |
Nate Fischer | d541ff8 | 2025-03-11 21:34:19 | [diff] [blame] | 323 | ), |
Eric Stevenson | a9a98097 | 2017-09-23 00:04:41 | [diff] [blame] | 324 | ) |
| 325 | |
Clement Yan | 9b330cb | 2022-11-17 05:25:29 | [diff] [blame] | 326 | _BANNED_JAVASCRIPT_FUNCTIONS : Sequence [BanRule] = ( |
| 327 | BanRule( |
| 328 | r'/\bchrome\.send\b', |
| 329 | ( |
| 330 | '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).', |
| 331 | 'Please use mojo instead for new webuis. https://docs.google.com/document/d/1RF-GSUoveYa37eoyZ9EhwMtaIwoW7Z88pIgNZ9YzQi4/edit#heading=h.gkk22wgk6wff', |
| 332 | ), |
| 333 | True, |
| 334 | ( |
| 335 | r'^(?!ash\/webui).+', |
| 336 | # TODO(crbug.com/1385601): pre-existing violations still need to be |
| 337 | # cleaned up. |
Rebekah Potter | 57aa94df | 2022-12-13 20:30:58 | [diff] [blame] | 338 | 'ash/webui/common/resources/cr.m.js', |
Clement Yan | 9b330cb | 2022-11-17 05:25:29 | [diff] [blame] | 339 | 'ash/webui/common/resources/multidevice_setup/multidevice_setup_browser_proxy.js', |
Martin Bidlingmaier | a921fee7 | 2023-06-03 07:52:22 | [diff] [blame] | 340 | 'ash/webui/common/resources/quick_unlock/lock_screen_constants.ts', |
Clement Yan | 9b330cb | 2022-11-17 05:25:29 | [diff] [blame] | 341 | 'ash/webui/common/resources/smb_shares/smb_browser_proxy.js', |
Chad Duffin | 06e47de | 2023-12-14 18:04:13 | [diff] [blame] | 342 | 'ash/webui/connectivity_diagnostics/resources/connectivity_diagnostics.ts', |
Clement Yan | 9b330cb | 2022-11-17 05:25:29 | [diff] [blame] | 343 | 'ash/webui/diagnostics_ui/resources/diagnostics_browser_proxy.ts', |
| 344 | 'ash/webui/multidevice_debug/resources/logs.js', |
| 345 | 'ash/webui/multidevice_debug/resources/webui.js', |
| 346 | 'ash/webui/projector_app/resources/annotator/trusted/annotator_browser_proxy.js', |
| 347 | 'ash/webui/projector_app/resources/app/trusted/projector_browser_proxy.js', |
Ashley Prasad | 71f9024e | 2023-09-25 22:33:55 | [diff] [blame] | 348 | # TODO(b/301634378): Remove violation exception once Scanning App |
| 349 | # migrated off usage of `chrome.send`. |
| 350 | 'ash/webui/scanning/resources/scanning_browser_proxy.ts', |
Clement Yan | 9b330cb | 2022-11-17 05:25:29 | [diff] [blame] | 351 | ), |
| 352 | ), |
| 353 | ) |
| 354 | |
Daniel Cheng | 917ce54 | 2022-03-15 20:46:57 | [diff] [blame] | 355 | _BANNED_OBJC_FUNCTIONS : Sequence[BanRule] = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 356 | BanRule( |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 357 | 'addTrackingRect:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 358 | ( |
| 359 | 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is' |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 360 | 'prohibited. Please use CrTrackingArea instead.', |
| 361 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 362 | ), |
| 363 | False, |
| 364 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 365 | BanRule( |
[email protected] | eaae197 | 2014-04-16 04:17:26 | [diff] [blame] | 366 | r'/NSTrackingArea\W', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 367 | ( |
| 368 | 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 369 | 'instead.', |
| 370 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 371 | ), |
| 372 | False, |
| 373 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 374 | BanRule( |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 375 | 'convertPointFromBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 376 | ( |
| 377 | 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 378 | 'Please use |convertPoint:(point) fromView:nil| instead.', |
| 379 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 380 | ), |
| 381 | True, |
| 382 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 383 | BanRule( |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 384 | 'convertPointToBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 385 | ( |
| 386 | 'The use of -[NSView convertPointToBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 387 | 'Please use |convertPoint:(point) toView:nil| instead.', |
| 388 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 389 | ), |
| 390 | True, |
| 391 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 392 | BanRule( |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 393 | 'convertRectFromBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 394 | ( |
| 395 | 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 396 | 'Please use |convertRect:(point) fromView:nil| instead.', |
| 397 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 398 | ), |
| 399 | True, |
| 400 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 401 | BanRule( |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 402 | 'convertRectToBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 403 | ( |
| 404 | 'The use of -[NSView convertRectToBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 405 | 'Please use |convertRect:(point) toView:nil| instead.', |
| 406 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 407 | ), |
| 408 | True, |
| 409 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 410 | BanRule( |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 411 | 'convertSizeFromBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 412 | ( |
| 413 | 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 414 | 'Please use |convertSize:(point) fromView:nil| instead.', |
| 415 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 416 | ), |
| 417 | True, |
| 418 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 419 | BanRule( |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 420 | 'convertSizeToBase:', |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 421 | ( |
| 422 | 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.', |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 423 | 'Please use |convertSize:(point) toView:nil| instead.', |
| 424 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 425 | ), |
| 426 | True, |
| 427 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 428 | BanRule( |
jif | 6539870 | 2016-10-27 10:19:48 | [diff] [blame] | 429 | r"/\s+UTF8String\s*]", |
| 430 | ( |
| 431 | 'The use of -[NSString UTF8String] is dangerous as it can return null', |
| 432 | 'even if |canBeConvertedToEncoding:NSUTF8StringEncoding| returns YES.', |
| 433 | 'Please use |SysNSStringToUTF8| instead.', |
| 434 | ), |
| 435 | True, |
Marijn Kruisselbrink | 1b7c4895 | 2023-08-31 16:58:34 | [diff] [blame] | 436 | excluded_paths = ( |
| 437 | '^third_party/ocmock/OCMock/', |
| 438 | ), |
jif | 6539870 | 2016-10-27 10:19:48 | [diff] [blame] | 439 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 440 | BanRule( |
Sylvain Defresne | 4cf1d18 | 2017-09-18 14:16:34 | [diff] [blame] | 441 | r'__unsafe_unretained', |
| 442 | ( |
| 443 | 'The use of __unsafe_unretained is almost certainly wrong, unless', |
| 444 | 'when interacting with NSFastEnumeration or NSInvocation.', |
| 445 | 'Please use __weak in files build with ARC, nothing otherwise.', |
| 446 | ), |
| 447 | False, |
| 448 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 449 | BanRule( |
Avi Drissman | 7382afa0 | 2019-04-29 23:27:13 | [diff] [blame] | 450 | 'freeWhenDone:NO', |
| 451 | ( |
| 452 | 'The use of "freeWhenDone:NO" with the NoCopy creation of ', |
| 453 | 'Foundation types is prohibited.', |
| 454 | ), |
| 455 | True, |
| 456 | ), |
Avi Drissman | 3d243a4 | 2023-08-01 16:53:59 | [diff] [blame] | 457 | BanRule( |
| 458 | 'This file requires ARC support.', |
| 459 | ( |
| 460 | 'ARC compilation is default in Chromium; do not add boilerplate to ', |
| 461 | 'files that require ARC.', |
| 462 | ), |
| 463 | True, |
| 464 | ), |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 465 | ) |
| 466 | |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 467 | _BANNED_IOS_OBJC_FUNCTIONS = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 468 | BanRule( |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 469 | r'/\bTEST[(]', |
| 470 | ( |
| 471 | 'TEST() macro should not be used in Objective-C++ code as it does not ', |
| 472 | 'drain the autorelease pool at the end of the test. Use TEST_F() ', |
| 473 | 'macro instead with a fixture inheriting from PlatformTest (or a ', |
| 474 | 'typedef).' |
| 475 | ), |
| 476 | True, |
| 477 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 478 | BanRule( |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 479 | r'/\btesting::Test\b', |
| 480 | ( |
| 481 | 'testing::Test should not be used in Objective-C++ code as it does ', |
| 482 | 'not drain the autorelease pool at the end of the test. Use ', |
| 483 | 'PlatformTest instead.' |
| 484 | ), |
| 485 | True, |
| 486 | ), |
Ewann | 2ecc8d7 | 2022-07-18 07:41:23 | [diff] [blame] | 487 | BanRule( |
| 488 | ' systemImageNamed:', |
| 489 | ( |
| 490 | '+[UIImage systemImageNamed:] should not be used to create symbols.', |
| 491 | 'Instead use a wrapper defined in:', |
Slobodan Pejic | 8ef56c70 | 2024-07-12 18:21:26 | [diff] [blame] | 492 | 'ios/chrome/browser/shared/ui/symbols/symbol_helpers.h' |
Ewann | 2ecc8d7 | 2022-07-18 07:41:23 | [diff] [blame] | 493 | ), |
| 494 | True, |
Ewann | 450a2ef | 2022-07-19 14:38:23 | [diff] [blame] | 495 | excluded_paths=( |
Gauthier Ambard | 4d8756b | 2023-04-07 17:26:41 | [diff] [blame] | 496 | 'ios/chrome/browser/shared/ui/symbols/symbol_helpers.mm', |
Slobodan Pejic | 8ef56c70 | 2024-07-12 18:21:26 | [diff] [blame] | 497 | 'ios/chrome/common', |
Tommy Martino | 2a1182dc | 2024-11-20 19:34:42 | [diff] [blame] | 498 | # App extensions have restricted dependencies and thus can't use the |
| 499 | # wrappers. |
Riley Wong | 49be8a88 | 2025-02-27 00:38:23 | [diff] [blame] | 500 | r'^ios/chrome/\w+_extension/', |
Ewann | 450a2ef | 2022-07-19 14:38:23 | [diff] [blame] | 501 | ), |
Ewann | 2ecc8d7 | 2022-07-18 07:41:23 | [diff] [blame] | 502 | ), |
Sylvain Defresne | 781b9f9 | 2024-12-11 09:36:18 | [diff] [blame] | 503 | BanRule( |
| 504 | r'public (RefCounted)?BrowserStateKeyedServiceFactory', |
| 505 | ( |
| 506 | 'KeyedService factories in //ios/chrome/browser should inherit from', |
| 507 | '(Refcounted)?ProfileKeyedServieFactoryIOS, not directory from', |
| 508 | '(Refcounted)?BrowserStateKeyedServiceFactory.' |
| 509 | ), |
| 510 | treat_as_error=True, |
| 511 | excluded_paths=( |
| 512 | 'ios/components', |
| 513 | 'ios/web_view', |
| 514 | ), |
| 515 | ), |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 516 | ) |
| 517 | |
Daniel Cheng | 917ce54 | 2022-03-15 20:46:57 | [diff] [blame] | 518 | _BANNED_IOS_EGTEST_FUNCTIONS : Sequence[BanRule] = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 519 | BanRule( |
Peter K. Lee | 6c03ccff | 2019-07-15 14:40:05 | [diff] [blame] | 520 | r'/\bEXPECT_OCMOCK_VERIFY\b', |
| 521 | ( |
| 522 | 'EXPECT_OCMOCK_VERIFY should not be used in EarlGrey tests because ', |
| 523 | 'it is meant for GTests. Use [mock verify] instead.' |
| 524 | ), |
| 525 | True, |
| 526 | ), |
| 527 | ) |
| 528 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 529 | _BANNED_CPP_FUNCTIONS: Sequence[BanRule] = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 530 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 531 | '%#0', |
| 532 | ( |
| 533 | 'Zero-padded values that use "#" to add prefixes don\'t exhibit ', |
| 534 | 'consistent behavior, since the prefix is not prepended for zero ', |
| 535 | 'values. Use "0x%0..." instead.', |
| 536 | ), |
| 537 | False, |
| 538 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 7c0d98a | 2023-10-06 15:42:39 | [diff] [blame] | 539 | ), |
| 540 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 541 | r'/\busing namespace ', |
| 542 | ( |
| 543 | 'Using directives ("using namespace x") are banned by the Google Style', |
| 544 | 'Guide ( http://google.github.io/styleguide/cppguide.html#Namespaces ).', |
| 545 | 'Explicitly qualify symbols or use using declarations ("using x::foo").', |
| 546 | ), |
| 547 | True, |
| 548 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 94a56c4 | 2019-10-25 21:54:04 | [diff] [blame] | 549 | ), |
Antonio Gomes | 07300d0 | 2019-03-13 20:59:57 | [diff] [blame] | 550 | # Make sure that gtest's FRIEND_TEST() macro is not used; the |
| 551 | # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be |
| 552 | # used instead since that allows for FLAKY_ and DISABLED_ prefixes. |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 553 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 554 | 'FRIEND_TEST(', |
| 555 | ( |
| 556 | 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include', |
| 557 | 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.', |
| 558 | ), |
| 559 | False, |
| 560 | excluded_paths=( |
| 561 | "base/gtest_prod_util.h", |
| 562 | "base/allocator/partition_allocator/src/partition_alloc/partition_alloc_base/gtest_prod_util.h", |
| 563 | ), |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 564 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 565 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 566 | 'setMatrixClip', |
| 567 | ( |
| 568 | 'Overriding setMatrixClip() is prohibited; ', |
| 569 | 'the base function is deprecated. ', |
| 570 | ), |
| 571 | True, |
| 572 | (), |
tomhudson | e2c14d55 | 2016-05-26 17:07:46 | [diff] [blame] | 573 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 574 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 575 | 'SkRefPtr', |
| 576 | ('The use of SkRefPtr is prohibited. ', 'Please use sk_sp<> instead.'), |
| 577 | True, |
| 578 | (), |
[email protected] | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 579 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 580 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 581 | 'SkAutoRef', |
| 582 | ('The indirect use of SkRefPtr via SkAutoRef is prohibited. ', |
| 583 | 'Please use sk_sp<> instead.'), |
| 584 | True, |
| 585 | (), |
[email protected] | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 586 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 587 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 588 | 'SkAutoTUnref', |
| 589 | ('The use of SkAutoTUnref is dangerous because it implicitly ', |
| 590 | 'converts to a raw pointer. Please use sk_sp<> instead.'), |
| 591 | True, |
| 592 | (), |
[email protected] | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 593 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 594 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 595 | 'SkAutoUnref', |
| 596 | ('The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ', |
| 597 | 'because it implicitly converts to a raw pointer. ', |
| 598 | 'Please use sk_sp<> instead.'), |
| 599 | True, |
| 600 | (), |
[email protected] | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 601 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 602 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 603 | r'/HANDLE_EINTR\(.*close', |
| 604 | ('HANDLE_EINTR(close) is invalid. If close fails with EINTR, the file', |
| 605 | 'descriptor will be closed, and it is incorrect to retry the close.', |
| 606 | 'Either call close directly and ignore its return value, or wrap close', |
| 607 | 'in IGNORE_EINTR to use its return value. See http://crbug.com/269623' |
| 608 | ), |
| 609 | True, |
| 610 | (), |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 611 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 612 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 613 | r'/IGNORE_EINTR\((?!.*close)', |
| 614 | ( |
| 615 | 'IGNORE_EINTR is only valid when wrapping close. To wrap other system', |
| 616 | 'calls, use HANDLE_EINTR. See http://crbug.com/269623', |
| 617 | ), |
| 618 | True, |
| 619 | ( |
| 620 | # Files that #define IGNORE_EINTR. |
| 621 | r'^base/posix/eintr_wrapper\.h$', |
| 622 | r'^ppapi/tests/test_broker\.cc$', |
| 623 | ), |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 624 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 625 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 626 | r'/v8::Extension\(', |
| 627 | ( |
| 628 | 'Do not introduce new v8::Extensions into the code base, use', |
| 629 | 'gin::Wrappable instead. See http://crbug.com/334679', |
| 630 | ), |
| 631 | True, |
| 632 | (r'extensions/renderer/safe_builtins\.*', ), |
[email protected] | ec5b3f0 | 2014-04-04 18:43:43 | [diff] [blame] | 633 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 634 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 635 | '#pragma comment(lib,', |
| 636 | ('Specify libraries to link with in build files and not in the source.', |
| 637 | ), |
| 638 | True, |
| 639 | ( |
| 640 | r'^base/third_party/symbolize/.*', |
| 641 | r'^third_party/abseil-cpp/.*', |
Victor Hugo Vianna Silva | c86846c0 | 2025-03-07 06:56:37 | [diff] [blame] | 642 | r'^third_party/grpc/source/.*', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 643 | ), |
jam | e2d1a95 | 2016-04-02 00:27:10 | [diff] [blame] | 644 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 645 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 646 | r'/base::SequenceChecker\b', |
| 647 | ('Consider using SEQUENCE_CHECKER macros instead of the class directly.', |
| 648 | ), |
| 649 | False, |
| 650 | (), |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 651 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 652 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 653 | r'/base::ThreadChecker\b', |
| 654 | ('Consider using THREAD_CHECKER macros instead of the class directly.', |
| 655 | ), |
| 656 | False, |
| 657 | (), |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 658 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 659 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 660 | r'/\b(?!(Sequenced|SingleThread))\w*TaskRunner::(GetCurrentDefault|CurrentDefaultHandle)', |
| 661 | ( |
| 662 | 'It is not allowed to call these methods from the subclasses ', |
| 663 | 'of Sequenced or SingleThread task runners.', |
| 664 | ), |
| 665 | True, |
| 666 | (), |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 667 | ), |
| 668 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 669 | r'/(Time(|Delta|Ticks)|ThreadTicks)::FromInternalValue|ToInternalValue', |
| 670 | ( |
| 671 | 'base::TimeXXX::FromInternalValue() and ToInternalValue() are', |
| 672 | 'deprecated (http://crbug.com/634507). Please avoid converting away', |
| 673 | 'from the Time types in Chromium code, especially if any math is', |
| 674 | 'being done on time values. For interfacing with platform/library', |
| 675 | 'APIs, use base::Time::(From,To)DeltaSinceWindowsEpoch() or', |
| 676 | 'base::{TimeDelta::In}Microseconds(), or one of the other type', |
| 677 | 'converter methods instead. For faking TimeXXX values (for unit', |
| 678 | 'testing only), use TimeXXX() + Microseconds(N). For', |
| 679 | 'other use cases, please contact base/time/OWNERS.', |
| 680 | ), |
| 681 | False, |
| 682 | excluded_paths=( |
| 683 | "base/time/time.h", |
| 684 | "base/allocator/partition_allocator/src/partition_alloc/partition_alloc_base/time/time.h", |
| 685 | ), |
Yuri Wiitala | 2f8de5c | 2017-07-21 00:11:06 | [diff] [blame] | 686 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 687 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 688 | 'CallJavascriptFunctionUnsafe', |
| 689 | ( |
| 690 | "Don't use CallJavascriptFunctionUnsafe() in new code. Instead, use", |
| 691 | 'AllowJavascript(), OnJavascriptAllowed()/OnJavascriptDisallowed(),', |
| 692 | 'and CallJavascriptFunction(). See https://goo.gl/qivavq.', |
| 693 | ), |
| 694 | False, |
| 695 | ( |
| 696 | r'^content/browser/webui/web_ui_impl\.(cc|h)$', |
| 697 | r'^content/public/browser/web_ui\.h$', |
| 698 | r'^content/public/test/test_web_ui\.(cc|h)$', |
| 699 | ), |
dbeam | b6f4fde | 2017-06-15 04:03:06 | [diff] [blame] | 700 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 701 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 702 | 'leveldb::DB::Open', |
| 703 | ( |
| 704 | 'Instead of leveldb::DB::Open() use leveldb_env::OpenDB() from', |
| 705 | 'third_party/leveldatabase/env_chromium.h. It exposes databases to', |
| 706 | "Chrome's tracing, making their memory usage visible.", |
| 707 | ), |
| 708 | True, |
| 709 | (r'^third_party/leveldatabase/.*\.(cc|h)$', ), |
Gabriel Charette | 0592c3a | 2017-07-26 12:02:04 | [diff] [blame] | 710 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 711 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 712 | 'leveldb::NewMemEnv', |
| 713 | ( |
| 714 | 'Instead of leveldb::NewMemEnv() use leveldb_chrome::NewMemEnv() from', |
| 715 | 'third_party/leveldatabase/leveldb_chrome.h. It exposes environments', |
| 716 | "to Chrome's tracing, making their memory usage visible.", |
| 717 | ), |
| 718 | True, |
| 719 | (r'^third_party/leveldatabase/.*\.(cc|h)$', ), |
Chris Mumford | c38afb6 | 2017-10-09 17:55:08 | [diff] [blame] | 720 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 721 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 722 | 'base::ScopedMockTimeMessageLoopTaskRunner', |
| 723 | ( |
| 724 | 'ScopedMockTimeMessageLoopTaskRunner is deprecated. Prefer', |
| 725 | 'TaskEnvironment::TimeSource::MOCK_TIME. There are still a', |
| 726 | 'few cases that may require a ScopedMockTimeMessageLoopTaskRunner', |
| 727 | '(i.e. mocking the main MessageLoopForUI in browser_tests), but check', |
| 728 | 'with gab@ first if you think you need it)', |
| 729 | ), |
| 730 | False, |
| 731 | (), |
Eric Stevenson | 6b47b44c | 2017-08-30 20:41:57 | [diff] [blame] | 732 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 733 | BanRule( |
Peter Kasting | 5fdcd78 | 2025-01-13 14:57:07 | [diff] [blame] | 734 | '\bstd::aligned_(storage|union)\b', |
| 735 | ( |
| 736 | 'std::aligned_storage and std::aligned_union are deprecated in', |
| 737 | 'C++23. Use an aligned char array instead.' |
| 738 | ), |
| 739 | True, |
| 740 | (), |
| 741 | ), |
| 742 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 743 | 'std::regex', |
| 744 | ( |
| 745 | 'Using std::regex adds unnecessary binary size to Chrome. Please use', |
| 746 | 're2::RE2 instead (crbug.com/755321)', |
| 747 | ), |
| 748 | True, |
| 749 | [ |
| 750 | # Abseil's benchmarks never linked into chrome. |
| 751 | 'third_party/abseil-cpp/.*_benchmark.cc', |
| 752 | ], |
Francois Doray | 43670e3 | 2017-09-27 12:40:38 | [diff] [blame] | 753 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 754 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 755 | r'/\bstd::sto(i|l|ul|ll|ull)\b', |
| 756 | ( |
| 757 | 'std::sto{i,l,ul,ll,ull}() use exceptions to communicate results. ', |
| 758 | 'Use base::StringTo[U]Int[64]() instead.', |
| 759 | ), |
| 760 | True, |
| 761 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 991618a6 | 2019-06-17 22:00:09 | [diff] [blame] | 762 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 763 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 764 | r'/\bstd::sto(f|d|ld)\b', |
| 765 | ( |
| 766 | 'std::sto{f,d,ld}() use exceptions to communicate results. ', |
| 767 | 'For locale-independent values, e.g. reading numbers from disk', |
| 768 | 'profiles, use base::StringToDouble().', |
| 769 | 'For user-visible values, parse using ICU.', |
| 770 | ), |
| 771 | True, |
| 772 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 991618a6 | 2019-06-17 22:00:09 | [diff] [blame] | 773 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 774 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 775 | r'/\bstd::to_string\b', |
| 776 | ( |
| 777 | 'std::to_string() is locale dependent and slower than alternatives.', |
| 778 | 'For locale-independent strings, e.g. writing numbers to disk', |
| 779 | 'profiles, use base::NumberToString().', |
| 780 | 'For user-visible strings, use base::FormatNumber() and', |
| 781 | 'the related functions in base/i18n/number_formatting.h.', |
| 782 | ), |
| 783 | True, |
| 784 | [ |
| 785 | # TODO(crbug.com/335672557): Please do not add to this list. Existing |
| 786 | # uses should removed. |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 787 | "third_party/blink/renderer/core/css/parser/css_proto_converter.cc", |
| 788 | "third_party/blink/renderer/core/editing/ime/edit_context.cc", |
| 789 | "third_party/blink/renderer/platform/graphics/bitmap_image_test.cc", |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 790 | _THIRD_PARTY_EXCEPT_BLINK |
| 791 | ], |
Daniel Bratell | 69334cc | 2019-03-26 11:07:45 | [diff] [blame] | 792 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 793 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 794 | r'/#include <(cctype|ctype\.h|cwctype|wctype.h)>', |
| 795 | ( |
| 796 | '<cctype>/<ctype.h>/<cwctype>/<wctype.h> are banned. Use', |
| 797 | '"third_party/abseil-cpp/absl/strings/ascii.h" instead.', |
| 798 | ), |
| 799 | True, |
| 800 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 6f79b20 | 2023-08-09 21:25:41 | [diff] [blame] | 801 | ), |
| 802 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 803 | r'/\bstd::shared_ptr\b', |
| 804 | ('std::shared_ptr is banned. Use scoped_refptr instead.', ), |
| 805 | True, |
| 806 | [ |
| 807 | # Needed for interop with third-party library. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 808 | r'^third_party/blink/renderer/core/typed_arrays/array_buffer/' + |
| 809 | r'array_buffer_contents\.(cc|h)', |
| 810 | r'^third_party/blink/renderer/core/typed_arrays/dom_array_buffer\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 811 | '^third_party/blink/renderer/bindings/core/v8/' + |
| 812 | 'v8_wasm_response_extensions.cc', |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 813 | r'^gin/array_buffer\.(cc|h)', |
| 814 | r'^gin/per_isolate_data\.(cc|h)', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 815 | '^chrome/services/sharing/nearby/', |
| 816 | # Needed for interop with third-party library libunwindstack. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 817 | r'^base/profiler/libunwindstack_unwinder_android\.(cc|h)', |
| 818 | r'^base/profiler/native_unwinder_android_memory_regions_map_impl.(cc|h)', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 819 | # Needed for interop with third-party boringssl cert verifier |
| 820 | '^third_party/boringssl/', |
| 821 | '^net/cert/', |
| 822 | '^net/tools/cert_verify_tool/', |
| 823 | '^services/cert_verifier/', |
| 824 | '^components/certificate_transparency/', |
| 825 | '^components/media_router/common/providers/cast/certificate/', |
| 826 | # gRPC provides some C++ libraries that use std::shared_ptr<>. |
| 827 | '^chromeos/ash/services/libassistant/grpc/', |
| 828 | '^chromecast/cast_core/grpc', |
| 829 | '^chromecast/cast_core/runtime/browser', |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 830 | r'^ios/chrome/test/earl_grey/chrome_egtest_plugin_client\.(mm|h)', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 831 | # Fuchsia provides C++ libraries that use std::shared_ptr<>. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 832 | r'^base/fuchsia/.*\.(cc|h)', |
| 833 | r'.*fuchsia.*test\.(cc|h)', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 834 | # Clang plugins have different build config. |
| 835 | '^tools/clang/plugins/', |
| 836 | _THIRD_PARTY_EXCEPT_BLINK |
| 837 | ], # Not an error in third_party folders. |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 838 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 839 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 840 | r'/\bstd::weak_ptr\b', |
| 841 | ('std::weak_ptr is banned. Use base::WeakPtr instead.', ), |
| 842 | True, |
| 843 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 991618a6 | 2019-06-17 22:00:09 | [diff] [blame] | 844 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 845 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 846 | r'/\blong long\b', |
| 847 | ('long long is banned. Use [u]int64_t instead.', ), |
| 848 | False, # Only a warning since it is already used. |
| 849 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 850 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 851 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 852 | r'/\b(absl|std)::any\b', |
| 853 | ( |
| 854 | '{absl,std}::any are banned due to incompatibility with the component ', |
| 855 | 'build.', |
| 856 | ), |
| 857 | True, |
| 858 | # Not an error in third party folders, though it probably should be :) |
| 859 | [_THIRD_PARTY_EXCEPT_BLINK], |
Daniel Cheng | c05fcc6 | 2022-01-12 16:54:29 | [diff] [blame] | 860 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 861 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 862 | r'/\bstd::bind\b', |
| 863 | ( |
| 864 | 'std::bind() is banned because of lifetime risks. Use ', |
| 865 | 'base::Bind{Once,Repeating}() instead.', |
| 866 | ), |
| 867 | True, |
| 868 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 869 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 870 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 871 | (r'/\bstd::(?:' |
| 872 | r'linear_congruential_engine|mersenne_twister_engine|' |
| 873 | r'subtract_with_carry_engine|discard_block_engine|' |
| 874 | r'independent_bits_engine|shuffle_order_engine|' |
| 875 | r'minstd_rand0?|mt19937(_64)?|ranlux(24|48)(_base)?|knuth_b|' |
| 876 | r'default_random_engine|' |
| 877 | r'random_device|' |
| 878 | r'seed_seq' |
| 879 | r')\b'), |
| 880 | ( |
| 881 | 'STL random number engines and generators are banned. Use the ', |
| 882 | 'helpers in base/rand_util.h instead, e.g. base::RandBytes() or ', |
| 883 | 'base::RandomBitGenerator.' |
| 884 | '', |
| 885 | 'Please reach out to [email protected] if the base APIs are ', |
| 886 | 'insufficient for your needs.', |
| 887 | ), |
| 888 | True, |
| 889 | [ |
| 890 | # Not an error in third_party folders. |
| 891 | _THIRD_PARTY_EXCEPT_BLINK, |
| 892 | # Various tools which build outside of Chrome. |
| 893 | r'testing/libfuzzer', |
Steinar H. Gunderson | e5689e4 | 2024-08-07 18:17:19 | [diff] [blame] | 894 | r'testing/perf/confidence', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 895 | r'tools/android/io_benchmark/', |
| 896 | # Fuzzers are allowed to use standard library random number generators |
| 897 | # since fuzzing speed + reproducibility is important. |
| 898 | r'tools/ipc_fuzzer/', |
| 899 | r'.+_fuzzer\.cc$', |
| 900 | r'.+_fuzzertest\.cc$', |
| 901 | # TODO(https://crbug.com/1380528): These are all unsanctioned uses of |
| 902 | # the standard library's random number generators, and should be |
| 903 | # migrated to the //base equivalent. |
| 904 | r'ash/ambient/model/ambient_topic_queue\.cc', |
| 905 | r'base/allocator/partition_allocator/src/partition_alloc/partition_alloc_unittest\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 906 | r'base/test/launcher/test_launcher\.cc', |
| 907 | r'cc/metrics/video_playback_roughness_reporter_unittest\.cc', |
| 908 | r'chrome/browser/apps/app_service/metrics/website_metrics\.cc', |
| 909 | r'chrome/browser/ash/power/auto_screen_brightness/monotone_cubic_spline_unittest\.cc', |
| 910 | r'chrome/browser/ash/printing/zeroconf_printer_detector_unittest\.cc', |
| 911 | r'chrome/browser/nearby_sharing/contacts/nearby_share_contact_manager_impl_unittest\.cc', |
| 912 | r'chrome/browser/nearby_sharing/contacts/nearby_share_contacts_sorter_unittest\.cc', |
| 913 | r'chrome/browser/privacy_budget/mesa_distribution_unittest\.cc', |
| 914 | r'chrome/browser/web_applications/test/web_app_test_utils\.cc', |
| 915 | r'chrome/browser/web_applications/test/web_app_test_utils\.cc', |
| 916 | r'chrome/browser/win/conflicts/module_blocklist_cache_util_unittest\.cc', |
| 917 | r'chromeos/ash/components/memory/userspace_swap/swap_storage_unittest\.cc', |
| 918 | r'chromeos/ash/components/memory/userspace_swap/userspace_swap\.cc', |
| 919 | r'components/metrics/metrics_state_manager\.cc', |
| 920 | r'components/omnibox/browser/history_quick_provider_performance_unittest\.cc', |
| 921 | r'components/zucchini/disassembler_elf_unittest\.cc', |
| 922 | r'content/browser/webid/federated_auth_request_impl\.cc', |
| 923 | r'content/browser/webid/federated_auth_request_impl\.cc', |
| 924 | r'media/cast/test/utility/udp_proxy\.h', |
| 925 | r'sql/recover_module/module_unittest\.cc', |
Nicolas Dossou-Gbete | af9023d | 2025-04-07 17:44:38 | [diff] [blame] | 926 | r'components/regional_capabilities/regional_capabilities_utils.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 927 | # Do not add new entries to this list. If you have a use case which is |
| 928 | # not satisfied by the current APIs (i.e. you need an explicitly-seeded |
| 929 | # sequence, or stability of some sort is required), please contact |
| 930 | # [email protected]. |
| 931 | ], |
Daniel Cheng | 192683f | 2022-11-01 20:52:44 | [diff] [blame] | 932 | ), |
| 933 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 934 | r'/\b(absl,std)::bind_front\b', |
| 935 | ('{absl,std}::bind_front() are banned. Use base::Bind{Once,Repeating}() ' |
| 936 | 'instead.', ), |
| 937 | True, |
| 938 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 939 | ), |
| 940 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 941 | r'/\bABSL_FLAG\b', |
| 942 | ('ABSL_FLAG is banned. Use base::CommandLine instead.', ), |
| 943 | True, |
| 944 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 945 | ), |
| 946 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 947 | r'/\babsl::c_', |
| 948 | ( |
Peter Kasting | 3b811ffd | 2025-01-29 22:20:16 | [diff] [blame] | 949 | 'Abseil container utilities are banned. Use std::ranges:: instead.', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 950 | ), |
| 951 | True, |
| 952 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 953 | ), |
| 954 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 955 | r'/\babsl::FixedArray\b', |
| 956 | ('absl::FixedArray is banned. Use base::FixedArray instead.', ), |
| 957 | True, |
| 958 | [ |
| 959 | # base::FixedArray provides canonical access. |
| 960 | r'^base/types/fixed_array.h', |
| 961 | # Not an error in third_party folders. |
| 962 | _THIRD_PARTY_EXCEPT_BLINK, |
| 963 | ], |
Peter Kasting | 431239a | 2023-09-29 03:11:44 | [diff] [blame] | 964 | ), |
| 965 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 966 | r'/\babsl::FunctionRef\b', |
| 967 | ('absl::FunctionRef is banned. Use base::FunctionRef instead.', ), |
| 968 | True, |
| 969 | [ |
| 970 | # base::Bind{Once,Repeating} references absl::FunctionRef to disallow |
| 971 | # interoperability. |
| 972 | r'^base/functional/bind_internal\.h', |
| 973 | # base::FunctionRef is implemented on top of absl::FunctionRef. |
| 974 | r'^base/functional/function_ref.*\..+', |
| 975 | # Not an error in third_party folders. |
| 976 | _THIRD_PARTY_EXCEPT_BLINK, |
| 977 | ], |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 978 | ), |
| 979 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 980 | r'/\babsl::(Insecure)?BitGen\b', |
| 981 | ('absl random number generators are banned. Use the helpers in ' |
| 982 | 'base/rand_util.h instead, e.g. base::RandBytes() or ', |
| 983 | 'base::RandomBitGenerator.'), |
| 984 | True, |
| 985 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 986 | ), |
| 987 | BanRule( |
Peter Kasting | 3b77a0c | 2024-08-22 00:22:26 | [diff] [blame] | 988 | pattern= |
| 989 | r'/\babsl::(optional|nullopt|make_optional)\b', |
| 990 | explanation=('absl::optional is banned. Use std::optional instead.', ), |
| 991 | treat_as_error=True, |
| 992 | excluded_paths=[ |
| 993 | _THIRD_PARTY_EXCEPT_BLINK, |
| 994 | ]), |
| 995 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 996 | r'/(\babsl::Span\b|#include <span>|\bstd::span\b)', |
| 997 | ( |
Peter Kasting | e73b89d | 2024-11-26 19:35:52 | [diff] [blame] | 998 | 'absl::Span and std::span are banned. Use base::span instead.', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 999 | ), |
| 1000 | True, |
| 1001 | [ |
| 1002 | # Included for conversions between base and std. |
| 1003 | r'base/containers/span.h', |
| 1004 | # Test base::span<> compatibility against std::span<>. |
| 1005 | r'base/containers/span_unittest.cc', |
| 1006 | # //base/numerics can't use base or absl. So it uses std. |
| 1007 | r'base/numerics/.*' |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 1008 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1009 | # Needed to use QUICHE API. |
Ciara McMullin | c029c8e | 2024-08-21 14:22:32 | [diff] [blame] | 1010 | r'android_webview/browser/ip_protection/.*', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1011 | r'chrome/browser/ip_protection/.*', |
| 1012 | r'components/ip_protection/.*', |
| 1013 | r'net/third_party/quiche/overrides/quiche_platform_impl/quiche_stack_trace_impl\.*', |
| 1014 | r'services/network/web_transport\.cc', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 1015 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1016 | # Not an error in third_party folders. |
| 1017 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1018 | ], |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1019 | ), |
| 1020 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1021 | r'/\babsl::StatusOr\b', |
| 1022 | ('absl::StatusOr is banned. Use base::expected instead.', ), |
| 1023 | True, |
| 1024 | [ |
| 1025 | # Needed to use liburlpattern API. |
| 1026 | r'components/url_pattern/.*', |
| 1027 | r'services/network/shared_dictionary/simple_url_pattern_matcher\.cc', |
| 1028 | r'third_party/blink/renderer/core/url_pattern/.*', |
| 1029 | r'third_party/blink/renderer/modules/manifest/manifest_parser\.cc', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 1030 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1031 | # Needed to use QUICHE API. |
Ciara McMullin | c029c8e | 2024-08-21 14:22:32 | [diff] [blame] | 1032 | r'android_webview/browser/ip_protection/.*', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1033 | r'chrome/browser/ip_protection/.*', |
| 1034 | r'components/ip_protection/.*', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 1035 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1036 | # Needed to use MediaPipe API. |
| 1037 | r'components/media_effects/.*\.cc', |
| 1038 | # Not an error in third_party folders. |
| 1039 | _THIRD_PARTY_EXCEPT_BLINK |
| 1040 | ], |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1041 | ), |
| 1042 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1043 | r'/\babsl::(StrSplit|StrJoin|StrCat|StrAppend|Substitute|StrContains)\b', |
| 1044 | ('Abseil string utilities are banned. Use base/strings instead.', ), |
| 1045 | True, |
| 1046 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1047 | ), |
| 1048 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1049 | r'/\babsl::(Mutex|CondVar|Notification|Barrier|BlockingCounter)\b', |
| 1050 | ( |
| 1051 | 'Abseil synchronization primitives are banned. Use', |
| 1052 | 'base/synchronization instead.', |
| 1053 | ), |
| 1054 | True, |
| 1055 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 1056 | ), |
| 1057 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1058 | r'/\babsl::(Duration|Time|TimeZone|CivilDay)\b', |
| 1059 | ('Abseil\'s time library is banned. Use base/time instead.', ), |
| 1060 | True, |
| 1061 | [ |
| 1062 | # Needed to use QUICHE API. |
Ciara McMullin | c029c8e | 2024-08-21 14:22:32 | [diff] [blame] | 1063 | r'android_webview/browser/ip_protection/.*', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1064 | r'chrome/browser/ip_protection/.*', |
| 1065 | r'components/ip_protection/.*', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 1066 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1067 | # Needed to integrate with //third_party/nearby |
| 1068 | r'components/cross_device/nearby/system_clock.cc', |
| 1069 | _THIRD_PARTY_EXCEPT_BLINK # Not an error in third_party folders. |
| 1070 | ], |
| 1071 | ), |
| 1072 | BanRule( |
Victor Hugo Vianna Silva | 6e84e8d4 | 2025-03-19 00:32:00 | [diff] [blame] | 1073 | r'/absl::(bad_variant_access|get|holds_alternative|monostate|variant|' |
| 1074 | r'visit)', |
| 1075 | ('Abseil\'s variant library is banned, use std.', ), |
Victor Hugo Vianna Silva | d9139fbe | 2025-03-19 20:59:08 | [diff] [blame] | 1076 | True, |
Victor Hugo Vianna Silva | 6e84e8d4 | 2025-03-19 00:32:00 | [diff] [blame] | 1077 | [ |
| 1078 | _THIRD_PARTY_EXCEPT_BLINK |
| 1079 | ], |
| 1080 | ), |
| 1081 | BanRule( |
| 1082 | r'/absl::(apply|exchange|forward|in_place|index_sequence|' |
| 1083 | r'integer_sequence|make_from_tuple|make_index_sequence|' |
| 1084 | r'make_integer_sequence|move)', |
| 1085 | ('Abseil\'s util library is banned, use std.', ), |
Victor Hugo Vianna Silva | d9139fbe | 2025-03-19 20:59:08 | [diff] [blame] | 1086 | True, |
Victor Hugo Vianna Silva | 6e84e8d4 | 2025-03-19 00:32:00 | [diff] [blame] | 1087 | [ |
| 1088 | _THIRD_PARTY_EXCEPT_BLINK |
| 1089 | ], |
| 1090 | ), |
| 1091 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1092 | r'/#include <chrono>', |
| 1093 | ('<chrono> is banned. Use base/time instead.', ), |
| 1094 | True, |
| 1095 | [ |
| 1096 | # Not an error in third_party folders: |
| 1097 | _THIRD_PARTY_EXCEPT_BLINK, |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1098 | # This uses openscreen API depending on std::chrono. |
| 1099 | "components/openscreen_platform/task_runner.cc", |
| 1100 | ]), |
| 1101 | BanRule( |
| 1102 | r'/#include <exception>', |
| 1103 | ('Exceptions are banned and disabled in Chromium.', ), |
| 1104 | True, |
| 1105 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 1106 | ), |
| 1107 | BanRule( |
| 1108 | r'/\bstd::function\b', |
| 1109 | ('std::function is banned. Use base::{Once,Repeating}Callback instead.', |
| 1110 | ), |
| 1111 | True, |
| 1112 | [ |
| 1113 | # Has tests that template trait helpers don't unintentionally match |
| 1114 | # std::function. |
| 1115 | r'base/functional/callback_helpers_unittest\.cc', |
| 1116 | # Required to implement interfaces from the third-party perfetto |
| 1117 | # library. |
| 1118 | r'base/tracing/perfetto_task_runner\.cc', |
| 1119 | r'base/tracing/perfetto_task_runner\.h', |
| 1120 | # Needed for interop with the third-party nearby library type |
| 1121 | # location::nearby::connections::ResultCallback. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1122 | r'chrome/services/sharing/nearby/nearby_connections_conversions\.cc' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1123 | # Needed for interop with the internal libassistant library. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1124 | r'chromeos/ash/services/libassistant/callback_utils\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1125 | # Needed for interop with Fuchsia fidl APIs. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1126 | r'fuchsia_web/webengine/browser/context_impl_browsertest\.cc', |
| 1127 | r'fuchsia_web/webengine/browser/cookie_manager_impl_unittest\.cc', |
| 1128 | r'fuchsia_web/webengine/browser/media_player_impl_unittest\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1129 | # Required to interop with interfaces from the third-party ChromeML |
| 1130 | # library API. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1131 | r'services/on_device_model/ml/chrome_ml_api\.h', |
| 1132 | r'services/on_device_model/ml/on_device_model_executor\.cc', |
| 1133 | r'services/on_device_model/ml/on_device_model_executor\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1134 | # Required to interop with interfaces from the third-party perfetto |
| 1135 | # library. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1136 | r'components/tracing/common/etw_consumer_win_unittest\.cc', |
| 1137 | r'services/tracing/public/cpp/perfetto/custom_event_recorder\.cc', |
| 1138 | r'services/tracing/public/cpp/perfetto/perfetto_traced_process\.cc', |
| 1139 | r'services/tracing/public/cpp/perfetto/perfetto_traced_process\.h', |
| 1140 | r'services/tracing/public/cpp/perfetto/perfetto_tracing_backend\.cc', |
| 1141 | r'services/tracing/public/cpp/perfetto/producer_client\.cc', |
| 1142 | r'services/tracing/public/cpp/perfetto/producer_client\.h', |
| 1143 | r'services/tracing/public/cpp/perfetto/producer_test_utils\.cc', |
| 1144 | r'services/tracing/public/cpp/perfetto/producer_test_utils\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1145 | # Required for interop with the third-party webrtc library. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1146 | r'third_party/blink/renderer/modules/peerconnection/mock_peer_connection_impl\.cc', |
| 1147 | r'third_party/blink/renderer/modules/peerconnection/mock_peer_connection_impl\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1148 | # TODO(https://crbug.com/1364577): Various uses that should be |
| 1149 | # migrated to something else. |
| 1150 | # Should use base::OnceCallback or base::RepeatingCallback. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1151 | r'base/allocator/dispatcher/initializer_unittest\.cc', |
| 1152 | r'chrome/browser/ash/accessibility/speech_monitor\.cc', |
| 1153 | r'chrome/browser/ash/accessibility/speech_monitor\.h', |
| 1154 | r'chrome/browser/ash/login/ash_hud_login_browsertest\.cc', |
| 1155 | r'chromecast/base/observer_unittest\.cc', |
| 1156 | r'chromecast/browser/cast_web_view\.h', |
| 1157 | r'chromecast/public/cast_media_shlib\.h', |
| 1158 | r'device/bluetooth/floss/exported_callback_manager\.h', |
| 1159 | r'device/bluetooth/floss/floss_dbus_client\.h', |
| 1160 | r'device/fido/cable/v2_handshake_unittest\.cc', |
| 1161 | r'device/fido/pin\.cc', |
| 1162 | r'services/tracing/perfetto/test_utils\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1163 | # Should use base::FunctionRef. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1164 | r'chrome/browser/media/webrtc/test_stats_dictionary\.cc', |
| 1165 | r'chrome/browser/media/webrtc/test_stats_dictionary\.h', |
| 1166 | r'chromeos/ash/services/libassistant/device_settings_controller\.cc', |
| 1167 | r'components/browser_ui/client_certificate/android/ssl_client_certificate_request\.cc', |
| 1168 | r'components/gwp_asan/client/sampling_malloc_shims_unittest\.cc', |
| 1169 | r'content/browser/font_unique_name_lookup/font_unique_name_lookup_unittest\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1170 | # Does not need std::function at all. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1171 | r'components/omnibox/browser/autocomplete_result\.cc', |
| 1172 | r'device/fido/win/webauthn_api\.cc', |
| 1173 | r'media/audio/alsa/alsa_util\.cc', |
| 1174 | r'media/remoting/stream_provider\.h', |
| 1175 | r'sql/vfs_wrapper\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1176 | # TODO(https://crbug.com/1364585): Remove usage and exception list |
| 1177 | # entries. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1178 | r'extensions/renderer/api/automation/automation_internal_custom_bindings\.cc', |
| 1179 | r'extensions/renderer/api/automation/automation_internal_custom_bindings\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1180 | # TODO(https://crbug.com/1364579): Remove usage and exception list |
| 1181 | # entry. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1182 | r'ui/views/controls/focus_ring\.h', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 1183 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1184 | # Various pre-existing uses in //tools that is low-priority to fix. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1185 | r'tools/binary_size/libsupersize/viewer/caspian/diff\.cc', |
| 1186 | r'tools/binary_size/libsupersize/viewer/caspian/model\.cc', |
| 1187 | r'tools/binary_size/libsupersize/viewer/caspian/model\.h', |
| 1188 | r'tools/binary_size/libsupersize/viewer/caspian/tree_builder\.h', |
| 1189 | r'tools/clang/base_bind_rewriters/BaseBindRewriters\.cpp', |
Daniel Cheng | e5583e3c | 2022-09-22 00:19:41 | [diff] [blame] | 1190 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1191 | # Not an error in third_party folders. |
| 1192 | _THIRD_PARTY_EXCEPT_BLINK |
| 1193 | ], |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 1194 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1195 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1196 | r'/#include <X11/', |
| 1197 | ('Do not use Xlib. Use xproto (from //ui/gfx/x:xproto) instead.', ), |
| 1198 | True, |
| 1199 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Tom Anderson | a95e1204 | 2020-09-09 23:08:00 | [diff] [blame] | 1200 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1201 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1202 | r'/\bstd::ratio\b', |
| 1203 | ('std::ratio is banned by the Google Style Guide.', ), |
| 1204 | True, |
| 1205 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Daniel Bratell | 69334cc | 2019-03-26 11:07:45 | [diff] [blame] | 1206 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1207 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1208 | r'/\bstd::aligned_alloc\b', |
| 1209 | ( |
| 1210 | 'std::aligned_alloc() is not yet allowed (crbug.com/1412818). Use ', |
| 1211 | 'base::AlignedAlloc() instead.', |
| 1212 | ), |
| 1213 | True, |
| 1214 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1215 | ), |
| 1216 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1217 | r'/#include <(barrier|latch|semaphore|stop_token)>', |
| 1218 | ('The thread support library is banned. Use base/synchronization ' |
| 1219 | 'instead.', ), |
| 1220 | True, |
| 1221 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1222 | ), |
| 1223 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1224 | r'/\bstd::execution::(par|seq)\b', |
| 1225 | ('std::execution::(par|seq) is banned; they do not fit into ' |
| 1226 | ' Chrome\'s threading model, and libc++ doesn\'t have full ' |
mikt | 19226ff2 | 2024-08-27 05:28:21 | [diff] [blame] | 1227 | 'support.', ), |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1228 | True, |
| 1229 | [_THIRD_PARTY_EXCEPT_BLINK], |
Helmut Januschka | 7cc8a84f | 2024-02-07 22:50:41 | [diff] [blame] | 1230 | ), |
| 1231 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1232 | r'/\bstd::bit_cast\b', |
| 1233 | ('std::bit_cast is banned; use base::bit_cast instead for values and ' |
| 1234 | 'standard C++ casting when pointers are involved.', ), |
| 1235 | True, |
| 1236 | [ |
| 1237 | # Don't warn in third_party folders. |
| 1238 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1239 | # //base/numerics can't use base or absl. |
| 1240 | r'base/numerics/.*' |
| 1241 | ], |
Avi Drissman | 70cb7f7 | 2023-12-12 17:44:37 | [diff] [blame] | 1242 | ), |
| 1243 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1244 | r'/\bstd::(c8rtomb|mbrtoc8)\b', |
| 1245 | ('std::c8rtomb() and std::mbrtoc8() are banned.', ), |
| 1246 | True, |
| 1247 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1248 | ), |
| 1249 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1250 | r'/\bchar8_t|std::u8string\b', |
| 1251 | ( |
| 1252 | 'char8_t and std::u8string are not yet allowed. Can you use [unsigned]', |
| 1253 | ' char and std::string instead?', |
| 1254 | ), |
| 1255 | True, |
| 1256 | [ |
| 1257 | # The demangler does not use this type but needs to know about it. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1258 | r'base/third_party/symbolize/demangle\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1259 | # Don't warn in third_party folders. |
| 1260 | _THIRD_PARTY_EXCEPT_BLINK |
| 1261 | ], |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1262 | ), |
| 1263 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1264 | r'/(\b(co_await|co_return|co_yield)\b|#include <coroutine>)', |
| 1265 | ('Coroutines are not yet allowed (https://crbug.com/1403840).', ), |
| 1266 | True, |
| 1267 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1268 | ), |
| 1269 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1270 | r'/^\s*(export\s|import\s+["<:\w]|module(;|\s+[:\w]))', |
| 1271 | ('Modules are disallowed for now due to lack of toolchain support.', ), |
| 1272 | True, |
| 1273 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 69357dc | 2023-03-14 01:34:29 | [diff] [blame] | 1274 | ), |
| 1275 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1276 | r'/\[\[(\w*::)?no_unique_address\]\]', |
| 1277 | ( |
| 1278 | '[[no_unique_address]] does not work as expected on Windows ', |
| 1279 | '(https://crbug.com/1414621). Use NO_UNIQUE_ADDRESS instead.', |
| 1280 | ), |
| 1281 | True, |
| 1282 | [ |
| 1283 | # NO_UNIQUE_ADDRESS / PA_NO_UNIQUE_ADDRESS provide canonical access. |
| 1284 | r'^base/compiler_specific\.h', |
| 1285 | r'^base/allocator/partition_allocator/src/partition_alloc/partition_alloc_base/compiler_specific\.h', |
| 1286 | # Not an error in third_party folders. |
| 1287 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1288 | ], |
Peter Kasting | 8bc046d2 | 2023-11-14 00:38:03 | [diff] [blame] | 1289 | ), |
| 1290 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1291 | r'/#include <format>', |
| 1292 | ('<format> is not yet allowed. Use base::StringPrintf() instead.', ), |
| 1293 | True, |
| 1294 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1295 | ), |
| 1296 | BanRule( |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1297 | pattern='std::views', |
| 1298 | explanation=( |
| 1299 | 'Use of std::views is banned in Chrome. If you need this ' |
| 1300 | 'functionality, please contact [email protected].', |
| 1301 | ), |
| 1302 | treat_as_error=True, |
| 1303 | excluded_paths=[ |
| 1304 | # Don't warn in third_party folders. |
| 1305 | _THIRD_PARTY_EXCEPT_BLINK |
| 1306 | ], |
| 1307 | ), |
| 1308 | BanRule( |
| 1309 | # Ban everything except specifically allowlisted constructs. |
| 1310 | pattern=r'/std::ranges::(?!' + '|'.join(( |
| 1311 | # From https://en.cppreference.com/w/cpp/ranges: |
| 1312 | # Range access |
| 1313 | 'begin', |
| 1314 | 'end', |
| 1315 | 'cbegin', |
| 1316 | 'cend', |
| 1317 | 'rbegin', |
| 1318 | 'rend', |
| 1319 | 'crbegin', |
| 1320 | 'crend', |
| 1321 | 'size', |
| 1322 | 'ssize', |
| 1323 | 'empty', |
| 1324 | 'data', |
| 1325 | 'cdata', |
| 1326 | # Range primitives |
| 1327 | 'iterator_t', |
| 1328 | 'const_iterator_t', |
| 1329 | 'sentinel_t', |
| 1330 | 'const_sentinel_t', |
| 1331 | 'range_difference_t', |
| 1332 | 'range_size_t', |
| 1333 | 'range_value_t', |
| 1334 | 'range_reference_t', |
| 1335 | 'range_const_reference_t', |
| 1336 | 'range_rvalue_reference_t', |
| 1337 | 'range_common_reference_t', |
| 1338 | # Dangling iterator handling |
| 1339 | 'dangling', |
| 1340 | 'borrowed_iterator_t', |
| 1341 | # Banned: borrowed_subrange_t |
| 1342 | # Range concepts |
| 1343 | 'range', |
| 1344 | 'borrowed_range', |
| 1345 | 'sized_range', |
| 1346 | 'view', |
| 1347 | 'input_range', |
| 1348 | 'output_range', |
| 1349 | 'forward_range', |
| 1350 | 'bidirectional_range', |
| 1351 | 'random_access_range', |
| 1352 | 'contiguous_range', |
| 1353 | 'common_range', |
| 1354 | 'viewable_range', |
| 1355 | 'constant_range', |
| 1356 | # Banned: Views |
| 1357 | # Banned: Range factories |
| 1358 | # Banned: Range adaptors |
Peter Kasting | a7f9375 | 2024-10-24 22:15:40 | [diff] [blame] | 1359 | # Incidentally listed on |
| 1360 | # https://en.cppreference.com/w/cpp/header/ranges: |
| 1361 | 'enable_borrowed_range', |
| 1362 | 'enable_view', |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1363 | # From https://en.cppreference.com/w/cpp/algorithm/ranges: |
| 1364 | # Constrained algorithms: non-modifying sequence operations |
| 1365 | 'all_of', |
| 1366 | 'any_of', |
| 1367 | 'none_of', |
| 1368 | 'for_each', |
| 1369 | 'for_each_n', |
| 1370 | 'count', |
| 1371 | 'count_if', |
| 1372 | 'mismatch', |
| 1373 | 'equal', |
| 1374 | 'lexicographical_compare', |
| 1375 | 'find', |
| 1376 | 'find_if', |
| 1377 | 'find_if_not', |
| 1378 | 'find_end', |
| 1379 | 'find_first_of', |
| 1380 | 'adjacent_find', |
| 1381 | 'search', |
| 1382 | 'search_n', |
| 1383 | # Constrained algorithms: modifying sequence operations |
| 1384 | 'copy', |
| 1385 | 'copy_if', |
| 1386 | 'copy_n', |
| 1387 | 'copy_backward', |
| 1388 | 'move', |
| 1389 | 'move_backward', |
| 1390 | 'fill', |
| 1391 | 'fill_n', |
| 1392 | 'transform', |
| 1393 | 'generate', |
| 1394 | 'generate_n', |
| 1395 | 'remove', |
| 1396 | 'remove_if', |
| 1397 | 'remove_copy', |
| 1398 | 'remove_copy_if', |
| 1399 | 'replace', |
| 1400 | 'replace_if', |
| 1401 | 'replace_copy', |
| 1402 | 'replace_copy_if', |
| 1403 | 'swap_ranges', |
| 1404 | 'reverse', |
| 1405 | 'reverse_copy', |
| 1406 | 'rotate', |
| 1407 | 'rotate_copy', |
| 1408 | 'shuffle', |
| 1409 | 'sample', |
| 1410 | 'unique', |
| 1411 | 'unique_copy', |
| 1412 | # Constrained algorithms: partitioning operations |
| 1413 | 'is_partitioned', |
| 1414 | 'partition', |
| 1415 | 'partition_copy', |
| 1416 | 'stable_partition', |
| 1417 | 'partition_point', |
| 1418 | # Constrained algorithms: sorting operations |
| 1419 | 'is_sorted', |
| 1420 | 'is_sorted_until', |
| 1421 | 'sort', |
| 1422 | 'partial_sort', |
| 1423 | 'partial_sort_copy', |
| 1424 | 'stable_sort', |
| 1425 | 'nth_element', |
| 1426 | # Constrained algorithms: binary search operations (on sorted ranges) |
| 1427 | 'lower_bound', |
| 1428 | 'upper_bound', |
| 1429 | 'binary_search', |
| 1430 | 'equal_range', |
| 1431 | # Constrained algorithms: set operations (on sorted ranges) |
| 1432 | 'merge', |
| 1433 | 'inplace_merge', |
| 1434 | 'includes', |
| 1435 | 'set_difference', |
| 1436 | 'set_intersection', |
| 1437 | 'set_symmetric_difference', |
| 1438 | 'set_union', |
| 1439 | # Constrained algorithms: heap operations |
| 1440 | 'is_heap', |
| 1441 | 'is_heap_until', |
| 1442 | 'make_heap', |
| 1443 | 'push_heap', |
| 1444 | 'pop_heap', |
| 1445 | 'sort_heap', |
| 1446 | # Constrained algorithms: minimum/maximum operations |
| 1447 | 'max', |
| 1448 | 'max_element', |
| 1449 | 'min', |
| 1450 | 'min_element', |
| 1451 | 'minmax', |
| 1452 | 'minmax_element', |
| 1453 | 'clamp', |
| 1454 | # Constrained algorithms: permutation operations |
| 1455 | 'is_permutation', |
| 1456 | 'next_permutation', |
| 1457 | 'prev_premutation', |
| 1458 | # Constrained uninitialized memory algorithms |
| 1459 | 'uninitialized_copy', |
| 1460 | 'uninitialized_copy_n', |
| 1461 | 'uninitialized_fill', |
| 1462 | 'uninitialized_fill_n', |
| 1463 | 'uninitialized_move', |
| 1464 | 'uninitialized_move_n', |
| 1465 | 'uninitialized_default_construct', |
| 1466 | 'uninitialized_default_construct_n', |
| 1467 | 'uninitialized_value_construct', |
| 1468 | 'uninitialized_value_construct_n', |
| 1469 | 'destroy', |
| 1470 | 'destroy_n', |
| 1471 | 'destroy_at', |
| 1472 | 'construct_at', |
| 1473 | # Return types |
| 1474 | 'in_fun_result', |
| 1475 | 'in_in_result', |
| 1476 | 'in_out_result', |
| 1477 | 'in_in_out_result', |
| 1478 | 'in_out_out_result', |
| 1479 | 'min_max_result', |
| 1480 | 'in_found_result', |
Peter Kasting | f379c02 | 2025-01-13 14:01:00 | [diff] [blame] | 1481 | # From https://en.cppreference.com/w/cpp/header/functional |
| 1482 | 'equal_to', |
| 1483 | 'not_equal_to', |
| 1484 | 'greater', |
| 1485 | 'less', |
| 1486 | 'greater_equal', |
| 1487 | 'less_equal', |
danakj | 91c715b | 2024-07-10 13:24:26 | [diff] [blame] | 1488 | # From https://en.cppreference.com/w/cpp/iterator |
| 1489 | 'advance', |
| 1490 | 'distance', |
| 1491 | 'next', |
| 1492 | 'prev', |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1493 | )) + r')\w+', |
| 1494 | explanation=( |
| 1495 | 'Use of range views and associated helpers is banned in Chrome. ' |
| 1496 | 'If you need this functionality, please contact [email protected].', |
| 1497 | ), |
| 1498 | treat_as_error=True, |
| 1499 | excluded_paths=[ |
| 1500 | # Don't warn in third_party folders. |
| 1501 | _THIRD_PARTY_EXCEPT_BLINK |
| 1502 | ], |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1503 | ), |
| 1504 | BanRule( |
Peter Kasting | 31879d8 | 2024-10-07 20:18:39 | [diff] [blame] | 1505 | r'/#include <regex>', |
| 1506 | ('<regex> is not allowed. Use third_party/re2 instead.', |
| 1507 | ), |
| 1508 | True, |
| 1509 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 1510 | ), |
| 1511 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1512 | r'/#include <source_location>', |
| 1513 | ('<source_location> is not yet allowed. Use base/location.h instead.', |
| 1514 | ), |
| 1515 | True, |
| 1516 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1517 | ), |
| 1518 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1519 | r'/\bstd::to_address\b', |
| 1520 | ( |
| 1521 | 'std::to_address is banned because it is not guaranteed to be', |
| 1522 | 'SFINAE-compatible. Use base::to_address from base/types/to_address.h', |
| 1523 | 'instead.', |
| 1524 | ), |
| 1525 | True, |
| 1526 | [ |
| 1527 | # Needed in base::to_address implementation. |
| 1528 | r'base/types/to_address.h', |
| 1529 | _THIRD_PARTY_EXCEPT_BLINK |
| 1530 | ], # Not an error in third_party folders. |
Nick Diego Yamane | e522ae8 | 2024-02-27 04:23:22 | [diff] [blame] | 1531 | ), |
| 1532 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1533 | r'/#include <syncstream>', |
| 1534 | ('<syncstream> is banned.', ), |
| 1535 | True, |
| 1536 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1537 | ), |
| 1538 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1539 | r'/\bRunMessageLoop\b', |
| 1540 | ('RunMessageLoop is deprecated, use RunLoop instead.', ), |
| 1541 | False, |
| 1542 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1543 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1544 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1545 | 'RunAllPendingInMessageLoop()', |
| 1546 | ( |
| 1547 | "Prefer RunLoop over RunAllPendingInMessageLoop, please contact gab@", |
| 1548 | "if you're convinced you need this.", |
| 1549 | ), |
| 1550 | False, |
| 1551 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1552 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1553 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1554 | 'RunAllPendingInMessageLoop(BrowserThread', |
| 1555 | ( |
| 1556 | 'RunAllPendingInMessageLoop is deprecated. Use RunLoop for', |
| 1557 | 'BrowserThread::UI, BrowserTaskEnvironment::RunIOThreadUntilIdle', |
| 1558 | 'for BrowserThread::IO, and prefer RunLoop::QuitClosure to observe', |
| 1559 | 'async events instead of flushing threads.', |
| 1560 | ), |
| 1561 | False, |
| 1562 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1563 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1564 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1565 | r'MessageLoopRunner', |
| 1566 | ('MessageLoopRunner is deprecated, use RunLoop instead.', ), |
| 1567 | False, |
| 1568 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1569 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1570 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1571 | 'GetDeferredQuitTaskForRunLoop', |
| 1572 | ( |
| 1573 | "GetDeferredQuitTaskForRunLoop shouldn't be needed, please contact", |
| 1574 | "gab@ if you found a use case where this is the only solution.", |
| 1575 | ), |
| 1576 | False, |
| 1577 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1578 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1579 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1580 | 'sqlite3_initialize(', |
| 1581 | ( |
| 1582 | 'Instead of calling sqlite3_initialize(), depend on //sql, ', |
| 1583 | '#include "sql/initialize.h" and use sql::EnsureSqliteInitialized().', |
| 1584 | ), |
| 1585 | True, |
| 1586 | ( |
| 1587 | r'^sql/initialization\.(cc|h)$', |
| 1588 | r'^third_party/sqlite/.*\.(c|cc|h)$', |
| 1589 | ), |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 1590 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1591 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1592 | 'CREATE VIEW', |
| 1593 | ( |
| 1594 | 'SQL views are disabled in Chromium feature code', |
| 1595 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/sql#no-views', |
| 1596 | ), |
| 1597 | True, |
| 1598 | ( |
| 1599 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1600 | # sql/ itself uses views when using memory-mapped IO. |
| 1601 | r'^sql/.*', |
| 1602 | # Various performance tools that do not build as part of Chrome. |
| 1603 | r'^infra/.*', |
| 1604 | r'^tools/perf.*', |
| 1605 | r'.*perfetto.*', |
| 1606 | ), |
Austin Sullivan | d661ab5 | 2022-11-16 08:55:15 | [diff] [blame] | 1607 | ), |
| 1608 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1609 | 'CREATE VIRTUAL TABLE', |
| 1610 | ( |
| 1611 | 'SQL virtual tables are disabled in Chromium feature code', |
| 1612 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/sql#no-virtual-tables', |
| 1613 | ), |
| 1614 | True, |
| 1615 | ( |
| 1616 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1617 | # sql/ itself uses virtual tables in the recovery module and tests. |
| 1618 | r'^sql/.*', |
| 1619 | # TODO(https://crbug.com/695592): Remove once WebSQL is deprecated. |
| 1620 | r'third_party/blink/web_tests/storage/websql/.*' |
| 1621 | # Various performance tools that do not build as part of Chrome. |
| 1622 | r'^tools/perf.*', |
| 1623 | r'.*perfetto.*', |
| 1624 | ), |
Austin Sullivan | d661ab5 | 2022-11-16 08:55:15 | [diff] [blame] | 1625 | ), |
| 1626 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1627 | 'std::random_shuffle', |
| 1628 | ('std::random_shuffle is deprecated in C++14, and removed in C++17. Use', |
| 1629 | 'base::RandomShuffle instead.'), |
| 1630 | True, |
| 1631 | (), |
tzik | 5de2157f | 2018-05-08 03:42:47 | [diff] [blame] | 1632 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1633 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1634 | 'ios/web/public/test/http_server', |
| 1635 | ('web::HTTPserver is deprecated use net::EmbeddedTestServer instead.', |
| 1636 | ), |
| 1637 | False, |
| 1638 | (), |
Javier Ernesto Flores Robles | 749e6c2 | 2018-10-08 09:36:24 | [diff] [blame] | 1639 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1640 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1641 | 'GetAddressOf', |
| 1642 | ('Improper use of Microsoft::WRL::ComPtr<T>::GetAddressOf() has been ', |
| 1643 | 'implicated in a few leaks. ReleaseAndGetAddressOf() is safe but ', |
| 1644 | 'operator& is generally recommended. So always use operator& instead. ', |
| 1645 | 'See http://crbug.com/914910 for more conversion guidance.'), |
| 1646 | True, |
| 1647 | (), |
Robert Liao | 764c949 | 2019-01-24 18:46:28 | [diff] [blame] | 1648 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1649 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1650 | 'SHFileOperation', |
| 1651 | ('SHFileOperation was deprecated in Windows Vista, and there are less ', |
| 1652 | 'complex functions to achieve the same goals. Use IFileOperation for ', |
| 1653 | 'any esoteric actions instead.'), |
| 1654 | True, |
| 1655 | (), |
Ben Lewis | a951460 | 2019-04-29 17:53:05 | [diff] [blame] | 1656 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1657 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1658 | 'StringFromGUID2', |
| 1659 | ('StringFromGUID2 introduces an unnecessary dependency on ole32.dll.', |
| 1660 | 'Use base::win::WStringFromGUID instead.'), |
| 1661 | True, |
| 1662 | (r'/base/win/win_util_unittest.cc', ), |
Cliff Smolinsky | 8195164 | 2019-04-30 21:39:51 | [diff] [blame] | 1663 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1664 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1665 | 'StringFromCLSID', |
| 1666 | ('StringFromCLSID introduces an unnecessary dependency on ole32.dll.', |
| 1667 | 'Use base::win::WStringFromGUID instead.'), |
| 1668 | True, |
| 1669 | (r'/base/win/win_util_unittest.cc', ), |
Cliff Smolinsky | 8195164 | 2019-04-30 21:39:51 | [diff] [blame] | 1670 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1671 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1672 | 'kCFAllocatorNull', |
| 1673 | ( |
| 1674 | 'The use of kCFAllocatorNull with the NoCopy creation of ', |
| 1675 | 'CoreFoundation types is prohibited.', |
| 1676 | ), |
| 1677 | True, |
| 1678 | (), |
Avi Drissman | 7382afa0 | 2019-04-29 23:27:13 | [diff] [blame] | 1679 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1680 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1681 | 'mojo::ConvertTo', |
| 1682 | ('mojo::ConvertTo and TypeConverter are deprecated. Please consider', |
| 1683 | 'StructTraits / UnionTraits / EnumTraits / ArrayTraits / MapTraits /', |
| 1684 | 'StringTraits if you would like to convert between custom types and', |
| 1685 | 'the wire format of mojom types.'), |
| 1686 | False, |
| 1687 | ( |
| 1688 | r'^fuchsia_web/webengine/browser/url_request_rewrite_rules_manager\.cc$', |
| 1689 | r'^fuchsia_web/webengine/url_request_rewrite_type_converters\.cc$', |
| 1690 | r'^third_party/blink/.*\.(cc|h)$', |
| 1691 | r'^content/renderer/.*\.(cc|h)$', |
| 1692 | ), |
Oksana Zhuravlova | fd24777 | 2019-05-16 16:57:29 | [diff] [blame] | 1693 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1694 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1695 | 'GetInterfaceProvider', |
| 1696 | ('InterfaceProvider is deprecated.', |
| 1697 | 'Please use ExecutionContext::GetBrowserInterfaceBroker and overrides', |
| 1698 | 'or Platform::GetBrowserInterfaceBroker.'), |
| 1699 | False, |
| 1700 | (), |
Oksana Zhuravlova | c8222d2 | 2019-12-19 19:21:16 | [diff] [blame] | 1701 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1702 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1703 | 'CComPtr', |
| 1704 | ('New code should use Microsoft::WRL::ComPtr from wrl/client.h as a ', |
| 1705 | 'replacement for CComPtr from ATL. See http://crbug.com/5027 for more ', |
| 1706 | 'details.'), |
| 1707 | False, |
| 1708 | (), |
Robert Liao | 1d78df5 | 2019-11-11 20:02:01 | [diff] [blame] | 1709 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1710 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1711 | r'/\b(IFACE|STD)METHOD_?\(', |
| 1712 | ('IFACEMETHOD() and STDMETHOD() make code harder to format and read.', |
| 1713 | 'Instead, always use IFACEMETHODIMP in the declaration.'), |
| 1714 | False, |
| 1715 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Xiaohan Wang | 72bd2ba | 2020-02-18 21:38:20 | [diff] [blame] | 1716 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1717 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1718 | 'RemoveAllChildViewsWithoutDeleting', |
| 1719 | ('RemoveAllChildViewsWithoutDeleting is deprecated.', |
| 1720 | 'This method is deemed dangerous as, unless raw pointers are re-added,', |
| 1721 | 'calls to this method introduce memory leaks.'), |
| 1722 | False, |
| 1723 | (), |
Peter Boström | 7ff4152 | 2021-07-29 03:43:27 | [diff] [blame] | 1724 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1725 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1726 | r'/\bTRACE_EVENT_ASYNC_', |
| 1727 | ( |
| 1728 | 'Please use TRACE_EVENT_NESTABLE_ASYNC_.. macros instead', |
| 1729 | 'of TRACE_EVENT_ASYNC_.. (crbug.com/1038710).', |
| 1730 | ), |
| 1731 | False, |
| 1732 | ( |
| 1733 | r'^base/trace_event/.*', |
| 1734 | r'^base/tracing/.*', |
| 1735 | ), |
Eric Seckler | be6f48d | 2020-05-06 18:09:12 | [diff] [blame] | 1736 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1737 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1738 | 'RoInitialize', |
| 1739 | ('Improper use of [base::win]::RoInitialize() has been implicated in a ', |
| 1740 | 'few COM initialization leaks. Use base::win::ScopedWinrtInitializer ', |
| 1741 | 'instead. See http://crbug.com/1197722 for more information.'), |
| 1742 | True, |
| 1743 | ( |
| 1744 | r'^base/win/scoped_winrt_initializer\.cc$', |
| 1745 | r'^third_party/abseil-cpp/absl/.*', |
| 1746 | ), |
Robert Liao | 22f66a5 | 2021-04-10 00:57:52 | [diff] [blame] | 1747 | ), |
Patrick Monette | c343bb98 | 2022-06-01 17:18:45 | [diff] [blame] | 1748 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1749 | r'base::Watchdog', |
| 1750 | ( |
| 1751 | 'base::Watchdog is deprecated because it creates its own thread.', |
| 1752 | 'Instead, manually start a timer on a SequencedTaskRunner.', |
| 1753 | ), |
| 1754 | False, |
| 1755 | (), |
Patrick Monette | c343bb98 | 2022-06-01 17:18:45 | [diff] [blame] | 1756 | ), |
Andrew Rayskiy | 04a51ce | 2022-06-07 11:47:09 | [diff] [blame] | 1757 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1758 | 'base::Passed', |
| 1759 | ('Do not use base::Passed. It is a legacy helper for capturing ', |
| 1760 | 'move-only types with base::BindRepeating, but invoking the ', |
| 1761 | 'resulting RepeatingCallback moves the captured value out of ', |
| 1762 | 'the callback storage, and subsequent invocations may pass the ', |
| 1763 | 'value in a valid but undefined state. Prefer base::BindOnce().', |
| 1764 | 'See http://crbug.com/1326449 for context.'), |
| 1765 | False, |
| 1766 | ( |
| 1767 | # False positive, but it is also fine to let bind internals reference |
| 1768 | # base::Passed. |
| 1769 | r'^base[\\/]functional[\\/]bind\.h', |
| 1770 | r'^base[\\/]functional[\\/]bind_internal\.h', |
| 1771 | ), |
Andrew Rayskiy | 04a51ce | 2022-06-07 11:47:09 | [diff] [blame] | 1772 | ), |
Daniel Cheng | 2248b33 | 2022-07-27 06:16:59 | [diff] [blame] | 1773 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1774 | r'base::Feature k', |
| 1775 | ('Please use BASE_DECLARE_FEATURE() or BASE_FEATURE() instead of ', |
| 1776 | 'directly declaring/defining features.'), |
| 1777 | True, |
| 1778 | [ |
| 1779 | # Implements BASE_DECLARE_FEATURE(). |
| 1780 | r'^base/feature_list\.h', |
| 1781 | ], |
Daniel Cheng | ba3bc2e | 2022-10-03 02:45:43 | [diff] [blame] | 1782 | ), |
Robert Ogden | 92101dcb | 2022-10-19 23:49:36 | [diff] [blame] | 1783 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1784 | r'/\bchartorune\b', |
| 1785 | ('chartorune is not memory-safe, unless you can guarantee the input ', |
| 1786 | 'string is always null-terminated. Otherwise, please use charntorune ', |
| 1787 | 'from libphonenumber instead.'), |
| 1788 | True, |
| 1789 | [ |
| 1790 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1791 | # Exceptions to this rule should have a fuzzer. |
| 1792 | ], |
Robert Ogden | 92101dcb | 2022-10-19 23:49:36 | [diff] [blame] | 1793 | ), |
Arthur Sonzogni | 1da65fa | 2023-03-27 16:01:52 | [diff] [blame] | 1794 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1795 | r'/\b#include "base/atomicops\.h"\b', |
| 1796 | ('Do not use base::subtle atomics, but std::atomic, which are simpler ' |
| 1797 | 'to use, have better understood, clearer and richer semantics, and are ' |
| 1798 | 'harder to mis-use. See details in base/atomicops.h.', ), |
| 1799 | False, |
| 1800 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Benoit Lize | 79cf059 | 2023-01-27 10:01:57 | [diff] [blame] | 1801 | ), |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1802 | BanRule(r'CrossThreadPersistent<', ( |
Arthur Sonzogni | 60348572 | 2023-04-07 10:22:52 | [diff] [blame] | 1803 | 'Do not use blink::CrossThreadPersistent, but ' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1804 | 'blink::CrossThreadHandle. It is harder to mis-use.', 'More info: ' |
Arthur Sonzogni | 60348572 | 2023-04-07 10:22:52 | [diff] [blame] | 1805 | 'https://docs.google.com/document/d/1GIT0ysdQ84sGhIo1r9EscF_fFt93lmNVM_q4vvHj2FQ/edit#heading=h.3e4d6y61tgs', |
| 1806 | 'Please contact platform-architecture-dev@ before adding new instances.' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1807 | ), False, []), |
| 1808 | BanRule(r'CrossThreadWeakPersistent<', ( |
Arthur Sonzogni | 60348572 | 2023-04-07 10:22:52 | [diff] [blame] | 1809 | 'Do not use blink::CrossThreadWeakPersistent, but ' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1810 | 'blink::CrossThreadWeakHandle. It is harder to mis-use.', 'More info: ' |
Arthur Sonzogni | 60348572 | 2023-04-07 10:22:52 | [diff] [blame] | 1811 | 'https://docs.google.com/document/d/1GIT0ysdQ84sGhIo1r9EscF_fFt93lmNVM_q4vvHj2FQ/edit#heading=h.3e4d6y61tgs', |
| 1812 | 'Please contact platform-architecture-dev@ before adding new instances.' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1813 | ), False, []), |
| 1814 | BanRule(r'objc/objc.h', ( |
Avi Drissman | 491617c | 2023-04-13 17:33:15 | [diff] [blame] | 1815 | 'Do not include <objc/objc.h>. It defines away ARC lifetime ' |
| 1816 | 'annotations, and is thus dangerous.', |
| 1817 | 'Please use the pimpl pattern; search for `ObjCStorage` for examples.', |
| 1818 | 'For further reading on how to safely mix C++ and Obj-C, see', |
| 1819 | 'https://chromium.googlesource.com/chromium/src/+/main/docs/mac/mixing_cpp_and_objc.md' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1820 | ), True, []), |
| 1821 | BanRule( |
| 1822 | r'/#include <filesystem>', |
| 1823 | ('libc++ <filesystem> is banned per the Google C++ styleguide.', ), |
| 1824 | True, |
| 1825 | # This fuzzing framework is a standalone open source project and |
| 1826 | # cannot rely on Chromium base. |
| 1827 | (r'third_party/centipede'), |
Avi Drissman | 491617c | 2023-04-13 17:33:15 | [diff] [blame] | 1828 | ), |
Grace Park | 8d59b54b | 2023-04-26 17:53:35 | [diff] [blame] | 1829 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1830 | r'TopDocument()', |
| 1831 | ('TopDocument() does not work correctly with out-of-process iframes. ' |
| 1832 | 'Please do not introduce new uses.', ), |
| 1833 | True, |
| 1834 | ( |
| 1835 | # TODO(crbug.com/617677): Remove all remaining uses. |
| 1836 | r'^third_party/blink/renderer/core/dom/document\.cc', |
| 1837 | r'^third_party/blink/renderer/core/dom/document\.h', |
| 1838 | r'^third_party/blink/renderer/core/dom/element\.cc', |
| 1839 | r'^third_party/blink/renderer/core/exported/web_disallow_transition_scope_test\.cc', |
| 1840 | r'^third_party/blink/renderer/core/exported/web_document_test\.cc', |
| 1841 | r'^third_party/blink/renderer/core/html/html_anchor_element\.cc', |
| 1842 | r'^third_party/blink/renderer/core/html/html_dialog_element\.cc', |
| 1843 | r'^third_party/blink/renderer/core/html/html_element\.cc', |
| 1844 | r'^third_party/blink/renderer/core/html/html_frame_owner_element\.cc', |
| 1845 | r'^third_party/blink/renderer/core/html/media/video_wake_lock\.cc', |
| 1846 | r'^third_party/blink/renderer/core/loader/anchor_element_interaction_tracker\.cc', |
| 1847 | r'^third_party/blink/renderer/core/page/scrolling/root_scroller_controller\.cc', |
| 1848 | r'^third_party/blink/renderer/core/page/scrolling/top_document_root_scroller_controller\.cc', |
| 1849 | r'^third_party/blink/renderer/core/page/scrolling/top_document_root_scroller_controller\.h', |
| 1850 | r'^third_party/blink/renderer/core/script/classic_pending_script\.cc', |
| 1851 | r'^third_party/blink/renderer/core/script/script_loader\.cc', |
| 1852 | ), |
Grace Park | 8d59b54b | 2023-04-26 17:53:35 | [diff] [blame] | 1853 | ), |
Daniel Cheng | 72153e0 | 2023-05-18 21:18:14 | [diff] [blame] | 1854 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1855 | pattern=r'base::raw_ptr<', |
| 1856 | explanation=('Do not use base::raw_ptr, use raw_ptr.', ), |
| 1857 | treat_as_error=True, |
| 1858 | excluded_paths=( |
| 1859 | '^base/', |
| 1860 | '^tools/', |
| 1861 | ), |
Daniel Cheng | 72153e0 | 2023-05-18 21:18:14 | [diff] [blame] | 1862 | ), |
Arthur Sonzogni | f0eea30 | 2023-08-18 19:20:31 | [diff] [blame] | 1863 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1864 | pattern=r'base:raw_ref<', |
| 1865 | explanation=('Do not use base::raw_ref, use raw_ref.', ), |
| 1866 | treat_as_error=True, |
| 1867 | excluded_paths=( |
| 1868 | '^base/', |
| 1869 | '^tools/', |
| 1870 | ), |
Arthur Sonzogni | f0eea30 | 2023-08-18 19:20:31 | [diff] [blame] | 1871 | ), |
| 1872 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1873 | pattern=r'/raw_ptr<[^;}]*\w{};', |
| 1874 | explanation=( |
| 1875 | 'Do not use {} for raw_ptr initialization, use = nullptr instead.', |
| 1876 | ), |
| 1877 | treat_as_error=True, |
| 1878 | excluded_paths=( |
| 1879 | '^base/', |
| 1880 | '^tools/', |
| 1881 | ), |
Arthur Sonzogni | f0eea30 | 2023-08-18 19:20:31 | [diff] [blame] | 1882 | ), |
Anton Maliev | 6675181 | 2023-08-24 16:28:13 | [diff] [blame] | 1883 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1884 | pattern=r'/#include "base/allocator/.*/raw_' |
| 1885 | r'(ptr|ptr_cast|ptr_exclusion|ref).h"', |
| 1886 | explanation=( |
| 1887 | 'Please include the corresponding facade headers:', |
| 1888 | '- #include "base/memory/raw_ptr.h"', |
| 1889 | '- #include "base/memory/raw_ptr_cast.h"', |
| 1890 | '- #include "base/memory/raw_ptr_exclusion.h"', |
| 1891 | '- #include "base/memory/raw_ref.h"', |
| 1892 | ), |
| 1893 | treat_as_error=True, |
| 1894 | excluded_paths=( |
| 1895 | '^base/', |
| 1896 | '^tools/', |
| 1897 | ), |
Tom Sepez | 41eb158d | 2023-09-12 16:16:22 | [diff] [blame] | 1898 | ), |
| 1899 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1900 | pattern=r'ContentSettingsType::COOKIES', |
| 1901 | explanation= |
| 1902 | ('Do not use ContentSettingsType::COOKIES to check whether cookies are ' |
| 1903 | 'supported in the provided context. Instead rely on the ' |
| 1904 | 'content_settings::CookieSettings API. If you are using ' |
| 1905 | 'ContentSettingsType::COOKIES to check the user preference setting ' |
| 1906 | 'specifically, disregard this warning.', ), |
| 1907 | treat_as_error=False, |
| 1908 | excluded_paths=( |
| 1909 | '^chrome/browser/ui/content_settings/', |
| 1910 | '^components/content_settings/', |
| 1911 | '^services/network/cookie_settings.cc', |
| 1912 | '.*test.cc', |
| 1913 | ), |
Arthur Sonzogni | 48c6aea2 | 2023-09-04 22:25:20 | [diff] [blame] | 1914 | ), |
| 1915 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1916 | pattern=r'ContentSettingsType::TRACKING_PROTECTION', |
| 1917 | explanation= |
| 1918 | ('Do not directly use ContentSettingsType::TRACKING_PROTECTION to check ' |
| 1919 | 'for tracking protection exceptions. Instead rely on the ' |
| 1920 | 'privacy_sandbox::TrackingProtectionSettings API.', ), |
| 1921 | treat_as_error=False, |
| 1922 | excluded_paths=( |
| 1923 | '^chrome/browser/ui/content_settings/', |
| 1924 | '^components/content_settings/', |
| 1925 | '^components/privacy_sandbox/tracking_protection_settings.cc', |
| 1926 | '.*test.cc', |
| 1927 | ), |
Anton Maliev | 6675181 | 2023-08-24 16:28:13 | [diff] [blame] | 1928 | ), |
Tom Anderson | cd52207 | 2023-10-03 00:52:35 | [diff] [blame] | 1929 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1930 | pattern=r'/\bg_signal_connect', |
| 1931 | explanation=('Use ScopedGSignal instead of g_signal_connect*()', ), |
| 1932 | treat_as_error=True, |
| 1933 | excluded_paths=('^ui/base/glib/scoped_gsignal.h', ), |
Michelle Abreo | 6b743782 | 2024-04-26 17:29:04 | [diff] [blame] | 1934 | ), |
| 1935 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1936 | pattern=r'features::kIsolatedWebApps', |
| 1937 | explanation=( |
| 1938 | 'Do not use `features::kIsolatedWebApps` directly to guard Isolated ', |
| 1939 | 'Web App code. ', |
Andrew Rayskiy | 7ae2b5d | 2025-02-28 16:59:38 | [diff] [blame] | 1940 | 'Use `content::AreIsolatedWebAppsEnabled()` in the browser process ' |
| 1941 | 'or check the `kEnableIsolatedWebAppsInRenderer` command line flag ' |
| 1942 | 'in the renderer process.', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1943 | ), |
| 1944 | treat_as_error=True, |
Andrew Rayskiy | 7ae2b5d | 2025-02-28 16:59:38 | [diff] [blame] | 1945 | excluded_paths=_TEST_CODE_EXCLUDED_PATHS + ( |
| 1946 | '^chrome/browser/about_flags.cc', |
| 1947 | '^chrome/browser/component_updater/iwa_key_distribution_component_installer.cc', |
| 1948 | '^chrome/browser/web_applications/isolated_web_apps/chrome_content_browser_client_isolated_web_apps_part.cc', |
| 1949 | '^chrome/browser/ui/startup/bad_flags_prompt.cc', |
| 1950 | '^content/shell/browser/shell_content_browser_client.cc', |
| 1951 | )), |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1952 | BanRule( |
| 1953 | pattern=r'features::kIsolatedWebAppDevMode', |
| 1954 | explanation=( |
| 1955 | 'Do not use `features::kIsolatedWebAppDevMode` directly to guard code ', |
| 1956 | 'related to Isolated Web App Developer Mode. ', |
| 1957 | 'Use `web_app::IsIwaDevModeEnabled()` instead.', |
| 1958 | ), |
| 1959 | treat_as_error=True, |
| 1960 | excluded_paths=_TEST_CODE_EXCLUDED_PATHS + ( |
| 1961 | '^chrome/browser/about_flags.cc', |
| 1962 | '^chrome/browser/web_applications/isolated_web_apps/isolated_web_app_features.cc', |
| 1963 | '^chrome/browser/ui/startup/bad_flags_prompt.cc', |
| 1964 | )), |
| 1965 | BanRule( |
| 1966 | pattern=r'features::kIsolatedWebAppUnmanagedInstall', |
| 1967 | explanation=( |
| 1968 | 'Do not use `features::kIsolatedWebAppUnmanagedInstall` directly to ', |
| 1969 | 'guard code related to unmanaged install flow for Isolated Web Apps. ', |
| 1970 | 'Use `web_app::IsIwaUnmanagedInstallEnabled()` instead.', |
| 1971 | ), |
| 1972 | treat_as_error=True, |
| 1973 | excluded_paths=_TEST_CODE_EXCLUDED_PATHS + ( |
| 1974 | '^chrome/browser/about_flags.cc', |
| 1975 | '^chrome/browser/web_applications/isolated_web_apps/isolated_web_app_features.cc', |
| 1976 | )), |
| 1977 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1978 | pattern='/(CUIAutomation|AccessibleObjectFromWindow)', |
| 1979 | explanation= |
| 1980 | ('Direct usage of UIAutomation or IAccessible2 in client code is ' |
| 1981 | 'discouraged in Chromium, as it is not an assistive technology and ' |
| 1982 | 'should not rely on accessibility APIs directly. These APIs can ' |
| 1983 | 'introduce significant performance overhead. However, if you believe ' |
| 1984 | 'your use case warrants an exception, please discuss it with an ' |
| 1985 | 'accessibility owner before proceeding. For more information on the ' |
| 1986 | 'performance implications, see https://docs.google.com/document/d/1jN4itpCe_bDXF0BhFaYwv4xVLsCWkL9eULdzjmLzkuk/edit#heading=h.pwth3nbwdub0.', |
| 1987 | ), |
| 1988 | treat_as_error=False, |
Andrew Rayskiy | cdd45e73 | 2024-03-20 14:32:39 | [diff] [blame] | 1989 | ), |
| 1990 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1991 | pattern=r'/WIDGET_OWNS_NATIVE_WIDGET|' |
| 1992 | r'NATIVE_WIDGET_OWNS_WIDGET', |
| 1993 | explanation= |
| 1994 | ('WIDGET_OWNS_NATIVE_WIDGET and NATIVE_WIDGET_OWNS_WIDGET are in the ' |
| 1995 | 'process of being deprecated. Consider using the new ' |
| 1996 | 'CLIENT_OWNS_WIDGET ownership model. Eventually, this will be the only ' |
| 1997 | 'available ownership model available and the associated enumeration' |
| 1998 | 'will be removed.', ), |
| 1999 | treat_as_error=False, |
Andrew Rayskiy | cdd45e73 | 2024-03-20 14:32:39 | [diff] [blame] | 2000 | ), |
| 2001 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 2002 | pattern='ProfileManager::GetLastUsedProfile', |
| 2003 | explanation= |
| 2004 | ('Most code should already be scoped to a Profile. Pass in a Profile* ' |
| 2005 | 'or retreive from an existing entity with a reference to the Profile ' |
| 2006 | '(e.g. WebContents).', ), |
| 2007 | treat_as_error=False, |
Arthur Sonzogni | 5cbd3e3 | 2024-02-08 17:51:32 | [diff] [blame] | 2008 | ), |
Helmut Januschka | b3f71ab5 | 2024-03-12 02:48:05 | [diff] [blame] | 2009 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 2010 | pattern=(r'/FindBrowserWithUiElementContext|' |
| 2011 | r'FindBrowserWithTab|' |
| 2012 | r'FindBrowserWithGroup|' |
| 2013 | r'FindTabbedBrowser|' |
| 2014 | r'FindAnyBrowser|' |
| 2015 | r'FindBrowserWithProfile|' |
Erik Chen | 5f02eb4c | 2024-08-23 06:30:44 | [diff] [blame] | 2016 | r'FindLastActive|' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 2017 | r'FindBrowserWithActiveWindow'), |
| 2018 | explanation= |
| 2019 | ('Most code should already be scoped to a Browser. Pass in a Browser* ' |
| 2020 | 'or retreive from an existing entity with a reference to the Browser.', |
| 2021 | ), |
| 2022 | treat_as_error=False, |
Helmut Januschka | b3f71ab5 | 2024-03-12 02:48:05 | [diff] [blame] | 2023 | ), |
| 2024 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 2025 | pattern='BrowserUserData', |
| 2026 | explanation= |
| 2027 | ('Do not use BrowserUserData to store state on a Browser instance. ' |
| 2028 | 'Instead use BrowserWindowFeatures. BrowserWindowFeatures is ' |
| 2029 | 'functionally identical but has two benefits: it does not force a ' |
| 2030 | 'dependency onto class Browser, and lifetime semantics are explicit ' |
| 2031 | 'rather than implicit. See BrowserUserData header file for more ' |
| 2032 | 'details.', ), |
| 2033 | treat_as_error=False, |
Mike Dougherty | ab1bdec | 2024-08-06 16:39:01 | [diff] [blame] | 2034 | excluded_paths=( |
| 2035 | # Exclude iOS as the iOS implementation of BrowserUserData is separate |
| 2036 | # and still in use. |
| 2037 | '^ios/', |
| 2038 | ), |
Erik Chen | 87358e8 | 2024-06-04 02:13:12 | [diff] [blame] | 2039 | ), |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 2040 | BanRule( |
Tom Sepez | d3272cd | 2025-02-21 19:11:31 | [diff] [blame] | 2041 | pattern=r'subspan(0u,', |
| 2042 | explanation= |
| 2043 | ('Prefer first(n) over subspan(0u, n) as it is shorter, and the ' |
| 2044 | 'compiler may have to emit a branch for the n == dynamic_extent ' |
| 2045 | 'case of subspan().', |
| 2046 | ), |
| 2047 | treat_as_error=False, |
| 2048 | ), |
| 2049 | BanRule( |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 2050 | pattern=r'UNSAFE_TODO(', |
| 2051 | explanation= |
| 2052 | ('Do not use UNSAFE_TODO() to write new unsafe code. Use only when ' |
Tom Sepez | a90f92b | 2024-08-15 16:01:35 | [diff] [blame] | 2053 | 'removing a pre-existing file-wide allow_unsafe_buffers pragma, or ' |
| 2054 | 'when incrementally converting code off of unsafe interfaces', |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 2055 | ), |
| 2056 | treat_as_error=False, |
| 2057 | ), |
| 2058 | BanRule( |
Claudio DeSouza | ad1d1f25 | 2025-04-22 23:38:58 | [diff] [blame] | 2059 | pattern='#pragma allow_unsafe_buffers', |
| 2060 | explanation= |
| 2061 | ('Do not use allow_unsafe_buffers to write new unsafe code. Use only ' |
| 2062 | 'when enabling unsafe buffers checks under a new uncovered path.', |
| 2063 | ), |
| 2064 | treat_as_error=False, |
| 2065 | ), |
| 2066 | BanRule( |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 2067 | pattern=r'UNSAFE_BUFFERS(', |
| 2068 | explanation= |
Tom Sepez | a90f92b | 2024-08-15 16:01:35 | [diff] [blame] | 2069 | ('Try to avoid using UNSAFE_BUFFERS() if at all possible. Otherwise, ' |
| 2070 | 'be sure to justify in a // SAFETY comment why other options are not ' |
| 2071 | 'available, and why the code is safe.', |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 2072 | ), |
| 2073 | treat_as_error=False, |
| 2074 | ), |
Erik Chen | d086ae0 | 2024-08-20 22:53:33 | [diff] [blame] | 2075 | BanRule( |
| 2076 | pattern='BrowserWithTestWindowTest', |
| 2077 | explanation= |
| 2078 | ('Do not use BrowserWithTestWindowTest. By instantiating an instance ' |
| 2079 | 'of class Browser, the test is no longer a unit test but is instead a ' |
| 2080 | 'browser test. The class BrowserWithTestWindowTest forces production ' |
| 2081 | 'logic to take on test-only conditionals, which is an anti-pattern. ' |
| 2082 | 'Features should be performing dependency injection rather than ' |
| 2083 | 'directly using class Browser. See ' |
mikt | 19226ff2 | 2024-08-27 05:28:21 | [diff] [blame] | 2084 | 'docs/chrome_browser_design_principles.md for more details.', |
Erik Chen | d086ae0 | 2024-08-20 22:53:33 | [diff] [blame] | 2085 | ), |
| 2086 | treat_as_error=False, |
| 2087 | ), |
Erik Chen | 8cf3a65 | 2024-08-23 17:13:30 | [diff] [blame] | 2088 | BanRule( |
Erik Chen | 959cdd7 | 2024-08-29 02:11:21 | [diff] [blame] | 2089 | pattern='TestWithBrowserView', |
| 2090 | explanation= |
| 2091 | ('Do not use TestWithBrowserView. See ' |
| 2092 | 'docs/chrome_browser_design_principles.md for details. If you want ' |
| 2093 | 'to write a test that has both a Browser and a BrowserView, create ' |
| 2094 | 'a browser_test. If you want to write a unit_test, your code must ' |
Erik Chen | dba2369 | 2024-09-26 06:43:36 | [diff] [blame] | 2095 | 'not reference Browser*.', |
Erik Chen | 959cdd7 | 2024-08-29 02:11:21 | [diff] [blame] | 2096 | ), |
| 2097 | treat_as_error=False, |
| 2098 | ), |
| 2099 | BanRule( |
Erik Chen | e89ebe3 | 2025-02-22 02:46:49 | [diff] [blame] | 2100 | pattern='CreateBrowserWithTestWindow', |
| 2101 | explanation= |
| 2102 | ('Do not use CreateBrowserWithTestWindow. See ' |
| 2103 | 'docs/chrome_browser_design_principles.md for details. If you want ' |
| 2104 | 'to write a test that has a Browser, create a browser_test. If you ' |
| 2105 | 'want to write a unit_test, your code must not reference Browser*.', |
| 2106 | ), |
| 2107 | treat_as_error=False, |
| 2108 | ), |
| 2109 | BanRule( |
Erik Chen | f12a0664 | 2025-03-13 23:30:34 | [diff] [blame] | 2110 | pattern='CreateBrowserWithTestWindowForParams', |
| 2111 | explanation= |
| 2112 | ('Do not use CreateBrowserWithTestWindowForParams. See ' |
| 2113 | 'docs/chrome_browser_design_principles.md for details. If you want ' |
| 2114 | 'to write a test that has a Browser, create a browser_test and use ' |
| 2115 | 'Browser::Browser. If you want to write a unit_test, your code must ' |
| 2116 | 'not reference Browser*.', |
| 2117 | ), |
| 2118 | treat_as_error=False, |
| 2119 | ), |
| 2120 | BanRule( |
Erik Chen | 8cf3a65 | 2024-08-23 17:13:30 | [diff] [blame] | 2121 | pattern='RunUntilIdle', |
| 2122 | explanation= |
| 2123 | ('Do not RunUntilIdle. If possible, explicitly quit the run loop using ' |
| 2124 | 'run_loop.Quit() or run_loop.QuitClosure() if completion can be ' |
| 2125 | 'observed using a lambda or callback. Otherwise, wait for the ' |
mikt | 19226ff2 | 2024-08-27 05:28:21 | [diff] [blame] | 2126 | 'condition to be true via base::test::RunUntil().', |
Erik Chen | 8cf3a65 | 2024-08-23 17:13:30 | [diff] [blame] | 2127 | ), |
| 2128 | treat_as_error=False, |
| 2129 | ), |
Daniel Cheng | ddde13a | 2024-09-05 21:39:28 | [diff] [blame] | 2130 | BanRule( |
| 2131 | pattern=r'/\bstd::(literals|string_literals|string_view_literals)\b', |
| 2132 | explanation = ( |
| 2133 | 'User-defined literals are banned by the Google C++ style guide. ' |
| 2134 | 'Exceptions are provided in Chrome for string and string_view ' |
| 2135 | 'literals that embed \\0.', |
| 2136 | ), |
| 2137 | treat_as_error=True, |
| 2138 | excluded_paths=( |
| 2139 | # Various tests or test helpers that embed NUL in strings or |
| 2140 | # string_views. |
Daniel Cheng | ddde13a | 2024-09-05 21:39:28 | [diff] [blame] | 2141 | r'^base/strings/string_util_unittest\.cc', |
| 2142 | r'^base/strings/utf_string_conversions_unittest\.cc', |
| 2143 | r'^chrome/browser/ash/crosapi/browser_data_back_migrator_unittest\.cc', |
| 2144 | r'^chrome/browser/ash/crosapi/browser_data_migrator_util_unittest\.cc', |
| 2145 | r'^chrome/browser/ash/crosapi/move_migrator_unittest\.cc', |
Hidehiko Abe | 5160181 | 2025-01-12 16:17:35 | [diff] [blame] | 2146 | r'^chromeos/ash/experiences/arc/session/serial_number_util_unittest\.cc', |
Daniel Cheng | ddde13a | 2024-09-05 21:39:28 | [diff] [blame] | 2147 | r'^components/history/core/browser/visit_annotations_database\.cc', |
| 2148 | r'^components/history/core/browser/visit_annotations_database_unittest\.cc', |
| 2149 | r'^components/os_crypt/sync/os_crypt_unittest\.cc', |
| 2150 | r'^components/password_manager/core/browser/credentials_cleaner_unittest\.cc', |
| 2151 | r'^content/browser/file_system_access/file_system_access_file_writer_impl_unittest\.cc', |
| 2152 | r'^net/cookies/parsed_cookie_unittest\.cc', |
| 2153 | r'^third_party/blink/renderer/modules/webcodecs/test_helpers\.cc', |
| 2154 | r'^third_party/blink/renderer/modules/websockets/websocket_channel_impl_test\.cc', |
| 2155 | ), |
Erik Chen | ba8b0cd3 | 2024-10-01 08:36:36 | [diff] [blame] | 2156 | ), |
| 2157 | BanRule( |
| 2158 | pattern='BUILDFLAG(GOOGLE_CHROME_BRANDING)', |
| 2159 | explanation= |
| 2160 | ('Code gated by GOOGLE_CHROME_BRANDING is effectively untested. This ' |
| 2161 | 'is typically wrong. Valid use cases are glue for private modules ' |
| 2162 | 'shipped alongside Chrome, and installation-related logic.', |
| 2163 | ), |
| 2164 | treat_as_error=False, |
| 2165 | ), |
| 2166 | BanRule( |
| 2167 | pattern='defined(OFFICIAL_BUILD)', |
| 2168 | explanation= |
| 2169 | ('Code gated by OFFICIAL_BUILD is effectively untested. This ' |
| 2170 | 'is typically wrong. One valid use case is low-level code that ' |
| 2171 | 'handles subtleties related to high-levels of optimizations that come ' |
| 2172 | 'with OFFICIAL_BUILD.', |
| 2173 | ), |
| 2174 | treat_as_error=False, |
| 2175 | ), |
Erik Chen | 95b9c78 | 2024-11-08 03:26:27 | [diff] [blame] | 2176 | BanRule( |
| 2177 | pattern='WebContentsDestroyed', |
| 2178 | explanation= |
| 2179 | ('Do not use this method. It is invoked half-way through the ' |
| 2180 | 'destructor of WebContentsImpl and using it often results in crashes ' |
| 2181 | 'or surprising behavior. Conceptually, this is only necessary by ' |
| 2182 | 'objects that depend on, but outlive the WebContents. These objects ' |
| 2183 | 'should instead coordinate with the owner of the WebContents which is ' |
| 2184 | 'responsible for destroying the WebContents.', |
| 2185 | ), |
| 2186 | treat_as_error=False, |
| 2187 | ), |
Maksim Sisov | c98fdfa | 2024-11-16 20:12:27 | [diff] [blame] | 2188 | BanRule( |
Georg Neis | a7f94e6 | 2025-02-28 07:01:48 | [diff] [blame] | 2189 | pattern='IS_CHROMEOS_ASH', |
Maksim Sisov | c98fdfa | 2024-11-16 20:12:27 | [diff] [blame] | 2190 | explanation= |
Georg Neis | a7f94e6 | 2025-02-28 07:01:48 | [diff] [blame] | 2191 | ('IS_CHROMEOS_ASH is deprecated. Please use the equivalent IS_CHROMEOS ' |
| 2192 | 'instead (Lacros is gone).', |
Maksim Sisov | c98fdfa | 2024-11-16 20:12:27 | [diff] [blame] | 2193 | ), |
| 2194 | treat_as_error=False, |
| 2195 | ), |
Erik Chen | 1396bbe | 2025-01-27 23:39:36 | [diff] [blame] | 2196 | BanRule( |
| 2197 | pattern=(r'namespace {'), |
| 2198 | explanation= |
| 2199 | ('Anonymous namespaces are disallowed in C++ header files. See ' |
| 2200 | 'https://google.github.io/styleguide/cppguide.html#Internal_Linkage ' |
| 2201 | ' for details.', |
| 2202 | ), |
| 2203 | treat_as_error=False, |
| 2204 | excluded_paths=[ |
| 2205 | _THIRD_PARTY_EXCEPT_BLINK, # Don't warn in third_party folders. |
| 2206 | r'^(?!.*\.h$).*$', # Exclude all files except those that end in .h |
| 2207 | ], |
| 2208 | ), |
Keren Zhu | f06d757d | 2025-03-04 05:32:36 | [diff] [blame] | 2209 | BanRule( |
| 2210 | pattern=('AddChildViewRaw'), |
| 2211 | explanation=( |
| 2212 | 'Do not use AddChildViewRaw. It is prone to memory leaks and ' |
| 2213 | 'use-after-free bugs. Instead, use AddChildView(std::unique_ptr). ' |
| 2214 | 'See https://crbug.com/40485510 for more details.', ), |
| 2215 | treat_as_error=False, |
| 2216 | ), |
Nate Fischer | d541ff8 | 2025-03-11 21:34:19 | [diff] [blame] | 2217 | BanRule( |
| 2218 | pattern=(r'IS_DESKTOP_ANDROID'), |
| 2219 | explanation=( |
| 2220 | 'Features which depend on IS_DESKTOP_ANDROID should only exist in ' |
| 2221 | 'chrome/ layer and similar layers. Lower layers such as content/ ' |
| 2222 | 'should not have features which are only designed for ' |
| 2223 | 'desktop-android builds. See https://crbug.com/401628399.', ), |
| 2224 | treat_as_error=False, |
| 2225 | excluded_paths=[ |
| 2226 | _THIRD_PARTY_EXCEPT_BLINK, # Don't warn in third_party folders. |
| 2227 | r'^build/', # This is permitted in build/ folder. |
| 2228 | r'^chrome/', # This is permitted in chrome/ folder. |
| 2229 | r'^components/', # This is permitted only for components/ that are not shared by WebView. |
| 2230 | r'^extensions/', # This is permitted in chrome/ folder. |
| 2231 | r'^infra/', # This is permitted in infra/ folder. |
| 2232 | r'^tools/', # This is permitted in tools/ folder. |
| 2233 | ], |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 2234 | surface_as_gerrit_lint=True, |
Nate Fischer | d541ff8 | 2025-03-11 21:34:19 | [diff] [blame] | 2235 | ), |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2236 | ) |
| 2237 | |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2238 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING = ( |
| 2239 | 'Used a predicate related to signin::ConsentLevel::kSync which will always ' |
| 2240 | 'return false in the future (crbug.com/40066949). Prefer using a predicate ' |
| 2241 | 'that also supports signin::ConsentLevel::kSignin when appropriate. It is ' |
| 2242 | 'safe to ignore this warning if you are just moving an existing call, or if ' |
| 2243 | 'you want special handling for users in the legacy state. In doubt, reach ' |
Victor Hugo Vianna Silva | e229297 | 2024-06-04 17:11:55 | [diff] [blame] | 2244 | 'out to //components/sync/OWNERS.', |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2245 | ) |
| 2246 | |
| 2247 | # C++ functions related to signin::ConsentLevel::kSync which are deprecated. |
| 2248 | _DEPRECATED_SYNC_CONSENT_CPP_FUNCTIONS : Sequence[BanRule] = ( |
| 2249 | BanRule( |
| 2250 | 'HasSyncConsent', |
| 2251 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2252 | False, |
| 2253 | ), |
| 2254 | BanRule( |
| 2255 | 'CanSyncFeatureStart', |
| 2256 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2257 | False, |
| 2258 | ), |
| 2259 | BanRule( |
| 2260 | 'IsSyncFeatureEnabled', |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2261 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2262 | False, |
| 2263 | ), |
| 2264 | BanRule( |
| 2265 | 'IsSyncFeatureActive', |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2266 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2267 | False, |
| 2268 | ), |
| 2269 | ) |
| 2270 | |
| 2271 | # Java functions related to signin::ConsentLevel::kSync which are deprecated. |
| 2272 | _DEPRECATED_SYNC_CONSENT_JAVA_FUNCTIONS : Sequence[BanRule] = ( |
| 2273 | BanRule( |
| 2274 | 'hasSyncConsent', |
| 2275 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2276 | False, |
| 2277 | ), |
| 2278 | BanRule( |
| 2279 | 'canSyncFeatureStart', |
| 2280 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2281 | False, |
| 2282 | ), |
| 2283 | BanRule( |
| 2284 | 'isSyncFeatureEnabled', |
| 2285 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2286 | False, |
| 2287 | ), |
| 2288 | BanRule( |
| 2289 | 'isSyncFeatureActive', |
| 2290 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2291 | False, |
| 2292 | ), |
| 2293 | ) |
| 2294 | |
Justin Lulejian | 09fd0687 | 2025-04-01 22:03:28 | [diff] [blame] | 2295 | _BANNED_MOJOM_PATTERNS: Sequence[BanRule] = ( |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 2296 | BanRule( |
| 2297 | 'handle<shared_buffer>', |
| 2298 | ( |
Justin Lulejian | 09fd0687 | 2025-04-01 22:03:28 | [diff] [blame] | 2299 | 'Please use one of the more specific shared memory types instead:', |
| 2300 | ' mojo_base.mojom.ReadOnlySharedMemoryRegion', |
| 2301 | ' mojo_base.mojom.WritableSharedMemoryRegion', |
| 2302 | ' mojo_base.mojom.UnsafeSharedMemoryRegion', |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 2303 | ), |
| 2304 | True, |
| 2305 | ), |
Justin Lulejian | 09fd0687 | 2025-04-01 22:03:28 | [diff] [blame] | 2306 | BanRule( |
| 2307 | 'string extension_id', |
| 2308 | ( |
| 2309 | 'Please use the extensions::mojom::ExtensionId struct when ' |
| 2310 | 'passing extensions::ExtensionIds as mojom messages in order to ', |
| 2311 | 'provide message validation.', |
| 2312 | ), |
| 2313 | True, |
| 2314 | # Only apply this to (mojom) files in a subdirectory of extensions. |
| 2315 | excluded_paths=(r'^((?!extensions/).)*$', ), |
| 2316 | ), |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 2317 | ) |
| 2318 | |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 2319 | _IPC_ENUM_TRAITS_DEPRECATED = ( |
| 2320 | '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] | 2321 | 'See http://www.chromium.org/Home/chromium-security/education/' |
| 2322 | 'security-tips-for-ipc') |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 2323 | |
Stephen Martinis | 97a39414 | 2018-06-07 23:06:05 | [diff] [blame] | 2324 | _LONG_PATH_ERROR = ( |
| 2325 | 'Some files included in this CL have file names that are too long (> 200' |
| 2326 | ' characters). If committed, these files will cause issues on Windows. See' |
| 2327 | ' https://crbug.com/612667 for more details.' |
| 2328 | ) |
| 2329 | |
Shenghua Zhang | bfaa38b8 | 2017-11-16 21:58:02 | [diff] [blame] | 2330 | _JAVA_MULTIPLE_DEFINITION_EXCLUDED_PATHS = [ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2331 | r".*/BuildHooksAndroidImpl\.java", |
| 2332 | r".*/LicenseContentProvider\.java", |
| 2333 | r".*/PlatformServiceBridgeImpl.java", |
| 2334 | r".*chrome/android/feed/dummy/.*\.java", |
Shenghua Zhang | bfaa38b8 | 2017-11-16 21:58:02 | [diff] [blame] | 2335 | ] |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2336 | |
Mohamed Heikal | d048240a | 2019-11-12 16:57:37 | [diff] [blame] | 2337 | # List of image extensions that are used as resources in chromium. |
| 2338 | _IMAGE_EXTENSIONS = ['.svg', '.png', '.webp'] |
| 2339 | |
Sean Kau | 46e29bc | 2017-08-28 16:31:16 | [diff] [blame] | 2340 | # These paths contain test data and other known invalid JSON files. |
Erik Staab | 2dd72b1 | 2020-04-16 15:03:40 | [diff] [blame] | 2341 | _KNOWN_TEST_DATA_AND_INVALID_JSON_FILE_PATTERNS = [ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2342 | r'test/data/', |
| 2343 | r'testing/buildbot/', |
| 2344 | r'^components/policy/resources/policy_templates\.json$', |
| 2345 | r'^third_party/protobuf/', |
Camillo Bruni | 1411a35 | 2023-05-24 12:39:03 | [diff] [blame] | 2346 | r'^third_party/blink/perf_tests/speedometer.*/resources/todomvc/learn\.json', |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2347 | r'^third_party/blink/renderer/devtools/protocol\.json$', |
| 2348 | r'^third_party/blink/web_tests/external/wpt/', |
| 2349 | r'^tools/perf/', |
| 2350 | r'^tools/traceline/svgui/startup-release.json', |
Daniel Cheng | 2d4c2d19 | 2022-07-01 01:38:31 | [diff] [blame] | 2351 | # vscode configuration files allow comments |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2352 | r'^tools/vscode/', |
Sean Kau | 46e29bc | 2017-08-28 16:31:16 | [diff] [blame] | 2353 | ] |
| 2354 | |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2355 | # These are not checked on the public chromium-presubmit trybot. |
| 2356 | # 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] | 2357 | # checkouts. |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 2358 | _ANDROID_SPECIFIC_PYDEPS_FILES = [ |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2359 | 'chrome/android/features/create_stripped_java_factory.pydeps', |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2360 | ] |
| 2361 | |
| 2362 | |
| 2363 | _GENERIC_PYDEPS_FILES = [ |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2364 | 'android_webview/tools/run_cts.pydeps', |
Andrew Grieve | 4c4cede | 2020-11-20 22:09:36 | [diff] [blame] | 2365 | 'build/android/apk_operations.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2366 | 'build/android/devil_chromium.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2367 | 'build/android/gyp/aar.pydeps', |
| 2368 | 'build/android/gyp/aidl.pydeps', |
| 2369 | 'build/android/gyp/apkbuilder.pydeps', |
Andrew Grieve | a417ad30 | 2019-02-06 19:54:38 | [diff] [blame] | 2370 | 'build/android/gyp/assert_static_initializers.pydeps', |
Mohamed Heikal | 133e1f2 | 2023-04-18 20:04:37 | [diff] [blame] | 2371 | 'build/android/gyp/binary_baseline_profile.pydeps', |
Robbie McElrath | 360e54d | 2020-11-12 20:38:02 | [diff] [blame] | 2372 | 'build/android/gyp/bytecode_rewriter.pydeps', |
Mohamed Heikal | 6305bcc | 2021-03-15 15:34:22 | [diff] [blame] | 2373 | 'build/android/gyp/check_flag_expectations.pydeps', |
Andrew Grieve | acac424 | 2024-12-20 19:39:42 | [diff] [blame] | 2374 | 'build/android/gyp/check_for_missing_direct_deps.pydeps', |
Andrew Grieve | 8d083ea | 2019-12-13 06:49:11 | [diff] [blame] | 2375 | 'build/android/gyp/compile_java.pydeps', |
Peter Wen | eaa963f | 2023-01-20 19:40:30 | [diff] [blame] | 2376 | 'build/android/gyp/compile_kt.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2377 | 'build/android/gyp/compile_resources.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2378 | 'build/android/gyp/copy_ex.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2379 | 'build/android/gyp/create_apk_operations_script.pydeps', |
Andrew Grieve | 8d083ea | 2019-12-13 06:49:11 | [diff] [blame] | 2380 | 'build/android/gyp/create_app_bundle.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2381 | 'build/android/gyp/create_app_bundle_apks.pydeps', |
| 2382 | 'build/android/gyp/create_bundle_wrapper_script.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2383 | 'build/android/gyp/create_java_binary_script.pydeps', |
Mohamed Heikal | adbe4e48 | 2020-07-09 19:25:12 | [diff] [blame] | 2384 | 'build/android/gyp/create_r_java.pydeps', |
Mohamed Heikal | 8cd763a5 | 2021-02-01 23:32:09 | [diff] [blame] | 2385 | 'build/android/gyp/create_r_txt.pydeps', |
Andrew Grieve | b838d83 | 2019-02-11 16:55:22 | [diff] [blame] | 2386 | 'build/android/gyp/create_size_info_files.pydeps', |
Andrew Grieve | 2d972e5f | 2025-01-28 18:28:14 | [diff] [blame] | 2387 | 'build/android/gyp/create_stub_manifest.pydeps', |
Peter Wen | e6e017e | 2022-07-27 21:40:40 | [diff] [blame] | 2388 | 'build/android/gyp/create_test_apk_wrapper_script.pydeps', |
Andrew Grieve | 5a01ad3 | 2020-06-25 18:06:00 | [diff] [blame] | 2389 | 'build/android/gyp/create_ui_locale_resources.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2390 | 'build/android/gyp/dex.pydeps', |
| 2391 | 'build/android/gyp/dist_aar.pydeps', |
Andrew Grieve | 651ddb3 | 2025-01-23 03:27:34 | [diff] [blame] | 2392 | 'build/android/gyp/errorprone.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2393 | 'build/android/gyp/filter_zip.pydeps', |
Mohamed Heikal | 21e1994b | 2021-11-12 21:37:21 | [diff] [blame] | 2394 | 'build/android/gyp/flatc_java.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2395 | 'build/android/gyp/gcc_preprocess.pydeps', |
Christopher Grant | 99e0e2006 | 2018-11-21 21:22:36 | [diff] [blame] | 2396 | 'build/android/gyp/generate_linker_version_script.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2397 | 'build/android/gyp/ijar.pydeps', |
Yun Liu | eb4075ddf | 2019-05-13 19:47:58 | [diff] [blame] | 2398 | 'build/android/gyp/jacoco_instr.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2399 | 'build/android/gyp/java_cpp_enum.pydeps', |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 2400 | 'build/android/gyp/java_cpp_features.pydeps', |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 2401 | 'build/android/gyp/java_cpp_strings.pydeps', |
Andrew Grieve | 0945791 | 2021-04-27 15:22:47 | [diff] [blame] | 2402 | 'build/android/gyp/java_google_api_keys.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2403 | 'build/android/gyp/jinja_template.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2404 | 'build/android/gyp/lint.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2405 | 'build/android/gyp/merge_manifest.pydeps', |
Bruce Dawson | 853b739e6 | 2022-05-03 23:03:10 | [diff] [blame] | 2406 | 'build/android/gyp/optimize_resources.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2407 | 'build/android/gyp/prepare_resources.pydeps', |
Mohamed Heikal | f85138b | 2020-10-06 15:43:22 | [diff] [blame] | 2408 | 'build/android/gyp/process_native_prebuilt.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2409 | 'build/android/gyp/proguard.pydeps', |
Mohamed Heikal | dd52b45 | 2024-09-10 17:10:50 | [diff] [blame] | 2410 | 'build/android/gyp/rename_java_classes.pydeps', |
Andrew Grieve | e3a775ab | 2022-05-16 15:59:22 | [diff] [blame] | 2411 | 'build/android/gyp/system_image_apks.pydeps', |
Bruce Dawson | 853b739e6 | 2022-05-03 23:03:10 | [diff] [blame] | 2412 | 'build/android/gyp/trace_event_bytecode_rewriter.pydeps', |
Andrew Grieve | 170b978 | 2025-02-03 15:54:53 | [diff] [blame] | 2413 | 'build/android/gyp/tracereferences.pydeps', |
Peter Wen | 578730b | 2020-03-19 19:55:46 | [diff] [blame] | 2414 | 'build/android/gyp/turbine.pydeps', |
Mohamed Heikal | 246710c | 2021-06-14 15:34:30 | [diff] [blame] | 2415 | 'build/android/gyp/unused_resources.pydeps', |
Eric Stevenson | a82cf608 | 2019-07-24 14:35:24 | [diff] [blame] | 2416 | 'build/android/gyp/validate_static_library_dex_references.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2417 | 'build/android/gyp/write_build_config.pydeps', |
Tibor Goldschwendt | c4caae9 | 2019-07-12 00:33:46 | [diff] [blame] | 2418 | 'build/android/gyp/write_native_libraries_java.pydeps', |
Andrew Grieve | 9ff1779 | 2018-11-30 04:55:56 | [diff] [blame] | 2419 | 'build/android/gyp/zip.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2420 | 'build/android/incremental_install/generate_android_manifest.pydeps', |
| 2421 | 'build/android/incremental_install/write_installer_json.pydeps', |
Stephanie Kim | 392913b45 | 2022-06-15 17:25:32 | [diff] [blame] | 2422 | 'build/android/pylib/results/presentation/test_results_presentation.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2423 | 'build/android/resource_sizes.pydeps', |
| 2424 | 'build/android/test_runner.pydeps', |
| 2425 | 'build/android/test_wrapper/logdog_wrapper.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2426 | 'build/protoc_java.pydeps', |
Peter Kotwicz | 64667b0 | 2020-10-18 06:43:32 | [diff] [blame] | 2427 | 'chrome/android/monochrome/scripts/monochrome_python_tests.pydeps', |
Peter Wen | efb56c7 | 2020-06-04 15:12:27 | [diff] [blame] | 2428 | 'chrome/test/chromedriver/log_replay/client_replay_unittest.pydeps', |
| 2429 | 'chrome/test/chromedriver/test/run_py_tests.pydeps', |
Bailey Myers-Morgan | bd12213 | 2025-03-26 23:09:16 | [diff] [blame] | 2430 | 'chrome/test/media_router/performance/performance_test.pydeps', |
Junbo Ke | dcd3a45 | 2021-03-19 17:55:04 | [diff] [blame] | 2431 | 'chromecast/resource_sizes/chromecast_resource_sizes.pydeps', |
Mohannad Farrag | 1910274 | 2023-12-01 01:16:30 | [diff] [blame] | 2432 | 'components/cronet/tools/check_combined_proguard_file.pydeps', |
| 2433 | 'components/cronet/tools/generate_proguard_file.pydeps', |
Andrew Grieve | 5a01ad3 | 2020-06-25 18:06:00 | [diff] [blame] | 2434 | 'components/cronet/tools/jar_src.pydeps', |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2435 | 'components/module_installer/android/module_desc_java.pydeps', |
Andrew Grieve | 5a01ad3 | 2020-06-25 18:06:00 | [diff] [blame] | 2436 | 'content/public/android/generate_child_service.pydeps', |
Hzj_jie | 77bdb80 | 2024-07-22 18:14:51 | [diff] [blame] | 2437 | 'fuchsia_web/av_testing/av_sync_tests.pydeps', |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2438 | 'net/tools/testserver/testserver.pydeps', |
Peter Kotwicz | 3c339f3 | 2020-10-19 19:59:18 | [diff] [blame] | 2439 | 'testing/scripts/run_isolated_script_test.pydeps', |
Stephanie Kim | c94072c | 2022-03-22 22:31:41 | [diff] [blame] | 2440 | 'testing/merge_scripts/standard_isolated_script_merge.pydeps', |
| 2441 | 'testing/merge_scripts/standard_gtest_merge.pydeps', |
| 2442 | 'testing/merge_scripts/code_coverage/merge_results.pydeps', |
| 2443 | 'testing/merge_scripts/code_coverage/merge_steps.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2444 | 'third_party/android_platform/development/scripts/stack.pydeps', |
Hitoshi Yoshida | 0f228c4 | 2019-08-07 09:37:42 | [diff] [blame] | 2445 | 'third_party/blink/renderer/bindings/scripts/build_web_idl_database.pydeps', |
Yuki Shiino | 38eeaad1 | 2022-08-11 06:40:25 | [diff] [blame] | 2446 | 'third_party/blink/renderer/bindings/scripts/check_generated_file_list.pydeps', |
Hitoshi Yoshida | 0f228c4 | 2019-08-07 09:37:42 | [diff] [blame] | 2447 | 'third_party/blink/renderer/bindings/scripts/collect_idl_files.pydeps', |
Yuki Shiino | e7827aa | 2019-09-13 12:26:13 | [diff] [blame] | 2448 | 'third_party/blink/renderer/bindings/scripts/generate_bindings.pydeps', |
Yuki Shiino | ea477d3 | 2023-08-21 06:24:34 | [diff] [blame] | 2449 | 'third_party/blink/renderer/bindings/scripts/generate_event_interface_names.pydeps', |
Canon Mukai | f32f8f59 | 2021-04-23 18:56:50 | [diff] [blame] | 2450 | 'third_party/blink/renderer/bindings/scripts/validate_web_idl.pydeps', |
Stephanie Kim | c94072c | 2022-03-22 22:31:41 | [diff] [blame] | 2451 | 'third_party/blink/tools/blinkpy/web_tests/merge_results.pydeps', |
| 2452 | 'third_party/blink/tools/merge_web_test_results.pydeps', |
John Budorick | bc3571aa | 2019-04-25 02:20:06 | [diff] [blame] | 2453 | 'tools/binary_size/sizes.pydeps', |
Andrew Grieve | a7f1ee90 | 2018-05-18 16:17:22 | [diff] [blame] | 2454 | 'tools/binary_size/supersize.pydeps', |
Peter Wen | 2dcfa6e | 2025-03-04 22:42:52 | [diff] [blame] | 2455 | 'tools/cygprofile/generate_orderfile.pydeps', |
Ben Pastene | 028104a | 2022-08-10 19:17:45 | [diff] [blame] | 2456 | 'tools/perf/process_perf_results.pydeps', |
Peter Wen | ce103e1 | 2024-10-09 19:23:51 | [diff] [blame] | 2457 | 'tools/pgo/generate_profile.pydeps', |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 2458 | ] |
| 2459 | |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2460 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 2461 | _ALL_PYDEPS_FILES = _ANDROID_SPECIFIC_PYDEPS_FILES + _GENERIC_PYDEPS_FILES |
| 2462 | |
| 2463 | |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 2464 | # Bypass the AUTHORS check for these accounts. |
| 2465 | _KNOWN_ROBOTS = set( |
Shuai Xia | 0d99ebf | 2025-02-11 23:47:59 | [diff] [blame] | 2466 | ) | set('%[email protected]' % s for s in ('findit-for-me', |
| 2467 | 'luci-bisection', |
| 2468 | 'predator-for-me-staging', |
| 2469 | 'predator-for-me') |
Achuith Bhandarkar | 3590556 | 2018-07-25 19:28:45 | [diff] [blame] | 2470 | ) | set('%[email protected]' % s for s in ('3su6n15k.default',) |
Sergiy Byelozyorov | 47158a5 | 2018-06-13 22:38:59 | [diff] [blame] | 2471 | ) | set('%[email protected]' % s |
smut | de79705 | 2019-12-04 02:03:52 | [diff] [blame] | 2472 | for s in ('bling-autoroll-builder', 'v8-ci-autoroll-builder', |
Sven Zheng | f7abd31d | 2021-08-09 19:06:23 | [diff] [blame] | 2473 | 'wpt-autoroller', 'chrome-weblayer-builder', |
Georg Neis | e5817eb | 2025-02-06 03:47:31 | [diff] [blame] | 2474 | 'skylab-test-cros-roller', 'infra-try-recipes-tester', |
| 2475 | 'chrome-automated-expectation', |
Stephanie Kim | b49bdd24 | 2023-04-28 16:46:04 | [diff] [blame] | 2476 | 'chromium-automated-expectation', 'chrome-branch-day', |
Gennady Tsitovich | 554fd42 | 2025-04-02 21:03:18 | [diff] [blame] | 2477 | 'chrome-cherry-picker', 'chromium-autosharder') |
Eric Boren | 835d71f | 2018-09-07 21:09:04 | [diff] [blame] | 2478 | ) | set('%[email protected]' % s |
Eric Boren | 66150e5 | 2020-01-08 11:20:27 | [diff] [blame] | 2479 | for s in ('chromium-autoroll', 'chromium-release-autoroll') |
Eric Boren | 835d71f | 2018-09-07 21:09:04 | [diff] [blame] | 2480 | ) | set('%[email protected]' % s |
Yulan Lin | eb0cfba | 2021-04-09 18:43:16 | [diff] [blame] | 2481 | for s in ('chromium-internal-autoroll',) |
Kyungjun Lee | 3b7c935 | 2024-04-02 23:59:14 | [diff] [blame] | 2482 | ) | set('%[email protected]' % s |
Shuai Xia | 174d35c | 2025-04-15 17:10:31 | [diff] [blame] | 2483 | for s in ('chrome-screen-ai-releaser', 'crash-eng', 'crash') |
Yulan Lin | eb0cfba | 2021-04-09 18:43:16 | [diff] [blame] | 2484 | ) | set('%[email protected]' % s |
Chong Gu | b277e34 | 2022-10-15 03:30:55 | [diff] [blame] | 2485 | for s in ('swarming-tasks',) |
| 2486 | ) | set('%[email protected]' % s |
| 2487 | for s in ('global-integration-try-builder', |
Joey Scarr | 1103c5d | 2023-09-14 01:17:55 | [diff] [blame] | 2488 | 'global-integration-ci-builder') |
Suma Kasa | 3b9cf7a | 2023-09-21 22:05:54 | [diff] [blame] | 2489 | ) | set('%[email protected]' % s |
| 2490 | for s in ('chops-security-borg', |
| 2491 | 'chops-security-cronjobs-cpesuggest')) |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 2492 | |
Matt Stark | 6ef0887 | 2021-07-29 01:21:46 | [diff] [blame] | 2493 | _INVALID_GRD_FILE_LINE = [ |
| 2494 | (r'<file lang=.* path=.*', 'Path should come before lang in GRD files.') |
| 2495 | ] |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 2496 | |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2497 | def _IsCPlusPlusFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2498 | """Returns True if this file contains C++-like code (and not Python, |
| 2499 | Go, Java, MarkDown, ...)""" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2500 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2501 | ext = input_api.os_path.splitext(file_path)[1] |
| 2502 | # This list is compatible with CppChecker.IsCppFile but we should |
| 2503 | # consider adding ".c" to it. If we do that we can use this function |
| 2504 | # at more places in the code. |
| 2505 | return ext in ( |
| 2506 | '.h', |
| 2507 | '.cc', |
| 2508 | '.cpp', |
| 2509 | '.m', |
| 2510 | '.mm', |
| 2511 | ) |
| 2512 | |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2513 | |
| 2514 | def _IsCPlusPlusHeaderFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2515 | return input_api.os_path.splitext(file_path)[1] == ".h" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2516 | |
| 2517 | |
| 2518 | def _IsJavaFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2519 | return input_api.os_path.splitext(file_path)[1] == ".java" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2520 | |
| 2521 | |
| 2522 | def _IsProtoFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2523 | return input_api.os_path.splitext(file_path)[1] == ".proto" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2524 | |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2525 | |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 2526 | def _IsXmlOrGrdFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2527 | ext = input_api.os_path.splitext(file_path)[1] |
| 2528 | return ext in ('.grd', '.xml') |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 2529 | |
| 2530 | |
Sven Zheng | 76a79ea | 2022-12-21 21:25:24 | [diff] [blame] | 2531 | def _IsMojomFile(input_api, file_path): |
| 2532 | return input_api.os_path.splitext(file_path)[1] == ".mojom" |
| 2533 | |
| 2534 | |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2535 | def CheckNoUpstreamDepsOnClank(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2536 | """Prevent additions of dependencies from the upstream repo on //clank.""" |
| 2537 | # clank can depend on clank |
| 2538 | if input_api.change.RepositoryRoot().endswith('clank'): |
| 2539 | return [] |
| 2540 | build_file_patterns = [ |
| 2541 | r'(.+/)?BUILD\.gn', |
| 2542 | r'.+\.gni', |
| 2543 | ] |
| 2544 | excluded_files = [r'build[/\\]config[/\\]android[/\\]config\.gni'] |
| 2545 | bad_pattern = input_api.re.compile(r'^[^#]*//clank') |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2546 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2547 | error_message = 'Disallowed import on //clank in an upstream build file:' |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2548 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2549 | def FilterFile(affected_file): |
| 2550 | return input_api.FilterSourceFile(affected_file, |
| 2551 | files_to_check=build_file_patterns, |
| 2552 | files_to_skip=excluded_files) |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2553 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2554 | problems = [] |
| 2555 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 2556 | local_path = f.LocalPath() |
| 2557 | for line_number, line in f.ChangedContents(): |
| 2558 | if (bad_pattern.search(line)): |
| 2559 | problems.append('%s:%d\n %s' % |
| 2560 | (local_path, line_number, line.strip())) |
| 2561 | if problems: |
| 2562 | return [output_api.PresubmitPromptOrNotify(error_message, problems)] |
| 2563 | else: |
| 2564 | return [] |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2565 | |
| 2566 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2567 | def CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2568 | """Attempts to prevent use of functions intended only for testing in |
| 2569 | non-testing code. For now this is just a best-effort implementation |
| 2570 | that ignores header files and may have some false positives. A |
| 2571 | better implementation would probably need a proper C++ parser. |
| 2572 | """ |
| 2573 | # We only scan .cc files and the like, as the declaration of |
| 2574 | # for-testing functions in header files are hard to distinguish from |
| 2575 | # calls to such functions without a proper C++ parser. |
| 2576 | file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS] |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2577 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2578 | base_function_pattern = r'[ :]test::[^\s]+|ForTest(s|ing)?|for_test(s|ing)?' |
| 2579 | inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % |
| 2580 | base_function_pattern) |
| 2581 | comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern) |
| 2582 | allowlist_pattern = input_api.re.compile(r'// IN-TEST$') |
| 2583 | exclusion_pattern = input_api.re.compile( |
| 2584 | r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % |
| 2585 | (base_function_pattern, base_function_pattern)) |
| 2586 | # Avoid a false positive in this case, where the method name, the ::, and |
| 2587 | # the closing { are all on different lines due to line wrapping. |
| 2588 | # HelperClassForTesting:: |
| 2589 | # HelperClassForTesting( |
| 2590 | # args) |
| 2591 | # : member(0) {} |
| 2592 | method_defn_pattern = input_api.re.compile(r'[A-Za-z0-9_]+::$') |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2593 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2594 | def FilterFile(affected_file): |
| 2595 | files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 2596 | input_api.DEFAULT_FILES_TO_SKIP) |
| 2597 | return input_api.FilterSourceFile( |
| 2598 | affected_file, |
| 2599 | files_to_check=file_inclusion_pattern, |
| 2600 | files_to_skip=files_to_skip) |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2601 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2602 | problems = [] |
| 2603 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 2604 | local_path = f.LocalPath() |
| 2605 | in_method_defn = False |
| 2606 | for line_number, line in f.ChangedContents(): |
| 2607 | if (inclusion_pattern.search(line) |
| 2608 | and not comment_pattern.search(line) |
| 2609 | and not exclusion_pattern.search(line) |
| 2610 | and not allowlist_pattern.search(line) |
| 2611 | and not in_method_defn): |
| 2612 | problems.append('%s:%d\n %s' % |
| 2613 | (local_path, line_number, line.strip())) |
| 2614 | in_method_defn = method_defn_pattern.search(line) |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2615 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2616 | if problems: |
| 2617 | return [ |
| 2618 | output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems) |
| 2619 | ] |
| 2620 | else: |
| 2621 | return [] |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2622 | |
| 2623 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2624 | def CheckNoProductionCodeUsingTestOnlyFunctionsJava(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2625 | """This is a simplified version of |
| 2626 | CheckNoProductionCodeUsingTestOnlyFunctions for Java files. |
| 2627 | """ |
| 2628 | javadoc_start_re = input_api.re.compile(r'^\s*/\*\*') |
| 2629 | javadoc_end_re = input_api.re.compile(r'^\s*\*/') |
| 2630 | name_pattern = r'ForTest(s|ing)?' |
| 2631 | # Describes an occurrence of "ForTest*" inside a // comment. |
| 2632 | comment_re = input_api.re.compile(r'//.*%s' % name_pattern) |
| 2633 | # Describes @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED) |
| 2634 | annotation_re = input_api.re.compile(r'@VisibleForTesting\(') |
| 2635 | # Catch calls. |
| 2636 | inclusion_re = input_api.re.compile(r'(%s)\s*\(' % name_pattern) |
| 2637 | # Ignore definitions. (Comments are ignored separately.) |
| 2638 | exclusion_re = input_api.re.compile(r'(%s)[^;]+\{' % name_pattern) |
Andrew Grieve | 40f451d | 2023-07-06 19:46:51 | [diff] [blame] | 2639 | allowlist_re = input_api.re.compile(r'// IN-TEST$') |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 2640 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2641 | problems = [] |
| 2642 | sources = lambda x: input_api.FilterSourceFile( |
| 2643 | x, |
| 2644 | files_to_skip=(('(?i).*test', r'.*\/junit\/') + input_api. |
| 2645 | DEFAULT_FILES_TO_SKIP), |
| 2646 | files_to_check=[r'.*\.java$']) |
| 2647 | for f in input_api.AffectedFiles(include_deletes=False, |
| 2648 | file_filter=sources): |
| 2649 | local_path = f.LocalPath() |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 2650 | is_inside_javadoc = False |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2651 | for line_number, line in f.ChangedContents(): |
| 2652 | if is_inside_javadoc and javadoc_end_re.search(line): |
| 2653 | is_inside_javadoc = False |
| 2654 | if not is_inside_javadoc and javadoc_start_re.search(line): |
| 2655 | is_inside_javadoc = True |
| 2656 | if is_inside_javadoc: |
| 2657 | continue |
| 2658 | if (inclusion_re.search(line) and not comment_re.search(line) |
| 2659 | and not annotation_re.search(line) |
Andrew Grieve | 40f451d | 2023-07-06 19:46:51 | [diff] [blame] | 2660 | and not allowlist_re.search(line) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2661 | and not exclusion_re.search(line)): |
| 2662 | problems.append('%s:%d\n %s' % |
| 2663 | (local_path, line_number, line.strip())) |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 2664 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2665 | if problems: |
| 2666 | return [ |
| 2667 | output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems) |
| 2668 | ] |
| 2669 | else: |
| 2670 | return [] |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 2671 | |
| 2672 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2673 | def CheckNoIOStreamInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2674 | """Checks to make sure no .h files include <iostream>.""" |
| 2675 | files = [] |
| 2676 | pattern = input_api.re.compile(r'^#include\s*<iostream>', |
| 2677 | input_api.re.MULTILINE) |
| 2678 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 2679 | if not f.LocalPath().endswith('.h'): |
| 2680 | continue |
| 2681 | contents = input_api.ReadFile(f) |
| 2682 | if pattern.search(contents): |
| 2683 | files.append(f) |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 2684 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2685 | if len(files): |
| 2686 | return [ |
| 2687 | output_api.PresubmitError( |
| 2688 | 'Do not #include <iostream> in header files, since it inserts static ' |
| 2689 | 'initialization into every file including the header. Instead, ' |
| 2690 | '#include <ostream>. See http://crbug.com/94794', files) |
| 2691 | ] |
| 2692 | return [] |
| 2693 | |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 2694 | |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 2695 | def CheckNoStrCatRedefines(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2696 | """Checks no windows headers with StrCat redefined are included directly.""" |
| 2697 | files = [] |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 2698 | files_to_check = (r'.+%s' % _HEADER_EXTENSIONS, |
| 2699 | r'.+%s' % _IMPLEMENTATION_EXTENSIONS) |
| 2700 | files_to_skip = (input_api.DEFAULT_FILES_TO_SKIP + |
| 2701 | _NON_BASE_DEPENDENT_PATHS) |
| 2702 | sources_filter = lambda f: input_api.FilterSourceFile( |
| 2703 | f, files_to_check=files_to_check, files_to_skip=files_to_skip) |
| 2704 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2705 | pattern_deny = input_api.re.compile( |
| 2706 | r'^#include\s*[<"](shlwapi|atlbase|propvarutil|sphelper).h[">]', |
| 2707 | input_api.re.MULTILINE) |
| 2708 | pattern_allow = input_api.re.compile( |
| 2709 | r'^#include\s"base/win/windows_defines.inc"', input_api.re.MULTILINE) |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 2710 | for f in input_api.AffectedSourceFiles(sources_filter): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2711 | contents = input_api.ReadFile(f) |
| 2712 | if pattern_deny.search( |
| 2713 | contents) and not pattern_allow.search(contents): |
| 2714 | files.append(f.LocalPath()) |
Danil Chapovalov | 3518f36 | 2018-08-11 16:13:43 | [diff] [blame] | 2715 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2716 | if len(files): |
| 2717 | return [ |
| 2718 | output_api.PresubmitError( |
| 2719 | 'Do not #include shlwapi.h, atlbase.h, propvarutil.h or sphelper.h ' |
| 2720 | 'directly since they pollute code with StrCat macro. Instead, ' |
| 2721 | 'include matching header from base/win. See http://crbug.com/856536', |
| 2722 | files) |
| 2723 | ] |
| 2724 | return [] |
Danil Chapovalov | 3518f36 | 2018-08-11 16:13:43 | [diff] [blame] | 2725 | |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 2726 | |
Andrew Williams | c9f69b48 | 2023-07-10 16:07:36 | [diff] [blame] | 2727 | def _CheckNoUNIT_TESTInSourceFiles(input_api, f): |
| 2728 | problems = [] |
| 2729 | |
| 2730 | unit_test_macro = input_api.re.compile( |
Riley Wong | 49be8a88 | 2025-02-27 00:38:23 | [diff] [blame] | 2731 | r'^\s*#.*(?:ifn?def\s+UNIT_TEST|defined\s*\(?\s*UNIT_TEST\s*\)?)(?:$|\s+)') |
Andrew Williams | c9f69b48 | 2023-07-10 16:07:36 | [diff] [blame] | 2732 | for line_num, line in f.ChangedContents(): |
| 2733 | if unit_test_macro.match(line): |
| 2734 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 2735 | |
| 2736 | return problems |
| 2737 | |
| 2738 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2739 | def CheckNoUNIT_TESTInSourceFiles(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2740 | """Checks to make sure no source files use UNIT_TEST.""" |
| 2741 | problems = [] |
| 2742 | for f in input_api.AffectedFiles(): |
| 2743 | if (not f.LocalPath().endswith(('.cc', '.mm'))): |
| 2744 | continue |
Andrew Williams | c9f69b48 | 2023-07-10 16:07:36 | [diff] [blame] | 2745 | problems.extend( |
| 2746 | _CheckNoUNIT_TESTInSourceFiles(input_api, f)) |
[email protected] | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 2747 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2748 | if not problems: |
| 2749 | return [] |
| 2750 | return [ |
| 2751 | output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' + |
| 2752 | '\n'.join(problems)) |
| 2753 | ] |
| 2754 | |
[email protected] | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 2755 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2756 | def CheckNoDISABLETypoInTests(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2757 | """Checks to prevent attempts to disable tests with DISABLE_ prefix. |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2758 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2759 | This test warns if somebody tries to disable a test with the DISABLE_ prefix |
| 2760 | instead of DISABLED_. To filter false positives, reports are only generated |
| 2761 | if a corresponding MAYBE_ line exists. |
| 2762 | """ |
| 2763 | problems = [] |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2764 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2765 | # The following two patterns are looked for in tandem - is a test labeled |
| 2766 | # as MAYBE_ followed by a DISABLE_ (instead of the correct DISABLED) |
| 2767 | maybe_pattern = input_api.re.compile(r'MAYBE_([a-zA-Z0-9_]+)') |
| 2768 | disable_pattern = input_api.re.compile(r'DISABLE_([a-zA-Z0-9_]+)') |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2769 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2770 | # This is for the case that a test is disabled on all platforms. |
| 2771 | full_disable_pattern = input_api.re.compile( |
| 2772 | r'^\s*TEST[^(]*\([a-zA-Z0-9_]+,\s*DISABLE_[a-zA-Z0-9_]+\)', |
| 2773 | input_api.re.MULTILINE) |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2774 | |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 2775 | for f in input_api.AffectedFiles(include_deletes=False): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2776 | if not 'test' in f.LocalPath() or not f.LocalPath().endswith('.cc'): |
| 2777 | continue |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2778 | |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 2779 | # Search for MAYBE_, DISABLE_ pairs. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2780 | disable_lines = {} # Maps of test name to line number. |
| 2781 | maybe_lines = {} |
| 2782 | for line_num, line in f.ChangedContents(): |
| 2783 | disable_match = disable_pattern.search(line) |
| 2784 | if disable_match: |
| 2785 | disable_lines[disable_match.group(1)] = line_num |
| 2786 | maybe_match = maybe_pattern.search(line) |
| 2787 | if maybe_match: |
| 2788 | maybe_lines[maybe_match.group(1)] = line_num |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2789 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2790 | # Search for DISABLE_ occurrences within a TEST() macro. |
| 2791 | disable_tests = set(disable_lines.keys()) |
| 2792 | maybe_tests = set(maybe_lines.keys()) |
| 2793 | for test in disable_tests.intersection(maybe_tests): |
| 2794 | problems.append(' %s:%d' % (f.LocalPath(), disable_lines[test])) |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2795 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2796 | contents = input_api.ReadFile(f) |
| 2797 | full_disable_match = full_disable_pattern.search(contents) |
| 2798 | if full_disable_match: |
| 2799 | problems.append(' %s' % f.LocalPath()) |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2800 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2801 | if not problems: |
| 2802 | return [] |
| 2803 | return [ |
| 2804 | output_api.PresubmitPromptWarning( |
| 2805 | 'Attempt to disable a test with DISABLE_ instead of DISABLED_?\n' + |
| 2806 | '\n'.join(problems)) |
| 2807 | ] |
| 2808 | |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2809 | |
Nina Satragno | f766053 | 2021-09-20 18:03:35 | [diff] [blame] | 2810 | def CheckForgettingMAYBEInTests(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2811 | """Checks to make sure tests disabled conditionally are not missing a |
| 2812 | corresponding MAYBE_ prefix. |
| 2813 | """ |
| 2814 | # Expect at least a lowercase character in the test name. This helps rule out |
| 2815 | # false positives with macros wrapping the actual tests name. |
| 2816 | define_maybe_pattern = input_api.re.compile( |
| 2817 | r'^\#define MAYBE_(?P<test_name>\w*[a-z]\w*)') |
Bruce Dawson | ffc5529 | 2022-04-20 04:18:19 | [diff] [blame] | 2818 | # The test_maybe_pattern needs to handle all of these forms. The standard: |
| 2819 | # IN_PROC_TEST_F(SyncTest, MAYBE_Start) { |
| 2820 | # With a wrapper macro around the test name: |
| 2821 | # IN_PROC_TEST_F(SyncTest, E2E_ENABLED(MAYBE_Start)) { |
| 2822 | # And the odd-ball NACL_BROWSER_TEST_f format: |
| 2823 | # NACL_BROWSER_TEST_F(NaClBrowserTest, SimpleLoad, { |
| 2824 | # The optional E2E_ENABLED-style is handled with (\w*\()? |
| 2825 | # The NACL_BROWSER_TEST_F pattern is handled by allowing a trailing comma or |
| 2826 | # trailing ')'. |
| 2827 | test_maybe_pattern = ( |
| 2828 | r'^\s*\w*TEST[^(]*\(\s*\w+,\s*(\w*\()?MAYBE_{test_name}[\),]') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2829 | suite_maybe_pattern = r'^\s*\w*TEST[^(]*\(\s*MAYBE_{test_name}[\),]' |
| 2830 | warnings = [] |
Nina Satragno | f766053 | 2021-09-20 18:03:35 | [diff] [blame] | 2831 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2832 | # Read the entire files. We can't just read the affected lines, forgetting to |
| 2833 | # add MAYBE_ on a change would not show up otherwise. |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 2834 | for f in input_api.AffectedFiles(include_deletes=False): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2835 | if not 'test' in f.LocalPath() or not f.LocalPath().endswith('.cc'): |
| 2836 | continue |
| 2837 | contents = input_api.ReadFile(f) |
| 2838 | lines = contents.splitlines(True) |
| 2839 | current_position = 0 |
| 2840 | warning_test_names = set() |
| 2841 | for line_num, line in enumerate(lines, start=1): |
| 2842 | current_position += len(line) |
| 2843 | maybe_match = define_maybe_pattern.search(line) |
| 2844 | if maybe_match: |
| 2845 | test_name = maybe_match.group('test_name') |
| 2846 | # Do not warn twice for the same test. |
| 2847 | if (test_name in warning_test_names): |
| 2848 | continue |
| 2849 | warning_test_names.add(test_name) |
Nina Satragno | f766053 | 2021-09-20 18:03:35 | [diff] [blame] | 2850 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2851 | # Attempt to find the corresponding MAYBE_ test or suite, starting from |
| 2852 | # the current position. |
| 2853 | test_match = input_api.re.compile( |
| 2854 | test_maybe_pattern.format(test_name=test_name), |
| 2855 | input_api.re.MULTILINE).search(contents, current_position) |
| 2856 | suite_match = input_api.re.compile( |
| 2857 | suite_maybe_pattern.format(test_name=test_name), |
| 2858 | input_api.re.MULTILINE).search(contents, current_position) |
| 2859 | if not test_match and not suite_match: |
| 2860 | warnings.append( |
| 2861 | output_api.PresubmitPromptWarning( |
| 2862 | '%s:%d found MAYBE_ defined without corresponding test %s' |
| 2863 | % (f.LocalPath(), line_num, test_name))) |
| 2864 | return warnings |
| 2865 | |
[email protected] | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 2866 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2867 | def CheckDCHECK_IS_ONHasBraces(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2868 | """Checks to make sure DCHECK_IS_ON() does not skip the parentheses.""" |
| 2869 | errors = [] |
Kalvin Lee | 4a3b79de | 2022-05-26 16:00:16 | [diff] [blame] | 2870 | pattern = input_api.re.compile(r'\bDCHECK_IS_ON\b(?!\(\))', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2871 | input_api.re.MULTILINE) |
| 2872 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 2873 | if (not f.LocalPath().endswith(('.cc', '.mm', '.h'))): |
| 2874 | continue |
| 2875 | for lnum, line in f.ChangedContents(): |
| 2876 | if input_api.re.search(pattern, line): |
| 2877 | errors.append( |
| 2878 | output_api.PresubmitError(( |
| 2879 | '%s:%d: Use of DCHECK_IS_ON() must be written as "#if ' |
| 2880 | + 'DCHECK_IS_ON()", not forgetting the parentheses.') % |
| 2881 | (f.LocalPath(), lnum))) |
| 2882 | return errors |
danakj | 61c1aa2 | 2015-10-26 19:55:52 | [diff] [blame] | 2883 | |
| 2884 | |
Weilun Shi | a487fad | 2020-10-28 00:10:34 | [diff] [blame] | 2885 | # TODO(crbug/1138055): Reimplement CheckUmaHistogramChangesOnUpload check in a |
| 2886 | # more reliable way. See |
| 2887 | # https://chromium-review.googlesource.com/c/chromium/src/+/2500269 |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 2888 | |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2889 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2890 | def CheckFlakyTestUsage(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2891 | """Check that FlakyTest annotation is our own instead of the android one""" |
| 2892 | pattern = input_api.re.compile(r'import android.test.FlakyTest;') |
| 2893 | files = [] |
| 2894 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 2895 | if f.LocalPath().endswith('Test.java'): |
| 2896 | if pattern.search(input_api.ReadFile(f)): |
| 2897 | files.append(f) |
| 2898 | if len(files): |
| 2899 | return [ |
| 2900 | output_api.PresubmitError( |
| 2901 | 'Use org.chromium.base.test.util.FlakyTest instead of ' |
| 2902 | 'android.test.FlakyTest', files) |
| 2903 | ] |
| 2904 | return [] |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 2905 | |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2906 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2907 | def CheckNoDEPSGIT(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2908 | """Make sure .DEPS.git is never modified manually.""" |
| 2909 | if any(f.LocalPath().endswith('.DEPS.git') |
| 2910 | for f in input_api.AffectedFiles()): |
| 2911 | return [ |
| 2912 | output_api.PresubmitError( |
| 2913 | 'Never commit changes to .DEPS.git. This file is maintained by an\n' |
| 2914 | 'automated system based on what\'s in DEPS and your changes will be\n' |
| 2915 | 'overwritten.\n' |
| 2916 | 'See https://sites.google.com/a/chromium.org/dev/developers/how-tos/' |
| 2917 | 'get-the-code#Rolling_DEPS\n' |
| 2918 | 'for more information') |
| 2919 | ] |
| 2920 | return [] |
[email protected] | 2a8ac9c | 2011-10-19 17:20:44 | [diff] [blame] | 2921 | |
| 2922 | |
Sven Zheng | 76a79ea | 2022-12-21 21:25:24 | [diff] [blame] | 2923 | def CheckCrosApiNeedBrowserTest(input_api, output_api): |
| 2924 | """Check new crosapi should add browser test.""" |
| 2925 | has_new_crosapi = False |
| 2926 | has_browser_test = False |
| 2927 | for f in input_api.AffectedFiles(): |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 2928 | path = f.UnixLocalPath() |
Sven Zheng | 76a79ea | 2022-12-21 21:25:24 | [diff] [blame] | 2929 | if (path.startswith('chromeos/crosapi/mojom') and |
| 2930 | _IsMojomFile(input_api, path) and f.Action() == 'A'): |
| 2931 | has_new_crosapi = True |
| 2932 | if path.endswith('browsertest.cc') or path.endswith('browser_test.cc'): |
| 2933 | has_browser_test = True |
| 2934 | if has_new_crosapi and not has_browser_test: |
| 2935 | return [ |
| 2936 | output_api.PresubmitPromptWarning( |
| 2937 | 'You are adding a new crosapi, but there is no file ends with ' |
| 2938 | 'browsertest.cc file being added or modified. It is important ' |
| 2939 | 'to add crosapi browser test coverage to avoid version ' |
| 2940 | ' skew issues.\n' |
| 2941 | 'Check //docs/lacros/test_instructions.md for more information.' |
| 2942 | ) |
| 2943 | ] |
| 2944 | return [] |
| 2945 | |
| 2946 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2947 | def CheckValidHostsInDEPSOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2948 | """Checks that DEPS file deps are from allowed_hosts.""" |
| 2949 | # Run only if DEPS file has been modified to annoy fewer bystanders. |
| 2950 | if all(f.LocalPath() != 'DEPS' for f in input_api.AffectedFiles()): |
| 2951 | return [] |
| 2952 | # Outsource work to gclient verify |
| 2953 | try: |
| 2954 | gclient_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 2955 | 'third_party', 'depot_tools', |
| 2956 | 'gclient.py') |
| 2957 | input_api.subprocess.check_output( |
Bruce Dawson | 8a43cf7 | 2022-05-13 17:10:32 | [diff] [blame] | 2958 | [input_api.python3_executable, gclient_path, 'verify'], |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2959 | stderr=input_api.subprocess.STDOUT) |
| 2960 | return [] |
| 2961 | except input_api.subprocess.CalledProcessError as error: |
| 2962 | return [ |
| 2963 | output_api.PresubmitError( |
| 2964 | 'DEPS file must have only git dependencies.', |
| 2965 | long_text=error.output) |
| 2966 | ] |
tandrii | ef66469 | 2014-09-23 14:51:47 | [diff] [blame] | 2967 | |
| 2968 | |
Mario Sanchez Prada | 2472cab | 2019-09-18 10:58:31 | [diff] [blame] | 2969 | def _GetMessageForMatchingType(input_api, affected_file, line_number, line, |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2970 | ban_rule): |
Allen Bauer | 8477868 | 2022-09-22 16:28:56 | [diff] [blame] | 2971 | """Helper method for checking for banned constructs. |
Mario Sanchez Prada | 2472cab | 2019-09-18 10:58:31 | [diff] [blame] | 2972 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2973 | Returns an string composed of the name of the file, the line number where the |
| 2974 | match has been found and the additional text passed as |message| in case the |
| 2975 | target type name matches the text inside the line passed as parameter. |
| 2976 | """ |
| 2977 | result = [] |
Peng Huang | 9c5949a0 | 2020-06-11 19:20:54 | [diff] [blame] | 2978 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2979 | # Ignore comments about banned types. |
| 2980 | if input_api.re.search(r"^ *//", line): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2981 | return result |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2982 | # A // nocheck comment will bypass this error. |
| 2983 | if line.endswith(" nocheck"): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2984 | return result |
| 2985 | |
| 2986 | matched = False |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2987 | if ban_rule.pattern[0:1] == '/': |
| 2988 | regex = ban_rule.pattern[1:] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2989 | if input_api.re.search(regex, line): |
| 2990 | matched = True |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2991 | elif ban_rule.pattern in line: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2992 | matched = True |
| 2993 | |
| 2994 | if matched: |
| 2995 | result.append(' %s:%d:' % (affected_file.LocalPath(), line_number)) |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2996 | for line in ban_rule.explanation: |
| 2997 | result.append(' %s' % line) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2998 | |
danakj | d18e889 | 2020-12-17 17:42:01 | [diff] [blame] | 2999 | return result |
Mario Sanchez Prada | 2472cab | 2019-09-18 10:58:31 | [diff] [blame] | 3000 | |
| 3001 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3002 | def CheckNoBannedFunctions(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3003 | """Make sure that banned functions are not used.""" |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 3004 | results= [] |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 3005 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3006 | def IsExcludedFile(affected_file, excluded_paths): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3007 | if not excluded_paths: |
| 3008 | return False |
| 3009 | |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 3010 | local_path = affected_file.UnixLocalPath() |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3011 | for item in excluded_paths: |
| 3012 | if input_api.re.match(item, local_path): |
| 3013 | return True |
| 3014 | return False |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 3015 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3016 | def IsIosObjcFile(affected_file): |
| 3017 | local_path = affected_file.LocalPath() |
| 3018 | if input_api.os_path.splitext(local_path)[-1] not in ('.mm', '.m', |
| 3019 | '.h'): |
| 3020 | return False |
| 3021 | basename = input_api.os_path.basename(local_path) |
| 3022 | if 'ios' in basename.split('_'): |
| 3023 | return True |
| 3024 | for sep in (input_api.os_path.sep, input_api.os_path.altsep): |
| 3025 | if sep and 'ios' in local_path.split(sep): |
| 3026 | return True |
| 3027 | return False |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 3028 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3029 | def CheckForMatch(affected_file, line_num: int, line: str, |
| 3030 | ban_rule: BanRule): |
| 3031 | if IsExcludedFile(affected_file, ban_rule.excluded_paths): |
| 3032 | return |
| 3033 | |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 3034 | message = _GetMessageForMatchingType(input_api, f, line_num, line, |
| 3035 | ban_rule) |
| 3036 | if message: |
| 3037 | result_loc = [] |
| 3038 | if ban_rule.surface_as_gerrit_lint: |
| 3039 | result_loc.append(output_api.PresubmitResultLocation( |
| 3040 | file_path=affected_file.LocalPath(), |
| 3041 | start_line=line_num, |
| 3042 | end_line=line_num, |
| 3043 | )) |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3044 | if ban_rule.treat_as_error is not None and ban_rule.treat_as_error: |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 3045 | results.append( |
| 3046 | output_api.PresubmitError('A banned function was used.\n' + |
| 3047 | '\n'.join(message), |
| 3048 | locations=result_loc)) |
| 3049 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3050 | else: |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 3051 | results.append( |
| 3052 | output_api.PresubmitPromptWarning('A banned function was used.\n' + |
| 3053 | '\n'.join(message), |
| 3054 | locations=result_loc)) |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 3055 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3056 | file_filter = lambda f: f.LocalPath().endswith(('.java')) |
| 3057 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3058 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3059 | for ban_rule in _BANNED_JAVA_FUNCTIONS: |
| 3060 | CheckForMatch(f, line_num, line, ban_rule) |
Eric Stevenson | a9a98097 | 2017-09-23 00:04:41 | [diff] [blame] | 3061 | |
Clement Yan | 9b330cb | 2022-11-17 05:25:29 | [diff] [blame] | 3062 | file_filter = lambda f: f.LocalPath().endswith(('.js', '.ts')) |
| 3063 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3064 | for line_num, line in f.ChangedContents(): |
| 3065 | for ban_rule in _BANNED_JAVASCRIPT_FUNCTIONS: |
| 3066 | CheckForMatch(f, line_num, line, ban_rule) |
| 3067 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3068 | file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h')) |
| 3069 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3070 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3071 | for ban_rule in _BANNED_OBJC_FUNCTIONS: |
| 3072 | CheckForMatch(f, line_num, line, ban_rule) |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 3073 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3074 | for f in input_api.AffectedFiles(file_filter=IsIosObjcFile): |
| 3075 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3076 | for ban_rule in _BANNED_IOS_OBJC_FUNCTIONS: |
| 3077 | CheckForMatch(f, line_num, line, ban_rule) |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 3078 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3079 | egtest_filter = lambda f: f.LocalPath().endswith(('_egtest.mm')) |
| 3080 | for f in input_api.AffectedFiles(file_filter=egtest_filter): |
| 3081 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3082 | for ban_rule in _BANNED_IOS_EGTEST_FUNCTIONS: |
| 3083 | CheckForMatch(f, line_num, line, ban_rule) |
Peter K. Lee | 6c03ccff | 2019-07-15 14:40:05 | [diff] [blame] | 3084 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3085 | file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h')) |
| 3086 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3087 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3088 | for ban_rule in _BANNED_CPP_FUNCTIONS: |
| 3089 | CheckForMatch(f, line_num, line, ban_rule) |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 3090 | |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 3091 | # As of 05/2024, iOS fully migrated ConsentLevel::kSync to kSignin, and |
| 3092 | # Android is in the process of preventing new users from entering kSync. |
| 3093 | # So the warning is restricted to those platforms. |
Riley Wong | 49be8a88 | 2025-02-27 00:38:23 | [diff] [blame] | 3094 | ios_pattern = input_api.re.compile(r'(^|[\W_])ios[\W_]') |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 3095 | file_filter = lambda f: (f.LocalPath().endswith(('.cc', '.mm', '.h')) and |
| 3096 | ('android' in f.LocalPath() or |
| 3097 | # Simply checking for an 'ios' substring would |
| 3098 | # catch unrelated cases, use a regex. |
| 3099 | ios_pattern.search(f.LocalPath()))) |
| 3100 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3101 | for line_num, line in f.ChangedContents(): |
| 3102 | for ban_rule in _DEPRECATED_SYNC_CONSENT_CPP_FUNCTIONS: |
| 3103 | CheckForMatch(f, line_num, line, ban_rule) |
| 3104 | |
| 3105 | file_filter = lambda f: f.LocalPath().endswith(('.java')) |
| 3106 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3107 | for line_num, line in f.ChangedContents(): |
| 3108 | for ban_rule in _DEPRECATED_SYNC_CONSENT_JAVA_FUNCTIONS: |
| 3109 | CheckForMatch(f, line_num, line, ban_rule) |
| 3110 | |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 3111 | file_filter = lambda f: f.LocalPath().endswith(('.mojom')) |
| 3112 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3113 | for line_num, line in f.ChangedContents(): |
| 3114 | for ban_rule in _BANNED_MOJOM_PATTERNS: |
| 3115 | CheckForMatch(f, line_num, line, ban_rule) |
| 3116 | |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 3117 | return results |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 3118 | |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 3119 | |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 3120 | def _CheckAndroidNoBannedImports(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3121 | """Make sure that banned java imports are not used.""" |
| 3122 | errors = [] |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 3123 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3124 | file_filter = lambda f: f.LocalPath().endswith(('.java')) |
| 3125 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3126 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3127 | for ban_rule in _BANNED_JAVA_IMPORTS: |
| 3128 | # Consider merging this into the above function. There is no |
| 3129 | # real difference anymore other than helping with a little |
| 3130 | # bit of boilerplate text. Doing so means things like |
| 3131 | # `treat_as_error` will also be uniformly handled. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3132 | problems = _GetMessageForMatchingType(input_api, f, line_num, |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3133 | line, ban_rule) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3134 | if problems: |
| 3135 | errors.extend(problems) |
| 3136 | result = [] |
| 3137 | if (errors): |
| 3138 | result.append( |
| 3139 | output_api.PresubmitError('Banned imports were used.\n' + |
| 3140 | '\n'.join(errors))) |
| 3141 | return result |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 3142 | |
| 3143 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3144 | def CheckNoPragmaOnce(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3145 | """Make sure that banned functions are not used.""" |
| 3146 | files = [] |
| 3147 | pattern = input_api.re.compile(r'^#pragma\s+once', input_api.re.MULTILINE) |
| 3148 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 3149 | if not f.LocalPath().endswith('.h'): |
| 3150 | continue |
Bruce Dawson | 4c4c292 | 2022-05-02 18:07:33 | [diff] [blame] | 3151 | if f.LocalPath().endswith('com_imported_mstscax.h'): |
| 3152 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3153 | contents = input_api.ReadFile(f) |
| 3154 | if pattern.search(contents): |
| 3155 | files.append(f) |
[email protected] | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 3156 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3157 | if files: |
| 3158 | return [ |
| 3159 | output_api.PresubmitError( |
| 3160 | 'Do not use #pragma once in header files.\n' |
| 3161 | 'See http://www.chromium.org/developers/coding-style#TOC-File-headers', |
| 3162 | files) |
| 3163 | ] |
| 3164 | return [] |
[email protected] | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 3165 | |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 3166 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3167 | def CheckNoTrinaryTrueFalse(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3168 | """Checks to make sure we don't introduce use of foo ? true : false.""" |
| 3169 | problems = [] |
| 3170 | pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)') |
| 3171 | for f in input_api.AffectedFiles(): |
| 3172 | if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')): |
| 3173 | continue |
[email protected] | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 3174 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3175 | for line_num, line in f.ChangedContents(): |
| 3176 | if pattern.match(line): |
| 3177 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
[email protected] | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 3178 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3179 | if not problems: |
| 3180 | return [] |
| 3181 | return [ |
| 3182 | output_api.PresubmitPromptWarning( |
| 3183 | 'Please consider avoiding the "? true : false" pattern if possible.\n' |
| 3184 | + '\n'.join(problems)) |
| 3185 | ] |
[email protected] | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 3186 | |
| 3187 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3188 | def CheckUnwantedDependencies(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3189 | """Runs checkdeps on #include and import statements added in this |
| 3190 | change. Breaking - rules is an error, breaking ! rules is a |
| 3191 | warning. |
| 3192 | """ |
| 3193 | # Return early if no relevant file types were modified. |
| 3194 | for f in input_api.AffectedFiles(): |
| 3195 | path = f.LocalPath() |
| 3196 | if (_IsCPlusPlusFile(input_api, path) or _IsProtoFile(input_api, path) |
| 3197 | or _IsJavaFile(input_api, path)): |
| 3198 | break |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 3199 | else: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3200 | return [] |
rhalavati | 08acd23 | 2017-04-03 07:23:28 | [diff] [blame] | 3201 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3202 | import sys |
| 3203 | # We need to wait until we have an input_api object and use this |
| 3204 | # roundabout construct to import checkdeps because this file is |
| 3205 | # eval-ed and thus doesn't have __file__. |
| 3206 | original_sys_path = sys.path |
| 3207 | try: |
| 3208 | sys.path = sys.path + [ |
| 3209 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 3210 | 'buildtools', 'checkdeps') |
| 3211 | ] |
| 3212 | import checkdeps |
| 3213 | from rules import Rule |
| 3214 | finally: |
| 3215 | # Restore sys.path to what it was before. |
| 3216 | sys.path = original_sys_path |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 3217 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3218 | added_includes = [] |
| 3219 | added_imports = [] |
| 3220 | added_java_imports = [] |
| 3221 | for f in input_api.AffectedFiles(): |
| 3222 | if _IsCPlusPlusFile(input_api, f.LocalPath()): |
| 3223 | changed_lines = [line for _, line in f.ChangedContents()] |
| 3224 | added_includes.append([f.AbsoluteLocalPath(), changed_lines]) |
| 3225 | elif _IsProtoFile(input_api, f.LocalPath()): |
| 3226 | changed_lines = [line for _, line in f.ChangedContents()] |
| 3227 | added_imports.append([f.AbsoluteLocalPath(), changed_lines]) |
| 3228 | elif _IsJavaFile(input_api, f.LocalPath()): |
| 3229 | changed_lines = [line for _, line in f.ChangedContents()] |
| 3230 | added_java_imports.append([f.AbsoluteLocalPath(), changed_lines]) |
Jinsuk Kim | 5a09267 | 2017-10-24 22:42:24 | [diff] [blame] | 3231 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3232 | deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath()) |
| 3233 | |
| 3234 | error_descriptions = [] |
| 3235 | warning_descriptions = [] |
| 3236 | error_subjects = set() |
| 3237 | warning_subjects = set() |
| 3238 | |
| 3239 | for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes( |
| 3240 | added_includes): |
| 3241 | path = input_api.os_path.relpath(path, input_api.PresubmitLocalPath()) |
| 3242 | description_with_path = '%s\n %s' % (path, rule_description) |
| 3243 | if rule_type == Rule.DISALLOW: |
| 3244 | error_descriptions.append(description_with_path) |
| 3245 | error_subjects.add("#includes") |
| 3246 | else: |
| 3247 | warning_descriptions.append(description_with_path) |
| 3248 | warning_subjects.add("#includes") |
| 3249 | |
| 3250 | for path, rule_type, rule_description in deps_checker.CheckAddedProtoImports( |
| 3251 | added_imports): |
| 3252 | path = input_api.os_path.relpath(path, input_api.PresubmitLocalPath()) |
| 3253 | description_with_path = '%s\n %s' % (path, rule_description) |
| 3254 | if rule_type == Rule.DISALLOW: |
| 3255 | error_descriptions.append(description_with_path) |
| 3256 | error_subjects.add("imports") |
| 3257 | else: |
| 3258 | warning_descriptions.append(description_with_path) |
| 3259 | warning_subjects.add("imports") |
| 3260 | |
| 3261 | for path, rule_type, rule_description in deps_checker.CheckAddedJavaImports( |
| 3262 | added_java_imports, _JAVA_MULTIPLE_DEFINITION_EXCLUDED_PATHS): |
| 3263 | path = input_api.os_path.relpath(path, input_api.PresubmitLocalPath()) |
| 3264 | description_with_path = '%s\n %s' % (path, rule_description) |
| 3265 | if rule_type == Rule.DISALLOW: |
| 3266 | error_descriptions.append(description_with_path) |
| 3267 | error_subjects.add("imports") |
| 3268 | else: |
| 3269 | warning_descriptions.append(description_with_path) |
| 3270 | warning_subjects.add("imports") |
| 3271 | |
| 3272 | results = [] |
| 3273 | if error_descriptions: |
| 3274 | results.append( |
| 3275 | output_api.PresubmitError( |
| 3276 | 'You added one or more %s that violate checkdeps rules.' % |
| 3277 | " and ".join(error_subjects), error_descriptions)) |
| 3278 | if warning_descriptions: |
| 3279 | results.append( |
| 3280 | output_api.PresubmitPromptOrNotify( |
| 3281 | 'You added one or more %s of files that are temporarily\n' |
| 3282 | 'allowed but being removed. Can you avoid introducing the\n' |
| 3283 | '%s? See relevant DEPS file(s) for details and contacts.' % |
| 3284 | (" and ".join(warning_subjects), "/".join(warning_subjects)), |
| 3285 | warning_descriptions)) |
| 3286 | return results |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 3287 | |
| 3288 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3289 | def CheckFilePermissions(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3290 | """Check that all files have their permissions properly set.""" |
| 3291 | if input_api.platform == 'win32': |
| 3292 | return [] |
| 3293 | checkperms_tool = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 3294 | 'tools', 'checkperms', |
| 3295 | 'checkperms.py') |
| 3296 | args = [ |
Bruce Dawson | 8a43cf7 | 2022-05-13 17:10:32 | [diff] [blame] | 3297 | input_api.python3_executable, checkperms_tool, '--root', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3298 | input_api.change.RepositoryRoot() |
| 3299 | ] |
| 3300 | with input_api.CreateTemporaryFile() as file_list: |
| 3301 | for f in input_api.AffectedFiles(): |
| 3302 | # checkperms.py file/directory arguments must be relative to the |
| 3303 | # repository. |
| 3304 | file_list.write((f.LocalPath() + '\n').encode('utf8')) |
| 3305 | file_list.close() |
| 3306 | args += ['--file-list', file_list.name] |
| 3307 | try: |
| 3308 | input_api.subprocess.check_output(args) |
| 3309 | return [] |
| 3310 | except input_api.subprocess.CalledProcessError as error: |
| 3311 | return [ |
| 3312 | output_api.PresubmitError('checkperms.py failed:', |
| 3313 | long_text=error.output.decode( |
| 3314 | 'utf-8', 'ignore')) |
| 3315 | ] |
[email protected] | fbcafe5a | 2012-08-08 15:31:22 | [diff] [blame] | 3316 | |
| 3317 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3318 | def CheckNoAuraWindowPropertyHInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3319 | """Makes sure we don't include ui/aura/window_property.h |
| 3320 | in header files. |
| 3321 | """ |
| 3322 | pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"') |
| 3323 | errors = [] |
| 3324 | for f in input_api.AffectedFiles(): |
| 3325 | if not f.LocalPath().endswith('.h'): |
| 3326 | continue |
| 3327 | for line_num, line in f.ChangedContents(): |
| 3328 | if pattern.match(line): |
| 3329 | errors.append(' %s:%d' % (f.LocalPath(), line_num)) |
[email protected] | c8278b3 | 2012-10-30 20:35:49 | [diff] [blame] | 3330 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3331 | results = [] |
| 3332 | if errors: |
| 3333 | results.append( |
| 3334 | output_api.PresubmitError( |
| 3335 | 'Header files should not include ui/aura/window_property.h', |
| 3336 | errors)) |
| 3337 | return results |
[email protected] | c8278b3 | 2012-10-30 20:35:49 | [diff] [blame] | 3338 | |
| 3339 | |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 3340 | def CheckNoInternalHeapIncludes(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3341 | """Makes sure we don't include any headers from |
| 3342 | third_party/blink/renderer/platform/heap/impl or |
| 3343 | third_party/blink/renderer/platform/heap/v8_wrapper from files outside of |
| 3344 | third_party/blink/renderer/platform/heap |
| 3345 | """ |
| 3346 | impl_pattern = input_api.re.compile( |
| 3347 | r'^\s*#include\s*"third_party/blink/renderer/platform/heap/impl/.*"') |
| 3348 | v8_wrapper_pattern = input_api.re.compile( |
| 3349 | r'^\s*#include\s*"third_party/blink/renderer/platform/heap/v8_wrapper/.*"' |
| 3350 | ) |
| 3351 | file_filter = lambda f: not input_api.re.match( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3352 | r"^third_party/blink/renderer/platform/heap/.*", |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 3353 | f.UnixLocalPath()) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3354 | errors = [] |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 3355 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3356 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3357 | for line_num, line in f.ChangedContents(): |
| 3358 | if impl_pattern.match(line) or v8_wrapper_pattern.match(line): |
| 3359 | errors.append(' %s:%d' % (f.LocalPath(), line_num)) |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 3360 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3361 | results = [] |
| 3362 | if errors: |
| 3363 | results.append( |
| 3364 | output_api.PresubmitError( |
| 3365 | 'Do not include files from third_party/blink/renderer/platform/heap/impl' |
| 3366 | ' or third_party/blink/renderer/platform/heap/v8_wrapper. Use the ' |
| 3367 | 'relevant counterparts from third_party/blink/renderer/platform/heap', |
| 3368 | errors)) |
| 3369 | return results |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 3370 | |
| 3371 | |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3372 | def _CheckForVersionControlConflictsInFile(input_api, f): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3373 | pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$') |
| 3374 | errors = [] |
| 3375 | for line_num, line in f.ChangedContents(): |
| 3376 | if f.LocalPath().endswith(('.md', '.rst', '.txt')): |
| 3377 | # First-level headers in markdown look a lot like version control |
| 3378 | # conflict markers. http://daringfireball.net/projects/markdown/basics |
| 3379 | continue |
| 3380 | if pattern.match(line): |
| 3381 | errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line)) |
| 3382 | return errors |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3383 | |
| 3384 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3385 | def CheckForVersionControlConflicts(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3386 | """Usually this is not intentional and will cause a compile failure.""" |
| 3387 | errors = [] |
| 3388 | for f in input_api.AffectedFiles(): |
| 3389 | errors.extend(_CheckForVersionControlConflictsInFile(input_api, f)) |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3390 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3391 | results = [] |
| 3392 | if errors: |
| 3393 | results.append( |
| 3394 | output_api.PresubmitError( |
| 3395 | 'Version control conflict markers found, please resolve.', |
| 3396 | errors)) |
| 3397 | return results |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3398 | |
Wei-Yin Chen (陳威尹) | f799d44 | 2018-07-31 02:20:20 | [diff] [blame] | 3399 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3400 | def CheckGoogleSupportAnswerUrlOnUpload(input_api, output_api): |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 3401 | pattern = input_api.re.compile(r'support\.google\.com\/chrome.*/answer') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3402 | errors = [] |
| 3403 | for f in input_api.AffectedFiles(): |
| 3404 | for line_num, line in f.ChangedContents(): |
| 3405 | if pattern.search(line): |
| 3406 | errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line)) |
estade | e17314a0 | 2017-01-12 16:22:16 | [diff] [blame] | 3407 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3408 | results = [] |
| 3409 | if errors: |
| 3410 | results.append( |
| 3411 | output_api.PresubmitPromptWarning( |
| 3412 | 'Found Google support URL addressed by answer number. Please replace ' |
| 3413 | 'with a p= identifier instead. See crbug.com/679462\n', |
| 3414 | errors)) |
| 3415 | return results |
estade | e17314a0 | 2017-01-12 16:22:16 | [diff] [blame] | 3416 | |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3417 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3418 | def CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3419 | def FilterFile(affected_file): |
| 3420 | """Filter function for use with input_api.AffectedSourceFiles, |
| 3421 | below. This filters out everything except non-test files from |
| 3422 | top-level directories that generally speaking should not hard-code |
| 3423 | service URLs (e.g. src/android_webview/, src/content/ and others). |
| 3424 | """ |
| 3425 | return input_api.FilterSourceFile( |
| 3426 | affected_file, |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3427 | files_to_check=[r'^(android_webview|base|content|net)/.*'], |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3428 | files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 3429 | input_api.DEFAULT_FILES_TO_SKIP)) |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 3430 | |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 3431 | base_pattern = (r'"[^"]*(google|googleapis|googlezip|googledrive|appspot)' |
| 3432 | r'\.(com|net)[^"]*"') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3433 | comment_pattern = input_api.re.compile('//.*%s' % base_pattern) |
| 3434 | pattern = input_api.re.compile(base_pattern) |
| 3435 | problems = [] # items are (filename, line_number, line) |
| 3436 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 3437 | for line_num, line in f.ChangedContents(): |
| 3438 | if not comment_pattern.search(line) and pattern.search(line): |
| 3439 | problems.append((f.LocalPath(), line_num, line)) |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 3440 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3441 | if problems: |
| 3442 | return [ |
| 3443 | output_api.PresubmitPromptOrNotify( |
| 3444 | 'Most layers below src/chrome/ should not hardcode service URLs.\n' |
| 3445 | 'Are you sure this is correct?', [ |
| 3446 | ' %s:%d: %s' % (problem[0], problem[1], problem[2]) |
| 3447 | for problem in problems |
| 3448 | ]) |
| 3449 | ] |
| 3450 | else: |
| 3451 | return [] |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 3452 | |
| 3453 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3454 | def CheckChromeOsSyncedPrefRegistration(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3455 | """Warns if Chrome OS C++ files register syncable prefs as browser prefs.""" |
James Cook | 6b6597c | 2019-11-06 22:05:29 | [diff] [blame] | 3456 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3457 | def FileFilter(affected_file): |
| 3458 | """Includes directories known to be Chrome OS only.""" |
| 3459 | return input_api.FilterSourceFile( |
| 3460 | affected_file, |
| 3461 | files_to_check=( |
| 3462 | '^ash/', |
| 3463 | '^chromeos/', # Top-level src/chromeos. |
| 3464 | '.*/chromeos/', # Any path component. |
| 3465 | '^components/arc', |
| 3466 | '^components/exo'), |
| 3467 | files_to_skip=(input_api.DEFAULT_FILES_TO_SKIP)) |
James Cook | 6b6597c | 2019-11-06 22:05:29 | [diff] [blame] | 3468 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3469 | prefs = [] |
| 3470 | priority_prefs = [] |
| 3471 | for f in input_api.AffectedFiles(file_filter=FileFilter): |
| 3472 | for line_num, line in f.ChangedContents(): |
| 3473 | if input_api.re.search('PrefRegistrySyncable::SYNCABLE_PREF', |
| 3474 | line): |
| 3475 | prefs.append(' %s:%d:' % (f.LocalPath(), line_num)) |
| 3476 | prefs.append(' %s' % line) |
| 3477 | if input_api.re.search( |
| 3478 | 'PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF', line): |
| 3479 | priority_prefs.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 3480 | priority_prefs.append(' %s' % line) |
| 3481 | |
| 3482 | results = [] |
| 3483 | if (prefs): |
| 3484 | results.append( |
| 3485 | output_api.PresubmitPromptWarning( |
| 3486 | 'Preferences were registered as SYNCABLE_PREF and will be controlled ' |
| 3487 | 'by browser sync settings. If these prefs should be controlled by OS ' |
| 3488 | 'sync settings use SYNCABLE_OS_PREF instead.\n' + |
| 3489 | '\n'.join(prefs))) |
| 3490 | if (priority_prefs): |
| 3491 | results.append( |
| 3492 | output_api.PresubmitPromptWarning( |
| 3493 | 'Preferences were registered as SYNCABLE_PRIORITY_PREF and will be ' |
| 3494 | 'controlled by browser sync settings. If these prefs should be ' |
| 3495 | 'controlled by OS sync settings use SYNCABLE_OS_PRIORITY_PREF ' |
| 3496 | 'instead.\n' + '\n'.join(prefs))) |
| 3497 | return results |
James Cook | 6b6597c | 2019-11-06 22:05:29 | [diff] [blame] | 3498 | |
| 3499 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3500 | def CheckNoAbbreviationInPngFileName(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3501 | """Makes sure there are no abbreviations in the name of PNG files. |
| 3502 | The native_client_sdk directory is excluded because it has auto-generated PNG |
| 3503 | files for documentation. |
| 3504 | """ |
| 3505 | errors = [] |
Yuanqing Zhu | 9eef0283 | 2022-12-04 14:42:17 | [diff] [blame] | 3506 | files_to_check = [r'.*\.png$'] |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3507 | files_to_skip = [r'^native_client_sdk/', |
| 3508 | r'^services/test/', |
| 3509 | r'^third_party/blink/web_tests/', |
Bruce Dawson | 3db45621 | 2022-05-02 05:34:18 | [diff] [blame] | 3510 | ] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3511 | file_filter = lambda f: input_api.FilterSourceFile( |
| 3512 | f, files_to_check=files_to_check, files_to_skip=files_to_skip) |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 3513 | abbreviation = input_api.re.compile(r'.+_[a-z]\.png|.+_[a-z]_.*\.png') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3514 | for f in input_api.AffectedFiles(include_deletes=False, |
| 3515 | file_filter=file_filter): |
Yuanqing Zhu | 9eef0283 | 2022-12-04 14:42:17 | [diff] [blame] | 3516 | file_name = input_api.os_path.split(f.LocalPath())[1] |
| 3517 | if abbreviation.search(file_name): |
| 3518 | errors.append(' %s' % f.LocalPath()) |
[email protected] | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 3519 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3520 | results = [] |
| 3521 | if errors: |
| 3522 | results.append( |
| 3523 | output_api.PresubmitError( |
| 3524 | 'The name of PNG files should not have abbreviations. \n' |
| 3525 | 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n' |
| 3526 | 'Contact [email protected] if you have questions.', errors)) |
| 3527 | return results |
[email protected] | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 3528 | |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 3529 | def CheckNoProductIconsAddedToPublicRepo(input_api, output_api): |
| 3530 | """Heuristically identifies product icons based on their file name and reminds |
| 3531 | contributors not to add them to the Chromium repository. |
| 3532 | """ |
Peter Kotwicz | f634d07 | 2025-04-28 22:48:15 | [diff] [blame] | 3533 | |
| 3534 | if input_api.change.RepositoryRoot().endswith('clank'): |
| 3535 | # TODO(crbug.com/414435241): Change check to compute whether change |
| 3536 | # belongs to internal repository instead of relying on string matching. |
| 3537 | return [] |
| 3538 | |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 3539 | errors = [] |
| 3540 | files_to_check = [r'.*google.*\.png$|.*google.*\.svg$|.*google.*\.icon$'] |
| 3541 | file_filter = lambda f: input_api.FilterSourceFile( |
| 3542 | f, files_to_check=files_to_check) |
| 3543 | for f in input_api.AffectedFiles(include_deletes=False, |
| 3544 | file_filter=file_filter): |
| 3545 | errors.append(' %s' % f.LocalPath()) |
| 3546 | |
| 3547 | results = [] |
| 3548 | if errors: |
Bruce Dawson | 3bcf0c9 | 2022-08-12 00:03:08 | [diff] [blame] | 3549 | # Give warnings instead of errors on presubmit --all and presubmit |
| 3550 | # --files. |
| 3551 | message_type = (output_api.PresubmitNotifyResult if input_api.no_diffs |
| 3552 | else output_api.PresubmitError) |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 3553 | results.append( |
Bruce Dawson | 3bcf0c9 | 2022-08-12 00:03:08 | [diff] [blame] | 3554 | message_type( |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 3555 | 'Trademarked images should not be added to the public repo. ' |
| 3556 | 'See crbug.com/944754', errors)) |
| 3557 | return results |
| 3558 | |
[email protected] | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 3559 | |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3560 | def _ExtractAddRulesFromParsedDeps(parsed_deps): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3561 | """Extract the rules that add dependencies from a parsed DEPS file. |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3562 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3563 | Args: |
| 3564 | parsed_deps: the locals dictionary from evaluating the DEPS file.""" |
| 3565 | add_rules = set() |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3566 | add_rules.update([ |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3567 | rule[1:] for rule in parsed_deps.get('include_rules', []) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3568 | if rule.startswith('+') or rule.startswith('!') |
| 3569 | ]) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3570 | for _, rules in parsed_deps.get('specific_include_rules', {}).items(): |
| 3571 | add_rules.update([ |
| 3572 | rule[1:] for rule in rules |
| 3573 | if rule.startswith('+') or rule.startswith('!') |
| 3574 | ]) |
| 3575 | return add_rules |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3576 | |
| 3577 | |
| 3578 | def _ParseDeps(contents): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3579 | """Simple helper for parsing DEPS files.""" |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3580 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3581 | # Stubs for handling special syntax in the root DEPS file. |
| 3582 | class _VarImpl: |
| 3583 | def __init__(self, local_scope): |
| 3584 | self._local_scope = local_scope |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3585 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3586 | def Lookup(self, var_name): |
| 3587 | """Implements the Var syntax.""" |
| 3588 | try: |
| 3589 | return self._local_scope['vars'][var_name] |
| 3590 | except KeyError: |
| 3591 | raise Exception('Var is not defined: %s' % var_name) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3592 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3593 | local_scope = {} |
| 3594 | global_scope = { |
| 3595 | 'Var': _VarImpl(local_scope).Lookup, |
| 3596 | 'Str': str, |
| 3597 | } |
Dirk Pranke | 1b9e0638 | 2021-05-14 01:16:22 | [diff] [blame] | 3598 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3599 | exec(contents, global_scope, local_scope) |
| 3600 | return local_scope |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3601 | |
| 3602 | |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3603 | def _FindAllDepsFilesForSubpath(input_api, subpath): |
| 3604 | ret = [] |
| 3605 | while subpath: |
| 3606 | cur = input_api.os_path.join(input_api.change.RepositoryRoot(), subpath, 'DEPS') |
Joanna Wang | 130e7bdd | 2024-12-10 17:39:03 | [diff] [blame] | 3607 | if input_api.os_path.isfile(cur): |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3608 | ret.append(cur) |
| 3609 | subpath = input_api.os_path.dirname(subpath) |
| 3610 | return ret |
| 3611 | |
| 3612 | |
| 3613 | def _FindAddedDepsThatRequireReview(input_api, depended_on_paths): |
| 3614 | """Filters to those whose DEPS set new_usages_require_review=True""" |
| 3615 | ret = set() |
| 3616 | cache = {} |
| 3617 | for target_path in depended_on_paths: |
| 3618 | for subpath in _FindAllDepsFilesForSubpath(input_api, target_path): |
| 3619 | config = cache.get(subpath) |
| 3620 | if config is None: |
| 3621 | config = _ParseDeps(input_api.ReadFile(subpath)) |
| 3622 | cache[subpath] = config |
| 3623 | if config.get('new_usages_require_review'): |
| 3624 | ret.add(target_path) |
| 3625 | break |
| 3626 | return ret |
| 3627 | |
| 3628 | |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3629 | def _CalculateAddedDeps(os_path, old_contents, new_contents): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3630 | """Helper method for CheckAddedDepsHaveTargetApprovals. Returns |
| 3631 | a set of DEPS entries that we should look up. |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 3632 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3633 | For a directory (rather than a specific filename) we fake a path to |
| 3634 | a specific filename by adding /DEPS. This is chosen as a file that |
| 3635 | will seldom or never be subject to per-file include_rules. |
| 3636 | """ |
| 3637 | # We ignore deps entries on auto-generated directories. |
| 3638 | AUTO_GENERATED_DIRS = ['grit', 'jni'] |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 3639 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3640 | old_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(old_contents)) |
| 3641 | new_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(new_contents)) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3642 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3643 | added_deps = new_deps.difference(old_deps) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3644 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3645 | results = set() |
| 3646 | for added_dep in added_deps: |
| 3647 | if added_dep.split('/')[0] in AUTO_GENERATED_DIRS: |
| 3648 | continue |
| 3649 | # Assume that a rule that ends in .h is a rule for a specific file. |
| 3650 | if added_dep.endswith('.h'): |
| 3651 | results.add(added_dep) |
| 3652 | else: |
| 3653 | results.add(os_path.join(added_dep, 'DEPS')) |
| 3654 | return results |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 3655 | |
Stephanie Kim | ec4f55a | 2024-04-24 16:54:02 | [diff] [blame] | 3656 | def CheckForNewDEPSDownloadFromGoogleStorageHooks(input_api, output_api): |
| 3657 | """Checks that there are no new download_from_google_storage hooks""" |
| 3658 | for f in input_api.AffectedFiles(include_deletes=False): |
| 3659 | if f.LocalPath() == 'DEPS': |
| 3660 | old_hooks = _ParseDeps('\n'.join(f.OldContents()))['hooks'] |
| 3661 | new_hooks = _ParseDeps('\n'.join(f.NewContents()))['hooks'] |
| 3662 | old_name_to_hook = {hook['name']: hook for hook in old_hooks} |
| 3663 | new_name_to_hook = {hook['name']: hook for hook in new_hooks} |
| 3664 | added_hook_names = set(new_name_to_hook.keys()) - set( |
| 3665 | old_name_to_hook.keys()) |
| 3666 | if not added_hook_names: |
| 3667 | return [] |
| 3668 | new_download_from_google_storage_hooks = [] |
| 3669 | for new_hook in added_hook_names: |
| 3670 | hook = new_name_to_hook[new_hook] |
| 3671 | action_cmd = hook['action'] |
| 3672 | if any('download_from_google_storage' in arg |
| 3673 | for arg in action_cmd): |
| 3674 | new_download_from_google_storage_hooks.append(new_hook) |
| 3675 | if new_download_from_google_storage_hooks: |
| 3676 | return [ |
| 3677 | output_api.PresubmitError( |
| 3678 | 'Please do not add new download_from_google_storage ' |
| 3679 | 'hooks. Instead, add a `gcs` dep_type entry to `deps`. ' |
| 3680 | 'See https://chromium.googlesource.com/chromium/src.git' |
| 3681 | '/+/refs/heads/main/docs/gcs_dependencies.md for more ' |
| 3682 | 'info. Added hooks:', |
| 3683 | items=new_download_from_google_storage_hooks) |
| 3684 | ] |
| 3685 | return [] |
| 3686 | |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 3687 | |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3688 | def CheckEachPerfettoTestDataFileHasDepsEntry(input_api, output_api): |
| 3689 | test_data_filter = lambda f: input_api.FilterSourceFile( |
Rasika Navarange | 08e54216 | 2024-05-31 13:31:26 | [diff] [blame] | 3690 | f, files_to_check=[r'^base/tracing/test/data_sha256/.*\.sha256']) |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3691 | if not any(input_api.AffectedFiles(file_filter=test_data_filter)): |
| 3692 | return [] |
| 3693 | |
| 3694 | # Find DEPS entry |
| 3695 | deps_entry = [] |
Rasika Navarange | 277cd66 | 2024-06-04 10:14:59 | [diff] [blame] | 3696 | old_deps_entry = [] |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3697 | for f in input_api.AffectedFiles(include_deletes=False): |
| 3698 | if f.LocalPath() == 'DEPS': |
| 3699 | new_deps = _ParseDeps('\n'.join(f.NewContents()))['deps'] |
| 3700 | deps_entry = new_deps['src/base/tracing/test/data'] |
Rasika Navarange | 277cd66 | 2024-06-04 10:14:59 | [diff] [blame] | 3701 | old_deps = _ParseDeps('\n'.join(f.OldContents()))['deps'] |
| 3702 | old_deps_entry = old_deps['src/base/tracing/test/data'] |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3703 | if not deps_entry: |
Rasika Navarange | 08e54216 | 2024-05-31 13:31:26 | [diff] [blame] | 3704 | # TODO(312895063):Add back error when .sha256 files have been moved. |
Rasika Navarange | d977df34 | 2024-06-05 10:01:27 | [diff] [blame] | 3705 | return [output_api.PresubmitError( |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3706 | 'You must update the DEPS file when you update a ' |
Rasika Navarange | 08e54216 | 2024-05-31 13:31:26 | [diff] [blame] | 3707 | '.sha256 file in base/tracing/test/data_sha256' |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3708 | )] |
| 3709 | |
| 3710 | output = [] |
| 3711 | for f in input_api.AffectedFiles(file_filter=test_data_filter): |
| 3712 | objects = deps_entry['objects'] |
| 3713 | if not f.NewContents(): |
| 3714 | # Deleted file so check that DEPS entry removed |
| 3715 | sha256_from_file = f.OldContents()[0] |
| 3716 | object_entry = next( |
| 3717 | (item for item in objects if item["sha256sum"] == sha256_from_file), |
| 3718 | None) |
Rasika Navarange | 277cd66 | 2024-06-04 10:14:59 | [diff] [blame] | 3719 | old_entry = next( |
| 3720 | (item for item in old_deps_entry['objects'] if item["sha256sum"] == sha256_from_file), |
| 3721 | None) |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3722 | if object_entry: |
Rasika Navarange | 277cd66 | 2024-06-04 10:14:59 | [diff] [blame] | 3723 | # Allow renaming of objects with the same hash |
| 3724 | if object_entry['object_name'] != old_entry['object_name']: |
| 3725 | continue |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3726 | output.append(output_api.PresubmitError( |
| 3727 | 'You deleted %s so you must also remove the corresponding DEPS entry.' |
| 3728 | % f.LocalPath() |
| 3729 | )) |
| 3730 | continue |
| 3731 | |
| 3732 | sha256_from_file = f.NewContents()[0] |
| 3733 | object_entry = next( |
| 3734 | (item for item in objects if item["sha256sum"] == sha256_from_file), |
| 3735 | None) |
| 3736 | if not object_entry: |
| 3737 | output.append(output_api.PresubmitError( |
| 3738 | 'No corresponding DEPS entry found for %s. ' |
| 3739 | 'Run `base/tracing/test/test_data.py get_deps --filepath %s` ' |
| 3740 | 'to generate the DEPS entry.' |
| 3741 | % (f.LocalPath(), f.LocalPath()) |
| 3742 | )) |
| 3743 | |
| 3744 | if output: |
| 3745 | output.append(output_api.PresubmitError( |
| 3746 | 'The DEPS entry for `src/base/tracing/test/data` in the DEPS file has not been ' |
| 3747 | 'updated properly. Run `base/tracing/test/test_data.py get_all_deps` to see what ' |
| 3748 | 'the DEPS entry should look like.' |
| 3749 | )) |
| 3750 | return output |
| 3751 | |
| 3752 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3753 | def CheckAddedDepsHaveTargetApprovals(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3754 | """When a dependency prefixed with + is added to a DEPS file, we |
| 3755 | want to make sure that the change is reviewed by an OWNER of the |
| 3756 | target file or directory, to avoid layering violations from being |
| 3757 | introduced. This check verifies that this happens. |
| 3758 | """ |
| 3759 | # We rely on Gerrit's code-owners to check approvals. |
| 3760 | # input_api.gerrit is always set for Chromium, but other projects |
| 3761 | # might not use Gerrit. |
Bruce Dawson | 344ab26 | 2022-06-04 11:35:10 | [diff] [blame] | 3762 | if not input_api.gerrit or input_api.no_diffs: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3763 | return [] |
Bruce Dawson | b357aeb | 2022-08-09 15:38:30 | [diff] [blame] | 3764 | if 'PRESUBMIT_SKIP_NETWORK' in input_api.environ: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3765 | return [] |
Bruce Dawson | b357aeb | 2022-08-09 15:38:30 | [diff] [blame] | 3766 | try: |
| 3767 | if (input_api.change.issue and |
| 3768 | input_api.gerrit.IsOwnersOverrideApproved( |
| 3769 | input_api.change.issue)): |
| 3770 | # Skip OWNERS check when Owners-Override label is approved. This is |
| 3771 | # intended for global owners, trusted bots, and on-call sheriffs. |
| 3772 | # Review is still required for these changes. |
| 3773 | return [] |
| 3774 | except Exception as e: |
Sam Maier | 4cef924 | 2022-10-03 14:21:24 | [diff] [blame] | 3775 | return [output_api.PresubmitPromptWarning( |
| 3776 | 'Failed to retrieve owner override status - %s' % str(e))] |
Edward Lesmes | 6fba5108 | 2021-01-20 04:20:23 | [diff] [blame] | 3777 | |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3778 | # A set of paths (that might not exist) that are being added as DEPS |
| 3779 | # (via lines like "+foo/bar/baz"). |
| 3780 | depended_on_paths = set() |
jochen | 53efcdd | 2016-01-29 05:09:24 | [diff] [blame] | 3781 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3782 | file_filter = lambda f: not input_api.re.match( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3783 | r"^third_party/blink/.*", |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 3784 | f.UnixLocalPath()) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3785 | for f in input_api.AffectedFiles(include_deletes=False, |
| 3786 | file_filter=file_filter): |
| 3787 | filename = input_api.os_path.basename(f.LocalPath()) |
| 3788 | if filename == 'DEPS': |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3789 | depended_on_paths.update( |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3790 | _CalculateAddedDeps(input_api.os_path, |
| 3791 | '\n'.join(f.OldContents()), |
| 3792 | '\n'.join(f.NewContents()))) |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3793 | |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3794 | # Requiring reviews is opt-in as of https://crbug.com/365797506 |
| 3795 | depended_on_paths = _FindAddedDepsThatRequireReview(input_api, depended_on_paths) |
| 3796 | if not depended_on_paths: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3797 | return [] |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3798 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3799 | if input_api.is_committing: |
| 3800 | if input_api.tbr: |
| 3801 | return [ |
| 3802 | output_api.PresubmitNotifyResult( |
| 3803 | '--tbr was specified, skipping OWNERS check for DEPS additions' |
| 3804 | ) |
| 3805 | ] |
Daniel Cheng | 3008dc1 | 2022-05-13 04:02:11 | [diff] [blame] | 3806 | # TODO(dcheng): Make this generate an error on dry runs if the reviewer |
| 3807 | # is not added, to prevent review serialization. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3808 | if input_api.dry_run: |
| 3809 | return [ |
| 3810 | output_api.PresubmitNotifyResult( |
| 3811 | 'This is a dry run, skipping OWNERS check for DEPS additions' |
| 3812 | ) |
| 3813 | ] |
| 3814 | if not input_api.change.issue: |
| 3815 | return [ |
| 3816 | output_api.PresubmitError( |
| 3817 | "DEPS approval by OWNERS check failed: this change has " |
| 3818 | "no change number, so we can't check it for approvals.") |
| 3819 | ] |
| 3820 | output = output_api.PresubmitError |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 3821 | else: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3822 | output = output_api.PresubmitNotifyResult |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3823 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3824 | owner_email, reviewers = ( |
| 3825 | input_api.canned_checks.GetCodereviewOwnerAndReviewers( |
| 3826 | input_api, None, approval_needed=input_api.is_committing)) |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3827 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3828 | owner_email = owner_email or input_api.change.author_email |
| 3829 | |
| 3830 | approval_status = input_api.owners_client.GetFilesApprovalStatus( |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3831 | depended_on_paths, reviewers.union([owner_email]), []) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3832 | missing_files = [ |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3833 | p for p in depended_on_paths |
| 3834 | if approval_status[p] != input_api.owners_client.APPROVED |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3835 | ] |
| 3836 | |
| 3837 | # We strip the /DEPS part that was added by |
| 3838 | # _FilesToCheckForIncomingDeps to fake a path to a file in a |
| 3839 | # directory. |
| 3840 | def StripDeps(path): |
| 3841 | start_deps = path.rfind('/DEPS') |
| 3842 | if start_deps != -1: |
| 3843 | return path[:start_deps] |
| 3844 | else: |
| 3845 | return path |
| 3846 | |
Scott Lee | bf6a094 | 2024-06-26 22:59:39 | [diff] [blame] | 3847 | submodule_paths = set(input_api.ListSubmodules()) |
| 3848 | def is_from_submodules(path, submodule_paths): |
| 3849 | path = input_api.os_path.normpath(path) |
| 3850 | while path: |
| 3851 | if path in submodule_paths: |
| 3852 | return True |
| 3853 | |
| 3854 | # All deps should be a relative path from the checkout. |
| 3855 | # i.e., shouldn't start with "/" or "c:\", for example. |
| 3856 | # |
| 3857 | # That said, this is to prevent an infinite loop, just in case |
| 3858 | # an input dep path starts with "/", because |
| 3859 | # os.path.dirname("/") => "/" |
| 3860 | parent = input_api.os_path.dirname(path) |
| 3861 | if parent == path: |
| 3862 | break |
| 3863 | path = parent |
| 3864 | |
| 3865 | return False |
| 3866 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3867 | unapproved_dependencies = [ |
| 3868 | "'+%s'," % StripDeps(path) for path in missing_files |
Scott Lee | bf6a094 | 2024-06-26 22:59:39 | [diff] [blame] | 3869 | # if a newly added dep is from a submodule, it becomes trickier |
| 3870 | # to get suggested owners, especially it is from a different host. |
| 3871 | # |
| 3872 | # skip the review enforcement for cross-repo deps. |
| 3873 | if not is_from_submodules(path, submodule_paths) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3874 | ] |
| 3875 | |
| 3876 | if unapproved_dependencies: |
| 3877 | output_list = [ |
| 3878 | output( |
| 3879 | 'You need LGTM from owners of depends-on paths in DEPS that were ' |
| 3880 | 'modified in this CL:\n %s' % |
| 3881 | '\n '.join(sorted(unapproved_dependencies))) |
| 3882 | ] |
| 3883 | suggested_owners = input_api.owners_client.SuggestOwners( |
| 3884 | missing_files, exclude=[owner_email]) |
| 3885 | output_list.append( |
| 3886 | output('Suggested missing target path OWNERS:\n %s' % |
| 3887 | '\n '.join(suggested_owners or []))) |
| 3888 | return output_list |
| 3889 | |
| 3890 | return [] |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3891 | |
| 3892 | |
Wei-Yin Chen (陳威尹) | dca729a | 2018-07-31 21:35:49 | [diff] [blame] | 3893 | # TODO: add unit tests. |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3894 | def CheckSpamLogging(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3895 | file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS] |
| 3896 | files_to_skip = ( |
| 3897 | _EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 3898 | input_api.DEFAULT_FILES_TO_SKIP + ( |
Jaewon Jung | 2f323bb | 2022-12-07 23:55:01 | [diff] [blame] | 3899 | r"^base/fuchsia/scoped_fx_logger\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3900 | r"^base/logging\.h$", |
| 3901 | r"^base/logging\.cc$", |
| 3902 | r"^base/task/thread_pool/task_tracker\.cc$", |
| 3903 | r"^chrome/app/chrome_main_delegate\.cc$", |
Yao Li | 359937b | 2023-02-15 23:43:03 | [diff] [blame] | 3904 | r"^chrome/browser/ash/arc/enterprise/cert_store/arc_cert_installer\.cc$", |
| 3905 | r"^chrome/browser/ash/policy/remote_commands/user_command_arc_job\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3906 | r"^chrome/browser/chrome_browser_main\.cc$", |
| 3907 | r"^chrome/browser/ui/startup/startup_browser_creator\.cc$", |
| 3908 | r"^chrome/browser/browser_switcher/bho/.*", |
| 3909 | r"^chrome/browser/diagnostics/diagnostics_writer\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3910 | r"^chrome/chrome_elf/dll_hash/dll_hash_main\.cc$", |
| 3911 | r"^chrome/installer/setup/.*", |
Daniel Rubery | ad36eea | 2024-08-01 01:38:32 | [diff] [blame] | 3912 | # crdmg runs as a separate binary which intentionally does |
| 3913 | # not depend on base logging. |
| 3914 | r"^chrome/utility/safe_browsing/mac/crdmg\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3915 | r"^chromecast/", |
Vigen Issahhanjan | e2d9382 | 2023-06-30 15:57:20 | [diff] [blame] | 3916 | r"^components/cast", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3917 | r"^components/media_control/renderer/media_playback_options\.cc$", |
Salma Elmahallawy | 5297645 | 2023-01-27 17:04:49 | [diff] [blame] | 3918 | r"^components/policy/core/common/policy_logger\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3919 | r"^components/viz/service/display/" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3920 | r"overlay_strategy_underlay_cast\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3921 | r"^components/zucchini/.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3922 | # TODO(peter): Remove exception. https://crbug.com/534537 |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3923 | r"^content/browser/notifications/" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3924 | r"notification_event_dispatcher_impl\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3925 | r"^content/common/gpu/client/gl_helper_benchmark\.cc$", |
| 3926 | r"^courgette/courgette_minimal_tool\.cc$", |
| 3927 | r"^courgette/courgette_tool\.cc$", |
| 3928 | r"^extensions/renderer/logging_native_handler\.cc$", |
| 3929 | r"^fuchsia_web/common/init_logging\.cc$", |
| 3930 | r"^fuchsia_web/runners/common/web_component\.cc$", |
Caroline Liu | a705013 | 2023-02-13 22:23:15 | [diff] [blame] | 3931 | r"^fuchsia_web/shell/.*\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3932 | r"^headless/app/headless_shell\.cc$", |
| 3933 | r"^ipc/ipc_logging\.cc$", |
| 3934 | r"^native_client_sdk/", |
| 3935 | r"^remoting/base/logging\.h$", |
| 3936 | r"^remoting/host/.*", |
| 3937 | r"^sandbox/linux/.*", |
Austin Sullivan | a6054e0 | 2024-05-20 16:31:29 | [diff] [blame] | 3938 | r"^services/webnn/tflite/graph_impl_tflite\.cc$", |
| 3939 | r"^services/webnn/coreml/graph_impl_coreml\.mm$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3940 | r"^storage/browser/file_system/dump_file_system\.cc$", |
Steinar H. Gunderson | e5689e4 | 2024-08-07 18:17:19 | [diff] [blame] | 3941 | r"^testing/perf/", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3942 | r"^tools/", |
| 3943 | r"^ui/base/resource/data_pack\.cc$", |
| 3944 | r"^ui/aura/bench/bench_main\.cc$", |
| 3945 | r"^ui/ozone/platform/cast/", |
| 3946 | r"^ui/base/x/xwmstartupcheck/" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3947 | r"xwmstartupcheck\.cc$")) |
| 3948 | source_file_filter = lambda x: input_api.FilterSourceFile( |
| 3949 | x, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip) |
[email protected] | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3950 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3951 | log_info = set([]) |
| 3952 | printf = set([]) |
[email protected] | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3953 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3954 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 3955 | for _, line in f.ChangedContents(): |
| 3956 | if input_api.re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", line): |
| 3957 | log_info.add(f.LocalPath()) |
| 3958 | elif input_api.re.search(r"\bD?LOG_IF\s*\(\s*INFO\s*,", line): |
| 3959 | log_info.add(f.LocalPath()) |
[email protected] | 18b466b | 2013-12-02 22:01:37 | [diff] [blame] | 3960 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3961 | if input_api.re.search(r"\bprintf\(", line): |
| 3962 | printf.add(f.LocalPath()) |
| 3963 | elif input_api.re.search(r"\bfprintf\((stdout|stderr)", line): |
| 3964 | printf.add(f.LocalPath()) |
[email protected] | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3965 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3966 | if log_info: |
| 3967 | return [ |
| 3968 | output_api.PresubmitError( |
| 3969 | 'These files spam the console log with LOG(INFO):', |
| 3970 | items=log_info) |
| 3971 | ] |
| 3972 | if printf: |
| 3973 | return [ |
| 3974 | output_api.PresubmitError( |
| 3975 | 'These files spam the console log with printf/fprintf:', |
| 3976 | items=printf) |
| 3977 | ] |
| 3978 | return [] |
[email protected] | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3979 | |
| 3980 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3981 | def CheckForAnonymousVariables(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3982 | """These types are all expected to hold locks while in scope and |
| 3983 | so should never be anonymous (which causes them to be immediately |
| 3984 | destroyed).""" |
| 3985 | they_who_must_be_named = [ |
| 3986 | 'base::AutoLock', |
| 3987 | 'base::AutoReset', |
| 3988 | 'base::AutoUnlock', |
| 3989 | 'SkAutoAlphaRestore', |
| 3990 | 'SkAutoBitmapShaderInstall', |
| 3991 | 'SkAutoBlitterChoose', |
| 3992 | 'SkAutoBounderCommit', |
| 3993 | 'SkAutoCallProc', |
| 3994 | 'SkAutoCanvasRestore', |
| 3995 | 'SkAutoCommentBlock', |
| 3996 | 'SkAutoDescriptor', |
| 3997 | 'SkAutoDisableDirectionCheck', |
| 3998 | 'SkAutoDisableOvalCheck', |
| 3999 | 'SkAutoFree', |
| 4000 | 'SkAutoGlyphCache', |
| 4001 | 'SkAutoHDC', |
| 4002 | 'SkAutoLockColors', |
| 4003 | 'SkAutoLockPixels', |
| 4004 | 'SkAutoMalloc', |
| 4005 | 'SkAutoMaskFreeImage', |
| 4006 | 'SkAutoMutexAcquire', |
| 4007 | 'SkAutoPathBoundsUpdate', |
| 4008 | 'SkAutoPDFRelease', |
| 4009 | 'SkAutoRasterClipValidate', |
| 4010 | 'SkAutoRef', |
| 4011 | 'SkAutoTime', |
| 4012 | 'SkAutoTrace', |
| 4013 | 'SkAutoUnref', |
| 4014 | ] |
| 4015 | anonymous = r'(%s)\s*[({]' % '|'.join(they_who_must_be_named) |
| 4016 | # bad: base::AutoLock(lock.get()); |
| 4017 | # not bad: base::AutoLock lock(lock.get()); |
| 4018 | bad_pattern = input_api.re.compile(anonymous) |
| 4019 | # good: new base::AutoLock(lock.get()) |
| 4020 | good_pattern = input_api.re.compile(r'\bnew\s*' + anonymous) |
| 4021 | errors = [] |
[email protected] | 49aa76a | 2013-12-04 06:59:16 | [diff] [blame] | 4022 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4023 | for f in input_api.AffectedFiles(): |
| 4024 | if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')): |
| 4025 | continue |
| 4026 | for linenum, line in f.ChangedContents(): |
| 4027 | if bad_pattern.search(line) and not good_pattern.search(line): |
| 4028 | errors.append('%s:%d' % (f.LocalPath(), linenum)) |
[email protected] | 49aa76a | 2013-12-04 06:59:16 | [diff] [blame] | 4029 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4030 | if errors: |
| 4031 | return [ |
| 4032 | output_api.PresubmitError( |
| 4033 | 'These lines create anonymous variables that need to be named:', |
| 4034 | items=errors) |
| 4035 | ] |
| 4036 | return [] |
[email protected] | 49aa76a | 2013-12-04 06:59:16 | [diff] [blame] | 4037 | |
| 4038 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 4039 | def CheckUniquePtrOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4040 | # Returns whether |template_str| is of the form <T, U...> for some types T |
Glen Robertson | 9142ffd7 | 2024-05-16 01:37:47 | [diff] [blame] | 4041 | # and U, or is invalid due to mismatched angle bracket pairs. Assumes that |
| 4042 | # |template_str| is already in the form <...>. |
| 4043 | def HasMoreThanOneArgOrInvalid(template_str): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4044 | # Level of <...> nesting. |
| 4045 | nesting = 0 |
| 4046 | for c in template_str: |
| 4047 | if c == '<': |
| 4048 | nesting += 1 |
| 4049 | elif c == '>': |
| 4050 | nesting -= 1 |
| 4051 | elif c == ',' and nesting == 1: |
| 4052 | return True |
Glen Robertson | 9142ffd7 | 2024-05-16 01:37:47 | [diff] [blame] | 4053 | if nesting != 0: |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 4054 | # Invalid. |
| 4055 | return True |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4056 | return False |
Vaclav Brozek | b7fadb69 | 2018-08-30 06:39:53 | [diff] [blame] | 4057 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4058 | file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS] |
| 4059 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 4060 | affected_file, |
| 4061 | files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api. |
| 4062 | DEFAULT_FILES_TO_SKIP), |
| 4063 | files_to_check=file_inclusion_pattern) |
Vaclav Brozek | a54c528b | 2018-04-06 19:23:55 | [diff] [blame] | 4064 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4065 | # Pattern to capture a single "<...>" block of template arguments. It can |
| 4066 | # handle linearly nested blocks, such as "<std::vector<std::set<T>>>", but |
| 4067 | # cannot handle branching structures, such as "<pair<set<T>,set<U>>". The |
| 4068 | # latter would likely require counting that < and > match, which is not |
| 4069 | # expressible in regular languages. Should the need arise, one can introduce |
| 4070 | # limited counting (matching up to a total number of nesting depth), which |
| 4071 | # should cover all practical cases for already a low nesting limit. |
| 4072 | template_arg_pattern = ( |
| 4073 | r'<[^>]*' # Opening block of <. |
| 4074 | r'>([^<]*>)?') # Closing block of >. |
| 4075 | # Prefix expressing that whatever follows is not already inside a <...> |
| 4076 | # block. |
| 4077 | not_inside_template_arg_pattern = r'(^|[^<,\s]\s*)' |
| 4078 | null_construct_pattern = input_api.re.compile( |
| 4079 | not_inside_template_arg_pattern + r'\bstd::unique_ptr' + |
| 4080 | template_arg_pattern + r'\(\)') |
Vaclav Brozek | a54c528b | 2018-04-06 19:23:55 | [diff] [blame] | 4081 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4082 | # Same as template_arg_pattern, but excluding type arrays, e.g., <T[]>. |
| 4083 | template_arg_no_array_pattern = ( |
| 4084 | r'<[^>]*[^]]' # Opening block of <. |
| 4085 | r'>([^(<]*[^]]>)?') # Closing block of >. |
| 4086 | # Prefix saying that what follows is the start of an expression. |
| 4087 | start_of_expr_pattern = r'(=|\breturn|^)\s*' |
| 4088 | # Suffix saying that what follows are call parentheses with a non-empty list |
| 4089 | # of arguments. |
| 4090 | nonempty_arg_list_pattern = r'\(([^)]|$)' |
| 4091 | # Put the template argument into a capture group for deeper examination later. |
| 4092 | return_construct_pattern = input_api.re.compile( |
| 4093 | start_of_expr_pattern + r'std::unique_ptr' + '(?P<template_arg>' + |
| 4094 | template_arg_no_array_pattern + ')' + nonempty_arg_list_pattern) |
Vaclav Brozek | a54c528b | 2018-04-06 19:23:55 | [diff] [blame] | 4095 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4096 | problems_constructor = [] |
| 4097 | problems_nullptr = [] |
| 4098 | for f in input_api.AffectedSourceFiles(sources): |
| 4099 | for line_number, line in f.ChangedContents(): |
| 4100 | # Disallow: |
| 4101 | # return std::unique_ptr<T>(foo); |
| 4102 | # bar = std::unique_ptr<T>(foo); |
| 4103 | # But allow: |
| 4104 | # return std::unique_ptr<T[]>(foo); |
| 4105 | # bar = std::unique_ptr<T[]>(foo); |
| 4106 | # And also allow cases when the second template argument is present. Those |
| 4107 | # cases cannot be handled by std::make_unique: |
| 4108 | # return std::unique_ptr<T, U>(foo); |
| 4109 | # bar = std::unique_ptr<T, U>(foo); |
| 4110 | local_path = f.LocalPath() |
| 4111 | return_construct_result = return_construct_pattern.search(line) |
Glen Robertson | 9142ffd7 | 2024-05-16 01:37:47 | [diff] [blame] | 4112 | if return_construct_result and not HasMoreThanOneArgOrInvalid( |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4113 | return_construct_result.group('template_arg')): |
| 4114 | problems_constructor.append( |
| 4115 | '%s:%d\n %s' % (local_path, line_number, line.strip())) |
| 4116 | # Disallow: |
| 4117 | # std::unique_ptr<T>() |
| 4118 | if null_construct_pattern.search(line): |
| 4119 | problems_nullptr.append( |
| 4120 | '%s:%d\n %s' % (local_path, line_number, line.strip())) |
Vaclav Brozek | 851d960 | 2018-04-04 16:13:05 | [diff] [blame] | 4121 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4122 | errors = [] |
| 4123 | if problems_nullptr: |
| 4124 | errors.append( |
| 4125 | output_api.PresubmitPromptWarning( |
| 4126 | 'The following files use std::unique_ptr<T>(). Use nullptr instead.', |
| 4127 | problems_nullptr)) |
| 4128 | if problems_constructor: |
| 4129 | errors.append( |
| 4130 | output_api.PresubmitError( |
| 4131 | 'The following files use explicit std::unique_ptr constructor. ' |
| 4132 | 'Use std::make_unique<T>() instead, or use base::WrapUnique if ' |
| 4133 | 'std::make_unique is not an option.', problems_constructor)) |
| 4134 | return errors |
Peter Kasting | 4844e46e | 2018-02-23 07:27:10 | [diff] [blame] | 4135 | |
| 4136 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 4137 | def CheckUserActionUpdate(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4138 | """Checks if any new user action has been added.""" |
| 4139 | if any('actions.xml' == input_api.os_path.basename(f) |
| 4140 | for f in input_api.LocalPaths()): |
| 4141 | # If actions.xml is already included in the changelist, the PRESUBMIT |
| 4142 | # for actions.xml will do a more complete presubmit check. |
| 4143 | return [] |
| 4144 | |
| 4145 | file_inclusion_pattern = [r'.*\.(cc|mm)$'] |
| 4146 | files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 4147 | input_api.DEFAULT_FILES_TO_SKIP) |
| 4148 | file_filter = lambda f: input_api.FilterSourceFile( |
| 4149 | f, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip) |
| 4150 | |
| 4151 | action_re = r'[^a-zA-Z]UserMetricsAction\("([^"]*)' |
| 4152 | current_actions = None |
| 4153 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 4154 | for line_num, line in f.ChangedContents(): |
| 4155 | match = input_api.re.search(action_re, line) |
| 4156 | if match: |
| 4157 | # Loads contents in tools/metrics/actions/actions.xml to memory. It's |
| 4158 | # loaded only once. |
| 4159 | if not current_actions: |
Bruce Dawson | 6cb2d4d | 2023-03-01 21:35:09 | [diff] [blame] | 4160 | with open('tools/metrics/actions/actions.xml', |
| 4161 | encoding='utf-8') as actions_f: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4162 | current_actions = actions_f.read() |
| 4163 | # Search for the matched user action name in |current_actions|. |
| 4164 | for action_name in match.groups(): |
| 4165 | action = 'name="{0}"'.format(action_name) |
| 4166 | if action not in current_actions: |
| 4167 | return [ |
| 4168 | output_api.PresubmitPromptWarning( |
| 4169 | 'File %s line %d: %s is missing in ' |
| 4170 | 'tools/metrics/actions/actions.xml. Please run ' |
| 4171 | 'tools/metrics/actions/extract_actions.py to update.' |
| 4172 | % (f.LocalPath(), line_num, action_name)) |
| 4173 | ] |
[email protected] | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 4174 | return [] |
| 4175 | |
[email protected] | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 4176 | |
Daniel Cheng | 13ca61a88 | 2017-08-25 15:11:25 | [diff] [blame] | 4177 | def _ImportJSONCommentEater(input_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4178 | import sys |
| 4179 | sys.path = sys.path + [ |
| 4180 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 4181 | 'json_comment_eater') |
| 4182 | ] |
| 4183 | import json_comment_eater |
| 4184 | return json_comment_eater |
Daniel Cheng | 13ca61a88 | 2017-08-25 15:11:25 | [diff] [blame] | 4185 | |
| 4186 | |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 4187 | def _GetJSONParseError(input_api, filename, eat_comments=True): |
dcheng | e07de81 | 2016-06-20 19:27:17 | [diff] [blame] | 4188 | try: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4189 | contents = input_api.ReadFile(filename) |
| 4190 | if eat_comments: |
| 4191 | json_comment_eater = _ImportJSONCommentEater(input_api) |
| 4192 | contents = json_comment_eater.Nom(contents) |
dcheng | e07de81 | 2016-06-20 19:27:17 | [diff] [blame] | 4193 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4194 | input_api.json.loads(contents) |
| 4195 | except ValueError as e: |
| 4196 | return e |
Andrew Grieve | 4deedb1 | 2022-02-03 21:34:50 | [diff] [blame] | 4197 | return None |
| 4198 | |
| 4199 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4200 | def _GetIDLParseError(input_api, filename): |
| 4201 | try: |
| 4202 | contents = input_api.ReadFile(filename) |
Devlin Cronin | f7582a1 | 2022-04-21 21:14:28 | [diff] [blame] | 4203 | for i, char in enumerate(contents): |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4204 | if not char.isascii(): |
| 4205 | return ( |
| 4206 | 'Non-ascii character "%s" (ord %d) found at offset %d.' % |
| 4207 | (char, ord(char), i)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4208 | idl_schema = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 4209 | 'tools', 'json_schema_compiler', |
| 4210 | 'idl_schema.py') |
| 4211 | process = input_api.subprocess.Popen( |
Bruce Dawson | 679fb08 | 2022-04-14 00:47:28 | [diff] [blame] | 4212 | [input_api.python3_executable, idl_schema], |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4213 | stdin=input_api.subprocess.PIPE, |
| 4214 | stdout=input_api.subprocess.PIPE, |
| 4215 | stderr=input_api.subprocess.PIPE, |
| 4216 | universal_newlines=True) |
| 4217 | (_, error) = process.communicate(input=contents) |
| 4218 | return error or None |
| 4219 | except ValueError as e: |
| 4220 | return e |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4221 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4222 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4223 | def CheckParseErrors(input_api, output_api): |
| 4224 | """Check that IDL and JSON files do not contain syntax errors.""" |
| 4225 | actions = { |
| 4226 | '.idl': _GetIDLParseError, |
| 4227 | '.json': _GetJSONParseError, |
| 4228 | } |
| 4229 | # Most JSON files are preprocessed and support comments, but these do not. |
| 4230 | json_no_comments_patterns = [ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4231 | r'^testing/', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4232 | ] |
| 4233 | # Only run IDL checker on files in these directories. |
| 4234 | idl_included_patterns = [ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4235 | r'^chrome/common/extensions/api/', |
| 4236 | r'^extensions/common/api/', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4237 | ] |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4238 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4239 | def get_action(affected_file): |
| 4240 | filename = affected_file.LocalPath() |
| 4241 | return actions.get(input_api.os_path.splitext(filename)[1]) |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4242 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4243 | def FilterFile(affected_file): |
| 4244 | action = get_action(affected_file) |
| 4245 | if not action: |
| 4246 | return False |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4247 | path = affected_file.UnixLocalPath() |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4248 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4249 | if _MatchesFile(input_api, |
| 4250 | _KNOWN_TEST_DATA_AND_INVALID_JSON_FILE_PATTERNS, path): |
| 4251 | return False |
| 4252 | |
| 4253 | if (action == _GetIDLParseError |
| 4254 | and not _MatchesFile(input_api, idl_included_patterns, path)): |
| 4255 | return False |
| 4256 | return True |
| 4257 | |
| 4258 | results = [] |
| 4259 | for affected_file in input_api.AffectedFiles(file_filter=FilterFile, |
| 4260 | include_deletes=False): |
| 4261 | action = get_action(affected_file) |
| 4262 | kwargs = {} |
| 4263 | if (action == _GetJSONParseError |
| 4264 | and _MatchesFile(input_api, json_no_comments_patterns, |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4265 | affected_file.UnixLocalPath())): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4266 | kwargs['eat_comments'] = False |
| 4267 | parse_error = action(input_api, affected_file.AbsoluteLocalPath(), |
| 4268 | **kwargs) |
| 4269 | if parse_error: |
| 4270 | results.append( |
| 4271 | output_api.PresubmitError( |
| 4272 | '%s could not be parsed: %s' % |
| 4273 | (affected_file.LocalPath(), parse_error))) |
| 4274 | return results |
| 4275 | |
| 4276 | |
| 4277 | def CheckJavaStyle(input_api, output_api): |
| 4278 | """Runs checkstyle on changed java files and returns errors if any exist.""" |
| 4279 | |
| 4280 | # Return early if no java files were modified. |
| 4281 | if not any( |
| 4282 | _IsJavaFile(input_api, f.LocalPath()) |
| 4283 | for f in input_api.AffectedFiles()): |
| 4284 | return [] |
| 4285 | |
| 4286 | import sys |
| 4287 | original_sys_path = sys.path |
| 4288 | try: |
| 4289 | sys.path = sys.path + [ |
| 4290 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 4291 | 'android', 'checkstyle') |
| 4292 | ] |
| 4293 | import checkstyle |
| 4294 | finally: |
| 4295 | # Restore sys.path to what it was before. |
| 4296 | sys.path = original_sys_path |
| 4297 | |
Andrew Grieve | 4f88e3ca | 2022-11-22 19:09:20 | [diff] [blame] | 4298 | return checkstyle.run_presubmit( |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4299 | input_api, |
| 4300 | output_api, |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4301 | files_to_skip=_EXCLUDED_PATHS + input_api.DEFAULT_FILES_TO_SKIP) |
| 4302 | |
| 4303 | |
| 4304 | def CheckPythonDevilInit(input_api, output_api): |
| 4305 | """Checks to make sure devil is initialized correctly in python scripts.""" |
| 4306 | script_common_initialize_pattern = input_api.re.compile( |
| 4307 | r'script_common\.InitializeEnvironment\(') |
| 4308 | devil_env_config_initialize = input_api.re.compile( |
| 4309 | r'devil_env\.config\.Initialize\(') |
| 4310 | |
| 4311 | errors = [] |
| 4312 | |
| 4313 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 4314 | affected_file, |
| 4315 | files_to_skip=(_EXCLUDED_PATHS + input_api.DEFAULT_FILES_TO_SKIP + ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4316 | r'^build/android/devil_chromium\.py', |
Sven Zheng | 8e07956 | 2024-05-10 20:11:06 | [diff] [blame] | 4317 | r'^tools/bisect-builds\.py', |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4318 | r'^third_party/.*', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4319 | )), |
| 4320 | files_to_check=[r'.*\.py$']) |
| 4321 | |
| 4322 | for f in input_api.AffectedSourceFiles(sources): |
| 4323 | for line_num, line in f.ChangedContents(): |
| 4324 | if (script_common_initialize_pattern.search(line) |
| 4325 | or devil_env_config_initialize.search(line)): |
| 4326 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4327 | |
| 4328 | results = [] |
| 4329 | |
| 4330 | if errors: |
| 4331 | results.append( |
| 4332 | output_api.PresubmitError( |
| 4333 | 'Devil initialization should always be done using ' |
| 4334 | 'devil_chromium.Initialize() in the chromium project, to use better ' |
| 4335 | 'defaults for dependencies (ex. up-to-date version of adb).', |
| 4336 | errors)) |
| 4337 | |
| 4338 | return results |
| 4339 | |
| 4340 | |
| 4341 | def _MatchesFile(input_api, patterns, path): |
| 4342 | for pattern in patterns: |
| 4343 | if input_api.re.search(pattern, path): |
| 4344 | return True |
| 4345 | return False |
| 4346 | |
| 4347 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4348 | def _ChangeHasSecurityReviewer(input_api, owners_file): |
| 4349 | """Returns True iff the CL has a reviewer from SECURITY_OWNERS. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4350 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4351 | Args: |
| 4352 | input_api: The presubmit input API. |
| 4353 | owners_file: OWNERS file with required reviewers. Typically, this is |
| 4354 | something like ipc/SECURITY_OWNERS. |
| 4355 | |
| 4356 | Note: if the presubmit is running for commit rather than for upload, this |
| 4357 | only returns True if a security reviewer has also approved the CL. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4358 | """ |
Daniel Cheng | d8824447 | 2022-05-16 09:08:47 | [diff] [blame] | 4359 | # Owners-Override should bypass all additional OWNERS enforcement checks. |
| 4360 | # A CR+1 vote will still be required to land this change. |
| 4361 | if (input_api.change.issue and input_api.gerrit.IsOwnersOverrideApproved( |
| 4362 | input_api.change.issue)): |
| 4363 | return True |
| 4364 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4365 | owner_email, reviewers = ( |
| 4366 | input_api.canned_checks.GetCodereviewOwnerAndReviewers( |
Daniel Cheng | 3008dc1 | 2022-05-13 04:02:11 | [diff] [blame] | 4367 | input_api, |
| 4368 | None, |
| 4369 | approval_needed=input_api.is_committing and not input_api.dry_run)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4370 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4371 | security_owners = input_api.owners_client.ListOwners(owners_file) |
| 4372 | return any(owner in reviewers for owner in security_owners) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4373 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4374 | |
| 4375 | @dataclass |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4376 | class _SecurityProblemWithItems: |
| 4377 | problem: str |
| 4378 | items: Sequence[str] |
| 4379 | |
| 4380 | |
| 4381 | @dataclass |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4382 | class _MissingSecurityOwnersResult: |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4383 | owners_file_problems: Sequence[_SecurityProblemWithItems] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4384 | has_security_sensitive_files: bool |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4385 | missing_reviewer_problem: Optional[_SecurityProblemWithItems] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4386 | |
| 4387 | |
| 4388 | def _FindMissingSecurityOwners(input_api, |
| 4389 | output_api, |
| 4390 | file_patterns: Sequence[str], |
| 4391 | excluded_patterns: Sequence[str], |
| 4392 | required_owners_file: str, |
| 4393 | custom_rule_function: Optional[Callable] = None |
| 4394 | ) -> _MissingSecurityOwnersResult: |
| 4395 | """Find OWNERS files missing per-file rules for security-sensitive files. |
| 4396 | |
| 4397 | Args: |
| 4398 | input_api: the PRESUBMIT input API object. |
| 4399 | output_api: the PRESUBMIT output API object. |
| 4400 | file_patterns: basename patterns that require a corresponding per-file |
| 4401 | security restriction. |
| 4402 | excluded_patterns: path patterns that should be exempted from |
| 4403 | requiring a security restriction. |
| 4404 | required_owners_file: path to the required OWNERS file, e.g. |
| 4405 | ipc/SECURITY_OWNERS |
| 4406 | cc_alias: If not None, email that will be CCed automatically if the |
| 4407 | change contains security-sensitive files, as determined by |
| 4408 | `file_patterns` and `excluded_patterns`. |
| 4409 | custom_rule_function: If not None, will be called with `input_api` and |
| 4410 | the current file under consideration. Returning True will add an |
| 4411 | exact match per-file rule check for the current file. |
| 4412 | """ |
| 4413 | |
| 4414 | # `to_check` is a mapping of an OWNERS file path to Patterns. |
| 4415 | # |
| 4416 | # Patterns is a dictionary mapping glob patterns (suitable for use in |
| 4417 | # per-file rules) to a PatternEntry. |
| 4418 | # |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4419 | # PatternEntry is a dictionary with two keys: |
| 4420 | # - 'files': the files that are matched by this pattern |
| 4421 | # - 'rules': the per-file rules needed for this pattern |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4422 | # |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4423 | # For example, if we expect OWNERS file to contain rules for *.mojom and |
| 4424 | # *_struct_traits*.*, Patterns might look like this: |
| 4425 | # { |
| 4426 | # '*.mojom': { |
| 4427 | # 'files': ..., |
| 4428 | # 'rules': [ |
| 4429 | # 'per-file *.mojom=set noparent', |
| 4430 | # 'per-file *.mojom=file://ipc/SECURITY_OWNERS', |
| 4431 | # ], |
| 4432 | # }, |
| 4433 | # '*_struct_traits*.*': { |
| 4434 | # 'files': ..., |
| 4435 | # 'rules': [ |
| 4436 | # 'per-file *_struct_traits*.*=set noparent', |
| 4437 | # 'per-file *_struct_traits*.*=file://ipc/SECURITY_OWNERS', |
| 4438 | # ], |
| 4439 | # }, |
| 4440 | # } |
| 4441 | to_check = {} |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4442 | files_to_review = [] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4443 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4444 | def AddPatternToCheck(file, pattern): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4445 | owners_file = input_api.os_path.join( |
Daniel Cheng | d8824447 | 2022-05-16 09:08:47 | [diff] [blame] | 4446 | input_api.os_path.dirname(file.LocalPath()), 'OWNERS') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4447 | if owners_file not in to_check: |
| 4448 | to_check[owners_file] = {} |
| 4449 | if pattern not in to_check[owners_file]: |
| 4450 | to_check[owners_file][pattern] = { |
| 4451 | 'files': [], |
| 4452 | 'rules': [ |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4453 | f'per-file {pattern}=set noparent', |
| 4454 | f'per-file {pattern}=file://{required_owners_file}', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4455 | ] |
| 4456 | } |
Daniel Cheng | ed57a16 | 2022-05-25 02:56:34 | [diff] [blame] | 4457 | to_check[owners_file][pattern]['files'].append(file.LocalPath()) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4458 | files_to_review.append(file.LocalPath()) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4459 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4460 | # Only enforce security OWNERS rules for a directory if that directory has a |
| 4461 | # file that matches `file_patterns`. For example, if a directory only |
| 4462 | # contains *.mojom files and no *_messages*.h files, the check should only |
| 4463 | # ensure that rules for *.mojom files are present. |
| 4464 | for file in input_api.AffectedFiles(include_deletes=False): |
| 4465 | file_basename = input_api.os_path.basename(file.LocalPath()) |
| 4466 | if custom_rule_function is not None and custom_rule_function( |
| 4467 | input_api, file): |
| 4468 | AddPatternToCheck(file, file_basename) |
| 4469 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4470 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4471 | if any( |
| 4472 | input_api.fnmatch.fnmatch(file.LocalPath(), pattern) |
| 4473 | for pattern in excluded_patterns): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4474 | continue |
| 4475 | |
| 4476 | for pattern in file_patterns: |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4477 | # Unlike `excluded_patterns`, `file_patterns` is checked only against the |
| 4478 | # file's basename. |
| 4479 | if input_api.fnmatch.fnmatch(file_basename, pattern): |
| 4480 | AddPatternToCheck(file, pattern) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4481 | break |
| 4482 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4483 | has_security_sensitive_files = bool(to_check) |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4484 | |
| 4485 | # Check if any newly added lines in OWNERS files intersect with required |
| 4486 | # per-file OWNERS lines. If so, ensure that a security reviewer is included. |
| 4487 | # This is a hack, but is needed because the OWNERS check (by design) ignores |
| 4488 | # new OWNERS entries; otherwise, a non-owner could add someone as a new |
| 4489 | # OWNER and have that newly-added OWNER self-approve their own addition. |
| 4490 | newly_covered_files = [] |
| 4491 | for file in input_api.AffectedFiles(include_deletes=False): |
| 4492 | if not file.LocalPath() in to_check: |
| 4493 | continue |
| 4494 | for _, line in file.ChangedContents(): |
| 4495 | for _, entry in to_check[file.LocalPath()].items(): |
| 4496 | if line in entry['rules']: |
| 4497 | newly_covered_files.extend(entry['files']) |
| 4498 | |
| 4499 | missing_reviewer_problems = None |
| 4500 | if newly_covered_files and not _ChangeHasSecurityReviewer( |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4501 | input_api, required_owners_file): |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4502 | missing_reviewer_problems = _SecurityProblemWithItems( |
| 4503 | f'Review from an owner in {required_owners_file} is required for ' |
| 4504 | 'the following newly-added files:', |
| 4505 | [f'{file}' for file in sorted(set(newly_covered_files))]) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4506 | |
| 4507 | # Go through the OWNERS files to check, filtering out rules that are already |
| 4508 | # present in that OWNERS file. |
| 4509 | for owners_file, patterns in to_check.items(): |
| 4510 | try: |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4511 | lines = set( |
| 4512 | input_api.ReadFile( |
| 4513 | input_api.os_path.join(input_api.change.RepositoryRoot(), |
| 4514 | owners_file)).splitlines()) |
| 4515 | for entry in patterns.values(): |
| 4516 | entry['rules'] = [ |
| 4517 | rule for rule in entry['rules'] if rule not in lines |
| 4518 | ] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4519 | except IOError: |
| 4520 | # No OWNERS file, so all the rules are definitely missing. |
| 4521 | continue |
| 4522 | |
| 4523 | # 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] | 4524 | owners_file_problems = [] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4525 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4526 | for owners_file, patterns in to_check.items(): |
| 4527 | missing_lines = [] |
| 4528 | files = [] |
| 4529 | for _, entry in patterns.items(): |
Daniel Cheng | ed57a16 | 2022-05-25 02:56:34 | [diff] [blame] | 4530 | files.extend(entry['files']) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4531 | missing_lines.extend(entry['rules']) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4532 | if missing_lines: |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4533 | joined_missing_lines = '\n'.join(line for line in missing_lines) |
| 4534 | owners_file_problems.append( |
| 4535 | _SecurityProblemWithItems( |
| 4536 | 'Found missing OWNERS lines for security-sensitive files. ' |
| 4537 | f'Please add the following lines to {owners_file}:\n' |
| 4538 | f'{joined_missing_lines}\n\nTo ensure security review for:', |
| 4539 | files)) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4540 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4541 | return _MissingSecurityOwnersResult(owners_file_problems, |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4542 | has_security_sensitive_files, |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4543 | missing_reviewer_problems) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4544 | |
| 4545 | |
| 4546 | def _CheckChangeForIpcSecurityOwners(input_api, output_api): |
| 4547 | # Whether or not a file affects IPC is (mostly) determined by a simple list |
| 4548 | # of filename patterns. |
| 4549 | file_patterns = [ |
| 4550 | # Legacy IPC: |
| 4551 | '*_messages.cc', |
| 4552 | '*_messages*.h', |
| 4553 | '*_param_traits*.*', |
| 4554 | # Mojo IPC: |
| 4555 | '*.mojom', |
| 4556 | '*_mojom_traits*.*', |
| 4557 | '*_type_converter*.*', |
| 4558 | # Android native IPC: |
| 4559 | '*.aidl', |
| 4560 | ] |
| 4561 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4562 | excluded_patterns = [ |
Daniel Cheng | 518943f | 2022-05-12 22:15:46 | [diff] [blame] | 4563 | # These third_party directories do not contain IPCs, but contain files |
| 4564 | # matching the above patterns, which trigger false positives. |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4565 | 'third_party/crashpad/*', |
| 4566 | 'third_party/blink/renderer/platform/bindings/*', |
Evan Stade | 23a77da | 2025-02-06 21:15:31 | [diff] [blame] | 4567 | 'third_party/protobuf/*', |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4568 | 'third_party/win_build_output/*', |
Daniel Cheng | d8824447 | 2022-05-16 09:08:47 | [diff] [blame] | 4569 | # Enum-only mojoms used for web metrics, so no security review needed. |
| 4570 | 'third_party/blink/public/mojom/use_counter/metrics/*', |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4571 | # These files are just used to communicate between class loaders running |
| 4572 | # in the same process. |
| 4573 | 'weblayer/browser/java/org/chromium/weblayer_private/interfaces/*', |
| 4574 | 'weblayer/browser/java/org/chromium/weblayer_private/test_interfaces/*', |
| 4575 | ] |
| 4576 | |
| 4577 | def IsMojoServiceManifestFile(input_api, file): |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 4578 | manifest_pattern = input_api.re.compile(r'manifests?\.(cc|h)$') |
| 4579 | test_manifest_pattern = input_api.re.compile(r'test_manifests?\.(cc|h)') |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4580 | if not manifest_pattern.search(file.LocalPath()): |
| 4581 | return False |
| 4582 | |
| 4583 | if test_manifest_pattern.search(file.LocalPath()): |
| 4584 | return False |
| 4585 | |
| 4586 | # All actual service manifest files should contain at least one |
| 4587 | # qualified reference to service_manager::Manifest. |
| 4588 | return any('service_manager::Manifest' in line |
| 4589 | for line in file.NewContents()) |
| 4590 | |
| 4591 | return _FindMissingSecurityOwners( |
| 4592 | input_api, |
| 4593 | output_api, |
| 4594 | file_patterns, |
| 4595 | excluded_patterns, |
| 4596 | 'ipc/SECURITY_OWNERS', |
| 4597 | custom_rule_function=IsMojoServiceManifestFile) |
| 4598 | |
| 4599 | |
| 4600 | def _CheckChangeForFuchsiaSecurityOwners(input_api, output_api): |
| 4601 | file_patterns = [ |
| 4602 | # Component specifications. |
| 4603 | '*.cml', # Component Framework v2. |
| 4604 | '*.cmx', # Component Framework v1. |
| 4605 | |
| 4606 | # Fuchsia IDL protocol specifications. |
| 4607 | '*.fidl', |
| 4608 | ] |
| 4609 | |
| 4610 | # Don't check for owners files for changes in these directories. |
| 4611 | excluded_patterns = [ |
| 4612 | 'third_party/crashpad/*', |
| 4613 | ] |
| 4614 | |
| 4615 | return _FindMissingSecurityOwners(input_api, output_api, file_patterns, |
| 4616 | excluded_patterns, |
| 4617 | 'build/fuchsia/SECURITY_OWNERS') |
| 4618 | |
| 4619 | |
| 4620 | def CheckSecurityOwners(input_api, output_api): |
| 4621 | """Checks that various security-sensitive files have an IPC OWNERS rule.""" |
| 4622 | ipc_results = _CheckChangeForIpcSecurityOwners(input_api, output_api) |
| 4623 | fuchsia_results = _CheckChangeForFuchsiaSecurityOwners( |
| 4624 | input_api, output_api) |
| 4625 | |
| 4626 | if ipc_results.has_security_sensitive_files: |
| 4627 | output_api.AppendCC('[email protected]') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4628 | |
| 4629 | results = [] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4630 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4631 | missing_reviewer_problems = [] |
| 4632 | if ipc_results.missing_reviewer_problem: |
| 4633 | missing_reviewer_problems.append(ipc_results.missing_reviewer_problem) |
| 4634 | if fuchsia_results.missing_reviewer_problem: |
| 4635 | missing_reviewer_problems.append( |
| 4636 | fuchsia_results.missing_reviewer_problem) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4637 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4638 | # Missing reviewers are an error unless there's no issue number |
| 4639 | # associated with this branch; in that case, the presubmit is being run |
| 4640 | # with --all or --files. |
| 4641 | # |
| 4642 | # Note that upload should never be an error; otherwise, it would be |
| 4643 | # impossible to upload changes at all. |
| 4644 | if input_api.is_committing and input_api.change.issue: |
| 4645 | make_presubmit_message = output_api.PresubmitError |
| 4646 | else: |
| 4647 | make_presubmit_message = output_api.PresubmitNotifyResult |
| 4648 | for problem in missing_reviewer_problems: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4649 | results.append( |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4650 | make_presubmit_message(problem.problem, items=problem.items)) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4651 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4652 | owners_file_problems = [] |
| 4653 | owners_file_problems.extend(ipc_results.owners_file_problems) |
| 4654 | owners_file_problems.extend(fuchsia_results.owners_file_problems) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4655 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4656 | for problem in owners_file_problems: |
Daniel Cheng | 3008dc1 | 2022-05-13 04:02:11 | [diff] [blame] | 4657 | # Missing per-file rules are always an error. While swarming and caching |
| 4658 | # means that uploading a patchset with updated OWNERS files and sending |
| 4659 | # it to the CQ again should not have a large incremental cost, it is |
| 4660 | # still frustrating to discover the error only after the change has |
| 4661 | # already been uploaded. |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4662 | results.append( |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4663 | output_api.PresubmitError(problem.problem, items=problem.items)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4664 | |
| 4665 | return results |
| 4666 | |
| 4667 | |
| 4668 | def _GetFilesUsingSecurityCriticalFunctions(input_api): |
| 4669 | """Checks affected files for changes to security-critical calls. This |
| 4670 | function checks the full change diff, to catch both additions/changes |
| 4671 | and removals. |
| 4672 | |
| 4673 | Returns a dict keyed by file name, and the value is a set of detected |
| 4674 | functions. |
| 4675 | """ |
| 4676 | # Map of function pretty name (displayed in an error) to the pattern to |
| 4677 | # match it with. |
| 4678 | _PATTERNS_TO_CHECK = { |
| 4679 | 'content::GetServiceSandboxType<>()': 'GetServiceSandboxType\\<' |
| 4680 | } |
| 4681 | _PATTERNS_TO_CHECK = { |
| 4682 | k: input_api.re.compile(v) |
| 4683 | for k, v in _PATTERNS_TO_CHECK.items() |
| 4684 | } |
| 4685 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4686 | # We don't want to trigger on strings within this file. |
| 4687 | def presubmit_file_filter(f): |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4688 | return 'PRESUBMIT.py' != input_api.os_path.split(f.LocalPath())[1] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4689 | |
| 4690 | # Scan all affected files for changes touching _FUNCTIONS_TO_CHECK. |
| 4691 | files_to_functions = {} |
| 4692 | for f in input_api.AffectedFiles(file_filter=presubmit_file_filter): |
| 4693 | diff = f.GenerateScmDiff() |
| 4694 | for line in diff.split('\n'): |
| 4695 | # Not using just RightHandSideLines() because removing a |
| 4696 | # call to a security-critical function can be just as important |
| 4697 | # as adding or changing the arguments. |
| 4698 | if line.startswith('-') or (line.startswith('+') |
| 4699 | and not line.startswith('++')): |
| 4700 | for name, pattern in _PATTERNS_TO_CHECK.items(): |
| 4701 | if pattern.search(line): |
| 4702 | path = f.LocalPath() |
| 4703 | if not path in files_to_functions: |
| 4704 | files_to_functions[path] = set() |
| 4705 | files_to_functions[path].add(name) |
| 4706 | return files_to_functions |
| 4707 | |
| 4708 | |
| 4709 | def CheckSecurityChanges(input_api, output_api): |
| 4710 | """Checks that changes involving security-critical functions are reviewed |
| 4711 | by the security team. |
| 4712 | """ |
| 4713 | files_to_functions = _GetFilesUsingSecurityCriticalFunctions(input_api) |
| 4714 | if not len(files_to_functions): |
| 4715 | return [] |
| 4716 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4717 | owners_file = 'ipc/SECURITY_OWNERS' |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4718 | if _ChangeHasSecurityReviewer(input_api, owners_file): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4719 | return [] |
| 4720 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4721 | msg = 'The following files change calls to security-sensitive functions\n' \ |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4722 | 'that need to be reviewed by {}.\n'.format(owners_file) |
| 4723 | for path, names in files_to_functions.items(): |
| 4724 | msg += ' {}\n'.format(path) |
| 4725 | for name in names: |
| 4726 | msg += ' {}\n'.format(name) |
| 4727 | msg += '\n' |
| 4728 | |
| 4729 | if input_api.is_committing: |
| 4730 | output = output_api.PresubmitError |
Mohamed Heikal | e217fc85 | 2020-07-06 19:44:03 | [diff] [blame] | 4731 | else: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4732 | output = output_api.PresubmitNotifyResult |
| 4733 | return [output(msg)] |
| 4734 | |
| 4735 | |
| 4736 | def CheckSetNoParent(input_api, output_api): |
| 4737 | """Checks that set noparent is only used together with an OWNERS file in |
| 4738 | //build/OWNERS.setnoparent (see also |
| 4739 | //docs/code_reviews.md#owners-files-details) |
| 4740 | """ |
| 4741 | # Return early if no OWNERS files were modified. |
| 4742 | if not any(f.LocalPath().endswith('OWNERS') |
| 4743 | for f in input_api.AffectedFiles(include_deletes=False)): |
| 4744 | return [] |
| 4745 | |
| 4746 | errors = [] |
| 4747 | |
| 4748 | allowed_owners_files_file = 'build/OWNERS.setnoparent' |
| 4749 | allowed_owners_files = set() |
Bruce Dawson | 58a45d2 | 2023-02-27 11:24:16 | [diff] [blame] | 4750 | with open(allowed_owners_files_file, 'r', encoding='utf-8') as f: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4751 | for line in f: |
| 4752 | line = line.strip() |
| 4753 | if not line or line.startswith('#'): |
| 4754 | continue |
| 4755 | allowed_owners_files.add(line) |
| 4756 | |
| 4757 | per_file_pattern = input_api.re.compile('per-file (.+)=(.+)') |
| 4758 | |
| 4759 | for f in input_api.AffectedFiles(include_deletes=False): |
| 4760 | if not f.LocalPath().endswith('OWNERS'): |
| 4761 | continue |
| 4762 | |
| 4763 | found_owners_files = set() |
| 4764 | found_set_noparent_lines = dict() |
| 4765 | |
| 4766 | # Parse the OWNERS file. |
| 4767 | for lineno, line in enumerate(f.NewContents(), 1): |
| 4768 | line = line.strip() |
| 4769 | if line.startswith('set noparent'): |
| 4770 | found_set_noparent_lines[''] = lineno |
| 4771 | if line.startswith('file://'): |
| 4772 | if line in allowed_owners_files: |
| 4773 | found_owners_files.add('') |
| 4774 | if line.startswith('per-file'): |
| 4775 | match = per_file_pattern.match(line) |
| 4776 | if match: |
| 4777 | glob = match.group(1).strip() |
| 4778 | directive = match.group(2).strip() |
| 4779 | if directive == 'set noparent': |
| 4780 | found_set_noparent_lines[glob] = lineno |
| 4781 | if directive.startswith('file://'): |
| 4782 | if directive in allowed_owners_files: |
| 4783 | found_owners_files.add(glob) |
| 4784 | |
| 4785 | # Check that every set noparent line has a corresponding file:// line |
| 4786 | # listed in build/OWNERS.setnoparent. An exception is made for top level |
| 4787 | # directories since src/OWNERS shouldn't review them. |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4788 | linux_path = f.UnixLocalPath() |
Bruce Dawson | 6bb0d67 | 2022-04-06 15:13:49 | [diff] [blame] | 4789 | if (linux_path.count('/') != 1 |
| 4790 | and (not linux_path in _EXCLUDED_SET_NO_PARENT_PATHS)): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4791 | for set_noparent_line in found_set_noparent_lines: |
| 4792 | if set_noparent_line in found_owners_files: |
| 4793 | continue |
| 4794 | errors.append(' %s:%d' % |
Bruce Dawson | 6bb0d67 | 2022-04-06 15:13:49 | [diff] [blame] | 4795 | (linux_path, |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4796 | found_set_noparent_lines[set_noparent_line])) |
| 4797 | |
| 4798 | results = [] |
| 4799 | if errors: |
| 4800 | if input_api.is_committing: |
| 4801 | output = output_api.PresubmitError |
| 4802 | else: |
| 4803 | output = output_api.PresubmitPromptWarning |
| 4804 | results.append( |
| 4805 | output( |
| 4806 | 'Found the following "set noparent" restrictions in OWNERS files that ' |
| 4807 | 'do not include owners from build/OWNERS.setnoparent:', |
| 4808 | long_text='\n\n'.join(errors))) |
| 4809 | return results |
| 4810 | |
| 4811 | |
| 4812 | def CheckUselessForwardDeclarations(input_api, output_api): |
| 4813 | """Checks that added or removed lines in non third party affected |
| 4814 | header files do not lead to new useless class or struct forward |
| 4815 | declaration. |
| 4816 | """ |
| 4817 | results = [] |
| 4818 | class_pattern = input_api.re.compile(r'^class\s+(\w+);$', |
| 4819 | input_api.re.MULTILINE) |
| 4820 | struct_pattern = input_api.re.compile(r'^struct\s+(\w+);$', |
| 4821 | input_api.re.MULTILINE) |
| 4822 | for f in input_api.AffectedFiles(include_deletes=False): |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4823 | local_path = f.UnixLocalPath() |
| 4824 | if (local_path.startswith('third_party') |
| 4825 | and not local_path.startswith('third_party/blink')): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4826 | continue |
| 4827 | |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4828 | if not local_path.endswith('.h'): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4829 | continue |
| 4830 | |
| 4831 | contents = input_api.ReadFile(f) |
| 4832 | fwd_decls = input_api.re.findall(class_pattern, contents) |
| 4833 | fwd_decls.extend(input_api.re.findall(struct_pattern, contents)) |
| 4834 | |
| 4835 | useless_fwd_decls = [] |
| 4836 | for decl in fwd_decls: |
| 4837 | count = sum(1 for _ in input_api.re.finditer( |
| 4838 | r'\b%s\b' % input_api.re.escape(decl), contents)) |
| 4839 | if count == 1: |
| 4840 | useless_fwd_decls.append(decl) |
| 4841 | |
| 4842 | if not useless_fwd_decls: |
| 4843 | continue |
| 4844 | |
| 4845 | for line in f.GenerateScmDiff().splitlines(): |
| 4846 | if (line.startswith('-') and not line.startswith('--') |
| 4847 | or line.startswith('+') and not line.startswith('++')): |
| 4848 | for decl in useless_fwd_decls: |
| 4849 | if input_api.re.search(r'\b%s\b' % decl, line[1:]): |
| 4850 | results.append( |
| 4851 | output_api.PresubmitPromptWarning( |
| 4852 | '%s: %s forward declaration is no longer needed' |
| 4853 | % (f.LocalPath(), decl))) |
| 4854 | useless_fwd_decls.remove(decl) |
| 4855 | |
| 4856 | return results |
| 4857 | |
| 4858 | |
| 4859 | def _CheckAndroidDebuggableBuild(input_api, output_api): |
| 4860 | """Checks that code uses BuildInfo.isDebugAndroid() instead of |
| 4861 | Build.TYPE.equals('') or ''.equals(Build.TYPE) to check if |
| 4862 | this is a debuggable build of Android. |
| 4863 | """ |
| 4864 | build_type_check_pattern = input_api.re.compile( |
| 4865 | r'\bBuild\.TYPE\.equals\(|\.equals\(\s*\bBuild\.TYPE\)') |
| 4866 | |
| 4867 | errors = [] |
| 4868 | |
| 4869 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 4870 | affected_file, |
| 4871 | files_to_skip=( |
| 4872 | _EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api. |
| 4873 | DEFAULT_FILES_TO_SKIP + ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4874 | r"^android_webview/support_library/boundary_interfaces/", |
| 4875 | r"^chrome/android/webapk/.*", |
| 4876 | r'^third_party/.*', |
| 4877 | r"tools/android/customtabs_benchmark/.*", |
| 4878 | r"webview/chromium/License.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4879 | )), |
| 4880 | files_to_check=[r'.*\.java$']) |
| 4881 | |
| 4882 | for f in input_api.AffectedSourceFiles(sources): |
| 4883 | for line_num, line in f.ChangedContents(): |
| 4884 | if build_type_check_pattern.search(line): |
| 4885 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4886 | |
| 4887 | results = [] |
| 4888 | |
| 4889 | if errors: |
| 4890 | results.append( |
| 4891 | output_api.PresubmitPromptWarning( |
| 4892 | 'Build.TYPE.equals or .equals(Build.TYPE) usage is detected.' |
| 4893 | ' Please use BuildInfo.isDebugAndroid() instead.', errors)) |
| 4894 | |
| 4895 | return results |
| 4896 | |
| 4897 | # TODO: add unit tests |
| 4898 | def _CheckAndroidToastUsage(input_api, output_api): |
| 4899 | """Checks that code uses org.chromium.ui.widget.Toast instead of |
| 4900 | android.widget.Toast (Chromium Toast doesn't force hardware |
| 4901 | acceleration on low-end devices, saving memory). |
| 4902 | """ |
| 4903 | toast_import_pattern = input_api.re.compile( |
| 4904 | r'^import android\.widget\.Toast;$') |
| 4905 | |
| 4906 | errors = [] |
| 4907 | |
| 4908 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 4909 | affected_file, |
| 4910 | files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4911 | DEFAULT_FILES_TO_SKIP + (r'^chromecast/.*', |
| 4912 | r'^remoting/.*')), |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4913 | files_to_check=[r'.*\.java$']) |
| 4914 | |
| 4915 | for f in input_api.AffectedSourceFiles(sources): |
| 4916 | for line_num, line in f.ChangedContents(): |
| 4917 | if toast_import_pattern.search(line): |
| 4918 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4919 | |
| 4920 | results = [] |
| 4921 | |
| 4922 | if errors: |
| 4923 | results.append( |
| 4924 | output_api.PresubmitError( |
| 4925 | 'android.widget.Toast usage is detected. Android toasts use hardware' |
| 4926 | ' acceleration, and can be\ncostly on low-end devices. Please use' |
| 4927 | ' org.chromium.ui.widget.Toast instead.\n' |
| 4928 | 'Contact [email protected] if you have any questions.', |
| 4929 | errors)) |
| 4930 | |
| 4931 | return results |
| 4932 | |
| 4933 | |
| 4934 | def _CheckAndroidCrLogUsage(input_api, output_api): |
| 4935 | """Checks that new logs using org.chromium.base.Log: |
| 4936 | - Are using 'TAG' as variable name for the tags (warn) |
| 4937 | - Are using a tag that is shorter than 20 characters (error) |
| 4938 | """ |
| 4939 | |
| 4940 | # Do not check format of logs in the given files |
| 4941 | cr_log_check_excluded_paths = [ |
| 4942 | # //chrome/android/webapk cannot depend on //base |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4943 | r"^chrome/android/webapk/.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4944 | # WebView license viewer code cannot depend on //base; used in stub APK. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4945 | r"^android_webview/glue/java/src/com/android/" |
| 4946 | r"webview/chromium/License.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4947 | # The customtabs_benchmark is a small app that does not depend on Chromium |
| 4948 | # java pieces. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4949 | r"tools/android/customtabs_benchmark/.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4950 | ] |
| 4951 | |
| 4952 | cr_log_import_pattern = input_api.re.compile( |
| 4953 | r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE) |
| 4954 | class_in_base_pattern = input_api.re.compile( |
| 4955 | r'^package org\.chromium\.base;$', input_api.re.MULTILINE) |
| 4956 | has_some_log_import_pattern = input_api.re.compile(r'^import .*\.Log;$', |
| 4957 | input_api.re.MULTILINE) |
| 4958 | # Extract the tag from lines like `Log.d(TAG, "*");` or `Log.d("TAG", "*");` |
| 4959 | log_call_pattern = input_api.re.compile(r'\bLog\.\w\((?P<tag>\"?\w+)') |
| 4960 | log_decl_pattern = input_api.re.compile( |
| 4961 | r'static final String TAG = "(?P<name>(.*))"') |
| 4962 | rough_log_decl_pattern = input_api.re.compile(r'\bString TAG\s*=') |
| 4963 | |
| 4964 | REF_MSG = ('See docs/android_logging.md for more info.') |
| 4965 | sources = lambda x: input_api.FilterSourceFile( |
| 4966 | x, |
| 4967 | files_to_check=[r'.*\.java$'], |
| 4968 | files_to_skip=cr_log_check_excluded_paths) |
| 4969 | |
| 4970 | tag_decl_errors = [] |
Andrew Grieve | d3a35d8 | 2024-01-02 21:24:38 | [diff] [blame] | 4971 | tag_length_errors = [] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4972 | tag_errors = [] |
| 4973 | tag_with_dot_errors = [] |
| 4974 | util_log_errors = [] |
| 4975 | |
| 4976 | for f in input_api.AffectedSourceFiles(sources): |
| 4977 | file_content = input_api.ReadFile(f) |
| 4978 | has_modified_logs = False |
| 4979 | # Per line checks |
| 4980 | if (cr_log_import_pattern.search(file_content) |
| 4981 | or (class_in_base_pattern.search(file_content) |
| 4982 | and not has_some_log_import_pattern.search(file_content))): |
| 4983 | # Checks to run for files using cr log |
| 4984 | for line_num, line in f.ChangedContents(): |
| 4985 | if rough_log_decl_pattern.search(line): |
| 4986 | has_modified_logs = True |
| 4987 | |
| 4988 | # Check if the new line is doing some logging |
| 4989 | match = log_call_pattern.search(line) |
| 4990 | if match: |
| 4991 | has_modified_logs = True |
| 4992 | |
| 4993 | # Make sure it uses "TAG" |
| 4994 | if not match.group('tag') == 'TAG': |
| 4995 | tag_errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4996 | else: |
| 4997 | # Report non cr Log function calls in changed lines |
| 4998 | for line_num, line in f.ChangedContents(): |
| 4999 | if log_call_pattern.search(line): |
| 5000 | util_log_errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 5001 | |
| 5002 | # Per file checks |
| 5003 | if has_modified_logs: |
| 5004 | # Make sure the tag is using the "cr" prefix and is not too long |
| 5005 | match = log_decl_pattern.search(file_content) |
| 5006 | tag_name = match.group('name') if match else None |
| 5007 | if not tag_name: |
| 5008 | tag_decl_errors.append(f.LocalPath()) |
Andrew Grieve | d3a35d8 | 2024-01-02 21:24:38 | [diff] [blame] | 5009 | elif len(tag_name) > 20: |
| 5010 | tag_length_errors.append(f.LocalPath()) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5011 | elif '.' in tag_name: |
| 5012 | tag_with_dot_errors.append(f.LocalPath()) |
| 5013 | |
| 5014 | results = [] |
| 5015 | if tag_decl_errors: |
| 5016 | results.append( |
| 5017 | output_api.PresubmitPromptWarning( |
| 5018 | 'Please define your tags using the suggested format: .\n' |
| 5019 | '"private static final String TAG = "<package tag>".\n' |
| 5020 | 'They will be prepended with "cr_" automatically.\n' + REF_MSG, |
| 5021 | tag_decl_errors)) |
| 5022 | |
Andrew Grieve | d3a35d8 | 2024-01-02 21:24:38 | [diff] [blame] | 5023 | if tag_length_errors: |
| 5024 | results.append( |
| 5025 | output_api.PresubmitError( |
| 5026 | 'The tag length is restricted by the system to be at most ' |
| 5027 | '20 characters.\n' + REF_MSG, tag_length_errors)) |
| 5028 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5029 | if tag_errors: |
| 5030 | results.append( |
| 5031 | output_api.PresubmitPromptWarning( |
| 5032 | 'Please use a variable named "TAG" for your log tags.\n' + |
| 5033 | REF_MSG, tag_errors)) |
| 5034 | |
| 5035 | if util_log_errors: |
| 5036 | results.append( |
| 5037 | output_api.PresubmitPromptWarning( |
| 5038 | 'Please use org.chromium.base.Log for new logs.\n' + REF_MSG, |
| 5039 | util_log_errors)) |
| 5040 | |
| 5041 | if tag_with_dot_errors: |
| 5042 | results.append( |
| 5043 | output_api.PresubmitPromptWarning( |
| 5044 | 'Dot in log tags cause them to be elided in crash reports.\n' + |
| 5045 | REF_MSG, tag_with_dot_errors)) |
| 5046 | |
| 5047 | return results |
| 5048 | |
| 5049 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5050 | def _CheckAndroidTestAnnotationUsage(input_api, output_api): |
| 5051 | """Checks that android.test.suitebuilder.annotation.* is no longer used.""" |
| 5052 | deprecated_annotation_import_pattern = input_api.re.compile( |
| 5053 | r'^import android\.test\.suitebuilder\.annotation\..*;', |
| 5054 | input_api.re.MULTILINE) |
| 5055 | sources = lambda x: input_api.FilterSourceFile( |
| 5056 | x, files_to_check=[r'.*\.java$'], files_to_skip=None) |
| 5057 | errors = [] |
| 5058 | for f in input_api.AffectedFiles(file_filter=sources): |
| 5059 | for line_num, line in f.ChangedContents(): |
| 5060 | if deprecated_annotation_import_pattern.search(line): |
| 5061 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 5062 | |
| 5063 | results = [] |
| 5064 | if errors: |
| 5065 | results.append( |
| 5066 | output_api.PresubmitError( |
| 5067 | 'Annotations in android.test.suitebuilder.annotation have been' |
Mohamed Heikal | 3d7a94c | 2023-03-28 16:55:24 | [diff] [blame] | 5068 | ' deprecated since API level 24. Please use androidx.test.filters' |
| 5069 | ' from //third_party/androidx:androidx_test_runner_java instead.' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5070 | ' Contact [email protected] if you have any questions.', |
| 5071 | errors)) |
| 5072 | return results |
| 5073 | |
| 5074 | |
| 5075 | def _CheckAndroidNewMdpiAssetLocation(input_api, output_api): |
| 5076 | """Checks if MDPI assets are placed in a correct directory.""" |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 5077 | file_filter = lambda f: (f.UnixLocalPath().endswith( |
| 5078 | '.png') and ('/res/drawable/' in f. |
| 5079 | UnixLocalPath() or '/res/drawable-ldrtl/' in f.UnixLocalPath())) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5080 | errors = [] |
| 5081 | for f in input_api.AffectedFiles(include_deletes=False, |
| 5082 | file_filter=file_filter): |
| 5083 | errors.append(' %s' % f.LocalPath()) |
| 5084 | |
| 5085 | results = [] |
| 5086 | if errors: |
| 5087 | results.append( |
| 5088 | output_api.PresubmitError( |
| 5089 | 'MDPI assets should be placed in /res/drawable-mdpi/ or ' |
| 5090 | '/res/drawable-ldrtl-mdpi/\ninstead of /res/drawable/ and' |
| 5091 | '/res/drawable-ldrtl/.\n' |
| 5092 | 'Contact [email protected] if you have questions.', errors)) |
| 5093 | return results |
| 5094 | |
| 5095 | |
| 5096 | def _CheckAndroidWebkitImports(input_api, output_api): |
| 5097 | """Checks that code uses org.chromium.base.Callback instead of |
| 5098 | android.webview.ValueCallback except in the WebView glue layer |
| 5099 | and WebLayer. |
| 5100 | """ |
| 5101 | valuecallback_import_pattern = input_api.re.compile( |
| 5102 | r'^import android\.webkit\.ValueCallback;$') |
| 5103 | |
| 5104 | errors = [] |
| 5105 | |
| 5106 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 5107 | affected_file, |
| 5108 | files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api. |
| 5109 | DEFAULT_FILES_TO_SKIP + ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5110 | r'^android_webview/glue/.*', |
elabadysayed | cbbaea7 | 2024-08-01 16:10:42 | [diff] [blame] | 5111 | r'^android_webview/support_library/.*', |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5112 | r'^weblayer/.*', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5113 | )), |
| 5114 | files_to_check=[r'.*\.java$']) |
| 5115 | |
| 5116 | for f in input_api.AffectedSourceFiles(sources): |
| 5117 | for line_num, line in f.ChangedContents(): |
| 5118 | if valuecallback_import_pattern.search(line): |
| 5119 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 5120 | |
| 5121 | results = [] |
| 5122 | |
| 5123 | if errors: |
| 5124 | results.append( |
| 5125 | output_api.PresubmitError( |
| 5126 | 'android.webkit.ValueCallback usage is detected outside of the glue' |
| 5127 | ' layer. To stay compatible with the support library, android.webkit.*' |
| 5128 | ' classes should only be used inside the glue layer and' |
| 5129 | ' org.chromium.base.Callback should be used instead.', errors)) |
| 5130 | |
| 5131 | return results |
| 5132 | |
| 5133 | |
| 5134 | def _CheckAndroidXmlStyle(input_api, output_api, is_check_on_upload): |
| 5135 | """Checks Android XML styles """ |
| 5136 | |
| 5137 | # Return early if no relevant files were modified. |
| 5138 | if not any( |
| 5139 | _IsXmlOrGrdFile(input_api, f.LocalPath()) |
| 5140 | for f in input_api.AffectedFiles(include_deletes=False)): |
| 5141 | return [] |
| 5142 | |
| 5143 | import sys |
| 5144 | original_sys_path = sys.path |
| 5145 | try: |
| 5146 | sys.path = sys.path + [ |
| 5147 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 5148 | 'android', 'checkxmlstyle') |
| 5149 | ] |
| 5150 | import checkxmlstyle |
| 5151 | finally: |
| 5152 | # Restore sys.path to what it was before. |
| 5153 | sys.path = original_sys_path |
| 5154 | |
| 5155 | if is_check_on_upload: |
| 5156 | return checkxmlstyle.CheckStyleOnUpload(input_api, output_api) |
| 5157 | else: |
| 5158 | return checkxmlstyle.CheckStyleOnCommit(input_api, output_api) |
| 5159 | |
| 5160 | |
| 5161 | def _CheckAndroidInfoBarDeprecation(input_api, output_api): |
| 5162 | """Checks Android Infobar Deprecation """ |
| 5163 | |
| 5164 | import sys |
| 5165 | original_sys_path = sys.path |
| 5166 | try: |
| 5167 | sys.path = sys.path + [ |
| 5168 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 5169 | 'android', 'infobar_deprecation') |
| 5170 | ] |
| 5171 | import infobar_deprecation |
| 5172 | finally: |
| 5173 | # Restore sys.path to what it was before. |
| 5174 | sys.path = original_sys_path |
| 5175 | |
| 5176 | return infobar_deprecation.CheckDeprecationOnUpload(input_api, output_api) |
| 5177 | |
| 5178 | |
| 5179 | class _PydepsCheckerResult: |
| 5180 | def __init__(self, cmd, pydeps_path, process, old_contents): |
| 5181 | self._cmd = cmd |
| 5182 | self._pydeps_path = pydeps_path |
| 5183 | self._process = process |
| 5184 | self._old_contents = old_contents |
| 5185 | |
| 5186 | def GetError(self): |
| 5187 | """Returns an error message, or None.""" |
| 5188 | import difflib |
Andrew Grieve | d27620b6 | 2023-07-13 16:35:07 | [diff] [blame] | 5189 | new_contents = self._process.stdout.read().splitlines()[2:] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5190 | if self._process.wait() != 0: |
| 5191 | # STDERR should already be printed. |
| 5192 | return 'Command failed: ' + self._cmd |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5193 | if self._old_contents != new_contents: |
| 5194 | diff = '\n'.join( |
| 5195 | difflib.context_diff(self._old_contents, new_contents)) |
| 5196 | return ('File is stale: {}\n' |
| 5197 | 'Diff (apply to fix):\n' |
| 5198 | '{}\n' |
| 5199 | 'To regenerate, run:\n\n' |
| 5200 | ' {}').format(self._pydeps_path, diff, self._cmd) |
| 5201 | return None |
| 5202 | |
| 5203 | |
| 5204 | class PydepsChecker: |
| 5205 | def __init__(self, input_api, pydeps_files): |
| 5206 | self._file_cache = {} |
| 5207 | self._input_api = input_api |
| 5208 | self._pydeps_files = pydeps_files |
| 5209 | |
| 5210 | def _LoadFile(self, path): |
| 5211 | """Returns the list of paths within a .pydeps file relative to //.""" |
| 5212 | if path not in self._file_cache: |
| 5213 | with open(path, encoding='utf-8') as f: |
| 5214 | self._file_cache[path] = f.read() |
| 5215 | return self._file_cache[path] |
| 5216 | |
| 5217 | def _ComputeNormalizedPydepsEntries(self, pydeps_path): |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 5218 | """Returns an iterable of paths within the .pydep, relativized to //.""" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5219 | pydeps_data = self._LoadFile(pydeps_path) |
| 5220 | uses_gn_paths = '--gn-paths' in pydeps_data |
| 5221 | entries = (l for l in pydeps_data.splitlines() |
| 5222 | if not l.startswith('#')) |
| 5223 | if uses_gn_paths: |
| 5224 | # Paths look like: //foo/bar/baz |
| 5225 | return (e[2:] for e in entries) |
| 5226 | else: |
| 5227 | # Paths look like: path/relative/to/file.pydeps |
| 5228 | os_path = self._input_api.os_path |
| 5229 | pydeps_dir = os_path.dirname(pydeps_path) |
| 5230 | return (os_path.normpath(os_path.join(pydeps_dir, e)) |
| 5231 | for e in entries) |
| 5232 | |
| 5233 | def _CreateFilesToPydepsMap(self): |
| 5234 | """Returns a map of local_path -> list_of_pydeps.""" |
| 5235 | ret = {} |
| 5236 | for pydep_local_path in self._pydeps_files: |
| 5237 | for path in self._ComputeNormalizedPydepsEntries(pydep_local_path): |
| 5238 | ret.setdefault(path, []).append(pydep_local_path) |
| 5239 | return ret |
| 5240 | |
| 5241 | def ComputeAffectedPydeps(self): |
| 5242 | """Returns an iterable of .pydeps files that might need regenerating.""" |
| 5243 | affected_pydeps = set() |
| 5244 | file_to_pydeps_map = None |
| 5245 | for f in self._input_api.AffectedFiles(include_deletes=True): |
| 5246 | local_path = f.LocalPath() |
| 5247 | # Changes to DEPS can lead to .pydeps changes if any .py files are in |
| 5248 | # subrepositories. We can't figure out which files change, so re-check |
| 5249 | # all files. |
| 5250 | # Changes to print_python_deps.py affect all .pydeps. |
| 5251 | if local_path in ('DEPS', 'PRESUBMIT.py' |
| 5252 | ) or local_path.endswith('print_python_deps.py'): |
| 5253 | return self._pydeps_files |
| 5254 | elif local_path.endswith('.pydeps'): |
| 5255 | if local_path in self._pydeps_files: |
| 5256 | affected_pydeps.add(local_path) |
| 5257 | elif local_path.endswith('.py'): |
| 5258 | if file_to_pydeps_map is None: |
| 5259 | file_to_pydeps_map = self._CreateFilesToPydepsMap() |
| 5260 | affected_pydeps.update(file_to_pydeps_map.get(local_path, ())) |
| 5261 | return affected_pydeps |
| 5262 | |
| 5263 | def DetermineIfStaleAsync(self, pydeps_path): |
| 5264 | """Runs print_python_deps.py to see if the files is stale.""" |
| 5265 | import os |
| 5266 | |
| 5267 | old_pydeps_data = self._LoadFile(pydeps_path).splitlines() |
| 5268 | if old_pydeps_data: |
| 5269 | cmd = old_pydeps_data[1][1:].strip() |
| 5270 | if '--output' not in cmd: |
| 5271 | cmd += ' --output ' + pydeps_path |
| 5272 | old_contents = old_pydeps_data[2:] |
| 5273 | else: |
| 5274 | # A default cmd that should work in most cases (as long as pydeps filename |
| 5275 | # matches the script name) so that PRESUBMIT.py does not crash if pydeps |
| 5276 | # file is empty/new. |
| 5277 | cmd = 'build/print_python_deps.py {} --root={} --output={}'.format( |
| 5278 | pydeps_path[:-4], os.path.dirname(pydeps_path), pydeps_path) |
| 5279 | old_contents = [] |
| 5280 | env = dict(os.environ) |
| 5281 | env['PYTHONDONTWRITEBYTECODE'] = '1' |
| 5282 | process = self._input_api.subprocess.Popen( |
| 5283 | cmd + ' --output ""', |
| 5284 | shell=True, |
| 5285 | env=env, |
| 5286 | stdout=self._input_api.subprocess.PIPE, |
| 5287 | encoding='utf-8') |
| 5288 | return _PydepsCheckerResult(cmd, pydeps_path, process, old_contents) |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 5289 | |
| 5290 | |
Tibor Goldschwendt | 360793f7 | 2019-06-25 18:23:49 | [diff] [blame] | 5291 | def _ParseGclientArgs(): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5292 | args = {} |
| 5293 | with open('build/config/gclient_args.gni', 'r') as f: |
| 5294 | for line in f: |
| 5295 | line = line.strip() |
| 5296 | if not line or line.startswith('#'): |
| 5297 | continue |
| 5298 | attribute, value = line.split('=') |
| 5299 | args[attribute.strip()] = value.strip() |
| 5300 | return args |
Tibor Goldschwendt | 360793f7 | 2019-06-25 18:23:49 | [diff] [blame] | 5301 | |
| 5302 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5303 | def CheckPydepsNeedsUpdating(input_api, output_api, checker_for_tests=None): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5304 | """Checks if a .pydeps file needs to be regenerated.""" |
| 5305 | # This check is for Python dependency lists (.pydeps files), and involves |
| 5306 | # paths not only in the PRESUBMIT.py, but also in the .pydeps files. It |
| 5307 | # doesn't work on Windows and Mac, so skip it on other platforms. |
| 5308 | if not input_api.platform.startswith('linux'): |
| 5309 | return [] |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 5310 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5311 | results = [] |
| 5312 | # First, check for new / deleted .pydeps. |
| 5313 | for f in input_api.AffectedFiles(include_deletes=True): |
| 5314 | # Check whether we are running the presubmit check for a file in src. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5315 | if f.LocalPath().endswith('.pydeps'): |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 5316 | # f.LocalPath is relative to repo (src, or internal repo). |
| 5317 | # os_path.exists is relative to src repo. |
| 5318 | # Therefore if os_path.exists is true, it means f.LocalPath is relative |
| 5319 | # to src and we can conclude that the pydeps is in src. |
| 5320 | exists = input_api.os_path.exists(f.LocalPath()) |
| 5321 | if f.Action() == 'D' and f.LocalPath() in _ALL_PYDEPS_FILES: |
| 5322 | results.append( |
| 5323 | output_api.PresubmitError( |
| 5324 | 'Please update _ALL_PYDEPS_FILES within //PRESUBMIT.py to ' |
| 5325 | 'remove %s' % f.LocalPath())) |
| 5326 | elif (f.Action() != 'D' and exists |
| 5327 | and f.LocalPath() not in _ALL_PYDEPS_FILES): |
| 5328 | results.append( |
| 5329 | output_api.PresubmitError( |
| 5330 | 'Please update _ALL_PYDEPS_FILES within //PRESUBMIT.py to ' |
| 5331 | 'include %s' % f.LocalPath())) |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 5332 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5333 | if results: |
| 5334 | return results |
| 5335 | |
Gavin Mak | 2388440 | 2024-07-25 20:39:26 | [diff] [blame] | 5336 | try: |
| 5337 | parsed_args = _ParseGclientArgs() |
| 5338 | except FileNotFoundError: |
| 5339 | message = ( |
| 5340 | 'build/config/gclient_args.gni not found. Please make sure your ' |
| 5341 | 'workspace has been initialized with gclient sync.' |
| 5342 | ) |
| 5343 | import sys |
| 5344 | original_sys_path = sys.path |
| 5345 | try: |
| 5346 | sys.path = sys.path + [ |
| 5347 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 5348 | 'third_party', 'depot_tools') |
| 5349 | ] |
| 5350 | import gclient_utils |
| 5351 | if gclient_utils.IsEnvCog(): |
| 5352 | # Users will always hit this when they run presubmits before cog |
| 5353 | # workspace initialization finishes. The check shouldn't fail in |
| 5354 | # this case. This is an unavoidable workaround that's needed for |
| 5355 | # good presubmit UX for cog. |
| 5356 | results.append(output_api.PresubmitPromptWarning(message)) |
| 5357 | else: |
| 5358 | results.append(output_api.PresubmitError(message)) |
| 5359 | return results |
| 5360 | finally: |
| 5361 | # Restore sys.path to what it was before. |
| 5362 | sys.path = original_sys_path |
| 5363 | |
| 5364 | is_android = parsed_args.get('checkout_android', 'false') == 'true' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5365 | checker = checker_for_tests or PydepsChecker(input_api, _ALL_PYDEPS_FILES) |
| 5366 | affected_pydeps = set(checker.ComputeAffectedPydeps()) |
| 5367 | affected_android_pydeps = affected_pydeps.intersection( |
| 5368 | set(_ANDROID_SPECIFIC_PYDEPS_FILES)) |
| 5369 | if affected_android_pydeps and not is_android: |
| 5370 | results.append( |
| 5371 | output_api.PresubmitPromptOrNotify( |
| 5372 | 'You have changed python files that may affect pydeps for android\n' |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 5373 | 'specific scripts. However, the relevant presubmit check cannot be\n' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5374 | 'run because you are not using an Android checkout. To validate that\n' |
| 5375 | 'the .pydeps are correct, re-run presubmit in an Android checkout, or\n' |
| 5376 | 'use the android-internal-presubmit optional trybot.\n' |
| 5377 | 'Possibly stale pydeps files:\n{}'.format( |
| 5378 | '\n'.join(affected_android_pydeps)))) |
| 5379 | |
| 5380 | all_pydeps = _ALL_PYDEPS_FILES if is_android else _GENERIC_PYDEPS_FILES |
| 5381 | pydeps_to_check = affected_pydeps.intersection(all_pydeps) |
| 5382 | # Process these concurrently, as each one takes 1-2 seconds. |
| 5383 | pydep_results = [checker.DetermineIfStaleAsync(p) for p in pydeps_to_check] |
| 5384 | for result in pydep_results: |
| 5385 | error_msg = result.GetError() |
| 5386 | if error_msg: |
| 5387 | results.append(output_api.PresubmitError(error_msg)) |
| 5388 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 5389 | return results |
| 5390 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 5391 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5392 | def CheckSingletonInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5393 | """Checks to make sure no header files have |Singleton<|.""" |
| 5394 | |
| 5395 | def FileFilter(affected_file): |
| 5396 | # It's ok for base/memory/singleton.h to have |Singleton<|. |
| 5397 | files_to_skip = (_EXCLUDED_PATHS + input_api.DEFAULT_FILES_TO_SKIP + |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5398 | (r"^base/memory/singleton\.h$", |
| 5399 | r"^net/quic/platform/impl/quic_singleton_impl\.h$")) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5400 | return input_api.FilterSourceFile(affected_file, |
| 5401 | files_to_skip=files_to_skip) |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 5402 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5403 | pattern = input_api.re.compile(r'(?<!class\sbase::)Singleton\s*<') |
| 5404 | files = [] |
| 5405 | for f in input_api.AffectedSourceFiles(FileFilter): |
| 5406 | if (f.LocalPath().endswith('.h') or f.LocalPath().endswith('.hxx') |
| 5407 | or f.LocalPath().endswith('.hpp') |
| 5408 | or f.LocalPath().endswith('.inl')): |
| 5409 | contents = input_api.ReadFile(f) |
| 5410 | for line in contents.splitlines(False): |
| 5411 | if (not line.lstrip().startswith('//') |
| 5412 | and # Strip C++ comment. |
| 5413 | pattern.search(line)): |
| 5414 | files.append(f) |
| 5415 | break |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 5416 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5417 | if files: |
| 5418 | return [ |
| 5419 | output_api.PresubmitError( |
| 5420 | 'Found base::Singleton<T> in the following header files.\n' + |
| 5421 | 'Please move them to an appropriate source file so that the ' + |
| 5422 | 'template gets instantiated in a single compilation unit.', |
| 5423 | files) |
| 5424 | ] |
| 5425 | return [] |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 5426 | |
| 5427 | |
[email protected] | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 5428 | _DEPRECATED_CSS = [ |
| 5429 | # Values |
| 5430 | ( "-webkit-box", "flex" ), |
| 5431 | ( "-webkit-inline-box", "inline-flex" ), |
| 5432 | ( "-webkit-flex", "flex" ), |
| 5433 | ( "-webkit-inline-flex", "inline-flex" ), |
| 5434 | ( "-webkit-min-content", "min-content" ), |
| 5435 | ( "-webkit-max-content", "max-content" ), |
| 5436 | |
| 5437 | # Properties |
| 5438 | ( "-webkit-background-clip", "background-clip" ), |
| 5439 | ( "-webkit-background-origin", "background-origin" ), |
| 5440 | ( "-webkit-background-size", "background-size" ), |
| 5441 | ( "-webkit-box-shadow", "box-shadow" ), |
dbeam | 6936c67f | 2017-01-19 01:51:44 | [diff] [blame] | 5442 | ( "-webkit-user-select", "user-select" ), |
[email protected] | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 5443 | |
| 5444 | # Functions |
| 5445 | ( "-webkit-gradient", "gradient" ), |
| 5446 | ( "-webkit-repeating-gradient", "repeating-gradient" ), |
| 5447 | ( "-webkit-linear-gradient", "linear-gradient" ), |
| 5448 | ( "-webkit-repeating-linear-gradient", "repeating-linear-gradient" ), |
| 5449 | ( "-webkit-radial-gradient", "radial-gradient" ), |
| 5450 | ( "-webkit-repeating-radial-gradient", "repeating-radial-gradient" ), |
| 5451 | ] |
| 5452 | |
Wei-Yin Chen (陳威尹) | f799d44 | 2018-07-31 02:20:20 | [diff] [blame] | 5453 | |
Wei-Yin Chen (陳威尹) | dca729a | 2018-07-31 21:35:49 | [diff] [blame] | 5454 | # TODO: add unit tests |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5455 | def CheckNoDeprecatedCss(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5456 | """ Make sure that we don't use deprecated CSS |
| 5457 | properties, functions or values. Our external |
| 5458 | documentation and iOS CSS for dom distiller |
| 5459 | (reader mode) are ignored by the hooks as it |
| 5460 | needs to be consumed by WebKit. """ |
| 5461 | results = [] |
| 5462 | file_inclusion_pattern = [r".+\.css$"] |
| 5463 | files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 5464 | input_api.DEFAULT_FILES_TO_SKIP + |
dpapad | 7fcdfc4 | 2024-12-06 01:21:38 | [diff] [blame] | 5465 | (# Legacy CSS file using deprecated CSS. |
| 5466 | r"^chrome/browser/resources/chromeos/arc_support/cr_overlay.css$", |
| 5467 | r"^chrome/common/extensions/docs", r"^chrome/docs", |
Teresa Mao | 1d91088 | 2024-04-26 21:06:25 | [diff] [blame] | 5468 | r"^native_client_sdk", |
| 5469 | # The NTP team prefers reserving -webkit-line-clamp for |
| 5470 | # ellipsis effect which can only be used with -webkit-box. |
| 5471 | r"ui/webui/resources/cr_components/most_visited/.*\.css$")) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5472 | file_filter = lambda f: input_api.FilterSourceFile( |
| 5473 | f, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip) |
| 5474 | for fpath in input_api.AffectedFiles(file_filter=file_filter): |
| 5475 | for line_num, line in fpath.ChangedContents(): |
| 5476 | for (deprecated_value, value) in _DEPRECATED_CSS: |
| 5477 | if deprecated_value in line: |
| 5478 | results.append( |
| 5479 | output_api.PresubmitError( |
| 5480 | "%s:%d: Use of deprecated CSS %s, use %s instead" % |
| 5481 | (fpath.LocalPath(), line_num, deprecated_value, |
| 5482 | value))) |
| 5483 | return results |
[email protected] | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 5484 | |
mohan.reddy | f21db96 | 2014-10-16 12:26:47 | [diff] [blame] | 5485 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5486 | def CheckForRelativeIncludes(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5487 | bad_files = {} |
| 5488 | for f in input_api.AffectedFiles(include_deletes=False): |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 5489 | if (f.UnixLocalPath().startswith('third_party') |
| 5490 | and not f.LocalPath().startswith('third_party/blink')): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5491 | continue |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5492 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5493 | if not _IsCPlusPlusFile(input_api, f.LocalPath()): |
| 5494 | continue |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5495 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5496 | relative_includes = [ |
| 5497 | line for _, line in f.ChangedContents() |
| 5498 | if "#include" in line and "../" in line |
| 5499 | ] |
| 5500 | if not relative_includes: |
| 5501 | continue |
| 5502 | bad_files[f.LocalPath()] = relative_includes |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5503 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5504 | if not bad_files: |
| 5505 | return [] |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5506 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5507 | error_descriptions = [] |
| 5508 | for file_path, bad_lines in bad_files.items(): |
| 5509 | error_description = file_path |
| 5510 | for line in bad_lines: |
| 5511 | error_description += '\n ' + line |
| 5512 | error_descriptions.append(error_description) |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5513 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5514 | results = [] |
| 5515 | results.append( |
| 5516 | output_api.PresubmitError( |
| 5517 | 'You added one or more relative #include paths (including "../").\n' |
| 5518 | 'These shouldn\'t be used because they can be used to include headers\n' |
| 5519 | 'from code that\'s not correctly specified as a dependency in the\n' |
| 5520 | 'relevant BUILD.gn file(s).', error_descriptions)) |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5521 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5522 | return results |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5523 | |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5524 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5525 | def CheckForCcIncludes(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5526 | """Check that nobody tries to include a cc file. It's a relatively |
| 5527 | common error which results in duplicate symbols in object |
| 5528 | files. This may not always break the build until someone later gets |
| 5529 | very confusing linking errors.""" |
| 5530 | results = [] |
| 5531 | for f in input_api.AffectedFiles(include_deletes=False): |
| 5532 | # We let third_party code do whatever it wants |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 5533 | if (f.UnixLocalPath().startswith('third_party') |
| 5534 | and not f.LocalPath().startswith('third_party/blink')): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5535 | continue |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 5536 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5537 | if not _IsCPlusPlusFile(input_api, f.LocalPath()): |
| 5538 | continue |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 5539 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5540 | for _, line in f.ChangedContents(): |
| 5541 | if line.startswith('#include "'): |
| 5542 | included_file = line.split('"')[1] |
| 5543 | if _IsCPlusPlusFile(input_api, included_file): |
| 5544 | # The most common naming for external files with C++ code, |
| 5545 | # apart from standard headers, is to call them foo.inc, but |
| 5546 | # Chromium sometimes uses foo-inc.cc so allow that as well. |
| 5547 | if not included_file.endswith(('.h', '-inc.cc')): |
| 5548 | results.append( |
| 5549 | output_api.PresubmitError( |
| 5550 | 'Only header files or .inc files should be included in other\n' |
| 5551 | 'C++ files. Compiling the contents of a cc file more than once\n' |
| 5552 | 'will cause duplicate information in the build which may later\n' |
| 5553 | 'result in strange link_errors.\n' + |
| 5554 | f.LocalPath() + ':\n ' + line)) |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 5555 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5556 | return results |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 5557 | |
| 5558 | |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5559 | def _CheckWatchlistDefinitionsEntrySyntax(key, value, ast): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5560 | if not isinstance(key, ast.Str): |
| 5561 | return 'Key at line %d must be a string literal' % key.lineno |
| 5562 | if not isinstance(value, ast.Dict): |
| 5563 | return 'Value at line %d must be a dict' % value.lineno |
| 5564 | if len(value.keys) != 1: |
| 5565 | return 'Dict at line %d must have single entry' % value.lineno |
| 5566 | if not isinstance(value.keys[0], ast.Str) or value.keys[0].s != 'filepath': |
| 5567 | return ( |
| 5568 | 'Entry at line %d must have a string literal \'filepath\' as key' % |
| 5569 | value.lineno) |
| 5570 | return None |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5571 | |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5572 | |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 5573 | def _CheckWatchlistsEntrySyntax(key, value, ast, email_regex): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5574 | if not isinstance(key, ast.Str): |
| 5575 | return 'Key at line %d must be a string literal' % key.lineno |
| 5576 | if not isinstance(value, ast.List): |
| 5577 | return 'Value at line %d must be a list' % value.lineno |
| 5578 | for element in value.elts: |
| 5579 | if not isinstance(element, ast.Str): |
| 5580 | return 'Watchlist elements on line %d is not a string' % key.lineno |
| 5581 | if not email_regex.match(element.s): |
| 5582 | return ('Watchlist element on line %d doesn\'t look like a valid ' |
| 5583 | + 'email: %s') % (key.lineno, element.s) |
| 5584 | return None |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5585 | |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5586 | |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 5587 | def _CheckWATCHLISTSEntries(wd_dict, w_dict, input_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5588 | mismatch_template = ( |
| 5589 | 'Mismatch between WATCHLIST_DEFINITIONS entry (%s) and WATCHLISTS ' |
| 5590 | 'entry (%s)') |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5591 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5592 | email_regex = input_api.re.compile( |
| 5593 | r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+$") |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 5594 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5595 | ast = input_api.ast |
| 5596 | i = 0 |
| 5597 | last_key = '' |
| 5598 | while True: |
| 5599 | if i >= len(wd_dict.keys): |
| 5600 | if i >= len(w_dict.keys): |
| 5601 | return None |
| 5602 | return mismatch_template % ('missing', |
| 5603 | 'line %d' % w_dict.keys[i].lineno) |
| 5604 | elif i >= len(w_dict.keys): |
| 5605 | return (mismatch_template % |
| 5606 | ('line %d' % wd_dict.keys[i].lineno, 'missing')) |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5607 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5608 | wd_key = wd_dict.keys[i] |
| 5609 | w_key = w_dict.keys[i] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5610 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5611 | result = _CheckWatchlistDefinitionsEntrySyntax(wd_key, |
| 5612 | wd_dict.values[i], ast) |
| 5613 | if result is not None: |
| 5614 | return 'Bad entry in WATCHLIST_DEFINITIONS dict: %s' % result |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5615 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5616 | result = _CheckWatchlistsEntrySyntax(w_key, w_dict.values[i], ast, |
| 5617 | email_regex) |
| 5618 | if result is not None: |
| 5619 | return 'Bad entry in WATCHLISTS dict: %s' % result |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5620 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5621 | if wd_key.s != w_key.s: |
| 5622 | return mismatch_template % ('%s at line %d' % |
| 5623 | (wd_key.s, wd_key.lineno), |
| 5624 | '%s at line %d' % |
| 5625 | (w_key.s, w_key.lineno)) |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5626 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5627 | if wd_key.s < last_key: |
| 5628 | return ( |
| 5629 | 'WATCHLISTS dict is not sorted lexicographically at line %d and %d' |
| 5630 | % (wd_key.lineno, w_key.lineno)) |
| 5631 | last_key = wd_key.s |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5632 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5633 | i = i + 1 |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5634 | |
| 5635 | |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 5636 | def _CheckWATCHLISTSSyntax(expression, input_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5637 | ast = input_api.ast |
| 5638 | if not isinstance(expression, ast.Expression): |
| 5639 | return 'WATCHLISTS file must contain a valid expression' |
| 5640 | dictionary = expression.body |
| 5641 | if not isinstance(dictionary, ast.Dict) or len(dictionary.keys) != 2: |
| 5642 | return 'WATCHLISTS file must have single dict with exactly two entries' |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5643 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5644 | first_key = dictionary.keys[0] |
| 5645 | first_value = dictionary.values[0] |
| 5646 | second_key = dictionary.keys[1] |
| 5647 | second_value = dictionary.values[1] |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5648 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5649 | if (not isinstance(first_key, ast.Str) |
| 5650 | or first_key.s != 'WATCHLIST_DEFINITIONS' |
| 5651 | or not isinstance(first_value, ast.Dict)): |
| 5652 | return ('The first entry of the dict in WATCHLISTS file must be ' |
| 5653 | 'WATCHLIST_DEFINITIONS dict') |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5654 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5655 | if (not isinstance(second_key, ast.Str) or second_key.s != 'WATCHLISTS' |
| 5656 | or not isinstance(second_value, ast.Dict)): |
| 5657 | return ('The second entry of the dict in WATCHLISTS file must be ' |
| 5658 | 'WATCHLISTS dict') |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5659 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5660 | return _CheckWATCHLISTSEntries(first_value, second_value, input_api) |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5661 | |
| 5662 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5663 | def CheckWATCHLISTS(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5664 | for f in input_api.AffectedFiles(include_deletes=False): |
| 5665 | if f.LocalPath() == 'WATCHLISTS': |
| 5666 | contents = input_api.ReadFile(f, 'r') |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5667 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5668 | try: |
| 5669 | # First, make sure that it can be evaluated. |
| 5670 | input_api.ast.literal_eval(contents) |
| 5671 | # Get an AST tree for it and scan the tree for detailed style checking. |
| 5672 | expression = input_api.ast.parse(contents, |
| 5673 | filename='WATCHLISTS', |
| 5674 | mode='eval') |
| 5675 | except ValueError as e: |
| 5676 | return [ |
| 5677 | output_api.PresubmitError('Cannot parse WATCHLISTS file', |
| 5678 | long_text=repr(e)) |
| 5679 | ] |
| 5680 | except SyntaxError as e: |
| 5681 | return [ |
| 5682 | output_api.PresubmitError('Cannot parse WATCHLISTS file', |
| 5683 | long_text=repr(e)) |
| 5684 | ] |
| 5685 | except TypeError as e: |
| 5686 | return [ |
| 5687 | output_api.PresubmitError('Cannot parse WATCHLISTS file', |
| 5688 | long_text=repr(e)) |
| 5689 | ] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5690 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5691 | result = _CheckWATCHLISTSSyntax(expression, input_api) |
| 5692 | if result is not None: |
| 5693 | return [output_api.PresubmitError(result)] |
| 5694 | break |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5695 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5696 | return [] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5697 | |
Sean Kau | cb7c9b3 | 2022-10-25 21:25:52 | [diff] [blame] | 5698 | def CheckGnRebasePath(input_api, output_api): |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 5699 | """Checks that target_gen_dir is not used with "//" in rebase_path(). |
Sean Kau | cb7c9b3 | 2022-10-25 21:25:52 | [diff] [blame] | 5700 | |
| 5701 | Developers should use root_build_dir instead of "//" when using target_gen_dir because |
| 5702 | Chromium is sometimes built outside of the source tree. |
| 5703 | """ |
| 5704 | |
| 5705 | def gn_files(f): |
| 5706 | return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gn', )) |
| 5707 | |
| 5708 | rebase_path_regex = input_api.re.compile(r'rebase_path\(("\$target_gen_dir"|target_gen_dir), ("/"|"//")\)') |
| 5709 | problems = [] |
| 5710 | for f in input_api.AffectedSourceFiles(gn_files): |
| 5711 | for line_num, line in f.ChangedContents(): |
| 5712 | if rebase_path_regex.search(line): |
| 5713 | problems.append( |
| 5714 | 'Absolute path in rebase_path() in %s:%d' % |
| 5715 | (f.LocalPath(), line_num)) |
| 5716 | |
| 5717 | if problems: |
| 5718 | return [ |
| 5719 | output_api.PresubmitPromptWarning( |
| 5720 | 'Using an absolute path in rebase_path()', |
| 5721 | items=sorted(problems), |
| 5722 | long_text=( |
| 5723 | 'rebase_path() should use root_build_dir instead of "/" ', |
| 5724 | 'since builds can be initiated from outside of the source ', |
| 5725 | 'root.')) |
| 5726 | ] |
| 5727 | return [] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5728 | |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5729 | def CheckGnGlobForward(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5730 | """Checks that forward_variables_from(invoker, "*") follows best practices. |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5731 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5732 | As documented at //build/docs/writing_gn_templates.md |
| 5733 | """ |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5734 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5735 | def gn_files(f): |
| 5736 | return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gni', )) |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5737 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5738 | problems = [] |
| 5739 | for f in input_api.AffectedSourceFiles(gn_files): |
| 5740 | for line_num, line in f.ChangedContents(): |
| 5741 | if 'forward_variables_from(invoker, "*")' in line: |
| 5742 | problems.append( |
| 5743 | 'Bare forward_variables_from(invoker, "*") in %s:%d' % |
| 5744 | (f.LocalPath(), line_num)) |
| 5745 | |
| 5746 | if problems: |
| 5747 | return [ |
| 5748 | output_api.PresubmitPromptWarning( |
| 5749 | 'forward_variables_from("*") without exclusions', |
| 5750 | items=sorted(problems), |
| 5751 | long_text=( |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 5752 | 'The variables "visibility" and "test_only" should be ' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5753 | 'explicitly listed in forward_variables_from(). For more ' |
| 5754 | 'details, see:\n' |
| 5755 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/' |
| 5756 | 'build/docs/writing_gn_templates.md' |
| 5757 | '#Using-forward_variables_from')) |
| 5758 | ] |
| 5759 | return [] |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5760 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5761 | def CheckNewHeaderWithoutGnChangeOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5762 | """Checks that newly added header files have corresponding GN changes. |
| 5763 | Note that this is only a heuristic. To be precise, run script: |
| 5764 | build/check_gn_headers.py. |
| 5765 | """ |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5766 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5767 | def headers(f): |
| 5768 | return input_api.FilterSourceFile( |
| 5769 | f, files_to_check=(r'.+%s' % _HEADER_EXTENSIONS, )) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5770 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5771 | new_headers = [] |
| 5772 | for f in input_api.AffectedSourceFiles(headers): |
| 5773 | if f.Action() != 'A': |
| 5774 | continue |
| 5775 | new_headers.append(f.LocalPath()) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5776 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5777 | def gn_files(f): |
| 5778 | return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gn', )) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5779 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5780 | all_gn_changed_contents = '' |
| 5781 | for f in input_api.AffectedSourceFiles(gn_files): |
| 5782 | for _, line in f.ChangedContents(): |
| 5783 | all_gn_changed_contents += line |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5784 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5785 | problems = [] |
| 5786 | for header in new_headers: |
| 5787 | basename = input_api.os_path.basename(header) |
| 5788 | if basename not in all_gn_changed_contents: |
| 5789 | problems.append(header) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5790 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5791 | if problems: |
| 5792 | return [ |
| 5793 | output_api.PresubmitPromptWarning( |
| 5794 | 'Missing GN changes for new header files', |
| 5795 | items=sorted(problems), |
| 5796 | long_text= |
| 5797 | 'Please double check whether newly added header files need ' |
| 5798 | 'corresponding changes in gn or gni files.\nThis checking is only a ' |
| 5799 | 'heuristic. Run build/check_gn_headers.py to be precise.\n' |
| 5800 | 'Read https://crbug.com/661774 for more info.') |
| 5801 | ] |
| 5802 | return [] |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5803 | |
| 5804 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5805 | def CheckCorrectProductNameInMessages(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5806 | """Check that Chromium-branded strings don't include "Chrome" or vice versa. |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5807 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5808 | This assumes we won't intentionally reference one product from the other |
| 5809 | product. |
| 5810 | """ |
| 5811 | all_problems = [] |
| 5812 | test_cases = [{ |
| 5813 | "filename_postfix": "google_chrome_strings.grd", |
| 5814 | "correct_name": "Chrome", |
| 5815 | "incorrect_name": "Chromium", |
| 5816 | }, { |
Thiago Perrotta | 099034f | 2023-06-05 18:10:20 | [diff] [blame] | 5817 | "filename_postfix": "google_chrome_strings.grd", |
| 5818 | "correct_name": "Chrome", |
| 5819 | "incorrect_name": "Chrome for Testing", |
| 5820 | }, { |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5821 | "filename_postfix": "chromium_strings.grd", |
| 5822 | "correct_name": "Chromium", |
| 5823 | "incorrect_name": "Chrome", |
| 5824 | }] |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5825 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5826 | for test_case in test_cases: |
| 5827 | problems = [] |
| 5828 | filename_filter = lambda x: x.LocalPath().endswith(test_case[ |
| 5829 | "filename_postfix"]) |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5830 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5831 | # Check each new line. Can yield false positives in multiline comments, but |
| 5832 | # easier than trying to parse the XML because messages can have nested |
| 5833 | # children, and associating message elements with affected lines is hard. |
| 5834 | for f in input_api.AffectedSourceFiles(filename_filter): |
| 5835 | for line_num, line in f.ChangedContents(): |
| 5836 | if "<message" in line or "<!--" in line or "-->" in line: |
| 5837 | continue |
| 5838 | if test_case["incorrect_name"] in line: |
Thiago Perrotta | 099034f | 2023-06-05 18:10:20 | [diff] [blame] | 5839 | # Chrome for Testing is a special edge case: https://goo.gle/chrome-for-testing#bookmark=id.n1rat320av91 |
| 5840 | if (test_case["correct_name"] == "Chromium" and line.count("Chrome") == line.count("Chrome for Testing")): |
| 5841 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5842 | problems.append("Incorrect product name in %s:%d" % |
| 5843 | (f.LocalPath(), line_num)) |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5844 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5845 | if problems: |
| 5846 | message = ( |
| 5847 | "Strings in %s-branded string files should reference \"%s\", not \"%s\"" |
| 5848 | % (test_case["correct_name"], test_case["correct_name"], |
| 5849 | test_case["incorrect_name"])) |
| 5850 | all_problems.append( |
| 5851 | output_api.PresubmitPromptWarning(message, items=problems)) |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5852 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5853 | return all_problems |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5854 | |
| 5855 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5856 | def CheckForTooLargeFiles(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5857 | """Avoid large files, especially binary files, in the repository since |
| 5858 | git doesn't scale well for those. They will be in everyone's repo |
| 5859 | clones forever, forever making Chromium slower to clone and work |
| 5860 | with.""" |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5861 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5862 | # Uploading files to cloud storage is not trivial so we don't want |
| 5863 | # to set the limit too low, but the upper limit for "normal" large |
| 5864 | # files seems to be 1-2 MB, with a handful around 5-8 MB, so |
| 5865 | # anything over 20 MB is exceptional. |
Bruce Dawson | bb414db | 2022-12-27 20:21:25 | [diff] [blame] | 5866 | TOO_LARGE_FILE_SIZE_LIMIT = 20 * 1024 * 1024 |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5867 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5868 | too_large_files = [] |
| 5869 | for f in input_api.AffectedFiles(): |
| 5870 | # Check both added and modified files (but not deleted files). |
| 5871 | if f.Action() in ('A', 'M'): |
| 5872 | size = input_api.os_path.getsize(f.AbsoluteLocalPath()) |
Joe DeBlasio | 10a832f | 2023-04-21 20:20:18 | [diff] [blame] | 5873 | if size > TOO_LARGE_FILE_SIZE_LIMIT: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5874 | too_large_files.append("%s: %d bytes" % (f.LocalPath(), size)) |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5875 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5876 | if too_large_files: |
| 5877 | message = ( |
| 5878 | 'Do not commit large files to git since git scales badly for those.\n' |
| 5879 | + |
| 5880 | 'Instead put the large files in cloud storage and use DEPS to\n' + |
| 5881 | 'fetch them.\n' + '\n'.join(too_large_files)) |
| 5882 | return [ |
| 5883 | output_api.PresubmitError('Too large files found in commit', |
| 5884 | long_text=message + '\n') |
| 5885 | ] |
| 5886 | else: |
| 5887 | return [] |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5888 | |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5889 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5890 | def CheckFuzzTargetsOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5891 | """Checks specific for fuzz target sources.""" |
| 5892 | EXPORTED_SYMBOLS = [ |
| 5893 | 'LLVMFuzzerInitialize', |
| 5894 | 'LLVMFuzzerCustomMutator', |
| 5895 | 'LLVMFuzzerCustomCrossOver', |
| 5896 | 'LLVMFuzzerMutate', |
| 5897 | ] |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5898 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5899 | REQUIRED_HEADER = '#include "testing/libfuzzer/libfuzzer_exports.h"' |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5900 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5901 | def FilterFile(affected_file): |
| 5902 | """Ignore libFuzzer source code.""" |
| 5903 | files_to_check = r'.*fuzz.*\.(h|hpp|hcc|cc|cpp|cxx)$' |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5904 | files_to_skip = r"^third_party/libFuzzer" |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5905 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5906 | return input_api.FilterSourceFile(affected_file, |
| 5907 | files_to_check=[files_to_check], |
| 5908 | files_to_skip=[files_to_skip]) |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5909 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5910 | files_with_missing_header = [] |
| 5911 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 5912 | contents = input_api.ReadFile(f, 'r') |
| 5913 | if REQUIRED_HEADER in contents: |
| 5914 | continue |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5915 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5916 | if any(symbol in contents for symbol in EXPORTED_SYMBOLS): |
| 5917 | files_with_missing_header.append(f.LocalPath()) |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5918 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5919 | if not files_with_missing_header: |
| 5920 | return [] |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5921 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5922 | long_text = ( |
| 5923 | 'If you define any of the libFuzzer optional functions (%s), it is ' |
| 5924 | 'recommended to add \'%s\' directive. Otherwise, the fuzz target may ' |
| 5925 | 'work incorrectly on Mac (crbug.com/687076).\nNote that ' |
| 5926 | 'LLVMFuzzerInitialize should not be used, unless your fuzz target needs ' |
| 5927 | 'to access command line arguments passed to the fuzzer. Instead, prefer ' |
| 5928 | 'static initialization and shared resources as documented in ' |
| 5929 | 'https://chromium.googlesource.com/chromium/src/+/main/testing/' |
| 5930 | 'libfuzzer/efficient_fuzzing.md#simplifying-initialization_cleanup.\n' |
| 5931 | % (', '.join(EXPORTED_SYMBOLS), REQUIRED_HEADER)) |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5932 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5933 | return [ |
| 5934 | output_api.PresubmitPromptWarning(message="Missing '%s' in:" % |
| 5935 | REQUIRED_HEADER, |
| 5936 | items=files_with_missing_header, |
| 5937 | long_text=long_text) |
| 5938 | ] |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5939 | |
| 5940 | |
Mohamed Heikal | d048240a | 2019-11-12 16:57:37 | [diff] [blame] | 5941 | def _CheckNewImagesWarning(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5942 | """ |
| 5943 | Warns authors who add images into the repo to make sure their images are |
| 5944 | optimized before committing. |
| 5945 | """ |
| 5946 | images_added = False |
| 5947 | image_paths = [] |
| 5948 | errors = [] |
| 5949 | filter_lambda = lambda x: input_api.FilterSourceFile( |
| 5950 | x, |
| 5951 | files_to_skip=(('(?i).*test', r'.*\/junit\/') + input_api. |
| 5952 | DEFAULT_FILES_TO_SKIP), |
| 5953 | files_to_check=[r'.*\/(drawable|mipmap)']) |
| 5954 | for f in input_api.AffectedFiles(include_deletes=False, |
| 5955 | file_filter=filter_lambda): |
| 5956 | local_path = f.LocalPath().lower() |
| 5957 | if any( |
| 5958 | local_path.endswith(extension) |
| 5959 | for extension in _IMAGE_EXTENSIONS): |
| 5960 | images_added = True |
| 5961 | image_paths.append(f) |
| 5962 | if images_added: |
| 5963 | errors.append( |
| 5964 | output_api.PresubmitPromptWarning( |
| 5965 | 'It looks like you are trying to commit some images. If these are ' |
| 5966 | 'non-test-only images, please make sure to read and apply the tips in ' |
| 5967 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/docs/speed/' |
| 5968 | 'binary_size/optimization_advice.md#optimizing-images\nThis check is ' |
| 5969 | 'FYI only and will not block your CL on the CQ.', image_paths)) |
| 5970 | return errors |
Mohamed Heikal | d048240a | 2019-11-12 16:57:37 | [diff] [blame] | 5971 | |
| 5972 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5973 | def ChecksAndroidSpecificOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5974 | """Groups upload checks that target android code.""" |
| 5975 | results = [] |
| 5976 | results.extend(_CheckAndroidCrLogUsage(input_api, output_api)) |
| 5977 | results.extend(_CheckAndroidDebuggableBuild(input_api, output_api)) |
| 5978 | results.extend(_CheckAndroidNewMdpiAssetLocation(input_api, output_api)) |
| 5979 | results.extend(_CheckAndroidToastUsage(input_api, output_api)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5980 | results.extend(_CheckAndroidTestAnnotationUsage(input_api, output_api)) |
| 5981 | results.extend(_CheckAndroidWebkitImports(input_api, output_api)) |
| 5982 | results.extend(_CheckAndroidXmlStyle(input_api, output_api, True)) |
| 5983 | results.extend(_CheckNewImagesWarning(input_api, output_api)) |
| 5984 | results.extend(_CheckAndroidNoBannedImports(input_api, output_api)) |
| 5985 | results.extend(_CheckAndroidInfoBarDeprecation(input_api, output_api)) |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 5986 | results.extend(_CheckAndroidNullAwayAnnotatedClasses(input_api, output_api)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5987 | return results |
| 5988 | |
Becky Zhou | 7c69b5099 | 2018-12-10 19:37:57 | [diff] [blame] | 5989 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5990 | def ChecksAndroidSpecificOnCommit(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5991 | """Groups commit checks that target android code.""" |
| 5992 | results = [] |
| 5993 | results.extend(_CheckAndroidXmlStyle(input_api, output_api, False)) |
Henrique Nakashima | 0a2eb00 | 2025-04-25 16:29:37 | [diff] [blame] | 5994 | results.extend(_CheckAndroidNullAwayAnnotatedClasses(input_api, output_api)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5995 | return results |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 5996 | |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5997 | # TODO(chrishall): could we additionally match on any path owned by |
| 5998 | # ui/accessibility/OWNERS ? |
| 5999 | _ACCESSIBILITY_PATHS = ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 6000 | r"^chrome/browser.*/accessibility/", |
| 6001 | r"^chrome/browser/extensions/api/automation.*/", |
| 6002 | r"^chrome/renderer/extensions/accessibility_.*", |
| 6003 | r"^chrome/tests/data/accessibility/", |
| 6004 | r"^content/browser/accessibility/", |
| 6005 | r"^content/renderer/accessibility/", |
| 6006 | r"^content/tests/data/accessibility/", |
| 6007 | r"^extensions/renderer/api/automation/", |
Katie Dektar | 58ef07b | 2022-09-27 13:19:17 | [diff] [blame] | 6008 | r"^services/accessibility/", |
Abigail Klein | 7a63c57 | 2024-02-28 20:45:09 | [diff] [blame] | 6009 | r"^services/screen_ai/", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 6010 | r"^ui/accessibility/", |
| 6011 | r"^ui/views/accessibility/", |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 6012 | ) |
| 6013 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6014 | def CheckAccessibilityRelnotesField(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6015 | """Checks that commits to accessibility code contain an AX-Relnotes field in |
| 6016 | their commit message.""" |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 6017 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6018 | def FileFilter(affected_file): |
| 6019 | paths = _ACCESSIBILITY_PATHS |
| 6020 | return input_api.FilterSourceFile(affected_file, files_to_check=paths) |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 6021 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6022 | # Only consider changes affecting accessibility paths. |
| 6023 | if not any(input_api.AffectedFiles(file_filter=FileFilter)): |
| 6024 | return [] |
Akihiro Ota | 08108e54 | 2020-05-20 15:30:53 | [diff] [blame] | 6025 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6026 | # AX-Relnotes can appear in either the description or the footer. |
| 6027 | # When searching the description, require 'AX-Relnotes:' to appear at the |
| 6028 | # beginning of a line. |
| 6029 | ax_regex = input_api.re.compile('ax-relnotes[:=]') |
| 6030 | description_has_relnotes = any( |
| 6031 | ax_regex.match(line) |
| 6032 | for line in input_api.change.DescriptionText().lower().splitlines()) |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 6033 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6034 | footer_relnotes = input_api.change.GitFootersFromDescription().get( |
| 6035 | 'AX-Relnotes', []) |
| 6036 | if description_has_relnotes or footer_relnotes: |
| 6037 | return [] |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 6038 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6039 | # TODO(chrishall): link to Relnotes documentation in message. |
| 6040 | message = ( |
| 6041 | "Missing 'AX-Relnotes:' field required for accessibility changes" |
| 6042 | "\n please add 'AX-Relnotes: [release notes].' to describe any " |
| 6043 | "user-facing changes" |
| 6044 | "\n otherwise add 'AX-Relnotes: n/a.' if this change has no " |
| 6045 | "user-facing effects" |
| 6046 | "\n if this is confusing or annoying then please contact members " |
| 6047 | "of ui/accessibility/OWNERS.") |
| 6048 | |
| 6049 | return [output_api.PresubmitNotifyResult(message)] |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 6050 | |
Mark Schillaci | 44c90b4 | 2024-11-22 20:44:38 | [diff] [blame] | 6051 | |
| 6052 | _ACCESSIBILITY_ARIA_METHOD_CANDIDATES_PATTERNS = r'(\-\>|\.)(get|has|FastGet|FastHas)Attribute\(' |
| 6053 | |
| 6054 | _ACCESSIBILITY_ARIA_BAD_PARAMS_PATTERNS = ( |
| 6055 | r"\(html_names::kAria(.*)Attr\)", |
| 6056 | r"\(html_names::kRoleAttr\)" |
| 6057 | ) |
| 6058 | |
| 6059 | _ACCESSIBILITY_ARIA_FILE_CANDIDATES_PATTERNS = ( |
| 6060 | r".*/accessibility/.*.(cc|h)", |
| 6061 | r".*/ax_.*.(cc|h)" |
| 6062 | ) |
| 6063 | |
| 6064 | def CheckAccessibilityAriaElementAttributeGetters(input_api, output_api): |
| 6065 | """Checks that the blink accessibility code follows the defined patterns |
| 6066 | for checking aria attributes, so that ElementInternals is not bypassed.""" |
| 6067 | |
| 6068 | # Limit to accessibility-related files. |
| 6069 | def FileFilter(affected_file): |
| 6070 | paths = _ACCESSIBILITY_ARIA_FILE_CANDIDATES_PATTERNS |
| 6071 | return input_api.FilterSourceFile(affected_file, files_to_check=paths) |
| 6072 | |
| 6073 | aria_method_regex = input_api.re.compile(_ACCESSIBILITY_ARIA_METHOD_CANDIDATES_PATTERNS) |
| 6074 | aria_bad_params_regex = input_api.re.compile( |
| 6075 | "|".join(_ACCESSIBILITY_ARIA_BAD_PARAMS_PATTERNS) |
| 6076 | ) |
| 6077 | problems = [] |
| 6078 | |
| 6079 | for f in input_api.AffectedSourceFiles(FileFilter): |
| 6080 | for line_num, line in f.ChangedContents(): |
| 6081 | if aria_method_regex.search(line) and aria_bad_params_regex.search(line): |
| 6082 | problems.append(f"{f.LocalPath()}:{line_num}\n {line.strip()}") |
| 6083 | |
| 6084 | if problems: |
| 6085 | return [ |
| 6086 | output_api.PresubmitPromptWarning( |
| 6087 | "Accessibility code should not use element methods to get or check" |
| 6088 | "\nthe presence of aria attributes" |
| 6089 | "\nPlease use ARIA-specific attribute access, e.g. HasAriaAttribute()," |
| 6090 | "\nAriaTokenAttribute(), AriaBoolAttribute(), AriaBooleanAttribute()," |
| 6091 | "\nAriaFloatAttribute().", |
| 6092 | problems, |
| 6093 | ) |
| 6094 | ] |
| 6095 | return [] |
| 6096 | |
seanmccullough | 4a935625 | 2021-04-08 19:54:09 | [diff] [blame] | 6097 | # string pattern, sequence of strings to show when pattern matches, |
| 6098 | # error flag. True if match is a presubmit error, otherwise it's a warning. |
| 6099 | _NON_INCLUSIVE_TERMS = ( |
| 6100 | ( |
| 6101 | # Note that \b pattern in python re is pretty particular. In this |
| 6102 | # regexp, 'class WhiteList ...' will match, but 'class FooWhiteList |
| 6103 | # ...' will not. This may require some tweaking to catch these cases |
| 6104 | # without triggering a lot of false positives. Leaving it naive and |
| 6105 | # less matchy for now. |
Josip Sokcevic | 9d2806a0 | 2023-12-13 03:04:02 | [diff] [blame] | 6106 | r'/(?i)\b((black|white)list|master|slave)\b', # nocheck |
seanmccullough | 4a935625 | 2021-04-08 19:54:09 | [diff] [blame] | 6107 | ( |
| 6108 | 'Please don\'t use blacklist, whitelist, ' # nocheck |
| 6109 | 'or slave in your', # nocheck |
| 6110 | 'code and make every effort to use other terms. Using "// nocheck"', |
| 6111 | '"# nocheck" or "<!-- nocheck -->"', |
| 6112 | 'at the end of the offending line will bypass this PRESUBMIT error', |
| 6113 | 'but avoid using this whenever possible. Reach out to', |
| 6114 | '[email protected] if you have questions'), |
| 6115 | True),) |
| 6116 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6117 | def ChecksCommon(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6118 | """Checks common to both upload and commit.""" |
| 6119 | results = [] |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 6120 | results.extend( |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6121 | input_api.canned_checks.PanProjectChecks( |
| 6122 | input_api, output_api, excluded_paths=_EXCLUDED_PATHS)) |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 6123 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6124 | author = input_api.change.author_email |
| 6125 | if author and author not in _KNOWN_ROBOTS: |
| 6126 | results.extend( |
| 6127 | input_api.canned_checks.CheckAuthorizedAuthor( |
| 6128 | input_api, output_api)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 6129 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6130 | results.extend( |
| 6131 | input_api.canned_checks.CheckChangeHasNoTabs( |
| 6132 | input_api, |
| 6133 | output_api, |
| 6134 | source_file_filter=lambda x: x.LocalPath().endswith('.grd'))) |
| 6135 | results.extend( |
| 6136 | input_api.RunTests( |
| 6137 | input_api.canned_checks.CheckVPythonSpec(input_api, output_api))) |
Edward Lesmes | ce51df5 | 2020-08-04 22:10:17 | [diff] [blame] | 6138 | |
Bruce Dawson | c805448 | 2022-03-28 15:33:37 | [diff] [blame] | 6139 | dirmd = 'dirmd.bat' if input_api.is_windows else 'dirmd' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6140 | dirmd_bin = input_api.os_path.join(input_api.PresubmitLocalPath(), |
Bruce Dawson | c805448 | 2022-03-28 15:33:37 | [diff] [blame] | 6141 | 'third_party', 'depot_tools', dirmd) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6142 | results.extend( |
| 6143 | input_api.RunTests( |
| 6144 | input_api.canned_checks.CheckDirMetadataFormat( |
| 6145 | input_api, output_api, dirmd_bin))) |
| 6146 | results.extend( |
| 6147 | input_api.canned_checks.CheckOwnersDirMetadataExclusive( |
| 6148 | input_api, output_api)) |
| 6149 | results.extend( |
| 6150 | input_api.canned_checks.CheckNoNewMetadataInOwners( |
| 6151 | input_api, output_api)) |
| 6152 | results.extend( |
| 6153 | input_api.canned_checks.CheckInclusiveLanguage( |
| 6154 | input_api, |
| 6155 | output_api, |
| 6156 | excluded_directories_relative_path=[ |
| 6157 | 'infra', 'inclusive_language_presubmit_exempt_dirs.txt' |
| 6158 | ], |
| 6159 | non_inclusive_terms=_NON_INCLUSIVE_TERMS)) |
Yiwei Zhang | 5341bf0 | 2025-03-20 16:34:13 | [diff] [blame] | 6160 | results.extend( |
| 6161 | input_api.canned_checks.CheckNewDEPSHooksHasRequiredReviewers( |
| 6162 | input_api, output_api)) |
Dirk Pranke | e3c9c62d | 2021-05-18 18:35:59 | [diff] [blame] | 6163 | |
Aleksey Khoroshilov | 2978c94 | 2022-06-13 16:14:12 | [diff] [blame] | 6164 | presubmit_py_filter = lambda f: input_api.FilterSourceFile( |
Daniel Cheng | 6f3d1ae1 | 2025-04-07 17:11:27 | [diff] [blame] | 6165 | f, files_to_check=[r'.*PRESUBMIT(?:_test)?\.py$']) |
| 6166 | potential_paths = set( |
| 6167 | map( |
| 6168 | lambda f: input_api.os_path.dirname(f.AbsoluteLocalPath()), |
| 6169 | input_api.AffectedFiles(include_deletes=False, |
| 6170 | file_filter=presubmit_py_filter))) |
| 6171 | for full_path in potential_paths: |
Aleksey Khoroshilov | 2978c94 | 2022-06-13 16:14:12 | [diff] [blame] | 6172 | test_file = input_api.os_path.join(full_path, 'PRESUBMIT_test.py') |
| 6173 | # The PRESUBMIT.py file (and the directory containing it) might have |
| 6174 | # been affected by being moved or removed, so only try to run the tests |
| 6175 | # if they still exist. |
| 6176 | if not input_api.os_path.exists(test_file): |
| 6177 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6178 | |
Aleksey Khoroshilov | 2978c94 | 2022-06-13 16:14:12 | [diff] [blame] | 6179 | results.extend( |
| 6180 | input_api.canned_checks.RunUnitTestsInDirectory( |
| 6181 | input_api, |
| 6182 | output_api, |
| 6183 | full_path, |
Takuto Ikuta | 40def48 | 2023-06-02 02:23:49 | [diff] [blame] | 6184 | files_to_check=[r'^PRESUBMIT_test\.py$'])) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6185 | return results |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 6186 | |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 6187 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6188 | def CheckPatchFiles(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6189 | problems = [ |
| 6190 | f.LocalPath() for f in input_api.AffectedFiles() |
| 6191 | if f.LocalPath().endswith(('.orig', '.rej')) |
| 6192 | ] |
| 6193 | # Cargo.toml.orig files are part of third-party crates downloaded from |
| 6194 | # crates.io and should be included. |
| 6195 | problems = [f for f in problems if not f.endswith('Cargo.toml.orig')] |
| 6196 | if problems: |
| 6197 | return [ |
| 6198 | output_api.PresubmitError("Don't commit .rej and .orig files.", |
| 6199 | problems) |
| 6200 | ] |
| 6201 | else: |
| 6202 | return [] |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 6203 | |
| 6204 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6205 | def CheckBuildConfigMacrosWithoutInclude(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6206 | # Excludes OS_CHROMEOS, which is not defined in build_config.h. |
| 6207 | macro_re = input_api.re.compile( |
| 6208 | r'^\s*#(el)?if.*\bdefined\(((COMPILER_|ARCH_CPU_|WCHAR_T_IS_)[^)]*)') |
| 6209 | include_re = input_api.re.compile(r'^#include\s+"build/build_config.h"', |
| 6210 | input_api.re.MULTILINE) |
| 6211 | extension_re = input_api.re.compile(r'\.[a-z]+$') |
| 6212 | errors = [] |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 6213 | config_h_file = input_api.os_path.join('build', 'build_config.h') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6214 | for f in input_api.AffectedFiles(include_deletes=False): |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 6215 | # The build-config macros are allowed to be used in build_config.h |
| 6216 | # without including itself. |
| 6217 | if f.LocalPath() == config_h_file: |
| 6218 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6219 | if not f.LocalPath().endswith( |
| 6220 | ('.h', '.c', '.cc', '.cpp', '.m', '.mm')): |
| 6221 | continue |
Arthur Sonzogni | a3dec41 | 2024-04-29 12:05:37 | [diff] [blame] | 6222 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6223 | found_line_number = None |
| 6224 | found_macro = None |
| 6225 | all_lines = input_api.ReadFile(f, 'r').splitlines() |
| 6226 | for line_num, line in enumerate(all_lines): |
| 6227 | match = macro_re.search(line) |
| 6228 | if match: |
| 6229 | found_line_number = line_num |
| 6230 | found_macro = match.group(2) |
| 6231 | break |
| 6232 | if not found_line_number: |
| 6233 | continue |
Kent Tamura | 5a8755d | 2017-06-29 23:37:07 | [diff] [blame] | 6234 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6235 | found_include_line = -1 |
| 6236 | for line_num, line in enumerate(all_lines): |
| 6237 | if include_re.search(line): |
| 6238 | found_include_line = line_num |
| 6239 | break |
| 6240 | if found_include_line >= 0 and found_include_line < found_line_number: |
| 6241 | continue |
Kent Tamura | 5a8755d | 2017-06-29 23:37:07 | [diff] [blame] | 6242 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6243 | if not f.LocalPath().endswith('.h'): |
| 6244 | primary_header_path = extension_re.sub('.h', f.AbsoluteLocalPath()) |
| 6245 | try: |
| 6246 | content = input_api.ReadFile(primary_header_path, 'r') |
| 6247 | if include_re.search(content): |
| 6248 | continue |
| 6249 | except IOError: |
| 6250 | pass |
| 6251 | errors.append('%s:%d %s macro is used without first including build/' |
| 6252 | 'build_config.h.' % |
| 6253 | (f.LocalPath(), found_line_number, found_macro)) |
| 6254 | if errors: |
| 6255 | return [output_api.PresubmitPromptWarning('\n'.join(errors))] |
| 6256 | return [] |
Kent Tamura | 5a8755d | 2017-06-29 23:37:07 | [diff] [blame] | 6257 | |
| 6258 | |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6259 | def CheckForSuperfluousStlIncludesInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6260 | stl_include_re = input_api.re.compile(r'^#include\s+<(' |
| 6261 | r'algorithm|' |
| 6262 | r'array|' |
| 6263 | r'limits|' |
| 6264 | r'list|' |
| 6265 | r'map|' |
| 6266 | r'memory|' |
| 6267 | r'queue|' |
| 6268 | r'set|' |
| 6269 | r'string|' |
| 6270 | r'unordered_map|' |
| 6271 | r'unordered_set|' |
| 6272 | r'utility|' |
| 6273 | r'vector)>') |
| 6274 | std_namespace_re = input_api.re.compile(r'std::') |
| 6275 | errors = [] |
| 6276 | for f in input_api.AffectedFiles(): |
| 6277 | if not _IsCPlusPlusHeaderFile(input_api, f.LocalPath()): |
| 6278 | continue |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6279 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6280 | uses_std_namespace = False |
| 6281 | has_stl_include = False |
| 6282 | for line in f.NewContents(): |
| 6283 | if has_stl_include and uses_std_namespace: |
| 6284 | break |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6285 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6286 | if not has_stl_include and stl_include_re.search(line): |
| 6287 | has_stl_include = True |
| 6288 | continue |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6289 | |
Bruce Dawson | 4a5579a | 2022-04-08 17:11:36 | [diff] [blame] | 6290 | if not uses_std_namespace and (std_namespace_re.search(line) |
| 6291 | or 'no-std-usage-because-pch-file' in line): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6292 | uses_std_namespace = True |
| 6293 | continue |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6294 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6295 | if has_stl_include and not uses_std_namespace: |
| 6296 | errors.append( |
| 6297 | '%s: Includes STL header(s) but does not reference std::' % |
| 6298 | f.LocalPath()) |
| 6299 | if errors: |
| 6300 | return [output_api.PresubmitPromptWarning('\n'.join(errors))] |
| 6301 | return [] |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6302 | |
| 6303 | |
Xiaohan Wang | 42d96c2 | 2022-01-20 17:23:11 | [diff] [blame] | 6304 | def _CheckForDeprecatedOSMacrosInFile(input_api, f): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6305 | """Check for sensible looking, totally invalid OS macros.""" |
| 6306 | preprocessor_statement = input_api.re.compile(r'^\s*#') |
| 6307 | os_macro = input_api.re.compile(r'defined\(OS_([^)]+)\)') |
| 6308 | results = [] |
| 6309 | for lnum, line in f.ChangedContents(): |
| 6310 | if preprocessor_statement.search(line): |
| 6311 | for match in os_macro.finditer(line): |
| 6312 | results.append( |
| 6313 | ' %s:%d: %s' % |
| 6314 | (f.LocalPath(), lnum, 'defined(OS_' + match.group(1) + |
| 6315 | ') -> BUILDFLAG(IS_' + match.group(1) + ')')) |
| 6316 | return results |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6317 | |
| 6318 | |
Xiaohan Wang | 42d96c2 | 2022-01-20 17:23:11 | [diff] [blame] | 6319 | def CheckForDeprecatedOSMacros(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6320 | """Check all affected files for invalid OS macros.""" |
| 6321 | bad_macros = [] |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 6322 | # The OS_ macros are allowed to be used in build/build_config.h. |
| 6323 | config_h_file = input_api.os_path.join('build', 'build_config.h') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6324 | for f in input_api.AffectedSourceFiles(None): |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 6325 | if not f.LocalPath().endswith(('.py', '.js', '.html', '.css', '.md')) \ |
| 6326 | and f.LocalPath() != config_h_file: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6327 | bad_macros.extend(_CheckForDeprecatedOSMacrosInFile(input_api, f)) |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6328 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6329 | if not bad_macros: |
| 6330 | return [] |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6331 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6332 | return [ |
| 6333 | output_api.PresubmitError( |
| 6334 | 'OS macros have been deprecated. Please use BUILDFLAGs instead (still ' |
| 6335 | 'defined in build_config.h):', bad_macros) |
| 6336 | ] |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6337 | |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6338 | |
| 6339 | def _CheckForInvalidIfDefinedMacrosInFile(input_api, f): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6340 | """Check all affected files for invalid "if defined" macros.""" |
| 6341 | ALWAYS_DEFINED_MACROS = ( |
| 6342 | "TARGET_CPU_PPC", |
| 6343 | "TARGET_CPU_PPC64", |
| 6344 | "TARGET_CPU_68K", |
| 6345 | "TARGET_CPU_X86", |
| 6346 | "TARGET_CPU_ARM", |
| 6347 | "TARGET_CPU_MIPS", |
| 6348 | "TARGET_CPU_SPARC", |
| 6349 | "TARGET_CPU_ALPHA", |
| 6350 | "TARGET_IPHONE_SIMULATOR", |
| 6351 | "TARGET_OS_EMBEDDED", |
| 6352 | "TARGET_OS_IPHONE", |
| 6353 | "TARGET_OS_MAC", |
| 6354 | "TARGET_OS_UNIX", |
| 6355 | "TARGET_OS_WIN32", |
| 6356 | ) |
| 6357 | ifdef_macro = input_api.re.compile( |
| 6358 | r'^\s*#.*(?:ifdef\s|defined\()([^\s\)]+)') |
| 6359 | results = [] |
| 6360 | for lnum, line in f.ChangedContents(): |
| 6361 | for match in ifdef_macro.finditer(line): |
| 6362 | if match.group(1) in ALWAYS_DEFINED_MACROS: |
| 6363 | always_defined = ' %s is always defined. ' % match.group(1) |
| 6364 | did_you_mean = 'Did you mean \'#if %s\'?' % match.group(1) |
| 6365 | results.append( |
| 6366 | ' %s:%d %s\n\t%s' % |
| 6367 | (f.LocalPath(), lnum, always_defined, did_you_mean)) |
| 6368 | return results |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6369 | |
| 6370 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6371 | def CheckForInvalidIfDefinedMacros(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6372 | """Check all affected files for invalid "if defined" macros.""" |
Arthur Sonzogni | 4fd14fd | 2024-06-02 18:42:52 | [diff] [blame] | 6373 | SKIPPED_PATHS = [ |
| 6374 | 'base/allocator/partition_allocator/src/partition_alloc/build_config.h', |
| 6375 | 'build/build_config.h', |
| 6376 | 'third_party/abseil-cpp/', |
| 6377 | 'third_party/sqlite/', |
| 6378 | ] |
| 6379 | def affected_files_filter(f): |
| 6380 | # Normalize the local path to Linux-style path separators so that the |
| 6381 | # path comparisons work on Windows as well. |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 6382 | path = f.UnixLocalPath() |
Arthur Sonzogni | 4fd14fd | 2024-06-02 18:42:52 | [diff] [blame] | 6383 | |
| 6384 | for skipped_path in SKIPPED_PATHS: |
| 6385 | if path.startswith(skipped_path): |
| 6386 | return False |
| 6387 | |
| 6388 | return path.endswith(('.h', '.c', '.cc', '.m', '.mm')) |
| 6389 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6390 | bad_macros = [] |
Arthur Sonzogni | 4fd14fd | 2024-06-02 18:42:52 | [diff] [blame] | 6391 | for f in input_api.AffectedSourceFiles(affected_files_filter): |
| 6392 | bad_macros.extend(_CheckForInvalidIfDefinedMacrosInFile(input_api, f)) |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6393 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6394 | if not bad_macros: |
| 6395 | return [] |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6396 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6397 | return [ |
| 6398 | output_api.PresubmitError( |
| 6399 | 'Found ifdef check on always-defined macro[s]. Please fix your code\n' |
| 6400 | 'or check the list of ALWAYS_DEFINED_MACROS in src/PRESUBMIT.py.', |
| 6401 | bad_macros) |
| 6402 | ] |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6403 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6404 | def CheckForIPCRules(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6405 | """Check for same IPC rules described in |
| 6406 | http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc |
| 6407 | """ |
| 6408 | base_pattern = r'IPC_ENUM_TRAITS\(' |
| 6409 | inclusion_pattern = input_api.re.compile(r'(%s)' % base_pattern) |
| 6410 | comment_pattern = input_api.re.compile(r'//.*(%s)' % base_pattern) |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 6411 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6412 | problems = [] |
| 6413 | for f in input_api.AffectedSourceFiles(None): |
| 6414 | local_path = f.LocalPath() |
| 6415 | if not local_path.endswith('.h'): |
| 6416 | continue |
| 6417 | for line_number, line in f.ChangedContents(): |
| 6418 | if inclusion_pattern.search( |
| 6419 | line) and not comment_pattern.search(line): |
| 6420 | problems.append('%s:%d\n %s' % |
| 6421 | (local_path, line_number, line.strip())) |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 6422 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6423 | if problems: |
| 6424 | return [ |
| 6425 | output_api.PresubmitPromptWarning(_IPC_ENUM_TRAITS_DEPRECATED, |
| 6426 | problems) |
| 6427 | ] |
| 6428 | else: |
| 6429 | return [] |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 6430 | |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6431 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6432 | def CheckForLongPathnames(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6433 | """Check to make sure no files being submitted have long paths. |
| 6434 | This causes issues on Windows. |
| 6435 | """ |
| 6436 | problems = [] |
| 6437 | for f in input_api.AffectedTestableFiles(): |
| 6438 | local_path = f.LocalPath() |
| 6439 | # Windows has a path limit of 260 characters. Limit path length to 200 so |
| 6440 | # that we have some extra for the prefix on dev machines and the bots. |
Weizhong Xia | 8b461f1 | 2024-06-21 21:46:33 | [diff] [blame] | 6441 | if (local_path.startswith('third_party/blink/web_tests/platform/') and |
| 6442 | not local_path.startswith('third_party/blink/web_tests/platform/win')): |
| 6443 | # Do not check length of the path for files not used by Windows |
| 6444 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6445 | if len(local_path) > 200: |
| 6446 | problems.append(local_path) |
Stephen Martinis | 97a39414 | 2018-06-07 23:06:05 | [diff] [blame] | 6447 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6448 | if problems: |
| 6449 | return [output_api.PresubmitError(_LONG_PATH_ERROR, problems)] |
| 6450 | else: |
| 6451 | return [] |
Stephen Martinis | 97a39414 | 2018-06-07 23:06:05 | [diff] [blame] | 6452 | |
| 6453 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6454 | def CheckForIncludeGuards(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6455 | """Check that header files have proper guards against multiple inclusion. |
| 6456 | 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] | 6457 | should include the string "no-include-guard-because-multiply-included" or |
| 6458 | "no-include-guard-because-pch-file". |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6459 | """ |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6460 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6461 | def is_chromium_header_file(f): |
| 6462 | # We only check header files under the control of the Chromium |
mikt | 84d6c71 | 2024-03-27 13:29:03 | [diff] [blame] | 6463 | # project. This excludes: |
| 6464 | # - third_party/*, except blink. |
| 6465 | # - base/allocator/partition_allocator/: PartitionAlloc is a standalone |
| 6466 | # library used outside of Chrome. Includes are referenced from its |
| 6467 | # own base directory. It has its own `CheckForIncludeGuards` |
| 6468 | # PRESUBMIT.py check. |
| 6469 | # - *_message_generator.h: They use include guards in a special, |
| 6470 | # non-typical way. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6471 | file_with_path = input_api.os_path.normpath(f.LocalPath()) |
| 6472 | return (file_with_path.endswith('.h') |
| 6473 | and not file_with_path.endswith('_message_generator.h') |
Bruce Dawson | 4c4c292 | 2022-05-02 18:07:33 | [diff] [blame] | 6474 | and not file_with_path.endswith('com_imported_mstscax.h') |
Peter Kasting | 66c1f75 | 2024-12-02 15:28:37 | [diff] [blame] | 6475 | and not file_with_path.startswith( |
| 6476 | input_api.os_path.join('base', 'allocator', |
| 6477 | 'partition_allocator')) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6478 | and (not file_with_path.startswith('third_party') |
| 6479 | or file_with_path.startswith( |
| 6480 | input_api.os_path.join('third_party', 'blink')))) |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6481 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6482 | def replace_special_with_underscore(string): |
| 6483 | return input_api.re.sub(r'[+\\/.-]', '_', string) |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6484 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6485 | errors = [] |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6486 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6487 | for f in input_api.AffectedSourceFiles(is_chromium_header_file): |
| 6488 | guard_name = None |
| 6489 | guard_line_number = None |
| 6490 | seen_guard_end = False |
Lei Zhang | d84f951 | 2024-05-28 19:43:30 | [diff] [blame] | 6491 | bypass_checks_at_end_of_file = False |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6492 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6493 | file_with_path = input_api.os_path.normpath(f.LocalPath()) |
| 6494 | base_file_name = input_api.os_path.splitext( |
| 6495 | input_api.os_path.basename(file_with_path))[0] |
| 6496 | upper_base_file_name = base_file_name.upper() |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6497 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6498 | expected_guard = replace_special_with_underscore( |
| 6499 | file_with_path.upper() + '_') |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6500 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6501 | # For "path/elem/file_name.h" we should really only accept |
| 6502 | # PATH_ELEM_FILE_NAME_H_ per coding style. Unfortunately there |
| 6503 | # are too many (1000+) files with slight deviations from the |
| 6504 | # coding style. The most important part is that the include guard |
| 6505 | # is there, and that it's unique, not the name so this check is |
| 6506 | # forgiving for existing files. |
| 6507 | # |
| 6508 | # As code becomes more uniform, this could be made stricter. |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6509 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6510 | guard_name_pattern_list = [ |
| 6511 | # Anything with the right suffix (maybe with an extra _). |
| 6512 | r'\w+_H__?', |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6513 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6514 | # To cover include guards with old Blink style. |
| 6515 | r'\w+_h', |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6516 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6517 | # Anything including the uppercase name of the file. |
| 6518 | r'\w*' + input_api.re.escape( |
| 6519 | replace_special_with_underscore(upper_base_file_name)) + |
| 6520 | r'\w*', |
| 6521 | ] |
| 6522 | guard_name_pattern = '|'.join(guard_name_pattern_list) |
| 6523 | guard_pattern = input_api.re.compile(r'#ifndef\s+(' + |
| 6524 | guard_name_pattern + ')') |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6525 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6526 | for line_number, line in enumerate(f.NewContents()): |
Bruce Dawson | 4a5579a | 2022-04-08 17:11:36 | [diff] [blame] | 6527 | if ('no-include-guard-because-multiply-included' in line |
| 6528 | or 'no-include-guard-because-pch-file' in line): |
Lei Zhang | d84f951 | 2024-05-28 19:43:30 | [diff] [blame] | 6529 | bypass_checks_at_end_of_file = True |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6530 | break |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6531 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6532 | if guard_name is None: |
| 6533 | match = guard_pattern.match(line) |
| 6534 | if match: |
| 6535 | guard_name = match.group(1) |
| 6536 | guard_line_number = line_number |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6537 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6538 | # We allow existing files to use include guards whose names |
| 6539 | # don't match the chromium style guide, but new files should |
| 6540 | # get it right. |
Bruce Dawson | 6cc154e | 2022-04-12 20:39:49 | [diff] [blame] | 6541 | if guard_name != expected_guard: |
Bruce Dawson | 95eb756 | 2022-09-14 15:27:16 | [diff] [blame] | 6542 | if f.Action() == 'A': # If file was just 'A'dded |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6543 | errors.append( |
| 6544 | output_api.PresubmitPromptWarning( |
| 6545 | 'Header using the wrong include guard name %s' |
| 6546 | % guard_name, [ |
| 6547 | '%s:%d' % |
| 6548 | (f.LocalPath(), line_number + 1) |
| 6549 | ], 'Expected: %r\nFound: %r' % |
| 6550 | (expected_guard, guard_name))) |
| 6551 | else: |
| 6552 | # The line after #ifndef should have a #define of the same name. |
| 6553 | if line_number == guard_line_number + 1: |
| 6554 | expected_line = '#define %s' % guard_name |
| 6555 | if line != expected_line: |
| 6556 | errors.append( |
| 6557 | output_api.PresubmitPromptWarning( |
| 6558 | 'Missing "%s" for include guard' % |
| 6559 | expected_line, |
| 6560 | ['%s:%d' % (f.LocalPath(), line_number + 1)], |
| 6561 | 'Expected: %r\nGot: %r' % |
| 6562 | (expected_line, line))) |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6563 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6564 | if not seen_guard_end and line == '#endif // %s' % guard_name: |
| 6565 | seen_guard_end = True |
| 6566 | elif seen_guard_end: |
| 6567 | if line.strip() != '': |
| 6568 | errors.append( |
| 6569 | output_api.PresubmitPromptWarning( |
| 6570 | 'Include guard %s not covering the whole file' |
| 6571 | % (guard_name), [f.LocalPath()])) |
| 6572 | break # Nothing else to check and enough to warn once. |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6573 | |
Lei Zhang | d84f951 | 2024-05-28 19:43:30 | [diff] [blame] | 6574 | if bypass_checks_at_end_of_file: |
| 6575 | continue |
| 6576 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6577 | if guard_name is None: |
| 6578 | errors.append( |
| 6579 | output_api.PresubmitPromptWarning( |
Bruce Dawson | 32114b6 | 2022-04-11 16:45:49 | [diff] [blame] | 6580 | 'Missing include guard in %s\n' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6581 | 'Recommended name: %s\n' |
| 6582 | 'This check can be disabled by having the string\n' |
Bruce Dawson | 4a5579a | 2022-04-08 17:11:36 | [diff] [blame] | 6583 | '"no-include-guard-because-multiply-included" or\n' |
| 6584 | '"no-include-guard-because-pch-file" in the header.' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6585 | % (f.LocalPath(), expected_guard))) |
Lei Zhang | d84f951 | 2024-05-28 19:43:30 | [diff] [blame] | 6586 | elif not seen_guard_end: |
| 6587 | errors.append( |
| 6588 | output_api.PresubmitPromptWarning( |
| 6589 | 'Incorrect or missing include guard #endif in %s\n' |
| 6590 | 'Recommended #endif comment: // %s' |
| 6591 | % (f.LocalPath(), expected_guard))) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6592 | |
| 6593 | return errors |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6594 | |
| 6595 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6596 | def CheckForWindowsLineEndings(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6597 | """Check source code and known ascii text files for Windows style line |
| 6598 | endings. |
| 6599 | """ |
Bruce Dawson | 5efbdc65 | 2022-04-11 19:29:51 | [diff] [blame] | 6600 | known_text_files = r'.*\.(txt|html|htm|py|gyp|gypi|gn|isolate|icon)$' |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6601 | |
dpapad | fd421fb | 2025-02-13 00:47:32 | [diff] [blame] | 6602 | _WEBUI_FILES_EXTENSIONS = r'\.(css|html|js|ts|svg)$' |
| 6603 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6604 | file_inclusion_pattern = (known_text_files, |
| 6605 | r'.+%s' % _IMPLEMENTATION_EXTENSIONS, |
dpapad | fd421fb | 2025-02-13 00:47:32 | [diff] [blame] | 6606 | r'.+%s' % _HEADER_EXTENSIONS, |
| 6607 | r'.+%s' % _WEBUI_FILES_EXTENSIONS) |
| 6608 | |
| 6609 | # Exclude folder that contains .ts files that are actually binary video |
| 6610 | # format and not TypeScript. |
| 6611 | file_exclusion_pattern = (r'media/test/data/') |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6612 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6613 | problems = [] |
| 6614 | source_file_filter = lambda f: input_api.FilterSourceFile( |
dpapad | fd421fb | 2025-02-13 00:47:32 | [diff] [blame] | 6615 | f, files_to_check=file_inclusion_pattern, |
| 6616 | files_to_skip=file_exclusion_pattern) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6617 | for f in input_api.AffectedSourceFiles(source_file_filter): |
Bruce Dawson | 5efbdc65 | 2022-04-11 19:29:51 | [diff] [blame] | 6618 | # Ignore test files that contain crlf intentionally. |
| 6619 | if f.LocalPath().endswith('crlf.txt'): |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 6620 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6621 | include_file = False |
| 6622 | for line in input_api.ReadFile(f, 'r').splitlines(True): |
| 6623 | if line.endswith('\r\n'): |
| 6624 | include_file = True |
| 6625 | if include_file: |
| 6626 | problems.append(f.LocalPath()) |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6627 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6628 | if problems: |
| 6629 | return [ |
| 6630 | output_api.PresubmitPromptWarning( |
| 6631 | 'Are you sure that you want ' |
| 6632 | 'these files to contain Windows style line endings?\n' + |
| 6633 | '\n'.join(problems)) |
| 6634 | ] |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6635 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6636 | return [] |
| 6637 | |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6638 | |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 6639 | def CheckIconFilesForLicenseHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6640 | """Check that .icon files (which are fragments of C++) have license headers. |
| 6641 | """ |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 6642 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6643 | icon_files = (r'.*\.icon$', ) |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 6644 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6645 | icons = lambda x: input_api.FilterSourceFile(x, files_to_check=icon_files) |
| 6646 | return input_api.canned_checks.CheckLicense(input_api, |
| 6647 | output_api, |
| 6648 | source_file_filter=icons) |
| 6649 | |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 6650 | |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6651 | def CheckForUseOfChromeAppsDeprecations(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6652 | """Check source code for use of Chrome App technologies being |
| 6653 | deprecated. |
| 6654 | """ |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6655 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6656 | def _CheckForDeprecatedTech(input_api, |
| 6657 | output_api, |
| 6658 | detection_list, |
| 6659 | files_to_check=None, |
| 6660 | files_to_skip=None): |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6661 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6662 | if (files_to_check or files_to_skip): |
| 6663 | source_file_filter = lambda f: input_api.FilterSourceFile( |
| 6664 | f, files_to_check=files_to_check, files_to_skip=files_to_skip) |
| 6665 | else: |
| 6666 | source_file_filter = None |
| 6667 | |
| 6668 | problems = [] |
| 6669 | |
| 6670 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 6671 | if f.Action() == 'D': |
| 6672 | continue |
| 6673 | for _, line in f.ChangedContents(): |
| 6674 | if any(detect in line for detect in detection_list): |
| 6675 | problems.append(f.LocalPath()) |
| 6676 | |
| 6677 | return problems |
| 6678 | |
| 6679 | # to avoid this presubmit script triggering warnings |
| 6680 | files_to_skip = ['PRESUBMIT.py', 'PRESUBMIT_test.py'] |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6681 | |
| 6682 | problems = [] |
| 6683 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6684 | # NMF: any files with extensions .nmf or NMF |
| 6685 | _NMF_FILES = r'\.(nmf|NMF)$' |
| 6686 | problems += _CheckForDeprecatedTech( |
| 6687 | input_api, |
| 6688 | output_api, |
| 6689 | detection_list=[''], # any change to the file will trigger warning |
| 6690 | files_to_check=[r'.+%s' % _NMF_FILES]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6691 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6692 | # MANIFEST: any manifest.json that in its diff includes "app": |
| 6693 | _MANIFEST_FILES = r'(manifest\.json)$' |
| 6694 | problems += _CheckForDeprecatedTech( |
| 6695 | input_api, |
| 6696 | output_api, |
| 6697 | detection_list=['"app":'], |
| 6698 | files_to_check=[r'.*%s' % _MANIFEST_FILES]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6699 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6700 | # NaCl / PNaCl: any file that in its diff contains the strings in the list |
| 6701 | problems += _CheckForDeprecatedTech( |
| 6702 | input_api, |
| 6703 | output_api, |
| 6704 | detection_list=['config=nacl', 'enable-nacl', 'cpu=pnacl', 'nacl_io'], |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 6705 | files_to_skip=files_to_skip + [r"^native_client_sdk/"]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6706 | |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 6707 | # PPAPI: any C/C++ file that in its diff includes a ppapi library |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6708 | problems += _CheckForDeprecatedTech( |
| 6709 | input_api, |
| 6710 | output_api, |
| 6711 | detection_list=['#include "ppapi', '#include <ppapi'], |
| 6712 | files_to_check=(r'.+%s' % _HEADER_EXTENSIONS, |
| 6713 | r'.+%s' % _IMPLEMENTATION_EXTENSIONS), |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 6714 | files_to_skip=[r"^ppapi/"]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6715 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6716 | if problems: |
| 6717 | return [ |
| 6718 | output_api.PresubmitPromptWarning( |
| 6719 | 'You are adding/modifying code' |
| 6720 | 'related to technologies which will soon be deprecated (Chrome Apps, NaCl,' |
| 6721 | ' PNaCl, PPAPI). See this blog post for more details:\n' |
| 6722 | 'https://blog.chromium.org/2020/08/changes-to-chrome-app-support-timeline.html\n' |
| 6723 | 'and this documentation for options to replace these technologies:\n' |
| 6724 | 'https://developer.chrome.com/docs/apps/migration/\n' + |
| 6725 | '\n'.join(problems)) |
| 6726 | ] |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6727 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6728 | return [] |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6729 | |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6730 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6731 | def CheckSyslogUseWarningOnUpload(input_api, output_api, src_file_filter=None): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6732 | """Checks that all source files use SYSLOG properly.""" |
| 6733 | syslog_files = [] |
| 6734 | for f in input_api.AffectedSourceFiles(src_file_filter): |
| 6735 | for line_number, line in f.ChangedContents(): |
| 6736 | if 'SYSLOG' in line: |
| 6737 | syslog_files.append(f.LocalPath() + ':' + str(line_number)) |
pastarmovj | 032ba5bc | 2017-01-12 10:41:56 | [diff] [blame] | 6738 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6739 | if syslog_files: |
| 6740 | return [ |
| 6741 | output_api.PresubmitPromptWarning( |
| 6742 | 'Please make sure there are no privacy sensitive bits of data in SYSLOG' |
| 6743 | ' calls.\nFiles to check:\n', |
| 6744 | items=syslog_files) |
| 6745 | ] |
| 6746 | return [] |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 6747 | |
| 6748 | |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 6749 | def CheckChangeOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6750 | if input_api.version < [2, 0, 0]: |
| 6751 | return [ |
| 6752 | output_api.PresubmitError( |
| 6753 | "Your depot_tools is out of date. " |
| 6754 | "This PRESUBMIT.py requires at least presubmit_support version 2.0.0, " |
| 6755 | "but your version is %d.%d.%d" % tuple(input_api.version)) |
| 6756 | ] |
| 6757 | results = [] |
| 6758 | results.extend( |
| 6759 | input_api.canned_checks.CheckPatchFormatted(input_api, output_api)) |
| 6760 | return results |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 6761 | |
| 6762 | |
| 6763 | def CheckChangeOnCommit(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6764 | if input_api.version < [2, 0, 0]: |
| 6765 | return [ |
| 6766 | output_api.PresubmitError( |
| 6767 | "Your depot_tools is out of date. " |
| 6768 | "This PRESUBMIT.py requires at least presubmit_support version 2.0.0, " |
| 6769 | "but your version is %d.%d.%d" % tuple(input_api.version)) |
| 6770 | ] |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6771 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6772 | results = [] |
| 6773 | # Make sure the tree is 'open'. |
| 6774 | results.extend( |
| 6775 | input_api.canned_checks.CheckTreeIsOpen( |
| 6776 | input_api, |
| 6777 | output_api, |
| 6778 | json_url='http://chromium-status.appspot.com/current?format=json')) |
[email protected] | 806e98e | 2010-03-19 17:49:27 | [diff] [blame] | 6779 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6780 | results.extend( |
| 6781 | input_api.canned_checks.CheckPatchFormatted(input_api, output_api)) |
| 6782 | results.extend( |
| 6783 | input_api.canned_checks.CheckChangeHasBugField(input_api, output_api)) |
| 6784 | results.extend( |
| 6785 | input_api.canned_checks.CheckChangeHasNoUnwantedTags( |
| 6786 | input_api, output_api)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6787 | return results |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6788 | |
| 6789 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6790 | def CheckStrings(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6791 | """Check string ICU syntax validity and if translation screenshots exist.""" |
| 6792 | # Skip translation screenshots check if a SkipTranslationScreenshotsCheck |
| 6793 | # footer is set to true. |
| 6794 | git_footers = input_api.change.GitFootersFromDescription() |
| 6795 | skip_screenshot_check_footer = [ |
| 6796 | footer.lower() for footer in git_footers.get( |
| 6797 | u'Skip-Translation-Screenshots-Check', []) |
| 6798 | ] |
| 6799 | run_screenshot_check = u'true' not in skip_screenshot_check_footer |
Edward Lesmes | f7c5c6d | 2020-05-14 23:30:02 | [diff] [blame] | 6800 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6801 | import os |
| 6802 | import re |
| 6803 | import sys |
| 6804 | from io import StringIO |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6805 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6806 | new_or_added_paths = set(f.LocalPath() for f in input_api.AffectedFiles() |
| 6807 | if (f.Action() == 'A' or f.Action() == 'M')) |
| 6808 | removed_paths = set(f.LocalPath() |
| 6809 | for f in input_api.AffectedFiles(include_deletes=True) |
| 6810 | if f.Action() == 'D') |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6811 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6812 | affected_grds = [ |
| 6813 | f for f in input_api.AffectedFiles() |
| 6814 | if f.LocalPath().endswith(('.grd', '.grdp')) |
| 6815 | ] |
| 6816 | affected_grds = [ |
| 6817 | f for f in affected_grds if not 'testdata' in f.LocalPath() |
| 6818 | ] |
| 6819 | if not affected_grds: |
| 6820 | return [] |
meacer | 8c0d383 | 2019-12-26 21:46:16 | [diff] [blame] | 6821 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6822 | affected_png_paths = [ |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 6823 | f.LocalPath() for f in input_api.AffectedFiles() |
| 6824 | if f.LocalPath().endswith('.png') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6825 | ] |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6826 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6827 | # Check for screenshots. Developers can upload screenshots using |
| 6828 | # tools/translation/upload_screenshots.py which finds and uploads |
| 6829 | # images associated with .grd files (e.g. test_grd/IDS_STRING.png for the |
| 6830 | # message named IDS_STRING in test.grd) and produces a .sha1 file (e.g. |
| 6831 | # test_grd/IDS_STRING.png.sha1) for each png when the upload is successful. |
| 6832 | # |
| 6833 | # The logic here is as follows: |
| 6834 | # |
| 6835 | # - If the CL has a .png file under the screenshots directory for a grd |
| 6836 | # file, warn the developer. Actual images should never be checked into the |
| 6837 | # Chrome repo. |
| 6838 | # |
| 6839 | # - If the CL contains modified or new messages in grd files and doesn't |
| 6840 | # contain the corresponding .sha1 files, warn the developer to add images |
| 6841 | # and upload them via tools/translation/upload_screenshots.py. |
| 6842 | # |
| 6843 | # - If the CL contains modified or new messages in grd files and the |
| 6844 | # corresponding .sha1 files, everything looks good. |
| 6845 | # |
| 6846 | # - If the CL contains removed messages in grd files but the corresponding |
| 6847 | # .sha1 files aren't removed, warn the developer to remove them. |
| 6848 | unnecessary_screenshots = [] |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6849 | invalid_sha1 = [] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6850 | missing_sha1 = [] |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6851 | missing_sha1_modified = [] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6852 | unnecessary_sha1_files = [] |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6853 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6854 | # This checks verifies that the ICU syntax of messages this CL touched is |
| 6855 | # valid, and reports any found syntax errors. |
| 6856 | # Without this presubmit check, ICU syntax errors in Chromium strings can land |
| 6857 | # without developers being aware of them. Later on, such ICU syntax errors |
| 6858 | # break message extraction for translation, hence would block Chromium |
| 6859 | # translations until they are fixed. |
| 6860 | icu_syntax_errors = [] |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6861 | sha1_pattern = input_api.re.compile(r'^[a-fA-F0-9]{40}$', |
| 6862 | input_api.re.MULTILINE) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6863 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6864 | def _CheckScreenshotAdded(screenshots_dir, message_id): |
| 6865 | sha1_path = input_api.os_path.join(screenshots_dir, |
| 6866 | message_id + '.png.sha1') |
| 6867 | if sha1_path not in new_or_added_paths: |
| 6868 | missing_sha1.append(sha1_path) |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6869 | elif not _CheckValidSha1(sha1_path): |
Sam Maier | b926c58c | 2023-08-08 19:58:25 | [diff] [blame] | 6870 | invalid_sha1.append(sha1_path) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6871 | |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6872 | def _CheckScreenshotModified(screenshots_dir, message_id): |
| 6873 | sha1_path = input_api.os_path.join(screenshots_dir, |
| 6874 | message_id + '.png.sha1') |
| 6875 | if sha1_path not in new_or_added_paths: |
| 6876 | missing_sha1_modified.append(sha1_path) |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6877 | elif not _CheckValidSha1(sha1_path): |
Sam Maier | b926c58c | 2023-08-08 19:58:25 | [diff] [blame] | 6878 | invalid_sha1.append(sha1_path) |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6879 | |
| 6880 | def _CheckValidSha1(sha1_path): |
Sam Maier | b926c58c | 2023-08-08 19:58:25 | [diff] [blame] | 6881 | return sha1_pattern.search( |
| 6882 | next("\n".join(f.NewContents()) for f in input_api.AffectedFiles() |
| 6883 | if f.LocalPath() == sha1_path)) |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6884 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6885 | def _CheckScreenshotRemoved(screenshots_dir, message_id): |
| 6886 | sha1_path = input_api.os_path.join(screenshots_dir, |
| 6887 | message_id + '.png.sha1') |
| 6888 | if input_api.os_path.exists( |
| 6889 | sha1_path) and sha1_path not in removed_paths: |
| 6890 | unnecessary_sha1_files.append(sha1_path) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6891 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6892 | def _ValidateIcuSyntax(text, level, signatures): |
| 6893 | """Validates ICU syntax of a text string. |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6894 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6895 | Check if text looks similar to ICU and checks for ICU syntax correctness |
| 6896 | in this case. Reports various issues with ICU syntax and values of |
| 6897 | variants. Supports checking of nested messages. Accumulate information of |
| 6898 | each ICU messages found in the text for further checking. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6899 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6900 | Args: |
| 6901 | text: a string to check. |
| 6902 | level: a number of current nesting level. |
| 6903 | signatures: an accumulator, a list of tuple of (level, variable, |
| 6904 | kind, variants). |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6905 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6906 | Returns: |
| 6907 | None if a string is not ICU or no issue detected. |
| 6908 | A tuple of (message, start index, end index) if an issue detected. |
| 6909 | """ |
| 6910 | valid_types = { |
| 6911 | 'plural': (frozenset( |
Rainhard Findling | 3cde3ef0 | 2024-02-05 18:40:32 | [diff] [blame] | 6912 | ['=0', '=1', '=2', '=3', 'zero', 'one', 'two', 'few', 'many', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6913 | 'other']), frozenset(['=1', 'other'])), |
| 6914 | 'selectordinal': (frozenset( |
Rainhard Findling | 3cde3ef0 | 2024-02-05 18:40:32 | [diff] [blame] | 6915 | ['=0', '=1', '=2', '=3', 'zero', 'one', 'two', 'few', 'many', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6916 | 'other']), frozenset(['one', 'other'])), |
| 6917 | 'select': (frozenset(), frozenset(['other'])), |
| 6918 | } |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6919 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6920 | # Check if the message looks like an attempt to use ICU |
| 6921 | # plural. If yes - check if its syntax strictly matches ICU format. |
| 6922 | like = re.match(r'^[^{]*\{[^{]*\b(plural|selectordinal|select)\b', |
| 6923 | text) |
| 6924 | if not like: |
| 6925 | signatures.append((level, None, None, None)) |
| 6926 | return |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6927 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6928 | # Check for valid prefix and suffix |
| 6929 | m = re.match( |
| 6930 | r'^([^{]*\{)([a-zA-Z0-9_]+),\s*' |
| 6931 | r'(plural|selectordinal|select),\s*' |
| 6932 | r'(?:offset:\d+)?\s*(.*)', text, re.DOTALL) |
| 6933 | if not m: |
| 6934 | return (('This message looks like an ICU plural, ' |
| 6935 | 'but does not follow ICU syntax.'), like.start(), |
| 6936 | like.end()) |
| 6937 | starting, variable, kind, variant_pairs = m.groups() |
| 6938 | variants, depth, last_pos = _ParseIcuVariants(variant_pairs, |
| 6939 | m.start(4)) |
| 6940 | if depth: |
| 6941 | return ('Invalid ICU format. Unbalanced opening bracket', last_pos, |
| 6942 | len(text)) |
| 6943 | first = text[0] |
| 6944 | ending = text[last_pos:] |
| 6945 | if not starting: |
| 6946 | return ('Invalid ICU format. No initial opening bracket', |
| 6947 | last_pos - 1, last_pos) |
| 6948 | if not ending or '}' not in ending: |
| 6949 | return ('Invalid ICU format. No final closing bracket', |
| 6950 | last_pos - 1, last_pos) |
| 6951 | elif first != '{': |
| 6952 | return (( |
| 6953 | 'Invalid ICU format. Extra characters at the start of a complex ' |
| 6954 | 'message (go/icu-message-migration): "%s"') % starting, 0, |
| 6955 | len(starting)) |
| 6956 | elif ending != '}': |
| 6957 | return (( |
| 6958 | 'Invalid ICU format. Extra characters at the end of a complex ' |
| 6959 | 'message (go/icu-message-migration): "%s"') % ending, |
| 6960 | last_pos - 1, len(text) - 1) |
| 6961 | if kind not in valid_types: |
| 6962 | return (('Unknown ICU message type %s. ' |
| 6963 | 'Valid types are: plural, select, selectordinal') % kind, |
| 6964 | 0, 0) |
| 6965 | known, required = valid_types[kind] |
| 6966 | defined_variants = set() |
| 6967 | for variant, variant_range, value, value_range in variants: |
| 6968 | start, end = variant_range |
| 6969 | if variant in defined_variants: |
| 6970 | return ('Variant "%s" is defined more than once' % variant, |
| 6971 | start, end) |
| 6972 | elif known and variant not in known: |
| 6973 | return ('Variant "%s" is not valid for %s message' % |
| 6974 | (variant, kind), start, end) |
| 6975 | defined_variants.add(variant) |
| 6976 | # Check for nested structure |
| 6977 | res = _ValidateIcuSyntax(value[1:-1], level + 1, signatures) |
| 6978 | if res: |
| 6979 | return (res[0], res[1] + value_range[0] + 1, |
| 6980 | res[2] + value_range[0] + 1) |
| 6981 | missing = required - defined_variants |
| 6982 | if missing: |
| 6983 | return ('Required variants missing: %s' % ', '.join(missing), 0, |
| 6984 | len(text)) |
| 6985 | signatures.append((level, variable, kind, defined_variants)) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6986 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6987 | def _ParseIcuVariants(text, offset=0): |
| 6988 | """Parse variants part of ICU complex message. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6989 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6990 | Builds a tuple of variant names and values, as well as |
| 6991 | their offsets in the input string. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6992 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6993 | Args: |
| 6994 | text: a string to parse |
| 6995 | offset: additional offset to add to positions in the text to get correct |
| 6996 | position in the complete ICU string. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6997 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6998 | Returns: |
| 6999 | List of tuples, each tuple consist of four fields: variant name, |
| 7000 | variant name span (tuple of two integers), variant value, value |
| 7001 | span (tuple of two integers). |
| 7002 | """ |
| 7003 | depth, start, end = 0, -1, -1 |
| 7004 | variants = [] |
| 7005 | key = None |
| 7006 | for idx, char in enumerate(text): |
| 7007 | if char == '{': |
| 7008 | if not depth: |
| 7009 | start = idx |
| 7010 | chunk = text[end + 1:start] |
| 7011 | key = chunk.strip() |
| 7012 | pos = offset + end + 1 + chunk.find(key) |
| 7013 | span = (pos, pos + len(key)) |
| 7014 | depth += 1 |
| 7015 | elif char == '}': |
| 7016 | if not depth: |
| 7017 | return variants, depth, offset + idx |
| 7018 | depth -= 1 |
| 7019 | if not depth: |
| 7020 | end = idx |
| 7021 | variants.append((key, span, text[start:end + 1], |
| 7022 | (offset + start, offset + end + 1))) |
| 7023 | return variants, depth, offset + end + 1 |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 7024 | |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7025 | old_sys_path = sys.path |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7026 | try: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7027 | sys.path = sys.path + [ |
| 7028 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 7029 | 'translation') |
| 7030 | ] |
| 7031 | from helper import grd_helper |
| 7032 | finally: |
| 7033 | sys.path = old_sys_path |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 7034 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7035 | for f in affected_grds: |
| 7036 | file_path = f.LocalPath() |
| 7037 | old_id_to_msg_map = {} |
| 7038 | new_id_to_msg_map = {} |
| 7039 | # Note that this code doesn't check if the file has been deleted. This is |
| 7040 | # OK because it only uses the old and new file contents and doesn't load |
| 7041 | # the file via its path. |
| 7042 | # It's also possible that a file's content refers to a renamed or deleted |
| 7043 | # file via a <part> tag, such as <part file="now-deleted-file.grdp">. This |
| 7044 | # is OK as well, because grd_helper ignores <part> tags when loading .grd or |
| 7045 | # .grdp files. |
| 7046 | if file_path.endswith('.grdp'): |
| 7047 | if f.OldContents(): |
| 7048 | old_id_to_msg_map = grd_helper.GetGrdpMessagesFromString( |
| 7049 | '\n'.join(f.OldContents())) |
| 7050 | if f.NewContents(): |
| 7051 | new_id_to_msg_map = grd_helper.GetGrdpMessagesFromString( |
| 7052 | '\n'.join(f.NewContents())) |
| 7053 | else: |
| 7054 | file_dir = input_api.os_path.dirname(file_path) or '.' |
| 7055 | if f.OldContents(): |
| 7056 | old_id_to_msg_map = grd_helper.GetGrdMessages( |
| 7057 | StringIO('\n'.join(f.OldContents())), file_dir) |
| 7058 | if f.NewContents(): |
| 7059 | new_id_to_msg_map = grd_helper.GetGrdMessages( |
| 7060 | StringIO('\n'.join(f.NewContents())), file_dir) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 7061 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7062 | grd_name, ext = input_api.os_path.splitext( |
| 7063 | input_api.os_path.basename(file_path)) |
| 7064 | screenshots_dir = input_api.os_path.join( |
| 7065 | input_api.os_path.dirname(file_path), |
| 7066 | grd_name + ext.replace('.', '_')) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 7067 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7068 | # Compute added, removed and modified message IDs. |
| 7069 | old_ids = set(old_id_to_msg_map) |
| 7070 | new_ids = set(new_id_to_msg_map) |
| 7071 | added_ids = new_ids - old_ids |
| 7072 | removed_ids = old_ids - new_ids |
| 7073 | modified_ids = set([]) |
| 7074 | for key in old_ids.intersection(new_ids): |
| 7075 | if (old_id_to_msg_map[key].ContentsAsXml('', True) != |
| 7076 | new_id_to_msg_map[key].ContentsAsXml('', True)): |
| 7077 | # The message content itself changed. Require an updated screenshot. |
| 7078 | modified_ids.add(key) |
| 7079 | elif old_id_to_msg_map[key].attrs['meaning'] != \ |
| 7080 | new_id_to_msg_map[key].attrs['meaning']: |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 7081 | # The message meaning changed. We later check for a screenshot. |
| 7082 | modified_ids.add(key) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7083 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7084 | if run_screenshot_check: |
| 7085 | # Check the screenshot directory for .png files. Warn if there is any. |
| 7086 | for png_path in affected_png_paths: |
| 7087 | if png_path.startswith(screenshots_dir): |
| 7088 | unnecessary_screenshots.append(png_path) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7089 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7090 | for added_id in added_ids: |
| 7091 | _CheckScreenshotAdded(screenshots_dir, added_id) |
Rainhard Findling | d8d0437 | 2020-08-13 13:30:09 | [diff] [blame] | 7092 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7093 | for modified_id in modified_ids: |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 7094 | _CheckScreenshotModified(screenshots_dir, modified_id) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7095 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7096 | for removed_id in removed_ids: |
| 7097 | _CheckScreenshotRemoved(screenshots_dir, removed_id) |
| 7098 | |
| 7099 | # Check new and changed strings for ICU syntax errors. |
| 7100 | for key in added_ids.union(modified_ids): |
| 7101 | msg = new_id_to_msg_map[key].ContentsAsXml('', True) |
| 7102 | err = _ValidateIcuSyntax(msg, 0, []) |
| 7103 | if err is not None: |
| 7104 | icu_syntax_errors.append(str(key) + ': ' + str(err[0])) |
| 7105 | |
| 7106 | results = [] |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 7107 | if run_screenshot_check: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7108 | if unnecessary_screenshots: |
| 7109 | results.append( |
| 7110 | output_api.PresubmitError( |
| 7111 | 'Do not include actual screenshots in the changelist. Run ' |
| 7112 | 'tools/translate/upload_screenshots.py to upload them instead:', |
| 7113 | sorted(unnecessary_screenshots))) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7114 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7115 | if missing_sha1: |
| 7116 | results.append( |
| 7117 | output_api.PresubmitError( |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 7118 | 'You are adding UI strings.\n' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7119 | 'To ensure the best translations, take screenshots of the relevant UI ' |
| 7120 | '(https://g.co/chrome/translation) and add these files to your ' |
| 7121 | 'changelist:', sorted(missing_sha1))) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7122 | |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 7123 | if invalid_sha1: |
| 7124 | results.append( |
| 7125 | output_api.PresubmitError( |
| 7126 | 'The following files do not seem to contain valid sha1 hashes. ' |
| 7127 | 'Make sure they contain hashes created by ' |
| 7128 | 'tools/translate/upload_screenshots.py:', sorted(invalid_sha1))) |
| 7129 | |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 7130 | if missing_sha1_modified: |
| 7131 | results.append( |
| 7132 | output_api.PresubmitError( |
| 7133 | 'You are modifying UI strings or their meanings.\n' |
| 7134 | 'To ensure the best translations, take screenshots of the relevant UI ' |
| 7135 | '(https://g.co/chrome/translation) and add these files to your ' |
| 7136 | 'changelist:', sorted(missing_sha1_modified))) |
| 7137 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7138 | if unnecessary_sha1_files: |
| 7139 | results.append( |
| 7140 | output_api.PresubmitError( |
| 7141 | 'You removed strings associated with these files. Remove:', |
| 7142 | sorted(unnecessary_sha1_files))) |
| 7143 | else: |
| 7144 | results.append( |
| 7145 | output_api.PresubmitPromptOrNotify('Skipping translation ' |
| 7146 | 'screenshots check.')) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7147 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7148 | if icu_syntax_errors: |
| 7149 | results.append( |
| 7150 | output_api.PresubmitPromptWarning( |
| 7151 | 'ICU syntax errors were found in the following strings (problems or ' |
| 7152 | 'feedback? Contact [email protected]):', |
| 7153 | items=icu_syntax_errors)) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 7154 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7155 | return results |
Mustafa Emre Acer | 51f2f74 | 2020-03-09 19:41:12 | [diff] [blame] | 7156 | |
| 7157 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 7158 | def CheckTranslationExpectations(input_api, output_api, |
Mustafa Emre Acer | 51f2f74 | 2020-03-09 19:41:12 | [diff] [blame] | 7159 | repo_root=None, |
| 7160 | translation_expectations_path=None, |
| 7161 | grd_files=None): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7162 | import sys |
| 7163 | affected_grds = [ |
| 7164 | f for f in input_api.AffectedFiles() |
| 7165 | if (f.LocalPath().endswith('.grd') or f.LocalPath().endswith('.grdp')) |
| 7166 | ] |
| 7167 | if not affected_grds: |
| 7168 | return [] |
| 7169 | |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7170 | old_sys_path = sys.path |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7171 | try: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7172 | sys.path = sys.path + [ |
| 7173 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 7174 | 'translation') |
| 7175 | ] |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7176 | sys.path = sys.path + [ |
| 7177 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 7178 | 'third_party', 'depot_tools') |
| 7179 | ] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7180 | from helper import git_helper |
| 7181 | from helper import translation_helper |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7182 | import gclient_utils |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7183 | finally: |
| 7184 | sys.path = old_sys_path |
| 7185 | |
| 7186 | # Check that translation expectations can be parsed and we can get a list of |
| 7187 | # translatable grd files. |repo_root| and |translation_expectations_path| are |
| 7188 | # only passed by tests. |
| 7189 | if not repo_root: |
| 7190 | repo_root = input_api.PresubmitLocalPath() |
| 7191 | if not translation_expectations_path: |
| 7192 | translation_expectations_path = input_api.os_path.join( |
| 7193 | repo_root, 'tools', 'gritsettings', 'translation_expectations.pyl') |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7194 | is_cog = gclient_utils.IsEnvCog() |
| 7195 | # Git is not available in cog workspaces. |
| 7196 | if not grd_files and not is_cog: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7197 | grd_files = git_helper.list_grds_in_repository(repo_root) |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7198 | if not grd_files: |
| 7199 | grd_files = [] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7200 | |
| 7201 | # Ignore bogus grd files used only for testing |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 7202 | # ui/webui/resources/tools/generate_grd.py. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7203 | ignore_path = input_api.os_path.join('ui', 'webui', 'resources', 'tools', |
| 7204 | 'tests') |
| 7205 | grd_files = [p for p in grd_files if ignore_path not in p] |
| 7206 | |
Ben Mason | 5d4c324 | 2025-04-15 20:28:37 | [diff] [blame] | 7207 | # Ensure no duplicate basenames. |
| 7208 | basename_to_src_paths = {} |
| 7209 | for grd_path in grd_files: |
| 7210 | basename = input_api.os_path.basename(grd_path) |
| 7211 | basename_to_src_paths.setdefault(basename, []) |
| 7212 | basename_to_src_paths[basename].append(grd_path) |
| 7213 | for src_paths in basename_to_src_paths.values(): |
| 7214 | if len(src_paths) > 1: |
| 7215 | return [ |
| 7216 | output_api.PresubmitNotifyResult( |
| 7217 | 'Multiple string files have the same basename. This will result in ' |
| 7218 | 'missing translations. Files: %s' |
| 7219 | % ', '.join(src_paths)) |
| 7220 | ] |
| 7221 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7222 | try: |
| 7223 | translation_helper.get_translatable_grds( |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7224 | repo_root, grd_files, translation_expectations_path, is_cog) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7225 | except Exception as e: |
| 7226 | return [ |
| 7227 | output_api.PresubmitNotifyResult( |
| 7228 | 'Failed to get a list of translatable grd files. This happens when:\n' |
| 7229 | ' - One of the modified grd or grdp files cannot be parsed or\n' |
| 7230 | ' - %s is not updated.\n' |
| 7231 | 'Stack:\n%s' % (translation_expectations_path, str(e))) |
| 7232 | ] |
Mustafa Emre Acer | 51f2f74 | 2020-03-09 19:41:12 | [diff] [blame] | 7233 | return [] |
| 7234 | |
Ken Rockot | c31f483 | 2020-05-29 18:58:51 | [diff] [blame] | 7235 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 7236 | def CheckStableMojomChanges(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7237 | """Changes to [Stable] mojom types must preserve backward-compatibility.""" |
| 7238 | changed_mojoms = input_api.AffectedFiles( |
| 7239 | include_deletes=True, |
| 7240 | file_filter=lambda f: f.LocalPath().endswith(('.mojom'))) |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 7241 | |
Bruce Dawson | 344ab26 | 2022-06-04 11:35:10 | [diff] [blame] | 7242 | if not changed_mojoms or input_api.no_diffs: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7243 | return [] |
| 7244 | |
| 7245 | delta = [] |
| 7246 | for mojom in changed_mojoms: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7247 | delta.append({ |
| 7248 | 'filename': mojom.LocalPath(), |
| 7249 | 'old': '\n'.join(mojom.OldContents()) or None, |
| 7250 | 'new': '\n'.join(mojom.NewContents()) or None, |
| 7251 | }) |
| 7252 | |
| 7253 | process = input_api.subprocess.Popen([ |
Takuto Ikuta | dca1022 | 2022-04-13 02:51:21 | [diff] [blame] | 7254 | input_api.python3_executable, |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7255 | input_api.os_path.join( |
| 7256 | input_api.PresubmitLocalPath(), 'mojo', 'public', 'tools', 'mojom', |
| 7257 | 'check_stable_mojom_compatibility.py'), '--src-root', |
| 7258 | input_api.PresubmitLocalPath() |
| 7259 | ], |
| 7260 | stdin=input_api.subprocess.PIPE, |
| 7261 | stdout=input_api.subprocess.PIPE, |
| 7262 | stderr=input_api.subprocess.PIPE, |
| 7263 | universal_newlines=True) |
| 7264 | (x, error) = process.communicate(input=input_api.json.dumps(delta)) |
| 7265 | if process.returncode: |
| 7266 | return [ |
| 7267 | output_api.PresubmitError( |
| 7268 | 'One or more [Stable] mojom definitions appears to have been changed ' |
Alex Gough | c9992165 | 2024-02-15 22:59:12 | [diff] [blame] | 7269 | 'in a way that is not backward-compatible. See ' |
| 7270 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/mojo/public/tools/bindings/README.md#versioning' |
| 7271 | ' for details.', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7272 | long_text=error) |
| 7273 | ] |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 7274 | return [] |
| 7275 | |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7276 | def CheckDeprecationOfPreferences(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7277 | """Removing a preference should come with a deprecation.""" |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7278 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7279 | def FilterFile(affected_file): |
| 7280 | """Accept only .cc files and the like.""" |
| 7281 | file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS] |
| 7282 | files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 7283 | input_api.DEFAULT_FILES_TO_SKIP) |
| 7284 | return input_api.FilterSourceFile( |
| 7285 | affected_file, |
| 7286 | files_to_check=file_inclusion_pattern, |
| 7287 | files_to_skip=files_to_skip) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7288 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7289 | def ModifiedLines(affected_file): |
| 7290 | """Returns a list of tuples (line number, line text) of added and removed |
| 7291 | lines. |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7292 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7293 | Deleted lines share the same line number as the previous line. |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7294 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7295 | This relies on the scm diff output describing each changed code section |
| 7296 | with a line of the form |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7297 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7298 | ^@@ <old line num>,<old size> <new line num>,<new size> @@$ |
| 7299 | """ |
| 7300 | line_num = 0 |
| 7301 | modified_lines = [] |
| 7302 | for line in affected_file.GenerateScmDiff().splitlines(): |
| 7303 | # Extract <new line num> of the patch fragment (see format above). |
| 7304 | m = input_api.re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', |
| 7305 | line) |
| 7306 | if m: |
| 7307 | line_num = int(m.groups(1)[0]) |
| 7308 | continue |
| 7309 | if ((line.startswith('+') and not line.startswith('++')) |
| 7310 | or (line.startswith('-') and not line.startswith('--'))): |
| 7311 | modified_lines.append((line_num, line)) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7312 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7313 | if not line.startswith('-'): |
| 7314 | line_num += 1 |
| 7315 | return modified_lines |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7316 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7317 | def FindLineWith(lines, needle): |
| 7318 | """Returns the line number (i.e. index + 1) in `lines` containing `needle`. |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7319 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7320 | If 0 or >1 lines contain `needle`, -1 is returned. |
| 7321 | """ |
| 7322 | matching_line_numbers = [ |
| 7323 | # + 1 for 1-based counting of line numbers. |
| 7324 | i + 1 for i, line in enumerate(lines) if needle in line |
| 7325 | ] |
| 7326 | return matching_line_numbers[0] if len( |
| 7327 | matching_line_numbers) == 1 else -1 |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7328 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7329 | def ModifiedPrefMigration(affected_file): |
| 7330 | """Returns whether the MigrateObsolete.*Pref functions were modified.""" |
| 7331 | # Determine first and last lines of MigrateObsolete.*Pref functions. |
| 7332 | new_contents = affected_file.NewContents() |
| 7333 | range_1 = (FindLineWith(new_contents, |
| 7334 | 'BEGIN_MIGRATE_OBSOLETE_LOCAL_STATE_PREFS'), |
| 7335 | FindLineWith(new_contents, |
| 7336 | 'END_MIGRATE_OBSOLETE_LOCAL_STATE_PREFS')) |
| 7337 | range_2 = (FindLineWith(new_contents, |
| 7338 | 'BEGIN_MIGRATE_OBSOLETE_PROFILE_PREFS'), |
| 7339 | FindLineWith(new_contents, |
| 7340 | 'END_MIGRATE_OBSOLETE_PROFILE_PREFS')) |
| 7341 | if (-1 in range_1 + range_2): |
| 7342 | raise Exception( |
| 7343 | 'Broken .*MIGRATE_OBSOLETE_.*_PREFS markers in browser_prefs.cc.' |
| 7344 | ) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7345 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7346 | # Check whether any of the modified lines are part of the |
| 7347 | # MigrateObsolete.*Pref functions. |
| 7348 | for line_nr, line in ModifiedLines(affected_file): |
| 7349 | if (range_1[0] <= line_nr <= range_1[1] |
| 7350 | or range_2[0] <= line_nr <= range_2[1]): |
| 7351 | return True |
| 7352 | return False |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7353 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7354 | register_pref_pattern = input_api.re.compile(r'Register.+Pref') |
| 7355 | browser_prefs_file_pattern = input_api.re.compile( |
| 7356 | r'chrome/browser/prefs/browser_prefs.cc') |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7357 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7358 | changes = input_api.AffectedFiles(include_deletes=True, |
| 7359 | file_filter=FilterFile) |
| 7360 | potential_problems = [] |
| 7361 | for f in changes: |
| 7362 | for line in f.GenerateScmDiff().splitlines(): |
| 7363 | # Check deleted lines for pref registrations. |
| 7364 | if (line.startswith('-') and not line.startswith('--') |
| 7365 | and register_pref_pattern.search(line)): |
| 7366 | potential_problems.append('%s: %s' % (f.LocalPath(), line)) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7367 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7368 | if browser_prefs_file_pattern.search(f.LocalPath()): |
| 7369 | # If the developer modified the MigrateObsolete.*Prefs() functions, we |
| 7370 | # assume that they knew that they have to deprecate preferences and don't |
| 7371 | # warn. |
| 7372 | try: |
| 7373 | if ModifiedPrefMigration(f): |
| 7374 | return [] |
| 7375 | except Exception as e: |
| 7376 | return [output_api.PresubmitError(str(e))] |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7377 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7378 | if potential_problems: |
| 7379 | return [ |
| 7380 | output_api.PresubmitPromptWarning( |
| 7381 | 'Discovered possible removal of preference registrations.\n\n' |
| 7382 | 'Please make sure to properly deprecate preferences by clearing their\n' |
| 7383 | 'value for a couple of milestones before finally removing the code.\n' |
| 7384 | 'Otherwise data may stay in the preferences files forever. See\n' |
| 7385 | 'Migrate*Prefs() in chrome/browser/prefs/browser_prefs.cc and\n' |
| 7386 | 'chrome/browser/prefs/README.md for examples.\n' |
| 7387 | 'This may be a false positive warning (e.g. if you move preference\n' |
| 7388 | 'registrations to a different place).\n', potential_problems) |
| 7389 | ] |
| 7390 | return [] |
| 7391 | |
Matt Stark | 6ef0887 | 2021-07-29 01:21:46 | [diff] [blame] | 7392 | |
| 7393 | def CheckConsistentGrdChanges(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7394 | """Changes to GRD files must be consistent for tools to read them.""" |
| 7395 | changed_grds = input_api.AffectedFiles( |
| 7396 | include_deletes=False, |
| 7397 | file_filter=lambda f: f.LocalPath().endswith(('.grd'))) |
| 7398 | errors = [] |
| 7399 | invalid_file_regexes = [(input_api.re.compile(matcher), msg) |
| 7400 | for matcher, msg in _INVALID_GRD_FILE_LINE] |
| 7401 | for grd in changed_grds: |
| 7402 | for i, line in enumerate(grd.NewContents()): |
| 7403 | for matcher, msg in invalid_file_regexes: |
| 7404 | if matcher.search(line): |
| 7405 | errors.append( |
| 7406 | output_api.PresubmitError( |
| 7407 | 'Problem on {grd}:{i} - {msg}'.format( |
| 7408 | grd=grd.LocalPath(), i=i + 1, msg=msg))) |
| 7409 | return errors |
| 7410 | |
Kevin McNee | 967dd2d2 | 2021-11-15 16:09:29 | [diff] [blame] | 7411 | |
Henrique Ferreiro | 2a4b5594 | 2021-11-29 23:45:36 | [diff] [blame] | 7412 | def CheckAssertAshOnlyCode(input_api, output_api): |
| 7413 | """Errors if a BUILD.gn file in an ash/ directory doesn't include |
Georg Neis | 94f87f0 | 2024-10-22 08:20:13 | [diff] [blame] | 7414 | assert(is_chromeos). |
| 7415 | For a transition period, assert(is_chromeos_ash) is also accepted. |
Henrique Ferreiro | 2a4b5594 | 2021-11-29 23:45:36 | [diff] [blame] | 7416 | """ |
| 7417 | |
| 7418 | def FileFilter(affected_file): |
| 7419 | """Includes directories known to be Ash only.""" |
| 7420 | return input_api.FilterSourceFile( |
| 7421 | affected_file, |
| 7422 | files_to_check=( |
| 7423 | r'^ash/.*BUILD\.gn', # Top-level src/ash/. |
| 7424 | r'.*/ash/.*BUILD\.gn'), # Any path component. |
| 7425 | files_to_skip=(input_api.DEFAULT_FILES_TO_SKIP)) |
| 7426 | |
| 7427 | errors = [] |
Georg Neis | 94f87f0 | 2024-10-22 08:20:13 | [diff] [blame] | 7428 | pattern = input_api.re.compile(r'assert\(is_chromeos(_ash)?\b') |
Jameson Thies | 0ce669f | 2021-12-09 15:56:56 | [diff] [blame] | 7429 | for f in input_api.AffectedFiles(include_deletes=False, |
| 7430 | file_filter=FileFilter): |
Henrique Ferreiro | 2a4b5594 | 2021-11-29 23:45:36 | [diff] [blame] | 7431 | if (not pattern.search(input_api.ReadFile(f))): |
| 7432 | errors.append( |
| 7433 | output_api.PresubmitError( |
Georg Neis | 94f87f0 | 2024-10-22 08:20:13 | [diff] [blame] | 7434 | 'Please add assert(is_chromeos) to %s. If that\'s not ' |
| 7435 | 'possible, please create an issue and add a comment such ' |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7436 | 'as:\n # TODO(crbug.com/XXX): add ' |
Georg Neis | 94f87f0 | 2024-10-22 08:20:13 | [diff] [blame] | 7437 | 'assert(is_chromeos) when ...' % f.LocalPath())) |
Henrique Ferreiro | 2a4b5594 | 2021-11-29 23:45:36 | [diff] [blame] | 7438 | return errors |
Lukasz Anforowicz | 7016d05e | 2021-11-30 03:56:27 | [diff] [blame] | 7439 | |
| 7440 | |
Kalvin Lee | 84ad17a | 2023-09-25 11:14:41 | [diff] [blame] | 7441 | def _IsMiraclePtrDisallowed(input_api, affected_file): |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 7442 | path = affected_file.UnixLocalPath() |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7443 | if not _IsCPlusPlusFile(input_api, path): |
| 7444 | return False |
| 7445 | |
Bartek Nowierski | 49b1a45 | 2024-06-08 00:24:35 | [diff] [blame] | 7446 | # Renderer-only code is generally allowed to use MiraclePtr. These |
| 7447 | # directories, however, are specifically disallowed, for perf reasons. |
Kalvin Lee | 84ad17a | 2023-09-25 11:14:41 | [diff] [blame] | 7448 | if ("third_party/blink/renderer/core/" in path |
| 7449 | or "third_party/blink/renderer/platform/heap/" in path |
Bartek Nowierski | 49b1a45 | 2024-06-08 00:24:35 | [diff] [blame] | 7450 | or "third_party/blink/renderer/platform/wtf/" in path |
| 7451 | or "third_party/blink/renderer/platform/fonts/" in path): |
| 7452 | return True |
| 7453 | |
| 7454 | # The below paths are an explicitly listed subset of Renderer-only code, |
| 7455 | # because the plan is to Oilpanize it. |
| 7456 | # TODO(crbug.com/330759291): Remove once Oilpanization is completed or |
| 7457 | # abandoned. |
| 7458 | if ("third_party/blink/renderer/core/paint/" in path |
| 7459 | or "third_party/blink/renderer/platform/graphics/compositing/" in path |
| 7460 | or "third_party/blink/renderer/platform/graphics/paint/" in path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7461 | return True |
| 7462 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7463 | # We assume that everything else may be used outside of Renderer processes. |
Lukasz Anforowicz | 7016d05e | 2021-11-30 03:56:27 | [diff] [blame] | 7464 | return False |
| 7465 | |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7466 | # TODO(crbug.com/40206238): Remove these checks, once they are replaced |
Lukasz Anforowicz | 7016d05e | 2021-11-30 03:56:27 | [diff] [blame] | 7467 | # by the Chromium Clang Plugin (which will be preferable because it will |
| 7468 | # 1) report errors earlier - at compile-time and 2) cover more rules). |
| 7469 | def CheckRawPtrUsage(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7470 | """Rough checks that raw_ptr<T> usage guidelines are followed.""" |
| 7471 | errors = [] |
| 7472 | # The regex below matches "raw_ptr<" following a word boundary, but not in a |
| 7473 | # C++ comment. |
| 7474 | raw_ptr_matcher = input_api.re.compile(r'^((?!//).)*\braw_ptr<') |
Kalvin Lee | 84ad17a | 2023-09-25 11:14:41 | [diff] [blame] | 7475 | file_filter = lambda f: _IsMiraclePtrDisallowed(input_api, f) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7476 | for f, line_num, line in input_api.RightHandSideLines(file_filter): |
| 7477 | if raw_ptr_matcher.search(line): |
| 7478 | errors.append( |
| 7479 | output_api.PresubmitError( |
| 7480 | 'Problem on {path}:{line} - '\ |
Kalvin Lee | 84ad17a | 2023-09-25 11:14:41 | [diff] [blame] | 7481 | 'raw_ptr<T> should not be used in this renderer code '\ |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7482 | '(as documented in the "Pointers to unprotected memory" '\ |
| 7483 | 'section in //base/memory/raw_ptr.md)'.format( |
| 7484 | path=f.LocalPath(), line=line_num))) |
| 7485 | return errors |
Henrique Ferreiro | f9819f2e3 | 2021-11-30 13:31:56 | [diff] [blame] | 7486 | |
mikt | 9337567c | 2023-09-08 18:38:17 | [diff] [blame] | 7487 | def CheckAdvancedMemorySafetyChecksUsage(input_api, output_api): |
| 7488 | """Checks that ADVANCED_MEMORY_SAFETY_CHECKS() macro is neither added nor |
| 7489 | removed as it is managed by the memory safety team internally. |
| 7490 | Do not add / remove it manually.""" |
| 7491 | paths = set([]) |
| 7492 | # The regex below matches "ADVANCED_MEMORY_SAFETY_CHECKS(" following a word |
| 7493 | # boundary, but not in a C++ comment. |
| 7494 | macro_matcher = input_api.re.compile( |
| 7495 | r'^((?!//).)*\bADVANCED_MEMORY_SAFETY_CHECKS\(', input_api.re.MULTILINE) |
| 7496 | for f in input_api.AffectedFiles(): |
| 7497 | if not _IsCPlusPlusFile(input_api, f.LocalPath()): |
| 7498 | continue |
| 7499 | if macro_matcher.search(f.GenerateScmDiff()): |
| 7500 | paths.add(f.LocalPath()) |
| 7501 | if not paths: |
| 7502 | return [] |
| 7503 | return [output_api.PresubmitPromptWarning( |
| 7504 | 'ADVANCED_MEMORY_SAFETY_CHECKS() macro is managed by ' \ |
| 7505 | 'the memory safety team (chrome-memory-safety@). ' \ |
| 7506 | 'Please contact us to add/delete the uses of the macro.', |
| 7507 | paths)] |
Henrique Ferreiro | f9819f2e3 | 2021-11-30 13:31:56 | [diff] [blame] | 7508 | |
| 7509 | def CheckPythonShebang(input_api, output_api): |
| 7510 | """Checks that python scripts use #!/usr/bin/env instead of hardcoding a |
| 7511 | system-wide python. |
| 7512 | """ |
| 7513 | errors = [] |
| 7514 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 7515 | affected_file, |
| 7516 | files_to_skip=((_THIRD_PARTY_EXCEPT_BLINK, |
| 7517 | r'third_party/blink/web_tests/external/') + input_api. |
| 7518 | DEFAULT_FILES_TO_SKIP), |
| 7519 | files_to_check=[r'.*\.py$']) |
| 7520 | for f in input_api.AffectedSourceFiles(sources): |
Takuto Ikuta | 3697651 | 2021-11-30 23:15:27 | [diff] [blame] | 7521 | for line_num, line in f.ChangedContents(): |
| 7522 | if line_num == 1 and line.startswith('#!/usr/bin/python'): |
| 7523 | errors.append(f.LocalPath()) |
| 7524 | break |
Henrique Ferreiro | f9819f2e3 | 2021-11-30 13:31:56 | [diff] [blame] | 7525 | |
| 7526 | result = [] |
| 7527 | for file in errors: |
| 7528 | result.append( |
| 7529 | output_api.PresubmitError( |
| 7530 | "Please use '#!/usr/bin/env python/2/3' as the shebang of %s" % |
| 7531 | file)) |
| 7532 | return result |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7533 | |
| 7534 | |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7535 | def CheckAndroidTestAnnotations(input_api, output_api): |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7536 | """Checks that tests have either @Batch or @DoNotBatch annotation. If this |
| 7537 | is not an instrumentation test, disregard.""" |
| 7538 | |
| 7539 | batch_annotation = input_api.re.compile(r'^\s*@Batch') |
| 7540 | do_not_batch_annotation = input_api.re.compile(r'^\s*@DoNotBatch') |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7541 | robolectric_test = input_api.re.compile(r'@RunWith\((.*?)RobolectricTestRunner') |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7542 | test_class_declaration = input_api.re.compile(r'^\s*public\sclass.*Test') |
| 7543 | uiautomator_test = input_api.re.compile(r'[uU]i[aA]utomator') |
Mark Schillaci | 8ef0d87 | 2023-07-18 22:07:59 | [diff] [blame] | 7544 | test_annotation_declaration = input_api.re.compile(r'^\s*public\s@interface\s.*{') |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7545 | |
ckitagawa | e8fd23b | 2022-06-17 15:29:38 | [diff] [blame] | 7546 | missing_annotation_errors = [] |
| 7547 | extra_annotation_errors = [] |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7548 | wrong_robolectric_test_runner_errors = [] |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7549 | |
| 7550 | def _FilterFile(affected_file): |
| 7551 | return input_api.FilterSourceFile( |
| 7552 | affected_file, |
| 7553 | files_to_skip=input_api.DEFAULT_FILES_TO_SKIP, |
| 7554 | files_to_check=[r'.*Test\.java$']) |
| 7555 | |
| 7556 | for f in input_api.AffectedSourceFiles(_FilterFile): |
| 7557 | batch_matched = None |
| 7558 | do_not_batch_matched = None |
| 7559 | is_instrumentation_test = True |
Mark Schillaci | 8ef0d87 | 2023-07-18 22:07:59 | [diff] [blame] | 7560 | test_annotation_declaration_matched = None |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7561 | has_base_robolectric_rule = False |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7562 | for line in f.NewContents(): |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7563 | if 'BaseRobolectricTestRule' in line: |
| 7564 | has_base_robolectric_rule = True |
| 7565 | continue |
| 7566 | if m := robolectric_test.search(line): |
| 7567 | is_instrumentation_test = False |
| 7568 | if m.group(1) == '' and not has_base_robolectric_rule: |
Yiwei Zhang | 5341bf0 | 2025-03-20 16:34:13 | [diff] [blame] | 7569 | path = str(f.LocalPath()) |
| 7570 | # These two spots cannot use it. |
| 7571 | if 'webapk' not in path and 'build' not in path: |
| 7572 | wrong_robolectric_test_runner_errors.append(path) |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7573 | break |
| 7574 | if uiautomator_test.search(line): |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7575 | is_instrumentation_test = False |
| 7576 | break |
| 7577 | if not batch_matched: |
| 7578 | batch_matched = batch_annotation.search(line) |
| 7579 | if not do_not_batch_matched: |
| 7580 | do_not_batch_matched = do_not_batch_annotation.search(line) |
| 7581 | test_class_declaration_matched = test_class_declaration.search( |
| 7582 | line) |
Mark Schillaci | 8ef0d87 | 2023-07-18 22:07:59 | [diff] [blame] | 7583 | test_annotation_declaration_matched = test_annotation_declaration.search(line) |
| 7584 | if test_class_declaration_matched or test_annotation_declaration_matched: |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7585 | break |
Mark Schillaci | 8ef0d87 | 2023-07-18 22:07:59 | [diff] [blame] | 7586 | if test_annotation_declaration_matched: |
| 7587 | continue |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7588 | if (is_instrumentation_test and |
| 7589 | not batch_matched and |
| 7590 | not do_not_batch_matched): |
Sam Maier | 4cef924 | 2022-10-03 14:21:24 | [diff] [blame] | 7591 | missing_annotation_errors.append(str(f.LocalPath())) |
ckitagawa | e8fd23b | 2022-06-17 15:29:38 | [diff] [blame] | 7592 | if (not is_instrumentation_test and |
| 7593 | (batch_matched or |
| 7594 | do_not_batch_matched)): |
Sam Maier | 4cef924 | 2022-10-03 14:21:24 | [diff] [blame] | 7595 | extra_annotation_errors.append(str(f.LocalPath())) |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7596 | |
| 7597 | results = [] |
| 7598 | |
ckitagawa | e8fd23b | 2022-06-17 15:29:38 | [diff] [blame] | 7599 | if missing_annotation_errors: |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7600 | results.append( |
| 7601 | output_api.PresubmitPromptWarning( |
| 7602 | """ |
Andrew Grieve | 43a5cf8 | 2023-09-08 15:09:46 | [diff] [blame] | 7603 | A change was made to an on-device test that has neither been annotated with |
| 7604 | @Batch nor @DoNotBatch. If this is a new test, please add the annotation. If |
| 7605 | this is an existing test, please consider adding it if you are sufficiently |
| 7606 | familiar with the test (but do so as a separate change). |
| 7607 | |
Jens Mueller | 2085ff8 | 2023-02-27 11:54:49 | [diff] [blame] | 7608 | 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] | 7609 | """, missing_annotation_errors)) |
| 7610 | if extra_annotation_errors: |
| 7611 | results.append( |
| 7612 | output_api.PresubmitPromptWarning( |
| 7613 | """ |
| 7614 | Robolectric tests do not need a @Batch or @DoNotBatch annotations. |
| 7615 | """, extra_annotation_errors)) |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7616 | if wrong_robolectric_test_runner_errors: |
| 7617 | results.append( |
| 7618 | output_api.PresubmitPromptWarning( |
| 7619 | """ |
Wenyu Fu | 0005ab8 | 2025-01-03 18:13:26 | [diff] [blame] | 7620 | Robolectric tests should use either @RunWith(BaseRobolectricTestRunner.class) (or |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7621 | a subclass of it), or use "@Rule BaseRobolectricTestRule". |
| 7622 | """, wrong_robolectric_test_runner_errors)) |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7623 | |
| 7624 | return results |
Sam Maier | 4cef924 | 2022-10-03 14:21:24 | [diff] [blame] | 7625 | |
| 7626 | |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7627 | def _CheckAndroidNullAwayAnnotatedClasses(input_api, output_api): |
| 7628 | """Checks that Java classes/interfaces/annotations are null-annotated.""" |
| 7629 | |
Henrique Nakashima | 2bdd8ad | 2025-04-08 18:24:57 | [diff] [blame] | 7630 | # Temporary, crbug.com/389129271 |
| 7631 | if input_api.change.RepositoryRoot().endswith('clank'): |
| 7632 | return [] |
| 7633 | |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7634 | nullmarked_annotation = input_api.re.compile(r'^\s*@(NullMarked|NullUnmarked)') |
| 7635 | |
| 7636 | missing_annotation_errors = [] |
| 7637 | |
| 7638 | def _FilterFile(affected_file): |
| 7639 | return input_api.FilterSourceFile( |
| 7640 | affected_file, |
| 7641 | files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api. |
| 7642 | DEFAULT_FILES_TO_SKIP + ( |
| 7643 | r'.*Test.*\.java', |
| 7644 | r'^android_webview/.*', # Temporary, crbug.com/389129271 |
| 7645 | r'^build/.*', |
Henrique Nakashima | f0a60480 | 2025-04-17 14:10:34 | [diff] [blame] | 7646 | r'^chrome/android/.*', # Temporary, crbug.com/389129271 |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7647 | r'^chromecast/.*', |
| 7648 | r'^components/cronet/.*', |
| 7649 | r'^tools/.*', |
| 7650 | )), |
| 7651 | files_to_check=[r'.*\.java$']) |
| 7652 | |
| 7653 | for f in input_api.AffectedSourceFiles(_FilterFile): |
Henrique Nakashima | c660543 | 2025-04-24 18:11:59 | [diff] [blame] | 7654 | if f.Action() != 'A': |
| 7655 | continue |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7656 | for line in f.NewContents(): |
| 7657 | if nullmarked_annotation.search(line): |
| 7658 | break |
| 7659 | else: |
| 7660 | missing_annotation_errors.append(str(f.LocalPath())) |
| 7661 | |
| 7662 | results = [] |
| 7663 | |
| 7664 | if missing_annotation_errors: |
| 7665 | results.append( |
Henrique Nakashima | 8bafbc5 | 2025-04-22 19:38:42 | [diff] [blame] | 7666 | output_api.PresubmitError( |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7667 | """ |
| 7668 | Please add @NullMarked and fix the NullAway warnings in the following files |
| 7669 | (see https://chromium.googlesource.com/chromium/src/+/main/styleguide/java/nullaway.md): |
| 7670 | """, missing_annotation_errors)) |
| 7671 | |
| 7672 | return results |
| 7673 | |
| 7674 | |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7675 | def CheckNoJsInIos(input_api, output_api): |
| 7676 | """Checks to make sure that JavaScript files are not used on iOS.""" |
| 7677 | |
| 7678 | def _FilterFile(affected_file): |
| 7679 | return input_api.FilterSourceFile( |
| 7680 | affected_file, |
| 7681 | files_to_skip=input_api.DEFAULT_FILES_TO_SKIP + |
Daniel White | 44b8bd0 | 2023-08-22 16:20:36 | [diff] [blame] | 7682 | (r'^ios/third_party/*', r'^ios/tools/*', r'^third_party/*', |
| 7683 | r'^components/autofill/ios/form_util/resources/*'), |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7684 | files_to_check=[r'^ios/.*\.js$', r'.*/ios/.*\.js$']) |
| 7685 | |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7686 | deleted_files = [] |
| 7687 | |
| 7688 | # Collect filenames of all removed JS files. |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 7689 | for f in input_api.AffectedFiles(file_filter=_FilterFile): |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7690 | local_path = f.LocalPath() |
| 7691 | |
| 7692 | if input_api.os_path.splitext(local_path)[1] == '.js' and f.Action() == 'D': |
| 7693 | deleted_files.append(input_api.os_path.basename(local_path)) |
| 7694 | |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7695 | error_paths = [] |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7696 | moved_paths = [] |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7697 | warning_paths = [] |
| 7698 | |
| 7699 | for f in input_api.AffectedSourceFiles(_FilterFile): |
| 7700 | local_path = f.LocalPath() |
| 7701 | |
| 7702 | if input_api.os_path.splitext(local_path)[1] == '.js': |
| 7703 | if f.Action() == 'A': |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7704 | if input_api.os_path.basename(local_path) in deleted_files: |
| 7705 | # This script was probably moved rather than newly created. |
| 7706 | # Present a warning instead of an error for these cases. |
| 7707 | moved_paths.append(local_path) |
| 7708 | else: |
| 7709 | error_paths.append(local_path) |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7710 | elif f.Action() != 'D': |
| 7711 | warning_paths.append(local_path) |
| 7712 | |
| 7713 | results = [] |
| 7714 | |
| 7715 | if warning_paths: |
| 7716 | results.append(output_api.PresubmitPromptWarning( |
| 7717 | 'TypeScript is now fully supported for iOS feature scripts. ' |
| 7718 | 'Consider converting JavaScript files to TypeScript. See ' |
| 7719 | '//ios/web/public/js_messaging/README.md for more details.', |
| 7720 | warning_paths)) |
| 7721 | |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7722 | if moved_paths: |
| 7723 | results.append(output_api.PresubmitPromptWarning( |
| 7724 | 'Do not use JavaScript on iOS for new files as TypeScript is ' |
| 7725 | 'fully supported. (If this is a moved file, you may leave the ' |
| 7726 | 'script unconverted.) See //ios/web/public/js_messaging/README.md ' |
| 7727 | 'for help using scripts on iOS.', moved_paths)) |
| 7728 | |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7729 | if error_paths: |
| 7730 | results.append(output_api.PresubmitError( |
| 7731 | 'Do not use JavaScript on iOS as TypeScript is fully supported. ' |
| 7732 | 'See //ios/web/public/js_messaging/README.md for help using ' |
| 7733 | 'scripts on iOS.', error_paths)) |
| 7734 | |
| 7735 | return results |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7736 | |
| 7737 | def CheckLibcxxRevisionsMatch(input_api, output_api): |
| 7738 | """Check to make sure the libc++ version matches across deps files.""" |
Andrew Grieve | 21bb679 | 2023-03-27 19:06:48 | [diff] [blame] | 7739 | # Disable check for changes to sub-repositories. |
| 7740 | if input_api.PresubmitLocalPath() != input_api.change.RepositoryRoot(): |
Sam Maier | b926c58c | 2023-08-08 19:58:25 | [diff] [blame] | 7741 | return [] |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7742 | |
| 7743 | DEPS_FILES = [ 'DEPS', 'buildtools/deps_revisions.gni' ] |
| 7744 | |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 7745 | file_filter = lambda f: f.UnixLocalPath() in DEPS_FILES |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7746 | changed_deps_files = input_api.AffectedFiles(file_filter=file_filter) |
| 7747 | if not changed_deps_files: |
| 7748 | return [] |
| 7749 | |
| 7750 | def LibcxxRevision(file): |
| 7751 | file = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 7752 | *file.split('/')) |
| 7753 | return input_api.re.search( |
| 7754 | r'libcxx_revision.*[:=].*[\'"](\w+)[\'"]', |
| 7755 | input_api.ReadFile(file)).group(1) |
| 7756 | |
| 7757 | if len(set([LibcxxRevision(f) for f in DEPS_FILES])) == 1: |
| 7758 | return [] |
| 7759 | |
| 7760 | return [output_api.PresubmitError( |
| 7761 | 'libcxx_revision not equal across %s' % ', '.join(DEPS_FILES), |
| 7762 | changed_deps_files)] |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7763 | |
| 7764 | |
| 7765 | def CheckDanglingUntriaged(input_api, output_api): |
| 7766 | """Warn developers adding DanglingUntriaged raw_ptr.""" |
| 7767 | |
| 7768 | # Ignore during git presubmit --all. |
| 7769 | # |
| 7770 | # This would be too costly, because this would check every lines of every |
| 7771 | # C++ files. Check from _BANNED_CPP_FUNCTIONS are also reading the whole |
| 7772 | # source code, but only once to apply every checks. It seems the bots like |
| 7773 | # `win-presubmit` are particularly sensitive to reading the files. Adding |
| 7774 | # this check caused the bot to run 2x longer. See https://crbug.com/1486612. |
| 7775 | if input_api.no_diffs: |
Arthur Sonzogni | 9eafd22 | 2023-11-10 08:50:39 | [diff] [blame] | 7776 | return [] |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7777 | |
| 7778 | def FilterFile(file): |
| 7779 | return input_api.FilterSourceFile( |
| 7780 | file, |
| 7781 | files_to_check=[r".*\.(h|cc|cpp|cxx|m|mm)$"], |
| 7782 | files_to_skip=[r"^base/allocator.*"], |
| 7783 | ) |
| 7784 | |
| 7785 | count = 0 |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 7786 | for f in input_api.AffectedFiles(file_filter=FilterFile): |
Arthur Sonzogni | 9eafd22 | 2023-11-10 08:50:39 | [diff] [blame] | 7787 | count -= sum([l.count("DanglingUntriaged") for l in f.OldContents()]) |
| 7788 | count += sum([l.count("DanglingUntriaged") for l in f.NewContents()]) |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7789 | |
| 7790 | # Most likely, nothing changed: |
| 7791 | if count == 0: |
| 7792 | return [] |
| 7793 | |
| 7794 | # Congrats developers for improving it: |
| 7795 | if count < 0: |
Arthur Sonzogni | 9eafd22 | 2023-11-10 08:50:39 | [diff] [blame] | 7796 | message = f"DanglingUntriaged pointers removed: {-count}\nThank you!" |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7797 | return [output_api.PresubmitNotifyResult(message)] |
| 7798 | |
| 7799 | # Check for 'DanglingUntriaged-notes' in the description: |
| 7800 | notes_regex = input_api.re.compile("DanglingUntriaged-notes[:=]") |
| 7801 | if any( |
| 7802 | notes_regex.match(line) |
| 7803 | for line in input_api.change.DescriptionText().splitlines()): |
| 7804 | return [] |
| 7805 | |
| 7806 | # Check for DanglingUntriaged-notes in the git footer: |
| 7807 | if input_api.change.GitFootersFromDescription().get( |
| 7808 | "DanglingUntriaged-notes", []): |
| 7809 | return [] |
| 7810 | |
| 7811 | message = ( |
Arthur Sonzogni | 9eafd22 | 2023-11-10 08:50:39 | [diff] [blame] | 7812 | "Unexpected new occurrences of `DanglingUntriaged` detected. Please\n" + |
| 7813 | "avoid adding new ones\n" + |
| 7814 | "\n" + |
| 7815 | "See documentation:\n" + |
| 7816 | "https://chromium.googlesource.com/chromium/src/+/main/docs/dangling_ptr.md\n" + |
| 7817 | "\n" + |
| 7818 | "See also the guide to fix dangling pointers:\n" + |
| 7819 | "https://chromium.googlesource.com/chromium/src/+/main/docs/dangling_ptr_guide.md\n" + |
| 7820 | "\n" + |
| 7821 | "To disable this warning, please add in the commit description:\n" + |
Alex Gough | 26dcd85 | 2023-12-22 16:47:19 | [diff] [blame] | 7822 | "DanglingUntriaged-notes: <rationale for new untriaged dangling " + |
Arthur Sonzogni | 9eafd22 | 2023-11-10 08:50:39 | [diff] [blame] | 7823 | "pointers>" |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7824 | ) |
| 7825 | return [output_api.PresubmitPromptWarning(message)] |
Jan Keitel | 77be752 | 2023-10-12 20:40:49 | [diff] [blame] | 7826 | |
| 7827 | def CheckInlineConstexprDefinitionsInHeaders(input_api, output_api): |
| 7828 | """Checks that non-static constexpr definitions in headers are inline.""" |
| 7829 | # In a properly formatted file, constexpr definitions inside classes or |
| 7830 | # structs will have additional whitespace at the beginning of the line. |
| 7831 | # The pattern looks for variables initialized as constexpr kVar = ...; or |
| 7832 | # constexpr kVar{...}; |
| 7833 | # The pattern does not match expressions that have braces in kVar to avoid |
| 7834 | # matching constexpr functions. |
| 7835 | pattern = input_api.re.compile(r'^constexpr (?!inline )[^\(\)]*[={]') |
| 7836 | attribute_pattern = input_api.re.compile(r'(\[\[[a-zA-Z_:]+\]\]|[A-Z]+[A-Z_]+) ') |
| 7837 | problems = [] |
| 7838 | for f in input_api.AffectedFiles(): |
| 7839 | if not _IsCPlusPlusHeaderFile(input_api, f.LocalPath()): |
| 7840 | continue |
| 7841 | |
| 7842 | for line_number, line in f.ChangedContents(): |
| 7843 | line = attribute_pattern.sub('', line) |
| 7844 | if pattern.search(line): |
| 7845 | problems.append( |
| 7846 | f"{f.LocalPath()}: {line_number}\n {line}") |
| 7847 | |
| 7848 | if problems: |
| 7849 | return [ |
| 7850 | output_api.PresubmitPromptWarning( |
| 7851 | 'Consider inlining constexpr variable definitions in headers ' |
| 7852 | 'outside of classes to avoid unnecessary copies of the ' |
| 7853 | 'constant. See https://abseil.io/tips/168 for more details.', |
| 7854 | problems) |
| 7855 | ] |
| 7856 | else: |
| 7857 | return [] |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7858 | |
| 7859 | def CheckTodoBugReferences(input_api, output_api): |
| 7860 | """Checks that bugs in TODOs use updated issue tracker IDs.""" |
| 7861 | |
Manish Goregaokar | dc9e351 | 2025-02-03 15:30:58 | [diff] [blame] | 7862 | files_to_skip = ['PRESUBMIT_test.py', r"^third_party/rust/chromium_crates_io/vendor/.*"] |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7863 | |
| 7864 | def _FilterFile(affected_file): |
| 7865 | return input_api.FilterSourceFile( |
| 7866 | affected_file, |
| 7867 | files_to_skip=files_to_skip) |
| 7868 | |
| 7869 | # Monorail bug IDs are all less than or equal to 1524553 so check that all |
| 7870 | # bugs in TODOs are greater than that value. |
Tom Sepez | 8e62858 | 2025-02-14 02:18:55 | [diff] [blame] | 7871 | pattern = input_api.re.compile(r'.*\bTODO\([^\)0-9]*([0-9]+)\).*') |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7872 | problems = [] |
| 7873 | for f in input_api.AffectedSourceFiles(_FilterFile): |
| 7874 | for line_number, line in f.ChangedContents(): |
| 7875 | match = pattern.match(line) |
| 7876 | if match and int(match.group(1)) <= 1524553: |
| 7877 | problems.append( |
| 7878 | f"{f.LocalPath()}: {line_number}\n {line}") |
| 7879 | |
| 7880 | if problems: |
| 7881 | return [ |
| 7882 | output_api.PresubmitPromptWarning( |
Alison Gale | cb598de5 | 2024-04-26 17:03:25 | [diff] [blame] | 7883 | 'TODOs should use the new Chromium Issue Tracker IDs which can ' |
| 7884 | 'be found by navigating to the bug. See ' |
| 7885 | 'https://crbug.com/336778624 for more details.', |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7886 | problems) |
| 7887 | ] |
| 7888 | else: |
| 7889 | return [] |