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. |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 4 | """Top-level presubmit script for Chromium. |
| 5 | |
Daniel Cheng | d8824447 | 2022-05-16 09:08:47 | [diff] [blame] | 6 | See https://www.chromium.org/developers/how-tos/depottools/presubmit-scripts/ |
tfarina | 78bb92f4 | 2015-01-31 00:20:48 | [diff] [blame] | 7 | for more details about the presubmit API built into depot_tools. |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 8 | """ |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 9 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 10 | from typing import Callable |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 11 | from typing import Optional |
| 12 | from typing import Sequence |
mikt | 19226ff2 | 2024-08-27 05:28:21 | [diff] [blame] | 13 | from typing import Tuple |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 14 | from dataclasses import dataclass |
| 15 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 16 | PRESUBMIT_VERSION = '2.0.0' |
[email protected] | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 17 | |
[email protected] | 379e7dd | 2010-01-28 17:39:21 | [diff] [blame] | 18 | _EXCLUDED_PATHS = ( |
Bruce Dawson | 7f8566b | 2022-05-06 16:22:18 | [diff] [blame] | 19 | # Generated file |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 20 | (r"chrome/android/webapk/shell_apk/src/org/chromium" |
| 21 | r"/webapk/lib/runtime_library/IWebApkApi.java"), |
Mila Green | e3aa722 | 2021-09-07 16:34:08 | [diff] [blame] | 22 | # File needs to write to stdout to emulate a tool it's replacing. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 23 | r"chrome/updater/mac/keystone/ksadmin.mm", |
Ilya Sherman | e8a7d2d | 2020-07-25 04:33:47 | [diff] [blame] | 24 | # Generated file. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 25 | (r"^components/variations/proto/devtools/" |
Ilya Sherman | c167a96 | 2020-08-18 18:40:26 | [diff] [blame] | 26 | r"client_variations.js"), |
Bruce Dawson | 3bd976c | 2022-05-06 22:47:52 | [diff] [blame] | 27 | # These are video files, not typescript. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 28 | r"^media/test/data/.*.ts", |
| 29 | r"^native_client_sdksrc/build_tools/make_rules.py", |
| 30 | r"^native_client_sdk/src/build_tools/make_simple.py", |
| 31 | r"^native_client_sdk/src/tools/.*.mk", |
| 32 | r"^net/tools/spdyshark/.*", |
| 33 | r"^skia/.*", |
| 34 | r"^third_party/blink/.*", |
| 35 | r"^third_party/breakpad/.*", |
Darwin Huang | d74a9d3 | 2019-07-17 17:58:46 | [diff] [blame] | 36 | # sqlite is an imported third party dependency. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 37 | r"^third_party/sqlite/.*", |
| 38 | r"^v8/.*", |
[email protected] | 3e4eb11 | 2011-01-18 03:29:54 | [diff] [blame] | 39 | r".*MakeFile$", |
[email protected] | 1084ccc | 2012-03-14 03:22:53 | [diff] [blame] | 40 | r".+_autogen\.h$", |
Yue She | cf138055 | 2022-08-23 20:59:20 | [diff] [blame] | 41 | r".+_pb2(_grpc)?\.py$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 42 | r".+/pnacl_shim\.c$", |
| 43 | r"^gpu/config/.*_list_json\.cc$", |
| 44 | r"tools/md_browser/.*\.css$", |
Kenneth Russell | 077c8d9 | 2017-12-16 02:52:14 | [diff] [blame] | 45 | # Test pages for Maps telemetry tests. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 46 | r"tools/perf/page_sets/maps_perf_test.*", |
ehmaldonado | 78eee2ed | 2017-03-28 13:16:54 | [diff] [blame] | 47 | # Test pages for WebRTC telemetry tests. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 48 | r"tools/perf/page_sets/webrtc_cases.*", |
dpapad | 2efd445 | 2023-04-06 01:43:45 | [diff] [blame] | 49 | # Test file compared with generated output. |
| 50 | r"tools/polymer/tests/html_to_wrapper/.*.html.ts$", |
dpapad | a45be36c | 2024-08-07 20:19:35 | [diff] [blame] | 51 | # Third-party dependency frozen at a fixed version. |
| 52 | r"chrome/test/data/webui/chromeos/chai_v4.js$", |
[email protected] | 430641764 | 2009-06-11 00:33:40 | [diff] [blame] | 53 | ) |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 54 | |
John Abd-El-Malek | 759fea6 | 2021-03-13 03:41:14 | [diff] [blame] | 55 | _EXCLUDED_SET_NO_PARENT_PATHS = ( |
| 56 | # It's for historical reasons that blink isn't a top level directory, where |
| 57 | # it would be allowed to have "set noparent" to avoid top level owners |
| 58 | # accidentally +1ing changes. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 59 | 'third_party/blink/OWNERS', ) |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 60 | |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 61 | # Fragment of a regular expression that matches C++ and Objective-C++ |
| 62 | # implementation files. |
| 63 | _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$' |
| 64 | |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 65 | # Fragment of a regular expression that matches C++ and Objective-C++ |
| 66 | # header files. |
| 67 | _HEADER_EXTENSIONS = r'\.(h|hpp|hxx)$' |
| 68 | |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 69 | # Paths with sources that don't use //base. |
| 70 | _NON_BASE_DEPENDENT_PATHS = ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 71 | r"^chrome/browser/browser_switcher/bho/", |
| 72 | r"^tools/win/", |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 73 | ) |
| 74 | |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 75 | # Regular expression that matches code only used for test binaries |
| 76 | # (best effort). |
| 77 | _TEST_CODE_EXCLUDED_PATHS = ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 78 | r'.*/(fake_|test_|mock_).+%s' % _IMPLEMENTATION_EXTENSIONS, |
Marijn Kruisselbrink | 2a2d5fc | 2024-05-15 15:23:49 | [diff] [blame] | 79 | # Test support files, like: |
| 80 | # foo_test_support.cc |
| 81 | # bar_test_util_linux.cc (suffix) |
| 82 | # baz_test_base.cc |
| 83 | r'.+_test_(base|support|util)(_[a-z]+)?%s' % _IMPLEMENTATION_EXTENSIONS, |
James Cook | 1b4dc13 | 2021-03-09 22:45:13 | [diff] [blame] | 84 | # Test suite files, like: |
| 85 | # foo_browsertest.cc |
| 86 | # bar_unittest_mac.cc (suffix) |
| 87 | # baz_unittests.cc (plural) |
| 88 | r'.+_(api|browser|eg|int|perf|pixel|unit|ui)?test(s)?(_[a-z]+)?%s' % |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 89 | _IMPLEMENTATION_EXTENSIONS, |
Matthew Denton | 63ea1e6 | 2019-03-25 20:39:18 | [diff] [blame] | 90 | r'.+_(fuzz|fuzzer)(_[a-z]+)?%s' % _IMPLEMENTATION_EXTENSIONS, |
Victor Hugo Vianna Silva | c22e020 | 2021-06-09 19:46:21 | [diff] [blame] | 91 | r'.+sync_service_impl_harness%s' % _IMPLEMENTATION_EXTENSIONS, |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 92 | r'.*/(test|tool(s)?)/.*', |
danakj | 89f4708 | 2020-09-02 17:53:43 | [diff] [blame] | 93 | # content_shell is used for running content_browsertests. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 94 | r'content/shell/.*', |
danakj | 89f4708 | 2020-09-02 17:53:43 | [diff] [blame] | 95 | # Web test harness. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 96 | r'content/web_test/.*', |
[email protected] | 7b05498 | 2013-11-27 00:44:47 | [diff] [blame] | 97 | # Non-production example code. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 98 | r'mojo/examples/.*', |
[email protected] | 8176de1 | 2014-06-20 19:07:08 | [diff] [blame] | 99 | # Launcher for running iOS tests on the simulator. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 100 | r'testing/iossim/iossim\.mm$', |
Olivier Robin | bcea0fa | 2019-11-12 08:56:41 | [diff] [blame] | 101 | # EarlGrey app side code for tests. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 102 | r'ios/.*_app_interface\.mm$', |
Allen Bauer | 0678d77 | 2020-05-11 22:25:17 | [diff] [blame] | 103 | # Views Examples code |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 104 | r'ui/views/examples/.*', |
Austin Sullivan | 33da70a | 2020-10-07 15:39:41 | [diff] [blame] | 105 | # Chromium Codelab |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 106 | r'codelabs/*') |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 107 | |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 108 | _THIRD_PARTY_EXCEPT_BLINK = 'third_party/(?!blink/)' |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 109 | |
[email protected] | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 110 | _TEST_ONLY_WARNING = ( |
| 111 | 'You might be calling functions intended only for testing from\n' |
danakj | 5f6e3b8 | 2020-09-10 13:52:55 | [diff] [blame] | 112 | 'production code. If you are doing this from inside another method\n' |
| 113 | 'named as *ForTesting(), then consider exposing things to have tests\n' |
| 114 | 'make that same call directly.\n' |
| 115 | 'If that is not possible, you may put a comment on the same line with\n' |
| 116 | ' // IN-TEST \n' |
| 117 | 'to tell the PRESUBMIT script that the code is inside a *ForTesting()\n' |
| 118 | 'method and can be ignored. Do not do this inside production code.\n' |
| 119 | '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] | 120 | 'release apk.\n' |
| 121 | 'Note: this warning might be a false positive (crbug.com/1196548).') |
[email protected] | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 122 | |
| 123 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 124 | @dataclass |
| 125 | class BanRule: |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 126 | # String pattern. If the pattern begins with a slash, the pattern will be |
| 127 | # treated as a regular expression instead. |
| 128 | pattern: str |
| 129 | # Explanation as a sequence of strings. Each string in the sequence will be |
| 130 | # printed on its own line. |
mikt | 19226ff2 | 2024-08-27 05:28:21 | [diff] [blame] | 131 | explanation: Tuple[str, ...] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 132 | # Whether or not to treat this ban as a fatal error. If unspecified, |
| 133 | # defaults to true. |
| 134 | treat_as_error: Optional[bool] = None |
| 135 | # Paths that should be excluded from the ban check. Each string is a regular |
| 136 | # expression that will be matched against the path of the file being checked |
| 137 | # relative to the root of the source tree. |
| 138 | excluded_paths: Optional[Sequence[str]] = None |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 139 | # If True, surfaces any violation as a Gerrit comment on the CL after |
| 140 | # running the CQ. |
| 141 | surface_as_gerrit_lint: Optional[bool] = None |
[email protected] | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 142 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 143 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 144 | _BANNED_JAVA_IMPORTS: Sequence[BanRule] = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 145 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 146 | 'import java.net.URI;', |
| 147 | ('Use org.chromium.url.GURL instead of java.net.URI, where possible.', |
| 148 | ), |
| 149 | excluded_paths=( |
| 150 | (r'net/android/javatests/src/org/chromium/net/' |
| 151 | r'AndroidProxySelectorTest\.java'), |
| 152 | r'components/cronet/', |
| 153 | r'third_party/robolectric/local/', |
| 154 | ), |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 155 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 156 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 157 | 'import android.annotation.TargetApi;', |
| 158 | ('Do not use TargetApi, use @androidx.annotation.RequiresApi instead. ' |
| 159 | 'RequiresApi ensures that any calls are guarded by the appropriate ' |
| 160 | 'SDK_INT check. See https://crbug.com/1116486.', ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 161 | ), |
| 162 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 163 | 'import androidx.test.rule.ActivityTestRule;', |
| 164 | ('Do not use ActivityTestRule, use ' |
| 165 | 'org.chromium.base.test.BaseActivityTestRule instead.', ), |
| 166 | excluded_paths=('components/cronet/', ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 167 | ), |
Min Qin | bc44383c | 2023-02-22 17:25:26 | [diff] [blame] | 168 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 169 | 'import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;', |
| 170 | ('Do not use VectorDrawableCompat, use getResources().getDrawable() to ' |
| 171 | 'avoid extra indirections. Please also add trace event as the call ' |
| 172 | 'might take more than 20 ms to complete.', ), |
Min Qin | bc44383c | 2023-02-22 17:25:26 | [diff] [blame] | 173 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 174 | ) |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 175 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 176 | _BANNED_JAVA_FUNCTIONS: Sequence[BanRule] = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 177 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 178 | 'StrictMode.allowThreadDiskReads()', |
| 179 | ('Prefer using StrictModeContext.allowDiskReads() to using StrictMode ' |
| 180 | 'directly.', ), |
| 181 | False, |
Eric Stevenson | a9a98097 | 2017-09-23 00:04:41 | [diff] [blame] | 182 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 183 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 184 | 'StrictMode.allowThreadDiskWrites()', |
| 185 | ('Prefer using StrictModeContext.allowDiskWrites() to using StrictMode ' |
| 186 | 'directly.', ), |
| 187 | False, |
Eric Stevenson | a9a98097 | 2017-09-23 00:04:41 | [diff] [blame] | 188 | ), |
Daniel Cheng | 917ce54 | 2022-03-15 20:46:57 | [diff] [blame] | 189 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 190 | '.waitForIdleSync()', |
| 191 | ('Do not use waitForIdleSync as it masks underlying issues. There is ' |
| 192 | 'almost always something else you should wait on instead.', ), |
| 193 | False, |
Michael Thiessen | 0f2547e | 2020-07-27 21:55:36 | [diff] [blame] | 194 | ), |
Ashley Newson | 09cbd60 | 2022-10-26 11:40:14 | [diff] [blame] | 195 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 196 | r'/(?<!\bsuper\.)(?<!\bIntent )\bregisterReceiver\(', |
| 197 | ('Do not call android.content.Context.registerReceiver (or an override) ' |
| 198 | 'directly. Use one of the wrapper methods defined in ' |
| 199 | 'org.chromium.base.ContextUtils, such as ' |
| 200 | 'registerProtectedBroadcastReceiver, ' |
| 201 | 'registerExportedBroadcastReceiver, or ' |
| 202 | 'registerNonExportedBroadcastReceiver. See their documentation for ' |
| 203 | 'which one to use.', ), |
| 204 | True, |
| 205 | excluded_paths=( |
| 206 | r'.*Test[^a-z]', |
| 207 | r'third_party/', |
| 208 | 'base/android/java/src/org/chromium/base/ContextUtils.java', |
| 209 | 'chromecast/browser/android/apk/src/org/chromium/chromecast/shell/BroadcastReceiverScope.java', |
| 210 | ), |
Ashley Newson | 09cbd60 | 2022-10-26 11:40:14 | [diff] [blame] | 211 | ), |
Ted Choc | d5b327b1 | 2022-11-05 02:13:22 | [diff] [blame] | 212 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 213 | r'/(?:extends|new)\s*(?:android.util.)?Property<[A-Za-z.]+,\s*(?:Integer|Float)>', |
| 214 | ('Do not use Property<..., Integer|Float>, but use FloatProperty or ' |
| 215 | 'IntProperty because it will avoid unnecessary autoboxing of ' |
| 216 | 'primitives.', ), |
Ted Choc | d5b327b1 | 2022-11-05 02:13:22 | [diff] [blame] | 217 | ), |
Peilin Wang | bba4a865 | 2022-11-10 16:33:57 | [diff] [blame] | 218 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 219 | 'requestLayout()', |
| 220 | ('Layouts can be expensive. Prefer using ViewUtils.requestLayout(), ' |
| 221 | 'which emits a trace event with additional information to help with ' |
| 222 | 'scroll jank investigations. See http://crbug.com/1354176.', ), |
| 223 | False, |
| 224 | excluded_paths=( |
| 225 | 'ui/android/java/src/org/chromium/ui/base/ViewUtils.java', ), |
Peilin Wang | bba4a865 | 2022-11-10 16:33:57 | [diff] [blame] | 226 | ), |
Ted Choc | f40ea915 | 2023-02-14 19:02:39 | [diff] [blame] | 227 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 228 | 'ProfileManager.getLastUsedRegularProfile()', |
| 229 | ('Prefer passing in the Profile reference instead of relying on the ' |
| 230 | 'static getLastUsedRegularProfile() call. Only top level entry points ' |
| 231 | '(e.g. Activities) should call this method. Otherwise, the Profile ' |
| 232 | 'should either be passed in explicitly or retreived from an existing ' |
| 233 | 'entity with a reference to the Profile (e.g. WebContents).', ), |
| 234 | False, |
| 235 | excluded_paths=(r'.*Test[A-Z]?.*\.java', ), |
Ted Choc | f40ea915 | 2023-02-14 19:02:39 | [diff] [blame] | 236 | ), |
Min Qin | bc44383c | 2023-02-22 17:25:26 | [diff] [blame] | 237 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 238 | r'/(ResourcesCompat|getResources\(\))\.getDrawable\(\)', |
| 239 | ('getDrawable() can be expensive. If you have a lot of calls to ' |
| 240 | 'GetDrawable() or your code may introduce janks, please put your calls ' |
| 241 | 'inside a trace().', ), |
| 242 | False, |
| 243 | excluded_paths=(r'.*Test[A-Z]?.*\.java', ), |
Min Qin | bc44383c | 2023-02-22 17:25:26 | [diff] [blame] | 244 | ), |
Henrique Nakashima | bbf2b26 | 2023-03-10 17:21:39 | [diff] [blame] | 245 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 246 | r'/RecordHistogram\.getHistogram(ValueCount|TotalCount|Samples)ForTesting\(', |
| 247 | ('Raw histogram counts are easy to misuse; for example they don\'t reset ' |
| 248 | 'between batched tests. Use HistogramWatcher to check histogram records ' |
| 249 | 'instead.', ), |
| 250 | False, |
| 251 | excluded_paths=( |
| 252 | 'base/android/javatests/src/org/chromium/base/metrics/RecordHistogramTest.java', |
| 253 | 'base/test/android/javatests/src/org/chromium/base/test/util/HistogramWatcher.java', |
| 254 | ), |
Henrique Nakashima | bbf2b26 | 2023-03-10 17:21:39 | [diff] [blame] | 255 | ), |
Jenna Himawan | 859865d | 2025-02-25 22:22:31 | [diff] [blame] | 256 | BanRule( |
| 257 | r'/((announceForAccessibility\()|TYPE_ANNOUNCEMENT)', |
| 258 | ('Android 16 deprecates accessibility announcements, characterized by ' |
| 259 | 'the use of announceForAccessibility or the dispatch of ' |
| 260 | 'TYPE_ANNOUNCEMENT accessibility events. See ' |
| 261 | 'https://developer.android.com/about/versions/16/behavior-changes-all#disruptive-a11y' |
| 262 | ' for more details and suggested replacements.', ), |
| 263 | False, |
| 264 | ), |
Nate Fischer | d541ff8 | 2025-03-11 21:34:19 | [diff] [blame] | 265 | BanRule( |
| 266 | pattern=(r'IS_DESKTOP_ANDROID'), |
| 267 | explanation=( |
Eric Lok | c26a4666 | 2025-05-02 01:04:03 | [diff] [blame] | 268 | 'Do not add new uses of IS_DESKTOP_ANDROID build flag until you ' |
| 269 | 'have the approval of tedchoc@ or twellington@. ' |
| 270 | 'Background: it is highly important to reduce the divergence of ' |
| 271 | 'features across platforms. ' |
| 272 | 'Allowances may be granted to only the directories below: ' |
| 273 | '[build/, chrome/, components/, extensions/, infra/, tools/] ', |
| 274 | 'Note: in particular we need to avoid components shared with ' |
| 275 | 'WebView.', |
| 276 | ), |
Nate Fischer | d541ff8 | 2025-03-11 21:34:19 | [diff] [blame] | 277 | treat_as_error=False, |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 278 | surface_as_gerrit_lint=True, |
Nate Fischer | d541ff8 | 2025-03-11 21:34:19 | [diff] [blame] | 279 | ), |
Eric Stevenson | a9a98097 | 2017-09-23 00:04:41 | [diff] [blame] | 280 | ) |
| 281 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 282 | _BANNED_JAVASCRIPT_FUNCTIONS: Sequence[BanRule] = ( |
Clement Yan | 9b330cb | 2022-11-17 05:25:29 | [diff] [blame] | 283 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 284 | r'/\bchrome\.send\b', |
| 285 | ( |
| 286 | '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).', |
| 287 | 'Please use mojo instead for new webuis. https://docs.google.com/document/d/1RF-GSUoveYa37eoyZ9EhwMtaIwoW7Z88pIgNZ9YzQi4/edit#heading=h.gkk22wgk6wff', |
| 288 | ), |
| 289 | True, |
| 290 | ( |
| 291 | r'^(?!ash\/webui).+', |
| 292 | # TODO(crbug.com/1385601): pre-existing violations still need to be |
| 293 | # cleaned up. |
| 294 | 'ash/webui/common/resources/cr.m.js', |
| 295 | 'ash/webui/common/resources/multidevice_setup/multidevice_setup_browser_proxy.js', |
| 296 | 'ash/webui/common/resources/quick_unlock/lock_screen_constants.ts', |
| 297 | 'ash/webui/common/resources/smb_shares/smb_browser_proxy.js', |
| 298 | 'ash/webui/connectivity_diagnostics/resources/connectivity_diagnostics.ts', |
| 299 | 'ash/webui/diagnostics_ui/resources/diagnostics_browser_proxy.ts', |
| 300 | 'ash/webui/multidevice_debug/resources/logs.js', |
| 301 | 'ash/webui/multidevice_debug/resources/webui.js', |
| 302 | 'ash/webui/projector_app/resources/annotator/trusted/annotator_browser_proxy.js', |
| 303 | 'ash/webui/projector_app/resources/app/trusted/projector_browser_proxy.js', |
| 304 | # TODO(b/301634378): Remove violation exception once Scanning App |
| 305 | # migrated off usage of `chrome.send`. |
| 306 | 'ash/webui/scanning/resources/scanning_browser_proxy.ts', |
| 307 | ), |
| 308 | ), ) |
Clement Yan | 9b330cb | 2022-11-17 05:25:29 | [diff] [blame] | 309 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 310 | _BANNED_OBJC_FUNCTIONS: Sequence[BanRule] = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 311 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 312 | 'addTrackingRect:', |
| 313 | ( |
| 314 | 'The use of -[NSView addTrackingRect:owner:userData:assumeInside:] is' |
| 315 | 'prohibited. Please use CrTrackingArea instead.', |
| 316 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 317 | ), |
| 318 | False, |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 319 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 320 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 321 | r'/NSTrackingArea\W', |
| 322 | ( |
| 323 | 'The use of NSTrackingAreas is prohibited. Please use CrTrackingArea', |
| 324 | 'instead.', |
| 325 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 326 | ), |
| 327 | False, |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 328 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 329 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 330 | 'convertPointFromBase:', |
| 331 | ( |
| 332 | 'The use of -[NSView convertPointFromBase:] is almost certainly wrong.', |
| 333 | 'Please use |convertPoint:(point) fromView:nil| instead.', |
| 334 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 335 | ), |
| 336 | True, |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 337 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 338 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 339 | 'convertPointToBase:', |
| 340 | ( |
| 341 | 'The use of -[NSView convertPointToBase:] is almost certainly wrong.', |
| 342 | 'Please use |convertPoint:(point) toView:nil| instead.', |
| 343 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 344 | ), |
| 345 | True, |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 346 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 347 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 348 | 'convertRectFromBase:', |
| 349 | ( |
| 350 | 'The use of -[NSView convertRectFromBase:] is almost certainly wrong.', |
| 351 | 'Please use |convertRect:(point) fromView:nil| instead.', |
| 352 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 353 | ), |
| 354 | True, |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 355 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 356 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 357 | 'convertRectToBase:', |
| 358 | ( |
| 359 | 'The use of -[NSView convertRectToBase:] is almost certainly wrong.', |
| 360 | 'Please use |convertRect:(point) toView:nil| instead.', |
| 361 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 362 | ), |
| 363 | True, |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 364 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 365 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 366 | 'convertSizeFromBase:', |
| 367 | ( |
| 368 | 'The use of -[NSView convertSizeFromBase:] is almost certainly wrong.', |
| 369 | 'Please use |convertSize:(point) fromView:nil| instead.', |
| 370 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 371 | ), |
| 372 | True, |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 373 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 374 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 375 | 'convertSizeToBase:', |
| 376 | ( |
| 377 | 'The use of -[NSView convertSizeToBase:] is almost certainly wrong.', |
| 378 | 'Please use |convertSize:(point) toView:nil| instead.', |
| 379 | 'http://dev.chromium.org/developers/coding-style/cocoa-dos-and-donts', |
| 380 | ), |
| 381 | True, |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 382 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 383 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 384 | r"/\s+UTF8String\s*]", |
| 385 | ( |
| 386 | 'The use of -[NSString UTF8String] is dangerous as it can return null', |
| 387 | 'even if |canBeConvertedToEncoding:NSUTF8StringEncoding| returns YES.', |
| 388 | 'Please use |SysNSStringToUTF8| instead.', |
| 389 | ), |
| 390 | True, |
| 391 | excluded_paths=('^third_party/ocmock/OCMock/', ), |
jif | 6539870 | 2016-10-27 10:19:48 | [diff] [blame] | 392 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 393 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 394 | r'__unsafe_unretained', |
| 395 | ( |
| 396 | 'The use of __unsafe_unretained is almost certainly wrong, unless', |
| 397 | 'when interacting with NSFastEnumeration or NSInvocation.', |
| 398 | 'Please use __weak in files build with ARC, nothing otherwise.', |
| 399 | ), |
| 400 | False, |
Sylvain Defresne | 4cf1d18 | 2017-09-18 14:16:34 | [diff] [blame] | 401 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 402 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 403 | 'freeWhenDone:NO', |
| 404 | ( |
| 405 | 'The use of "freeWhenDone:NO" with the NoCopy creation of ', |
| 406 | 'Foundation types is prohibited.', |
| 407 | ), |
| 408 | True, |
Avi Drissman | 7382afa0 | 2019-04-29 23:27:13 | [diff] [blame] | 409 | ), |
Avi Drissman | 3d243a4 | 2023-08-01 16:53:59 | [diff] [blame] | 410 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 411 | 'This file requires ARC support.', |
| 412 | ( |
| 413 | 'ARC compilation is default in Chromium; do not add boilerplate to ', |
| 414 | 'files that require ARC.', |
| 415 | ), |
| 416 | True, |
Avi Drissman | 3d243a4 | 2023-08-01 16:53:59 | [diff] [blame] | 417 | ), |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 418 | ) |
| 419 | |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 420 | _BANNED_IOS_OBJC_FUNCTIONS = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 421 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 422 | r'/\bTEST[(]', |
| 423 | ('TEST() macro should not be used in Objective-C++ code as it does not ', |
| 424 | 'drain the autorelease pool at the end of the test. Use TEST_F() ', |
| 425 | 'macro instead with a fixture inheriting from PlatformTest (or a ', |
| 426 | 'typedef).'), |
| 427 | True, |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 428 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 429 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 430 | r'/\btesting::Test\b', |
| 431 | ('testing::Test should not be used in Objective-C++ code as it does ', |
| 432 | 'not drain the autorelease pool at the end of the test. Use ', |
| 433 | 'PlatformTest instead.'), |
| 434 | True, |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 435 | ), |
Ewann | 2ecc8d7 | 2022-07-18 07:41:23 | [diff] [blame] | 436 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 437 | ' systemImageNamed:', |
| 438 | ('+[UIImage systemImageNamed:] should not be used to create symbols.', |
| 439 | 'Instead use a wrapper defined in:', |
| 440 | 'ios/chrome/browser/shared/ui/symbols/symbol_helpers.h'), |
| 441 | True, |
| 442 | excluded_paths=( |
| 443 | 'ios/chrome/browser/shared/ui/symbols/symbol_helpers.mm', |
| 444 | 'ios/chrome/common', |
| 445 | # App extensions have restricted dependencies and thus can't use the |
| 446 | # wrappers. |
| 447 | r'^ios/chrome/\w+_extension/', |
| 448 | ), |
Ewann | 2ecc8d7 | 2022-07-18 07:41:23 | [diff] [blame] | 449 | ), |
Sylvain Defresne | 781b9f9 | 2024-12-11 09:36:18 | [diff] [blame] | 450 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 451 | r'public (RefCounted)?BrowserStateKeyedServiceFactory', |
| 452 | ('KeyedService factories in //ios/chrome/browser should inherit from', |
| 453 | '(Refcounted)?ProfileKeyedServieFactoryIOS, not directory from', |
| 454 | '(Refcounted)?BrowserStateKeyedServiceFactory.'), |
| 455 | treat_as_error=True, |
| 456 | excluded_paths=( |
| 457 | 'ios/components', |
| 458 | 'ios/web_view', |
| 459 | ), |
Sylvain Defresne | 781b9f9 | 2024-12-11 09:36:18 | [diff] [blame] | 460 | ), |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 461 | ) |
| 462 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 463 | _BANNED_IOS_EGTEST_FUNCTIONS: Sequence[BanRule] = (BanRule( |
| 464 | r'/\bEXPECT_OCMOCK_VERIFY\b', |
| 465 | ('EXPECT_OCMOCK_VERIFY should not be used in EarlGrey tests because ', |
| 466 | 'it is meant for GTests. Use [mock verify] instead.'), |
| 467 | True, |
| 468 | ), ) |
Peter K. Lee | 6c03ccff | 2019-07-15 14:40:05 | [diff] [blame] | 469 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 470 | _BANNED_CPP_FUNCTIONS: Sequence[BanRule] = ( |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 471 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 472 | '%#0', |
| 473 | ( |
| 474 | 'Zero-padded values that use "#" to add prefixes don\'t exhibit ', |
| 475 | 'consistent behavior, since the prefix is not prepended for zero ', |
| 476 | 'values. Use "0x%0..." instead.', |
| 477 | ), |
| 478 | False, |
| 479 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 7c0d98a | 2023-10-06 15:42:39 | [diff] [blame] | 480 | ), |
| 481 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 482 | r'/\busing namespace ', |
| 483 | ( |
| 484 | 'Using directives ("using namespace x") are banned by the Google Style', |
| 485 | 'Guide ( http://google.github.io/styleguide/cppguide.html#Namespaces ).', |
| 486 | 'Explicitly qualify symbols or use using declarations ("using x::foo").', |
| 487 | ), |
| 488 | True, |
| 489 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 94a56c4 | 2019-10-25 21:54:04 | [diff] [blame] | 490 | ), |
Antonio Gomes | 07300d0 | 2019-03-13 20:59:57 | [diff] [blame] | 491 | # Make sure that gtest's FRIEND_TEST() macro is not used; the |
| 492 | # FRIEND_TEST_ALL_PREFIXES() macro from base/gtest_prod_util.h should be |
| 493 | # used instead since that allows for FLAKY_ and DISABLED_ prefixes. |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 494 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 495 | 'FRIEND_TEST(', |
| 496 | ( |
| 497 | 'Chromium code should not use gtest\'s FRIEND_TEST() macro. Include', |
| 498 | 'base/gtest_prod_util.h and use FRIEND_TEST_ALL_PREFIXES() instead.', |
| 499 | ), |
| 500 | False, |
| 501 | excluded_paths=( |
| 502 | "base/gtest_prod_util.h", |
| 503 | "base/allocator/partition_allocator/src/partition_alloc/partition_alloc_base/gtest_prod_util.h", |
| 504 | ), |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 505 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 506 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 507 | 'setMatrixClip', |
| 508 | ( |
| 509 | 'Overriding setMatrixClip() is prohibited; ', |
| 510 | 'the base function is deprecated. ', |
| 511 | ), |
| 512 | True, |
| 513 | (), |
tomhudson | e2c14d55 | 2016-05-26 17:07:46 | [diff] [blame] | 514 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 515 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 516 | 'SkRefPtr', |
| 517 | ('The use of SkRefPtr is prohibited. ', 'Please use sk_sp<> instead.'), |
| 518 | True, |
| 519 | (), |
[email protected] | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 520 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 521 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 522 | 'SkAutoRef', |
| 523 | ('The indirect use of SkRefPtr via SkAutoRef is prohibited. ', |
| 524 | 'Please use sk_sp<> instead.'), |
| 525 | True, |
| 526 | (), |
[email protected] | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 527 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 528 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 529 | 'SkAutoTUnref', |
| 530 | ('The use of SkAutoTUnref is dangerous because it implicitly ', |
| 531 | 'converts to a raw pointer. Please use sk_sp<> instead.'), |
| 532 | True, |
| 533 | (), |
[email protected] | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 534 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 535 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 536 | 'SkAutoUnref', |
| 537 | ('The indirect use of SkAutoTUnref through SkAutoUnref is dangerous ', |
| 538 | 'because it implicitly converts to a raw pointer. ', |
| 539 | 'Please use sk_sp<> instead.'), |
| 540 | True, |
| 541 | (), |
[email protected] | 52657f6 | 2013-05-20 05:30:31 | [diff] [blame] | 542 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 543 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 544 | r'/HANDLE_EINTR\(.*close', |
| 545 | ('HANDLE_EINTR(close) is invalid. If close fails with EINTR, the file', |
| 546 | 'descriptor will be closed, and it is incorrect to retry the close.', |
| 547 | 'Either call close directly and ignore its return value, or wrap close', |
| 548 | 'in IGNORE_EINTR to use its return value. See http://crbug.com/269623' |
| 549 | ), |
| 550 | True, |
| 551 | (), |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 552 | ), |
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 | r'/IGNORE_EINTR\((?!.*close)', |
| 555 | ( |
| 556 | 'IGNORE_EINTR is only valid when wrapping close. To wrap other system', |
| 557 | 'calls, use HANDLE_EINTR. See http://crbug.com/269623', |
| 558 | ), |
| 559 | True, |
| 560 | ( |
| 561 | # Files that #define IGNORE_EINTR. |
| 562 | r'^base/posix/eintr_wrapper\.h$', |
| 563 | r'^ppapi/tests/test_broker\.cc$', |
| 564 | ), |
[email protected] | d89eec8 | 2013-12-03 14:10:59 | [diff] [blame] | 565 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 566 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 567 | r'/v8::Extension\(', |
| 568 | ( |
| 569 | 'Do not introduce new v8::Extensions into the code base, use', |
| 570 | 'gin::Wrappable instead. See http://crbug.com/334679', |
| 571 | ), |
| 572 | True, |
| 573 | (r'extensions/renderer/safe_builtins\.*', ), |
[email protected] | ec5b3f0 | 2014-04-04 18:43:43 | [diff] [blame] | 574 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 575 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 576 | '#pragma comment(lib,', |
| 577 | ('Specify libraries to link with in build files and not in the source.', |
| 578 | ), |
| 579 | True, |
| 580 | ( |
| 581 | r'^base/third_party/symbolize/.*', |
| 582 | r'^third_party/abseil-cpp/.*', |
Victor Hugo Vianna Silva | c86846c0 | 2025-03-07 06:56:37 | [diff] [blame] | 583 | r'^third_party/grpc/source/.*', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 584 | ), |
jam | e2d1a95 | 2016-04-02 00:27:10 | [diff] [blame] | 585 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 586 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 587 | r'/base::SequenceChecker\b', |
| 588 | ('Consider using SEQUENCE_CHECKER macros instead of the class directly.', |
| 589 | ), |
| 590 | False, |
| 591 | (), |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 592 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 593 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 594 | r'/base::ThreadChecker\b', |
| 595 | ('Consider using THREAD_CHECKER macros instead of the class directly.', |
| 596 | ), |
| 597 | False, |
| 598 | (), |
gab | d52c912a | 2017-05-11 04:15:59 | [diff] [blame] | 599 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 600 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 601 | r'/\b(?!(Sequenced|SingleThread))\w*TaskRunner::(GetCurrentDefault|CurrentDefaultHandle)', |
| 602 | ( |
| 603 | 'It is not allowed to call these methods from the subclasses ', |
| 604 | 'of Sequenced or SingleThread task runners.', |
| 605 | ), |
| 606 | True, |
| 607 | (), |
Sean Maher | 03efef1 | 2022-09-23 22:43:13 | [diff] [blame] | 608 | ), |
| 609 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 610 | r'/(Time(|Delta|Ticks)|ThreadTicks)::FromInternalValue|ToInternalValue', |
| 611 | ( |
| 612 | 'base::TimeXXX::FromInternalValue() and ToInternalValue() are', |
| 613 | 'deprecated (http://crbug.com/634507). Please avoid converting away', |
| 614 | 'from the Time types in Chromium code, especially if any math is', |
| 615 | 'being done on time values. For interfacing with platform/library', |
| 616 | 'APIs, use base::Time::(From,To)DeltaSinceWindowsEpoch() or', |
| 617 | 'base::{TimeDelta::In}Microseconds(), or one of the other type', |
| 618 | 'converter methods instead. For faking TimeXXX values (for unit', |
| 619 | 'testing only), use TimeXXX() + Microseconds(N). For', |
| 620 | 'other use cases, please contact base/time/OWNERS.', |
| 621 | ), |
| 622 | False, |
| 623 | excluded_paths=( |
| 624 | "base/time/time.h", |
| 625 | "base/allocator/partition_allocator/src/partition_alloc/partition_alloc_base/time/time.h", |
| 626 | ), |
Yuri Wiitala | 2f8de5c | 2017-07-21 00:11:06 | [diff] [blame] | 627 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 628 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 629 | 'CallJavascriptFunctionUnsafe', |
| 630 | ( |
| 631 | "Don't use CallJavascriptFunctionUnsafe() in new code. Instead, use", |
| 632 | 'AllowJavascript(), OnJavascriptAllowed()/OnJavascriptDisallowed(),', |
| 633 | 'and CallJavascriptFunction(). See https://goo.gl/qivavq.', |
| 634 | ), |
| 635 | False, |
| 636 | ( |
| 637 | r'^content/browser/webui/web_ui_impl\.(cc|h)$', |
| 638 | r'^content/public/browser/web_ui\.h$', |
| 639 | r'^content/public/test/test_web_ui\.(cc|h)$', |
| 640 | ), |
dbeam | b6f4fde | 2017-06-15 04:03:06 | [diff] [blame] | 641 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 642 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 643 | 'leveldb::DB::Open', |
| 644 | ( |
| 645 | 'Instead of leveldb::DB::Open() use leveldb_env::OpenDB() from', |
| 646 | 'third_party/leveldatabase/env_chromium.h. It exposes databases to', |
| 647 | "Chrome's tracing, making their memory usage visible.", |
| 648 | ), |
| 649 | True, |
| 650 | (r'^third_party/leveldatabase/.*\.(cc|h)$', ), |
Gabriel Charette | 0592c3a | 2017-07-26 12:02:04 | [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 | 'leveldb::NewMemEnv', |
| 654 | ( |
| 655 | 'Instead of leveldb::NewMemEnv() use leveldb_chrome::NewMemEnv() from', |
| 656 | 'third_party/leveldatabase/leveldb_chrome.h. It exposes environments', |
| 657 | "to Chrome's tracing, making their memory usage visible.", |
| 658 | ), |
| 659 | True, |
| 660 | (r'^third_party/leveldatabase/.*\.(cc|h)$', ), |
Chris Mumford | c38afb6 | 2017-10-09 17:55:08 | [diff] [blame] | 661 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 662 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 663 | 'base::ScopedMockTimeMessageLoopTaskRunner', |
| 664 | ( |
| 665 | 'ScopedMockTimeMessageLoopTaskRunner is deprecated. Prefer', |
| 666 | 'TaskEnvironment::TimeSource::MOCK_TIME. There are still a', |
| 667 | 'few cases that may require a ScopedMockTimeMessageLoopTaskRunner', |
| 668 | '(i.e. mocking the main MessageLoopForUI in browser_tests), but check', |
| 669 | 'with gab@ first if you think you need it)', |
| 670 | ), |
| 671 | False, |
| 672 | (), |
Eric Stevenson | 6b47b44c | 2017-08-30 20:41:57 | [diff] [blame] | 673 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 674 | BanRule( |
Peter Kasting | 5fdcd78 | 2025-01-13 14:57:07 | [diff] [blame] | 675 | '\bstd::aligned_(storage|union)\b', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 676 | ('std::aligned_storage and std::aligned_union are deprecated in', |
| 677 | 'C++23. Use an aligned char array instead.'), |
Peter Kasting | 5fdcd78 | 2025-01-13 14:57:07 | [diff] [blame] | 678 | True, |
| 679 | (), |
| 680 | ), |
| 681 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 682 | 'std::regex', |
| 683 | ( |
| 684 | 'Using std::regex adds unnecessary binary size to Chrome. Please use', |
| 685 | 're2::RE2 instead (crbug.com/755321)', |
| 686 | ), |
| 687 | True, |
| 688 | [ |
| 689 | # Abseil's benchmarks never linked into chrome. |
| 690 | 'third_party/abseil-cpp/.*_benchmark.cc', |
| 691 | ], |
Francois Doray | 43670e3 | 2017-09-27 12:40:38 | [diff] [blame] | 692 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 693 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 694 | r'/\bstd::sto(i|l|ul|ll|ull)\b', |
| 695 | ( |
| 696 | 'std::sto{i,l,ul,ll,ull}() use exceptions to communicate results. ', |
| 697 | 'Use base::StringTo[U]Int[64]() instead.', |
| 698 | ), |
| 699 | True, |
| 700 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 991618a6 | 2019-06-17 22:00:09 | [diff] [blame] | 701 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 702 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 703 | r'/\bstd::sto(f|d|ld)\b', |
| 704 | ( |
| 705 | 'std::sto{f,d,ld}() use exceptions to communicate results. ', |
| 706 | 'For locale-independent values, e.g. reading numbers from disk', |
| 707 | 'profiles, use base::StringToDouble().', |
| 708 | 'For user-visible values, parse using ICU.', |
| 709 | ), |
| 710 | True, |
| 711 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 991618a6 | 2019-06-17 22:00:09 | [diff] [blame] | 712 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 713 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 714 | r'/\bstd::to_string\b', |
| 715 | ( |
| 716 | 'std::to_string() is locale dependent and slower than alternatives.', |
| 717 | 'For locale-independent strings, e.g. writing numbers to disk', |
| 718 | 'profiles, use base::NumberToString().', |
| 719 | 'For user-visible strings, use base::FormatNumber() and', |
| 720 | 'the related functions in base/i18n/number_formatting.h.', |
| 721 | ), |
| 722 | True, |
| 723 | [ |
| 724 | # TODO(crbug.com/335672557): Please do not add to this list. Existing |
| 725 | # uses should removed. |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 726 | "third_party/blink/renderer/core/css/parser/css_proto_converter.cc", |
| 727 | "third_party/blink/renderer/core/editing/ime/edit_context.cc", |
| 728 | "third_party/blink/renderer/platform/graphics/bitmap_image_test.cc", |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 729 | _THIRD_PARTY_EXCEPT_BLINK |
| 730 | ], |
Daniel Bratell | 69334cc | 2019-03-26 11:07:45 | [diff] [blame] | 731 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 732 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 733 | r'/#include <(cctype|ctype\.h|cwctype|wctype.h)>', |
| 734 | ( |
| 735 | '<cctype>/<ctype.h>/<cwctype>/<wctype.h> are banned. Use', |
| 736 | '"third_party/abseil-cpp/absl/strings/ascii.h" instead.', |
| 737 | ), |
| 738 | True, |
| 739 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 6f79b20 | 2023-08-09 21:25:41 | [diff] [blame] | 740 | ), |
| 741 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 742 | r'/\bstd::shared_ptr\b', |
| 743 | ('std::shared_ptr is banned. Use scoped_refptr instead.', ), |
| 744 | True, |
| 745 | [ |
| 746 | # Needed for interop with third-party library. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 747 | r'^third_party/blink/renderer/core/typed_arrays/array_buffer/' + |
| 748 | r'array_buffer_contents\.(cc|h)', |
| 749 | r'^third_party/blink/renderer/core/typed_arrays/dom_array_buffer\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 750 | '^third_party/blink/renderer/bindings/core/v8/' + |
| 751 | 'v8_wasm_response_extensions.cc', |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 752 | r'^gin/array_buffer\.(cc|h)', |
| 753 | r'^gin/per_isolate_data\.(cc|h)', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 754 | '^chrome/services/sharing/nearby/', |
| 755 | # Needed for interop with third-party library libunwindstack. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 756 | r'^base/profiler/libunwindstack_unwinder_android\.(cc|h)', |
| 757 | r'^base/profiler/native_unwinder_android_memory_regions_map_impl.(cc|h)', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 758 | # Needed for interop with third-party boringssl cert verifier |
| 759 | '^third_party/boringssl/', |
| 760 | '^net/cert/', |
| 761 | '^net/tools/cert_verify_tool/', |
| 762 | '^services/cert_verifier/', |
| 763 | '^components/certificate_transparency/', |
| 764 | '^components/media_router/common/providers/cast/certificate/', |
Nina Satragno | e447f2d | 2025-05-08 16:32:19 | [diff] [blame] | 765 | '^components/trusted_vault/', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 766 | # gRPC provides some C++ libraries that use std::shared_ptr<>. |
| 767 | '^chromeos/ash/services/libassistant/grpc/', |
| 768 | '^chromecast/cast_core/grpc', |
| 769 | '^chromecast/cast_core/runtime/browser', |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 770 | r'^ios/chrome/test/earl_grey/chrome_egtest_plugin_client\.(mm|h)', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 771 | # Fuchsia provides C++ libraries that use std::shared_ptr<>. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 772 | r'^base/fuchsia/.*\.(cc|h)', |
| 773 | r'.*fuchsia.*test\.(cc|h)', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 774 | # Clang plugins have different build config. |
| 775 | '^tools/clang/plugins/', |
| 776 | _THIRD_PARTY_EXCEPT_BLINK |
| 777 | ], # Not an error in third_party folders. |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 778 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 779 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 780 | r'/\bstd::weak_ptr\b', |
| 781 | ('std::weak_ptr is banned. Use base::WeakPtr instead.', ), |
| 782 | True, |
| 783 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 991618a6 | 2019-06-17 22:00:09 | [diff] [blame] | 784 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 785 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 786 | r'/\blong long\b', |
| 787 | ('long long is banned. Use [u]int64_t instead.', ), |
| 788 | False, # Only a warning since it is already used. |
| 789 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 790 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 791 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 792 | r'/\b(absl|std)::any\b', |
| 793 | ( |
| 794 | '{absl,std}::any are banned due to incompatibility with the component ', |
| 795 | 'build.', |
| 796 | ), |
| 797 | True, |
| 798 | # Not an error in third party folders, though it probably should be :) |
| 799 | [_THIRD_PARTY_EXCEPT_BLINK], |
Daniel Cheng | c05fcc6 | 2022-01-12 16:54:29 | [diff] [blame] | 800 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 801 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 802 | r'/\bstd::bind\b', |
| 803 | ( |
| 804 | 'std::bind() is banned because of lifetime risks. Use ', |
| 805 | 'base::Bind{Once,Repeating}() instead.', |
| 806 | ), |
| 807 | True, |
| 808 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 809 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 810 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 811 | (r'/\bstd::(?:' |
| 812 | r'linear_congruential_engine|mersenne_twister_engine|' |
| 813 | r'subtract_with_carry_engine|discard_block_engine|' |
| 814 | r'independent_bits_engine|shuffle_order_engine|' |
| 815 | r'minstd_rand0?|mt19937(_64)?|ranlux(24|48)(_base)?|knuth_b|' |
| 816 | r'default_random_engine|' |
| 817 | r'random_device|' |
| 818 | r'seed_seq' |
| 819 | r')\b'), |
| 820 | ( |
| 821 | 'STL random number engines and generators are banned. Use the ', |
| 822 | 'helpers in base/rand_util.h instead, e.g. base::RandBytes() or ', |
| 823 | 'base::RandomBitGenerator.' |
| 824 | '', |
| 825 | 'Please reach out to [email protected] if the base APIs are ', |
| 826 | 'insufficient for your needs.', |
| 827 | ), |
| 828 | True, |
| 829 | [ |
| 830 | # Not an error in third_party folders. |
| 831 | _THIRD_PARTY_EXCEPT_BLINK, |
| 832 | # Various tools which build outside of Chrome. |
| 833 | r'testing/libfuzzer', |
Steinar H. Gunderson | e5689e4 | 2024-08-07 18:17:19 | [diff] [blame] | 834 | r'testing/perf/confidence', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 835 | r'tools/android/io_benchmark/', |
| 836 | # Fuzzers are allowed to use standard library random number generators |
| 837 | # since fuzzing speed + reproducibility is important. |
| 838 | r'tools/ipc_fuzzer/', |
| 839 | r'.+_fuzzer\.cc$', |
| 840 | r'.+_fuzzertest\.cc$', |
| 841 | # TODO(https://crbug.com/1380528): These are all unsanctioned uses of |
| 842 | # the standard library's random number generators, and should be |
| 843 | # migrated to the //base equivalent. |
| 844 | r'ash/ambient/model/ambient_topic_queue\.cc', |
| 845 | r'base/allocator/partition_allocator/src/partition_alloc/partition_alloc_unittest\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 846 | r'base/test/launcher/test_launcher\.cc', |
| 847 | r'cc/metrics/video_playback_roughness_reporter_unittest\.cc', |
| 848 | r'chrome/browser/apps/app_service/metrics/website_metrics\.cc', |
| 849 | r'chrome/browser/ash/power/auto_screen_brightness/monotone_cubic_spline_unittest\.cc', |
| 850 | r'chrome/browser/ash/printing/zeroconf_printer_detector_unittest\.cc', |
| 851 | r'chrome/browser/nearby_sharing/contacts/nearby_share_contact_manager_impl_unittest\.cc', |
| 852 | r'chrome/browser/nearby_sharing/contacts/nearby_share_contacts_sorter_unittest\.cc', |
| 853 | r'chrome/browser/privacy_budget/mesa_distribution_unittest\.cc', |
| 854 | r'chrome/browser/web_applications/test/web_app_test_utils\.cc', |
| 855 | r'chrome/browser/web_applications/test/web_app_test_utils\.cc', |
| 856 | r'chrome/browser/win/conflicts/module_blocklist_cache_util_unittest\.cc', |
| 857 | r'chromeos/ash/components/memory/userspace_swap/swap_storage_unittest\.cc', |
| 858 | r'chromeos/ash/components/memory/userspace_swap/userspace_swap\.cc', |
| 859 | r'components/metrics/metrics_state_manager\.cc', |
| 860 | r'components/omnibox/browser/history_quick_provider_performance_unittest\.cc', |
| 861 | r'components/zucchini/disassembler_elf_unittest\.cc', |
| 862 | r'content/browser/webid/federated_auth_request_impl\.cc', |
| 863 | r'content/browser/webid/federated_auth_request_impl\.cc', |
| 864 | r'media/cast/test/utility/udp_proxy\.h', |
| 865 | r'sql/recover_module/module_unittest\.cc', |
Nicolas Dossou-Gbete | af9023d | 2025-04-07 17:44:38 | [diff] [blame] | 866 | r'components/regional_capabilities/regional_capabilities_utils.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 867 | # Do not add new entries to this list. If you have a use case which is |
| 868 | # not satisfied by the current APIs (i.e. you need an explicitly-seeded |
| 869 | # sequence, or stability of some sort is required), please contact |
| 870 | # [email protected]. |
| 871 | ], |
Daniel Cheng | 192683f | 2022-11-01 20:52:44 | [diff] [blame] | 872 | ), |
| 873 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 874 | r'/\b(absl,std)::bind_front\b', |
| 875 | ('{absl,std}::bind_front() are banned. Use base::Bind{Once,Repeating}() ' |
| 876 | 'instead.', ), |
| 877 | True, |
| 878 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 879 | ), |
| 880 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 881 | r'/\bABSL_FLAG\b', |
| 882 | ('ABSL_FLAG is banned. Use base::CommandLine instead.', ), |
| 883 | True, |
| 884 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 885 | ), |
| 886 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 887 | r'/\babsl::c_', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 888 | ('Abseil container utilities are banned. Use std::ranges:: instead.', |
| 889 | ), |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 890 | True, |
| 891 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 892 | ), |
| 893 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 894 | r'/\babsl::FixedArray\b', |
| 895 | ('absl::FixedArray is banned. Use base::FixedArray instead.', ), |
| 896 | True, |
| 897 | [ |
| 898 | # base::FixedArray provides canonical access. |
| 899 | r'^base/types/fixed_array.h', |
| 900 | # Not an error in third_party folders. |
| 901 | _THIRD_PARTY_EXCEPT_BLINK, |
| 902 | ], |
Peter Kasting | 431239a | 2023-09-29 03:11:44 | [diff] [blame] | 903 | ), |
| 904 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 905 | r'/\babsl::FunctionRef\b', |
| 906 | ('absl::FunctionRef is banned. Use base::FunctionRef instead.', ), |
| 907 | True, |
| 908 | [ |
| 909 | # base::Bind{Once,Repeating} references absl::FunctionRef to disallow |
| 910 | # interoperability. |
| 911 | r'^base/functional/bind_internal\.h', |
| 912 | # base::FunctionRef is implemented on top of absl::FunctionRef. |
| 913 | r'^base/functional/function_ref.*\..+', |
| 914 | # Not an error in third_party folders. |
| 915 | _THIRD_PARTY_EXCEPT_BLINK, |
| 916 | ], |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 917 | ), |
| 918 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 919 | r'/\babsl::(Insecure)?BitGen\b', |
| 920 | ('absl random number generators are banned. Use the helpers in ' |
| 921 | 'base/rand_util.h instead, e.g. base::RandBytes() or ', |
| 922 | 'base::RandomBitGenerator.'), |
| 923 | True, |
| 924 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 925 | ), |
| 926 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 927 | pattern=r'/\babsl::(optional|nullopt|make_optional)\b', |
Peter Kasting | 3b77a0c | 2024-08-22 00:22:26 | [diff] [blame] | 928 | explanation=('absl::optional is banned. Use std::optional instead.', ), |
| 929 | treat_as_error=True, |
| 930 | excluded_paths=[ |
| 931 | _THIRD_PARTY_EXCEPT_BLINK, |
| 932 | ]), |
| 933 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 934 | r'/(\babsl::Span\b|#include <span>|\bstd::span\b)', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 935 | ('absl::Span and std::span are banned. Use base::span instead.', ), |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 936 | True, |
| 937 | [ |
| 938 | # Included for conversions between base and std. |
| 939 | r'base/containers/span.h', |
| 940 | # Test base::span<> compatibility against std::span<>. |
| 941 | r'base/containers/span_unittest.cc', |
| 942 | # //base/numerics can't use base or absl. So it uses std. |
| 943 | r'base/numerics/.*' |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 944 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 945 | # Needed to use QUICHE API. |
Ciara McMullin | c029c8e | 2024-08-21 14:22:32 | [diff] [blame] | 946 | r'android_webview/browser/ip_protection/.*', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 947 | r'chrome/browser/ip_protection/.*', |
| 948 | r'components/ip_protection/.*', |
| 949 | r'net/third_party/quiche/overrides/quiche_platform_impl/quiche_stack_trace_impl\.*', |
| 950 | r'services/network/web_transport\.cc', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 951 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 952 | # Not an error in third_party folders. |
| 953 | _THIRD_PARTY_EXCEPT_BLINK, |
| 954 | ], |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 955 | ), |
| 956 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 957 | r'/\babsl::StatusOr\b', |
| 958 | ('absl::StatusOr is banned. Use base::expected instead.', ), |
| 959 | True, |
| 960 | [ |
| 961 | # Needed to use liburlpattern API. |
| 962 | r'components/url_pattern/.*', |
| 963 | r'services/network/shared_dictionary/simple_url_pattern_matcher\.cc', |
| 964 | r'third_party/blink/renderer/core/url_pattern/.*', |
| 965 | r'third_party/blink/renderer/modules/manifest/manifest_parser\.cc', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 966 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 967 | # Needed to use QUICHE API. |
Ciara McMullin | c029c8e | 2024-08-21 14:22:32 | [diff] [blame] | 968 | r'android_webview/browser/ip_protection/.*', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 969 | r'chrome/browser/ip_protection/.*', |
| 970 | r'components/ip_protection/.*', |
Victor Vasiliev | 435ec10 | 2025-05-07 08:33:39 | [diff] [blame] | 971 | r'net/quic/dedicated_web_transport_http3_client\.cc', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 972 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 973 | # Needed to use MediaPipe API. |
| 974 | r'components/media_effects/.*\.cc', |
| 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::(StrSplit|StrJoin|StrCat|StrAppend|Substitute|StrContains)\b', |
| 981 | ('Abseil string utilities are banned. Use base/strings instead.', ), |
| 982 | True, |
| 983 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 984 | ), |
| 985 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 986 | r'/\babsl::(Mutex|CondVar|Notification|Barrier|BlockingCounter)\b', |
| 987 | ( |
| 988 | 'Abseil synchronization primitives are banned. Use', |
| 989 | 'base/synchronization instead.', |
| 990 | ), |
| 991 | True, |
| 992 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 993 | ), |
| 994 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 995 | r'/\babsl::(Duration|Time|TimeZone|CivilDay)\b', |
| 996 | ('Abseil\'s time library is banned. Use base/time instead.', ), |
| 997 | True, |
| 998 | [ |
| 999 | # Needed to use QUICHE API. |
Ciara McMullin | c029c8e | 2024-08-21 14:22:32 | [diff] [blame] | 1000 | r'android_webview/browser/ip_protection/.*', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1001 | r'chrome/browser/ip_protection/.*', |
| 1002 | r'components/ip_protection/.*', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 1003 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1004 | # Needed to integrate with //third_party/nearby |
| 1005 | r'components/cross_device/nearby/system_clock.cc', |
| 1006 | _THIRD_PARTY_EXCEPT_BLINK # Not an error in third_party folders. |
| 1007 | ], |
| 1008 | ), |
| 1009 | BanRule( |
Victor Hugo Vianna Silva | 6e84e8d4 | 2025-03-19 00:32:00 | [diff] [blame] | 1010 | r'/absl::(bad_variant_access|get|holds_alternative|monostate|variant|' |
| 1011 | r'visit)', |
| 1012 | ('Abseil\'s variant library is banned, use std.', ), |
Victor Hugo Vianna Silva | d9139fbe | 2025-03-19 20:59:08 | [diff] [blame] | 1013 | True, |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1014 | [_THIRD_PARTY_EXCEPT_BLINK], |
Victor Hugo Vianna Silva | 6e84e8d4 | 2025-03-19 00:32:00 | [diff] [blame] | 1015 | ), |
| 1016 | BanRule( |
| 1017 | r'/absl::(apply|exchange|forward|in_place|index_sequence|' |
| 1018 | r'integer_sequence|make_from_tuple|make_index_sequence|' |
| 1019 | r'make_integer_sequence|move)', |
| 1020 | ('Abseil\'s util library is banned, use std.', ), |
Victor Hugo Vianna Silva | d9139fbe | 2025-03-19 20:59:08 | [diff] [blame] | 1021 | True, |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1022 | [_THIRD_PARTY_EXCEPT_BLINK], |
Victor Hugo Vianna Silva | 6e84e8d4 | 2025-03-19 00:32:00 | [diff] [blame] | 1023 | ), |
| 1024 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1025 | r'/#include <chrono>', |
| 1026 | ('<chrono> is banned. Use base/time instead.', ), |
| 1027 | True, |
| 1028 | [ |
| 1029 | # Not an error in third_party folders: |
| 1030 | _THIRD_PARTY_EXCEPT_BLINK, |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1031 | # This uses openscreen API depending on std::chrono. |
| 1032 | "components/openscreen_platform/task_runner.cc", |
| 1033 | ]), |
| 1034 | BanRule( |
| 1035 | r'/#include <exception>', |
| 1036 | ('Exceptions are banned and disabled in Chromium.', ), |
| 1037 | True, |
| 1038 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
| 1039 | ), |
| 1040 | BanRule( |
| 1041 | r'/\bstd::function\b', |
| 1042 | ('std::function is banned. Use base::{Once,Repeating}Callback instead.', |
| 1043 | ), |
| 1044 | True, |
| 1045 | [ |
| 1046 | # Has tests that template trait helpers don't unintentionally match |
| 1047 | # std::function. |
| 1048 | r'base/functional/callback_helpers_unittest\.cc', |
| 1049 | # Required to implement interfaces from the third-party perfetto |
| 1050 | # library. |
| 1051 | r'base/tracing/perfetto_task_runner\.cc', |
| 1052 | r'base/tracing/perfetto_task_runner\.h', |
| 1053 | # Needed for interop with the third-party nearby library type |
| 1054 | # location::nearby::connections::ResultCallback. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1055 | r'chrome/services/sharing/nearby/nearby_connections_conversions\.cc' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1056 | # Needed for interop with the internal libassistant library. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1057 | r'chromeos/ash/services/libassistant/callback_utils\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1058 | # Needed for interop with Fuchsia fidl APIs. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1059 | r'fuchsia_web/webengine/browser/context_impl_browsertest\.cc', |
| 1060 | r'fuchsia_web/webengine/browser/cookie_manager_impl_unittest\.cc', |
| 1061 | r'fuchsia_web/webengine/browser/media_player_impl_unittest\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1062 | # Required to interop with interfaces from the third-party ChromeML |
| 1063 | # library API. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1064 | r'services/on_device_model/ml/chrome_ml_api\.h', |
| 1065 | r'services/on_device_model/ml/on_device_model_executor\.cc', |
| 1066 | r'services/on_device_model/ml/on_device_model_executor\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1067 | # Required to interop with interfaces from the third-party perfetto |
| 1068 | # library. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1069 | r'components/tracing/common/etw_consumer_win_unittest\.cc', |
| 1070 | r'services/tracing/public/cpp/perfetto/custom_event_recorder\.cc', |
| 1071 | r'services/tracing/public/cpp/perfetto/perfetto_traced_process\.cc', |
| 1072 | r'services/tracing/public/cpp/perfetto/perfetto_traced_process\.h', |
| 1073 | r'services/tracing/public/cpp/perfetto/perfetto_tracing_backend\.cc', |
| 1074 | r'services/tracing/public/cpp/perfetto/producer_client\.cc', |
| 1075 | r'services/tracing/public/cpp/perfetto/producer_client\.h', |
| 1076 | r'services/tracing/public/cpp/perfetto/producer_test_utils\.cc', |
| 1077 | r'services/tracing/public/cpp/perfetto/producer_test_utils\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1078 | # Required for interop with the third-party webrtc library. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1079 | r'third_party/blink/renderer/modules/peerconnection/mock_peer_connection_impl\.cc', |
| 1080 | r'third_party/blink/renderer/modules/peerconnection/mock_peer_connection_impl\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1081 | # TODO(https://crbug.com/1364577): Various uses that should be |
| 1082 | # migrated to something else. |
| 1083 | # Should use base::OnceCallback or base::RepeatingCallback. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1084 | r'base/allocator/dispatcher/initializer_unittest\.cc', |
| 1085 | r'chrome/browser/ash/accessibility/speech_monitor\.cc', |
| 1086 | r'chrome/browser/ash/accessibility/speech_monitor\.h', |
| 1087 | r'chrome/browser/ash/login/ash_hud_login_browsertest\.cc', |
| 1088 | r'chromecast/base/observer_unittest\.cc', |
| 1089 | r'chromecast/browser/cast_web_view\.h', |
| 1090 | r'chromecast/public/cast_media_shlib\.h', |
| 1091 | r'device/bluetooth/floss/exported_callback_manager\.h', |
| 1092 | r'device/bluetooth/floss/floss_dbus_client\.h', |
| 1093 | r'device/fido/cable/v2_handshake_unittest\.cc', |
| 1094 | r'device/fido/pin\.cc', |
| 1095 | r'services/tracing/perfetto/test_utils\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1096 | # Should use base::FunctionRef. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1097 | r'chrome/browser/media/webrtc/test_stats_dictionary\.cc', |
| 1098 | r'chrome/browser/media/webrtc/test_stats_dictionary\.h', |
| 1099 | r'chromeos/ash/services/libassistant/device_settings_controller\.cc', |
| 1100 | r'components/browser_ui/client_certificate/android/ssl_client_certificate_request\.cc', |
| 1101 | r'components/gwp_asan/client/sampling_malloc_shims_unittest\.cc', |
| 1102 | r'content/browser/font_unique_name_lookup/font_unique_name_lookup_unittest\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1103 | # Does not need std::function at all. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1104 | r'components/omnibox/browser/autocomplete_result\.cc', |
| 1105 | r'device/fido/win/webauthn_api\.cc', |
| 1106 | r'media/audio/alsa/alsa_util\.cc', |
| 1107 | r'media/remoting/stream_provider\.h', |
| 1108 | r'sql/vfs_wrapper\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1109 | # TODO(https://crbug.com/1364585): Remove usage and exception list |
| 1110 | # entries. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1111 | r'extensions/renderer/api/automation/automation_internal_custom_bindings\.cc', |
| 1112 | r'extensions/renderer/api/automation/automation_internal_custom_bindings\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1113 | # TODO(https://crbug.com/1364579): Remove usage and exception list |
| 1114 | # entry. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1115 | r'ui/views/controls/focus_ring\.h', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 1116 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1117 | # Various pre-existing uses in //tools that is low-priority to fix. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1118 | r'tools/binary_size/libsupersize/viewer/caspian/diff\.cc', |
| 1119 | r'tools/binary_size/libsupersize/viewer/caspian/model\.cc', |
| 1120 | r'tools/binary_size/libsupersize/viewer/caspian/model\.h', |
| 1121 | r'tools/binary_size/libsupersize/viewer/caspian/tree_builder\.h', |
| 1122 | r'tools/clang/base_bind_rewriters/BaseBindRewriters\.cpp', |
Daniel Cheng | e5583e3c | 2022-09-22 00:19:41 | [diff] [blame] | 1123 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1124 | # Not an error in third_party folders. |
| 1125 | _THIRD_PARTY_EXCEPT_BLINK |
| 1126 | ], |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 1127 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1128 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1129 | r'/#include <X11/', |
| 1130 | ('Do not use Xlib. Use xproto (from //ui/gfx/x:xproto) instead.', ), |
| 1131 | True, |
| 1132 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Tom Anderson | a95e1204 | 2020-09-09 23:08:00 | [diff] [blame] | 1133 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1134 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1135 | r'/\bstd::ratio\b', |
| 1136 | ('std::ratio is banned by the Google Style Guide.', ), |
| 1137 | True, |
| 1138 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Daniel Bratell | 69334cc | 2019-03-26 11:07:45 | [diff] [blame] | 1139 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1140 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1141 | r'/\bstd::aligned_alloc\b', |
| 1142 | ( |
| 1143 | 'std::aligned_alloc() is not yet allowed (crbug.com/1412818). Use ', |
| 1144 | 'base::AlignedAlloc() instead.', |
| 1145 | ), |
| 1146 | True, |
| 1147 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1148 | ), |
| 1149 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1150 | r'/#include <(barrier|latch|semaphore|stop_token)>', |
| 1151 | ('The thread support library is banned. Use base/synchronization ' |
| 1152 | 'instead.', ), |
| 1153 | True, |
| 1154 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1155 | ), |
| 1156 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1157 | r'/\bstd::execution::(par|seq)\b', |
| 1158 | ('std::execution::(par|seq) is banned; they do not fit into ' |
| 1159 | ' Chrome\'s threading model, and libc++ doesn\'t have full ' |
mikt | 19226ff2 | 2024-08-27 05:28:21 | [diff] [blame] | 1160 | 'support.', ), |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1161 | True, |
| 1162 | [_THIRD_PARTY_EXCEPT_BLINK], |
Helmut Januschka | 7cc8a84f | 2024-02-07 22:50:41 | [diff] [blame] | 1163 | ), |
| 1164 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1165 | r'/\bstd::bit_cast\b', |
| 1166 | ('std::bit_cast is banned; use base::bit_cast instead for values and ' |
| 1167 | 'standard C++ casting when pointers are involved.', ), |
| 1168 | True, |
| 1169 | [ |
| 1170 | # Don't warn in third_party folders. |
| 1171 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1172 | # //base/numerics can't use base or absl. |
| 1173 | r'base/numerics/.*' |
| 1174 | ], |
Avi Drissman | 70cb7f7 | 2023-12-12 17:44:37 | [diff] [blame] | 1175 | ), |
| 1176 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1177 | r'/\bstd::(c8rtomb|mbrtoc8)\b', |
| 1178 | ('std::c8rtomb() and std::mbrtoc8() are banned.', ), |
| 1179 | True, |
| 1180 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1181 | ), |
| 1182 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1183 | r'/\bchar8_t|std::u8string\b', |
| 1184 | ( |
| 1185 | 'char8_t and std::u8string are not yet allowed. Can you use [unsigned]', |
| 1186 | ' char and std::string instead?', |
| 1187 | ), |
| 1188 | True, |
| 1189 | [ |
| 1190 | # The demangler does not use this type but needs to know about it. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1191 | r'base/third_party/symbolize/demangle\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1192 | # Don't warn in third_party folders. |
| 1193 | _THIRD_PARTY_EXCEPT_BLINK |
| 1194 | ], |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1195 | ), |
| 1196 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1197 | r'/(\b(co_await|co_return|co_yield)\b|#include <coroutine>)', |
| 1198 | ('Coroutines are not yet allowed (https://crbug.com/1403840).', ), |
| 1199 | True, |
| 1200 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1201 | ), |
| 1202 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1203 | r'/^\s*(export\s|import\s+["<:\w]|module(;|\s+[:\w]))', |
| 1204 | ('Modules are disallowed for now due to lack of toolchain support.', ), |
| 1205 | True, |
| 1206 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 69357dc | 2023-03-14 01:34:29 | [diff] [blame] | 1207 | ), |
| 1208 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1209 | r'/\[\[(\w*::)?no_unique_address\]\]', |
| 1210 | ( |
| 1211 | '[[no_unique_address]] does not work as expected on Windows ', |
| 1212 | '(https://crbug.com/1414621). Use NO_UNIQUE_ADDRESS instead.', |
| 1213 | ), |
| 1214 | True, |
| 1215 | [ |
| 1216 | # NO_UNIQUE_ADDRESS / PA_NO_UNIQUE_ADDRESS provide canonical access. |
| 1217 | r'^base/compiler_specific\.h', |
| 1218 | r'^base/allocator/partition_allocator/src/partition_alloc/partition_alloc_base/compiler_specific\.h', |
| 1219 | # Not an error in third_party folders. |
| 1220 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1221 | ], |
Peter Kasting | 8bc046d2 | 2023-11-14 00:38:03 | [diff] [blame] | 1222 | ), |
| 1223 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1224 | r'/#include <format>', |
| 1225 | ('<format> is not yet allowed. Use base::StringPrintf() instead.', ), |
| 1226 | True, |
| 1227 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1228 | ), |
| 1229 | BanRule( |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1230 | pattern='std::views', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1231 | explanation=('Use of std::views is banned in Chrome. If you need this ' |
| 1232 | 'functionality, please contact [email protected].', ), |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1233 | treat_as_error=True, |
| 1234 | excluded_paths=[ |
| 1235 | # Don't warn in third_party folders. |
| 1236 | _THIRD_PARTY_EXCEPT_BLINK |
| 1237 | ], |
| 1238 | ), |
| 1239 | BanRule( |
| 1240 | # Ban everything except specifically allowlisted constructs. |
Daniel Cheng | 70a3527 | 2025-05-06 16:41:34 | [diff] [blame] | 1241 | pattern=r'/std::ranges::(?!(?:' + '|'.join(( |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1242 | # From https://en.cppreference.com/w/cpp/ranges: |
| 1243 | # Range access |
| 1244 | 'begin', |
| 1245 | 'end', |
| 1246 | 'cbegin', |
| 1247 | 'cend', |
| 1248 | 'rbegin', |
| 1249 | 'rend', |
| 1250 | 'crbegin', |
| 1251 | 'crend', |
| 1252 | 'size', |
| 1253 | 'ssize', |
| 1254 | 'empty', |
| 1255 | 'data', |
| 1256 | 'cdata', |
| 1257 | # Range primitives |
| 1258 | 'iterator_t', |
| 1259 | 'const_iterator_t', |
| 1260 | 'sentinel_t', |
| 1261 | 'const_sentinel_t', |
| 1262 | 'range_difference_t', |
| 1263 | 'range_size_t', |
| 1264 | 'range_value_t', |
| 1265 | 'range_reference_t', |
| 1266 | 'range_const_reference_t', |
| 1267 | 'range_rvalue_reference_t', |
| 1268 | 'range_common_reference_t', |
| 1269 | # Dangling iterator handling |
| 1270 | 'dangling', |
| 1271 | 'borrowed_iterator_t', |
| 1272 | # Banned: borrowed_subrange_t |
| 1273 | # Range concepts |
| 1274 | 'range', |
| 1275 | 'borrowed_range', |
| 1276 | 'sized_range', |
| 1277 | 'view', |
| 1278 | 'input_range', |
| 1279 | 'output_range', |
| 1280 | 'forward_range', |
| 1281 | 'bidirectional_range', |
| 1282 | 'random_access_range', |
| 1283 | 'contiguous_range', |
| 1284 | 'common_range', |
| 1285 | 'viewable_range', |
| 1286 | 'constant_range', |
Michael Tang | 626d898 | 2025-05-08 23:24:29 | [diff] [blame] | 1287 | # Views |
| 1288 | 'subrange', |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1289 | # Banned: Range factories |
| 1290 | # Banned: Range adaptors |
Peter Kasting | a7f9375 | 2024-10-24 22:15:40 | [diff] [blame] | 1291 | # Incidentally listed on |
| 1292 | # https://en.cppreference.com/w/cpp/header/ranges: |
| 1293 | 'enable_borrowed_range', |
| 1294 | 'enable_view', |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1295 | # From https://en.cppreference.com/w/cpp/algorithm/ranges: |
| 1296 | # Constrained algorithms: non-modifying sequence operations |
| 1297 | 'all_of', |
| 1298 | 'any_of', |
| 1299 | 'none_of', |
| 1300 | 'for_each', |
| 1301 | 'for_each_n', |
| 1302 | 'count', |
| 1303 | 'count_if', |
| 1304 | 'mismatch', |
| 1305 | 'equal', |
| 1306 | 'lexicographical_compare', |
| 1307 | 'find', |
| 1308 | 'find_if', |
| 1309 | 'find_if_not', |
| 1310 | 'find_end', |
| 1311 | 'find_first_of', |
| 1312 | 'adjacent_find', |
| 1313 | 'search', |
| 1314 | 'search_n', |
| 1315 | # Constrained algorithms: modifying sequence operations |
| 1316 | 'copy', |
| 1317 | 'copy_if', |
| 1318 | 'copy_n', |
| 1319 | 'copy_backward', |
| 1320 | 'move', |
| 1321 | 'move_backward', |
| 1322 | 'fill', |
| 1323 | 'fill_n', |
| 1324 | 'transform', |
| 1325 | 'generate', |
| 1326 | 'generate_n', |
| 1327 | 'remove', |
| 1328 | 'remove_if', |
| 1329 | 'remove_copy', |
| 1330 | 'remove_copy_if', |
| 1331 | 'replace', |
| 1332 | 'replace_if', |
| 1333 | 'replace_copy', |
| 1334 | 'replace_copy_if', |
| 1335 | 'swap_ranges', |
| 1336 | 'reverse', |
| 1337 | 'reverse_copy', |
| 1338 | 'rotate', |
| 1339 | 'rotate_copy', |
| 1340 | 'shuffle', |
| 1341 | 'sample', |
| 1342 | 'unique', |
| 1343 | 'unique_copy', |
| 1344 | # Constrained algorithms: partitioning operations |
| 1345 | 'is_partitioned', |
| 1346 | 'partition', |
| 1347 | 'partition_copy', |
| 1348 | 'stable_partition', |
| 1349 | 'partition_point', |
| 1350 | # Constrained algorithms: sorting operations |
| 1351 | 'is_sorted', |
| 1352 | 'is_sorted_until', |
| 1353 | 'sort', |
| 1354 | 'partial_sort', |
| 1355 | 'partial_sort_copy', |
| 1356 | 'stable_sort', |
| 1357 | 'nth_element', |
| 1358 | # Constrained algorithms: binary search operations (on sorted ranges) |
| 1359 | 'lower_bound', |
| 1360 | 'upper_bound', |
| 1361 | 'binary_search', |
| 1362 | 'equal_range', |
| 1363 | # Constrained algorithms: set operations (on sorted ranges) |
| 1364 | 'merge', |
| 1365 | 'inplace_merge', |
| 1366 | 'includes', |
| 1367 | 'set_difference', |
| 1368 | 'set_intersection', |
| 1369 | 'set_symmetric_difference', |
| 1370 | 'set_union', |
| 1371 | # Constrained algorithms: heap operations |
| 1372 | 'is_heap', |
| 1373 | 'is_heap_until', |
| 1374 | 'make_heap', |
| 1375 | 'push_heap', |
| 1376 | 'pop_heap', |
| 1377 | 'sort_heap', |
| 1378 | # Constrained algorithms: minimum/maximum operations |
| 1379 | 'max', |
| 1380 | 'max_element', |
| 1381 | 'min', |
| 1382 | 'min_element', |
| 1383 | 'minmax', |
| 1384 | 'minmax_element', |
| 1385 | 'clamp', |
| 1386 | # Constrained algorithms: permutation operations |
| 1387 | 'is_permutation', |
| 1388 | 'next_permutation', |
| 1389 | 'prev_premutation', |
| 1390 | # Constrained uninitialized memory algorithms |
| 1391 | 'uninitialized_copy', |
| 1392 | 'uninitialized_copy_n', |
| 1393 | 'uninitialized_fill', |
| 1394 | 'uninitialized_fill_n', |
| 1395 | 'uninitialized_move', |
| 1396 | 'uninitialized_move_n', |
| 1397 | 'uninitialized_default_construct', |
| 1398 | 'uninitialized_default_construct_n', |
| 1399 | 'uninitialized_value_construct', |
| 1400 | 'uninitialized_value_construct_n', |
| 1401 | 'destroy', |
| 1402 | 'destroy_n', |
| 1403 | 'destroy_at', |
| 1404 | 'construct_at', |
| 1405 | # Return types |
| 1406 | 'in_fun_result', |
| 1407 | 'in_in_result', |
| 1408 | 'in_out_result', |
| 1409 | 'in_in_out_result', |
| 1410 | 'in_out_out_result', |
| 1411 | 'min_max_result', |
| 1412 | 'in_found_result', |
Peter Kasting | f379c02 | 2025-01-13 14:01:00 | [diff] [blame] | 1413 | # From https://en.cppreference.com/w/cpp/header/functional |
| 1414 | 'equal_to', |
| 1415 | 'not_equal_to', |
| 1416 | 'greater', |
| 1417 | 'less', |
| 1418 | 'greater_equal', |
| 1419 | 'less_equal', |
danakj | 91c715b | 2024-07-10 13:24:26 | [diff] [blame] | 1420 | # From https://en.cppreference.com/w/cpp/iterator |
| 1421 | 'advance', |
| 1422 | 'distance', |
| 1423 | 'next', |
| 1424 | 'prev', |
Daniel Cheng | 70a3527 | 2025-05-06 16:41:34 | [diff] [blame] | 1425 | # Require a word boundary at the end of negative lookahead |
| 1426 | # assertion, e.g. to ensure that even though `view` is allowed (and |
| 1427 | # should not match this regex), `views` is still treated as |
| 1428 | # disallowed (and matches the regex). |
| 1429 | )) + r')\b)\w+', |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1430 | explanation=( |
| 1431 | 'Use of range views and associated helpers is banned in Chrome. ' |
| 1432 | 'If you need this functionality, please contact [email protected].', |
| 1433 | ), |
| 1434 | treat_as_error=True, |
| 1435 | excluded_paths=[ |
| 1436 | # Don't warn in third_party folders. |
| 1437 | _THIRD_PARTY_EXCEPT_BLINK |
| 1438 | ], |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1439 | ), |
| 1440 | BanRule( |
Peter Kasting | 31879d8 | 2024-10-07 20:18:39 | [diff] [blame] | 1441 | r'/#include <regex>', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1442 | ('<regex> is not allowed. Use third_party/re2 instead.', ), |
Peter Kasting | 31879d8 | 2024-10-07 20:18:39 | [diff] [blame] | 1443 | True, |
| 1444 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
| 1445 | ), |
| 1446 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1447 | r'/#include <source_location>', |
| 1448 | ('<source_location> is not yet allowed. Use base/location.h instead.', |
| 1449 | ), |
| 1450 | True, |
| 1451 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1452 | ), |
| 1453 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1454 | r'/\bstd::to_address\b', |
| 1455 | ( |
| 1456 | 'std::to_address is banned because it is not guaranteed to be', |
| 1457 | 'SFINAE-compatible. Use base::to_address from base/types/to_address.h', |
| 1458 | 'instead.', |
| 1459 | ), |
| 1460 | True, |
| 1461 | [ |
| 1462 | # Needed in base::to_address implementation. |
| 1463 | r'base/types/to_address.h', |
| 1464 | _THIRD_PARTY_EXCEPT_BLINK |
| 1465 | ], # Not an error in third_party folders. |
Nick Diego Yamane | e522ae8 | 2024-02-27 04:23:22 | [diff] [blame] | 1466 | ), |
| 1467 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1468 | r'/#include <syncstream>', |
| 1469 | ('<syncstream> is banned.', ), |
| 1470 | True, |
| 1471 | [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1472 | ), |
| 1473 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1474 | r'/\bRunMessageLoop\b', |
| 1475 | ('RunMessageLoop is deprecated, use RunLoop instead.', ), |
| 1476 | False, |
| 1477 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1478 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1479 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1480 | 'RunAllPendingInMessageLoop()', |
| 1481 | ( |
| 1482 | "Prefer RunLoop over RunAllPendingInMessageLoop, please contact gab@", |
| 1483 | "if you're convinced you need this.", |
| 1484 | ), |
| 1485 | False, |
| 1486 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1487 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1488 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1489 | 'RunAllPendingInMessageLoop(BrowserThread', |
| 1490 | ( |
| 1491 | 'RunAllPendingInMessageLoop is deprecated. Use RunLoop for', |
| 1492 | 'BrowserThread::UI, BrowserTaskEnvironment::RunIOThreadUntilIdle', |
| 1493 | 'for BrowserThread::IO, and prefer RunLoop::QuitClosure to observe', |
| 1494 | 'async events instead of flushing threads.', |
| 1495 | ), |
| 1496 | False, |
| 1497 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1498 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1499 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1500 | r'MessageLoopRunner', |
| 1501 | ('MessageLoopRunner is deprecated, use RunLoop instead.', ), |
| 1502 | False, |
| 1503 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1504 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1505 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1506 | 'GetDeferredQuitTaskForRunLoop', |
| 1507 | ( |
| 1508 | "GetDeferredQuitTaskForRunLoop shouldn't be needed, please contact", |
| 1509 | "gab@ if you found a use case where this is the only solution.", |
| 1510 | ), |
| 1511 | False, |
| 1512 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1513 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1514 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1515 | 'sqlite3_initialize(', |
| 1516 | ( |
| 1517 | 'Instead of calling sqlite3_initialize(), depend on //sql, ', |
| 1518 | '#include "sql/initialize.h" and use sql::EnsureSqliteInitialized().', |
| 1519 | ), |
| 1520 | True, |
| 1521 | ( |
| 1522 | r'^sql/initialization\.(cc|h)$', |
| 1523 | r'^third_party/sqlite/.*\.(c|cc|h)$', |
| 1524 | ), |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 1525 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1526 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1527 | 'CREATE VIEW', |
| 1528 | ( |
| 1529 | 'SQL views are disabled in Chromium feature code', |
| 1530 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/sql#no-views', |
| 1531 | ), |
| 1532 | True, |
| 1533 | ( |
| 1534 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1535 | # sql/ itself uses views when using memory-mapped IO. |
| 1536 | r'^sql/.*', |
| 1537 | # Various performance tools that do not build as part of Chrome. |
| 1538 | r'^infra/.*', |
| 1539 | r'^tools/perf.*', |
| 1540 | r'.*perfetto.*', |
| 1541 | ), |
Austin Sullivan | d661ab5 | 2022-11-16 08:55:15 | [diff] [blame] | 1542 | ), |
| 1543 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1544 | 'CREATE VIRTUAL TABLE', |
| 1545 | ( |
| 1546 | 'SQL virtual tables are disabled in Chromium feature code', |
| 1547 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/sql#no-virtual-tables', |
| 1548 | ), |
| 1549 | True, |
| 1550 | ( |
| 1551 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1552 | # sql/ itself uses virtual tables in the recovery module and tests. |
| 1553 | r'^sql/.*', |
| 1554 | # TODO(https://crbug.com/695592): Remove once WebSQL is deprecated. |
| 1555 | r'third_party/blink/web_tests/storage/websql/.*' |
| 1556 | # Various performance tools that do not build as part of Chrome. |
| 1557 | r'^tools/perf.*', |
| 1558 | r'.*perfetto.*', |
| 1559 | ), |
Austin Sullivan | d661ab5 | 2022-11-16 08:55:15 | [diff] [blame] | 1560 | ), |
| 1561 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1562 | 'std::random_shuffle', |
| 1563 | ('std::random_shuffle is deprecated in C++14, and removed in C++17. Use', |
| 1564 | 'base::RandomShuffle instead.'), |
| 1565 | True, |
| 1566 | (), |
tzik | 5de2157f | 2018-05-08 03:42:47 | [diff] [blame] | 1567 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1568 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1569 | 'ios/web/public/test/http_server', |
| 1570 | ('web::HTTPserver is deprecated use net::EmbeddedTestServer instead.', |
| 1571 | ), |
| 1572 | False, |
| 1573 | (), |
Javier Ernesto Flores Robles | 749e6c2 | 2018-10-08 09:36:24 | [diff] [blame] | 1574 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1575 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1576 | 'GetAddressOf', |
| 1577 | ('Improper use of Microsoft::WRL::ComPtr<T>::GetAddressOf() has been ', |
| 1578 | 'implicated in a few leaks. ReleaseAndGetAddressOf() is safe but ', |
| 1579 | 'operator& is generally recommended. So always use operator& instead. ', |
| 1580 | 'See http://crbug.com/914910 for more conversion guidance.'), |
| 1581 | True, |
| 1582 | (), |
Robert Liao | 764c949 | 2019-01-24 18:46:28 | [diff] [blame] | 1583 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1584 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1585 | 'SHFileOperation', |
| 1586 | ('SHFileOperation was deprecated in Windows Vista, and there are less ', |
| 1587 | 'complex functions to achieve the same goals. Use IFileOperation for ', |
| 1588 | 'any esoteric actions instead.'), |
| 1589 | True, |
| 1590 | (), |
Ben Lewis | a951460 | 2019-04-29 17:53:05 | [diff] [blame] | 1591 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1592 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1593 | 'StringFromGUID2', |
| 1594 | ('StringFromGUID2 introduces an unnecessary dependency on ole32.dll.', |
| 1595 | 'Use base::win::WStringFromGUID instead.'), |
| 1596 | True, |
| 1597 | (r'/base/win/win_util_unittest.cc', ), |
Cliff Smolinsky | 8195164 | 2019-04-30 21:39:51 | [diff] [blame] | 1598 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1599 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1600 | 'StringFromCLSID', |
| 1601 | ('StringFromCLSID introduces an unnecessary dependency on ole32.dll.', |
| 1602 | 'Use base::win::WStringFromGUID instead.'), |
| 1603 | True, |
| 1604 | (r'/base/win/win_util_unittest.cc', ), |
Cliff Smolinsky | 8195164 | 2019-04-30 21:39:51 | [diff] [blame] | 1605 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1606 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1607 | 'kCFAllocatorNull', |
| 1608 | ( |
| 1609 | 'The use of kCFAllocatorNull with the NoCopy creation of ', |
| 1610 | 'CoreFoundation types is prohibited.', |
| 1611 | ), |
| 1612 | True, |
| 1613 | (), |
Avi Drissman | 7382afa0 | 2019-04-29 23:27:13 | [diff] [blame] | 1614 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1615 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1616 | 'mojo::ConvertTo', |
| 1617 | ('mojo::ConvertTo and TypeConverter are deprecated. Please consider', |
| 1618 | 'StructTraits / UnionTraits / EnumTraits / ArrayTraits / MapTraits /', |
| 1619 | 'StringTraits if you would like to convert between custom types and', |
| 1620 | 'the wire format of mojom types.'), |
| 1621 | False, |
| 1622 | ( |
| 1623 | r'^fuchsia_web/webengine/browser/url_request_rewrite_rules_manager\.cc$', |
| 1624 | r'^fuchsia_web/webengine/url_request_rewrite_type_converters\.cc$', |
| 1625 | r'^third_party/blink/.*\.(cc|h)$', |
| 1626 | r'^content/renderer/.*\.(cc|h)$', |
| 1627 | ), |
Oksana Zhuravlova | fd24777 | 2019-05-16 16:57:29 | [diff] [blame] | 1628 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1629 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1630 | 'GetInterfaceProvider', |
| 1631 | ('InterfaceProvider is deprecated.', |
| 1632 | 'Please use ExecutionContext::GetBrowserInterfaceBroker and overrides', |
| 1633 | 'or Platform::GetBrowserInterfaceBroker.'), |
| 1634 | False, |
| 1635 | (), |
Oksana Zhuravlova | c8222d2 | 2019-12-19 19:21:16 | [diff] [blame] | 1636 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1637 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1638 | 'CComPtr', |
| 1639 | ('New code should use Microsoft::WRL::ComPtr from wrl/client.h as a ', |
| 1640 | 'replacement for CComPtr from ATL. See http://crbug.com/5027 for more ', |
| 1641 | 'details.'), |
| 1642 | False, |
| 1643 | (), |
Robert Liao | 1d78df5 | 2019-11-11 20:02:01 | [diff] [blame] | 1644 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1645 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1646 | r'/\b(IFACE|STD)METHOD_?\(', |
| 1647 | ('IFACEMETHOD() and STDMETHOD() make code harder to format and read.', |
| 1648 | 'Instead, always use IFACEMETHODIMP in the declaration.'), |
| 1649 | False, |
| 1650 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Xiaohan Wang | 72bd2ba | 2020-02-18 21:38:20 | [diff] [blame] | 1651 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1652 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1653 | 'RemoveAllChildViewsWithoutDeleting', |
| 1654 | ('RemoveAllChildViewsWithoutDeleting is deprecated.', |
| 1655 | 'This method is deemed dangerous as, unless raw pointers are re-added,', |
| 1656 | 'calls to this method introduce memory leaks.'), |
| 1657 | False, |
| 1658 | (), |
Peter Boström | 7ff4152 | 2021-07-29 03:43:27 | [diff] [blame] | 1659 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1660 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1661 | r'/\bTRACE_EVENT_ASYNC_', |
| 1662 | ( |
| 1663 | 'Please use TRACE_EVENT_NESTABLE_ASYNC_.. macros instead', |
| 1664 | 'of TRACE_EVENT_ASYNC_.. (crbug.com/1038710).', |
| 1665 | ), |
| 1666 | False, |
| 1667 | ( |
| 1668 | r'^base/trace_event/.*', |
| 1669 | r'^base/tracing/.*', |
| 1670 | ), |
Eric Seckler | be6f48d | 2020-05-06 18:09:12 | [diff] [blame] | 1671 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1672 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1673 | 'RoInitialize', |
| 1674 | ('Improper use of [base::win]::RoInitialize() has been implicated in a ', |
| 1675 | 'few COM initialization leaks. Use base::win::ScopedWinrtInitializer ', |
| 1676 | 'instead. See http://crbug.com/1197722 for more information.'), |
| 1677 | True, |
| 1678 | ( |
| 1679 | r'^base/win/scoped_winrt_initializer\.cc$', |
| 1680 | r'^third_party/abseil-cpp/absl/.*', |
| 1681 | ), |
Robert Liao | 22f66a5 | 2021-04-10 00:57:52 | [diff] [blame] | 1682 | ), |
Patrick Monette | c343bb98 | 2022-06-01 17:18:45 | [diff] [blame] | 1683 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1684 | r'base::Watchdog', |
| 1685 | ( |
| 1686 | 'base::Watchdog is deprecated because it creates its own thread.', |
| 1687 | 'Instead, manually start a timer on a SequencedTaskRunner.', |
| 1688 | ), |
| 1689 | False, |
| 1690 | (), |
Patrick Monette | c343bb98 | 2022-06-01 17:18:45 | [diff] [blame] | 1691 | ), |
Andrew Rayskiy | 04a51ce | 2022-06-07 11:47:09 | [diff] [blame] | 1692 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1693 | 'base::Passed', |
| 1694 | ('Do not use base::Passed. It is a legacy helper for capturing ', |
| 1695 | 'move-only types with base::BindRepeating, but invoking the ', |
| 1696 | 'resulting RepeatingCallback moves the captured value out of ', |
| 1697 | 'the callback storage, and subsequent invocations may pass the ', |
| 1698 | 'value in a valid but undefined state. Prefer base::BindOnce().', |
| 1699 | 'See http://crbug.com/1326449 for context.'), |
| 1700 | False, |
| 1701 | ( |
| 1702 | # False positive, but it is also fine to let bind internals reference |
| 1703 | # base::Passed. |
| 1704 | r'^base[\\/]functional[\\/]bind\.h', |
| 1705 | r'^base[\\/]functional[\\/]bind_internal\.h', |
| 1706 | ), |
Andrew Rayskiy | 04a51ce | 2022-06-07 11:47:09 | [diff] [blame] | 1707 | ), |
Daniel Cheng | 2248b33 | 2022-07-27 06:16:59 | [diff] [blame] | 1708 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1709 | r'base::Feature k', |
| 1710 | ('Please use BASE_DECLARE_FEATURE() or BASE_FEATURE() instead of ', |
| 1711 | 'directly declaring/defining features.'), |
| 1712 | True, |
| 1713 | [ |
| 1714 | # Implements BASE_DECLARE_FEATURE(). |
| 1715 | r'^base/feature_list\.h', |
| 1716 | ], |
Daniel Cheng | ba3bc2e | 2022-10-03 02:45:43 | [diff] [blame] | 1717 | ), |
Robert Ogden | 92101dcb | 2022-10-19 23:49:36 | [diff] [blame] | 1718 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1719 | r'/\bchartorune\b', |
| 1720 | ('chartorune is not memory-safe, unless you can guarantee the input ', |
| 1721 | 'string is always null-terminated. Otherwise, please use charntorune ', |
| 1722 | 'from libphonenumber instead.'), |
| 1723 | True, |
| 1724 | [ |
| 1725 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1726 | # Exceptions to this rule should have a fuzzer. |
| 1727 | ], |
Robert Ogden | 92101dcb | 2022-10-19 23:49:36 | [diff] [blame] | 1728 | ), |
Arthur Sonzogni | 1da65fa | 2023-03-27 16:01:52 | [diff] [blame] | 1729 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1730 | r'/\b#include "base/atomicops\.h"\b', |
| 1731 | ('Do not use base::subtle atomics, but std::atomic, which are simpler ' |
| 1732 | 'to use, have better understood, clearer and richer semantics, and are ' |
| 1733 | 'harder to mis-use. See details in base/atomicops.h.', ), |
| 1734 | False, |
| 1735 | [_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders. |
Benoit Lize | 79cf059 | 2023-01-27 10:01:57 | [diff] [blame] | 1736 | ), |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1737 | BanRule(r'CrossThreadPersistent<', ( |
Arthur Sonzogni | 60348572e | 2023-04-07 10:22:52 | [diff] [blame] | 1738 | 'Do not use blink::CrossThreadPersistent, but ' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1739 | 'blink::CrossThreadHandle. It is harder to mis-use.', 'More info: ' |
Arthur Sonzogni | 60348572e | 2023-04-07 10:22:52 | [diff] [blame] | 1740 | 'https://docs.google.com/document/d/1GIT0ysdQ84sGhIo1r9EscF_fFt93lmNVM_q4vvHj2FQ/edit#heading=h.3e4d6y61tgs', |
| 1741 | 'Please contact platform-architecture-dev@ before adding new instances.' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1742 | ), False, []), |
| 1743 | BanRule(r'CrossThreadWeakPersistent<', ( |
Arthur Sonzogni | 60348572e | 2023-04-07 10:22:52 | [diff] [blame] | 1744 | 'Do not use blink::CrossThreadWeakPersistent, but ' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1745 | 'blink::CrossThreadWeakHandle. It is harder to mis-use.', 'More info: ' |
Arthur Sonzogni | 60348572e | 2023-04-07 10:22:52 | [diff] [blame] | 1746 | 'https://docs.google.com/document/d/1GIT0ysdQ84sGhIo1r9EscF_fFt93lmNVM_q4vvHj2FQ/edit#heading=h.3e4d6y61tgs', |
| 1747 | 'Please contact platform-architecture-dev@ before adding new instances.' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1748 | ), False, []), |
| 1749 | BanRule(r'objc/objc.h', ( |
Avi Drissman | 491617c | 2023-04-13 17:33:15 | [diff] [blame] | 1750 | 'Do not include <objc/objc.h>. It defines away ARC lifetime ' |
| 1751 | 'annotations, and is thus dangerous.', |
| 1752 | 'Please use the pimpl pattern; search for `ObjCStorage` for examples.', |
| 1753 | 'For further reading on how to safely mix C++ and Obj-C, see', |
| 1754 | '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] | 1755 | ), True, []), |
| 1756 | BanRule( |
| 1757 | r'/#include <filesystem>', |
| 1758 | ('libc++ <filesystem> is banned per the Google C++ styleguide.', ), |
| 1759 | True, |
| 1760 | # This fuzzing framework is a standalone open source project and |
| 1761 | # cannot rely on Chromium base. |
| 1762 | (r'third_party/centipede'), |
Avi Drissman | 491617c | 2023-04-13 17:33:15 | [diff] [blame] | 1763 | ), |
Grace Park | 8d59b54b | 2023-04-26 17:53:35 | [diff] [blame] | 1764 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1765 | r'TopDocument()', |
| 1766 | ('TopDocument() does not work correctly with out-of-process iframes. ' |
| 1767 | 'Please do not introduce new uses.', ), |
| 1768 | True, |
| 1769 | ( |
| 1770 | # TODO(crbug.com/617677): Remove all remaining uses. |
| 1771 | r'^third_party/blink/renderer/core/dom/document\.cc', |
| 1772 | r'^third_party/blink/renderer/core/dom/document\.h', |
| 1773 | r'^third_party/blink/renderer/core/dom/element\.cc', |
| 1774 | r'^third_party/blink/renderer/core/exported/web_disallow_transition_scope_test\.cc', |
| 1775 | r'^third_party/blink/renderer/core/exported/web_document_test\.cc', |
| 1776 | r'^third_party/blink/renderer/core/html/html_anchor_element\.cc', |
| 1777 | r'^third_party/blink/renderer/core/html/html_dialog_element\.cc', |
| 1778 | r'^third_party/blink/renderer/core/html/html_element\.cc', |
| 1779 | r'^third_party/blink/renderer/core/html/html_frame_owner_element\.cc', |
| 1780 | r'^third_party/blink/renderer/core/html/media/video_wake_lock\.cc', |
| 1781 | r'^third_party/blink/renderer/core/loader/anchor_element_interaction_tracker\.cc', |
| 1782 | r'^third_party/blink/renderer/core/page/scrolling/root_scroller_controller\.cc', |
| 1783 | r'^third_party/blink/renderer/core/page/scrolling/top_document_root_scroller_controller\.cc', |
| 1784 | r'^third_party/blink/renderer/core/page/scrolling/top_document_root_scroller_controller\.h', |
| 1785 | r'^third_party/blink/renderer/core/script/classic_pending_script\.cc', |
| 1786 | r'^third_party/blink/renderer/core/script/script_loader\.cc', |
| 1787 | ), |
Grace Park | 8d59b54b | 2023-04-26 17:53:35 | [diff] [blame] | 1788 | ), |
Daniel Cheng | 72153e0 | 2023-05-18 21:18:14 | [diff] [blame] | 1789 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1790 | pattern=r'base::raw_ptr<', |
| 1791 | explanation=('Do not use base::raw_ptr, use raw_ptr.', ), |
| 1792 | treat_as_error=True, |
| 1793 | excluded_paths=( |
| 1794 | '^base/', |
| 1795 | '^tools/', |
| 1796 | ), |
Daniel Cheng | 72153e0 | 2023-05-18 21:18:14 | [diff] [blame] | 1797 | ), |
Arthur Sonzogni | f0eea30 | 2023-08-18 19:20:31 | [diff] [blame] | 1798 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1799 | pattern=r'base:raw_ref<', |
| 1800 | explanation=('Do not use base::raw_ref, use raw_ref.', ), |
| 1801 | treat_as_error=True, |
| 1802 | excluded_paths=( |
| 1803 | '^base/', |
| 1804 | '^tools/', |
| 1805 | ), |
Arthur Sonzogni | f0eea30 | 2023-08-18 19:20:31 | [diff] [blame] | 1806 | ), |
| 1807 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1808 | pattern=r'/raw_ptr<[^;}]*\w{};', |
| 1809 | explanation=( |
| 1810 | 'Do not use {} for raw_ptr initialization, use = nullptr instead.', |
| 1811 | ), |
| 1812 | treat_as_error=True, |
| 1813 | excluded_paths=( |
| 1814 | '^base/', |
| 1815 | '^tools/', |
| 1816 | ), |
Arthur Sonzogni | f0eea30 | 2023-08-18 19:20:31 | [diff] [blame] | 1817 | ), |
Anton Maliev | 6675181 | 2023-08-24 16:28:13 | [diff] [blame] | 1818 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1819 | pattern=r'/#include "base/allocator/.*/raw_' |
| 1820 | r'(ptr|ptr_cast|ptr_exclusion|ref).h"', |
| 1821 | explanation=( |
| 1822 | 'Please include the corresponding facade headers:', |
| 1823 | '- #include "base/memory/raw_ptr.h"', |
| 1824 | '- #include "base/memory/raw_ptr_cast.h"', |
| 1825 | '- #include "base/memory/raw_ptr_exclusion.h"', |
| 1826 | '- #include "base/memory/raw_ref.h"', |
| 1827 | ), |
| 1828 | treat_as_error=True, |
| 1829 | excluded_paths=( |
| 1830 | '^base/', |
| 1831 | '^tools/', |
| 1832 | ), |
Tom Sepez | 41eb158d | 2023-09-12 16:16:22 | [diff] [blame] | 1833 | ), |
| 1834 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1835 | pattern=r'ContentSettingsType::COOKIES', |
| 1836 | explanation= |
| 1837 | ('Do not use ContentSettingsType::COOKIES to check whether cookies are ' |
| 1838 | 'supported in the provided context. Instead rely on the ' |
| 1839 | 'content_settings::CookieSettings API. If you are using ' |
| 1840 | 'ContentSettingsType::COOKIES to check the user preference setting ' |
| 1841 | 'specifically, disregard this warning.', ), |
| 1842 | treat_as_error=False, |
| 1843 | excluded_paths=( |
| 1844 | '^chrome/browser/ui/content_settings/', |
| 1845 | '^components/content_settings/', |
| 1846 | '^services/network/cookie_settings.cc', |
| 1847 | '.*test.cc', |
| 1848 | ), |
Arthur Sonzogni | 48c6aea2 | 2023-09-04 22:25:20 | [diff] [blame] | 1849 | ), |
| 1850 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1851 | pattern=r'ContentSettingsType::TRACKING_PROTECTION', |
| 1852 | explanation= |
| 1853 | ('Do not directly use ContentSettingsType::TRACKING_PROTECTION to check ' |
| 1854 | 'for tracking protection exceptions. Instead rely on the ' |
| 1855 | 'privacy_sandbox::TrackingProtectionSettings API.', ), |
| 1856 | treat_as_error=False, |
| 1857 | excluded_paths=( |
| 1858 | '^chrome/browser/ui/content_settings/', |
| 1859 | '^components/content_settings/', |
| 1860 | '^components/privacy_sandbox/tracking_protection_settings.cc', |
| 1861 | '.*test.cc', |
| 1862 | ), |
Anton Maliev | 6675181 | 2023-08-24 16:28:13 | [diff] [blame] | 1863 | ), |
Tom Anderson | cd52207 | 2023-10-03 00:52:35 | [diff] [blame] | 1864 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1865 | pattern=r'/\bg_signal_connect', |
| 1866 | explanation=('Use ScopedGSignal instead of g_signal_connect*()', ), |
| 1867 | treat_as_error=True, |
| 1868 | excluded_paths=('^ui/base/glib/scoped_gsignal.h', ), |
Michelle Abreo | 6b743782 | 2024-04-26 17:29:04 | [diff] [blame] | 1869 | ), |
| 1870 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1871 | pattern=r'features::kIsolatedWebApps', |
| 1872 | explanation=( |
| 1873 | 'Do not use `features::kIsolatedWebApps` directly to guard Isolated ', |
| 1874 | 'Web App code. ', |
Andrew Rayskiy | 7ae2b5d | 2025-02-28 16:59:38 | [diff] [blame] | 1875 | 'Use `content::AreIsolatedWebAppsEnabled()` in the browser process ' |
| 1876 | 'or check the `kEnableIsolatedWebAppsInRenderer` command line flag ' |
| 1877 | 'in the renderer process.', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1878 | ), |
| 1879 | treat_as_error=True, |
Andrew Rayskiy | 7ae2b5d | 2025-02-28 16:59:38 | [diff] [blame] | 1880 | excluded_paths=_TEST_CODE_EXCLUDED_PATHS + ( |
| 1881 | '^chrome/browser/about_flags.cc', |
| 1882 | '^chrome/browser/component_updater/iwa_key_distribution_component_installer.cc', |
| 1883 | '^chrome/browser/web_applications/isolated_web_apps/chrome_content_browser_client_isolated_web_apps_part.cc', |
| 1884 | '^chrome/browser/ui/startup/bad_flags_prompt.cc', |
| 1885 | '^content/shell/browser/shell_content_browser_client.cc', |
| 1886 | )), |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1887 | BanRule( |
| 1888 | pattern=r'features::kIsolatedWebAppDevMode', |
| 1889 | explanation=( |
| 1890 | 'Do not use `features::kIsolatedWebAppDevMode` directly to guard code ', |
| 1891 | 'related to Isolated Web App Developer Mode. ', |
| 1892 | 'Use `web_app::IsIwaDevModeEnabled()` instead.', |
| 1893 | ), |
| 1894 | treat_as_error=True, |
| 1895 | excluded_paths=_TEST_CODE_EXCLUDED_PATHS + ( |
| 1896 | '^chrome/browser/about_flags.cc', |
| 1897 | '^chrome/browser/web_applications/isolated_web_apps/isolated_web_app_features.cc', |
| 1898 | '^chrome/browser/ui/startup/bad_flags_prompt.cc', |
| 1899 | )), |
| 1900 | BanRule( |
| 1901 | pattern=r'features::kIsolatedWebAppUnmanagedInstall', |
| 1902 | explanation=( |
| 1903 | 'Do not use `features::kIsolatedWebAppUnmanagedInstall` directly to ', |
| 1904 | 'guard code related to unmanaged install flow for Isolated Web Apps. ', |
| 1905 | 'Use `web_app::IsIwaUnmanagedInstallEnabled()` instead.', |
| 1906 | ), |
| 1907 | treat_as_error=True, |
| 1908 | excluded_paths=_TEST_CODE_EXCLUDED_PATHS + ( |
| 1909 | '^chrome/browser/about_flags.cc', |
| 1910 | '^chrome/browser/web_applications/isolated_web_apps/isolated_web_app_features.cc', |
| 1911 | )), |
| 1912 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1913 | pattern='/(CUIAutomation|AccessibleObjectFromWindow)', |
| 1914 | explanation= |
| 1915 | ('Direct usage of UIAutomation or IAccessible2 in client code is ' |
| 1916 | 'discouraged in Chromium, as it is not an assistive technology and ' |
| 1917 | 'should not rely on accessibility APIs directly. These APIs can ' |
| 1918 | 'introduce significant performance overhead. However, if you believe ' |
| 1919 | 'your use case warrants an exception, please discuss it with an ' |
| 1920 | 'accessibility owner before proceeding. For more information on the ' |
| 1921 | 'performance implications, see https://docs.google.com/document/d/1jN4itpCe_bDXF0BhFaYwv4xVLsCWkL9eULdzjmLzkuk/edit#heading=h.pwth3nbwdub0.', |
| 1922 | ), |
| 1923 | treat_as_error=False, |
Andrew Rayskiy | cdd45e73 | 2024-03-20 14:32:39 | [diff] [blame] | 1924 | ), |
| 1925 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1926 | pattern=r'/WIDGET_OWNS_NATIVE_WIDGET|' |
| 1927 | r'NATIVE_WIDGET_OWNS_WIDGET', |
| 1928 | explanation= |
| 1929 | ('WIDGET_OWNS_NATIVE_WIDGET and NATIVE_WIDGET_OWNS_WIDGET are in the ' |
| 1930 | 'process of being deprecated. Consider using the new ' |
| 1931 | 'CLIENT_OWNS_WIDGET ownership model. Eventually, this will be the only ' |
| 1932 | 'available ownership model available and the associated enumeration' |
| 1933 | 'will be removed.', ), |
| 1934 | treat_as_error=False, |
Andrew Rayskiy | cdd45e73 | 2024-03-20 14:32:39 | [diff] [blame] | 1935 | ), |
| 1936 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1937 | pattern='ProfileManager::GetLastUsedProfile', |
| 1938 | explanation= |
| 1939 | ('Most code should already be scoped to a Profile. Pass in a Profile* ' |
| 1940 | 'or retreive from an existing entity with a reference to the Profile ' |
| 1941 | '(e.g. WebContents).', ), |
| 1942 | treat_as_error=False, |
Arthur Sonzogni | 5cbd3e3 | 2024-02-08 17:51:32 | [diff] [blame] | 1943 | ), |
Helmut Januschka | b3f71ab5 | 2024-03-12 02:48:05 | [diff] [blame] | 1944 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1945 | pattern=(r'/FindBrowserWithUiElementContext|' |
| 1946 | r'FindBrowserWithTab|' |
| 1947 | r'FindBrowserWithGroup|' |
| 1948 | r'FindTabbedBrowser|' |
| 1949 | r'FindAnyBrowser|' |
| 1950 | r'FindBrowserWithProfile|' |
Erik Chen | 5f02eb4c | 2024-08-23 06:30:44 | [diff] [blame] | 1951 | r'FindLastActive|' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1952 | r'FindBrowserWithActiveWindow'), |
| 1953 | explanation= |
| 1954 | ('Most code should already be scoped to a Browser. Pass in a Browser* ' |
| 1955 | 'or retreive from an existing entity with a reference to the Browser.', |
| 1956 | ), |
| 1957 | treat_as_error=False, |
Helmut Januschka | b3f71ab5 | 2024-03-12 02:48:05 | [diff] [blame] | 1958 | ), |
| 1959 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1960 | pattern='BrowserUserData', |
| 1961 | explanation= |
| 1962 | ('Do not use BrowserUserData to store state on a Browser instance. ' |
| 1963 | 'Instead use BrowserWindowFeatures. BrowserWindowFeatures is ' |
| 1964 | 'functionally identical but has two benefits: it does not force a ' |
| 1965 | 'dependency onto class Browser, and lifetime semantics are explicit ' |
| 1966 | 'rather than implicit. See BrowserUserData header file for more ' |
| 1967 | 'details.', ), |
| 1968 | treat_as_error=False, |
Mike Dougherty | ab1bdec | 2024-08-06 16:39:01 | [diff] [blame] | 1969 | excluded_paths=( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1970 | # Exclude iOS as the iOS implementation of BrowserUserData is separate |
| 1971 | # and still in use. |
| 1972 | '^ios/', ), |
Erik Chen | 87358e8 | 2024-06-04 02:13:12 | [diff] [blame] | 1973 | ), |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 1974 | BanRule( |
Tom Sepez | d3272cd | 2025-02-21 19:11:31 | [diff] [blame] | 1975 | pattern=r'subspan(0u,', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1976 | explanation=( |
| 1977 | 'Prefer first(n) over subspan(0u, n) as it is shorter, and the ' |
| 1978 | 'compiler may have to emit a branch for the n == dynamic_extent ' |
| 1979 | 'case of subspan().', ), |
Tom Sepez | d3272cd | 2025-02-21 19:11:31 | [diff] [blame] | 1980 | treat_as_error=False, |
| 1981 | ), |
| 1982 | BanRule( |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 1983 | pattern=r'UNSAFE_TODO(', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1984 | explanation=( |
| 1985 | 'Do not use UNSAFE_TODO() to write new unsafe code. Use only when ' |
| 1986 | 'removing a pre-existing file-wide allow_unsafe_buffers pragma, or ' |
| 1987 | 'when incrementally converting code off of unsafe interfaces', ), |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 1988 | treat_as_error=False, |
| 1989 | ), |
| 1990 | BanRule( |
Claudio DeSouza | ad1d1f25 | 2025-04-22 23:38:58 | [diff] [blame] | 1991 | pattern='#pragma allow_unsafe_buffers', |
| 1992 | explanation= |
| 1993 | ('Do not use allow_unsafe_buffers to write new unsafe code. Use only ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1994 | 'when enabling unsafe buffers checks under a new uncovered path.', ), |
Claudio DeSouza | ad1d1f25 | 2025-04-22 23:38:58 | [diff] [blame] | 1995 | treat_as_error=False, |
| 1996 | ), |
| 1997 | BanRule( |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 1998 | pattern=r'UNSAFE_BUFFERS(', |
| 1999 | explanation= |
Tom Sepez | a90f92b | 2024-08-15 16:01:35 | [diff] [blame] | 2000 | ('Try to avoid using UNSAFE_BUFFERS() if at all possible. Otherwise, ' |
| 2001 | 'be sure to justify in a // SAFETY comment why other options are not ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2002 | 'available, and why the code is safe.', ), |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 2003 | treat_as_error=False, |
| 2004 | ), |
Erik Chen | d086ae0 | 2024-08-20 22:53:33 | [diff] [blame] | 2005 | BanRule( |
| 2006 | pattern='BrowserWithTestWindowTest', |
| 2007 | explanation= |
| 2008 | ('Do not use BrowserWithTestWindowTest. By instantiating an instance ' |
| 2009 | 'of class Browser, the test is no longer a unit test but is instead a ' |
| 2010 | 'browser test. The class BrowserWithTestWindowTest forces production ' |
| 2011 | 'logic to take on test-only conditionals, which is an anti-pattern. ' |
| 2012 | 'Features should be performing dependency injection rather than ' |
| 2013 | 'directly using class Browser. See ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2014 | 'docs/chrome_browser_design_principles.md for more details.', ), |
Erik Chen | d086ae0 | 2024-08-20 22:53:33 | [diff] [blame] | 2015 | treat_as_error=False, |
| 2016 | ), |
Erik Chen | 8cf3a65 | 2024-08-23 17:13:30 | [diff] [blame] | 2017 | BanRule( |
Erik Chen | 959cdd7 | 2024-08-29 02:11:21 | [diff] [blame] | 2018 | pattern='TestWithBrowserView', |
| 2019 | explanation= |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2020 | ('Do not use TestWithBrowserView. See ' |
| 2021 | 'docs/chrome_browser_design_principles.md for details. If you want ' |
| 2022 | 'to write a test that has both a Browser and a BrowserView, create ' |
| 2023 | 'a browser_test. If you want to write a unit_test, your code must ' |
| 2024 | 'not reference Browser*.', ), |
Erik Chen | 959cdd7 | 2024-08-29 02:11:21 | [diff] [blame] | 2025 | treat_as_error=False, |
| 2026 | ), |
| 2027 | BanRule( |
Erik Chen | e89ebe3 | 2025-02-22 02:46:49 | [diff] [blame] | 2028 | pattern='CreateBrowserWithTestWindow', |
| 2029 | explanation= |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2030 | ('Do not use CreateBrowserWithTestWindow. See ' |
| 2031 | 'docs/chrome_browser_design_principles.md for details. If you want ' |
| 2032 | 'to write a test that has a Browser, create a browser_test. If you ' |
| 2033 | 'want to write a unit_test, your code must not reference Browser*.', |
Erik Chen | e89ebe3 | 2025-02-22 02:46:49 | [diff] [blame] | 2034 | ), |
| 2035 | treat_as_error=False, |
| 2036 | ), |
| 2037 | BanRule( |
Erik Chen | f12a0664 | 2025-03-13 23:30:34 | [diff] [blame] | 2038 | pattern='CreateBrowserWithTestWindowForParams', |
| 2039 | explanation= |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2040 | ('Do not use CreateBrowserWithTestWindowForParams. See ' |
| 2041 | 'docs/chrome_browser_design_principles.md for details. If you want ' |
| 2042 | 'to write a test that has a Browser, create a browser_test and use ' |
| 2043 | 'Browser::Browser. If you want to write a unit_test, your code must ' |
| 2044 | 'not reference Browser*.', ), |
Erik Chen | f12a0664 | 2025-03-13 23:30:34 | [diff] [blame] | 2045 | treat_as_error=False, |
| 2046 | ), |
| 2047 | BanRule( |
Erik Chen | 8cf3a65 | 2024-08-23 17:13:30 | [diff] [blame] | 2048 | pattern='RunUntilIdle', |
| 2049 | explanation= |
| 2050 | ('Do not RunUntilIdle. If possible, explicitly quit the run loop using ' |
| 2051 | 'run_loop.Quit() or run_loop.QuitClosure() if completion can be ' |
| 2052 | 'observed using a lambda or callback. Otherwise, wait for the ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2053 | 'condition to be true via base::test::RunUntil().', ), |
Erik Chen | 8cf3a65 | 2024-08-23 17:13:30 | [diff] [blame] | 2054 | treat_as_error=False, |
| 2055 | ), |
Daniel Cheng | ddde13a | 2024-09-05 21:39:28 | [diff] [blame] | 2056 | BanRule( |
| 2057 | pattern=r'/\bstd::(literals|string_literals|string_view_literals)\b', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2058 | explanation=( |
Daniel Cheng | ddde13a | 2024-09-05 21:39:28 | [diff] [blame] | 2059 | 'User-defined literals are banned by the Google C++ style guide. ' |
| 2060 | 'Exceptions are provided in Chrome for string and string_view ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2061 | 'literals that embed \\0.', ), |
Daniel Cheng | ddde13a | 2024-09-05 21:39:28 | [diff] [blame] | 2062 | treat_as_error=True, |
| 2063 | excluded_paths=( |
| 2064 | # Various tests or test helpers that embed NUL in strings or |
| 2065 | # string_views. |
Daniel Cheng | ddde13a | 2024-09-05 21:39:28 | [diff] [blame] | 2066 | r'^base/strings/string_util_unittest\.cc', |
| 2067 | r'^base/strings/utf_string_conversions_unittest\.cc', |
| 2068 | r'^chrome/browser/ash/crosapi/browser_data_back_migrator_unittest\.cc', |
| 2069 | r'^chrome/browser/ash/crosapi/browser_data_migrator_util_unittest\.cc', |
| 2070 | r'^chrome/browser/ash/crosapi/move_migrator_unittest\.cc', |
Hidehiko Abe | 5160181 | 2025-01-12 16:17:35 | [diff] [blame] | 2071 | r'^chromeos/ash/experiences/arc/session/serial_number_util_unittest\.cc', |
Daniel Cheng | ddde13a | 2024-09-05 21:39:28 | [diff] [blame] | 2072 | r'^components/history/core/browser/visit_annotations_database\.cc', |
| 2073 | r'^components/history/core/browser/visit_annotations_database_unittest\.cc', |
| 2074 | r'^components/os_crypt/sync/os_crypt_unittest\.cc', |
| 2075 | r'^components/password_manager/core/browser/credentials_cleaner_unittest\.cc', |
| 2076 | r'^content/browser/file_system_access/file_system_access_file_writer_impl_unittest\.cc', |
| 2077 | r'^net/cookies/parsed_cookie_unittest\.cc', |
| 2078 | r'^third_party/blink/renderer/modules/webcodecs/test_helpers\.cc', |
| 2079 | r'^third_party/blink/renderer/modules/websockets/websocket_channel_impl_test\.cc', |
| 2080 | ), |
Erik Chen | ba8b0cd3 | 2024-10-01 08:36:36 | [diff] [blame] | 2081 | ), |
| 2082 | BanRule( |
| 2083 | pattern='BUILDFLAG(GOOGLE_CHROME_BRANDING)', |
| 2084 | explanation= |
| 2085 | ('Code gated by GOOGLE_CHROME_BRANDING is effectively untested. This ' |
| 2086 | 'is typically wrong. Valid use cases are glue for private modules ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2087 | 'shipped alongside Chrome, and installation-related logic.', ), |
Erik Chen | ba8b0cd3 | 2024-10-01 08:36:36 | [diff] [blame] | 2088 | treat_as_error=False, |
| 2089 | ), |
| 2090 | BanRule( |
| 2091 | pattern='defined(OFFICIAL_BUILD)', |
| 2092 | explanation= |
| 2093 | ('Code gated by OFFICIAL_BUILD is effectively untested. This ' |
| 2094 | 'is typically wrong. One valid use case is low-level code that ' |
| 2095 | 'handles subtleties related to high-levels of optimizations that come ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2096 | 'with OFFICIAL_BUILD.', ), |
Erik Chen | ba8b0cd3 | 2024-10-01 08:36:36 | [diff] [blame] | 2097 | treat_as_error=False, |
| 2098 | ), |
Erik Chen | 95b9c78 | 2024-11-08 03:26:27 | [diff] [blame] | 2099 | BanRule( |
| 2100 | pattern='WebContentsDestroyed', |
| 2101 | explanation= |
| 2102 | ('Do not use this method. It is invoked half-way through the ' |
| 2103 | 'destructor of WebContentsImpl and using it often results in crashes ' |
| 2104 | 'or surprising behavior. Conceptually, this is only necessary by ' |
| 2105 | 'objects that depend on, but outlive the WebContents. These objects ' |
| 2106 | 'should instead coordinate with the owner of the WebContents which is ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2107 | 'responsible for destroying the WebContents.', ), |
Erik Chen | 95b9c78 | 2024-11-08 03:26:27 | [diff] [blame] | 2108 | treat_as_error=False, |
| 2109 | ), |
Maksim Sisov | c98fdfa | 2024-11-16 20:12:27 | [diff] [blame] | 2110 | BanRule( |
Georg Neis | a7f94e6 | 2025-02-28 07:01:48 | [diff] [blame] | 2111 | pattern='IS_CHROMEOS_ASH', |
Maksim Sisov | c98fdfa | 2024-11-16 20:12:27 | [diff] [blame] | 2112 | explanation= |
Georg Neis | a7f94e6 | 2025-02-28 07:01:48 | [diff] [blame] | 2113 | ('IS_CHROMEOS_ASH is deprecated. Please use the equivalent IS_CHROMEOS ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2114 | 'instead (Lacros is gone).', ), |
Maksim Sisov | c98fdfa | 2024-11-16 20:12:27 | [diff] [blame] | 2115 | treat_as_error=False, |
| 2116 | ), |
Erik Chen | 1396bbe | 2025-01-27 23:39:36 | [diff] [blame] | 2117 | BanRule( |
| 2118 | pattern=(r'namespace {'), |
| 2119 | explanation= |
| 2120 | ('Anonymous namespaces are disallowed in C++ header files. See ' |
| 2121 | 'https://google.github.io/styleguide/cppguide.html#Internal_Linkage ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2122 | ' for details.', ), |
Erik Chen | 1396bbe | 2025-01-27 23:39:36 | [diff] [blame] | 2123 | treat_as_error=False, |
| 2124 | excluded_paths=[ |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2125 | _THIRD_PARTY_EXCEPT_BLINK, # Don't warn in third_party folders. |
| 2126 | r'^(?!.*\.h$).*$', # Exclude all files except those that end in .h |
Erik Chen | 1396bbe | 2025-01-27 23:39:36 | [diff] [blame] | 2127 | ], |
| 2128 | ), |
Keren Zhu | f06d757d | 2025-03-04 05:32:36 | [diff] [blame] | 2129 | BanRule( |
| 2130 | pattern=('AddChildViewRaw'), |
| 2131 | explanation=( |
| 2132 | 'Do not use AddChildViewRaw. It is prone to memory leaks and ' |
| 2133 | 'use-after-free bugs. Instead, use AddChildView(std::unique_ptr). ' |
| 2134 | 'See https://crbug.com/40485510 for more details.', ), |
| 2135 | treat_as_error=False, |
| 2136 | ), |
Nate Fischer | d541ff8 | 2025-03-11 21:34:19 | [diff] [blame] | 2137 | BanRule( |
| 2138 | pattern=(r'IS_DESKTOP_ANDROID'), |
| 2139 | explanation=( |
Eric Lok | c26a4666 | 2025-05-02 01:04:03 | [diff] [blame] | 2140 | 'Do not add new uses of IS_DESKTOP_ANDROID build flag until you ' |
| 2141 | 'have the approval of tedchoc@ or twellington@. ' |
| 2142 | 'Background: it is highly important to reduce the divergence of ' |
| 2143 | 'features across platforms. ' |
| 2144 | 'Allowances may be granted to only the directories below: ' |
| 2145 | '[build/, chrome/, components/, extensions/, infra/, tools/] ', |
| 2146 | 'Note: in particular we need to avoid components shared with ' |
| 2147 | 'WebView.', |
| 2148 | ), |
Nate Fischer | d541ff8 | 2025-03-11 21:34:19 | [diff] [blame] | 2149 | treat_as_error=False, |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 2150 | surface_as_gerrit_lint=True, |
Nate Fischer | d541ff8 | 2025-03-11 21:34:19 | [diff] [blame] | 2151 | ), |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2152 | ) |
| 2153 | |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2154 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING = ( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2155 | 'Used a predicate related to signin::ConsentLevel::kSync which will always ' |
| 2156 | 'return false in the future (crbug.com/40066949). Prefer using a predicate ' |
| 2157 | 'that also supports signin::ConsentLevel::kSignin when appropriate. It is ' |
| 2158 | 'safe to ignore this warning if you are just moving an existing call, or if ' |
| 2159 | 'you want special handling for users in the legacy state. In doubt, reach ' |
| 2160 | 'out to //components/sync/OWNERS.', ) |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2161 | |
| 2162 | # C++ functions related to signin::ConsentLevel::kSync which are deprecated. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2163 | _DEPRECATED_SYNC_CONSENT_CPP_FUNCTIONS: Sequence[BanRule] = ( |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2164 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2165 | 'HasSyncConsent', |
| 2166 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2167 | False, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2168 | ), |
| 2169 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2170 | 'CanSyncFeatureStart', |
| 2171 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2172 | False, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2173 | ), |
| 2174 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2175 | 'IsSyncFeatureEnabled', |
| 2176 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2177 | False, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2178 | ), |
| 2179 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2180 | 'IsSyncFeatureActive', |
| 2181 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2182 | False, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2183 | ), |
| 2184 | ) |
| 2185 | |
| 2186 | # Java functions related to signin::ConsentLevel::kSync which are deprecated. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2187 | _DEPRECATED_SYNC_CONSENT_JAVA_FUNCTIONS: Sequence[BanRule] = ( |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2188 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2189 | 'hasSyncConsent', |
| 2190 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2191 | False, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2192 | ), |
| 2193 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2194 | 'canSyncFeatureStart', |
| 2195 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2196 | False, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2197 | ), |
| 2198 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2199 | 'isSyncFeatureEnabled', |
| 2200 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2201 | False, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2202 | ), |
| 2203 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2204 | 'isSyncFeatureActive', |
| 2205 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2206 | False, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2207 | ), |
| 2208 | ) |
| 2209 | |
Justin Lulejian | 09fd0687 | 2025-04-01 22:03:28 | [diff] [blame] | 2210 | _BANNED_MOJOM_PATTERNS: Sequence[BanRule] = ( |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 2211 | BanRule( |
| 2212 | 'handle<shared_buffer>', |
| 2213 | ( |
Justin Lulejian | 09fd0687 | 2025-04-01 22:03:28 | [diff] [blame] | 2214 | 'Please use one of the more specific shared memory types instead:', |
| 2215 | ' mojo_base.mojom.ReadOnlySharedMemoryRegion', |
| 2216 | ' mojo_base.mojom.WritableSharedMemoryRegion', |
| 2217 | ' mojo_base.mojom.UnsafeSharedMemoryRegion', |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 2218 | ), |
| 2219 | True, |
| 2220 | ), |
Justin Lulejian | 09fd0687 | 2025-04-01 22:03:28 | [diff] [blame] | 2221 | BanRule( |
| 2222 | 'string extension_id', |
| 2223 | ( |
| 2224 | 'Please use the extensions::mojom::ExtensionId struct when ' |
| 2225 | 'passing extensions::ExtensionIds as mojom messages in order to ', |
| 2226 | 'provide message validation.', |
| 2227 | ), |
| 2228 | True, |
| 2229 | # Only apply this to (mojom) files in a subdirectory of extensions. |
| 2230 | excluded_paths=(r'^((?!extensions/).)*$', ), |
| 2231 | ), |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 2232 | ) |
| 2233 | |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 2234 | _IPC_ENUM_TRAITS_DEPRECATED = ( |
| 2235 | '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] | 2236 | 'See http://www.chromium.org/Home/chromium-security/education/' |
| 2237 | 'security-tips-for-ipc') |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 2238 | |
Stephen Martinis | 97a39414 | 2018-06-07 23:06:05 | [diff] [blame] | 2239 | _LONG_PATH_ERROR = ( |
| 2240 | 'Some files included in this CL have file names that are too long (> 200' |
| 2241 | ' characters). If committed, these files will cause issues on Windows. See' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2242 | ' https://crbug.com/612667 for more details.') |
Stephen Martinis | 97a39414 | 2018-06-07 23:06:05 | [diff] [blame] | 2243 | |
Shenghua Zhang | bfaa38b8 | 2017-11-16 21:58:02 | [diff] [blame] | 2244 | _JAVA_MULTIPLE_DEFINITION_EXCLUDED_PATHS = [ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2245 | r".*/BuildHooksAndroidImpl\.java", |
| 2246 | r".*/LicenseContentProvider\.java", |
| 2247 | r".*/PlatformServiceBridgeImpl.java", |
| 2248 | r".*chrome/android/feed/dummy/.*\.java", |
Shenghua Zhang | bfaa38b8 | 2017-11-16 21:58:02 | [diff] [blame] | 2249 | ] |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2250 | |
Mohamed Heikal | d048240a | 2019-11-12 16:57:37 | [diff] [blame] | 2251 | # List of image extensions that are used as resources in chromium. |
| 2252 | _IMAGE_EXTENSIONS = ['.svg', '.png', '.webp'] |
| 2253 | |
Sean Kau | 46e29bc | 2017-08-28 16:31:16 | [diff] [blame] | 2254 | # These paths contain test data and other known invalid JSON files. |
Erik Staab | 2dd72b1 | 2020-04-16 15:03:40 | [diff] [blame] | 2255 | _KNOWN_TEST_DATA_AND_INVALID_JSON_FILE_PATTERNS = [ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2256 | r'test/data/', |
| 2257 | r'testing/buildbot/', |
| 2258 | r'^components/policy/resources/policy_templates\.json$', |
| 2259 | r'^third_party/protobuf/', |
Camillo Bruni | 1411a35 | 2023-05-24 12:39:03 | [diff] [blame] | 2260 | r'^third_party/blink/perf_tests/speedometer.*/resources/todomvc/learn\.json', |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2261 | r'^third_party/blink/renderer/devtools/protocol\.json$', |
| 2262 | r'^third_party/blink/web_tests/external/wpt/', |
| 2263 | r'^tools/perf/', |
| 2264 | r'^tools/traceline/svgui/startup-release.json', |
Daniel Cheng | 2d4c2d19 | 2022-07-01 01:38:31 | [diff] [blame] | 2265 | # vscode configuration files allow comments |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2266 | r'^tools/vscode/', |
Sean Kau | 46e29bc | 2017-08-28 16:31:16 | [diff] [blame] | 2267 | ] |
| 2268 | |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2269 | # These are not checked on the public chromium-presubmit trybot. |
| 2270 | # 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] | 2271 | # checkouts. |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 2272 | _ANDROID_SPECIFIC_PYDEPS_FILES = [ |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2273 | 'chrome/android/features/create_stripped_java_factory.pydeps', |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2274 | ] |
| 2275 | |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2276 | _GENERIC_PYDEPS_FILES = [ |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2277 | 'android_webview/tools/run_cts.pydeps', |
Andrew Grieve | 4c4cede | 2020-11-20 22:09:36 | [diff] [blame] | 2278 | 'build/android/apk_operations.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2279 | 'build/android/devil_chromium.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2280 | 'build/android/gyp/aar.pydeps', |
| 2281 | 'build/android/gyp/aidl.pydeps', |
| 2282 | 'build/android/gyp/apkbuilder.pydeps', |
Andrew Grieve | a417ad30 | 2019-02-06 19:54:38 | [diff] [blame] | 2283 | 'build/android/gyp/assert_static_initializers.pydeps', |
Mohamed Heikal | 133e1f2 | 2023-04-18 20:04:37 | [diff] [blame] | 2284 | 'build/android/gyp/binary_baseline_profile.pydeps', |
Robbie McElrath | 360e54d | 2020-11-12 20:38:02 | [diff] [blame] | 2285 | 'build/android/gyp/bytecode_rewriter.pydeps', |
Mohamed Heikal | 6305bcc | 2021-03-15 15:34:22 | [diff] [blame] | 2286 | 'build/android/gyp/check_flag_expectations.pydeps', |
Andrew Grieve | acac424 | 2024-12-20 19:39:42 | [diff] [blame] | 2287 | 'build/android/gyp/check_for_missing_direct_deps.pydeps', |
Andrew Grieve | 8d083ea | 2019-12-13 06:49:11 | [diff] [blame] | 2288 | 'build/android/gyp/compile_java.pydeps', |
Peter Wen | eaa963f | 2023-01-20 19:40:30 | [diff] [blame] | 2289 | 'build/android/gyp/compile_kt.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2290 | 'build/android/gyp/compile_resources.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2291 | 'build/android/gyp/copy_ex.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2292 | 'build/android/gyp/create_apk_operations_script.pydeps', |
Andrew Grieve | 8d083ea | 2019-12-13 06:49:11 | [diff] [blame] | 2293 | 'build/android/gyp/create_app_bundle.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2294 | 'build/android/gyp/create_app_bundle_apks.pydeps', |
| 2295 | 'build/android/gyp/create_bundle_wrapper_script.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2296 | 'build/android/gyp/create_java_binary_script.pydeps', |
Mohamed Heikal | adbe4e48 | 2020-07-09 19:25:12 | [diff] [blame] | 2297 | 'build/android/gyp/create_r_java.pydeps', |
Mohamed Heikal | 8cd763a5 | 2021-02-01 23:32:09 | [diff] [blame] | 2298 | 'build/android/gyp/create_r_txt.pydeps', |
Andrew Grieve | b838d83 | 2019-02-11 16:55:22 | [diff] [blame] | 2299 | 'build/android/gyp/create_size_info_files.pydeps', |
Andrew Grieve | 2d972e5f | 2025-01-28 18:28:14 | [diff] [blame] | 2300 | 'build/android/gyp/create_stub_manifest.pydeps', |
Peter Wen | e6e017e | 2022-07-27 21:40:40 | [diff] [blame] | 2301 | 'build/android/gyp/create_test_apk_wrapper_script.pydeps', |
Andrew Grieve | 5a01ad3 | 2020-06-25 18:06:00 | [diff] [blame] | 2302 | 'build/android/gyp/create_ui_locale_resources.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2303 | 'build/android/gyp/dex.pydeps', |
| 2304 | 'build/android/gyp/dist_aar.pydeps', |
Andrew Grieve | 651ddb3 | 2025-01-23 03:27:34 | [diff] [blame] | 2305 | 'build/android/gyp/errorprone.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2306 | 'build/android/gyp/filter_zip.pydeps', |
Mohamed Heikal | 21e1994b | 2021-11-12 21:37:21 | [diff] [blame] | 2307 | 'build/android/gyp/flatc_java.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2308 | 'build/android/gyp/gcc_preprocess.pydeps', |
Christopher Grant | 99e0e2006 | 2018-11-21 21:22:36 | [diff] [blame] | 2309 | 'build/android/gyp/generate_linker_version_script.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2310 | 'build/android/gyp/ijar.pydeps', |
Yun Liu | eb4075ddf | 2019-05-13 19:47:58 | [diff] [blame] | 2311 | 'build/android/gyp/jacoco_instr.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2312 | 'build/android/gyp/java_cpp_enum.pydeps', |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 2313 | 'build/android/gyp/java_cpp_features.pydeps', |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 2314 | 'build/android/gyp/java_cpp_strings.pydeps', |
Andrew Grieve | 0945791 | 2021-04-27 15:22:47 | [diff] [blame] | 2315 | 'build/android/gyp/java_google_api_keys.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2316 | 'build/android/gyp/jinja_template.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2317 | 'build/android/gyp/lint.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2318 | 'build/android/gyp/merge_manifest.pydeps', |
Bruce Dawson | 853b739e6 | 2022-05-03 23:03:10 | [diff] [blame] | 2319 | 'build/android/gyp/optimize_resources.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2320 | 'build/android/gyp/prepare_resources.pydeps', |
Mohamed Heikal | f85138b | 2020-10-06 15:43:22 | [diff] [blame] | 2321 | 'build/android/gyp/process_native_prebuilt.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2322 | 'build/android/gyp/proguard.pydeps', |
Mohamed Heikal | dd52b45 | 2024-09-10 17:10:50 | [diff] [blame] | 2323 | 'build/android/gyp/rename_java_classes.pydeps', |
Andrew Grieve | e3a775ab | 2022-05-16 15:59:22 | [diff] [blame] | 2324 | 'build/android/gyp/system_image_apks.pydeps', |
Bruce Dawson | 853b739e6 | 2022-05-03 23:03:10 | [diff] [blame] | 2325 | 'build/android/gyp/trace_event_bytecode_rewriter.pydeps', |
Andrew Grieve | 170b978 | 2025-02-03 15:54:53 | [diff] [blame] | 2326 | 'build/android/gyp/tracereferences.pydeps', |
Peter Wen | 578730b | 2020-03-19 19:55:46 | [diff] [blame] | 2327 | 'build/android/gyp/turbine.pydeps', |
Mohamed Heikal | 246710c | 2021-06-14 15:34:30 | [diff] [blame] | 2328 | 'build/android/gyp/unused_resources.pydeps', |
Eric Stevenson | a82cf608 | 2019-07-24 14:35:24 | [diff] [blame] | 2329 | 'build/android/gyp/validate_static_library_dex_references.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2330 | 'build/android/gyp/write_build_config.pydeps', |
Tibor Goldschwendt | c4caae9 | 2019-07-12 00:33:46 | [diff] [blame] | 2331 | 'build/android/gyp/write_native_libraries_java.pydeps', |
Andrew Grieve | 9ff1779 | 2018-11-30 04:55:56 | [diff] [blame] | 2332 | 'build/android/gyp/zip.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2333 | 'build/android/incremental_install/generate_android_manifest.pydeps', |
| 2334 | 'build/android/incremental_install/write_installer_json.pydeps', |
Stephanie Kim | 392913b45 | 2022-06-15 17:25:32 | [diff] [blame] | 2335 | 'build/android/pylib/results/presentation/test_results_presentation.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2336 | 'build/android/resource_sizes.pydeps', |
| 2337 | 'build/android/test_runner.pydeps', |
| 2338 | 'build/android/test_wrapper/logdog_wrapper.pydeps', |
zijiehe-google-com | 356980f | 2025-05-08 20:58:15 | [diff] [blame] | 2339 | 'build/fuchsia/test/component_storage_test.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2340 | 'build/protoc_java.pydeps', |
Peter Kotwicz | 64667b0 | 2020-10-18 06:43:32 | [diff] [blame] | 2341 | 'chrome/android/monochrome/scripts/monochrome_python_tests.pydeps', |
Peter Wen | efb56c7 | 2020-06-04 15:12:27 | [diff] [blame] | 2342 | 'chrome/test/chromedriver/log_replay/client_replay_unittest.pydeps', |
| 2343 | 'chrome/test/chromedriver/test/run_py_tests.pydeps', |
Bailey Myers-Morgan | bd12213 | 2025-03-26 23:09:16 | [diff] [blame] | 2344 | 'chrome/test/media_router/performance/performance_test.pydeps', |
Junbo Ke | dcd3a45 | 2021-03-19 17:55:04 | [diff] [blame] | 2345 | 'chromecast/resource_sizes/chromecast_resource_sizes.pydeps', |
Mohannad Farrag | 1910274 | 2023-12-01 01:16:30 | [diff] [blame] | 2346 | 'components/cronet/tools/check_combined_proguard_file.pydeps', |
| 2347 | 'components/cronet/tools/generate_proguard_file.pydeps', |
Andrew Grieve | 5a01ad3 | 2020-06-25 18:06:00 | [diff] [blame] | 2348 | 'components/cronet/tools/jar_src.pydeps', |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2349 | 'components/module_installer/android/module_desc_java.pydeps', |
Andrew Grieve | 5a01ad3 | 2020-06-25 18:06:00 | [diff] [blame] | 2350 | 'content/public/android/generate_child_service.pydeps', |
Hzj_jie | 77bdb80 | 2024-07-22 18:14:51 | [diff] [blame] | 2351 | 'fuchsia_web/av_testing/av_sync_tests.pydeps', |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2352 | 'net/tools/testserver/testserver.pydeps', |
Peter Kotwicz | 3c339f3 | 2020-10-19 19:59:18 | [diff] [blame] | 2353 | 'testing/scripts/run_isolated_script_test.pydeps', |
Stephanie Kim | c94072c | 2022-03-22 22:31:41 | [diff] [blame] | 2354 | 'testing/merge_scripts/standard_isolated_script_merge.pydeps', |
| 2355 | 'testing/merge_scripts/standard_gtest_merge.pydeps', |
| 2356 | 'testing/merge_scripts/code_coverage/merge_results.pydeps', |
| 2357 | 'testing/merge_scripts/code_coverage/merge_steps.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2358 | 'third_party/android_platform/development/scripts/stack.pydeps', |
Hitoshi Yoshida | 0f228c4 | 2019-08-07 09:37:42 | [diff] [blame] | 2359 | 'third_party/blink/renderer/bindings/scripts/build_web_idl_database.pydeps', |
Yuki Shiino | 38eeaad1 | 2022-08-11 06:40:25 | [diff] [blame] | 2360 | 'third_party/blink/renderer/bindings/scripts/check_generated_file_list.pydeps', |
Hitoshi Yoshida | 0f228c4 | 2019-08-07 09:37:42 | [diff] [blame] | 2361 | 'third_party/blink/renderer/bindings/scripts/collect_idl_files.pydeps', |
Yuki Shiino | e7827aa | 2019-09-13 12:26:13 | [diff] [blame] | 2362 | 'third_party/blink/renderer/bindings/scripts/generate_bindings.pydeps', |
Yuki Shiino | ea477d3 | 2023-08-21 06:24:34 | [diff] [blame] | 2363 | 'third_party/blink/renderer/bindings/scripts/generate_event_interface_names.pydeps', |
Canon Mukai | f32f8f59 | 2021-04-23 18:56:50 | [diff] [blame] | 2364 | 'third_party/blink/renderer/bindings/scripts/validate_web_idl.pydeps', |
Stephanie Kim | c94072c | 2022-03-22 22:31:41 | [diff] [blame] | 2365 | 'third_party/blink/tools/blinkpy/web_tests/merge_results.pydeps', |
| 2366 | 'third_party/blink/tools/merge_web_test_results.pydeps', |
John Budorick | bc3571aa | 2019-04-25 02:20:06 | [diff] [blame] | 2367 | 'tools/binary_size/sizes.pydeps', |
Andrew Grieve | a7f1ee90 | 2018-05-18 16:17:22 | [diff] [blame] | 2368 | 'tools/binary_size/supersize.pydeps', |
Peter Wen | 2dcfa6e | 2025-03-04 22:42:52 | [diff] [blame] | 2369 | 'tools/cygprofile/generate_orderfile.pydeps', |
Ben Pastene | 028104a | 2022-08-10 19:17:45 | [diff] [blame] | 2370 | 'tools/perf/process_perf_results.pydeps', |
Peter Wen | ce103e1 | 2024-10-09 19:23:51 | [diff] [blame] | 2371 | 'tools/pgo/generate_profile.pydeps', |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 2372 | ] |
| 2373 | |
| 2374 | _ALL_PYDEPS_FILES = _ANDROID_SPECIFIC_PYDEPS_FILES + _GENERIC_PYDEPS_FILES |
| 2375 | |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 2376 | # Bypass the AUTHORS check for these accounts. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2377 | _KNOWN_ROBOTS = set() | set('%[email protected]' % s for s in ( |
| 2378 | 'findit-for-me', 'luci-bisection', 'predator-for-me-staging', |
| 2379 | 'predator-for-me')) | set( |
| 2380 | '%[email protected]' % s |
| 2381 | for s in ('3su6n15k.default', )) | set( |
| 2382 | '%[email protected]' % s |
| 2383 | for s in ('bling-autoroll-builder', 'v8-ci-autoroll-builder', |
| 2384 | 'wpt-autoroller', 'chrome-weblayer-builder', |
| 2385 | 'skylab-test-cros-roller', 'infra-try-recipes-tester', |
| 2386 | 'chrome-automated-expectation', |
| 2387 | 'chromium-automated-expectation', 'chrome-branch-day', |
| 2388 | 'chrome-cherry-picker', 'chromium-autosharder') |
| 2389 | ) | set( |
| 2390 | '%[email protected]' % s |
| 2391 | for s in ('chromium-autoroll', 'chromium-release-autoroll')) | set( |
| 2392 | '%[email protected]' % s |
| 2393 | for s in ('chromium-internal-autoroll', ) |
| 2394 | ) | set( |
| 2395 | '%[email protected]' % |
| 2396 | s for s in ('chrome-screen-ai-releaser', 'crash-eng', 'crash') |
| 2397 | ) | set( |
| 2398 | '%[email protected]' % s |
| 2399 | for s in ('swarming-tasks', )) | set( |
| 2400 | '%[email protected]' % s |
| 2401 | for s in ('global-integration-try-builder', |
Rachael Newitt | e5664ef9 | 2025-05-08 14:00:23 | [diff] [blame] | 2402 | 'global-integration-ci-builder') |
| 2403 | ) | set('%[email protected]' % s for s in ( |
| 2404 | 'chops-security-borg', |
| 2405 | 'chops-security-cronjobs-cpesuggest')) | set( |
| 2406 | '%[email protected]' % s |
| 2407 | for s in ('chromeos-ci-release', )) |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 2408 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2409 | _INVALID_GRD_FILE_LINE = [(r'<file lang=.* path=.*', |
| 2410 | 'Path should come before lang in GRD files.')] |
| 2411 | |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 2412 | |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2413 | def _IsCPlusPlusFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2414 | """Returns True if this file contains C++-like code (and not Python, |
| 2415 | Go, Java, MarkDown, ...)""" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2416 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2417 | ext = input_api.os_path.splitext(file_path)[1] |
| 2418 | # This list is compatible with CppChecker.IsCppFile but we should |
| 2419 | # consider adding ".c" to it. If we do that we can use this function |
| 2420 | # at more places in the code. |
| 2421 | return ext in ( |
| 2422 | '.h', |
| 2423 | '.cc', |
| 2424 | '.cpp', |
| 2425 | '.m', |
| 2426 | '.mm', |
| 2427 | ) |
| 2428 | |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2429 | |
| 2430 | def _IsCPlusPlusHeaderFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2431 | return input_api.os_path.splitext(file_path)[1] == ".h" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2432 | |
| 2433 | |
| 2434 | def _IsJavaFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2435 | return input_api.os_path.splitext(file_path)[1] == ".java" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2436 | |
| 2437 | |
| 2438 | def _IsProtoFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2439 | return input_api.os_path.splitext(file_path)[1] == ".proto" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2440 | |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2441 | |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 2442 | def _IsXmlOrGrdFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2443 | ext = input_api.os_path.splitext(file_path)[1] |
| 2444 | return ext in ('.grd', '.xml') |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 2445 | |
| 2446 | |
Sven Zheng | 76a79ea | 2022-12-21 21:25:24 | [diff] [blame] | 2447 | def _IsMojomFile(input_api, file_path): |
| 2448 | return input_api.os_path.splitext(file_path)[1] == ".mojom" |
| 2449 | |
| 2450 | |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2451 | def CheckNoUpstreamDepsOnClank(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2452 | """Prevent additions of dependencies from the upstream repo on //clank.""" |
| 2453 | # clank can depend on clank |
| 2454 | if input_api.change.RepositoryRoot().endswith('clank'): |
| 2455 | return [] |
| 2456 | build_file_patterns = [ |
| 2457 | r'(.+/)?BUILD\.gn', |
| 2458 | r'.+\.gni', |
| 2459 | ] |
| 2460 | excluded_files = [r'build[/\\]config[/\\]android[/\\]config\.gni'] |
| 2461 | bad_pattern = input_api.re.compile(r'^[^#]*//clank') |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2462 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2463 | error_message = 'Disallowed import on //clank in an upstream build file:' |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2464 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2465 | def FilterFile(affected_file): |
| 2466 | return input_api.FilterSourceFile(affected_file, |
| 2467 | files_to_check=build_file_patterns, |
| 2468 | files_to_skip=excluded_files) |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2469 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2470 | problems = [] |
| 2471 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 2472 | local_path = f.LocalPath() |
| 2473 | for line_number, line in f.ChangedContents(): |
| 2474 | if (bad_pattern.search(line)): |
| 2475 | problems.append('%s:%d\n %s' % |
| 2476 | (local_path, line_number, line.strip())) |
| 2477 | if problems: |
| 2478 | return [output_api.PresubmitPromptOrNotify(error_message, problems)] |
| 2479 | else: |
| 2480 | return [] |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2481 | |
| 2482 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2483 | def CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2484 | """Attempts to prevent use of functions intended only for testing in |
| 2485 | non-testing code. For now this is just a best-effort implementation |
| 2486 | that ignores header files and may have some false positives. A |
| 2487 | better implementation would probably need a proper C++ parser. |
| 2488 | """ |
| 2489 | # We only scan .cc files and the like, as the declaration of |
| 2490 | # for-testing functions in header files are hard to distinguish from |
| 2491 | # calls to such functions without a proper C++ parser. |
| 2492 | file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS] |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2493 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2494 | base_function_pattern = r'[ :]test::[^\s]+|ForTest(s|ing)?|for_test(s|ing)?' |
| 2495 | inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % |
| 2496 | base_function_pattern) |
| 2497 | comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern) |
| 2498 | allowlist_pattern = input_api.re.compile(r'// IN-TEST$') |
| 2499 | exclusion_pattern = input_api.re.compile( |
| 2500 | r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % |
| 2501 | (base_function_pattern, base_function_pattern)) |
| 2502 | # Avoid a false positive in this case, where the method name, the ::, and |
| 2503 | # the closing { are all on different lines due to line wrapping. |
| 2504 | # HelperClassForTesting:: |
| 2505 | # HelperClassForTesting( |
| 2506 | # args) |
| 2507 | # : member(0) {} |
| 2508 | method_defn_pattern = input_api.re.compile(r'[A-Za-z0-9_]+::$') |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2509 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2510 | def FilterFile(affected_file): |
| 2511 | files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 2512 | input_api.DEFAULT_FILES_TO_SKIP) |
| 2513 | return input_api.FilterSourceFile( |
| 2514 | affected_file, |
| 2515 | files_to_check=file_inclusion_pattern, |
| 2516 | files_to_skip=files_to_skip) |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2517 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2518 | problems = [] |
| 2519 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 2520 | local_path = f.LocalPath() |
| 2521 | in_method_defn = False |
| 2522 | for line_number, line in f.ChangedContents(): |
| 2523 | if (inclusion_pattern.search(line) |
| 2524 | and not comment_pattern.search(line) |
| 2525 | and not exclusion_pattern.search(line) |
| 2526 | and not allowlist_pattern.search(line) |
| 2527 | and not in_method_defn): |
| 2528 | problems.append('%s:%d\n %s' % |
| 2529 | (local_path, line_number, line.strip())) |
| 2530 | in_method_defn = method_defn_pattern.search(line) |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2531 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2532 | if problems: |
| 2533 | return [ |
| 2534 | output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems) |
| 2535 | ] |
| 2536 | else: |
| 2537 | return [] |
[email protected] | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2538 | |
| 2539 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2540 | def CheckNoProductionCodeUsingTestOnlyFunctionsJava(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2541 | """This is a simplified version of |
| 2542 | CheckNoProductionCodeUsingTestOnlyFunctions for Java files. |
| 2543 | """ |
| 2544 | javadoc_start_re = input_api.re.compile(r'^\s*/\*\*') |
| 2545 | javadoc_end_re = input_api.re.compile(r'^\s*\*/') |
| 2546 | name_pattern = r'ForTest(s|ing)?' |
| 2547 | # Describes an occurrence of "ForTest*" inside a // comment. |
| 2548 | comment_re = input_api.re.compile(r'//.*%s' % name_pattern) |
| 2549 | # Describes @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED) |
| 2550 | annotation_re = input_api.re.compile(r'@VisibleForTesting\(') |
| 2551 | # Catch calls. |
| 2552 | inclusion_re = input_api.re.compile(r'(%s)\s*\(' % name_pattern) |
| 2553 | # Ignore definitions. (Comments are ignored separately.) |
| 2554 | exclusion_re = input_api.re.compile(r'(%s)[^;]+\{' % name_pattern) |
Andrew Grieve | 40f451d | 2023-07-06 19:46:51 | [diff] [blame] | 2555 | allowlist_re = input_api.re.compile(r'// IN-TEST$') |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 2556 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2557 | problems = [] |
| 2558 | sources = lambda x: input_api.FilterSourceFile( |
| 2559 | x, |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2560 | files_to_skip=( |
| 2561 | ('(?i).*test', r'.*\/junit\/') + input_api.DEFAULT_FILES_TO_SKIP), |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2562 | files_to_check=[r'.*\.java$']) |
| 2563 | for f in input_api.AffectedFiles(include_deletes=False, |
| 2564 | file_filter=sources): |
| 2565 | local_path = f.LocalPath() |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 2566 | is_inside_javadoc = False |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2567 | for line_number, line in f.ChangedContents(): |
| 2568 | if is_inside_javadoc and javadoc_end_re.search(line): |
| 2569 | is_inside_javadoc = False |
| 2570 | if not is_inside_javadoc and javadoc_start_re.search(line): |
| 2571 | is_inside_javadoc = True |
| 2572 | if is_inside_javadoc: |
| 2573 | continue |
| 2574 | if (inclusion_re.search(line) and not comment_re.search(line) |
| 2575 | and not annotation_re.search(line) |
Andrew Grieve | 40f451d | 2023-07-06 19:46:51 | [diff] [blame] | 2576 | and not allowlist_re.search(line) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2577 | and not exclusion_re.search(line)): |
| 2578 | problems.append('%s:%d\n %s' % |
| 2579 | (local_path, line_number, line.strip())) |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 2580 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2581 | if problems: |
| 2582 | return [ |
| 2583 | output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems) |
| 2584 | ] |
| 2585 | else: |
| 2586 | return [] |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 2587 | |
| 2588 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2589 | def CheckNoIOStreamInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2590 | """Checks to make sure no .h files include <iostream>.""" |
| 2591 | files = [] |
| 2592 | pattern = input_api.re.compile(r'^#include\s*<iostream>', |
| 2593 | input_api.re.MULTILINE) |
| 2594 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 2595 | if not f.LocalPath().endswith('.h'): |
| 2596 | continue |
| 2597 | contents = input_api.ReadFile(f) |
| 2598 | if pattern.search(contents): |
| 2599 | files.append(f) |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 2600 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2601 | if len(files): |
| 2602 | return [ |
| 2603 | output_api.PresubmitError( |
| 2604 | 'Do not #include <iostream> in header files, since it inserts static ' |
| 2605 | 'initialization into every file including the header. Instead, ' |
| 2606 | '#include <ostream>. See http://crbug.com/94794', files) |
| 2607 | ] |
| 2608 | return [] |
| 2609 | |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 2610 | |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 2611 | def CheckNoStrCatRedefines(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2612 | """Checks no windows headers with StrCat redefined are included directly.""" |
| 2613 | files = [] |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 2614 | files_to_check = (r'.+%s' % _HEADER_EXTENSIONS, |
| 2615 | r'.+%s' % _IMPLEMENTATION_EXTENSIONS) |
| 2616 | files_to_skip = (input_api.DEFAULT_FILES_TO_SKIP + |
| 2617 | _NON_BASE_DEPENDENT_PATHS) |
| 2618 | sources_filter = lambda f: input_api.FilterSourceFile( |
| 2619 | f, files_to_check=files_to_check, files_to_skip=files_to_skip) |
| 2620 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2621 | pattern_deny = input_api.re.compile( |
| 2622 | r'^#include\s*[<"](shlwapi|atlbase|propvarutil|sphelper).h[">]', |
| 2623 | input_api.re.MULTILINE) |
| 2624 | pattern_allow = input_api.re.compile( |
| 2625 | r'^#include\s"base/win/windows_defines.inc"', input_api.re.MULTILINE) |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 2626 | for f in input_api.AffectedSourceFiles(sources_filter): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2627 | contents = input_api.ReadFile(f) |
| 2628 | if pattern_deny.search( |
| 2629 | contents) and not pattern_allow.search(contents): |
| 2630 | files.append(f.LocalPath()) |
Danil Chapovalov | 3518f36 | 2018-08-11 16:13:43 | [diff] [blame] | 2631 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2632 | if len(files): |
| 2633 | return [ |
| 2634 | output_api.PresubmitError( |
| 2635 | 'Do not #include shlwapi.h, atlbase.h, propvarutil.h or sphelper.h ' |
| 2636 | 'directly since they pollute code with StrCat macro. Instead, ' |
| 2637 | 'include matching header from base/win. See http://crbug.com/856536', |
| 2638 | files) |
| 2639 | ] |
| 2640 | return [] |
Danil Chapovalov | 3518f36 | 2018-08-11 16:13:43 | [diff] [blame] | 2641 | |
[email protected] | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 2642 | |
Andrew Williams | c9f69b48 | 2023-07-10 16:07:36 | [diff] [blame] | 2643 | def _CheckNoUNIT_TESTInSourceFiles(input_api, f): |
| 2644 | problems = [] |
| 2645 | |
| 2646 | unit_test_macro = input_api.re.compile( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2647 | r'^\s*#.*(?:ifn?def\s+UNIT_TEST|defined\s*\(?\s*UNIT_TEST\s*\)?)(?:$|\s+)' |
| 2648 | ) |
Andrew Williams | c9f69b48 | 2023-07-10 16:07:36 | [diff] [blame] | 2649 | for line_num, line in f.ChangedContents(): |
| 2650 | if unit_test_macro.match(line): |
| 2651 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 2652 | |
| 2653 | return problems |
| 2654 | |
| 2655 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2656 | def CheckNoUNIT_TESTInSourceFiles(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2657 | """Checks to make sure no source files use UNIT_TEST.""" |
| 2658 | problems = [] |
| 2659 | for f in input_api.AffectedFiles(): |
| 2660 | if (not f.LocalPath().endswith(('.cc', '.mm'))): |
| 2661 | continue |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2662 | problems.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, f)) |
[email protected] | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 2663 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2664 | if not problems: |
| 2665 | return [] |
| 2666 | return [ |
| 2667 | output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n' + |
| 2668 | '\n'.join(problems)) |
| 2669 | ] |
| 2670 | |
[email protected] | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 2671 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2672 | def CheckNoDISABLETypoInTests(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2673 | """Checks to prevent attempts to disable tests with DISABLE_ prefix. |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2674 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2675 | This test warns if somebody tries to disable a test with the DISABLE_ prefix |
| 2676 | instead of DISABLED_. To filter false positives, reports are only generated |
| 2677 | if a corresponding MAYBE_ line exists. |
| 2678 | """ |
| 2679 | problems = [] |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2680 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2681 | # The following two patterns are looked for in tandem - is a test labeled |
| 2682 | # as MAYBE_ followed by a DISABLE_ (instead of the correct DISABLED) |
| 2683 | maybe_pattern = input_api.re.compile(r'MAYBE_([a-zA-Z0-9_]+)') |
| 2684 | disable_pattern = input_api.re.compile(r'DISABLE_([a-zA-Z0-9_]+)') |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2685 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2686 | # This is for the case that a test is disabled on all platforms. |
| 2687 | full_disable_pattern = input_api.re.compile( |
| 2688 | r'^\s*TEST[^(]*\([a-zA-Z0-9_]+,\s*DISABLE_[a-zA-Z0-9_]+\)', |
| 2689 | input_api.re.MULTILINE) |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2690 | |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 2691 | for f in input_api.AffectedFiles(include_deletes=False): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2692 | if not 'test' in f.LocalPath() or not f.LocalPath().endswith('.cc'): |
| 2693 | continue |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2694 | |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 2695 | # Search for MAYBE_, DISABLE_ pairs. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2696 | disable_lines = {} # Maps of test name to line number. |
| 2697 | maybe_lines = {} |
| 2698 | for line_num, line in f.ChangedContents(): |
| 2699 | disable_match = disable_pattern.search(line) |
| 2700 | if disable_match: |
| 2701 | disable_lines[disable_match.group(1)] = line_num |
| 2702 | maybe_match = maybe_pattern.search(line) |
| 2703 | if maybe_match: |
| 2704 | maybe_lines[maybe_match.group(1)] = line_num |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2705 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2706 | # Search for DISABLE_ occurrences within a TEST() macro. |
| 2707 | disable_tests = set(disable_lines.keys()) |
| 2708 | maybe_tests = set(maybe_lines.keys()) |
| 2709 | for test in disable_tests.intersection(maybe_tests): |
| 2710 | problems.append(' %s:%d' % (f.LocalPath(), disable_lines[test])) |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2711 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2712 | contents = input_api.ReadFile(f) |
| 2713 | full_disable_match = full_disable_pattern.search(contents) |
| 2714 | if full_disable_match: |
| 2715 | problems.append(' %s' % f.LocalPath()) |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2716 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2717 | if not problems: |
| 2718 | return [] |
| 2719 | return [ |
| 2720 | output_api.PresubmitPromptWarning( |
| 2721 | 'Attempt to disable a test with DISABLE_ instead of DISABLED_?\n' + |
| 2722 | '\n'.join(problems)) |
| 2723 | ] |
| 2724 | |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2725 | |
Nina Satragno | f766053 | 2021-09-20 18:03:35 | [diff] [blame] | 2726 | def CheckForgettingMAYBEInTests(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2727 | """Checks to make sure tests disabled conditionally are not missing a |
| 2728 | corresponding MAYBE_ prefix. |
| 2729 | """ |
| 2730 | # Expect at least a lowercase character in the test name. This helps rule out |
| 2731 | # false positives with macros wrapping the actual tests name. |
| 2732 | define_maybe_pattern = input_api.re.compile( |
| 2733 | r'^\#define MAYBE_(?P<test_name>\w*[a-z]\w*)') |
Bruce Dawson | ffc5529 | 2022-04-20 04:18:19 | [diff] [blame] | 2734 | # The test_maybe_pattern needs to handle all of these forms. The standard: |
| 2735 | # IN_PROC_TEST_F(SyncTest, MAYBE_Start) { |
| 2736 | # With a wrapper macro around the test name: |
| 2737 | # IN_PROC_TEST_F(SyncTest, E2E_ENABLED(MAYBE_Start)) { |
| 2738 | # And the odd-ball NACL_BROWSER_TEST_f format: |
| 2739 | # NACL_BROWSER_TEST_F(NaClBrowserTest, SimpleLoad, { |
| 2740 | # The optional E2E_ENABLED-style is handled with (\w*\()? |
| 2741 | # The NACL_BROWSER_TEST_F pattern is handled by allowing a trailing comma or |
| 2742 | # trailing ')'. |
| 2743 | test_maybe_pattern = ( |
| 2744 | r'^\s*\w*TEST[^(]*\(\s*\w+,\s*(\w*\()?MAYBE_{test_name}[\),]') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2745 | suite_maybe_pattern = r'^\s*\w*TEST[^(]*\(\s*MAYBE_{test_name}[\),]' |
| 2746 | warnings = [] |
Nina Satragno | f766053 | 2021-09-20 18:03:35 | [diff] [blame] | 2747 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2748 | # Read the entire files. We can't just read the affected lines, forgetting to |
| 2749 | # add MAYBE_ on a change would not show up otherwise. |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 2750 | for f in input_api.AffectedFiles(include_deletes=False): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2751 | if not 'test' in f.LocalPath() or not f.LocalPath().endswith('.cc'): |
| 2752 | continue |
| 2753 | contents = input_api.ReadFile(f) |
| 2754 | lines = contents.splitlines(True) |
| 2755 | current_position = 0 |
| 2756 | warning_test_names = set() |
| 2757 | for line_num, line in enumerate(lines, start=1): |
| 2758 | current_position += len(line) |
| 2759 | maybe_match = define_maybe_pattern.search(line) |
| 2760 | if maybe_match: |
| 2761 | test_name = maybe_match.group('test_name') |
| 2762 | # Do not warn twice for the same test. |
| 2763 | if (test_name in warning_test_names): |
| 2764 | continue |
| 2765 | warning_test_names.add(test_name) |
Nina Satragno | f766053 | 2021-09-20 18:03:35 | [diff] [blame] | 2766 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2767 | # Attempt to find the corresponding MAYBE_ test or suite, starting from |
| 2768 | # the current position. |
| 2769 | test_match = input_api.re.compile( |
| 2770 | test_maybe_pattern.format(test_name=test_name), |
| 2771 | input_api.re.MULTILINE).search(contents, current_position) |
| 2772 | suite_match = input_api.re.compile( |
| 2773 | suite_maybe_pattern.format(test_name=test_name), |
| 2774 | input_api.re.MULTILINE).search(contents, current_position) |
| 2775 | if not test_match and not suite_match: |
| 2776 | warnings.append( |
| 2777 | output_api.PresubmitPromptWarning( |
| 2778 | '%s:%d found MAYBE_ defined without corresponding test %s' |
| 2779 | % (f.LocalPath(), line_num, test_name))) |
| 2780 | return warnings |
| 2781 | |
[email protected] | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 2782 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2783 | def CheckDCHECK_IS_ONHasBraces(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2784 | """Checks to make sure DCHECK_IS_ON() does not skip the parentheses.""" |
| 2785 | errors = [] |
Kalvin Lee | 4a3b79de | 2022-05-26 16:00:16 | [diff] [blame] | 2786 | pattern = input_api.re.compile(r'\bDCHECK_IS_ON\b(?!\(\))', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2787 | input_api.re.MULTILINE) |
| 2788 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 2789 | if (not f.LocalPath().endswith(('.cc', '.mm', '.h'))): |
| 2790 | continue |
| 2791 | for lnum, line in f.ChangedContents(): |
| 2792 | if input_api.re.search(pattern, line): |
| 2793 | errors.append( |
| 2794 | output_api.PresubmitError(( |
| 2795 | '%s:%d: Use of DCHECK_IS_ON() must be written as "#if ' |
| 2796 | + 'DCHECK_IS_ON()", not forgetting the parentheses.') % |
| 2797 | (f.LocalPath(), lnum))) |
| 2798 | return errors |
danakj | 61c1aa2 | 2015-10-26 19:55:52 | [diff] [blame] | 2799 | |
| 2800 | |
Weilun Shi | a487fad | 2020-10-28 00:10:34 | [diff] [blame] | 2801 | # TODO(crbug/1138055): Reimplement CheckUmaHistogramChangesOnUpload check in a |
| 2802 | # more reliable way. See |
| 2803 | # https://chromium-review.googlesource.com/c/chromium/src/+/2500269 |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 2804 | |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2805 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2806 | def CheckFlakyTestUsage(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2807 | """Check that FlakyTest annotation is our own instead of the android one""" |
| 2808 | pattern = input_api.re.compile(r'import android.test.FlakyTest;') |
| 2809 | files = [] |
| 2810 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 2811 | if f.LocalPath().endswith('Test.java'): |
| 2812 | if pattern.search(input_api.ReadFile(f)): |
| 2813 | files.append(f) |
| 2814 | if len(files): |
| 2815 | return [ |
| 2816 | output_api.PresubmitError( |
| 2817 | 'Use org.chromium.base.test.util.FlakyTest instead of ' |
| 2818 | 'android.test.FlakyTest', files) |
| 2819 | ] |
| 2820 | return [] |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 2821 | |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2822 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2823 | def CheckNoDEPSGIT(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2824 | """Make sure .DEPS.git is never modified manually.""" |
| 2825 | if any(f.LocalPath().endswith('.DEPS.git') |
| 2826 | for f in input_api.AffectedFiles()): |
| 2827 | return [ |
| 2828 | output_api.PresubmitError( |
| 2829 | 'Never commit changes to .DEPS.git. This file is maintained by an\n' |
| 2830 | 'automated system based on what\'s in DEPS and your changes will be\n' |
| 2831 | 'overwritten.\n' |
| 2832 | 'See https://sites.google.com/a/chromium.org/dev/developers/how-tos/' |
| 2833 | 'get-the-code#Rolling_DEPS\n' |
| 2834 | 'for more information') |
| 2835 | ] |
| 2836 | return [] |
[email protected] | 2a8ac9c | 2011-10-19 17:20:44 | [diff] [blame] | 2837 | |
| 2838 | |
Sven Zheng | 76a79ea | 2022-12-21 21:25:24 | [diff] [blame] | 2839 | def CheckCrosApiNeedBrowserTest(input_api, output_api): |
| 2840 | """Check new crosapi should add browser test.""" |
| 2841 | has_new_crosapi = False |
| 2842 | has_browser_test = False |
| 2843 | for f in input_api.AffectedFiles(): |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 2844 | path = f.UnixLocalPath() |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2845 | if (path.startswith('chromeos/crosapi/mojom') |
| 2846 | and _IsMojomFile(input_api, path) and f.Action() == 'A'): |
Sven Zheng | 76a79ea | 2022-12-21 21:25:24 | [diff] [blame] | 2847 | has_new_crosapi = True |
| 2848 | if path.endswith('browsertest.cc') or path.endswith('browser_test.cc'): |
| 2849 | has_browser_test = True |
| 2850 | if has_new_crosapi and not has_browser_test: |
| 2851 | return [ |
| 2852 | output_api.PresubmitPromptWarning( |
| 2853 | 'You are adding a new crosapi, but there is no file ends with ' |
| 2854 | 'browsertest.cc file being added or modified. It is important ' |
| 2855 | 'to add crosapi browser test coverage to avoid version ' |
| 2856 | ' skew issues.\n' |
| 2857 | 'Check //docs/lacros/test_instructions.md for more information.' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2858 | ) |
Sven Zheng | 76a79ea | 2022-12-21 21:25:24 | [diff] [blame] | 2859 | ] |
| 2860 | return [] |
| 2861 | |
| 2862 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2863 | def CheckValidHostsInDEPSOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2864 | """Checks that DEPS file deps are from allowed_hosts.""" |
| 2865 | # Run only if DEPS file has been modified to annoy fewer bystanders. |
| 2866 | if all(f.LocalPath() != 'DEPS' for f in input_api.AffectedFiles()): |
| 2867 | return [] |
| 2868 | # Outsource work to gclient verify |
| 2869 | try: |
| 2870 | gclient_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 2871 | 'third_party', 'depot_tools', |
| 2872 | 'gclient.py') |
| 2873 | input_api.subprocess.check_output( |
Bruce Dawson | 8a43cf7 | 2022-05-13 17:10:32 | [diff] [blame] | 2874 | [input_api.python3_executable, gclient_path, 'verify'], |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2875 | stderr=input_api.subprocess.STDOUT) |
| 2876 | return [] |
| 2877 | except input_api.subprocess.CalledProcessError as error: |
| 2878 | return [ |
| 2879 | output_api.PresubmitError( |
| 2880 | 'DEPS file must have only git dependencies.', |
| 2881 | long_text=error.output) |
| 2882 | ] |
tandrii | ef66469 | 2014-09-23 14:51:47 | [diff] [blame] | 2883 | |
| 2884 | |
Mario Sanchez Prada | 2472cab | 2019-09-18 10:58:31 | [diff] [blame] | 2885 | def _GetMessageForMatchingType(input_api, affected_file, line_number, line, |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2886 | ban_rule): |
Allen Bauer | 8477868 | 2022-09-22 16:28:56 | [diff] [blame] | 2887 | """Helper method for checking for banned constructs. |
Mario Sanchez Prada | 2472cab | 2019-09-18 10:58:31 | [diff] [blame] | 2888 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2889 | Returns an string composed of the name of the file, the line number where the |
| 2890 | match has been found and the additional text passed as |message| in case the |
| 2891 | target type name matches the text inside the line passed as parameter. |
| 2892 | """ |
| 2893 | result = [] |
Peng Huang | 9c5949a0 | 2020-06-11 19:20:54 | [diff] [blame] | 2894 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2895 | # Ignore comments about banned types. |
| 2896 | if input_api.re.search(r"^ *//", line): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2897 | return result |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2898 | # A // nocheck comment will bypass this error. |
| 2899 | if line.endswith(" nocheck"): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2900 | return result |
| 2901 | |
| 2902 | matched = False |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2903 | if ban_rule.pattern[0:1] == '/': |
| 2904 | regex = ban_rule.pattern[1:] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2905 | if input_api.re.search(regex, line): |
| 2906 | matched = True |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2907 | elif ban_rule.pattern in line: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2908 | matched = True |
| 2909 | |
| 2910 | if matched: |
| 2911 | result.append(' %s:%d:' % (affected_file.LocalPath(), line_number)) |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2912 | for line in ban_rule.explanation: |
| 2913 | result.append(' %s' % line) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2914 | |
danakj | d18e889 | 2020-12-17 17:42:01 | [diff] [blame] | 2915 | return result |
Mario Sanchez Prada | 2472cab | 2019-09-18 10:58:31 | [diff] [blame] | 2916 | |
| 2917 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2918 | def CheckNoBannedFunctions(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2919 | """Make sure that banned functions are not used.""" |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2920 | results = [] |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2921 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2922 | def IsExcludedFile(affected_file, excluded_paths): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2923 | if not excluded_paths: |
| 2924 | return False |
| 2925 | |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 2926 | local_path = affected_file.UnixLocalPath() |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2927 | for item in excluded_paths: |
| 2928 | if input_api.re.match(item, local_path): |
| 2929 | return True |
| 2930 | return False |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2931 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2932 | def IsIosObjcFile(affected_file): |
| 2933 | local_path = affected_file.LocalPath() |
| 2934 | if input_api.os_path.splitext(local_path)[-1] not in ('.mm', '.m', |
| 2935 | '.h'): |
| 2936 | return False |
| 2937 | basename = input_api.os_path.basename(local_path) |
| 2938 | if 'ios' in basename.split('_'): |
| 2939 | return True |
| 2940 | for sep in (input_api.os_path.sep, input_api.os_path.altsep): |
| 2941 | if sep and 'ios' in local_path.split(sep): |
| 2942 | return True |
| 2943 | return False |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 2944 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2945 | def CheckForMatch(affected_file, line_num: int, line: str, |
| 2946 | ban_rule: BanRule): |
| 2947 | if IsExcludedFile(affected_file, ban_rule.excluded_paths): |
| 2948 | return |
| 2949 | |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 2950 | message = _GetMessageForMatchingType(input_api, f, line_num, line, |
| 2951 | ban_rule) |
| 2952 | if message: |
| 2953 | result_loc = [] |
| 2954 | if ban_rule.surface_as_gerrit_lint: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2955 | result_loc.append( |
| 2956 | output_api.PresubmitResultLocation( |
| 2957 | file_path=affected_file.LocalPath(), |
| 2958 | start_line=line_num, |
| 2959 | end_line=line_num, |
| 2960 | )) |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2961 | 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] | 2962 | results.append( |
| 2963 | output_api.PresubmitError('A banned function was used.\n' + |
| 2964 | '\n'.join(message), |
| 2965 | locations=result_loc)) |
| 2966 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2967 | else: |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 2968 | results.append( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2969 | output_api.PresubmitPromptWarning( |
| 2970 | 'A banned function was used.\n' + '\n'.join(message), |
| 2971 | locations=result_loc)) |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2972 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2973 | file_filter = lambda f: f.LocalPath().endswith(('.java')) |
| 2974 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 2975 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2976 | for ban_rule in _BANNED_JAVA_FUNCTIONS: |
| 2977 | CheckForMatch(f, line_num, line, ban_rule) |
Eric Stevenson | a9a98097 | 2017-09-23 00:04:41 | [diff] [blame] | 2978 | |
Clement Yan | 9b330cb | 2022-11-17 05:25:29 | [diff] [blame] | 2979 | file_filter = lambda f: f.LocalPath().endswith(('.js', '.ts')) |
| 2980 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 2981 | for line_num, line in f.ChangedContents(): |
| 2982 | for ban_rule in _BANNED_JAVASCRIPT_FUNCTIONS: |
| 2983 | CheckForMatch(f, line_num, line, ban_rule) |
| 2984 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2985 | file_filter = lambda f: f.LocalPath().endswith(('.mm', '.m', '.h')) |
| 2986 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 2987 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2988 | for ban_rule in _BANNED_OBJC_FUNCTIONS: |
| 2989 | CheckForMatch(f, line_num, line, ban_rule) |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2990 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2991 | for f in input_api.AffectedFiles(file_filter=IsIosObjcFile): |
| 2992 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2993 | for ban_rule in _BANNED_IOS_OBJC_FUNCTIONS: |
| 2994 | CheckForMatch(f, line_num, line, ban_rule) |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 2995 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2996 | egtest_filter = lambda f: f.LocalPath().endswith(('_egtest.mm')) |
| 2997 | for f in input_api.AffectedFiles(file_filter=egtest_filter): |
| 2998 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2999 | for ban_rule in _BANNED_IOS_EGTEST_FUNCTIONS: |
| 3000 | CheckForMatch(f, line_num, line, ban_rule) |
Peter K. Lee | 6c03ccff | 2019-07-15 14:40:05 | [diff] [blame] | 3001 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3002 | file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.h')) |
| 3003 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3004 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3005 | for ban_rule in _BANNED_CPP_FUNCTIONS: |
| 3006 | CheckForMatch(f, line_num, line, ban_rule) |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 3007 | |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 3008 | # As of 05/2024, iOS fully migrated ConsentLevel::kSync to kSignin, and |
| 3009 | # Android is in the process of preventing new users from entering kSync. |
| 3010 | # So the warning is restricted to those platforms. |
Riley Wong | 49be8a88 | 2025-02-27 00:38:23 | [diff] [blame] | 3011 | ios_pattern = input_api.re.compile(r'(^|[\W_])ios[\W_]') |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3012 | file_filter = lambda f: ( |
| 3013 | f.LocalPath().endswith(('.cc', '.mm', '.h')) and |
| 3014 | ('android' in f.LocalPath() or |
| 3015 | # Simply checking for an 'ios' substring would |
| 3016 | # catch unrelated cases, use a regex. |
| 3017 | ios_pattern.search(f.LocalPath()))) |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 3018 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3019 | for line_num, line in f.ChangedContents(): |
| 3020 | for ban_rule in _DEPRECATED_SYNC_CONSENT_CPP_FUNCTIONS: |
| 3021 | CheckForMatch(f, line_num, line, ban_rule) |
| 3022 | |
| 3023 | file_filter = lambda f: f.LocalPath().endswith(('.java')) |
| 3024 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3025 | for line_num, line in f.ChangedContents(): |
| 3026 | for ban_rule in _DEPRECATED_SYNC_CONSENT_JAVA_FUNCTIONS: |
| 3027 | CheckForMatch(f, line_num, line, ban_rule) |
| 3028 | |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 3029 | file_filter = lambda f: f.LocalPath().endswith(('.mojom')) |
| 3030 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3031 | for line_num, line in f.ChangedContents(): |
| 3032 | for ban_rule in _BANNED_MOJOM_PATTERNS: |
| 3033 | CheckForMatch(f, line_num, line, ban_rule) |
| 3034 | |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 3035 | return results |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 3036 | |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 3037 | |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 3038 | def _CheckAndroidNoBannedImports(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3039 | """Make sure that banned java imports are not used.""" |
| 3040 | errors = [] |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 3041 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3042 | file_filter = lambda f: f.LocalPath().endswith(('.java')) |
| 3043 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3044 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3045 | for ban_rule in _BANNED_JAVA_IMPORTS: |
| 3046 | # Consider merging this into the above function. There is no |
| 3047 | # real difference anymore other than helping with a little |
| 3048 | # bit of boilerplate text. Doing so means things like |
| 3049 | # `treat_as_error` will also be uniformly handled. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3050 | problems = _GetMessageForMatchingType(input_api, f, line_num, |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3051 | line, ban_rule) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3052 | if problems: |
| 3053 | errors.extend(problems) |
| 3054 | result = [] |
| 3055 | if (errors): |
| 3056 | result.append( |
| 3057 | output_api.PresubmitError('Banned imports were used.\n' + |
| 3058 | '\n'.join(errors))) |
| 3059 | return result |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 3060 | |
| 3061 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3062 | def CheckNoPragmaOnce(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3063 | """Make sure that banned functions are not used.""" |
| 3064 | files = [] |
| 3065 | pattern = input_api.re.compile(r'^#pragma\s+once', input_api.re.MULTILINE) |
| 3066 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 3067 | if not f.LocalPath().endswith('.h'): |
| 3068 | continue |
Bruce Dawson | 4c4c292 | 2022-05-02 18:07:33 | [diff] [blame] | 3069 | if f.LocalPath().endswith('com_imported_mstscax.h'): |
| 3070 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3071 | contents = input_api.ReadFile(f) |
| 3072 | if pattern.search(contents): |
| 3073 | files.append(f) |
[email protected] | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 3074 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3075 | if files: |
| 3076 | return [ |
| 3077 | output_api.PresubmitError( |
| 3078 | 'Do not use #pragma once in header files.\n' |
| 3079 | 'See http://www.chromium.org/developers/coding-style#TOC-File-headers', |
| 3080 | files) |
| 3081 | ] |
| 3082 | return [] |
[email protected] | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 3083 | |
[email protected] | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 3084 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3085 | def CheckNoTrinaryTrueFalse(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3086 | """Checks to make sure we don't introduce use of foo ? true : false.""" |
| 3087 | problems = [] |
| 3088 | pattern = input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)') |
| 3089 | for f in input_api.AffectedFiles(): |
| 3090 | if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')): |
| 3091 | continue |
[email protected] | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 3092 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3093 | for line_num, line in f.ChangedContents(): |
| 3094 | if pattern.match(line): |
| 3095 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
[email protected] | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 3096 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3097 | if not problems: |
| 3098 | return [] |
| 3099 | return [ |
| 3100 | output_api.PresubmitPromptWarning( |
| 3101 | 'Please consider avoiding the "? true : false" pattern if possible.\n' |
| 3102 | + '\n'.join(problems)) |
| 3103 | ] |
[email protected] | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 3104 | |
| 3105 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3106 | def CheckUnwantedDependencies(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3107 | """Runs checkdeps on #include and import statements added in this |
| 3108 | change. Breaking - rules is an error, breaking ! rules is a |
| 3109 | warning. |
| 3110 | """ |
| 3111 | # Return early if no relevant file types were modified. |
| 3112 | for f in input_api.AffectedFiles(): |
| 3113 | path = f.LocalPath() |
| 3114 | if (_IsCPlusPlusFile(input_api, path) or _IsProtoFile(input_api, path) |
| 3115 | or _IsJavaFile(input_api, path)): |
| 3116 | break |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 3117 | else: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3118 | return [] |
rhalavati | 08acd23 | 2017-04-03 07:23:28 | [diff] [blame] | 3119 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3120 | import sys |
| 3121 | # We need to wait until we have an input_api object and use this |
| 3122 | # roundabout construct to import checkdeps because this file is |
| 3123 | # eval-ed and thus doesn't have __file__. |
| 3124 | original_sys_path = sys.path |
| 3125 | try: |
| 3126 | sys.path = sys.path + [ |
| 3127 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 3128 | 'buildtools', 'checkdeps') |
| 3129 | ] |
| 3130 | import checkdeps |
| 3131 | from rules import Rule |
| 3132 | finally: |
| 3133 | # Restore sys.path to what it was before. |
| 3134 | sys.path = original_sys_path |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 3135 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3136 | added_includes = [] |
| 3137 | added_imports = [] |
| 3138 | added_java_imports = [] |
| 3139 | for f in input_api.AffectedFiles(): |
| 3140 | if _IsCPlusPlusFile(input_api, f.LocalPath()): |
| 3141 | changed_lines = [line for _, line in f.ChangedContents()] |
| 3142 | added_includes.append([f.AbsoluteLocalPath(), changed_lines]) |
| 3143 | elif _IsProtoFile(input_api, f.LocalPath()): |
| 3144 | changed_lines = [line for _, line in f.ChangedContents()] |
| 3145 | added_imports.append([f.AbsoluteLocalPath(), changed_lines]) |
| 3146 | elif _IsJavaFile(input_api, f.LocalPath()): |
| 3147 | changed_lines = [line for _, line in f.ChangedContents()] |
| 3148 | added_java_imports.append([f.AbsoluteLocalPath(), changed_lines]) |
Jinsuk Kim | 5a09267 | 2017-10-24 22:42:24 | [diff] [blame] | 3149 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3150 | deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath()) |
| 3151 | |
| 3152 | error_descriptions = [] |
| 3153 | warning_descriptions = [] |
| 3154 | error_subjects = set() |
| 3155 | warning_subjects = set() |
| 3156 | |
| 3157 | for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes( |
| 3158 | added_includes): |
| 3159 | path = input_api.os_path.relpath(path, input_api.PresubmitLocalPath()) |
| 3160 | description_with_path = '%s\n %s' % (path, rule_description) |
| 3161 | if rule_type == Rule.DISALLOW: |
| 3162 | error_descriptions.append(description_with_path) |
| 3163 | error_subjects.add("#includes") |
| 3164 | else: |
| 3165 | warning_descriptions.append(description_with_path) |
| 3166 | warning_subjects.add("#includes") |
| 3167 | |
| 3168 | for path, rule_type, rule_description in deps_checker.CheckAddedProtoImports( |
| 3169 | added_imports): |
| 3170 | path = input_api.os_path.relpath(path, input_api.PresubmitLocalPath()) |
| 3171 | description_with_path = '%s\n %s' % (path, rule_description) |
| 3172 | if rule_type == Rule.DISALLOW: |
| 3173 | error_descriptions.append(description_with_path) |
| 3174 | error_subjects.add("imports") |
| 3175 | else: |
| 3176 | warning_descriptions.append(description_with_path) |
| 3177 | warning_subjects.add("imports") |
| 3178 | |
| 3179 | for path, rule_type, rule_description in deps_checker.CheckAddedJavaImports( |
| 3180 | added_java_imports, _JAVA_MULTIPLE_DEFINITION_EXCLUDED_PATHS): |
| 3181 | path = input_api.os_path.relpath(path, input_api.PresubmitLocalPath()) |
| 3182 | description_with_path = '%s\n %s' % (path, rule_description) |
| 3183 | if rule_type == Rule.DISALLOW: |
| 3184 | error_descriptions.append(description_with_path) |
| 3185 | error_subjects.add("imports") |
| 3186 | else: |
| 3187 | warning_descriptions.append(description_with_path) |
| 3188 | warning_subjects.add("imports") |
| 3189 | |
| 3190 | results = [] |
| 3191 | if error_descriptions: |
| 3192 | results.append( |
| 3193 | output_api.PresubmitError( |
| 3194 | 'You added one or more %s that violate checkdeps rules.' % |
| 3195 | " and ".join(error_subjects), error_descriptions)) |
| 3196 | if warning_descriptions: |
| 3197 | results.append( |
| 3198 | output_api.PresubmitPromptOrNotify( |
| 3199 | 'You added one or more %s of files that are temporarily\n' |
| 3200 | 'allowed but being removed. Can you avoid introducing the\n' |
| 3201 | '%s? See relevant DEPS file(s) for details and contacts.' % |
| 3202 | (" and ".join(warning_subjects), "/".join(warning_subjects)), |
| 3203 | warning_descriptions)) |
| 3204 | return results |
[email protected] | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 3205 | |
| 3206 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3207 | def CheckFilePermissions(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3208 | """Check that all files have their permissions properly set.""" |
| 3209 | if input_api.platform == 'win32': |
| 3210 | return [] |
| 3211 | checkperms_tool = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 3212 | 'tools', 'checkperms', |
| 3213 | 'checkperms.py') |
| 3214 | args = [ |
Bruce Dawson | 8a43cf7 | 2022-05-13 17:10:32 | [diff] [blame] | 3215 | input_api.python3_executable, checkperms_tool, '--root', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3216 | input_api.change.RepositoryRoot() |
| 3217 | ] |
| 3218 | with input_api.CreateTemporaryFile() as file_list: |
| 3219 | for f in input_api.AffectedFiles(): |
| 3220 | # checkperms.py file/directory arguments must be relative to the |
| 3221 | # repository. |
| 3222 | file_list.write((f.LocalPath() + '\n').encode('utf8')) |
| 3223 | file_list.close() |
| 3224 | args += ['--file-list', file_list.name] |
| 3225 | try: |
| 3226 | input_api.subprocess.check_output(args) |
| 3227 | return [] |
| 3228 | except input_api.subprocess.CalledProcessError as error: |
| 3229 | return [ |
| 3230 | output_api.PresubmitError('checkperms.py failed:', |
| 3231 | long_text=error.output.decode( |
| 3232 | 'utf-8', 'ignore')) |
| 3233 | ] |
[email protected] | fbcafe5a | 2012-08-08 15:31:22 | [diff] [blame] | 3234 | |
| 3235 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3236 | def CheckNoAuraWindowPropertyHInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3237 | """Makes sure we don't include ui/aura/window_property.h |
| 3238 | in header files. |
| 3239 | """ |
| 3240 | pattern = input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"') |
| 3241 | errors = [] |
| 3242 | for f in input_api.AffectedFiles(): |
| 3243 | if not f.LocalPath().endswith('.h'): |
| 3244 | continue |
| 3245 | for line_num, line in f.ChangedContents(): |
| 3246 | if pattern.match(line): |
| 3247 | errors.append(' %s:%d' % (f.LocalPath(), line_num)) |
[email protected] | c8278b3 | 2012-10-30 20:35:49 | [diff] [blame] | 3248 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3249 | results = [] |
| 3250 | if errors: |
| 3251 | results.append( |
| 3252 | output_api.PresubmitError( |
| 3253 | 'Header files should not include ui/aura/window_property.h', |
| 3254 | errors)) |
| 3255 | return results |
[email protected] | c8278b3 | 2012-10-30 20:35:49 | [diff] [blame] | 3256 | |
| 3257 | |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 3258 | def CheckNoInternalHeapIncludes(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3259 | """Makes sure we don't include any headers from |
| 3260 | third_party/blink/renderer/platform/heap/impl or |
| 3261 | third_party/blink/renderer/platform/heap/v8_wrapper from files outside of |
| 3262 | third_party/blink/renderer/platform/heap |
| 3263 | """ |
| 3264 | impl_pattern = input_api.re.compile( |
| 3265 | r'^\s*#include\s*"third_party/blink/renderer/platform/heap/impl/.*"') |
| 3266 | v8_wrapper_pattern = input_api.re.compile( |
| 3267 | r'^\s*#include\s*"third_party/blink/renderer/platform/heap/v8_wrapper/.*"' |
| 3268 | ) |
| 3269 | file_filter = lambda f: not input_api.re.match( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3270 | r"^third_party/blink/renderer/platform/heap/.*", f.UnixLocalPath()) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3271 | errors = [] |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 3272 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3273 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 3274 | for line_num, line in f.ChangedContents(): |
| 3275 | if impl_pattern.match(line) or v8_wrapper_pattern.match(line): |
| 3276 | errors.append(' %s:%d' % (f.LocalPath(), line_num)) |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 3277 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3278 | results = [] |
| 3279 | if errors: |
| 3280 | results.append( |
| 3281 | output_api.PresubmitError( |
| 3282 | 'Do not include files from third_party/blink/renderer/platform/heap/impl' |
| 3283 | ' or third_party/blink/renderer/platform/heap/v8_wrapper. Use the ' |
| 3284 | 'relevant counterparts from third_party/blink/renderer/platform/heap', |
| 3285 | errors)) |
| 3286 | return results |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 3287 | |
| 3288 | |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3289 | def _CheckForVersionControlConflictsInFile(input_api, f): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3290 | pattern = input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$') |
| 3291 | errors = [] |
| 3292 | for line_num, line in f.ChangedContents(): |
| 3293 | if f.LocalPath().endswith(('.md', '.rst', '.txt')): |
| 3294 | # First-level headers in markdown look a lot like version control |
| 3295 | # conflict markers. http://daringfireball.net/projects/markdown/basics |
| 3296 | continue |
| 3297 | if pattern.match(line): |
| 3298 | errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line)) |
| 3299 | return errors |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3300 | |
| 3301 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3302 | def CheckForVersionControlConflicts(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3303 | """Usually this is not intentional and will cause a compile failure.""" |
| 3304 | errors = [] |
| 3305 | for f in input_api.AffectedFiles(): |
| 3306 | errors.extend(_CheckForVersionControlConflictsInFile(input_api, f)) |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3307 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3308 | results = [] |
| 3309 | if errors: |
| 3310 | results.append( |
| 3311 | output_api.PresubmitError( |
| 3312 | 'Version control conflict markers found, please resolve.', |
| 3313 | errors)) |
| 3314 | return results |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3315 | |
Wei-Yin Chen (陳威尹) | f799d44 | 2018-07-31 02:20:20 | [diff] [blame] | 3316 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3317 | def CheckGoogleSupportAnswerUrlOnUpload(input_api, output_api): |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 3318 | pattern = input_api.re.compile(r'support\.google\.com\/chrome.*/answer') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3319 | errors = [] |
| 3320 | for f in input_api.AffectedFiles(): |
| 3321 | for line_num, line in f.ChangedContents(): |
| 3322 | if pattern.search(line): |
| 3323 | errors.append(' %s:%d %s' % (f.LocalPath(), line_num, line)) |
estade | e17314a0 | 2017-01-12 16:22:16 | [diff] [blame] | 3324 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3325 | results = [] |
| 3326 | if errors: |
| 3327 | results.append( |
| 3328 | output_api.PresubmitPromptWarning( |
| 3329 | 'Found Google support URL addressed by answer number. Please replace ' |
| 3330 | 'with a p= identifier instead. See crbug.com/679462\n', |
| 3331 | errors)) |
| 3332 | return results |
estade | e17314a0 | 2017-01-12 16:22:16 | [diff] [blame] | 3333 | |
[email protected] | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3334 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3335 | def CheckHardcodedGoogleHostsInLowerLayers(input_api, output_api): |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3336 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3337 | def FilterFile(affected_file): |
| 3338 | """Filter function for use with input_api.AffectedSourceFiles, |
| 3339 | below. This filters out everything except non-test files from |
| 3340 | top-level directories that generally speaking should not hard-code |
| 3341 | service URLs (e.g. src/android_webview/, src/content/ and others). |
| 3342 | """ |
| 3343 | return input_api.FilterSourceFile( |
| 3344 | affected_file, |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3345 | files_to_check=[r'^(android_webview|base|content|net)/.*'], |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3346 | files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 3347 | input_api.DEFAULT_FILES_TO_SKIP)) |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 3348 | |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 3349 | base_pattern = (r'"[^"]*(google|googleapis|googlezip|googledrive|appspot)' |
| 3350 | r'\.(com|net)[^"]*"') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3351 | comment_pattern = input_api.re.compile('//.*%s' % base_pattern) |
| 3352 | pattern = input_api.re.compile(base_pattern) |
| 3353 | problems = [] # items are (filename, line_number, line) |
| 3354 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 3355 | for line_num, line in f.ChangedContents(): |
| 3356 | if not comment_pattern.search(line) and pattern.search(line): |
| 3357 | problems.append((f.LocalPath(), line_num, line)) |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 3358 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3359 | if problems: |
| 3360 | return [ |
| 3361 | output_api.PresubmitPromptOrNotify( |
| 3362 | 'Most layers below src/chrome/ should not hardcode service URLs.\n' |
| 3363 | 'Are you sure this is correct?', [ |
| 3364 | ' %s:%d: %s' % (problem[0], problem[1], problem[2]) |
| 3365 | for problem in problems |
| 3366 | ]) |
| 3367 | ] |
| 3368 | else: |
| 3369 | return [] |
[email protected] | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 3370 | |
| 3371 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3372 | def CheckChromeOsSyncedPrefRegistration(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3373 | """Warns if Chrome OS C++ files register syncable prefs as browser prefs.""" |
James Cook | 6b6597c | 2019-11-06 22:05:29 | [diff] [blame] | 3374 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3375 | def FileFilter(affected_file): |
| 3376 | """Includes directories known to be Chrome OS only.""" |
| 3377 | return input_api.FilterSourceFile( |
| 3378 | affected_file, |
| 3379 | files_to_check=( |
| 3380 | '^ash/', |
| 3381 | '^chromeos/', # Top-level src/chromeos. |
| 3382 | '.*/chromeos/', # Any path component. |
| 3383 | '^components/arc', |
| 3384 | '^components/exo'), |
| 3385 | files_to_skip=(input_api.DEFAULT_FILES_TO_SKIP)) |
James Cook | 6b6597c | 2019-11-06 22:05:29 | [diff] [blame] | 3386 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3387 | prefs = [] |
| 3388 | priority_prefs = [] |
| 3389 | for f in input_api.AffectedFiles(file_filter=FileFilter): |
| 3390 | for line_num, line in f.ChangedContents(): |
| 3391 | if input_api.re.search('PrefRegistrySyncable::SYNCABLE_PREF', |
| 3392 | line): |
| 3393 | prefs.append(' %s:%d:' % (f.LocalPath(), line_num)) |
| 3394 | prefs.append(' %s' % line) |
| 3395 | if input_api.re.search( |
| 3396 | 'PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF', line): |
| 3397 | priority_prefs.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 3398 | priority_prefs.append(' %s' % line) |
| 3399 | |
| 3400 | results = [] |
| 3401 | if (prefs): |
| 3402 | results.append( |
| 3403 | output_api.PresubmitPromptWarning( |
| 3404 | 'Preferences were registered as SYNCABLE_PREF and will be controlled ' |
| 3405 | 'by browser sync settings. If these prefs should be controlled by OS ' |
| 3406 | 'sync settings use SYNCABLE_OS_PREF instead.\n' + |
| 3407 | '\n'.join(prefs))) |
| 3408 | if (priority_prefs): |
| 3409 | results.append( |
| 3410 | output_api.PresubmitPromptWarning( |
| 3411 | 'Preferences were registered as SYNCABLE_PRIORITY_PREF and will be ' |
| 3412 | 'controlled by browser sync settings. If these prefs should be ' |
| 3413 | 'controlled by OS sync settings use SYNCABLE_OS_PRIORITY_PREF ' |
| 3414 | 'instead.\n' + '\n'.join(prefs))) |
| 3415 | return results |
James Cook | 6b6597c | 2019-11-06 22:05:29 | [diff] [blame] | 3416 | |
| 3417 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3418 | def CheckNoAbbreviationInPngFileName(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3419 | """Makes sure there are no abbreviations in the name of PNG files. |
| 3420 | The native_client_sdk directory is excluded because it has auto-generated PNG |
| 3421 | files for documentation. |
| 3422 | """ |
| 3423 | errors = [] |
Yuanqing Zhu | 9eef0283 | 2022-12-04 14:42:17 | [diff] [blame] | 3424 | files_to_check = [r'.*\.png$'] |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3425 | files_to_skip = [ |
| 3426 | r'^native_client_sdk/', |
| 3427 | r'^services/test/', |
| 3428 | r'^third_party/blink/web_tests/', |
| 3429 | ] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3430 | file_filter = lambda f: input_api.FilterSourceFile( |
| 3431 | f, files_to_check=files_to_check, files_to_skip=files_to_skip) |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 3432 | abbreviation = input_api.re.compile(r'.+_[a-z]\.png|.+_[a-z]_.*\.png') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3433 | for f in input_api.AffectedFiles(include_deletes=False, |
| 3434 | file_filter=file_filter): |
Yuanqing Zhu | 9eef0283 | 2022-12-04 14:42:17 | [diff] [blame] | 3435 | file_name = input_api.os_path.split(f.LocalPath())[1] |
| 3436 | if abbreviation.search(file_name): |
| 3437 | errors.append(' %s' % f.LocalPath()) |
[email protected] | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 3438 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3439 | results = [] |
| 3440 | if errors: |
| 3441 | results.append( |
| 3442 | output_api.PresubmitError( |
| 3443 | 'The name of PNG files should not have abbreviations. \n' |
| 3444 | 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n' |
| 3445 | 'Contact [email protected] if you have questions.', errors)) |
| 3446 | return results |
[email protected] | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 3447 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3448 | |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 3449 | def CheckNoProductIconsAddedToPublicRepo(input_api, output_api): |
| 3450 | """Heuristically identifies product icons based on their file name and reminds |
| 3451 | contributors not to add them to the Chromium repository. |
| 3452 | """ |
Peter Kotwicz | f634d07 | 2025-04-28 22:48:15 | [diff] [blame] | 3453 | |
| 3454 | if input_api.change.RepositoryRoot().endswith('clank'): |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3455 | # TODO(crbug.com/414435241): Change check to compute whether change |
| 3456 | # belongs to internal repository instead of relying on string matching. |
| 3457 | return [] |
Peter Kotwicz | f634d07 | 2025-04-28 22:48:15 | [diff] [blame] | 3458 | |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 3459 | errors = [] |
| 3460 | files_to_check = [r'.*google.*\.png$|.*google.*\.svg$|.*google.*\.icon$'] |
| 3461 | file_filter = lambda f: input_api.FilterSourceFile( |
| 3462 | f, files_to_check=files_to_check) |
| 3463 | for f in input_api.AffectedFiles(include_deletes=False, |
| 3464 | file_filter=file_filter): |
| 3465 | errors.append(' %s' % f.LocalPath()) |
| 3466 | |
| 3467 | results = [] |
| 3468 | if errors: |
Bruce Dawson | 3bcf0c9 | 2022-08-12 00:03:08 | [diff] [blame] | 3469 | # Give warnings instead of errors on presubmit --all and presubmit |
| 3470 | # --files. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3471 | message_type = (output_api.PresubmitNotifyResult |
| 3472 | if input_api.no_diffs else output_api.PresubmitError) |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 3473 | results.append( |
Bruce Dawson | 3bcf0c9 | 2022-08-12 00:03:08 | [diff] [blame] | 3474 | message_type( |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 3475 | 'Trademarked images should not be added to the public repo. ' |
| 3476 | 'See crbug.com/944754', errors)) |
| 3477 | return results |
| 3478 | |
[email protected] | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 3479 | |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3480 | def _ExtractAddRulesFromParsedDeps(parsed_deps): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3481 | """Extract the rules that add dependencies from a parsed DEPS file. |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3482 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3483 | Args: |
| 3484 | parsed_deps: the locals dictionary from evaluating the DEPS file.""" |
| 3485 | add_rules = set() |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3486 | add_rules.update([ |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3487 | rule[1:] for rule in parsed_deps.get('include_rules', []) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3488 | if rule.startswith('+') or rule.startswith('!') |
| 3489 | ]) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3490 | for _, rules in parsed_deps.get('specific_include_rules', {}).items(): |
| 3491 | add_rules.update([ |
| 3492 | rule[1:] for rule in rules |
| 3493 | if rule.startswith('+') or rule.startswith('!') |
| 3494 | ]) |
| 3495 | return add_rules |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3496 | |
| 3497 | |
| 3498 | def _ParseDeps(contents): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3499 | """Simple helper for parsing DEPS files.""" |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3500 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3501 | # Stubs for handling special syntax in the root DEPS file. |
| 3502 | class _VarImpl: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3503 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3504 | def __init__(self, local_scope): |
| 3505 | self._local_scope = local_scope |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3506 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3507 | def Lookup(self, var_name): |
| 3508 | """Implements the Var syntax.""" |
| 3509 | try: |
| 3510 | return self._local_scope['vars'][var_name] |
| 3511 | except KeyError: |
| 3512 | raise Exception('Var is not defined: %s' % var_name) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3513 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3514 | local_scope = {} |
| 3515 | global_scope = { |
| 3516 | 'Var': _VarImpl(local_scope).Lookup, |
| 3517 | 'Str': str, |
| 3518 | } |
Dirk Pranke | 1b9e0638 | 2021-05-14 01:16:22 | [diff] [blame] | 3519 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3520 | exec(contents, global_scope, local_scope) |
| 3521 | return local_scope |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3522 | |
| 3523 | |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3524 | def _FindAllDepsFilesForSubpath(input_api, subpath): |
| 3525 | ret = [] |
| 3526 | while subpath: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3527 | cur = input_api.os_path.join(input_api.change.RepositoryRoot(), |
| 3528 | subpath, 'DEPS') |
Joanna Wang | 130e7bdd | 2024-12-10 17:39:03 | [diff] [blame] | 3529 | if input_api.os_path.isfile(cur): |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3530 | ret.append(cur) |
| 3531 | subpath = input_api.os_path.dirname(subpath) |
| 3532 | return ret |
| 3533 | |
| 3534 | |
| 3535 | def _FindAddedDepsThatRequireReview(input_api, depended_on_paths): |
| 3536 | """Filters to those whose DEPS set new_usages_require_review=True""" |
| 3537 | ret = set() |
| 3538 | cache = {} |
| 3539 | for target_path in depended_on_paths: |
| 3540 | for subpath in _FindAllDepsFilesForSubpath(input_api, target_path): |
| 3541 | config = cache.get(subpath) |
| 3542 | if config is None: |
| 3543 | config = _ParseDeps(input_api.ReadFile(subpath)) |
| 3544 | cache[subpath] = config |
| 3545 | if config.get('new_usages_require_review'): |
| 3546 | ret.add(target_path) |
| 3547 | break |
| 3548 | return ret |
| 3549 | |
| 3550 | |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3551 | def _CalculateAddedDeps(os_path, old_contents, new_contents): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3552 | """Helper method for CheckAddedDepsHaveTargetApprovals. Returns |
| 3553 | a set of DEPS entries that we should look up. |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 3554 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3555 | For a directory (rather than a specific filename) we fake a path to |
| 3556 | a specific filename by adding /DEPS. This is chosen as a file that |
| 3557 | will seldom or never be subject to per-file include_rules. |
| 3558 | """ |
| 3559 | # We ignore deps entries on auto-generated directories. |
| 3560 | AUTO_GENERATED_DIRS = ['grit', 'jni'] |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 3561 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3562 | old_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(old_contents)) |
| 3563 | new_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(new_contents)) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3564 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3565 | added_deps = new_deps.difference(old_deps) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3566 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3567 | results = set() |
| 3568 | for added_dep in added_deps: |
| 3569 | if added_dep.split('/')[0] in AUTO_GENERATED_DIRS: |
| 3570 | continue |
| 3571 | # Assume that a rule that ends in .h is a rule for a specific file. |
| 3572 | if added_dep.endswith('.h'): |
| 3573 | results.add(added_dep) |
| 3574 | else: |
| 3575 | results.add(os_path.join(added_dep, 'DEPS')) |
| 3576 | return results |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 3577 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3578 | |
Stephanie Kim | ec4f55a | 2024-04-24 16:54:02 | [diff] [blame] | 3579 | def CheckForNewDEPSDownloadFromGoogleStorageHooks(input_api, output_api): |
| 3580 | """Checks that there are no new download_from_google_storage hooks""" |
| 3581 | for f in input_api.AffectedFiles(include_deletes=False): |
| 3582 | if f.LocalPath() == 'DEPS': |
| 3583 | old_hooks = _ParseDeps('\n'.join(f.OldContents()))['hooks'] |
| 3584 | new_hooks = _ParseDeps('\n'.join(f.NewContents()))['hooks'] |
| 3585 | old_name_to_hook = {hook['name']: hook for hook in old_hooks} |
| 3586 | new_name_to_hook = {hook['name']: hook for hook in new_hooks} |
| 3587 | added_hook_names = set(new_name_to_hook.keys()) - set( |
| 3588 | old_name_to_hook.keys()) |
| 3589 | if not added_hook_names: |
| 3590 | return [] |
| 3591 | new_download_from_google_storage_hooks = [] |
| 3592 | for new_hook in added_hook_names: |
| 3593 | hook = new_name_to_hook[new_hook] |
| 3594 | action_cmd = hook['action'] |
| 3595 | if any('download_from_google_storage' in arg |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3596 | for arg in action_cmd): |
Stephanie Kim | ec4f55a | 2024-04-24 16:54:02 | [diff] [blame] | 3597 | new_download_from_google_storage_hooks.append(new_hook) |
| 3598 | if new_download_from_google_storage_hooks: |
| 3599 | return [ |
| 3600 | output_api.PresubmitError( |
| 3601 | 'Please do not add new download_from_google_storage ' |
| 3602 | 'hooks. Instead, add a `gcs` dep_type entry to `deps`. ' |
| 3603 | 'See https://chromium.googlesource.com/chromium/src.git' |
| 3604 | '/+/refs/heads/main/docs/gcs_dependencies.md for more ' |
| 3605 | 'info. Added hooks:', |
| 3606 | items=new_download_from_google_storage_hooks) |
| 3607 | ] |
| 3608 | return [] |
| 3609 | |
[email protected] | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 3610 | |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3611 | def CheckEachPerfettoTestDataFileHasDepsEntry(input_api, output_api): |
| 3612 | test_data_filter = lambda f: input_api.FilterSourceFile( |
Rasika Navarange | 08e54216 | 2024-05-31 13:31:26 | [diff] [blame] | 3613 | f, files_to_check=[r'^base/tracing/test/data_sha256/.*\.sha256']) |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3614 | if not any(input_api.AffectedFiles(file_filter=test_data_filter)): |
| 3615 | return [] |
| 3616 | |
| 3617 | # Find DEPS entry |
| 3618 | deps_entry = [] |
Rasika Navarange | 277cd66 | 2024-06-04 10:14:59 | [diff] [blame] | 3619 | old_deps_entry = [] |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3620 | for f in input_api.AffectedFiles(include_deletes=False): |
| 3621 | if f.LocalPath() == 'DEPS': |
| 3622 | new_deps = _ParseDeps('\n'.join(f.NewContents()))['deps'] |
| 3623 | deps_entry = new_deps['src/base/tracing/test/data'] |
Rasika Navarange | 277cd66 | 2024-06-04 10:14:59 | [diff] [blame] | 3624 | old_deps = _ParseDeps('\n'.join(f.OldContents()))['deps'] |
| 3625 | old_deps_entry = old_deps['src/base/tracing/test/data'] |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3626 | if not deps_entry: |
Rasika Navarange | 08e54216 | 2024-05-31 13:31:26 | [diff] [blame] | 3627 | # TODO(312895063):Add back error when .sha256 files have been moved. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3628 | return [ |
| 3629 | output_api.PresubmitError( |
| 3630 | 'You must update the DEPS file when you update a ' |
| 3631 | '.sha256 file in base/tracing/test/data_sha256') |
| 3632 | ] |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3633 | |
| 3634 | output = [] |
| 3635 | for f in input_api.AffectedFiles(file_filter=test_data_filter): |
| 3636 | objects = deps_entry['objects'] |
| 3637 | if not f.NewContents(): |
| 3638 | # Deleted file so check that DEPS entry removed |
| 3639 | sha256_from_file = f.OldContents()[0] |
| 3640 | object_entry = next( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3641 | (item |
| 3642 | for item in objects if item["sha256sum"] == sha256_from_file), |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3643 | None) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3644 | old_entry = next((item for item in old_deps_entry['objects'] |
| 3645 | if item["sha256sum"] == sha256_from_file), None) |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3646 | if object_entry: |
Rasika Navarange | 277cd66 | 2024-06-04 10:14:59 | [diff] [blame] | 3647 | # Allow renaming of objects with the same hash |
| 3648 | if object_entry['object_name'] != old_entry['object_name']: |
| 3649 | continue |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3650 | output.append( |
| 3651 | output_api.PresubmitError( |
| 3652 | 'You deleted %s so you must also remove the corresponding DEPS entry.' |
| 3653 | % f.LocalPath())) |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3654 | continue |
| 3655 | |
| 3656 | sha256_from_file = f.NewContents()[0] |
| 3657 | object_entry = next( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3658 | (item |
| 3659 | for item in objects if item["sha256sum"] == sha256_from_file), |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3660 | None) |
| 3661 | if not object_entry: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3662 | output.append( |
| 3663 | output_api.PresubmitError( |
| 3664 | 'No corresponding DEPS entry found for %s. ' |
| 3665 | 'Run `base/tracing/test/test_data.py get_deps --filepath %s` ' |
| 3666 | 'to generate the DEPS entry.' % |
| 3667 | (f.LocalPath(), f.LocalPath()))) |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3668 | |
| 3669 | if output: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3670 | output.append( |
| 3671 | output_api.PresubmitError( |
| 3672 | 'The DEPS entry for `src/base/tracing/test/data` in the DEPS file has not been ' |
| 3673 | 'updated properly. Run `base/tracing/test/test_data.py get_all_deps` to see what ' |
| 3674 | 'the DEPS entry should look like.')) |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3675 | return output |
| 3676 | |
| 3677 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3678 | def CheckAddedDepsHaveTargetApprovals(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3679 | """When a dependency prefixed with + is added to a DEPS file, we |
| 3680 | want to make sure that the change is reviewed by an OWNER of the |
| 3681 | target file or directory, to avoid layering violations from being |
| 3682 | introduced. This check verifies that this happens. |
| 3683 | """ |
| 3684 | # We rely on Gerrit's code-owners to check approvals. |
| 3685 | # input_api.gerrit is always set for Chromium, but other projects |
| 3686 | # might not use Gerrit. |
Bruce Dawson | 344ab26 | 2022-06-04 11:35:10 | [diff] [blame] | 3687 | if not input_api.gerrit or input_api.no_diffs: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3688 | return [] |
Bruce Dawson | b357aeb | 2022-08-09 15:38:30 | [diff] [blame] | 3689 | if 'PRESUBMIT_SKIP_NETWORK' in input_api.environ: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3690 | return [] |
Bruce Dawson | b357aeb | 2022-08-09 15:38:30 | [diff] [blame] | 3691 | try: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3692 | if (input_api.change.issue |
| 3693 | and input_api.gerrit.IsOwnersOverrideApproved( |
| 3694 | input_api.change.issue)): |
Bruce Dawson | b357aeb | 2022-08-09 15:38:30 | [diff] [blame] | 3695 | # Skip OWNERS check when Owners-Override label is approved. This is |
| 3696 | # intended for global owners, trusted bots, and on-call sheriffs. |
| 3697 | # Review is still required for these changes. |
| 3698 | return [] |
| 3699 | except Exception as e: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3700 | return [ |
| 3701 | output_api.PresubmitPromptWarning( |
| 3702 | 'Failed to retrieve owner override status - %s' % str(e)) |
| 3703 | ] |
Edward Lesmes | 6fba5108 | 2021-01-20 04:20:23 | [diff] [blame] | 3704 | |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3705 | # A set of paths (that might not exist) that are being added as DEPS |
| 3706 | # (via lines like "+foo/bar/baz"). |
| 3707 | depended_on_paths = set() |
jochen | 53efcdd | 2016-01-29 05:09:24 | [diff] [blame] | 3708 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3709 | file_filter = lambda f: not input_api.re.match(r"^third_party/blink/.*", |
| 3710 | f.UnixLocalPath()) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3711 | for f in input_api.AffectedFiles(include_deletes=False, |
| 3712 | file_filter=file_filter): |
| 3713 | filename = input_api.os_path.basename(f.LocalPath()) |
| 3714 | if filename == 'DEPS': |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3715 | depended_on_paths.update( |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3716 | _CalculateAddedDeps(input_api.os_path, |
| 3717 | '\n'.join(f.OldContents()), |
| 3718 | '\n'.join(f.NewContents()))) |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3719 | |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3720 | # Requiring reviews is opt-in as of https://crbug.com/365797506 |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3721 | depended_on_paths = _FindAddedDepsThatRequireReview( |
| 3722 | input_api, depended_on_paths) |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3723 | if not depended_on_paths: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3724 | return [] |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3725 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3726 | if input_api.is_committing: |
| 3727 | if input_api.tbr: |
| 3728 | return [ |
| 3729 | output_api.PresubmitNotifyResult( |
| 3730 | '--tbr was specified, skipping OWNERS check for DEPS additions' |
| 3731 | ) |
| 3732 | ] |
Daniel Cheng | 3008dc1 | 2022-05-13 04:02:11 | [diff] [blame] | 3733 | # TODO(dcheng): Make this generate an error on dry runs if the reviewer |
| 3734 | # is not added, to prevent review serialization. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3735 | if input_api.dry_run: |
| 3736 | return [ |
| 3737 | output_api.PresubmitNotifyResult( |
| 3738 | 'This is a dry run, skipping OWNERS check for DEPS additions' |
| 3739 | ) |
| 3740 | ] |
| 3741 | if not input_api.change.issue: |
| 3742 | return [ |
| 3743 | output_api.PresubmitError( |
| 3744 | "DEPS approval by OWNERS check failed: this change has " |
| 3745 | "no change number, so we can't check it for approvals.") |
| 3746 | ] |
| 3747 | output = output_api.PresubmitError |
[email protected] | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 3748 | else: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3749 | output = output_api.PresubmitNotifyResult |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3750 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3751 | owner_email, reviewers = ( |
| 3752 | input_api.canned_checks.GetCodereviewOwnerAndReviewers( |
| 3753 | input_api, None, approval_needed=input_api.is_committing)) |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3754 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3755 | owner_email = owner_email or input_api.change.author_email |
| 3756 | |
| 3757 | approval_status = input_api.owners_client.GetFilesApprovalStatus( |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3758 | depended_on_paths, reviewers.union([owner_email]), []) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3759 | missing_files = [ |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3760 | p for p in depended_on_paths |
| 3761 | if approval_status[p] != input_api.owners_client.APPROVED |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3762 | ] |
| 3763 | |
| 3764 | # We strip the /DEPS part that was added by |
| 3765 | # _FilesToCheckForIncomingDeps to fake a path to a file in a |
| 3766 | # directory. |
| 3767 | def StripDeps(path): |
| 3768 | start_deps = path.rfind('/DEPS') |
| 3769 | if start_deps != -1: |
| 3770 | return path[:start_deps] |
| 3771 | else: |
| 3772 | return path |
| 3773 | |
Scott Lee | bf6a094 | 2024-06-26 22:59:39 | [diff] [blame] | 3774 | submodule_paths = set(input_api.ListSubmodules()) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3775 | |
Scott Lee | bf6a094 | 2024-06-26 22:59:39 | [diff] [blame] | 3776 | def is_from_submodules(path, submodule_paths): |
| 3777 | path = input_api.os_path.normpath(path) |
| 3778 | while path: |
| 3779 | if path in submodule_paths: |
| 3780 | return True |
| 3781 | |
| 3782 | # All deps should be a relative path from the checkout. |
| 3783 | # i.e., shouldn't start with "/" or "c:\", for example. |
| 3784 | # |
| 3785 | # That said, this is to prevent an infinite loop, just in case |
| 3786 | # an input dep path starts with "/", because |
| 3787 | # os.path.dirname("/") => "/" |
| 3788 | parent = input_api.os_path.dirname(path) |
| 3789 | if parent == path: |
| 3790 | break |
| 3791 | path = parent |
| 3792 | |
| 3793 | return False |
| 3794 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3795 | unapproved_dependencies = [ |
| 3796 | "'+%s'," % StripDeps(path) for path in missing_files |
Scott Lee | bf6a094 | 2024-06-26 22:59:39 | [diff] [blame] | 3797 | # if a newly added dep is from a submodule, it becomes trickier |
| 3798 | # to get suggested owners, especially it is from a different host. |
| 3799 | # |
| 3800 | # skip the review enforcement for cross-repo deps. |
| 3801 | if not is_from_submodules(path, submodule_paths) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3802 | ] |
| 3803 | |
| 3804 | if unapproved_dependencies: |
| 3805 | output_list = [ |
| 3806 | output( |
| 3807 | 'You need LGTM from owners of depends-on paths in DEPS that were ' |
| 3808 | 'modified in this CL:\n %s' % |
| 3809 | '\n '.join(sorted(unapproved_dependencies))) |
| 3810 | ] |
| 3811 | suggested_owners = input_api.owners_client.SuggestOwners( |
| 3812 | missing_files, exclude=[owner_email]) |
| 3813 | output_list.append( |
| 3814 | output('Suggested missing target path OWNERS:\n %s' % |
| 3815 | '\n '.join(suggested_owners or []))) |
| 3816 | return output_list |
| 3817 | |
| 3818 | return [] |
[email protected] | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3819 | |
| 3820 | |
Wei-Yin Chen (陳威尹) | dca729a | 2018-07-31 21:35:49 | [diff] [blame] | 3821 | # TODO: add unit tests. |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3822 | def CheckSpamLogging(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3823 | file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS] |
| 3824 | files_to_skip = ( |
| 3825 | _EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 3826 | input_api.DEFAULT_FILES_TO_SKIP + ( |
Jaewon Jung | 2f323bb | 2022-12-07 23:55:01 | [diff] [blame] | 3827 | r"^base/fuchsia/scoped_fx_logger\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3828 | r"^base/logging\.h$", |
| 3829 | r"^base/logging\.cc$", |
| 3830 | r"^base/task/thread_pool/task_tracker\.cc$", |
| 3831 | r"^chrome/app/chrome_main_delegate\.cc$", |
Yao Li | 359937b | 2023-02-15 23:43:03 | [diff] [blame] | 3832 | r"^chrome/browser/ash/arc/enterprise/cert_store/arc_cert_installer\.cc$", |
| 3833 | r"^chrome/browser/ash/policy/remote_commands/user_command_arc_job\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3834 | r"^chrome/browser/chrome_browser_main\.cc$", |
| 3835 | r"^chrome/browser/ui/startup/startup_browser_creator\.cc$", |
| 3836 | r"^chrome/browser/browser_switcher/bho/.*", |
| 3837 | r"^chrome/browser/diagnostics/diagnostics_writer\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3838 | r"^chrome/chrome_elf/dll_hash/dll_hash_main\.cc$", |
| 3839 | r"^chrome/installer/setup/.*", |
Daniel Rubery | ad36eea | 2024-08-01 01:38:32 | [diff] [blame] | 3840 | # crdmg runs as a separate binary which intentionally does |
| 3841 | # not depend on base logging. |
| 3842 | r"^chrome/utility/safe_browsing/mac/crdmg\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3843 | r"^chromecast/", |
Vigen Issahhanjan | e2d9382 | 2023-06-30 15:57:20 | [diff] [blame] | 3844 | r"^components/cast", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3845 | r"^components/media_control/renderer/media_playback_options\.cc$", |
Salma Elmahallawy | 5297645 | 2023-01-27 17:04:49 | [diff] [blame] | 3846 | r"^components/policy/core/common/policy_logger\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3847 | r"^components/viz/service/display/" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3848 | r"overlay_strategy_underlay_cast\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3849 | r"^components/zucchini/.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3850 | # TODO(peter): Remove exception. https://crbug.com/534537 |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3851 | r"^content/browser/notifications/" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3852 | r"notification_event_dispatcher_impl\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3853 | r"^content/common/gpu/client/gl_helper_benchmark\.cc$", |
| 3854 | r"^courgette/courgette_minimal_tool\.cc$", |
| 3855 | r"^courgette/courgette_tool\.cc$", |
| 3856 | r"^extensions/renderer/logging_native_handler\.cc$", |
| 3857 | r"^fuchsia_web/common/init_logging\.cc$", |
| 3858 | r"^fuchsia_web/runners/common/web_component\.cc$", |
Caroline Liu | a705013 | 2023-02-13 22:23:15 | [diff] [blame] | 3859 | r"^fuchsia_web/shell/.*\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3860 | r"^headless/app/headless_shell\.cc$", |
| 3861 | r"^ipc/ipc_logging\.cc$", |
| 3862 | r"^native_client_sdk/", |
| 3863 | r"^remoting/base/logging\.h$", |
| 3864 | r"^remoting/host/.*", |
| 3865 | r"^sandbox/linux/.*", |
Austin Sullivan | a6054e0 | 2024-05-20 16:31:29 | [diff] [blame] | 3866 | r"^services/webnn/tflite/graph_impl_tflite\.cc$", |
| 3867 | r"^services/webnn/coreml/graph_impl_coreml\.mm$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3868 | r"^storage/browser/file_system/dump_file_system\.cc$", |
Steinar H. Gunderson | e5689e4 | 2024-08-07 18:17:19 | [diff] [blame] | 3869 | r"^testing/perf/", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3870 | r"^tools/", |
| 3871 | r"^ui/base/resource/data_pack\.cc$", |
| 3872 | r"^ui/aura/bench/bench_main\.cc$", |
| 3873 | r"^ui/ozone/platform/cast/", |
| 3874 | r"^ui/base/x/xwmstartupcheck/" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3875 | r"xwmstartupcheck\.cc$")) |
| 3876 | source_file_filter = lambda x: input_api.FilterSourceFile( |
| 3877 | x, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip) |
[email protected] | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3878 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3879 | log_info = set([]) |
| 3880 | printf = set([]) |
[email protected] | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3881 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3882 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 3883 | for _, line in f.ChangedContents(): |
| 3884 | if input_api.re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", line): |
| 3885 | log_info.add(f.LocalPath()) |
| 3886 | elif input_api.re.search(r"\bD?LOG_IF\s*\(\s*INFO\s*,", line): |
| 3887 | log_info.add(f.LocalPath()) |
[email protected] | 18b466b | 2013-12-02 22:01:37 | [diff] [blame] | 3888 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3889 | if input_api.re.search(r"\bprintf\(", line): |
| 3890 | printf.add(f.LocalPath()) |
| 3891 | elif input_api.re.search(r"\bfprintf\((stdout|stderr)", line): |
| 3892 | printf.add(f.LocalPath()) |
[email protected] | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3893 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3894 | if log_info: |
| 3895 | return [ |
| 3896 | output_api.PresubmitError( |
| 3897 | 'These files spam the console log with LOG(INFO):', |
| 3898 | items=log_info) |
| 3899 | ] |
| 3900 | if printf: |
| 3901 | return [ |
| 3902 | output_api.PresubmitError( |
| 3903 | 'These files spam the console log with printf/fprintf:', |
| 3904 | items=printf) |
| 3905 | ] |
| 3906 | return [] |
[email protected] | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3907 | |
| 3908 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3909 | def CheckForAnonymousVariables(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3910 | """These types are all expected to hold locks while in scope and |
| 3911 | so should never be anonymous (which causes them to be immediately |
| 3912 | destroyed).""" |
| 3913 | they_who_must_be_named = [ |
| 3914 | 'base::AutoLock', |
| 3915 | 'base::AutoReset', |
| 3916 | 'base::AutoUnlock', |
| 3917 | 'SkAutoAlphaRestore', |
| 3918 | 'SkAutoBitmapShaderInstall', |
| 3919 | 'SkAutoBlitterChoose', |
| 3920 | 'SkAutoBounderCommit', |
| 3921 | 'SkAutoCallProc', |
| 3922 | 'SkAutoCanvasRestore', |
| 3923 | 'SkAutoCommentBlock', |
| 3924 | 'SkAutoDescriptor', |
| 3925 | 'SkAutoDisableDirectionCheck', |
| 3926 | 'SkAutoDisableOvalCheck', |
| 3927 | 'SkAutoFree', |
| 3928 | 'SkAutoGlyphCache', |
| 3929 | 'SkAutoHDC', |
| 3930 | 'SkAutoLockColors', |
| 3931 | 'SkAutoLockPixels', |
| 3932 | 'SkAutoMalloc', |
| 3933 | 'SkAutoMaskFreeImage', |
| 3934 | 'SkAutoMutexAcquire', |
| 3935 | 'SkAutoPathBoundsUpdate', |
| 3936 | 'SkAutoPDFRelease', |
| 3937 | 'SkAutoRasterClipValidate', |
| 3938 | 'SkAutoRef', |
| 3939 | 'SkAutoTime', |
| 3940 | 'SkAutoTrace', |
| 3941 | 'SkAutoUnref', |
| 3942 | ] |
| 3943 | anonymous = r'(%s)\s*[({]' % '|'.join(they_who_must_be_named) |
| 3944 | # bad: base::AutoLock(lock.get()); |
| 3945 | # not bad: base::AutoLock lock(lock.get()); |
| 3946 | bad_pattern = input_api.re.compile(anonymous) |
| 3947 | # good: new base::AutoLock(lock.get()) |
| 3948 | good_pattern = input_api.re.compile(r'\bnew\s*' + anonymous) |
| 3949 | errors = [] |
[email protected] | 49aa76a | 2013-12-04 06:59:16 | [diff] [blame] | 3950 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3951 | for f in input_api.AffectedFiles(): |
| 3952 | if not f.LocalPath().endswith(('.cc', '.h', '.inl', '.m', '.mm')): |
| 3953 | continue |
| 3954 | for linenum, line in f.ChangedContents(): |
| 3955 | if bad_pattern.search(line) and not good_pattern.search(line): |
| 3956 | errors.append('%s:%d' % (f.LocalPath(), linenum)) |
[email protected] | 49aa76a | 2013-12-04 06:59:16 | [diff] [blame] | 3957 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3958 | if errors: |
| 3959 | return [ |
| 3960 | output_api.PresubmitError( |
| 3961 | 'These lines create anonymous variables that need to be named:', |
| 3962 | items=errors) |
| 3963 | ] |
| 3964 | return [] |
[email protected] | 49aa76a | 2013-12-04 06:59:16 | [diff] [blame] | 3965 | |
| 3966 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3967 | def CheckUniquePtrOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3968 | # 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] | 3969 | # and U, or is invalid due to mismatched angle bracket pairs. Assumes that |
| 3970 | # |template_str| is already in the form <...>. |
| 3971 | def HasMoreThanOneArgOrInvalid(template_str): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3972 | # Level of <...> nesting. |
| 3973 | nesting = 0 |
| 3974 | for c in template_str: |
| 3975 | if c == '<': |
| 3976 | nesting += 1 |
| 3977 | elif c == '>': |
| 3978 | nesting -= 1 |
| 3979 | elif c == ',' and nesting == 1: |
| 3980 | return True |
Glen Robertson | 9142ffd7 | 2024-05-16 01:37:47 | [diff] [blame] | 3981 | if nesting != 0: |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 3982 | # Invalid. |
| 3983 | return True |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3984 | return False |
Vaclav Brozek | b7fadb69 | 2018-08-30 06:39:53 | [diff] [blame] | 3985 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3986 | file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS] |
| 3987 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 3988 | affected_file, |
| 3989 | files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api. |
| 3990 | DEFAULT_FILES_TO_SKIP), |
| 3991 | files_to_check=file_inclusion_pattern) |
Vaclav Brozek | a54c528b | 2018-04-06 19:23:55 | [diff] [blame] | 3992 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3993 | # Pattern to capture a single "<...>" block of template arguments. It can |
| 3994 | # handle linearly nested blocks, such as "<std::vector<std::set<T>>>", but |
| 3995 | # cannot handle branching structures, such as "<pair<set<T>,set<U>>". The |
| 3996 | # latter would likely require counting that < and > match, which is not |
| 3997 | # expressible in regular languages. Should the need arise, one can introduce |
| 3998 | # limited counting (matching up to a total number of nesting depth), which |
| 3999 | # should cover all practical cases for already a low nesting limit. |
| 4000 | template_arg_pattern = ( |
| 4001 | r'<[^>]*' # Opening block of <. |
| 4002 | r'>([^<]*>)?') # Closing block of >. |
| 4003 | # Prefix expressing that whatever follows is not already inside a <...> |
| 4004 | # block. |
| 4005 | not_inside_template_arg_pattern = r'(^|[^<,\s]\s*)' |
| 4006 | null_construct_pattern = input_api.re.compile( |
| 4007 | not_inside_template_arg_pattern + r'\bstd::unique_ptr' + |
| 4008 | template_arg_pattern + r'\(\)') |
Vaclav Brozek | a54c528b | 2018-04-06 19:23:55 | [diff] [blame] | 4009 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4010 | # Same as template_arg_pattern, but excluding type arrays, e.g., <T[]>. |
| 4011 | template_arg_no_array_pattern = ( |
| 4012 | r'<[^>]*[^]]' # Opening block of <. |
| 4013 | r'>([^(<]*[^]]>)?') # Closing block of >. |
| 4014 | # Prefix saying that what follows is the start of an expression. |
| 4015 | start_of_expr_pattern = r'(=|\breturn|^)\s*' |
| 4016 | # Suffix saying that what follows are call parentheses with a non-empty list |
| 4017 | # of arguments. |
| 4018 | nonempty_arg_list_pattern = r'\(([^)]|$)' |
| 4019 | # Put the template argument into a capture group for deeper examination later. |
| 4020 | return_construct_pattern = input_api.re.compile( |
| 4021 | start_of_expr_pattern + r'std::unique_ptr' + '(?P<template_arg>' + |
| 4022 | template_arg_no_array_pattern + ')' + nonempty_arg_list_pattern) |
Vaclav Brozek | a54c528b | 2018-04-06 19:23:55 | [diff] [blame] | 4023 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4024 | problems_constructor = [] |
| 4025 | problems_nullptr = [] |
| 4026 | for f in input_api.AffectedSourceFiles(sources): |
| 4027 | for line_number, line in f.ChangedContents(): |
| 4028 | # Disallow: |
| 4029 | # return std::unique_ptr<T>(foo); |
| 4030 | # bar = std::unique_ptr<T>(foo); |
| 4031 | # But allow: |
| 4032 | # return std::unique_ptr<T[]>(foo); |
| 4033 | # bar = std::unique_ptr<T[]>(foo); |
| 4034 | # And also allow cases when the second template argument is present. Those |
| 4035 | # cases cannot be handled by std::make_unique: |
| 4036 | # return std::unique_ptr<T, U>(foo); |
| 4037 | # bar = std::unique_ptr<T, U>(foo); |
| 4038 | local_path = f.LocalPath() |
| 4039 | return_construct_result = return_construct_pattern.search(line) |
Glen Robertson | 9142ffd7 | 2024-05-16 01:37:47 | [diff] [blame] | 4040 | if return_construct_result and not HasMoreThanOneArgOrInvalid( |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4041 | return_construct_result.group('template_arg')): |
| 4042 | problems_constructor.append( |
| 4043 | '%s:%d\n %s' % (local_path, line_number, line.strip())) |
| 4044 | # Disallow: |
| 4045 | # std::unique_ptr<T>() |
| 4046 | if null_construct_pattern.search(line): |
| 4047 | problems_nullptr.append( |
| 4048 | '%s:%d\n %s' % (local_path, line_number, line.strip())) |
Vaclav Brozek | 851d960 | 2018-04-04 16:13:05 | [diff] [blame] | 4049 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4050 | errors = [] |
| 4051 | if problems_nullptr: |
| 4052 | errors.append( |
| 4053 | output_api.PresubmitPromptWarning( |
| 4054 | 'The following files use std::unique_ptr<T>(). Use nullptr instead.', |
| 4055 | problems_nullptr)) |
| 4056 | if problems_constructor: |
| 4057 | errors.append( |
| 4058 | output_api.PresubmitError( |
| 4059 | 'The following files use explicit std::unique_ptr constructor. ' |
| 4060 | 'Use std::make_unique<T>() instead, or use base::WrapUnique if ' |
| 4061 | 'std::make_unique is not an option.', problems_constructor)) |
| 4062 | return errors |
Peter Kasting | 4844e46e | 2018-02-23 07:27:10 | [diff] [blame] | 4063 | |
| 4064 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 4065 | def CheckUserActionUpdate(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4066 | """Checks if any new user action has been added.""" |
| 4067 | if any('actions.xml' == input_api.os_path.basename(f) |
| 4068 | for f in input_api.LocalPaths()): |
| 4069 | # If actions.xml is already included in the changelist, the PRESUBMIT |
| 4070 | # for actions.xml will do a more complete presubmit check. |
| 4071 | return [] |
| 4072 | |
| 4073 | file_inclusion_pattern = [r'.*\.(cc|mm)$'] |
| 4074 | files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 4075 | input_api.DEFAULT_FILES_TO_SKIP) |
| 4076 | file_filter = lambda f: input_api.FilterSourceFile( |
| 4077 | f, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip) |
| 4078 | |
| 4079 | action_re = r'[^a-zA-Z]UserMetricsAction\("([^"]*)' |
| 4080 | current_actions = None |
| 4081 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 4082 | for line_num, line in f.ChangedContents(): |
| 4083 | match = input_api.re.search(action_re, line) |
| 4084 | if match: |
| 4085 | # Loads contents in tools/metrics/actions/actions.xml to memory. It's |
| 4086 | # loaded only once. |
| 4087 | if not current_actions: |
Bruce Dawson | 6cb2d4d | 2023-03-01 21:35:09 | [diff] [blame] | 4088 | with open('tools/metrics/actions/actions.xml', |
| 4089 | encoding='utf-8') as actions_f: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4090 | current_actions = actions_f.read() |
| 4091 | # Search for the matched user action name in |current_actions|. |
| 4092 | for action_name in match.groups(): |
| 4093 | action = 'name="{0}"'.format(action_name) |
| 4094 | if action not in current_actions: |
| 4095 | return [ |
| 4096 | output_api.PresubmitPromptWarning( |
| 4097 | 'File %s line %d: %s is missing in ' |
| 4098 | 'tools/metrics/actions/actions.xml. Please run ' |
| 4099 | 'tools/metrics/actions/extract_actions.py to update.' |
| 4100 | % (f.LocalPath(), line_num, action_name)) |
| 4101 | ] |
[email protected] | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 4102 | return [] |
| 4103 | |
[email protected] | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 4104 | |
Daniel Cheng | 13ca61a88 | 2017-08-25 15:11:25 | [diff] [blame] | 4105 | def _ImportJSONCommentEater(input_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4106 | import sys |
| 4107 | sys.path = sys.path + [ |
| 4108 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 4109 | 'json_comment_eater') |
| 4110 | ] |
| 4111 | import json_comment_eater |
| 4112 | return json_comment_eater |
Daniel Cheng | 13ca61a88 | 2017-08-25 15:11:25 | [diff] [blame] | 4113 | |
| 4114 | |
[email protected] | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 4115 | def _GetJSONParseError(input_api, filename, eat_comments=True): |
dcheng | e07de81 | 2016-06-20 19:27:17 | [diff] [blame] | 4116 | try: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4117 | contents = input_api.ReadFile(filename) |
| 4118 | if eat_comments: |
| 4119 | json_comment_eater = _ImportJSONCommentEater(input_api) |
| 4120 | contents = json_comment_eater.Nom(contents) |
dcheng | e07de81 | 2016-06-20 19:27:17 | [diff] [blame] | 4121 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4122 | input_api.json.loads(contents) |
| 4123 | except ValueError as e: |
| 4124 | return e |
Andrew Grieve | 4deedb1 | 2022-02-03 21:34:50 | [diff] [blame] | 4125 | return None |
| 4126 | |
| 4127 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4128 | def _GetIDLParseError(input_api, filename): |
| 4129 | try: |
| 4130 | contents = input_api.ReadFile(filename) |
Devlin Cronin | f7582a1 | 2022-04-21 21:14:28 | [diff] [blame] | 4131 | for i, char in enumerate(contents): |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4132 | if not char.isascii(): |
| 4133 | return ( |
| 4134 | 'Non-ascii character "%s" (ord %d) found at offset %d.' % |
| 4135 | (char, ord(char), i)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4136 | idl_schema = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 4137 | 'tools', 'json_schema_compiler', |
| 4138 | 'idl_schema.py') |
| 4139 | process = input_api.subprocess.Popen( |
Bruce Dawson | 679fb08 | 2022-04-14 00:47:28 | [diff] [blame] | 4140 | [input_api.python3_executable, idl_schema], |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4141 | stdin=input_api.subprocess.PIPE, |
| 4142 | stdout=input_api.subprocess.PIPE, |
| 4143 | stderr=input_api.subprocess.PIPE, |
| 4144 | universal_newlines=True) |
| 4145 | (_, error) = process.communicate(input=contents) |
| 4146 | return error or None |
| 4147 | except ValueError as e: |
| 4148 | return e |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4149 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4150 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4151 | def CheckParseErrors(input_api, output_api): |
| 4152 | """Check that IDL and JSON files do not contain syntax errors.""" |
| 4153 | actions = { |
| 4154 | '.idl': _GetIDLParseError, |
| 4155 | '.json': _GetJSONParseError, |
| 4156 | } |
| 4157 | # Most JSON files are preprocessed and support comments, but these do not. |
| 4158 | json_no_comments_patterns = [ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4159 | r'^testing/', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4160 | ] |
| 4161 | # Only run IDL checker on files in these directories. |
| 4162 | idl_included_patterns = [ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4163 | r'^chrome/common/extensions/api/', |
| 4164 | r'^extensions/common/api/', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4165 | ] |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4166 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4167 | def get_action(affected_file): |
| 4168 | filename = affected_file.LocalPath() |
| 4169 | return actions.get(input_api.os_path.splitext(filename)[1]) |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4170 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4171 | def FilterFile(affected_file): |
| 4172 | action = get_action(affected_file) |
| 4173 | if not action: |
| 4174 | return False |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4175 | path = affected_file.UnixLocalPath() |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4176 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4177 | if _MatchesFile(input_api, |
| 4178 | _KNOWN_TEST_DATA_AND_INVALID_JSON_FILE_PATTERNS, path): |
| 4179 | return False |
| 4180 | |
| 4181 | if (action == _GetIDLParseError |
| 4182 | and not _MatchesFile(input_api, idl_included_patterns, path)): |
| 4183 | return False |
| 4184 | return True |
| 4185 | |
| 4186 | results = [] |
| 4187 | for affected_file in input_api.AffectedFiles(file_filter=FilterFile, |
| 4188 | include_deletes=False): |
| 4189 | action = get_action(affected_file) |
| 4190 | kwargs = {} |
| 4191 | if (action == _GetJSONParseError |
| 4192 | and _MatchesFile(input_api, json_no_comments_patterns, |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4193 | affected_file.UnixLocalPath())): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4194 | kwargs['eat_comments'] = False |
| 4195 | parse_error = action(input_api, affected_file.AbsoluteLocalPath(), |
| 4196 | **kwargs) |
| 4197 | if parse_error: |
| 4198 | results.append( |
| 4199 | output_api.PresubmitError( |
| 4200 | '%s could not be parsed: %s' % |
| 4201 | (affected_file.LocalPath(), parse_error))) |
| 4202 | return results |
| 4203 | |
| 4204 | |
| 4205 | def CheckJavaStyle(input_api, output_api): |
| 4206 | """Runs checkstyle on changed java files and returns errors if any exist.""" |
| 4207 | |
| 4208 | # Return early if no java files were modified. |
| 4209 | if not any( |
| 4210 | _IsJavaFile(input_api, f.LocalPath()) |
| 4211 | for f in input_api.AffectedFiles()): |
| 4212 | return [] |
| 4213 | |
| 4214 | import sys |
| 4215 | original_sys_path = sys.path |
| 4216 | try: |
| 4217 | sys.path = sys.path + [ |
| 4218 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 4219 | 'android', 'checkstyle') |
| 4220 | ] |
| 4221 | import checkstyle |
| 4222 | finally: |
| 4223 | # Restore sys.path to what it was before. |
| 4224 | sys.path = original_sys_path |
| 4225 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 4226 | return checkstyle.run_presubmit(input_api, |
| 4227 | output_api, |
| 4228 | files_to_skip=_EXCLUDED_PATHS + |
| 4229 | input_api.DEFAULT_FILES_TO_SKIP) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4230 | |
| 4231 | |
| 4232 | def CheckPythonDevilInit(input_api, output_api): |
| 4233 | """Checks to make sure devil is initialized correctly in python scripts.""" |
| 4234 | script_common_initialize_pattern = input_api.re.compile( |
| 4235 | r'script_common\.InitializeEnvironment\(') |
| 4236 | devil_env_config_initialize = input_api.re.compile( |
| 4237 | r'devil_env\.config\.Initialize\(') |
| 4238 | |
| 4239 | errors = [] |
| 4240 | |
| 4241 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 4242 | affected_file, |
| 4243 | files_to_skip=(_EXCLUDED_PATHS + input_api.DEFAULT_FILES_TO_SKIP + ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4244 | r'^build/android/devil_chromium\.py', |
Sven Zheng | 8e07956 | 2024-05-10 20:11:06 | [diff] [blame] | 4245 | r'^tools/bisect-builds\.py', |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4246 | r'^third_party/.*', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4247 | )), |
| 4248 | files_to_check=[r'.*\.py$']) |
| 4249 | |
| 4250 | for f in input_api.AffectedSourceFiles(sources): |
| 4251 | for line_num, line in f.ChangedContents(): |
| 4252 | if (script_common_initialize_pattern.search(line) |
| 4253 | or devil_env_config_initialize.search(line)): |
| 4254 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4255 | |
| 4256 | results = [] |
| 4257 | |
| 4258 | if errors: |
| 4259 | results.append( |
| 4260 | output_api.PresubmitError( |
| 4261 | 'Devil initialization should always be done using ' |
| 4262 | 'devil_chromium.Initialize() in the chromium project, to use better ' |
| 4263 | 'defaults for dependencies (ex. up-to-date version of adb).', |
| 4264 | errors)) |
| 4265 | |
| 4266 | return results |
| 4267 | |
| 4268 | |
| 4269 | def _MatchesFile(input_api, patterns, path): |
| 4270 | for pattern in patterns: |
| 4271 | if input_api.re.search(pattern, path): |
| 4272 | return True |
| 4273 | return False |
| 4274 | |
| 4275 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4276 | def _ChangeHasSecurityReviewer(input_api, owners_file): |
| 4277 | """Returns True iff the CL has a reviewer from SECURITY_OWNERS. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4278 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4279 | Args: |
| 4280 | input_api: The presubmit input API. |
| 4281 | owners_file: OWNERS file with required reviewers. Typically, this is |
| 4282 | something like ipc/SECURITY_OWNERS. |
| 4283 | |
| 4284 | Note: if the presubmit is running for commit rather than for upload, this |
| 4285 | only returns True if a security reviewer has also approved the CL. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4286 | """ |
Daniel Cheng | d8824447 | 2022-05-16 09:08:47 | [diff] [blame] | 4287 | # Owners-Override should bypass all additional OWNERS enforcement checks. |
| 4288 | # A CR+1 vote will still be required to land this change. |
| 4289 | if (input_api.change.issue and input_api.gerrit.IsOwnersOverrideApproved( |
| 4290 | input_api.change.issue)): |
| 4291 | return True |
| 4292 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4293 | owner_email, reviewers = ( |
| 4294 | input_api.canned_checks.GetCodereviewOwnerAndReviewers( |
Daniel Cheng | 3008dc1 | 2022-05-13 04:02:11 | [diff] [blame] | 4295 | input_api, |
| 4296 | None, |
| 4297 | approval_needed=input_api.is_committing and not input_api.dry_run)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4298 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4299 | security_owners = input_api.owners_client.ListOwners(owners_file) |
| 4300 | return any(owner in reviewers for owner in security_owners) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4301 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4302 | |
| 4303 | @dataclass |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4304 | class _SecurityProblemWithItems: |
| 4305 | problem: str |
| 4306 | items: Sequence[str] |
| 4307 | |
| 4308 | |
| 4309 | @dataclass |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4310 | class _MissingSecurityOwnersResult: |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4311 | owners_file_problems: Sequence[_SecurityProblemWithItems] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4312 | has_security_sensitive_files: bool |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4313 | missing_reviewer_problem: Optional[_SecurityProblemWithItems] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4314 | |
| 4315 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 4316 | def _FindMissingSecurityOwners( |
| 4317 | input_api, |
| 4318 | output_api, |
| 4319 | file_patterns: Sequence[str], |
| 4320 | excluded_patterns: Sequence[str], |
| 4321 | required_owners_file: str, |
| 4322 | custom_rule_function: Optional[Callable] = None |
| 4323 | ) -> _MissingSecurityOwnersResult: |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4324 | """Find OWNERS files missing per-file rules for security-sensitive files. |
| 4325 | |
| 4326 | Args: |
| 4327 | input_api: the PRESUBMIT input API object. |
| 4328 | output_api: the PRESUBMIT output API object. |
| 4329 | file_patterns: basename patterns that require a corresponding per-file |
| 4330 | security restriction. |
| 4331 | excluded_patterns: path patterns that should be exempted from |
| 4332 | requiring a security restriction. |
| 4333 | required_owners_file: path to the required OWNERS file, e.g. |
| 4334 | ipc/SECURITY_OWNERS |
| 4335 | cc_alias: If not None, email that will be CCed automatically if the |
| 4336 | change contains security-sensitive files, as determined by |
| 4337 | `file_patterns` and `excluded_patterns`. |
| 4338 | custom_rule_function: If not None, will be called with `input_api` and |
| 4339 | the current file under consideration. Returning True will add an |
| 4340 | exact match per-file rule check for the current file. |
| 4341 | """ |
| 4342 | |
| 4343 | # `to_check` is a mapping of an OWNERS file path to Patterns. |
| 4344 | # |
| 4345 | # Patterns is a dictionary mapping glob patterns (suitable for use in |
| 4346 | # per-file rules) to a PatternEntry. |
| 4347 | # |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4348 | # PatternEntry is a dictionary with two keys: |
| 4349 | # - 'files': the files that are matched by this pattern |
| 4350 | # - 'rules': the per-file rules needed for this pattern |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4351 | # |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4352 | # For example, if we expect OWNERS file to contain rules for *.mojom and |
| 4353 | # *_struct_traits*.*, Patterns might look like this: |
| 4354 | # { |
| 4355 | # '*.mojom': { |
| 4356 | # 'files': ..., |
| 4357 | # 'rules': [ |
| 4358 | # 'per-file *.mojom=set noparent', |
| 4359 | # 'per-file *.mojom=file://ipc/SECURITY_OWNERS', |
| 4360 | # ], |
| 4361 | # }, |
| 4362 | # '*_struct_traits*.*': { |
| 4363 | # 'files': ..., |
| 4364 | # 'rules': [ |
| 4365 | # 'per-file *_struct_traits*.*=set noparent', |
| 4366 | # 'per-file *_struct_traits*.*=file://ipc/SECURITY_OWNERS', |
| 4367 | # ], |
| 4368 | # }, |
| 4369 | # } |
| 4370 | to_check = {} |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4371 | files_to_review = [] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4372 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4373 | def AddPatternToCheck(file, pattern): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4374 | owners_file = input_api.os_path.join( |
Daniel Cheng | d8824447 | 2022-05-16 09:08:47 | [diff] [blame] | 4375 | input_api.os_path.dirname(file.LocalPath()), 'OWNERS') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4376 | if owners_file not in to_check: |
| 4377 | to_check[owners_file] = {} |
| 4378 | if pattern not in to_check[owners_file]: |
| 4379 | to_check[owners_file][pattern] = { |
| 4380 | 'files': [], |
| 4381 | 'rules': [ |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4382 | f'per-file {pattern}=set noparent', |
| 4383 | f'per-file {pattern}=file://{required_owners_file}', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4384 | ] |
| 4385 | } |
Daniel Cheng | ed57a16 | 2022-05-25 02:56:34 | [diff] [blame] | 4386 | to_check[owners_file][pattern]['files'].append(file.LocalPath()) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4387 | files_to_review.append(file.LocalPath()) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4388 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4389 | # Only enforce security OWNERS rules for a directory if that directory has a |
| 4390 | # file that matches `file_patterns`. For example, if a directory only |
| 4391 | # contains *.mojom files and no *_messages*.h files, the check should only |
| 4392 | # ensure that rules for *.mojom files are present. |
| 4393 | for file in input_api.AffectedFiles(include_deletes=False): |
| 4394 | file_basename = input_api.os_path.basename(file.LocalPath()) |
| 4395 | if custom_rule_function is not None and custom_rule_function( |
| 4396 | input_api, file): |
| 4397 | AddPatternToCheck(file, file_basename) |
| 4398 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4399 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4400 | if any( |
| 4401 | input_api.fnmatch.fnmatch(file.LocalPath(), pattern) |
| 4402 | for pattern in excluded_patterns): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4403 | continue |
| 4404 | |
| 4405 | for pattern in file_patterns: |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4406 | # Unlike `excluded_patterns`, `file_patterns` is checked only against the |
| 4407 | # file's basename. |
| 4408 | if input_api.fnmatch.fnmatch(file_basename, pattern): |
| 4409 | AddPatternToCheck(file, pattern) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4410 | break |
| 4411 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4412 | has_security_sensitive_files = bool(to_check) |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4413 | |
| 4414 | # Check if any newly added lines in OWNERS files intersect with required |
| 4415 | # per-file OWNERS lines. If so, ensure that a security reviewer is included. |
| 4416 | # This is a hack, but is needed because the OWNERS check (by design) ignores |
| 4417 | # new OWNERS entries; otherwise, a non-owner could add someone as a new |
| 4418 | # OWNER and have that newly-added OWNER self-approve their own addition. |
| 4419 | newly_covered_files = [] |
| 4420 | for file in input_api.AffectedFiles(include_deletes=False): |
| 4421 | if not file.LocalPath() in to_check: |
| 4422 | continue |
| 4423 | for _, line in file.ChangedContents(): |
| 4424 | for _, entry in to_check[file.LocalPath()].items(): |
| 4425 | if line in entry['rules']: |
| 4426 | newly_covered_files.extend(entry['files']) |
| 4427 | |
| 4428 | missing_reviewer_problems = None |
| 4429 | if newly_covered_files and not _ChangeHasSecurityReviewer( |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4430 | input_api, required_owners_file): |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4431 | missing_reviewer_problems = _SecurityProblemWithItems( |
| 4432 | f'Review from an owner in {required_owners_file} is required for ' |
| 4433 | 'the following newly-added files:', |
| 4434 | [f'{file}' for file in sorted(set(newly_covered_files))]) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4435 | |
| 4436 | # Go through the OWNERS files to check, filtering out rules that are already |
| 4437 | # present in that OWNERS file. |
| 4438 | for owners_file, patterns in to_check.items(): |
| 4439 | try: |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4440 | lines = set( |
| 4441 | input_api.ReadFile( |
| 4442 | input_api.os_path.join(input_api.change.RepositoryRoot(), |
| 4443 | owners_file)).splitlines()) |
| 4444 | for entry in patterns.values(): |
| 4445 | entry['rules'] = [ |
| 4446 | rule for rule in entry['rules'] if rule not in lines |
| 4447 | ] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4448 | except IOError: |
| 4449 | # No OWNERS file, so all the rules are definitely missing. |
| 4450 | continue |
| 4451 | |
| 4452 | # 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] | 4453 | owners_file_problems = [] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4454 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4455 | for owners_file, patterns in to_check.items(): |
| 4456 | missing_lines = [] |
| 4457 | files = [] |
| 4458 | for _, entry in patterns.items(): |
Daniel Cheng | ed57a16 | 2022-05-25 02:56:34 | [diff] [blame] | 4459 | files.extend(entry['files']) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4460 | missing_lines.extend(entry['rules']) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4461 | if missing_lines: |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4462 | joined_missing_lines = '\n'.join(line for line in missing_lines) |
| 4463 | owners_file_problems.append( |
| 4464 | _SecurityProblemWithItems( |
| 4465 | 'Found missing OWNERS lines for security-sensitive files. ' |
| 4466 | f'Please add the following lines to {owners_file}:\n' |
| 4467 | f'{joined_missing_lines}\n\nTo ensure security review for:', |
| 4468 | files)) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4469 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4470 | return _MissingSecurityOwnersResult(owners_file_problems, |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4471 | has_security_sensitive_files, |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4472 | missing_reviewer_problems) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4473 | |
| 4474 | |
| 4475 | def _CheckChangeForIpcSecurityOwners(input_api, output_api): |
| 4476 | # Whether or not a file affects IPC is (mostly) determined by a simple list |
| 4477 | # of filename patterns. |
| 4478 | file_patterns = [ |
| 4479 | # Legacy IPC: |
| 4480 | '*_messages.cc', |
| 4481 | '*_messages*.h', |
| 4482 | '*_param_traits*.*', |
| 4483 | # Mojo IPC: |
| 4484 | '*.mojom', |
| 4485 | '*_mojom_traits*.*', |
| 4486 | '*_type_converter*.*', |
| 4487 | # Android native IPC: |
| 4488 | '*.aidl', |
| 4489 | ] |
| 4490 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4491 | excluded_patterns = [ |
Daniel Cheng | 518943f | 2022-05-12 22:15:46 | [diff] [blame] | 4492 | # These third_party directories do not contain IPCs, but contain files |
| 4493 | # matching the above patterns, which trigger false positives. |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4494 | 'third_party/crashpad/*', |
| 4495 | 'third_party/blink/renderer/platform/bindings/*', |
Evan Stade | 23a77da | 2025-02-06 21:15:31 | [diff] [blame] | 4496 | 'third_party/protobuf/*', |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4497 | 'third_party/win_build_output/*', |
Daniel Cheng | d8824447 | 2022-05-16 09:08:47 | [diff] [blame] | 4498 | # Enum-only mojoms used for web metrics, so no security review needed. |
| 4499 | 'third_party/blink/public/mojom/use_counter/metrics/*', |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4500 | # These files are just used to communicate between class loaders running |
| 4501 | # in the same process. |
| 4502 | 'weblayer/browser/java/org/chromium/weblayer_private/interfaces/*', |
| 4503 | 'weblayer/browser/java/org/chromium/weblayer_private/test_interfaces/*', |
| 4504 | ] |
| 4505 | |
| 4506 | def IsMojoServiceManifestFile(input_api, file): |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 4507 | manifest_pattern = input_api.re.compile(r'manifests?\.(cc|h)$') |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 4508 | test_manifest_pattern = input_api.re.compile( |
| 4509 | r'test_manifests?\.(cc|h)') |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4510 | if not manifest_pattern.search(file.LocalPath()): |
| 4511 | return False |
| 4512 | |
| 4513 | if test_manifest_pattern.search(file.LocalPath()): |
| 4514 | return False |
| 4515 | |
| 4516 | # All actual service manifest files should contain at least one |
| 4517 | # qualified reference to service_manager::Manifest. |
| 4518 | return any('service_manager::Manifest' in line |
| 4519 | for line in file.NewContents()) |
| 4520 | |
| 4521 | return _FindMissingSecurityOwners( |
| 4522 | input_api, |
| 4523 | output_api, |
| 4524 | file_patterns, |
| 4525 | excluded_patterns, |
| 4526 | 'ipc/SECURITY_OWNERS', |
| 4527 | custom_rule_function=IsMojoServiceManifestFile) |
| 4528 | |
| 4529 | |
| 4530 | def _CheckChangeForFuchsiaSecurityOwners(input_api, output_api): |
| 4531 | file_patterns = [ |
| 4532 | # Component specifications. |
| 4533 | '*.cml', # Component Framework v2. |
| 4534 | '*.cmx', # Component Framework v1. |
| 4535 | |
| 4536 | # Fuchsia IDL protocol specifications. |
| 4537 | '*.fidl', |
| 4538 | ] |
| 4539 | |
| 4540 | # Don't check for owners files for changes in these directories. |
| 4541 | excluded_patterns = [ |
| 4542 | 'third_party/crashpad/*', |
| 4543 | ] |
| 4544 | |
| 4545 | return _FindMissingSecurityOwners(input_api, output_api, file_patterns, |
| 4546 | excluded_patterns, |
| 4547 | 'build/fuchsia/SECURITY_OWNERS') |
| 4548 | |
| 4549 | |
| 4550 | def CheckSecurityOwners(input_api, output_api): |
| 4551 | """Checks that various security-sensitive files have an IPC OWNERS rule.""" |
| 4552 | ipc_results = _CheckChangeForIpcSecurityOwners(input_api, output_api) |
| 4553 | fuchsia_results = _CheckChangeForFuchsiaSecurityOwners( |
| 4554 | input_api, output_api) |
| 4555 | |
| 4556 | if ipc_results.has_security_sensitive_files: |
| 4557 | output_api.AppendCC('[email protected]') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4558 | |
| 4559 | results = [] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4560 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4561 | missing_reviewer_problems = [] |
| 4562 | if ipc_results.missing_reviewer_problem: |
| 4563 | missing_reviewer_problems.append(ipc_results.missing_reviewer_problem) |
| 4564 | if fuchsia_results.missing_reviewer_problem: |
| 4565 | missing_reviewer_problems.append( |
| 4566 | fuchsia_results.missing_reviewer_problem) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4567 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4568 | # Missing reviewers are an error unless there's no issue number |
| 4569 | # associated with this branch; in that case, the presubmit is being run |
| 4570 | # with --all or --files. |
| 4571 | # |
| 4572 | # Note that upload should never be an error; otherwise, it would be |
| 4573 | # impossible to upload changes at all. |
| 4574 | if input_api.is_committing and input_api.change.issue: |
| 4575 | make_presubmit_message = output_api.PresubmitError |
| 4576 | else: |
| 4577 | make_presubmit_message = output_api.PresubmitNotifyResult |
| 4578 | for problem in missing_reviewer_problems: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4579 | results.append( |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4580 | make_presubmit_message(problem.problem, items=problem.items)) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4581 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4582 | owners_file_problems = [] |
| 4583 | owners_file_problems.extend(ipc_results.owners_file_problems) |
| 4584 | owners_file_problems.extend(fuchsia_results.owners_file_problems) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4585 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4586 | for problem in owners_file_problems: |
Daniel Cheng | 3008dc1 | 2022-05-13 04:02:11 | [diff] [blame] | 4587 | # Missing per-file rules are always an error. While swarming and caching |
| 4588 | # means that uploading a patchset with updated OWNERS files and sending |
| 4589 | # it to the CQ again should not have a large incremental cost, it is |
| 4590 | # still frustrating to discover the error only after the change has |
| 4591 | # already been uploaded. |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4592 | results.append( |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4593 | output_api.PresubmitError(problem.problem, items=problem.items)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4594 | |
| 4595 | return results |
| 4596 | |
| 4597 | |
| 4598 | def _GetFilesUsingSecurityCriticalFunctions(input_api): |
| 4599 | """Checks affected files for changes to security-critical calls. This |
| 4600 | function checks the full change diff, to catch both additions/changes |
| 4601 | and removals. |
| 4602 | |
| 4603 | Returns a dict keyed by file name, and the value is a set of detected |
| 4604 | functions. |
| 4605 | """ |
| 4606 | # Map of function pretty name (displayed in an error) to the pattern to |
| 4607 | # match it with. |
| 4608 | _PATTERNS_TO_CHECK = { |
| 4609 | 'content::GetServiceSandboxType<>()': 'GetServiceSandboxType\\<' |
| 4610 | } |
| 4611 | _PATTERNS_TO_CHECK = { |
| 4612 | k: input_api.re.compile(v) |
| 4613 | for k, v in _PATTERNS_TO_CHECK.items() |
| 4614 | } |
| 4615 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4616 | # We don't want to trigger on strings within this file. |
| 4617 | def presubmit_file_filter(f): |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4618 | return 'PRESUBMIT.py' != input_api.os_path.split(f.LocalPath())[1] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4619 | |
| 4620 | # Scan all affected files for changes touching _FUNCTIONS_TO_CHECK. |
| 4621 | files_to_functions = {} |
| 4622 | for f in input_api.AffectedFiles(file_filter=presubmit_file_filter): |
| 4623 | diff = f.GenerateScmDiff() |
| 4624 | for line in diff.split('\n'): |
| 4625 | # Not using just RightHandSideLines() because removing a |
| 4626 | # call to a security-critical function can be just as important |
| 4627 | # as adding or changing the arguments. |
| 4628 | if line.startswith('-') or (line.startswith('+') |
| 4629 | and not line.startswith('++')): |
| 4630 | for name, pattern in _PATTERNS_TO_CHECK.items(): |
| 4631 | if pattern.search(line): |
| 4632 | path = f.LocalPath() |
| 4633 | if not path in files_to_functions: |
| 4634 | files_to_functions[path] = set() |
| 4635 | files_to_functions[path].add(name) |
| 4636 | return files_to_functions |
| 4637 | |
| 4638 | |
| 4639 | def CheckSecurityChanges(input_api, output_api): |
| 4640 | """Checks that changes involving security-critical functions are reviewed |
| 4641 | by the security team. |
| 4642 | """ |
| 4643 | files_to_functions = _GetFilesUsingSecurityCriticalFunctions(input_api) |
| 4644 | if not len(files_to_functions): |
| 4645 | return [] |
| 4646 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4647 | owners_file = 'ipc/SECURITY_OWNERS' |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4648 | if _ChangeHasSecurityReviewer(input_api, owners_file): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4649 | return [] |
| 4650 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4651 | msg = 'The following files change calls to security-sensitive functions\n' \ |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4652 | 'that need to be reviewed by {}.\n'.format(owners_file) |
| 4653 | for path, names in files_to_functions.items(): |
| 4654 | msg += ' {}\n'.format(path) |
| 4655 | for name in names: |
| 4656 | msg += ' {}\n'.format(name) |
| 4657 | msg += '\n' |
| 4658 | |
| 4659 | if input_api.is_committing: |
| 4660 | output = output_api.PresubmitError |
Mohamed Heikal | e217fc85 | 2020-07-06 19:44:03 | [diff] [blame] | 4661 | else: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4662 | output = output_api.PresubmitNotifyResult |
| 4663 | return [output(msg)] |
| 4664 | |
| 4665 | |
| 4666 | def CheckSetNoParent(input_api, output_api): |
| 4667 | """Checks that set noparent is only used together with an OWNERS file in |
| 4668 | //build/OWNERS.setnoparent (see also |
| 4669 | //docs/code_reviews.md#owners-files-details) |
| 4670 | """ |
| 4671 | # Return early if no OWNERS files were modified. |
| 4672 | if not any(f.LocalPath().endswith('OWNERS') |
| 4673 | for f in input_api.AffectedFiles(include_deletes=False)): |
| 4674 | return [] |
| 4675 | |
| 4676 | errors = [] |
| 4677 | |
| 4678 | allowed_owners_files_file = 'build/OWNERS.setnoparent' |
| 4679 | allowed_owners_files = set() |
Bruce Dawson | 58a45d2 | 2023-02-27 11:24:16 | [diff] [blame] | 4680 | with open(allowed_owners_files_file, 'r', encoding='utf-8') as f: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4681 | for line in f: |
| 4682 | line = line.strip() |
| 4683 | if not line or line.startswith('#'): |
| 4684 | continue |
| 4685 | allowed_owners_files.add(line) |
| 4686 | |
| 4687 | per_file_pattern = input_api.re.compile('per-file (.+)=(.+)') |
| 4688 | |
| 4689 | for f in input_api.AffectedFiles(include_deletes=False): |
| 4690 | if not f.LocalPath().endswith('OWNERS'): |
| 4691 | continue |
| 4692 | |
| 4693 | found_owners_files = set() |
| 4694 | found_set_noparent_lines = dict() |
| 4695 | |
| 4696 | # Parse the OWNERS file. |
| 4697 | for lineno, line in enumerate(f.NewContents(), 1): |
| 4698 | line = line.strip() |
| 4699 | if line.startswith('set noparent'): |
| 4700 | found_set_noparent_lines[''] = lineno |
| 4701 | if line.startswith('file://'): |
| 4702 | if line in allowed_owners_files: |
| 4703 | found_owners_files.add('') |
| 4704 | if line.startswith('per-file'): |
| 4705 | match = per_file_pattern.match(line) |
| 4706 | if match: |
| 4707 | glob = match.group(1).strip() |
| 4708 | directive = match.group(2).strip() |
| 4709 | if directive == 'set noparent': |
| 4710 | found_set_noparent_lines[glob] = lineno |
| 4711 | if directive.startswith('file://'): |
| 4712 | if directive in allowed_owners_files: |
| 4713 | found_owners_files.add(glob) |
| 4714 | |
| 4715 | # Check that every set noparent line has a corresponding file:// line |
| 4716 | # listed in build/OWNERS.setnoparent. An exception is made for top level |
| 4717 | # directories since src/OWNERS shouldn't review them. |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4718 | linux_path = f.UnixLocalPath() |
Bruce Dawson | 6bb0d67 | 2022-04-06 15:13:49 | [diff] [blame] | 4719 | if (linux_path.count('/') != 1 |
| 4720 | and (not linux_path in _EXCLUDED_SET_NO_PARENT_PATHS)): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4721 | for set_noparent_line in found_set_noparent_lines: |
| 4722 | if set_noparent_line in found_owners_files: |
| 4723 | continue |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 4724 | errors.append( |
| 4725 | ' %s:%d' % |
| 4726 | (linux_path, found_set_noparent_lines[set_noparent_line])) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4727 | |
| 4728 | results = [] |
| 4729 | if errors: |
| 4730 | if input_api.is_committing: |
| 4731 | output = output_api.PresubmitError |
| 4732 | else: |
| 4733 | output = output_api.PresubmitPromptWarning |
| 4734 | results.append( |
| 4735 | output( |
| 4736 | 'Found the following "set noparent" restrictions in OWNERS files that ' |
| 4737 | 'do not include owners from build/OWNERS.setnoparent:', |
| 4738 | long_text='\n\n'.join(errors))) |
| 4739 | return results |
| 4740 | |
| 4741 | |
| 4742 | def CheckUselessForwardDeclarations(input_api, output_api): |
| 4743 | """Checks that added or removed lines in non third party affected |
| 4744 | header files do not lead to new useless class or struct forward |
| 4745 | declaration. |
| 4746 | """ |
| 4747 | results = [] |
| 4748 | class_pattern = input_api.re.compile(r'^class\s+(\w+);$', |
| 4749 | input_api.re.MULTILINE) |
| 4750 | struct_pattern = input_api.re.compile(r'^struct\s+(\w+);$', |
| 4751 | input_api.re.MULTILINE) |
| 4752 | for f in input_api.AffectedFiles(include_deletes=False): |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4753 | local_path = f.UnixLocalPath() |
| 4754 | if (local_path.startswith('third_party') |
| 4755 | and not local_path.startswith('third_party/blink')): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4756 | continue |
| 4757 | |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4758 | if not local_path.endswith('.h'): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4759 | continue |
| 4760 | |
| 4761 | contents = input_api.ReadFile(f) |
| 4762 | fwd_decls = input_api.re.findall(class_pattern, contents) |
| 4763 | fwd_decls.extend(input_api.re.findall(struct_pattern, contents)) |
| 4764 | |
| 4765 | useless_fwd_decls = [] |
| 4766 | for decl in fwd_decls: |
| 4767 | count = sum(1 for _ in input_api.re.finditer( |
| 4768 | r'\b%s\b' % input_api.re.escape(decl), contents)) |
| 4769 | if count == 1: |
| 4770 | useless_fwd_decls.append(decl) |
| 4771 | |
| 4772 | if not useless_fwd_decls: |
| 4773 | continue |
| 4774 | |
| 4775 | for line in f.GenerateScmDiff().splitlines(): |
| 4776 | if (line.startswith('-') and not line.startswith('--') |
| 4777 | or line.startswith('+') and not line.startswith('++')): |
| 4778 | for decl in useless_fwd_decls: |
| 4779 | if input_api.re.search(r'\b%s\b' % decl, line[1:]): |
| 4780 | results.append( |
| 4781 | output_api.PresubmitPromptWarning( |
| 4782 | '%s: %s forward declaration is no longer needed' |
| 4783 | % (f.LocalPath(), decl))) |
| 4784 | useless_fwd_decls.remove(decl) |
| 4785 | |
| 4786 | return results |
| 4787 | |
| 4788 | |
| 4789 | def _CheckAndroidDebuggableBuild(input_api, output_api): |
| 4790 | """Checks that code uses BuildInfo.isDebugAndroid() instead of |
| 4791 | Build.TYPE.equals('') or ''.equals(Build.TYPE) to check if |
| 4792 | this is a debuggable build of Android. |
| 4793 | """ |
| 4794 | build_type_check_pattern = input_api.re.compile( |
| 4795 | r'\bBuild\.TYPE\.equals\(|\.equals\(\s*\bBuild\.TYPE\)') |
| 4796 | |
| 4797 | errors = [] |
| 4798 | |
| 4799 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 4800 | affected_file, |
| 4801 | files_to_skip=( |
| 4802 | _EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api. |
| 4803 | DEFAULT_FILES_TO_SKIP + ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4804 | r"^android_webview/support_library/boundary_interfaces/", |
| 4805 | r"^chrome/android/webapk/.*", |
| 4806 | r'^third_party/.*', |
| 4807 | r"tools/android/customtabs_benchmark/.*", |
| 4808 | r"webview/chromium/License.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4809 | )), |
| 4810 | files_to_check=[r'.*\.java$']) |
| 4811 | |
| 4812 | for f in input_api.AffectedSourceFiles(sources): |
| 4813 | for line_num, line in f.ChangedContents(): |
| 4814 | if build_type_check_pattern.search(line): |
| 4815 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4816 | |
| 4817 | results = [] |
| 4818 | |
| 4819 | if errors: |
| 4820 | results.append( |
| 4821 | output_api.PresubmitPromptWarning( |
| 4822 | 'Build.TYPE.equals or .equals(Build.TYPE) usage is detected.' |
| 4823 | ' Please use BuildInfo.isDebugAndroid() instead.', errors)) |
| 4824 | |
| 4825 | return results |
| 4826 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 4827 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4828 | # TODO: add unit tests |
| 4829 | def _CheckAndroidToastUsage(input_api, output_api): |
| 4830 | """Checks that code uses org.chromium.ui.widget.Toast instead of |
| 4831 | android.widget.Toast (Chromium Toast doesn't force hardware |
| 4832 | acceleration on low-end devices, saving memory). |
| 4833 | """ |
| 4834 | toast_import_pattern = input_api.re.compile( |
| 4835 | r'^import android\.widget\.Toast;$') |
| 4836 | |
| 4837 | errors = [] |
| 4838 | |
| 4839 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 4840 | affected_file, |
| 4841 | files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 4842 | DEFAULT_FILES_TO_SKIP + |
| 4843 | (r'^chromecast/.*', r'^remoting/.*')), |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4844 | files_to_check=[r'.*\.java$']) |
| 4845 | |
| 4846 | for f in input_api.AffectedSourceFiles(sources): |
| 4847 | for line_num, line in f.ChangedContents(): |
| 4848 | if toast_import_pattern.search(line): |
| 4849 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4850 | |
| 4851 | results = [] |
| 4852 | |
| 4853 | if errors: |
| 4854 | results.append( |
| 4855 | output_api.PresubmitError( |
| 4856 | 'android.widget.Toast usage is detected. Android toasts use hardware' |
| 4857 | ' acceleration, and can be\ncostly on low-end devices. Please use' |
| 4858 | ' org.chromium.ui.widget.Toast instead.\n' |
| 4859 | 'Contact [email protected] if you have any questions.', |
| 4860 | errors)) |
| 4861 | |
| 4862 | return results |
| 4863 | |
| 4864 | |
| 4865 | def _CheckAndroidCrLogUsage(input_api, output_api): |
| 4866 | """Checks that new logs using org.chromium.base.Log: |
| 4867 | - Are using 'TAG' as variable name for the tags (warn) |
| 4868 | - Are using a tag that is shorter than 20 characters (error) |
| 4869 | """ |
| 4870 | |
| 4871 | # Do not check format of logs in the given files |
| 4872 | cr_log_check_excluded_paths = [ |
| 4873 | # //chrome/android/webapk cannot depend on //base |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4874 | r"^chrome/android/webapk/.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4875 | # WebView license viewer code cannot depend on //base; used in stub APK. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4876 | r"^android_webview/glue/java/src/com/android/" |
| 4877 | r"webview/chromium/License.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4878 | # The customtabs_benchmark is a small app that does not depend on Chromium |
| 4879 | # java pieces. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4880 | r"tools/android/customtabs_benchmark/.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4881 | ] |
| 4882 | |
| 4883 | cr_log_import_pattern = input_api.re.compile( |
| 4884 | r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE) |
| 4885 | class_in_base_pattern = input_api.re.compile( |
| 4886 | r'^package org\.chromium\.base;$', input_api.re.MULTILINE) |
| 4887 | has_some_log_import_pattern = input_api.re.compile(r'^import .*\.Log;$', |
| 4888 | input_api.re.MULTILINE) |
| 4889 | # Extract the tag from lines like `Log.d(TAG, "*");` or `Log.d("TAG", "*");` |
| 4890 | log_call_pattern = input_api.re.compile(r'\bLog\.\w\((?P<tag>\"?\w+)') |
| 4891 | log_decl_pattern = input_api.re.compile( |
| 4892 | r'static final String TAG = "(?P<name>(.*))"') |
| 4893 | rough_log_decl_pattern = input_api.re.compile(r'\bString TAG\s*=') |
| 4894 | |
| 4895 | REF_MSG = ('See docs/android_logging.md for more info.') |
| 4896 | sources = lambda x: input_api.FilterSourceFile( |
| 4897 | x, |
| 4898 | files_to_check=[r'.*\.java$'], |
| 4899 | files_to_skip=cr_log_check_excluded_paths) |
| 4900 | |
| 4901 | tag_decl_errors = [] |
Andrew Grieve | d3a35d8 | 2024-01-02 21:24:38 | [diff] [blame] | 4902 | tag_length_errors = [] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4903 | tag_errors = [] |
| 4904 | tag_with_dot_errors = [] |
| 4905 | util_log_errors = [] |
| 4906 | |
| 4907 | for f in input_api.AffectedSourceFiles(sources): |
| 4908 | file_content = input_api.ReadFile(f) |
| 4909 | has_modified_logs = False |
| 4910 | # Per line checks |
| 4911 | if (cr_log_import_pattern.search(file_content) |
| 4912 | or (class_in_base_pattern.search(file_content) |
| 4913 | and not has_some_log_import_pattern.search(file_content))): |
| 4914 | # Checks to run for files using cr log |
| 4915 | for line_num, line in f.ChangedContents(): |
| 4916 | if rough_log_decl_pattern.search(line): |
| 4917 | has_modified_logs = True |
| 4918 | |
| 4919 | # Check if the new line is doing some logging |
| 4920 | match = log_call_pattern.search(line) |
| 4921 | if match: |
| 4922 | has_modified_logs = True |
| 4923 | |
| 4924 | # Make sure it uses "TAG" |
| 4925 | if not match.group('tag') == 'TAG': |
| 4926 | tag_errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4927 | else: |
| 4928 | # Report non cr Log function calls in changed lines |
| 4929 | for line_num, line in f.ChangedContents(): |
| 4930 | if log_call_pattern.search(line): |
| 4931 | util_log_errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4932 | |
| 4933 | # Per file checks |
| 4934 | if has_modified_logs: |
| 4935 | # Make sure the tag is using the "cr" prefix and is not too long |
| 4936 | match = log_decl_pattern.search(file_content) |
| 4937 | tag_name = match.group('name') if match else None |
| 4938 | if not tag_name: |
| 4939 | tag_decl_errors.append(f.LocalPath()) |
Andrew Grieve | d3a35d8 | 2024-01-02 21:24:38 | [diff] [blame] | 4940 | elif len(tag_name) > 20: |
| 4941 | tag_length_errors.append(f.LocalPath()) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4942 | elif '.' in tag_name: |
| 4943 | tag_with_dot_errors.append(f.LocalPath()) |
| 4944 | |
| 4945 | results = [] |
| 4946 | if tag_decl_errors: |
| 4947 | results.append( |
| 4948 | output_api.PresubmitPromptWarning( |
| 4949 | 'Please define your tags using the suggested format: .\n' |
| 4950 | '"private static final String TAG = "<package tag>".\n' |
| 4951 | 'They will be prepended with "cr_" automatically.\n' + REF_MSG, |
| 4952 | tag_decl_errors)) |
| 4953 | |
Andrew Grieve | d3a35d8 | 2024-01-02 21:24:38 | [diff] [blame] | 4954 | if tag_length_errors: |
| 4955 | results.append( |
| 4956 | output_api.PresubmitError( |
| 4957 | 'The tag length is restricted by the system to be at most ' |
| 4958 | '20 characters.\n' + REF_MSG, tag_length_errors)) |
| 4959 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4960 | if tag_errors: |
| 4961 | results.append( |
| 4962 | output_api.PresubmitPromptWarning( |
| 4963 | 'Please use a variable named "TAG" for your log tags.\n' + |
| 4964 | REF_MSG, tag_errors)) |
| 4965 | |
| 4966 | if util_log_errors: |
| 4967 | results.append( |
| 4968 | output_api.PresubmitPromptWarning( |
| 4969 | 'Please use org.chromium.base.Log for new logs.\n' + REF_MSG, |
| 4970 | util_log_errors)) |
| 4971 | |
| 4972 | if tag_with_dot_errors: |
| 4973 | results.append( |
| 4974 | output_api.PresubmitPromptWarning( |
| 4975 | 'Dot in log tags cause them to be elided in crash reports.\n' + |
| 4976 | REF_MSG, tag_with_dot_errors)) |
| 4977 | |
| 4978 | return results |
| 4979 | |
| 4980 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4981 | def _CheckAndroidTestAnnotationUsage(input_api, output_api): |
| 4982 | """Checks that android.test.suitebuilder.annotation.* is no longer used.""" |
| 4983 | deprecated_annotation_import_pattern = input_api.re.compile( |
| 4984 | r'^import android\.test\.suitebuilder\.annotation\..*;', |
| 4985 | input_api.re.MULTILINE) |
| 4986 | sources = lambda x: input_api.FilterSourceFile( |
| 4987 | x, files_to_check=[r'.*\.java$'], files_to_skip=None) |
| 4988 | errors = [] |
| 4989 | for f in input_api.AffectedFiles(file_filter=sources): |
| 4990 | for line_num, line in f.ChangedContents(): |
| 4991 | if deprecated_annotation_import_pattern.search(line): |
| 4992 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 4993 | |
| 4994 | results = [] |
| 4995 | if errors: |
| 4996 | results.append( |
| 4997 | output_api.PresubmitError( |
| 4998 | 'Annotations in android.test.suitebuilder.annotation have been' |
Mohamed Heikal | 3d7a94c | 2023-03-28 16:55:24 | [diff] [blame] | 4999 | ' deprecated since API level 24. Please use androidx.test.filters' |
| 5000 | ' from //third_party/androidx:androidx_test_runner_java instead.' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5001 | ' Contact [email protected] if you have any questions.', |
| 5002 | errors)) |
| 5003 | return results |
| 5004 | |
| 5005 | |
| 5006 | def _CheckAndroidNewMdpiAssetLocation(input_api, output_api): |
| 5007 | """Checks if MDPI assets are placed in a correct directory.""" |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5008 | file_filter = lambda f: (f.UnixLocalPath().endswith('.png') and |
| 5009 | ('/res/drawable/' in f.UnixLocalPath() or |
| 5010 | '/res/drawable-ldrtl/' in f.UnixLocalPath())) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5011 | errors = [] |
| 5012 | for f in input_api.AffectedFiles(include_deletes=False, |
| 5013 | file_filter=file_filter): |
| 5014 | errors.append(' %s' % f.LocalPath()) |
| 5015 | |
| 5016 | results = [] |
| 5017 | if errors: |
| 5018 | results.append( |
| 5019 | output_api.PresubmitError( |
| 5020 | 'MDPI assets should be placed in /res/drawable-mdpi/ or ' |
| 5021 | '/res/drawable-ldrtl-mdpi/\ninstead of /res/drawable/ and' |
| 5022 | '/res/drawable-ldrtl/.\n' |
| 5023 | 'Contact [email protected] if you have questions.', errors)) |
| 5024 | return results |
| 5025 | |
| 5026 | |
| 5027 | def _CheckAndroidWebkitImports(input_api, output_api): |
| 5028 | """Checks that code uses org.chromium.base.Callback instead of |
| 5029 | android.webview.ValueCallback except in the WebView glue layer |
| 5030 | and WebLayer. |
| 5031 | """ |
| 5032 | valuecallback_import_pattern = input_api.re.compile( |
| 5033 | r'^import android\.webkit\.ValueCallback;$') |
| 5034 | |
| 5035 | errors = [] |
| 5036 | |
| 5037 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 5038 | affected_file, |
| 5039 | files_to_skip=(_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + input_api. |
| 5040 | DEFAULT_FILES_TO_SKIP + ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5041 | r'^android_webview/glue/.*', |
elabadysayed | cbbaea7 | 2024-08-01 16:10:42 | [diff] [blame] | 5042 | r'^android_webview/support_library/.*', |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5043 | r'^weblayer/.*', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5044 | )), |
| 5045 | files_to_check=[r'.*\.java$']) |
| 5046 | |
| 5047 | for f in input_api.AffectedSourceFiles(sources): |
| 5048 | for line_num, line in f.ChangedContents(): |
| 5049 | if valuecallback_import_pattern.search(line): |
| 5050 | errors.append("%s:%d" % (f.LocalPath(), line_num)) |
| 5051 | |
| 5052 | results = [] |
| 5053 | |
| 5054 | if errors: |
| 5055 | results.append( |
| 5056 | output_api.PresubmitError( |
| 5057 | 'android.webkit.ValueCallback usage is detected outside of the glue' |
| 5058 | ' layer. To stay compatible with the support library, android.webkit.*' |
| 5059 | ' classes should only be used inside the glue layer and' |
| 5060 | ' org.chromium.base.Callback should be used instead.', errors)) |
| 5061 | |
| 5062 | return results |
| 5063 | |
| 5064 | |
| 5065 | def _CheckAndroidXmlStyle(input_api, output_api, is_check_on_upload): |
| 5066 | """Checks Android XML styles """ |
| 5067 | |
| 5068 | # Return early if no relevant files were modified. |
| 5069 | if not any( |
| 5070 | _IsXmlOrGrdFile(input_api, f.LocalPath()) |
| 5071 | for f in input_api.AffectedFiles(include_deletes=False)): |
| 5072 | return [] |
| 5073 | |
| 5074 | import sys |
| 5075 | original_sys_path = sys.path |
| 5076 | try: |
| 5077 | sys.path = sys.path + [ |
| 5078 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 5079 | 'android', 'checkxmlstyle') |
| 5080 | ] |
| 5081 | import checkxmlstyle |
| 5082 | finally: |
| 5083 | # Restore sys.path to what it was before. |
| 5084 | sys.path = original_sys_path |
| 5085 | |
| 5086 | if is_check_on_upload: |
| 5087 | return checkxmlstyle.CheckStyleOnUpload(input_api, output_api) |
| 5088 | else: |
| 5089 | return checkxmlstyle.CheckStyleOnCommit(input_api, output_api) |
| 5090 | |
| 5091 | |
| 5092 | def _CheckAndroidInfoBarDeprecation(input_api, output_api): |
| 5093 | """Checks Android Infobar Deprecation """ |
| 5094 | |
| 5095 | import sys |
| 5096 | original_sys_path = sys.path |
| 5097 | try: |
| 5098 | sys.path = sys.path + [ |
| 5099 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 5100 | 'android', 'infobar_deprecation') |
| 5101 | ] |
| 5102 | import infobar_deprecation |
| 5103 | finally: |
| 5104 | # Restore sys.path to what it was before. |
| 5105 | sys.path = original_sys_path |
| 5106 | |
| 5107 | return infobar_deprecation.CheckDeprecationOnUpload(input_api, output_api) |
| 5108 | |
| 5109 | |
| 5110 | class _PydepsCheckerResult: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5111 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5112 | def __init__(self, cmd, pydeps_path, process, old_contents): |
| 5113 | self._cmd = cmd |
| 5114 | self._pydeps_path = pydeps_path |
| 5115 | self._process = process |
| 5116 | self._old_contents = old_contents |
| 5117 | |
| 5118 | def GetError(self): |
| 5119 | """Returns an error message, or None.""" |
| 5120 | import difflib |
Andrew Grieve | d27620b6 | 2023-07-13 16:35:07 | [diff] [blame] | 5121 | new_contents = self._process.stdout.read().splitlines()[2:] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5122 | if self._process.wait() != 0: |
| 5123 | # STDERR should already be printed. |
| 5124 | return 'Command failed: ' + self._cmd |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5125 | if self._old_contents != new_contents: |
| 5126 | diff = '\n'.join( |
| 5127 | difflib.context_diff(self._old_contents, new_contents)) |
| 5128 | return ('File is stale: {}\n' |
| 5129 | 'Diff (apply to fix):\n' |
| 5130 | '{}\n' |
| 5131 | 'To regenerate, run:\n\n' |
| 5132 | ' {}').format(self._pydeps_path, diff, self._cmd) |
| 5133 | return None |
| 5134 | |
| 5135 | |
| 5136 | class PydepsChecker: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5137 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5138 | def __init__(self, input_api, pydeps_files): |
| 5139 | self._file_cache = {} |
| 5140 | self._input_api = input_api |
| 5141 | self._pydeps_files = pydeps_files |
| 5142 | |
| 5143 | def _LoadFile(self, path): |
| 5144 | """Returns the list of paths within a .pydeps file relative to //.""" |
| 5145 | if path not in self._file_cache: |
| 5146 | with open(path, encoding='utf-8') as f: |
| 5147 | self._file_cache[path] = f.read() |
| 5148 | return self._file_cache[path] |
| 5149 | |
| 5150 | def _ComputeNormalizedPydepsEntries(self, pydeps_path): |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 5151 | """Returns an iterable of paths within the .pydep, relativized to //.""" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5152 | pydeps_data = self._LoadFile(pydeps_path) |
| 5153 | uses_gn_paths = '--gn-paths' in pydeps_data |
| 5154 | entries = (l for l in pydeps_data.splitlines() |
| 5155 | if not l.startswith('#')) |
| 5156 | if uses_gn_paths: |
| 5157 | # Paths look like: //foo/bar/baz |
| 5158 | return (e[2:] for e in entries) |
| 5159 | else: |
| 5160 | # Paths look like: path/relative/to/file.pydeps |
| 5161 | os_path = self._input_api.os_path |
| 5162 | pydeps_dir = os_path.dirname(pydeps_path) |
| 5163 | return (os_path.normpath(os_path.join(pydeps_dir, e)) |
| 5164 | for e in entries) |
| 5165 | |
| 5166 | def _CreateFilesToPydepsMap(self): |
| 5167 | """Returns a map of local_path -> list_of_pydeps.""" |
| 5168 | ret = {} |
| 5169 | for pydep_local_path in self._pydeps_files: |
| 5170 | for path in self._ComputeNormalizedPydepsEntries(pydep_local_path): |
| 5171 | ret.setdefault(path, []).append(pydep_local_path) |
| 5172 | return ret |
| 5173 | |
| 5174 | def ComputeAffectedPydeps(self): |
| 5175 | """Returns an iterable of .pydeps files that might need regenerating.""" |
| 5176 | affected_pydeps = set() |
| 5177 | file_to_pydeps_map = None |
| 5178 | for f in self._input_api.AffectedFiles(include_deletes=True): |
| 5179 | local_path = f.LocalPath() |
| 5180 | # Changes to DEPS can lead to .pydeps changes if any .py files are in |
| 5181 | # subrepositories. We can't figure out which files change, so re-check |
| 5182 | # all files. |
| 5183 | # Changes to print_python_deps.py affect all .pydeps. |
| 5184 | if local_path in ('DEPS', 'PRESUBMIT.py' |
| 5185 | ) or local_path.endswith('print_python_deps.py'): |
| 5186 | return self._pydeps_files |
| 5187 | elif local_path.endswith('.pydeps'): |
| 5188 | if local_path in self._pydeps_files: |
| 5189 | affected_pydeps.add(local_path) |
| 5190 | elif local_path.endswith('.py'): |
| 5191 | if file_to_pydeps_map is None: |
| 5192 | file_to_pydeps_map = self._CreateFilesToPydepsMap() |
| 5193 | affected_pydeps.update(file_to_pydeps_map.get(local_path, ())) |
| 5194 | return affected_pydeps |
| 5195 | |
| 5196 | def DetermineIfStaleAsync(self, pydeps_path): |
| 5197 | """Runs print_python_deps.py to see if the files is stale.""" |
| 5198 | import os |
| 5199 | |
| 5200 | old_pydeps_data = self._LoadFile(pydeps_path).splitlines() |
| 5201 | if old_pydeps_data: |
| 5202 | cmd = old_pydeps_data[1][1:].strip() |
| 5203 | if '--output' not in cmd: |
| 5204 | cmd += ' --output ' + pydeps_path |
| 5205 | old_contents = old_pydeps_data[2:] |
| 5206 | else: |
| 5207 | # A default cmd that should work in most cases (as long as pydeps filename |
| 5208 | # matches the script name) so that PRESUBMIT.py does not crash if pydeps |
| 5209 | # file is empty/new. |
| 5210 | cmd = 'build/print_python_deps.py {} --root={} --output={}'.format( |
| 5211 | pydeps_path[:-4], os.path.dirname(pydeps_path), pydeps_path) |
| 5212 | old_contents = [] |
| 5213 | env = dict(os.environ) |
| 5214 | env['PYTHONDONTWRITEBYTECODE'] = '1' |
| 5215 | process = self._input_api.subprocess.Popen( |
| 5216 | cmd + ' --output ""', |
| 5217 | shell=True, |
| 5218 | env=env, |
| 5219 | stdout=self._input_api.subprocess.PIPE, |
| 5220 | encoding='utf-8') |
| 5221 | return _PydepsCheckerResult(cmd, pydeps_path, process, old_contents) |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 5222 | |
| 5223 | |
Tibor Goldschwendt | 360793f7 | 2019-06-25 18:23:49 | [diff] [blame] | 5224 | def _ParseGclientArgs(): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5225 | args = {} |
| 5226 | with open('build/config/gclient_args.gni', 'r') as f: |
| 5227 | for line in f: |
| 5228 | line = line.strip() |
| 5229 | if not line or line.startswith('#'): |
| 5230 | continue |
| 5231 | attribute, value = line.split('=') |
| 5232 | args[attribute.strip()] = value.strip() |
| 5233 | return args |
Tibor Goldschwendt | 360793f7 | 2019-06-25 18:23:49 | [diff] [blame] | 5234 | |
| 5235 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5236 | def CheckPydepsNeedsUpdating(input_api, output_api, checker_for_tests=None): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5237 | """Checks if a .pydeps file needs to be regenerated.""" |
| 5238 | # This check is for Python dependency lists (.pydeps files), and involves |
| 5239 | # paths not only in the PRESUBMIT.py, but also in the .pydeps files. It |
| 5240 | # doesn't work on Windows and Mac, so skip it on other platforms. |
| 5241 | if not input_api.platform.startswith('linux'): |
| 5242 | return [] |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 5243 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5244 | results = [] |
| 5245 | # First, check for new / deleted .pydeps. |
| 5246 | for f in input_api.AffectedFiles(include_deletes=True): |
| 5247 | # Check whether we are running the presubmit check for a file in src. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5248 | if f.LocalPath().endswith('.pydeps'): |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 5249 | # f.LocalPath is relative to repo (src, or internal repo). |
| 5250 | # os_path.exists is relative to src repo. |
| 5251 | # Therefore if os_path.exists is true, it means f.LocalPath is relative |
| 5252 | # to src and we can conclude that the pydeps is in src. |
| 5253 | exists = input_api.os_path.exists(f.LocalPath()) |
| 5254 | if f.Action() == 'D' and f.LocalPath() in _ALL_PYDEPS_FILES: |
| 5255 | results.append( |
| 5256 | output_api.PresubmitError( |
| 5257 | 'Please update _ALL_PYDEPS_FILES within //PRESUBMIT.py to ' |
| 5258 | 'remove %s' % f.LocalPath())) |
| 5259 | elif (f.Action() != 'D' and exists |
| 5260 | and f.LocalPath() not in _ALL_PYDEPS_FILES): |
| 5261 | results.append( |
| 5262 | output_api.PresubmitError( |
| 5263 | 'Please update _ALL_PYDEPS_FILES within //PRESUBMIT.py to ' |
| 5264 | 'include %s' % f.LocalPath())) |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 5265 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5266 | if results: |
| 5267 | return results |
| 5268 | |
Gavin Mak | 2388440 | 2024-07-25 20:39:26 | [diff] [blame] | 5269 | try: |
| 5270 | parsed_args = _ParseGclientArgs() |
| 5271 | except FileNotFoundError: |
| 5272 | message = ( |
| 5273 | 'build/config/gclient_args.gni not found. Please make sure your ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5274 | 'workspace has been initialized with gclient sync.') |
Gavin Mak | 2388440 | 2024-07-25 20:39:26 | [diff] [blame] | 5275 | import sys |
| 5276 | original_sys_path = sys.path |
| 5277 | try: |
| 5278 | sys.path = sys.path + [ |
| 5279 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5280 | 'third_party', 'depot_tools') |
Gavin Mak | 2388440 | 2024-07-25 20:39:26 | [diff] [blame] | 5281 | ] |
| 5282 | import gclient_utils |
| 5283 | if gclient_utils.IsEnvCog(): |
| 5284 | # Users will always hit this when they run presubmits before cog |
| 5285 | # workspace initialization finishes. The check shouldn't fail in |
| 5286 | # this case. This is an unavoidable workaround that's needed for |
| 5287 | # good presubmit UX for cog. |
| 5288 | results.append(output_api.PresubmitPromptWarning(message)) |
| 5289 | else: |
| 5290 | results.append(output_api.PresubmitError(message)) |
| 5291 | return results |
| 5292 | finally: |
| 5293 | # Restore sys.path to what it was before. |
| 5294 | sys.path = original_sys_path |
| 5295 | |
| 5296 | is_android = parsed_args.get('checkout_android', 'false') == 'true' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5297 | checker = checker_for_tests or PydepsChecker(input_api, _ALL_PYDEPS_FILES) |
| 5298 | affected_pydeps = set(checker.ComputeAffectedPydeps()) |
| 5299 | affected_android_pydeps = affected_pydeps.intersection( |
| 5300 | set(_ANDROID_SPECIFIC_PYDEPS_FILES)) |
| 5301 | if affected_android_pydeps and not is_android: |
| 5302 | results.append( |
| 5303 | output_api.PresubmitPromptOrNotify( |
| 5304 | 'You have changed python files that may affect pydeps for android\n' |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 5305 | 'specific scripts. However, the relevant presubmit check cannot be\n' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5306 | 'run because you are not using an Android checkout. To validate that\n' |
| 5307 | 'the .pydeps are correct, re-run presubmit in an Android checkout, or\n' |
| 5308 | 'use the android-internal-presubmit optional trybot.\n' |
| 5309 | 'Possibly stale pydeps files:\n{}'.format( |
| 5310 | '\n'.join(affected_android_pydeps)))) |
| 5311 | |
| 5312 | all_pydeps = _ALL_PYDEPS_FILES if is_android else _GENERIC_PYDEPS_FILES |
| 5313 | pydeps_to_check = affected_pydeps.intersection(all_pydeps) |
| 5314 | # Process these concurrently, as each one takes 1-2 seconds. |
| 5315 | pydep_results = [checker.DetermineIfStaleAsync(p) for p in pydeps_to_check] |
| 5316 | for result in pydep_results: |
| 5317 | error_msg = result.GetError() |
| 5318 | if error_msg: |
| 5319 | results.append(output_api.PresubmitError(error_msg)) |
| 5320 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 5321 | return results |
| 5322 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 5323 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5324 | def CheckSingletonInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5325 | """Checks to make sure no header files have |Singleton<|.""" |
| 5326 | |
| 5327 | def FileFilter(affected_file): |
| 5328 | # It's ok for base/memory/singleton.h to have |Singleton<|. |
| 5329 | files_to_skip = (_EXCLUDED_PATHS + input_api.DEFAULT_FILES_TO_SKIP + |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5330 | (r"^base/memory/singleton\.h$", |
| 5331 | r"^net/quic/platform/impl/quic_singleton_impl\.h$")) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5332 | return input_api.FilterSourceFile(affected_file, |
| 5333 | files_to_skip=files_to_skip) |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 5334 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5335 | pattern = input_api.re.compile(r'(?<!class\sbase::)Singleton\s*<') |
| 5336 | files = [] |
| 5337 | for f in input_api.AffectedSourceFiles(FileFilter): |
| 5338 | if (f.LocalPath().endswith('.h') or f.LocalPath().endswith('.hxx') |
| 5339 | or f.LocalPath().endswith('.hpp') |
| 5340 | or f.LocalPath().endswith('.inl')): |
| 5341 | contents = input_api.ReadFile(f) |
| 5342 | for line in contents.splitlines(False): |
| 5343 | if (not line.lstrip().startswith('//') |
| 5344 | and # Strip C++ comment. |
| 5345 | pattern.search(line)): |
| 5346 | files.append(f) |
| 5347 | break |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 5348 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5349 | if files: |
| 5350 | return [ |
| 5351 | output_api.PresubmitError( |
| 5352 | 'Found base::Singleton<T> in the following header files.\n' + |
| 5353 | 'Please move them to an appropriate source file so that the ' + |
| 5354 | 'template gets instantiated in a single compilation unit.', |
| 5355 | files) |
| 5356 | ] |
| 5357 | return [] |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 5358 | |
| 5359 | |
[email protected] | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 5360 | _DEPRECATED_CSS = [ |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5361 | # Values |
| 5362 | ("-webkit-box", "flex"), |
| 5363 | ("-webkit-inline-box", "inline-flex"), |
| 5364 | ("-webkit-flex", "flex"), |
| 5365 | ("-webkit-inline-flex", "inline-flex"), |
| 5366 | ("-webkit-min-content", "min-content"), |
| 5367 | ("-webkit-max-content", "max-content"), |
[email protected] | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 5368 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5369 | # Properties |
| 5370 | ("-webkit-background-clip", "background-clip"), |
| 5371 | ("-webkit-background-origin", "background-origin"), |
| 5372 | ("-webkit-background-size", "background-size"), |
| 5373 | ("-webkit-box-shadow", "box-shadow"), |
| 5374 | ("-webkit-user-select", "user-select"), |
[email protected] | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 5375 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5376 | # Functions |
| 5377 | ("-webkit-gradient", "gradient"), |
| 5378 | ("-webkit-repeating-gradient", "repeating-gradient"), |
| 5379 | ("-webkit-linear-gradient", "linear-gradient"), |
| 5380 | ("-webkit-repeating-linear-gradient", "repeating-linear-gradient"), |
| 5381 | ("-webkit-radial-gradient", "radial-gradient"), |
| 5382 | ("-webkit-repeating-radial-gradient", "repeating-radial-gradient"), |
[email protected] | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 5383 | ] |
| 5384 | |
Wei-Yin Chen (陳威尹) | f799d44 | 2018-07-31 02:20:20 | [diff] [blame] | 5385 | |
Wei-Yin Chen (陳威尹) | dca729a | 2018-07-31 21:35:49 | [diff] [blame] | 5386 | # TODO: add unit tests |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5387 | def CheckNoDeprecatedCss(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5388 | """ Make sure that we don't use deprecated CSS |
| 5389 | properties, functions or values. Our external |
| 5390 | documentation and iOS CSS for dom distiller |
| 5391 | (reader mode) are ignored by the hooks as it |
| 5392 | needs to be consumed by WebKit. """ |
| 5393 | results = [] |
| 5394 | file_inclusion_pattern = [r".+\.css$"] |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5395 | files_to_skip = ( |
| 5396 | _EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 5397 | input_api.DEFAULT_FILES_TO_SKIP + |
| 5398 | ( # Legacy CSS file using deprecated CSS. |
| 5399 | r"^chrome/browser/resources/chromeos/arc_support/cr_overlay.css$", |
| 5400 | r"^chrome/common/extensions/docs", |
| 5401 | r"^chrome/docs", |
| 5402 | r"^native_client_sdk", |
| 5403 | # The NTP team prefers reserving -webkit-line-clamp for |
| 5404 | # ellipsis effect which can only be used with -webkit-box. |
| 5405 | r"ui/webui/resources/cr_components/most_visited/.*\.css$")) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5406 | file_filter = lambda f: input_api.FilterSourceFile( |
| 5407 | f, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip) |
| 5408 | for fpath in input_api.AffectedFiles(file_filter=file_filter): |
| 5409 | for line_num, line in fpath.ChangedContents(): |
| 5410 | for (deprecated_value, value) in _DEPRECATED_CSS: |
| 5411 | if deprecated_value in line: |
| 5412 | results.append( |
| 5413 | output_api.PresubmitError( |
| 5414 | "%s:%d: Use of deprecated CSS %s, use %s instead" % |
| 5415 | (fpath.LocalPath(), line_num, deprecated_value, |
| 5416 | value))) |
| 5417 | return results |
[email protected] | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 5418 | |
mohan.reddy | f21db96 | 2014-10-16 12:26:47 | [diff] [blame] | 5419 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5420 | def CheckForRelativeIncludes(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5421 | bad_files = {} |
| 5422 | for f in input_api.AffectedFiles(include_deletes=False): |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 5423 | if (f.UnixLocalPath().startswith('third_party') |
| 5424 | and not f.LocalPath().startswith('third_party/blink')): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5425 | continue |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5426 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5427 | if not _IsCPlusPlusFile(input_api, f.LocalPath()): |
| 5428 | continue |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5429 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5430 | relative_includes = [ |
| 5431 | line for _, line in f.ChangedContents() |
| 5432 | if "#include" in line and "../" in line |
| 5433 | ] |
| 5434 | if not relative_includes: |
| 5435 | continue |
| 5436 | bad_files[f.LocalPath()] = relative_includes |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5437 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5438 | if not bad_files: |
| 5439 | return [] |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5440 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5441 | error_descriptions = [] |
| 5442 | for file_path, bad_lines in bad_files.items(): |
| 5443 | error_description = file_path |
| 5444 | for line in bad_lines: |
| 5445 | error_description += '\n ' + line |
| 5446 | error_descriptions.append(error_description) |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5447 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5448 | results = [] |
| 5449 | results.append( |
| 5450 | output_api.PresubmitError( |
| 5451 | 'You added one or more relative #include paths (including "../").\n' |
| 5452 | 'These shouldn\'t be used because they can be used to include headers\n' |
| 5453 | 'from code that\'s not correctly specified as a dependency in the\n' |
| 5454 | 'relevant BUILD.gn file(s).', error_descriptions)) |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5455 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5456 | return results |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5457 | |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5458 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5459 | def CheckForCcIncludes(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5460 | """Check that nobody tries to include a cc file. It's a relatively |
| 5461 | common error which results in duplicate symbols in object |
| 5462 | files. This may not always break the build until someone later gets |
| 5463 | very confusing linking errors.""" |
| 5464 | results = [] |
| 5465 | for f in input_api.AffectedFiles(include_deletes=False): |
| 5466 | # We let third_party code do whatever it wants |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 5467 | if (f.UnixLocalPath().startswith('third_party') |
| 5468 | and not f.LocalPath().startswith('third_party/blink')): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5469 | continue |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 5470 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5471 | if not _IsCPlusPlusFile(input_api, f.LocalPath()): |
| 5472 | continue |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 5473 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5474 | for _, line in f.ChangedContents(): |
| 5475 | if line.startswith('#include "'): |
| 5476 | included_file = line.split('"')[1] |
| 5477 | if _IsCPlusPlusFile(input_api, included_file): |
| 5478 | # The most common naming for external files with C++ code, |
| 5479 | # apart from standard headers, is to call them foo.inc, but |
| 5480 | # Chromium sometimes uses foo-inc.cc so allow that as well. |
| 5481 | if not included_file.endswith(('.h', '-inc.cc')): |
| 5482 | results.append( |
| 5483 | output_api.PresubmitError( |
| 5484 | 'Only header files or .inc files should be included in other\n' |
| 5485 | 'C++ files. Compiling the contents of a cc file more than once\n' |
| 5486 | 'will cause duplicate information in the build which may later\n' |
| 5487 | 'result in strange link_errors.\n' + |
| 5488 | f.LocalPath() + ':\n ' + line)) |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 5489 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5490 | return results |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 5491 | |
| 5492 | |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5493 | def _CheckWatchlistDefinitionsEntrySyntax(key, value, ast): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5494 | if not isinstance(key, ast.Str): |
| 5495 | return 'Key at line %d must be a string literal' % key.lineno |
| 5496 | if not isinstance(value, ast.Dict): |
| 5497 | return 'Value at line %d must be a dict' % value.lineno |
| 5498 | if len(value.keys) != 1: |
| 5499 | return 'Dict at line %d must have single entry' % value.lineno |
| 5500 | if not isinstance(value.keys[0], ast.Str) or value.keys[0].s != 'filepath': |
| 5501 | return ( |
| 5502 | 'Entry at line %d must have a string literal \'filepath\' as key' % |
| 5503 | value.lineno) |
| 5504 | return None |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5505 | |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5506 | |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 5507 | def _CheckWatchlistsEntrySyntax(key, value, ast, email_regex): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5508 | if not isinstance(key, ast.Str): |
| 5509 | return 'Key at line %d must be a string literal' % key.lineno |
| 5510 | if not isinstance(value, ast.List): |
| 5511 | return 'Value at line %d must be a list' % value.lineno |
| 5512 | for element in value.elts: |
| 5513 | if not isinstance(element, ast.Str): |
| 5514 | return 'Watchlist elements on line %d is not a string' % key.lineno |
| 5515 | if not email_regex.match(element.s): |
| 5516 | return ('Watchlist element on line %d doesn\'t look like a valid ' |
| 5517 | + 'email: %s') % (key.lineno, element.s) |
| 5518 | return None |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5519 | |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5520 | |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 5521 | def _CheckWATCHLISTSEntries(wd_dict, w_dict, input_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5522 | mismatch_template = ( |
| 5523 | 'Mismatch between WATCHLIST_DEFINITIONS entry (%s) and WATCHLISTS ' |
| 5524 | 'entry (%s)') |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5525 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5526 | email_regex = input_api.re.compile( |
| 5527 | r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+$") |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 5528 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5529 | ast = input_api.ast |
| 5530 | i = 0 |
| 5531 | last_key = '' |
| 5532 | while True: |
| 5533 | if i >= len(wd_dict.keys): |
| 5534 | if i >= len(w_dict.keys): |
| 5535 | return None |
| 5536 | return mismatch_template % ('missing', |
| 5537 | 'line %d' % w_dict.keys[i].lineno) |
| 5538 | elif i >= len(w_dict.keys): |
| 5539 | return (mismatch_template % |
| 5540 | ('line %d' % wd_dict.keys[i].lineno, 'missing')) |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5541 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5542 | wd_key = wd_dict.keys[i] |
| 5543 | w_key = w_dict.keys[i] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5544 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5545 | result = _CheckWatchlistDefinitionsEntrySyntax(wd_key, |
| 5546 | wd_dict.values[i], ast) |
| 5547 | if result is not None: |
| 5548 | return 'Bad entry in WATCHLIST_DEFINITIONS dict: %s' % result |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5549 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5550 | result = _CheckWatchlistsEntrySyntax(w_key, w_dict.values[i], ast, |
| 5551 | email_regex) |
| 5552 | if result is not None: |
| 5553 | return 'Bad entry in WATCHLISTS dict: %s' % result |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5554 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5555 | if wd_key.s != w_key.s: |
| 5556 | return mismatch_template % ('%s at line %d' % |
| 5557 | (wd_key.s, wd_key.lineno), |
| 5558 | '%s at line %d' % |
| 5559 | (w_key.s, w_key.lineno)) |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5560 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5561 | if wd_key.s < last_key: |
| 5562 | return ( |
| 5563 | 'WATCHLISTS dict is not sorted lexicographically at line %d and %d' |
| 5564 | % (wd_key.lineno, w_key.lineno)) |
| 5565 | last_key = wd_key.s |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5566 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5567 | i = i + 1 |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5568 | |
| 5569 | |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 5570 | def _CheckWATCHLISTSSyntax(expression, input_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5571 | ast = input_api.ast |
| 5572 | if not isinstance(expression, ast.Expression): |
| 5573 | return 'WATCHLISTS file must contain a valid expression' |
| 5574 | dictionary = expression.body |
| 5575 | if not isinstance(dictionary, ast.Dict) or len(dictionary.keys) != 2: |
| 5576 | return 'WATCHLISTS file must have single dict with exactly two entries' |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5577 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5578 | first_key = dictionary.keys[0] |
| 5579 | first_value = dictionary.values[0] |
| 5580 | second_key = dictionary.keys[1] |
| 5581 | second_value = dictionary.values[1] |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5582 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5583 | if (not isinstance(first_key, ast.Str) |
| 5584 | or first_key.s != 'WATCHLIST_DEFINITIONS' |
| 5585 | or not isinstance(first_value, ast.Dict)): |
| 5586 | return ('The first entry of the dict in WATCHLISTS file must be ' |
| 5587 | 'WATCHLIST_DEFINITIONS dict') |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5588 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5589 | if (not isinstance(second_key, ast.Str) or second_key.s != 'WATCHLISTS' |
| 5590 | or not isinstance(second_value, ast.Dict)): |
| 5591 | return ('The second entry of the dict in WATCHLISTS file must be ' |
| 5592 | 'WATCHLISTS dict') |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5593 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5594 | return _CheckWATCHLISTSEntries(first_value, second_value, input_api) |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5595 | |
| 5596 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5597 | def CheckWATCHLISTS(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5598 | for f in input_api.AffectedFiles(include_deletes=False): |
| 5599 | if f.LocalPath() == 'WATCHLISTS': |
| 5600 | contents = input_api.ReadFile(f, 'r') |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5601 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5602 | try: |
| 5603 | # First, make sure that it can be evaluated. |
| 5604 | input_api.ast.literal_eval(contents) |
| 5605 | # Get an AST tree for it and scan the tree for detailed style checking. |
| 5606 | expression = input_api.ast.parse(contents, |
| 5607 | filename='WATCHLISTS', |
| 5608 | mode='eval') |
| 5609 | except ValueError as e: |
| 5610 | return [ |
| 5611 | output_api.PresubmitError('Cannot parse WATCHLISTS file', |
| 5612 | long_text=repr(e)) |
| 5613 | ] |
| 5614 | except SyntaxError as e: |
| 5615 | return [ |
| 5616 | output_api.PresubmitError('Cannot parse WATCHLISTS file', |
| 5617 | long_text=repr(e)) |
| 5618 | ] |
| 5619 | except TypeError as e: |
| 5620 | return [ |
| 5621 | output_api.PresubmitError('Cannot parse WATCHLISTS file', |
| 5622 | long_text=repr(e)) |
| 5623 | ] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5624 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5625 | result = _CheckWATCHLISTSSyntax(expression, input_api) |
| 5626 | if result is not None: |
| 5627 | return [output_api.PresubmitError(result)] |
| 5628 | break |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5629 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5630 | return [] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5631 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5632 | |
Sean Kau | cb7c9b3 | 2022-10-25 21:25:52 | [diff] [blame] | 5633 | def CheckGnRebasePath(input_api, output_api): |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 5634 | """Checks that target_gen_dir is not used with "//" in rebase_path(). |
Sean Kau | cb7c9b3 | 2022-10-25 21:25:52 | [diff] [blame] | 5635 | |
| 5636 | Developers should use root_build_dir instead of "//" when using target_gen_dir because |
| 5637 | Chromium is sometimes built outside of the source tree. |
| 5638 | """ |
| 5639 | |
| 5640 | def gn_files(f): |
| 5641 | return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gn', )) |
| 5642 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5643 | rebase_path_regex = input_api.re.compile( |
| 5644 | r'rebase_path\(("\$target_gen_dir"|target_gen_dir), ("/"|"//")\)') |
Sean Kau | cb7c9b3 | 2022-10-25 21:25:52 | [diff] [blame] | 5645 | problems = [] |
| 5646 | for f in input_api.AffectedSourceFiles(gn_files): |
| 5647 | for line_num, line in f.ChangedContents(): |
| 5648 | if rebase_path_regex.search(line): |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5649 | problems.append('Absolute path in rebase_path() in %s:%d' % |
| 5650 | (f.LocalPath(), line_num)) |
Sean Kau | cb7c9b3 | 2022-10-25 21:25:52 | [diff] [blame] | 5651 | |
| 5652 | if problems: |
| 5653 | return [ |
| 5654 | output_api.PresubmitPromptWarning( |
| 5655 | 'Using an absolute path in rebase_path()', |
| 5656 | items=sorted(problems), |
| 5657 | long_text=( |
| 5658 | 'rebase_path() should use root_build_dir instead of "/" ', |
| 5659 | 'since builds can be initiated from outside of the source ', |
| 5660 | 'root.')) |
| 5661 | ] |
| 5662 | return [] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5663 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5664 | |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5665 | def CheckGnGlobForward(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5666 | """Checks that forward_variables_from(invoker, "*") follows best practices. |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5667 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5668 | As documented at //build/docs/writing_gn_templates.md |
| 5669 | """ |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5670 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5671 | def gn_files(f): |
| 5672 | return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gni', )) |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5673 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5674 | problems = [] |
| 5675 | for f in input_api.AffectedSourceFiles(gn_files): |
| 5676 | for line_num, line in f.ChangedContents(): |
| 5677 | if 'forward_variables_from(invoker, "*")' in line: |
| 5678 | problems.append( |
| 5679 | 'Bare forward_variables_from(invoker, "*") in %s:%d' % |
| 5680 | (f.LocalPath(), line_num)) |
| 5681 | |
| 5682 | if problems: |
| 5683 | return [ |
| 5684 | output_api.PresubmitPromptWarning( |
| 5685 | 'forward_variables_from("*") without exclusions', |
| 5686 | items=sorted(problems), |
| 5687 | long_text=( |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 5688 | 'The variables "visibility" and "test_only" should be ' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5689 | 'explicitly listed in forward_variables_from(). For more ' |
| 5690 | 'details, see:\n' |
| 5691 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/' |
| 5692 | 'build/docs/writing_gn_templates.md' |
| 5693 | '#Using-forward_variables_from')) |
| 5694 | ] |
| 5695 | return [] |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5696 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5697 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5698 | def CheckNewHeaderWithoutGnChangeOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5699 | """Checks that newly added header files have corresponding GN changes. |
| 5700 | Note that this is only a heuristic. To be precise, run script: |
| 5701 | build/check_gn_headers.py. |
| 5702 | """ |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5703 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5704 | def headers(f): |
| 5705 | return input_api.FilterSourceFile( |
| 5706 | f, files_to_check=(r'.+%s' % _HEADER_EXTENSIONS, )) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5707 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5708 | new_headers = [] |
| 5709 | for f in input_api.AffectedSourceFiles(headers): |
| 5710 | if f.Action() != 'A': |
| 5711 | continue |
| 5712 | new_headers.append(f.LocalPath()) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5713 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5714 | def gn_files(f): |
| 5715 | return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gn', )) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5716 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5717 | all_gn_changed_contents = '' |
| 5718 | for f in input_api.AffectedSourceFiles(gn_files): |
| 5719 | for _, line in f.ChangedContents(): |
| 5720 | all_gn_changed_contents += line |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5721 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5722 | problems = [] |
| 5723 | for header in new_headers: |
| 5724 | basename = input_api.os_path.basename(header) |
| 5725 | if basename not in all_gn_changed_contents: |
| 5726 | problems.append(header) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5727 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5728 | if problems: |
| 5729 | return [ |
| 5730 | output_api.PresubmitPromptWarning( |
| 5731 | 'Missing GN changes for new header files', |
| 5732 | items=sorted(problems), |
| 5733 | long_text= |
| 5734 | 'Please double check whether newly added header files need ' |
| 5735 | 'corresponding changes in gn or gni files.\nThis checking is only a ' |
| 5736 | 'heuristic. Run build/check_gn_headers.py to be precise.\n' |
| 5737 | 'Read https://crbug.com/661774 for more info.') |
| 5738 | ] |
| 5739 | return [] |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5740 | |
| 5741 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5742 | def CheckCorrectProductNameInMessages(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5743 | """Check that Chromium-branded strings don't include "Chrome" or vice versa. |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5744 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5745 | This assumes we won't intentionally reference one product from the other |
| 5746 | product. |
| 5747 | """ |
| 5748 | all_problems = [] |
| 5749 | test_cases = [{ |
| 5750 | "filename_postfix": "google_chrome_strings.grd", |
| 5751 | "correct_name": "Chrome", |
| 5752 | "incorrect_name": "Chromium", |
| 5753 | }, { |
Thiago Perrotta | 099034f | 2023-06-05 18:10:20 | [diff] [blame] | 5754 | "filename_postfix": "google_chrome_strings.grd", |
| 5755 | "correct_name": "Chrome", |
| 5756 | "incorrect_name": "Chrome for Testing", |
| 5757 | }, { |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5758 | "filename_postfix": "chromium_strings.grd", |
| 5759 | "correct_name": "Chromium", |
| 5760 | "incorrect_name": "Chrome", |
| 5761 | }] |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5762 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5763 | for test_case in test_cases: |
| 5764 | problems = [] |
| 5765 | filename_filter = lambda x: x.LocalPath().endswith(test_case[ |
| 5766 | "filename_postfix"]) |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5767 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5768 | # Check each new line. Can yield false positives in multiline comments, but |
| 5769 | # easier than trying to parse the XML because messages can have nested |
| 5770 | # children, and associating message elements with affected lines is hard. |
| 5771 | for f in input_api.AffectedSourceFiles(filename_filter): |
| 5772 | for line_num, line in f.ChangedContents(): |
| 5773 | if "<message" in line or "<!--" in line or "-->" in line: |
| 5774 | continue |
| 5775 | if test_case["incorrect_name"] in line: |
Thiago Perrotta | 099034f | 2023-06-05 18:10:20 | [diff] [blame] | 5776 | # Chrome for Testing is a special edge case: https://goo.gle/chrome-for-testing#bookmark=id.n1rat320av91 |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5777 | if (test_case["correct_name"] == "Chromium" |
| 5778 | and line.count("Chrome") |
| 5779 | == line.count("Chrome for Testing")): |
Thiago Perrotta | 099034f | 2023-06-05 18:10:20 | [diff] [blame] | 5780 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5781 | problems.append("Incorrect product name in %s:%d" % |
| 5782 | (f.LocalPath(), line_num)) |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5783 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5784 | if problems: |
| 5785 | message = ( |
| 5786 | "Strings in %s-branded string files should reference \"%s\", not \"%s\"" |
| 5787 | % (test_case["correct_name"], test_case["correct_name"], |
| 5788 | test_case["incorrect_name"])) |
| 5789 | all_problems.append( |
| 5790 | output_api.PresubmitPromptWarning(message, items=problems)) |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5791 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5792 | return all_problems |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5793 | |
| 5794 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5795 | def CheckForTooLargeFiles(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5796 | """Avoid large files, especially binary files, in the repository since |
| 5797 | git doesn't scale well for those. They will be in everyone's repo |
| 5798 | clones forever, forever making Chromium slower to clone and work |
| 5799 | with.""" |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5800 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5801 | # Uploading files to cloud storage is not trivial so we don't want |
| 5802 | # to set the limit too low, but the upper limit for "normal" large |
| 5803 | # files seems to be 1-2 MB, with a handful around 5-8 MB, so |
| 5804 | # anything over 20 MB is exceptional. |
Bruce Dawson | bb414db | 2022-12-27 20:21:25 | [diff] [blame] | 5805 | TOO_LARGE_FILE_SIZE_LIMIT = 20 * 1024 * 1024 |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5806 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5807 | too_large_files = [] |
| 5808 | for f in input_api.AffectedFiles(): |
| 5809 | # Check both added and modified files (but not deleted files). |
| 5810 | if f.Action() in ('A', 'M'): |
| 5811 | size = input_api.os_path.getsize(f.AbsoluteLocalPath()) |
Joe DeBlasio | 10a832f | 2023-04-21 20:20:18 | [diff] [blame] | 5812 | if size > TOO_LARGE_FILE_SIZE_LIMIT: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5813 | too_large_files.append("%s: %d bytes" % (f.LocalPath(), size)) |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5814 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5815 | if too_large_files: |
| 5816 | message = ( |
| 5817 | 'Do not commit large files to git since git scales badly for those.\n' |
| 5818 | + |
| 5819 | 'Instead put the large files in cloud storage and use DEPS to\n' + |
| 5820 | 'fetch them.\n' + '\n'.join(too_large_files)) |
| 5821 | return [ |
| 5822 | output_api.PresubmitError('Too large files found in commit', |
| 5823 | long_text=message + '\n') |
| 5824 | ] |
| 5825 | else: |
| 5826 | return [] |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5827 | |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5828 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5829 | def CheckFuzzTargetsOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5830 | """Checks specific for fuzz target sources.""" |
| 5831 | EXPORTED_SYMBOLS = [ |
| 5832 | 'LLVMFuzzerInitialize', |
| 5833 | 'LLVMFuzzerCustomMutator', |
| 5834 | 'LLVMFuzzerCustomCrossOver', |
| 5835 | 'LLVMFuzzerMutate', |
| 5836 | ] |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5837 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5838 | REQUIRED_HEADER = '#include "testing/libfuzzer/libfuzzer_exports.h"' |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5839 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5840 | def FilterFile(affected_file): |
| 5841 | """Ignore libFuzzer source code.""" |
| 5842 | files_to_check = r'.*fuzz.*\.(h|hpp|hcc|cc|cpp|cxx)$' |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5843 | files_to_skip = r"^third_party/libFuzzer" |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5844 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5845 | return input_api.FilterSourceFile(affected_file, |
| 5846 | files_to_check=[files_to_check], |
| 5847 | files_to_skip=[files_to_skip]) |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5848 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5849 | files_with_missing_header = [] |
| 5850 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 5851 | contents = input_api.ReadFile(f, 'r') |
| 5852 | if REQUIRED_HEADER in contents: |
| 5853 | continue |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5854 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5855 | if any(symbol in contents for symbol in EXPORTED_SYMBOLS): |
| 5856 | files_with_missing_header.append(f.LocalPath()) |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5857 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5858 | if not files_with_missing_header: |
| 5859 | return [] |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5860 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5861 | long_text = ( |
| 5862 | 'If you define any of the libFuzzer optional functions (%s), it is ' |
| 5863 | 'recommended to add \'%s\' directive. Otherwise, the fuzz target may ' |
| 5864 | 'work incorrectly on Mac (crbug.com/687076).\nNote that ' |
| 5865 | 'LLVMFuzzerInitialize should not be used, unless your fuzz target needs ' |
| 5866 | 'to access command line arguments passed to the fuzzer. Instead, prefer ' |
| 5867 | 'static initialization and shared resources as documented in ' |
| 5868 | 'https://chromium.googlesource.com/chromium/src/+/main/testing/' |
| 5869 | 'libfuzzer/efficient_fuzzing.md#simplifying-initialization_cleanup.\n' |
| 5870 | % (', '.join(EXPORTED_SYMBOLS), REQUIRED_HEADER)) |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5871 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5872 | return [ |
| 5873 | output_api.PresubmitPromptWarning(message="Missing '%s' in:" % |
| 5874 | REQUIRED_HEADER, |
| 5875 | items=files_with_missing_header, |
| 5876 | long_text=long_text) |
| 5877 | ] |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5878 | |
| 5879 | |
Mohamed Heikal | d048240a | 2019-11-12 16:57:37 | [diff] [blame] | 5880 | def _CheckNewImagesWarning(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5881 | """ |
| 5882 | Warns authors who add images into the repo to make sure their images are |
| 5883 | optimized before committing. |
| 5884 | """ |
| 5885 | images_added = False |
| 5886 | image_paths = [] |
| 5887 | errors = [] |
| 5888 | filter_lambda = lambda x: input_api.FilterSourceFile( |
| 5889 | x, |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5890 | files_to_skip=( |
| 5891 | ('(?i).*test', r'.*\/junit\/') + input_api.DEFAULT_FILES_TO_SKIP), |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5892 | files_to_check=[r'.*\/(drawable|mipmap)']) |
| 5893 | for f in input_api.AffectedFiles(include_deletes=False, |
| 5894 | file_filter=filter_lambda): |
| 5895 | local_path = f.LocalPath().lower() |
| 5896 | if any( |
| 5897 | local_path.endswith(extension) |
| 5898 | for extension in _IMAGE_EXTENSIONS): |
| 5899 | images_added = True |
| 5900 | image_paths.append(f) |
| 5901 | if images_added: |
| 5902 | errors.append( |
| 5903 | output_api.PresubmitPromptWarning( |
| 5904 | 'It looks like you are trying to commit some images. If these are ' |
| 5905 | 'non-test-only images, please make sure to read and apply the tips in ' |
| 5906 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/docs/speed/' |
| 5907 | 'binary_size/optimization_advice.md#optimizing-images\nThis check is ' |
| 5908 | 'FYI only and will not block your CL on the CQ.', image_paths)) |
| 5909 | return errors |
Mohamed Heikal | d048240a | 2019-11-12 16:57:37 | [diff] [blame] | 5910 | |
| 5911 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5912 | def ChecksAndroidSpecificOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5913 | """Groups upload checks that target android code.""" |
| 5914 | results = [] |
| 5915 | results.extend(_CheckAndroidCrLogUsage(input_api, output_api)) |
| 5916 | results.extend(_CheckAndroidDebuggableBuild(input_api, output_api)) |
| 5917 | results.extend(_CheckAndroidNewMdpiAssetLocation(input_api, output_api)) |
| 5918 | results.extend(_CheckAndroidToastUsage(input_api, output_api)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5919 | results.extend(_CheckAndroidTestAnnotationUsage(input_api, output_api)) |
| 5920 | results.extend(_CheckAndroidWebkitImports(input_api, output_api)) |
| 5921 | results.extend(_CheckAndroidXmlStyle(input_api, output_api, True)) |
| 5922 | results.extend(_CheckNewImagesWarning(input_api, output_api)) |
| 5923 | results.extend(_CheckAndroidNoBannedImports(input_api, output_api)) |
| 5924 | results.extend(_CheckAndroidInfoBarDeprecation(input_api, output_api)) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5925 | results.extend(_CheckAndroidNullAwayAnnotatedClasses( |
| 5926 | input_api, output_api)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5927 | return results |
| 5928 | |
Becky Zhou | 7c69b5099 | 2018-12-10 19:37:57 | [diff] [blame] | 5929 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5930 | def ChecksAndroidSpecificOnCommit(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5931 | """Groups commit checks that target android code.""" |
| 5932 | results = [] |
| 5933 | results.extend(_CheckAndroidXmlStyle(input_api, output_api, False)) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5934 | results.extend(_CheckAndroidNullAwayAnnotatedClasses( |
| 5935 | input_api, output_api)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5936 | return results |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 5937 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5938 | |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5939 | # TODO(chrishall): could we additionally match on any path owned by |
| 5940 | # ui/accessibility/OWNERS ? |
| 5941 | _ACCESSIBILITY_PATHS = ( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5942 | r"^chrome/browser.*/accessibility/", |
| 5943 | r"^chrome/browser/extensions/api/automation.*/", |
| 5944 | r"^chrome/renderer/extensions/accessibility_.*", |
| 5945 | r"^chrome/tests/data/accessibility/", |
| 5946 | r"^content/browser/accessibility/", |
| 5947 | r"^content/renderer/accessibility/", |
| 5948 | r"^content/tests/data/accessibility/", |
| 5949 | r"^extensions/renderer/api/automation/", |
Katie Dektar | 58ef07b | 2022-09-27 13:19:17 | [diff] [blame] | 5950 | r"^services/accessibility/", |
Abigail Klein | 7a63c57 | 2024-02-28 20:45:09 | [diff] [blame] | 5951 | r"^services/screen_ai/", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5952 | r"^ui/accessibility/", |
| 5953 | r"^ui/views/accessibility/", |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5954 | ) |
| 5955 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5956 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5957 | def CheckAccessibilityRelnotesField(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5958 | """Checks that commits to accessibility code contain an AX-Relnotes field in |
| 5959 | their commit message.""" |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5960 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5961 | def FileFilter(affected_file): |
| 5962 | paths = _ACCESSIBILITY_PATHS |
| 5963 | return input_api.FilterSourceFile(affected_file, files_to_check=paths) |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5964 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5965 | # Only consider changes affecting accessibility paths. |
| 5966 | if not any(input_api.AffectedFiles(file_filter=FileFilter)): |
| 5967 | return [] |
Akihiro Ota | 08108e54 | 2020-05-20 15:30:53 | [diff] [blame] | 5968 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5969 | # AX-Relnotes can appear in either the description or the footer. |
| 5970 | # When searching the description, require 'AX-Relnotes:' to appear at the |
| 5971 | # beginning of a line. |
| 5972 | ax_regex = input_api.re.compile('ax-relnotes[:=]') |
| 5973 | description_has_relnotes = any( |
| 5974 | ax_regex.match(line) |
| 5975 | for line in input_api.change.DescriptionText().lower().splitlines()) |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5976 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5977 | footer_relnotes = input_api.change.GitFootersFromDescription().get( |
| 5978 | 'AX-Relnotes', []) |
| 5979 | if description_has_relnotes or footer_relnotes: |
| 5980 | return [] |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5981 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5982 | # TODO(chrishall): link to Relnotes documentation in message. |
| 5983 | message = ( |
| 5984 | "Missing 'AX-Relnotes:' field required for accessibility changes" |
| 5985 | "\n please add 'AX-Relnotes: [release notes].' to describe any " |
| 5986 | "user-facing changes" |
| 5987 | "\n otherwise add 'AX-Relnotes: n/a.' if this change has no " |
| 5988 | "user-facing effects" |
| 5989 | "\n if this is confusing or annoying then please contact members " |
| 5990 | "of ui/accessibility/OWNERS.") |
| 5991 | |
| 5992 | return [output_api.PresubmitNotifyResult(message)] |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 5993 | |
Mark Schillaci | 44c90b4 | 2024-11-22 20:44:38 | [diff] [blame] | 5994 | |
| 5995 | _ACCESSIBILITY_ARIA_METHOD_CANDIDATES_PATTERNS = r'(\-\>|\.)(get|has|FastGet|FastHas)Attribute\(' |
| 5996 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5997 | _ACCESSIBILITY_ARIA_BAD_PARAMS_PATTERNS = (r"\(html_names::kAria(.*)Attr\)", |
| 5998 | r"\(html_names::kRoleAttr\)") |
Mark Schillaci | 44c90b4 | 2024-11-22 20:44:38 | [diff] [blame] | 5999 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6000 | _ACCESSIBILITY_ARIA_FILE_CANDIDATES_PATTERNS = (r".*/accessibility/.*.(cc|h)", |
| 6001 | r".*/ax_.*.(cc|h)") |
| 6002 | |
Mark Schillaci | 44c90b4 | 2024-11-22 20:44:38 | [diff] [blame] | 6003 | |
| 6004 | def CheckAccessibilityAriaElementAttributeGetters(input_api, output_api): |
| 6005 | """Checks that the blink accessibility code follows the defined patterns |
| 6006 | for checking aria attributes, so that ElementInternals is not bypassed.""" |
| 6007 | |
| 6008 | # Limit to accessibility-related files. |
| 6009 | def FileFilter(affected_file): |
| 6010 | paths = _ACCESSIBILITY_ARIA_FILE_CANDIDATES_PATTERNS |
| 6011 | return input_api.FilterSourceFile(affected_file, files_to_check=paths) |
| 6012 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6013 | aria_method_regex = input_api.re.compile( |
| 6014 | _ACCESSIBILITY_ARIA_METHOD_CANDIDATES_PATTERNS) |
Mark Schillaci | 44c90b4 | 2024-11-22 20:44:38 | [diff] [blame] | 6015 | aria_bad_params_regex = input_api.re.compile( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6016 | "|".join(_ACCESSIBILITY_ARIA_BAD_PARAMS_PATTERNS)) |
Mark Schillaci | 44c90b4 | 2024-11-22 20:44:38 | [diff] [blame] | 6017 | problems = [] |
| 6018 | |
| 6019 | for f in input_api.AffectedSourceFiles(FileFilter): |
| 6020 | for line_num, line in f.ChangedContents(): |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6021 | if aria_method_regex.search(line) and aria_bad_params_regex.search( |
| 6022 | line): |
| 6023 | problems.append( |
| 6024 | f"{f.LocalPath()}:{line_num}\n {line.strip()}") |
Mark Schillaci | 44c90b4 | 2024-11-22 20:44:38 | [diff] [blame] | 6025 | |
| 6026 | if problems: |
| 6027 | return [ |
| 6028 | output_api.PresubmitPromptWarning( |
| 6029 | "Accessibility code should not use element methods to get or check" |
| 6030 | "\nthe presence of aria attributes" |
| 6031 | "\nPlease use ARIA-specific attribute access, e.g. HasAriaAttribute()," |
| 6032 | "\nAriaTokenAttribute(), AriaBoolAttribute(), AriaBooleanAttribute()," |
| 6033 | "\nAriaFloatAttribute().", |
| 6034 | problems, |
| 6035 | ) |
| 6036 | ] |
| 6037 | return [] |
| 6038 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6039 | |
seanmccullough | 4a935625 | 2021-04-08 19:54:09 | [diff] [blame] | 6040 | # string pattern, sequence of strings to show when pattern matches, |
| 6041 | # error flag. True if match is a presubmit error, otherwise it's a warning. |
| 6042 | _NON_INCLUSIVE_TERMS = ( |
| 6043 | ( |
| 6044 | # Note that \b pattern in python re is pretty particular. In this |
| 6045 | # regexp, 'class WhiteList ...' will match, but 'class FooWhiteList |
| 6046 | # ...' will not. This may require some tweaking to catch these cases |
| 6047 | # without triggering a lot of false positives. Leaving it naive and |
| 6048 | # less matchy for now. |
Josip Sokcevic | 9d2806a0 | 2023-12-13 03:04:02 | [diff] [blame] | 6049 | r'/(?i)\b((black|white)list|master|slave)\b', # nocheck |
seanmccullough | 4a935625 | 2021-04-08 19:54:09 | [diff] [blame] | 6050 | ( |
| 6051 | 'Please don\'t use blacklist, whitelist, ' # nocheck |
| 6052 | 'or slave in your', # nocheck |
| 6053 | 'code and make every effort to use other terms. Using "// nocheck"', |
| 6054 | '"# nocheck" or "<!-- nocheck -->"', |
| 6055 | 'at the end of the offending line will bypass this PRESUBMIT error', |
| 6056 | 'but avoid using this whenever possible. Reach out to', |
| 6057 | '[email protected] if you have questions'), |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6058 | True), ) |
| 6059 | |
seanmccullough | 4a935625 | 2021-04-08 19:54:09 | [diff] [blame] | 6060 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6061 | def ChecksCommon(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6062 | """Checks common to both upload and commit.""" |
| 6063 | results = [] |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 6064 | results.extend( |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6065 | input_api.canned_checks.PanProjectChecks( |
| 6066 | input_api, output_api, excluded_paths=_EXCLUDED_PATHS)) |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 6067 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6068 | author = input_api.change.author_email |
| 6069 | if author and author not in _KNOWN_ROBOTS: |
| 6070 | results.extend( |
| 6071 | input_api.canned_checks.CheckAuthorizedAuthor( |
| 6072 | input_api, output_api)) |
[email protected] | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 6073 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6074 | results.extend( |
| 6075 | input_api.canned_checks.CheckChangeHasNoTabs( |
| 6076 | input_api, |
| 6077 | output_api, |
| 6078 | source_file_filter=lambda x: x.LocalPath().endswith('.grd'))) |
| 6079 | results.extend( |
| 6080 | input_api.RunTests( |
| 6081 | input_api.canned_checks.CheckVPythonSpec(input_api, output_api))) |
Edward Lesmes | ce51df5 | 2020-08-04 22:10:17 | [diff] [blame] | 6082 | |
Mohamed Amir Yosef | 0ca6efb | 2025-05-14 10:02:28 | [diff] [blame] | 6083 | dirmd = 'dirmd.bat' if input_api.is_windows else 'dirmd' |
| 6084 | dirmd_bin = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 6085 | 'third_party', 'depot_tools', dirmd) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6086 | results.extend( |
| 6087 | input_api.RunTests( |
| 6088 | input_api.canned_checks.CheckDirMetadataFormat( |
Mohamed Amir Yosef | 0ca6efb | 2025-05-14 10:02:28 | [diff] [blame] | 6089 | input_api, output_api, dirmd_bin))) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6090 | results.extend( |
| 6091 | input_api.canned_checks.CheckOwnersDirMetadataExclusive( |
| 6092 | input_api, output_api)) |
| 6093 | results.extend( |
| 6094 | input_api.canned_checks.CheckNoNewMetadataInOwners( |
| 6095 | input_api, output_api)) |
| 6096 | results.extend( |
| 6097 | input_api.canned_checks.CheckInclusiveLanguage( |
| 6098 | input_api, |
| 6099 | output_api, |
| 6100 | excluded_directories_relative_path=[ |
| 6101 | 'infra', 'inclusive_language_presubmit_exempt_dirs.txt' |
| 6102 | ], |
| 6103 | non_inclusive_terms=_NON_INCLUSIVE_TERMS)) |
Yiwei Zhang | 5341bf0 | 2025-03-20 16:34:13 | [diff] [blame] | 6104 | results.extend( |
| 6105 | input_api.canned_checks.CheckNewDEPSHooksHasRequiredReviewers( |
| 6106 | input_api, output_api)) |
Dirk Pranke | e3c9c62d | 2021-05-18 18:35:59 | [diff] [blame] | 6107 | |
Aleksey Khoroshilov | 2978c94 | 2022-06-13 16:14:12 | [diff] [blame] | 6108 | presubmit_py_filter = lambda f: input_api.FilterSourceFile( |
Daniel Cheng | 6f3d1ae1 | 2025-04-07 17:11:27 | [diff] [blame] | 6109 | f, files_to_check=[r'.*PRESUBMIT(?:_test)?\.py$']) |
| 6110 | potential_paths = set( |
| 6111 | map( |
| 6112 | lambda f: input_api.os_path.dirname(f.AbsoluteLocalPath()), |
| 6113 | input_api.AffectedFiles(include_deletes=False, |
| 6114 | file_filter=presubmit_py_filter))) |
| 6115 | for full_path in potential_paths: |
Aleksey Khoroshilov | 2978c94 | 2022-06-13 16:14:12 | [diff] [blame] | 6116 | test_file = input_api.os_path.join(full_path, 'PRESUBMIT_test.py') |
| 6117 | # The PRESUBMIT.py file (and the directory containing it) might have |
| 6118 | # been affected by being moved or removed, so only try to run the tests |
| 6119 | # if they still exist. |
| 6120 | if not input_api.os_path.exists(test_file): |
| 6121 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6122 | |
Aleksey Khoroshilov | 2978c94 | 2022-06-13 16:14:12 | [diff] [blame] | 6123 | results.extend( |
| 6124 | input_api.canned_checks.RunUnitTestsInDirectory( |
| 6125 | input_api, |
| 6126 | output_api, |
| 6127 | full_path, |
Takuto Ikuta | 40def48 | 2023-06-02 02:23:49 | [diff] [blame] | 6128 | files_to_check=[r'^PRESUBMIT_test\.py$'])) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6129 | return results |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 6130 | |
[email protected] | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 6131 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6132 | def CheckPatchFiles(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6133 | problems = [ |
| 6134 | f.LocalPath() for f in input_api.AffectedFiles() |
| 6135 | if f.LocalPath().endswith(('.orig', '.rej')) |
| 6136 | ] |
| 6137 | # Cargo.toml.orig files are part of third-party crates downloaded from |
| 6138 | # crates.io and should be included. |
| 6139 | problems = [f for f in problems if not f.endswith('Cargo.toml.orig')] |
| 6140 | if problems: |
| 6141 | return [ |
| 6142 | output_api.PresubmitError("Don't commit .rej and .orig files.", |
| 6143 | problems) |
| 6144 | ] |
| 6145 | else: |
| 6146 | return [] |
[email protected] | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 6147 | |
| 6148 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6149 | def CheckBuildConfigMacrosWithoutInclude(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6150 | # Excludes OS_CHROMEOS, which is not defined in build_config.h. |
| 6151 | macro_re = input_api.re.compile( |
| 6152 | r'^\s*#(el)?if.*\bdefined\(((COMPILER_|ARCH_CPU_|WCHAR_T_IS_)[^)]*)') |
| 6153 | include_re = input_api.re.compile(r'^#include\s+"build/build_config.h"', |
| 6154 | input_api.re.MULTILINE) |
| 6155 | extension_re = input_api.re.compile(r'\.[a-z]+$') |
| 6156 | errors = [] |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 6157 | config_h_file = input_api.os_path.join('build', 'build_config.h') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6158 | for f in input_api.AffectedFiles(include_deletes=False): |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 6159 | # The build-config macros are allowed to be used in build_config.h |
| 6160 | # without including itself. |
| 6161 | if f.LocalPath() == config_h_file: |
| 6162 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6163 | if not f.LocalPath().endswith( |
| 6164 | ('.h', '.c', '.cc', '.cpp', '.m', '.mm')): |
| 6165 | continue |
Arthur Sonzogni | a3dec41 | 2024-04-29 12:05:37 | [diff] [blame] | 6166 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6167 | found_line_number = None |
| 6168 | found_macro = None |
| 6169 | all_lines = input_api.ReadFile(f, 'r').splitlines() |
| 6170 | for line_num, line in enumerate(all_lines): |
| 6171 | match = macro_re.search(line) |
| 6172 | if match: |
| 6173 | found_line_number = line_num |
| 6174 | found_macro = match.group(2) |
| 6175 | break |
| 6176 | if not found_line_number: |
| 6177 | continue |
Kent Tamura | 5a8755d | 2017-06-29 23:37:07 | [diff] [blame] | 6178 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6179 | found_include_line = -1 |
| 6180 | for line_num, line in enumerate(all_lines): |
| 6181 | if include_re.search(line): |
| 6182 | found_include_line = line_num |
| 6183 | break |
| 6184 | if found_include_line >= 0 and found_include_line < found_line_number: |
| 6185 | continue |
Kent Tamura | 5a8755d | 2017-06-29 23:37:07 | [diff] [blame] | 6186 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6187 | if not f.LocalPath().endswith('.h'): |
| 6188 | primary_header_path = extension_re.sub('.h', f.AbsoluteLocalPath()) |
| 6189 | try: |
| 6190 | content = input_api.ReadFile(primary_header_path, 'r') |
| 6191 | if include_re.search(content): |
| 6192 | continue |
| 6193 | except IOError: |
| 6194 | pass |
| 6195 | errors.append('%s:%d %s macro is used without first including build/' |
| 6196 | 'build_config.h.' % |
| 6197 | (f.LocalPath(), found_line_number, found_macro)) |
| 6198 | if errors: |
| 6199 | return [output_api.PresubmitPromptWarning('\n'.join(errors))] |
| 6200 | return [] |
Kent Tamura | 5a8755d | 2017-06-29 23:37:07 | [diff] [blame] | 6201 | |
| 6202 | |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6203 | def CheckForSuperfluousStlIncludesInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6204 | stl_include_re = input_api.re.compile(r'^#include\s+<(' |
| 6205 | r'algorithm|' |
| 6206 | r'array|' |
| 6207 | r'limits|' |
| 6208 | r'list|' |
| 6209 | r'map|' |
| 6210 | r'memory|' |
| 6211 | r'queue|' |
| 6212 | r'set|' |
| 6213 | r'string|' |
| 6214 | r'unordered_map|' |
| 6215 | r'unordered_set|' |
| 6216 | r'utility|' |
| 6217 | r'vector)>') |
| 6218 | std_namespace_re = input_api.re.compile(r'std::') |
| 6219 | errors = [] |
| 6220 | for f in input_api.AffectedFiles(): |
| 6221 | if not _IsCPlusPlusHeaderFile(input_api, f.LocalPath()): |
| 6222 | continue |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6223 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6224 | uses_std_namespace = False |
| 6225 | has_stl_include = False |
| 6226 | for line in f.NewContents(): |
| 6227 | if has_stl_include and uses_std_namespace: |
| 6228 | break |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6229 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6230 | if not has_stl_include and stl_include_re.search(line): |
| 6231 | has_stl_include = True |
| 6232 | continue |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6233 | |
Bruce Dawson | 4a5579a | 2022-04-08 17:11:36 | [diff] [blame] | 6234 | if not uses_std_namespace and (std_namespace_re.search(line) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6235 | or 'no-std-usage-because-pch-file' |
| 6236 | in line): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6237 | uses_std_namespace = True |
| 6238 | continue |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6239 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6240 | if has_stl_include and not uses_std_namespace: |
| 6241 | errors.append( |
| 6242 | '%s: Includes STL header(s) but does not reference std::' % |
| 6243 | f.LocalPath()) |
| 6244 | if errors: |
| 6245 | return [output_api.PresubmitPromptWarning('\n'.join(errors))] |
| 6246 | return [] |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6247 | |
| 6248 | |
Xiaohan Wang | 42d96c2 | 2022-01-20 17:23:11 | [diff] [blame] | 6249 | def _CheckForDeprecatedOSMacrosInFile(input_api, f): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6250 | """Check for sensible looking, totally invalid OS macros.""" |
| 6251 | preprocessor_statement = input_api.re.compile(r'^\s*#') |
| 6252 | os_macro = input_api.re.compile(r'defined\(OS_([^)]+)\)') |
| 6253 | results = [] |
| 6254 | for lnum, line in f.ChangedContents(): |
| 6255 | if preprocessor_statement.search(line): |
| 6256 | for match in os_macro.finditer(line): |
| 6257 | results.append( |
| 6258 | ' %s:%d: %s' % |
| 6259 | (f.LocalPath(), lnum, 'defined(OS_' + match.group(1) + |
| 6260 | ') -> BUILDFLAG(IS_' + match.group(1) + ')')) |
| 6261 | return results |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6262 | |
| 6263 | |
Xiaohan Wang | 42d96c2 | 2022-01-20 17:23:11 | [diff] [blame] | 6264 | def CheckForDeprecatedOSMacros(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6265 | """Check all affected files for invalid OS macros.""" |
| 6266 | bad_macros = [] |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 6267 | # The OS_ macros are allowed to be used in build/build_config.h. |
| 6268 | config_h_file = input_api.os_path.join('build', 'build_config.h') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6269 | for f in input_api.AffectedSourceFiles(None): |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 6270 | if not f.LocalPath().endswith(('.py', '.js', '.html', '.css', '.md')) \ |
| 6271 | and f.LocalPath() != config_h_file: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6272 | bad_macros.extend(_CheckForDeprecatedOSMacrosInFile(input_api, f)) |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6273 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6274 | if not bad_macros: |
| 6275 | return [] |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6276 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6277 | return [ |
| 6278 | output_api.PresubmitError( |
| 6279 | 'OS macros have been deprecated. Please use BUILDFLAGs instead (still ' |
| 6280 | 'defined in build_config.h):', bad_macros) |
| 6281 | ] |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6282 | |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6283 | |
| 6284 | def _CheckForInvalidIfDefinedMacrosInFile(input_api, f): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6285 | """Check all affected files for invalid "if defined" macros.""" |
| 6286 | ALWAYS_DEFINED_MACROS = ( |
| 6287 | "TARGET_CPU_PPC", |
| 6288 | "TARGET_CPU_PPC64", |
| 6289 | "TARGET_CPU_68K", |
| 6290 | "TARGET_CPU_X86", |
| 6291 | "TARGET_CPU_ARM", |
| 6292 | "TARGET_CPU_MIPS", |
| 6293 | "TARGET_CPU_SPARC", |
| 6294 | "TARGET_CPU_ALPHA", |
| 6295 | "TARGET_IPHONE_SIMULATOR", |
| 6296 | "TARGET_OS_EMBEDDED", |
| 6297 | "TARGET_OS_IPHONE", |
| 6298 | "TARGET_OS_MAC", |
| 6299 | "TARGET_OS_UNIX", |
| 6300 | "TARGET_OS_WIN32", |
| 6301 | ) |
| 6302 | ifdef_macro = input_api.re.compile( |
| 6303 | r'^\s*#.*(?:ifdef\s|defined\()([^\s\)]+)') |
| 6304 | results = [] |
| 6305 | for lnum, line in f.ChangedContents(): |
| 6306 | for match in ifdef_macro.finditer(line): |
| 6307 | if match.group(1) in ALWAYS_DEFINED_MACROS: |
| 6308 | always_defined = ' %s is always defined. ' % match.group(1) |
| 6309 | did_you_mean = 'Did you mean \'#if %s\'?' % match.group(1) |
| 6310 | results.append( |
| 6311 | ' %s:%d %s\n\t%s' % |
| 6312 | (f.LocalPath(), lnum, always_defined, did_you_mean)) |
| 6313 | return results |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6314 | |
| 6315 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6316 | def CheckForInvalidIfDefinedMacros(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6317 | """Check all affected files for invalid "if defined" macros.""" |
Arthur Sonzogni | 4fd14fd | 2024-06-02 18:42:52 | [diff] [blame] | 6318 | SKIPPED_PATHS = [ |
| 6319 | 'base/allocator/partition_allocator/src/partition_alloc/build_config.h', |
| 6320 | 'build/build_config.h', |
| 6321 | 'third_party/abseil-cpp/', |
| 6322 | 'third_party/sqlite/', |
| 6323 | ] |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6324 | |
Arthur Sonzogni | 4fd14fd | 2024-06-02 18:42:52 | [diff] [blame] | 6325 | def affected_files_filter(f): |
| 6326 | # Normalize the local path to Linux-style path separators so that the |
| 6327 | # path comparisons work on Windows as well. |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 6328 | path = f.UnixLocalPath() |
Arthur Sonzogni | 4fd14fd | 2024-06-02 18:42:52 | [diff] [blame] | 6329 | |
| 6330 | for skipped_path in SKIPPED_PATHS: |
| 6331 | if path.startswith(skipped_path): |
| 6332 | return False |
| 6333 | |
| 6334 | return path.endswith(('.h', '.c', '.cc', '.m', '.mm')) |
| 6335 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6336 | bad_macros = [] |
Arthur Sonzogni | 4fd14fd | 2024-06-02 18:42:52 | [diff] [blame] | 6337 | for f in input_api.AffectedSourceFiles(affected_files_filter): |
| 6338 | bad_macros.extend(_CheckForInvalidIfDefinedMacrosInFile(input_api, f)) |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6339 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6340 | if not bad_macros: |
| 6341 | return [] |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6342 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6343 | return [ |
| 6344 | output_api.PresubmitError( |
| 6345 | 'Found ifdef check on always-defined macro[s]. Please fix your code\n' |
| 6346 | 'or check the list of ALWAYS_DEFINED_MACROS in src/PRESUBMIT.py.', |
| 6347 | bad_macros) |
| 6348 | ] |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6349 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6350 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6351 | def CheckForIPCRules(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6352 | """Check for same IPC rules described in |
| 6353 | http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc |
| 6354 | """ |
| 6355 | base_pattern = r'IPC_ENUM_TRAITS\(' |
| 6356 | inclusion_pattern = input_api.re.compile(r'(%s)' % base_pattern) |
| 6357 | comment_pattern = input_api.re.compile(r'//.*(%s)' % base_pattern) |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 6358 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6359 | problems = [] |
| 6360 | for f in input_api.AffectedSourceFiles(None): |
| 6361 | local_path = f.LocalPath() |
| 6362 | if not local_path.endswith('.h'): |
| 6363 | continue |
| 6364 | for line_number, line in f.ChangedContents(): |
| 6365 | if inclusion_pattern.search( |
| 6366 | line) and not comment_pattern.search(line): |
| 6367 | problems.append('%s:%d\n %s' % |
| 6368 | (local_path, line_number, line.strip())) |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 6369 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6370 | if problems: |
| 6371 | return [ |
| 6372 | output_api.PresubmitPromptWarning(_IPC_ENUM_TRAITS_DEPRECATED, |
| 6373 | problems) |
| 6374 | ] |
| 6375 | else: |
| 6376 | return [] |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 6377 | |
[email protected] | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6378 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6379 | def CheckForLongPathnames(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6380 | """Check to make sure no files being submitted have long paths. |
| 6381 | This causes issues on Windows. |
| 6382 | """ |
| 6383 | problems = [] |
| 6384 | for f in input_api.AffectedTestableFiles(): |
| 6385 | local_path = f.LocalPath() |
| 6386 | # Windows has a path limit of 260 characters. Limit path length to 200 so |
| 6387 | # that we have some extra for the prefix on dev machines and the bots. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6388 | if (local_path.startswith('third_party/blink/web_tests/platform/') |
| 6389 | and not local_path.startswith( |
| 6390 | 'third_party/blink/web_tests/platform/win')): |
Weizhong Xia | 8b461f1 | 2024-06-21 21:46:33 | [diff] [blame] | 6391 | # Do not check length of the path for files not used by Windows |
| 6392 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6393 | if len(local_path) > 200: |
| 6394 | problems.append(local_path) |
Stephen Martinis | 97a39414 | 2018-06-07 23:06:05 | [diff] [blame] | 6395 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6396 | if problems: |
| 6397 | return [output_api.PresubmitError(_LONG_PATH_ERROR, problems)] |
| 6398 | else: |
| 6399 | return [] |
Stephen Martinis | 97a39414 | 2018-06-07 23:06:05 | [diff] [blame] | 6400 | |
| 6401 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6402 | def CheckForIncludeGuards(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6403 | """Check that header files have proper guards against multiple inclusion. |
| 6404 | 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] | 6405 | should include the string "no-include-guard-because-multiply-included" or |
| 6406 | "no-include-guard-because-pch-file". |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6407 | """ |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6408 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6409 | def is_chromium_header_file(f): |
| 6410 | # We only check header files under the control of the Chromium |
mikt | 84d6c71 | 2024-03-27 13:29:03 | [diff] [blame] | 6411 | # project. This excludes: |
| 6412 | # - third_party/*, except blink. |
| 6413 | # - base/allocator/partition_allocator/: PartitionAlloc is a standalone |
| 6414 | # library used outside of Chrome. Includes are referenced from its |
| 6415 | # own base directory. It has its own `CheckForIncludeGuards` |
| 6416 | # PRESUBMIT.py check. |
| 6417 | # - *_message_generator.h: They use include guards in a special, |
| 6418 | # non-typical way. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6419 | file_with_path = input_api.os_path.normpath(f.LocalPath()) |
| 6420 | return (file_with_path.endswith('.h') |
| 6421 | and not file_with_path.endswith('_message_generator.h') |
Bruce Dawson | 4c4c292 | 2022-05-02 18:07:33 | [diff] [blame] | 6422 | and not file_with_path.endswith('com_imported_mstscax.h') |
Peter Kasting | 66c1f75 | 2024-12-02 15:28:37 | [diff] [blame] | 6423 | and not file_with_path.startswith( |
| 6424 | input_api.os_path.join('base', 'allocator', |
| 6425 | 'partition_allocator')) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6426 | and (not file_with_path.startswith('third_party') |
| 6427 | or file_with_path.startswith( |
| 6428 | input_api.os_path.join('third_party', 'blink')))) |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6429 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6430 | def replace_special_with_underscore(string): |
| 6431 | return input_api.re.sub(r'[+\\/.-]', '_', string) |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6432 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6433 | errors = [] |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6434 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6435 | for f in input_api.AffectedSourceFiles(is_chromium_header_file): |
| 6436 | guard_name = None |
| 6437 | guard_line_number = None |
| 6438 | seen_guard_end = False |
Lei Zhang | d84f951 | 2024-05-28 19:43:30 | [diff] [blame] | 6439 | bypass_checks_at_end_of_file = False |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6440 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6441 | file_with_path = input_api.os_path.normpath(f.LocalPath()) |
| 6442 | base_file_name = input_api.os_path.splitext( |
| 6443 | input_api.os_path.basename(file_with_path))[0] |
| 6444 | upper_base_file_name = base_file_name.upper() |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6445 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6446 | expected_guard = replace_special_with_underscore( |
| 6447 | file_with_path.upper() + '_') |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6448 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6449 | # For "path/elem/file_name.h" we should really only accept |
| 6450 | # PATH_ELEM_FILE_NAME_H_ per coding style. Unfortunately there |
| 6451 | # are too many (1000+) files with slight deviations from the |
| 6452 | # coding style. The most important part is that the include guard |
| 6453 | # is there, and that it's unique, not the name so this check is |
| 6454 | # forgiving for existing files. |
| 6455 | # |
| 6456 | # As code becomes more uniform, this could be made stricter. |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6457 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6458 | guard_name_pattern_list = [ |
| 6459 | # Anything with the right suffix (maybe with an extra _). |
| 6460 | r'\w+_H__?', |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6461 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6462 | # To cover include guards with old Blink style. |
| 6463 | r'\w+_h', |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6464 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6465 | # Anything including the uppercase name of the file. |
| 6466 | r'\w*' + input_api.re.escape( |
| 6467 | replace_special_with_underscore(upper_base_file_name)) + |
| 6468 | r'\w*', |
| 6469 | ] |
| 6470 | guard_name_pattern = '|'.join(guard_name_pattern_list) |
| 6471 | guard_pattern = input_api.re.compile(r'#ifndef\s+(' + |
| 6472 | guard_name_pattern + ')') |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6473 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6474 | for line_number, line in enumerate(f.NewContents()): |
Bruce Dawson | 4a5579a | 2022-04-08 17:11:36 | [diff] [blame] | 6475 | if ('no-include-guard-because-multiply-included' in line |
| 6476 | or 'no-include-guard-because-pch-file' in line): |
Lei Zhang | d84f951 | 2024-05-28 19:43:30 | [diff] [blame] | 6477 | bypass_checks_at_end_of_file = True |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6478 | break |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6479 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6480 | if guard_name is None: |
| 6481 | match = guard_pattern.match(line) |
| 6482 | if match: |
| 6483 | guard_name = match.group(1) |
| 6484 | guard_line_number = line_number |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6485 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6486 | # We allow existing files to use include guards whose names |
| 6487 | # don't match the chromium style guide, but new files should |
| 6488 | # get it right. |
Bruce Dawson | 6cc154e | 2022-04-12 20:39:49 | [diff] [blame] | 6489 | if guard_name != expected_guard: |
Bruce Dawson | 95eb756 | 2022-09-14 15:27:16 | [diff] [blame] | 6490 | if f.Action() == 'A': # If file was just 'A'dded |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6491 | errors.append( |
| 6492 | output_api.PresubmitPromptWarning( |
| 6493 | 'Header using the wrong include guard name %s' |
| 6494 | % guard_name, [ |
| 6495 | '%s:%d' % |
| 6496 | (f.LocalPath(), line_number + 1) |
| 6497 | ], 'Expected: %r\nFound: %r' % |
| 6498 | (expected_guard, guard_name))) |
| 6499 | else: |
| 6500 | # The line after #ifndef should have a #define of the same name. |
| 6501 | if line_number == guard_line_number + 1: |
| 6502 | expected_line = '#define %s' % guard_name |
| 6503 | if line != expected_line: |
| 6504 | errors.append( |
| 6505 | output_api.PresubmitPromptWarning( |
| 6506 | 'Missing "%s" for include guard' % |
| 6507 | expected_line, |
| 6508 | ['%s:%d' % (f.LocalPath(), line_number + 1)], |
| 6509 | 'Expected: %r\nGot: %r' % |
| 6510 | (expected_line, line))) |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6511 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6512 | if not seen_guard_end and line == '#endif // %s' % guard_name: |
| 6513 | seen_guard_end = True |
| 6514 | elif seen_guard_end: |
| 6515 | if line.strip() != '': |
| 6516 | errors.append( |
| 6517 | output_api.PresubmitPromptWarning( |
| 6518 | 'Include guard %s not covering the whole file' |
| 6519 | % (guard_name), [f.LocalPath()])) |
| 6520 | break # Nothing else to check and enough to warn once. |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6521 | |
Lei Zhang | d84f951 | 2024-05-28 19:43:30 | [diff] [blame] | 6522 | if bypass_checks_at_end_of_file: |
| 6523 | continue |
| 6524 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6525 | if guard_name is None: |
| 6526 | errors.append( |
| 6527 | output_api.PresubmitPromptWarning( |
Bruce Dawson | 32114b6 | 2022-04-11 16:45:49 | [diff] [blame] | 6528 | 'Missing include guard in %s\n' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6529 | 'Recommended name: %s\n' |
| 6530 | 'This check can be disabled by having the string\n' |
Bruce Dawson | 4a5579a | 2022-04-08 17:11:36 | [diff] [blame] | 6531 | '"no-include-guard-because-multiply-included" or\n' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6532 | '"no-include-guard-because-pch-file" in the header.' % |
| 6533 | (f.LocalPath(), expected_guard))) |
Lei Zhang | d84f951 | 2024-05-28 19:43:30 | [diff] [blame] | 6534 | elif not seen_guard_end: |
| 6535 | errors.append( |
| 6536 | output_api.PresubmitPromptWarning( |
| 6537 | 'Incorrect or missing include guard #endif in %s\n' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6538 | 'Recommended #endif comment: // %s' % |
| 6539 | (f.LocalPath(), expected_guard))) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6540 | |
| 6541 | return errors |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6542 | |
| 6543 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6544 | def CheckForWindowsLineEndings(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6545 | """Check source code and known ascii text files for Windows style line |
| 6546 | endings. |
| 6547 | """ |
Bruce Dawson | 5efbdc65 | 2022-04-11 19:29:51 | [diff] [blame] | 6548 | known_text_files = r'.*\.(txt|html|htm|py|gyp|gypi|gn|isolate|icon)$' |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6549 | |
dpapad | fd421fb | 2025-02-13 00:47:32 | [diff] [blame] | 6550 | _WEBUI_FILES_EXTENSIONS = r'\.(css|html|js|ts|svg)$' |
| 6551 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6552 | file_inclusion_pattern = (known_text_files, |
| 6553 | r'.+%s' % _IMPLEMENTATION_EXTENSIONS, |
dpapad | fd421fb | 2025-02-13 00:47:32 | [diff] [blame] | 6554 | r'.+%s' % _HEADER_EXTENSIONS, |
| 6555 | r'.+%s' % _WEBUI_FILES_EXTENSIONS) |
| 6556 | |
| 6557 | # Exclude folder that contains .ts files that are actually binary video |
| 6558 | # format and not TypeScript. |
| 6559 | file_exclusion_pattern = (r'media/test/data/') |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6560 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6561 | problems = [] |
| 6562 | source_file_filter = lambda f: input_api.FilterSourceFile( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6563 | f, |
| 6564 | files_to_check=file_inclusion_pattern, |
dpapad | fd421fb | 2025-02-13 00:47:32 | [diff] [blame] | 6565 | files_to_skip=file_exclusion_pattern) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6566 | for f in input_api.AffectedSourceFiles(source_file_filter): |
Bruce Dawson | 5efbdc65 | 2022-04-11 19:29:51 | [diff] [blame] | 6567 | # Ignore test files that contain crlf intentionally. |
| 6568 | if f.LocalPath().endswith('crlf.txt'): |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 6569 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6570 | include_file = False |
| 6571 | for line in input_api.ReadFile(f, 'r').splitlines(True): |
| 6572 | if line.endswith('\r\n'): |
| 6573 | include_file = True |
| 6574 | if include_file: |
| 6575 | problems.append(f.LocalPath()) |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6576 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6577 | if problems: |
| 6578 | return [ |
| 6579 | output_api.PresubmitPromptWarning( |
| 6580 | 'Are you sure that you want ' |
| 6581 | 'these files to contain Windows style line endings?\n' + |
| 6582 | '\n'.join(problems)) |
| 6583 | ] |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6584 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6585 | return [] |
| 6586 | |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6587 | |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 6588 | def CheckIconFilesForLicenseHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6589 | """Check that .icon files (which are fragments of C++) have license headers. |
| 6590 | """ |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 6591 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6592 | icon_files = (r'.*\.icon$', ) |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 6593 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6594 | icons = lambda x: input_api.FilterSourceFile(x, files_to_check=icon_files) |
| 6595 | return input_api.canned_checks.CheckLicense(input_api, |
| 6596 | output_api, |
| 6597 | source_file_filter=icons) |
| 6598 | |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 6599 | |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6600 | def CheckForUseOfChromeAppsDeprecations(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6601 | """Check source code for use of Chrome App technologies being |
| 6602 | deprecated. |
| 6603 | """ |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6604 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6605 | def _CheckForDeprecatedTech(input_api, |
| 6606 | output_api, |
| 6607 | detection_list, |
| 6608 | files_to_check=None, |
| 6609 | files_to_skip=None): |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6610 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6611 | if (files_to_check or files_to_skip): |
| 6612 | source_file_filter = lambda f: input_api.FilterSourceFile( |
| 6613 | f, files_to_check=files_to_check, files_to_skip=files_to_skip) |
| 6614 | else: |
| 6615 | source_file_filter = None |
| 6616 | |
| 6617 | problems = [] |
| 6618 | |
| 6619 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 6620 | if f.Action() == 'D': |
| 6621 | continue |
| 6622 | for _, line in f.ChangedContents(): |
| 6623 | if any(detect in line for detect in detection_list): |
| 6624 | problems.append(f.LocalPath()) |
| 6625 | |
| 6626 | return problems |
| 6627 | |
| 6628 | # to avoid this presubmit script triggering warnings |
| 6629 | files_to_skip = ['PRESUBMIT.py', 'PRESUBMIT_test.py'] |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6630 | |
| 6631 | problems = [] |
| 6632 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6633 | # NMF: any files with extensions .nmf or NMF |
| 6634 | _NMF_FILES = r'\.(nmf|NMF)$' |
| 6635 | problems += _CheckForDeprecatedTech( |
| 6636 | input_api, |
| 6637 | output_api, |
| 6638 | detection_list=[''], # any change to the file will trigger warning |
| 6639 | files_to_check=[r'.+%s' % _NMF_FILES]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6640 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6641 | # MANIFEST: any manifest.json that in its diff includes "app": |
| 6642 | _MANIFEST_FILES = r'(manifest\.json)$' |
| 6643 | problems += _CheckForDeprecatedTech( |
| 6644 | input_api, |
| 6645 | output_api, |
| 6646 | detection_list=['"app":'], |
| 6647 | files_to_check=[r'.*%s' % _MANIFEST_FILES]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6648 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6649 | # NaCl / PNaCl: any file that in its diff contains the strings in the list |
| 6650 | problems += _CheckForDeprecatedTech( |
| 6651 | input_api, |
| 6652 | output_api, |
| 6653 | detection_list=['config=nacl', 'enable-nacl', 'cpu=pnacl', 'nacl_io'], |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 6654 | files_to_skip=files_to_skip + [r"^native_client_sdk/"]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6655 | |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 6656 | # PPAPI: any C/C++ file that in its diff includes a ppapi library |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6657 | problems += _CheckForDeprecatedTech( |
| 6658 | input_api, |
| 6659 | output_api, |
| 6660 | detection_list=['#include "ppapi', '#include <ppapi'], |
| 6661 | files_to_check=(r'.+%s' % _HEADER_EXTENSIONS, |
| 6662 | r'.+%s' % _IMPLEMENTATION_EXTENSIONS), |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 6663 | files_to_skip=[r"^ppapi/"]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6664 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6665 | if problems: |
| 6666 | return [ |
| 6667 | output_api.PresubmitPromptWarning( |
| 6668 | 'You are adding/modifying code' |
| 6669 | 'related to technologies which will soon be deprecated (Chrome Apps, NaCl,' |
| 6670 | ' PNaCl, PPAPI). See this blog post for more details:\n' |
| 6671 | 'https://blog.chromium.org/2020/08/changes-to-chrome-app-support-timeline.html\n' |
| 6672 | 'and this documentation for options to replace these technologies:\n' |
| 6673 | 'https://developer.chrome.com/docs/apps/migration/\n' + |
| 6674 | '\n'.join(problems)) |
| 6675 | ] |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6676 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6677 | return [] |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6678 | |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6679 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6680 | def CheckSyslogUseWarningOnUpload(input_api, output_api, src_file_filter=None): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6681 | """Checks that all source files use SYSLOG properly.""" |
| 6682 | syslog_files = [] |
| 6683 | for f in input_api.AffectedSourceFiles(src_file_filter): |
| 6684 | for line_number, line in f.ChangedContents(): |
| 6685 | if 'SYSLOG' in line: |
| 6686 | syslog_files.append(f.LocalPath() + ':' + str(line_number)) |
pastarmovj | 032ba5bc | 2017-01-12 10:41:56 | [diff] [blame] | 6687 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6688 | if syslog_files: |
| 6689 | return [ |
| 6690 | output_api.PresubmitPromptWarning( |
| 6691 | 'Please make sure there are no privacy sensitive bits of data in SYSLOG' |
| 6692 | ' calls.\nFiles to check:\n', |
| 6693 | items=syslog_files) |
| 6694 | ] |
| 6695 | return [] |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 6696 | |
| 6697 | |
[email protected] | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 6698 | def CheckChangeOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6699 | if input_api.version < [2, 0, 0]: |
| 6700 | return [ |
| 6701 | output_api.PresubmitError( |
| 6702 | "Your depot_tools is out of date. " |
| 6703 | "This PRESUBMIT.py requires at least presubmit_support version 2.0.0, " |
| 6704 | "but your version is %d.%d.%d" % tuple(input_api.version)) |
| 6705 | ] |
| 6706 | results = [] |
| 6707 | results.extend( |
| 6708 | input_api.canned_checks.CheckPatchFormatted(input_api, output_api)) |
| 6709 | return results |
[email protected] | ca8d198 | 2009-02-19 16:33:12 | [diff] [blame] | 6710 | |
| 6711 | |
| 6712 | def CheckChangeOnCommit(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6713 | if input_api.version < [2, 0, 0]: |
| 6714 | return [ |
| 6715 | output_api.PresubmitError( |
| 6716 | "Your depot_tools is out of date. " |
| 6717 | "This PRESUBMIT.py requires at least presubmit_support version 2.0.0, " |
| 6718 | "but your version is %d.%d.%d" % tuple(input_api.version)) |
| 6719 | ] |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6720 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6721 | results = [] |
| 6722 | # Make sure the tree is 'open'. |
| 6723 | results.extend( |
| 6724 | input_api.canned_checks.CheckTreeIsOpen( |
| 6725 | input_api, |
| 6726 | output_api, |
| 6727 | json_url='http://chromium-status.appspot.com/current?format=json')) |
[email protected] | 806e98e | 2010-03-19 17:49:27 | [diff] [blame] | 6728 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6729 | results.extend( |
| 6730 | input_api.canned_checks.CheckPatchFormatted(input_api, output_api)) |
| 6731 | results.extend( |
| 6732 | input_api.canned_checks.CheckChangeHasBugField(input_api, output_api)) |
| 6733 | results.extend( |
| 6734 | input_api.canned_checks.CheckChangeHasNoUnwantedTags( |
| 6735 | input_api, output_api)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6736 | return results |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6737 | |
| 6738 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6739 | def CheckStrings(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6740 | """Check string ICU syntax validity and if translation screenshots exist.""" |
| 6741 | # Skip translation screenshots check if a SkipTranslationScreenshotsCheck |
| 6742 | # footer is set to true. |
| 6743 | git_footers = input_api.change.GitFootersFromDescription() |
| 6744 | skip_screenshot_check_footer = [ |
| 6745 | footer.lower() for footer in git_footers.get( |
| 6746 | u'Skip-Translation-Screenshots-Check', []) |
| 6747 | ] |
| 6748 | run_screenshot_check = u'true' not in skip_screenshot_check_footer |
Edward Lesmes | f7c5c6d | 2020-05-14 23:30:02 | [diff] [blame] | 6749 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6750 | import os |
| 6751 | import re |
| 6752 | import sys |
| 6753 | from io import StringIO |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6754 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6755 | new_or_added_paths = set(f.LocalPath() for f in input_api.AffectedFiles() |
| 6756 | if (f.Action() == 'A' or f.Action() == 'M')) |
| 6757 | removed_paths = set(f.LocalPath() |
| 6758 | for f in input_api.AffectedFiles(include_deletes=True) |
| 6759 | if f.Action() == 'D') |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6760 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6761 | affected_grds = [ |
| 6762 | f for f in input_api.AffectedFiles() |
| 6763 | if f.LocalPath().endswith(('.grd', '.grdp')) |
| 6764 | ] |
| 6765 | affected_grds = [ |
| 6766 | f for f in affected_grds if not 'testdata' in f.LocalPath() |
| 6767 | ] |
| 6768 | if not affected_grds: |
| 6769 | return [] |
meacer | 8c0d383 | 2019-12-26 21:46:16 | [diff] [blame] | 6770 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6771 | affected_png_paths = [ |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 6772 | f.LocalPath() for f in input_api.AffectedFiles() |
| 6773 | if f.LocalPath().endswith('.png') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6774 | ] |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6775 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6776 | # Check for screenshots. Developers can upload screenshots using |
| 6777 | # tools/translation/upload_screenshots.py which finds and uploads |
| 6778 | # images associated with .grd files (e.g. test_grd/IDS_STRING.png for the |
| 6779 | # message named IDS_STRING in test.grd) and produces a .sha1 file (e.g. |
| 6780 | # test_grd/IDS_STRING.png.sha1) for each png when the upload is successful. |
| 6781 | # |
| 6782 | # The logic here is as follows: |
| 6783 | # |
| 6784 | # - If the CL has a .png file under the screenshots directory for a grd |
| 6785 | # file, warn the developer. Actual images should never be checked into the |
| 6786 | # Chrome repo. |
| 6787 | # |
| 6788 | # - If the CL contains modified or new messages in grd files and doesn't |
| 6789 | # contain the corresponding .sha1 files, warn the developer to add images |
| 6790 | # and upload them via tools/translation/upload_screenshots.py. |
| 6791 | # |
| 6792 | # - If the CL contains modified or new messages in grd files and the |
| 6793 | # corresponding .sha1 files, everything looks good. |
| 6794 | # |
| 6795 | # - If the CL contains removed messages in grd files but the corresponding |
| 6796 | # .sha1 files aren't removed, warn the developer to remove them. |
| 6797 | unnecessary_screenshots = [] |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6798 | invalid_sha1 = [] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6799 | missing_sha1 = [] |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6800 | missing_sha1_modified = [] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6801 | unnecessary_sha1_files = [] |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6802 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6803 | # This checks verifies that the ICU syntax of messages this CL touched is |
| 6804 | # valid, and reports any found syntax errors. |
| 6805 | # Without this presubmit check, ICU syntax errors in Chromium strings can land |
| 6806 | # without developers being aware of them. Later on, such ICU syntax errors |
| 6807 | # break message extraction for translation, hence would block Chromium |
| 6808 | # translations until they are fixed. |
| 6809 | icu_syntax_errors = [] |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6810 | sha1_pattern = input_api.re.compile(r'^[a-fA-F0-9]{40}$', |
| 6811 | input_api.re.MULTILINE) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6812 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6813 | def _CheckScreenshotAdded(screenshots_dir, message_id): |
| 6814 | sha1_path = input_api.os_path.join(screenshots_dir, |
| 6815 | message_id + '.png.sha1') |
| 6816 | if sha1_path not in new_or_added_paths: |
| 6817 | missing_sha1.append(sha1_path) |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6818 | elif not _CheckValidSha1(sha1_path): |
Sam Maier | b926c58c | 2023-08-08 19:58:25 | [diff] [blame] | 6819 | invalid_sha1.append(sha1_path) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6820 | |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6821 | def _CheckScreenshotModified(screenshots_dir, message_id): |
| 6822 | sha1_path = input_api.os_path.join(screenshots_dir, |
| 6823 | message_id + '.png.sha1') |
| 6824 | if sha1_path not in new_or_added_paths: |
| 6825 | missing_sha1_modified.append(sha1_path) |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6826 | elif not _CheckValidSha1(sha1_path): |
Sam Maier | b926c58c | 2023-08-08 19:58:25 | [diff] [blame] | 6827 | invalid_sha1.append(sha1_path) |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6828 | |
| 6829 | def _CheckValidSha1(sha1_path): |
Sam Maier | b926c58c | 2023-08-08 19:58:25 | [diff] [blame] | 6830 | return sha1_pattern.search( |
| 6831 | next("\n".join(f.NewContents()) for f in input_api.AffectedFiles() |
| 6832 | if f.LocalPath() == sha1_path)) |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6833 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6834 | def _CheckScreenshotRemoved(screenshots_dir, message_id): |
| 6835 | sha1_path = input_api.os_path.join(screenshots_dir, |
| 6836 | message_id + '.png.sha1') |
| 6837 | if input_api.os_path.exists( |
| 6838 | sha1_path) and sha1_path not in removed_paths: |
| 6839 | unnecessary_sha1_files.append(sha1_path) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6840 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6841 | def _ValidateIcuSyntax(text, level, signatures): |
| 6842 | """Validates ICU syntax of a text string. |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6843 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6844 | Check if text looks similar to ICU and checks for ICU syntax correctness |
| 6845 | in this case. Reports various issues with ICU syntax and values of |
| 6846 | variants. Supports checking of nested messages. Accumulate information of |
| 6847 | each ICU messages found in the text for further checking. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6848 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6849 | Args: |
| 6850 | text: a string to check. |
| 6851 | level: a number of current nesting level. |
| 6852 | signatures: an accumulator, a list of tuple of (level, variable, |
| 6853 | kind, variants). |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6854 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6855 | Returns: |
| 6856 | None if a string is not ICU or no issue detected. |
| 6857 | A tuple of (message, start index, end index) if an issue detected. |
| 6858 | """ |
| 6859 | valid_types = { |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6860 | 'plural': (frozenset([ |
| 6861 | '=0', '=1', '=2', '=3', 'zero', 'one', 'two', 'few', 'many', |
| 6862 | 'other' |
| 6863 | ]), frozenset(['=1', 'other'])), |
| 6864 | 'selectordinal': (frozenset([ |
| 6865 | '=0', '=1', '=2', '=3', 'zero', 'one', 'two', 'few', 'many', |
| 6866 | 'other' |
| 6867 | ]), frozenset(['one', 'other'])), |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6868 | 'select': (frozenset(), frozenset(['other'])), |
| 6869 | } |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6870 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6871 | # Check if the message looks like an attempt to use ICU |
| 6872 | # plural. If yes - check if its syntax strictly matches ICU format. |
| 6873 | like = re.match(r'^[^{]*\{[^{]*\b(plural|selectordinal|select)\b', |
| 6874 | text) |
| 6875 | if not like: |
| 6876 | signatures.append((level, None, None, None)) |
| 6877 | return |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6878 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6879 | # Check for valid prefix and suffix |
| 6880 | m = re.match( |
| 6881 | r'^([^{]*\{)([a-zA-Z0-9_]+),\s*' |
| 6882 | r'(plural|selectordinal|select),\s*' |
| 6883 | r'(?:offset:\d+)?\s*(.*)', text, re.DOTALL) |
| 6884 | if not m: |
| 6885 | return (('This message looks like an ICU plural, ' |
| 6886 | 'but does not follow ICU syntax.'), like.start(), |
| 6887 | like.end()) |
| 6888 | starting, variable, kind, variant_pairs = m.groups() |
| 6889 | variants, depth, last_pos = _ParseIcuVariants(variant_pairs, |
| 6890 | m.start(4)) |
| 6891 | if depth: |
| 6892 | return ('Invalid ICU format. Unbalanced opening bracket', last_pos, |
| 6893 | len(text)) |
| 6894 | first = text[0] |
| 6895 | ending = text[last_pos:] |
| 6896 | if not starting: |
| 6897 | return ('Invalid ICU format. No initial opening bracket', |
| 6898 | last_pos - 1, last_pos) |
| 6899 | if not ending or '}' not in ending: |
| 6900 | return ('Invalid ICU format. No final closing bracket', |
| 6901 | last_pos - 1, last_pos) |
| 6902 | elif first != '{': |
| 6903 | return (( |
| 6904 | 'Invalid ICU format. Extra characters at the start of a complex ' |
| 6905 | 'message (go/icu-message-migration): "%s"') % starting, 0, |
| 6906 | len(starting)) |
| 6907 | elif ending != '}': |
| 6908 | return (( |
| 6909 | 'Invalid ICU format. Extra characters at the end of a complex ' |
| 6910 | 'message (go/icu-message-migration): "%s"') % ending, |
| 6911 | last_pos - 1, len(text) - 1) |
| 6912 | if kind not in valid_types: |
| 6913 | return (('Unknown ICU message type %s. ' |
| 6914 | 'Valid types are: plural, select, selectordinal') % kind, |
| 6915 | 0, 0) |
| 6916 | known, required = valid_types[kind] |
| 6917 | defined_variants = set() |
| 6918 | for variant, variant_range, value, value_range in variants: |
| 6919 | start, end = variant_range |
| 6920 | if variant in defined_variants: |
| 6921 | return ('Variant "%s" is defined more than once' % variant, |
| 6922 | start, end) |
| 6923 | elif known and variant not in known: |
| 6924 | return ('Variant "%s" is not valid for %s message' % |
| 6925 | (variant, kind), start, end) |
| 6926 | defined_variants.add(variant) |
| 6927 | # Check for nested structure |
| 6928 | res = _ValidateIcuSyntax(value[1:-1], level + 1, signatures) |
| 6929 | if res: |
| 6930 | return (res[0], res[1] + value_range[0] + 1, |
| 6931 | res[2] + value_range[0] + 1) |
| 6932 | missing = required - defined_variants |
| 6933 | if missing: |
| 6934 | return ('Required variants missing: %s' % ', '.join(missing), 0, |
| 6935 | len(text)) |
| 6936 | signatures.append((level, variable, kind, defined_variants)) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6937 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6938 | def _ParseIcuVariants(text, offset=0): |
| 6939 | """Parse variants part of ICU complex message. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6940 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6941 | Builds a tuple of variant names and values, as well as |
| 6942 | their offsets in the input string. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6943 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6944 | Args: |
| 6945 | text: a string to parse |
| 6946 | offset: additional offset to add to positions in the text to get correct |
| 6947 | position in the complete ICU string. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6948 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6949 | Returns: |
| 6950 | List of tuples, each tuple consist of four fields: variant name, |
| 6951 | variant name span (tuple of two integers), variant value, value |
| 6952 | span (tuple of two integers). |
| 6953 | """ |
| 6954 | depth, start, end = 0, -1, -1 |
| 6955 | variants = [] |
| 6956 | key = None |
| 6957 | for idx, char in enumerate(text): |
| 6958 | if char == '{': |
| 6959 | if not depth: |
| 6960 | start = idx |
| 6961 | chunk = text[end + 1:start] |
| 6962 | key = chunk.strip() |
| 6963 | pos = offset + end + 1 + chunk.find(key) |
| 6964 | span = (pos, pos + len(key)) |
| 6965 | depth += 1 |
| 6966 | elif char == '}': |
| 6967 | if not depth: |
| 6968 | return variants, depth, offset + idx |
| 6969 | depth -= 1 |
| 6970 | if not depth: |
| 6971 | end = idx |
| 6972 | variants.append((key, span, text[start:end + 1], |
| 6973 | (offset + start, offset + end + 1))) |
| 6974 | return variants, depth, offset + end + 1 |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6975 | |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 6976 | old_sys_path = sys.path |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6977 | try: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6978 | sys.path = sys.path + [ |
| 6979 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 6980 | 'translation') |
| 6981 | ] |
| 6982 | from helper import grd_helper |
| 6983 | finally: |
| 6984 | sys.path = old_sys_path |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6985 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6986 | for f in affected_grds: |
| 6987 | file_path = f.LocalPath() |
| 6988 | old_id_to_msg_map = {} |
| 6989 | new_id_to_msg_map = {} |
| 6990 | # Note that this code doesn't check if the file has been deleted. This is |
| 6991 | # OK because it only uses the old and new file contents and doesn't load |
| 6992 | # the file via its path. |
| 6993 | # It's also possible that a file's content refers to a renamed or deleted |
| 6994 | # file via a <part> tag, such as <part file="now-deleted-file.grdp">. This |
| 6995 | # is OK as well, because grd_helper ignores <part> tags when loading .grd or |
| 6996 | # .grdp files. |
| 6997 | if file_path.endswith('.grdp'): |
| 6998 | if f.OldContents(): |
| 6999 | old_id_to_msg_map = grd_helper.GetGrdpMessagesFromString( |
| 7000 | '\n'.join(f.OldContents())) |
| 7001 | if f.NewContents(): |
| 7002 | new_id_to_msg_map = grd_helper.GetGrdpMessagesFromString( |
| 7003 | '\n'.join(f.NewContents())) |
| 7004 | else: |
| 7005 | file_dir = input_api.os_path.dirname(file_path) or '.' |
| 7006 | if f.OldContents(): |
| 7007 | old_id_to_msg_map = grd_helper.GetGrdMessages( |
| 7008 | StringIO('\n'.join(f.OldContents())), file_dir) |
| 7009 | if f.NewContents(): |
| 7010 | new_id_to_msg_map = grd_helper.GetGrdMessages( |
| 7011 | StringIO('\n'.join(f.NewContents())), file_dir) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 7012 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7013 | grd_name, ext = input_api.os_path.splitext( |
| 7014 | input_api.os_path.basename(file_path)) |
| 7015 | screenshots_dir = input_api.os_path.join( |
| 7016 | input_api.os_path.dirname(file_path), |
| 7017 | grd_name + ext.replace('.', '_')) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 7018 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7019 | # Compute added, removed and modified message IDs. |
| 7020 | old_ids = set(old_id_to_msg_map) |
| 7021 | new_ids = set(new_id_to_msg_map) |
| 7022 | added_ids = new_ids - old_ids |
| 7023 | removed_ids = old_ids - new_ids |
| 7024 | modified_ids = set([]) |
| 7025 | for key in old_ids.intersection(new_ids): |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7026 | if (old_id_to_msg_map[key].ContentsAsXml('', True) |
| 7027 | != new_id_to_msg_map[key].ContentsAsXml('', True)): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7028 | # The message content itself changed. Require an updated screenshot. |
| 7029 | modified_ids.add(key) |
| 7030 | elif old_id_to_msg_map[key].attrs['meaning'] != \ |
| 7031 | new_id_to_msg_map[key].attrs['meaning']: |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 7032 | # The message meaning changed. We later check for a screenshot. |
| 7033 | modified_ids.add(key) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7034 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7035 | if run_screenshot_check: |
| 7036 | # Check the screenshot directory for .png files. Warn if there is any. |
| 7037 | for png_path in affected_png_paths: |
| 7038 | if png_path.startswith(screenshots_dir): |
| 7039 | unnecessary_screenshots.append(png_path) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7040 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7041 | for added_id in added_ids: |
| 7042 | _CheckScreenshotAdded(screenshots_dir, added_id) |
Rainhard Findling | d8d0437 | 2020-08-13 13:30:09 | [diff] [blame] | 7043 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7044 | for modified_id in modified_ids: |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 7045 | _CheckScreenshotModified(screenshots_dir, modified_id) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7046 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7047 | for removed_id in removed_ids: |
| 7048 | _CheckScreenshotRemoved(screenshots_dir, removed_id) |
| 7049 | |
| 7050 | # Check new and changed strings for ICU syntax errors. |
| 7051 | for key in added_ids.union(modified_ids): |
| 7052 | msg = new_id_to_msg_map[key].ContentsAsXml('', True) |
| 7053 | err = _ValidateIcuSyntax(msg, 0, []) |
| 7054 | if err is not None: |
| 7055 | icu_syntax_errors.append(str(key) + ': ' + str(err[0])) |
| 7056 | |
| 7057 | results = [] |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 7058 | if run_screenshot_check: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7059 | if unnecessary_screenshots: |
| 7060 | results.append( |
| 7061 | output_api.PresubmitError( |
| 7062 | 'Do not include actual screenshots in the changelist. Run ' |
| 7063 | 'tools/translate/upload_screenshots.py to upload them instead:', |
| 7064 | sorted(unnecessary_screenshots))) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7065 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7066 | if missing_sha1: |
| 7067 | results.append( |
| 7068 | output_api.PresubmitError( |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 7069 | 'You are adding UI strings.\n' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7070 | 'To ensure the best translations, take screenshots of the relevant UI ' |
| 7071 | '(https://g.co/chrome/translation) and add these files to your ' |
| 7072 | 'changelist:', sorted(missing_sha1))) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7073 | |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 7074 | if invalid_sha1: |
| 7075 | results.append( |
| 7076 | output_api.PresubmitError( |
| 7077 | 'The following files do not seem to contain valid sha1 hashes. ' |
| 7078 | 'Make sure they contain hashes created by ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7079 | 'tools/translate/upload_screenshots.py:', |
| 7080 | sorted(invalid_sha1))) |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 7081 | |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 7082 | if missing_sha1_modified: |
| 7083 | results.append( |
| 7084 | output_api.PresubmitError( |
| 7085 | 'You are modifying UI strings or their meanings.\n' |
| 7086 | 'To ensure the best translations, take screenshots of the relevant UI ' |
| 7087 | '(https://g.co/chrome/translation) and add these files to your ' |
| 7088 | 'changelist:', sorted(missing_sha1_modified))) |
| 7089 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7090 | if unnecessary_sha1_files: |
| 7091 | results.append( |
| 7092 | output_api.PresubmitError( |
| 7093 | 'You removed strings associated with these files. Remove:', |
| 7094 | sorted(unnecessary_sha1_files))) |
| 7095 | else: |
| 7096 | results.append( |
| 7097 | output_api.PresubmitPromptOrNotify('Skipping translation ' |
| 7098 | 'screenshots check.')) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7099 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7100 | if icu_syntax_errors: |
| 7101 | results.append( |
| 7102 | output_api.PresubmitPromptWarning( |
| 7103 | 'ICU syntax errors were found in the following strings (problems or ' |
| 7104 | 'feedback? Contact [email protected]):', |
| 7105 | items=icu_syntax_errors)) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 7106 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7107 | return results |
Mustafa Emre Acer | 51f2f74 | 2020-03-09 19:41:12 | [diff] [blame] | 7108 | |
| 7109 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7110 | def CheckTranslationExpectations(input_api, |
| 7111 | output_api, |
| 7112 | repo_root=None, |
| 7113 | translation_expectations_path=None, |
| 7114 | grd_files=None): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7115 | import sys |
| 7116 | affected_grds = [ |
| 7117 | f for f in input_api.AffectedFiles() |
| 7118 | if (f.LocalPath().endswith('.grd') or f.LocalPath().endswith('.grdp')) |
| 7119 | ] |
| 7120 | if not affected_grds: |
| 7121 | return [] |
| 7122 | |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7123 | old_sys_path = sys.path |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7124 | try: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7125 | sys.path = sys.path + [ |
| 7126 | input_api.os_path.join(input_api.PresubmitLocalPath(), 'tools', |
| 7127 | 'translation') |
| 7128 | ] |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7129 | sys.path = sys.path + [ |
| 7130 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 7131 | 'third_party', 'depot_tools') |
| 7132 | ] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7133 | from helper import git_helper |
| 7134 | from helper import translation_helper |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7135 | import gclient_utils |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7136 | finally: |
| 7137 | sys.path = old_sys_path |
| 7138 | |
| 7139 | # Check that translation expectations can be parsed and we can get a list of |
| 7140 | # translatable grd files. |repo_root| and |translation_expectations_path| are |
| 7141 | # only passed by tests. |
| 7142 | if not repo_root: |
| 7143 | repo_root = input_api.PresubmitLocalPath() |
| 7144 | if not translation_expectations_path: |
| 7145 | translation_expectations_path = input_api.os_path.join( |
| 7146 | repo_root, 'tools', 'gritsettings', 'translation_expectations.pyl') |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7147 | is_cog = gclient_utils.IsEnvCog() |
| 7148 | # Git is not available in cog workspaces. |
| 7149 | if not grd_files and not is_cog: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7150 | grd_files = git_helper.list_grds_in_repository(repo_root) |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7151 | if not grd_files: |
| 7152 | grd_files = [] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7153 | |
| 7154 | # Ignore bogus grd files used only for testing |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 7155 | # ui/webui/resources/tools/generate_grd.py. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7156 | ignore_path = input_api.os_path.join('ui', 'webui', 'resources', 'tools', |
| 7157 | 'tests') |
| 7158 | grd_files = [p for p in grd_files if ignore_path not in p] |
| 7159 | |
Ben Mason | 5d4c324 | 2025-04-15 20:28:37 | [diff] [blame] | 7160 | # Ensure no duplicate basenames. |
| 7161 | basename_to_src_paths = {} |
| 7162 | for grd_path in grd_files: |
| 7163 | basename = input_api.os_path.basename(grd_path) |
| 7164 | basename_to_src_paths.setdefault(basename, []) |
| 7165 | basename_to_src_paths[basename].append(grd_path) |
| 7166 | for src_paths in basename_to_src_paths.values(): |
| 7167 | if len(src_paths) > 1: |
| 7168 | return [ |
| 7169 | output_api.PresubmitNotifyResult( |
| 7170 | 'Multiple string files have the same basename. This will result in ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7171 | 'missing translations. Files: %s' % ', '.join(src_paths)) |
Ben Mason | 5d4c324 | 2025-04-15 20:28:37 | [diff] [blame] | 7172 | ] |
| 7173 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7174 | try: |
| 7175 | translation_helper.get_translatable_grds( |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7176 | repo_root, grd_files, translation_expectations_path, is_cog) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7177 | except Exception as e: |
| 7178 | return [ |
| 7179 | output_api.PresubmitNotifyResult( |
| 7180 | 'Failed to get a list of translatable grd files. This happens when:\n' |
| 7181 | ' - One of the modified grd or grdp files cannot be parsed or\n' |
| 7182 | ' - %s is not updated.\n' |
| 7183 | 'Stack:\n%s' % (translation_expectations_path, str(e))) |
| 7184 | ] |
Mustafa Emre Acer | 51f2f74 | 2020-03-09 19:41:12 | [diff] [blame] | 7185 | return [] |
| 7186 | |
Ken Rockot | c31f483 | 2020-05-29 18:58:51 | [diff] [blame] | 7187 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 7188 | def CheckStableMojomChanges(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7189 | """Changes to [Stable] mojom types must preserve backward-compatibility.""" |
| 7190 | changed_mojoms = input_api.AffectedFiles( |
| 7191 | include_deletes=True, |
| 7192 | file_filter=lambda f: f.LocalPath().endswith(('.mojom'))) |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 7193 | |
Bruce Dawson | 344ab26 | 2022-06-04 11:35:10 | [diff] [blame] | 7194 | if not changed_mojoms or input_api.no_diffs: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7195 | return [] |
| 7196 | |
| 7197 | delta = [] |
| 7198 | for mojom in changed_mojoms: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7199 | delta.append({ |
| 7200 | 'filename': mojom.LocalPath(), |
| 7201 | 'old': '\n'.join(mojom.OldContents()) or None, |
| 7202 | 'new': '\n'.join(mojom.NewContents()) or None, |
| 7203 | }) |
| 7204 | |
| 7205 | process = input_api.subprocess.Popen([ |
Takuto Ikuta | dca1022 | 2022-04-13 02:51:21 | [diff] [blame] | 7206 | input_api.python3_executable, |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7207 | input_api.os_path.join( |
| 7208 | input_api.PresubmitLocalPath(), 'mojo', 'public', 'tools', 'mojom', |
| 7209 | 'check_stable_mojom_compatibility.py'), '--src-root', |
| 7210 | input_api.PresubmitLocalPath() |
| 7211 | ], |
| 7212 | stdin=input_api.subprocess.PIPE, |
| 7213 | stdout=input_api.subprocess.PIPE, |
| 7214 | stderr=input_api.subprocess.PIPE, |
| 7215 | universal_newlines=True) |
| 7216 | (x, error) = process.communicate(input=input_api.json.dumps(delta)) |
| 7217 | if process.returncode: |
| 7218 | return [ |
| 7219 | output_api.PresubmitError( |
| 7220 | 'One or more [Stable] mojom definitions appears to have been changed ' |
Alex Gough | c9992165 | 2024-02-15 22:59:12 | [diff] [blame] | 7221 | 'in a way that is not backward-compatible. See ' |
| 7222 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/mojo/public/tools/bindings/README.md#versioning' |
| 7223 | ' for details.', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7224 | long_text=error) |
| 7225 | ] |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 7226 | return [] |
| 7227 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7228 | |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7229 | def CheckDeprecationOfPreferences(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7230 | """Removing a preference should come with a deprecation.""" |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7231 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7232 | def FilterFile(affected_file): |
| 7233 | """Accept only .cc files and the like.""" |
| 7234 | file_inclusion_pattern = [r'.+%s' % _IMPLEMENTATION_EXTENSIONS] |
| 7235 | files_to_skip = (_EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 7236 | input_api.DEFAULT_FILES_TO_SKIP) |
| 7237 | return input_api.FilterSourceFile( |
| 7238 | affected_file, |
| 7239 | files_to_check=file_inclusion_pattern, |
| 7240 | files_to_skip=files_to_skip) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7241 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7242 | def ModifiedLines(affected_file): |
| 7243 | """Returns a list of tuples (line number, line text) of added and removed |
| 7244 | lines. |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7245 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7246 | Deleted lines share the same line number as the previous line. |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7247 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7248 | This relies on the scm diff output describing each changed code section |
| 7249 | with a line of the form |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7250 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7251 | ^@@ <old line num>,<old size> <new line num>,<new size> @@$ |
| 7252 | """ |
| 7253 | line_num = 0 |
| 7254 | modified_lines = [] |
| 7255 | for line in affected_file.GenerateScmDiff().splitlines(): |
| 7256 | # Extract <new line num> of the patch fragment (see format above). |
| 7257 | m = input_api.re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', |
| 7258 | line) |
| 7259 | if m: |
| 7260 | line_num = int(m.groups(1)[0]) |
| 7261 | continue |
| 7262 | if ((line.startswith('+') and not line.startswith('++')) |
| 7263 | or (line.startswith('-') and not line.startswith('--'))): |
| 7264 | modified_lines.append((line_num, line)) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7265 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7266 | if not line.startswith('-'): |
| 7267 | line_num += 1 |
| 7268 | return modified_lines |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7269 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7270 | def FindLineWith(lines, needle): |
| 7271 | """Returns the line number (i.e. index + 1) in `lines` containing `needle`. |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7272 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7273 | If 0 or >1 lines contain `needle`, -1 is returned. |
| 7274 | """ |
| 7275 | matching_line_numbers = [ |
| 7276 | # + 1 for 1-based counting of line numbers. |
| 7277 | i + 1 for i, line in enumerate(lines) if needle in line |
| 7278 | ] |
| 7279 | return matching_line_numbers[0] if len( |
| 7280 | matching_line_numbers) == 1 else -1 |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7281 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7282 | def ModifiedPrefMigration(affected_file): |
| 7283 | """Returns whether the MigrateObsolete.*Pref functions were modified.""" |
| 7284 | # Determine first and last lines of MigrateObsolete.*Pref functions. |
| 7285 | new_contents = affected_file.NewContents() |
| 7286 | range_1 = (FindLineWith(new_contents, |
| 7287 | 'BEGIN_MIGRATE_OBSOLETE_LOCAL_STATE_PREFS'), |
| 7288 | FindLineWith(new_contents, |
| 7289 | 'END_MIGRATE_OBSOLETE_LOCAL_STATE_PREFS')) |
| 7290 | range_2 = (FindLineWith(new_contents, |
| 7291 | 'BEGIN_MIGRATE_OBSOLETE_PROFILE_PREFS'), |
| 7292 | FindLineWith(new_contents, |
| 7293 | 'END_MIGRATE_OBSOLETE_PROFILE_PREFS')) |
| 7294 | if (-1 in range_1 + range_2): |
| 7295 | raise Exception( |
| 7296 | 'Broken .*MIGRATE_OBSOLETE_.*_PREFS markers in browser_prefs.cc.' |
| 7297 | ) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7298 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7299 | # Check whether any of the modified lines are part of the |
| 7300 | # MigrateObsolete.*Pref functions. |
| 7301 | for line_nr, line in ModifiedLines(affected_file): |
| 7302 | if (range_1[0] <= line_nr <= range_1[1] |
| 7303 | or range_2[0] <= line_nr <= range_2[1]): |
| 7304 | return True |
| 7305 | return False |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7306 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7307 | register_pref_pattern = input_api.re.compile(r'Register.+Pref') |
| 7308 | browser_prefs_file_pattern = input_api.re.compile( |
| 7309 | r'chrome/browser/prefs/browser_prefs.cc') |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7310 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7311 | changes = input_api.AffectedFiles(include_deletes=True, |
| 7312 | file_filter=FilterFile) |
| 7313 | potential_problems = [] |
| 7314 | for f in changes: |
| 7315 | for line in f.GenerateScmDiff().splitlines(): |
| 7316 | # Check deleted lines for pref registrations. |
| 7317 | if (line.startswith('-') and not line.startswith('--') |
| 7318 | and register_pref_pattern.search(line)): |
| 7319 | potential_problems.append('%s: %s' % (f.LocalPath(), line)) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7320 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7321 | if browser_prefs_file_pattern.search(f.LocalPath()): |
| 7322 | # If the developer modified the MigrateObsolete.*Prefs() functions, we |
| 7323 | # assume that they knew that they have to deprecate preferences and don't |
| 7324 | # warn. |
| 7325 | try: |
| 7326 | if ModifiedPrefMigration(f): |
| 7327 | return [] |
| 7328 | except Exception as e: |
| 7329 | return [output_api.PresubmitError(str(e))] |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7330 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7331 | if potential_problems: |
| 7332 | return [ |
| 7333 | output_api.PresubmitPromptWarning( |
| 7334 | 'Discovered possible removal of preference registrations.\n\n' |
| 7335 | 'Please make sure to properly deprecate preferences by clearing their\n' |
| 7336 | 'value for a couple of milestones before finally removing the code.\n' |
| 7337 | 'Otherwise data may stay in the preferences files forever. See\n' |
| 7338 | 'Migrate*Prefs() in chrome/browser/prefs/browser_prefs.cc and\n' |
| 7339 | 'chrome/browser/prefs/README.md for examples.\n' |
| 7340 | 'This may be a false positive warning (e.g. if you move preference\n' |
| 7341 | 'registrations to a different place).\n', potential_problems) |
| 7342 | ] |
| 7343 | return [] |
| 7344 | |
Matt Stark | 6ef0887 | 2021-07-29 01:21:46 | [diff] [blame] | 7345 | |
| 7346 | def CheckConsistentGrdChanges(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7347 | """Changes to GRD files must be consistent for tools to read them.""" |
| 7348 | changed_grds = input_api.AffectedFiles( |
| 7349 | include_deletes=False, |
| 7350 | file_filter=lambda f: f.LocalPath().endswith(('.grd'))) |
| 7351 | errors = [] |
| 7352 | invalid_file_regexes = [(input_api.re.compile(matcher), msg) |
| 7353 | for matcher, msg in _INVALID_GRD_FILE_LINE] |
| 7354 | for grd in changed_grds: |
| 7355 | for i, line in enumerate(grd.NewContents()): |
| 7356 | for matcher, msg in invalid_file_regexes: |
| 7357 | if matcher.search(line): |
| 7358 | errors.append( |
| 7359 | output_api.PresubmitError( |
| 7360 | 'Problem on {grd}:{i} - {msg}'.format( |
| 7361 | grd=grd.LocalPath(), i=i + 1, msg=msg))) |
| 7362 | return errors |
| 7363 | |
Kevin McNee | 967dd2d2 | 2021-11-15 16:09:29 | [diff] [blame] | 7364 | |
Henrique Ferreiro | 2a4b5594 | 2021-11-29 23:45:36 | [diff] [blame] | 7365 | def CheckAssertAshOnlyCode(input_api, output_api): |
| 7366 | """Errors if a BUILD.gn file in an ash/ directory doesn't include |
Georg Neis | 94f87f0 | 2024-10-22 08:20:13 | [diff] [blame] | 7367 | assert(is_chromeos). |
| 7368 | For a transition period, assert(is_chromeos_ash) is also accepted. |
Henrique Ferreiro | 2a4b5594 | 2021-11-29 23:45:36 | [diff] [blame] | 7369 | """ |
| 7370 | |
| 7371 | def FileFilter(affected_file): |
| 7372 | """Includes directories known to be Ash only.""" |
| 7373 | return input_api.FilterSourceFile( |
| 7374 | affected_file, |
| 7375 | files_to_check=( |
| 7376 | r'^ash/.*BUILD\.gn', # Top-level src/ash/. |
| 7377 | r'.*/ash/.*BUILD\.gn'), # Any path component. |
| 7378 | files_to_skip=(input_api.DEFAULT_FILES_TO_SKIP)) |
| 7379 | |
| 7380 | errors = [] |
Georg Neis | 94f87f0 | 2024-10-22 08:20:13 | [diff] [blame] | 7381 | pattern = input_api.re.compile(r'assert\(is_chromeos(_ash)?\b') |
Jameson Thies | 0ce669f | 2021-12-09 15:56:56 | [diff] [blame] | 7382 | for f in input_api.AffectedFiles(include_deletes=False, |
| 7383 | file_filter=FileFilter): |
Henrique Ferreiro | 2a4b5594 | 2021-11-29 23:45:36 | [diff] [blame] | 7384 | if (not pattern.search(input_api.ReadFile(f))): |
| 7385 | errors.append( |
| 7386 | output_api.PresubmitError( |
Georg Neis | 94f87f0 | 2024-10-22 08:20:13 | [diff] [blame] | 7387 | 'Please add assert(is_chromeos) to %s. If that\'s not ' |
| 7388 | 'possible, please create an issue and add a comment such ' |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7389 | 'as:\n # TODO(crbug.com/XXX): add ' |
Georg Neis | 94f87f0 | 2024-10-22 08:20:13 | [diff] [blame] | 7390 | 'assert(is_chromeos) when ...' % f.LocalPath())) |
Henrique Ferreiro | 2a4b5594 | 2021-11-29 23:45:36 | [diff] [blame] | 7391 | return errors |
Lukasz Anforowicz | 7016d05e | 2021-11-30 03:56:27 | [diff] [blame] | 7392 | |
| 7393 | |
Kalvin Lee | 84ad17a | 2023-09-25 11:14:41 | [diff] [blame] | 7394 | def _IsMiraclePtrDisallowed(input_api, affected_file): |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 7395 | path = affected_file.UnixLocalPath() |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7396 | if not _IsCPlusPlusFile(input_api, path): |
| 7397 | return False |
| 7398 | |
Bartek Nowierski | 49b1a45 | 2024-06-08 00:24:35 | [diff] [blame] | 7399 | # Renderer-only code is generally allowed to use MiraclePtr. These |
| 7400 | # directories, however, are specifically disallowed, for perf reasons. |
Kalvin Lee | 84ad17a | 2023-09-25 11:14:41 | [diff] [blame] | 7401 | if ("third_party/blink/renderer/core/" in path |
| 7402 | or "third_party/blink/renderer/platform/heap/" in path |
Bartek Nowierski | 49b1a45 | 2024-06-08 00:24:35 | [diff] [blame] | 7403 | or "third_party/blink/renderer/platform/wtf/" in path |
| 7404 | or "third_party/blink/renderer/platform/fonts/" in path): |
| 7405 | return True |
| 7406 | |
| 7407 | # The below paths are an explicitly listed subset of Renderer-only code, |
| 7408 | # because the plan is to Oilpanize it. |
| 7409 | # TODO(crbug.com/330759291): Remove once Oilpanization is completed or |
| 7410 | # abandoned. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7411 | if ("third_party/blink/renderer/core/paint/" in path or |
| 7412 | "third_party/blink/renderer/platform/graphics/compositing/" in path |
Bartek Nowierski | 49b1a45 | 2024-06-08 00:24:35 | [diff] [blame] | 7413 | or "third_party/blink/renderer/platform/graphics/paint/" in path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7414 | return True |
| 7415 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7416 | # We assume that everything else may be used outside of Renderer processes. |
Lukasz Anforowicz | 7016d05e | 2021-11-30 03:56:27 | [diff] [blame] | 7417 | return False |
| 7418 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7419 | |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7420 | # TODO(crbug.com/40206238): Remove these checks, once they are replaced |
Lukasz Anforowicz | 7016d05e | 2021-11-30 03:56:27 | [diff] [blame] | 7421 | # by the Chromium Clang Plugin (which will be preferable because it will |
| 7422 | # 1) report errors earlier - at compile-time and 2) cover more rules). |
| 7423 | def CheckRawPtrUsage(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7424 | """Rough checks that raw_ptr<T> usage guidelines are followed.""" |
| 7425 | errors = [] |
| 7426 | # The regex below matches "raw_ptr<" following a word boundary, but not in a |
| 7427 | # C++ comment. |
| 7428 | raw_ptr_matcher = input_api.re.compile(r'^((?!//).)*\braw_ptr<') |
Kalvin Lee | 84ad17a | 2023-09-25 11:14:41 | [diff] [blame] | 7429 | file_filter = lambda f: _IsMiraclePtrDisallowed(input_api, f) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7430 | for f, line_num, line in input_api.RightHandSideLines(file_filter): |
| 7431 | if raw_ptr_matcher.search(line): |
| 7432 | errors.append( |
| 7433 | output_api.PresubmitError( |
| 7434 | 'Problem on {path}:{line} - '\ |
Kalvin Lee | 84ad17a | 2023-09-25 11:14:41 | [diff] [blame] | 7435 | 'raw_ptr<T> should not be used in this renderer code '\ |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7436 | '(as documented in the "Pointers to unprotected memory" '\ |
| 7437 | 'section in //base/memory/raw_ptr.md)'.format( |
| 7438 | path=f.LocalPath(), line=line_num))) |
| 7439 | return errors |
Henrique Ferreiro | f9819f2e3 | 2021-11-30 13:31:56 | [diff] [blame] | 7440 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7441 | |
mikt | 9337567c | 2023-09-08 18:38:17 | [diff] [blame] | 7442 | def CheckAdvancedMemorySafetyChecksUsage(input_api, output_api): |
| 7443 | """Checks that ADVANCED_MEMORY_SAFETY_CHECKS() macro is neither added nor |
| 7444 | removed as it is managed by the memory safety team internally. |
| 7445 | Do not add / remove it manually.""" |
| 7446 | paths = set([]) |
| 7447 | # The regex below matches "ADVANCED_MEMORY_SAFETY_CHECKS(" following a word |
| 7448 | # boundary, but not in a C++ comment. |
| 7449 | macro_matcher = input_api.re.compile( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7450 | r'^((?!//).)*\bADVANCED_MEMORY_SAFETY_CHECKS\(', |
| 7451 | input_api.re.MULTILINE) |
mikt | 9337567c | 2023-09-08 18:38:17 | [diff] [blame] | 7452 | for f in input_api.AffectedFiles(): |
| 7453 | if not _IsCPlusPlusFile(input_api, f.LocalPath()): |
| 7454 | continue |
| 7455 | if macro_matcher.search(f.GenerateScmDiff()): |
| 7456 | paths.add(f.LocalPath()) |
| 7457 | if not paths: |
| 7458 | return [] |
| 7459 | return [output_api.PresubmitPromptWarning( |
| 7460 | 'ADVANCED_MEMORY_SAFETY_CHECKS() macro is managed by ' \ |
| 7461 | 'the memory safety team (chrome-memory-safety@). ' \ |
| 7462 | 'Please contact us to add/delete the uses of the macro.', |
| 7463 | paths)] |
Henrique Ferreiro | f9819f2e3 | 2021-11-30 13:31:56 | [diff] [blame] | 7464 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7465 | |
Henrique Ferreiro | f9819f2e3 | 2021-11-30 13:31:56 | [diff] [blame] | 7466 | def CheckPythonShebang(input_api, output_api): |
| 7467 | """Checks that python scripts use #!/usr/bin/env instead of hardcoding a |
| 7468 | system-wide python. |
| 7469 | """ |
| 7470 | errors = [] |
| 7471 | sources = lambda affected_file: input_api.FilterSourceFile( |
| 7472 | affected_file, |
| 7473 | files_to_skip=((_THIRD_PARTY_EXCEPT_BLINK, |
| 7474 | r'third_party/blink/web_tests/external/') + input_api. |
| 7475 | DEFAULT_FILES_TO_SKIP), |
| 7476 | files_to_check=[r'.*\.py$']) |
| 7477 | for f in input_api.AffectedSourceFiles(sources): |
Takuto Ikuta | 3697651 | 2021-11-30 23:15:27 | [diff] [blame] | 7478 | for line_num, line in f.ChangedContents(): |
| 7479 | if line_num == 1 and line.startswith('#!/usr/bin/python'): |
| 7480 | errors.append(f.LocalPath()) |
| 7481 | break |
Henrique Ferreiro | f9819f2e3 | 2021-11-30 13:31:56 | [diff] [blame] | 7482 | |
| 7483 | result = [] |
| 7484 | for file in errors: |
| 7485 | result.append( |
| 7486 | output_api.PresubmitError( |
| 7487 | "Please use '#!/usr/bin/env python/2/3' as the shebang of %s" % |
| 7488 | file)) |
| 7489 | return result |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7490 | |
| 7491 | |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7492 | def CheckAndroidTestAnnotations(input_api, output_api): |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7493 | """Checks that tests have either @Batch or @DoNotBatch annotation. If this |
| 7494 | is not an instrumentation test, disregard.""" |
| 7495 | |
| 7496 | batch_annotation = input_api.re.compile(r'^\s*@Batch') |
| 7497 | do_not_batch_annotation = input_api.re.compile(r'^\s*@DoNotBatch') |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7498 | robolectric_test = input_api.re.compile( |
| 7499 | r'@RunWith\((.*?)RobolectricTestRunner') |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7500 | test_class_declaration = input_api.re.compile(r'^\s*public\sclass.*Test') |
| 7501 | uiautomator_test = input_api.re.compile(r'[uU]i[aA]utomator') |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7502 | test_annotation_declaration = input_api.re.compile( |
| 7503 | r'^\s*public\s@interface\s.*{') |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7504 | |
ckitagawa | e8fd23b | 2022-06-17 15:29:38 | [diff] [blame] | 7505 | missing_annotation_errors = [] |
| 7506 | extra_annotation_errors = [] |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7507 | wrong_robolectric_test_runner_errors = [] |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7508 | |
| 7509 | def _FilterFile(affected_file): |
| 7510 | return input_api.FilterSourceFile( |
| 7511 | affected_file, |
| 7512 | files_to_skip=input_api.DEFAULT_FILES_TO_SKIP, |
| 7513 | files_to_check=[r'.*Test\.java$']) |
| 7514 | |
| 7515 | for f in input_api.AffectedSourceFiles(_FilterFile): |
| 7516 | batch_matched = None |
| 7517 | do_not_batch_matched = None |
| 7518 | is_instrumentation_test = True |
Mark Schillaci | 8ef0d87 | 2023-07-18 22:07:59 | [diff] [blame] | 7519 | test_annotation_declaration_matched = None |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7520 | has_base_robolectric_rule = False |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7521 | for line in f.NewContents(): |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7522 | if 'BaseRobolectricTestRule' in line: |
| 7523 | has_base_robolectric_rule = True |
| 7524 | continue |
| 7525 | if m := robolectric_test.search(line): |
| 7526 | is_instrumentation_test = False |
| 7527 | if m.group(1) == '' and not has_base_robolectric_rule: |
Yiwei Zhang | 5341bf0 | 2025-03-20 16:34:13 | [diff] [blame] | 7528 | path = str(f.LocalPath()) |
| 7529 | # These two spots cannot use it. |
| 7530 | if 'webapk' not in path and 'build' not in path: |
| 7531 | wrong_robolectric_test_runner_errors.append(path) |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7532 | break |
| 7533 | if uiautomator_test.search(line): |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7534 | is_instrumentation_test = False |
| 7535 | break |
| 7536 | if not batch_matched: |
| 7537 | batch_matched = batch_annotation.search(line) |
| 7538 | if not do_not_batch_matched: |
| 7539 | do_not_batch_matched = do_not_batch_annotation.search(line) |
| 7540 | test_class_declaration_matched = test_class_declaration.search( |
| 7541 | line) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7542 | test_annotation_declaration_matched = test_annotation_declaration.search( |
| 7543 | line) |
Mark Schillaci | 8ef0d87 | 2023-07-18 22:07:59 | [diff] [blame] | 7544 | if test_class_declaration_matched or test_annotation_declaration_matched: |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7545 | break |
Mark Schillaci | 8ef0d87 | 2023-07-18 22:07:59 | [diff] [blame] | 7546 | if test_annotation_declaration_matched: |
| 7547 | continue |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7548 | if (is_instrumentation_test and not batch_matched |
| 7549 | and not do_not_batch_matched): |
Sam Maier | 4cef924 | 2022-10-03 14:21:24 | [diff] [blame] | 7550 | missing_annotation_errors.append(str(f.LocalPath())) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7551 | if (not is_instrumentation_test |
| 7552 | and (batch_matched or do_not_batch_matched)): |
Sam Maier | 4cef924 | 2022-10-03 14:21:24 | [diff] [blame] | 7553 | extra_annotation_errors.append(str(f.LocalPath())) |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7554 | |
| 7555 | results = [] |
| 7556 | |
ckitagawa | e8fd23b | 2022-06-17 15:29:38 | [diff] [blame] | 7557 | if missing_annotation_errors: |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7558 | results.append( |
| 7559 | output_api.PresubmitPromptWarning( |
| 7560 | """ |
Andrew Grieve | 43a5cf8 | 2023-09-08 15:09:46 | [diff] [blame] | 7561 | A change was made to an on-device test that has neither been annotated with |
| 7562 | @Batch nor @DoNotBatch. If this is a new test, please add the annotation. If |
| 7563 | this is an existing test, please consider adding it if you are sufficiently |
| 7564 | familiar with the test (but do so as a separate change). |
| 7565 | |
Jens Mueller | 2085ff8 | 2023-02-27 11:54:49 | [diff] [blame] | 7566 | 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] | 7567 | """, missing_annotation_errors)) |
| 7568 | if extra_annotation_errors: |
| 7569 | results.append( |
| 7570 | output_api.PresubmitPromptWarning( |
| 7571 | """ |
| 7572 | Robolectric tests do not need a @Batch or @DoNotBatch annotations. |
| 7573 | """, extra_annotation_errors)) |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7574 | if wrong_robolectric_test_runner_errors: |
| 7575 | results.append( |
| 7576 | output_api.PresubmitPromptWarning( |
| 7577 | """ |
Wenyu Fu | 0005ab8 | 2025-01-03 18:13:26 | [diff] [blame] | 7578 | Robolectric tests should use either @RunWith(BaseRobolectricTestRunner.class) (or |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7579 | a subclass of it), or use "@Rule BaseRobolectricTestRule". |
| 7580 | """, wrong_robolectric_test_runner_errors)) |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7581 | |
| 7582 | return results |
Sam Maier | 4cef924 | 2022-10-03 14:21:24 | [diff] [blame] | 7583 | |
| 7584 | |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7585 | def _CheckAndroidNullAwayAnnotatedClasses(input_api, output_api): |
| 7586 | """Checks that Java classes/interfaces/annotations are null-annotated.""" |
| 7587 | |
Henrique Nakashima | 2bdd8ad | 2025-04-08 18:24:57 | [diff] [blame] | 7588 | # Temporary, crbug.com/389129271 |
| 7589 | if input_api.change.RepositoryRoot().endswith('clank'): |
| 7590 | return [] |
| 7591 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7592 | nullmarked_annotation = input_api.re.compile( |
| 7593 | r'^\s*@(NullMarked|NullUnmarked)') |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7594 | |
| 7595 | missing_annotation_errors = [] |
| 7596 | |
| 7597 | def _FilterFile(affected_file): |
| 7598 | return input_api.FilterSourceFile( |
| 7599 | affected_file, |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7600 | files_to_skip=( |
| 7601 | _EXCLUDED_PATHS + _TEST_CODE_EXCLUDED_PATHS + |
| 7602 | input_api.DEFAULT_FILES_TO_SKIP + ( |
| 7603 | r'.*Test.*\.java', |
| 7604 | r'^android_webview/.*', # Temporary, crbug.com/389129271 |
| 7605 | r'^build/.*', |
| 7606 | r'^chrome/android/.*', # Temporary, crbug.com/389129271 |
| 7607 | r'^chromecast/.*', |
| 7608 | r'^components/cronet/.*', |
| 7609 | r'^tools/.*', |
| 7610 | )), |
| 7611 | files_to_check=[r'.*\.java$']) |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7612 | |
| 7613 | for f in input_api.AffectedSourceFiles(_FilterFile): |
Henrique Nakashima | c660543 | 2025-04-24 18:11:59 | [diff] [blame] | 7614 | if f.Action() != 'A': |
| 7615 | continue |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7616 | for line in f.NewContents(): |
| 7617 | if nullmarked_annotation.search(line): |
| 7618 | break |
| 7619 | else: |
| 7620 | missing_annotation_errors.append(str(f.LocalPath())) |
| 7621 | |
| 7622 | results = [] |
| 7623 | |
| 7624 | if missing_annotation_errors: |
| 7625 | results.append( |
Henrique Nakashima | 8bafbc5 | 2025-04-22 19:38:42 | [diff] [blame] | 7626 | output_api.PresubmitError( |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7627 | """ |
| 7628 | Please add @NullMarked and fix the NullAway warnings in the following files |
| 7629 | (see https://chromium.googlesource.com/chromium/src/+/main/styleguide/java/nullaway.md): |
| 7630 | """, missing_annotation_errors)) |
| 7631 | |
| 7632 | return results |
| 7633 | |
| 7634 | |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7635 | def CheckNoJsInIos(input_api, output_api): |
| 7636 | """Checks to make sure that JavaScript files are not used on iOS.""" |
| 7637 | |
| 7638 | def _FilterFile(affected_file): |
| 7639 | return input_api.FilterSourceFile( |
| 7640 | affected_file, |
| 7641 | files_to_skip=input_api.DEFAULT_FILES_TO_SKIP + |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7642 | (r'^ios/third_party/*', r'^ios/tools/*', r'^third_party/*', |
| 7643 | r'^components/autofill/ios/form_util/resources/*'), |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7644 | files_to_check=[r'^ios/.*\.js$', r'.*/ios/.*\.js$']) |
| 7645 | |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7646 | deleted_files = [] |
| 7647 | |
| 7648 | # Collect filenames of all removed JS files. |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 7649 | for f in input_api.AffectedFiles(file_filter=_FilterFile): |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7650 | local_path = f.LocalPath() |
| 7651 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7652 | if input_api.os_path.splitext( |
| 7653 | local_path)[1] == '.js' and f.Action() == 'D': |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7654 | deleted_files.append(input_api.os_path.basename(local_path)) |
| 7655 | |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7656 | error_paths = [] |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7657 | moved_paths = [] |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7658 | warning_paths = [] |
| 7659 | |
| 7660 | for f in input_api.AffectedSourceFiles(_FilterFile): |
| 7661 | local_path = f.LocalPath() |
| 7662 | |
| 7663 | if input_api.os_path.splitext(local_path)[1] == '.js': |
| 7664 | if f.Action() == 'A': |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7665 | if input_api.os_path.basename(local_path) in deleted_files: |
| 7666 | # This script was probably moved rather than newly created. |
| 7667 | # Present a warning instead of an error for these cases. |
| 7668 | moved_paths.append(local_path) |
| 7669 | else: |
| 7670 | error_paths.append(local_path) |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7671 | elif f.Action() != 'D': |
| 7672 | warning_paths.append(local_path) |
| 7673 | |
| 7674 | results = [] |
| 7675 | |
| 7676 | if warning_paths: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7677 | results.append( |
| 7678 | output_api.PresubmitPromptWarning( |
| 7679 | 'TypeScript is now fully supported for iOS feature scripts. ' |
| 7680 | 'Consider converting JavaScript files to TypeScript. See ' |
| 7681 | '//ios/web/public/js_messaging/README.md for more details.', |
| 7682 | warning_paths)) |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7683 | |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7684 | if moved_paths: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7685 | results.append( |
| 7686 | output_api.PresubmitPromptWarning( |
| 7687 | 'Do not use JavaScript on iOS for new files as TypeScript is ' |
| 7688 | 'fully supported. (If this is a moved file, you may leave the ' |
| 7689 | 'script unconverted.) See //ios/web/public/js_messaging/README.md ' |
| 7690 | 'for help using scripts on iOS.', moved_paths)) |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7691 | |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7692 | if error_paths: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7693 | results.append( |
| 7694 | output_api.PresubmitError( |
| 7695 | 'Do not use JavaScript on iOS as TypeScript is fully supported. ' |
| 7696 | 'See //ios/web/public/js_messaging/README.md for help using ' |
| 7697 | 'scripts on iOS.', error_paths)) |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7698 | |
| 7699 | return results |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7700 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7701 | |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7702 | def CheckLibcxxRevisionsMatch(input_api, output_api): |
| 7703 | """Check to make sure the libc++ version matches across deps files.""" |
Andrew Grieve | 21bb679 | 2023-03-27 19:06:48 | [diff] [blame] | 7704 | # Disable check for changes to sub-repositories. |
| 7705 | if input_api.PresubmitLocalPath() != input_api.change.RepositoryRoot(): |
Sam Maier | b926c58c | 2023-08-08 19:58:25 | [diff] [blame] | 7706 | return [] |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7707 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7708 | DEPS_FILES = ['DEPS', 'buildtools/deps_revisions.gni'] |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7709 | |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 7710 | file_filter = lambda f: f.UnixLocalPath() in DEPS_FILES |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7711 | changed_deps_files = input_api.AffectedFiles(file_filter=file_filter) |
| 7712 | if not changed_deps_files: |
| 7713 | return [] |
| 7714 | |
| 7715 | def LibcxxRevision(file): |
| 7716 | file = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 7717 | *file.split('/')) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7718 | return input_api.re.search(r'libcxx_revision.*[:=].*[\'"](\w+)[\'"]', |
| 7719 | input_api.ReadFile(file)).group(1) |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7720 | |
| 7721 | if len(set([LibcxxRevision(f) for f in DEPS_FILES])) == 1: |
| 7722 | return [] |
| 7723 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7724 | return [ |
| 7725 | output_api.PresubmitError( |
| 7726 | 'libcxx_revision not equal across %s' % ', '.join(DEPS_FILES), |
| 7727 | changed_deps_files) |
| 7728 | ] |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7729 | |
| 7730 | |
| 7731 | def CheckDanglingUntriaged(input_api, output_api): |
| 7732 | """Warn developers adding DanglingUntriaged raw_ptr.""" |
| 7733 | |
| 7734 | # Ignore during git presubmit --all. |
| 7735 | # |
| 7736 | # This would be too costly, because this would check every lines of every |
| 7737 | # C++ files. Check from _BANNED_CPP_FUNCTIONS are also reading the whole |
| 7738 | # source code, but only once to apply every checks. It seems the bots like |
| 7739 | # `win-presubmit` are particularly sensitive to reading the files. Adding |
| 7740 | # this check caused the bot to run 2x longer. See https://crbug.com/1486612. |
| 7741 | if input_api.no_diffs: |
Arthur Sonzogni | 9eafd22 | 2023-11-10 08:50:39 | [diff] [blame] | 7742 | return [] |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7743 | |
| 7744 | def FilterFile(file): |
| 7745 | return input_api.FilterSourceFile( |
| 7746 | file, |
| 7747 | files_to_check=[r".*\.(h|cc|cpp|cxx|m|mm)$"], |
| 7748 | files_to_skip=[r"^base/allocator.*"], |
| 7749 | ) |
| 7750 | |
| 7751 | count = 0 |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 7752 | for f in input_api.AffectedFiles(file_filter=FilterFile): |
Arthur Sonzogni | 9eafd22 | 2023-11-10 08:50:39 | [diff] [blame] | 7753 | count -= sum([l.count("DanglingUntriaged") for l in f.OldContents()]) |
| 7754 | count += sum([l.count("DanglingUntriaged") for l in f.NewContents()]) |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7755 | |
| 7756 | # Most likely, nothing changed: |
| 7757 | if count == 0: |
| 7758 | return [] |
| 7759 | |
| 7760 | # Congrats developers for improving it: |
| 7761 | if count < 0: |
Arthur Sonzogni | 9eafd22 | 2023-11-10 08:50:39 | [diff] [blame] | 7762 | message = f"DanglingUntriaged pointers removed: {-count}\nThank you!" |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7763 | return [output_api.PresubmitNotifyResult(message)] |
| 7764 | |
| 7765 | # Check for 'DanglingUntriaged-notes' in the description: |
| 7766 | notes_regex = input_api.re.compile("DanglingUntriaged-notes[:=]") |
| 7767 | if any( |
| 7768 | notes_regex.match(line) |
| 7769 | for line in input_api.change.DescriptionText().splitlines()): |
| 7770 | return [] |
| 7771 | |
| 7772 | # Check for DanglingUntriaged-notes in the git footer: |
| 7773 | if input_api.change.GitFootersFromDescription().get( |
| 7774 | "DanglingUntriaged-notes", []): |
| 7775 | return [] |
| 7776 | |
| 7777 | message = ( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7778 | "Unexpected new occurrences of `DanglingUntriaged` detected. Please\n" |
| 7779 | + "avoid adding new ones\n" + "\n" + "See documentation:\n" + |
| 7780 | "https://chromium.googlesource.com/chromium/src/+/main/docs/dangling_ptr.md\n" |
| 7781 | + "\n" + "See also the guide to fix dangling pointers:\n" + |
| 7782 | "https://chromium.googlesource.com/chromium/src/+/main/docs/dangling_ptr_guide.md\n" |
| 7783 | + "\n" + |
Arthur Sonzogni | 9eafd22 | 2023-11-10 08:50:39 | [diff] [blame] | 7784 | "To disable this warning, please add in the commit description:\n" + |
Alex Gough | 26dcd85 | 2023-12-22 16:47:19 | [diff] [blame] | 7785 | "DanglingUntriaged-notes: <rationale for new untriaged dangling " + |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7786 | "pointers>") |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7787 | return [output_api.PresubmitPromptWarning(message)] |
Jan Keitel | 77be752 | 2023-10-12 20:40:49 | [diff] [blame] | 7788 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7789 | |
Jan Keitel | 77be752 | 2023-10-12 20:40:49 | [diff] [blame] | 7790 | def CheckInlineConstexprDefinitionsInHeaders(input_api, output_api): |
| 7791 | """Checks that non-static constexpr definitions in headers are inline.""" |
| 7792 | # In a properly formatted file, constexpr definitions inside classes or |
| 7793 | # structs will have additional whitespace at the beginning of the line. |
| 7794 | # The pattern looks for variables initialized as constexpr kVar = ...; or |
| 7795 | # constexpr kVar{...}; |
| 7796 | # The pattern does not match expressions that have braces in kVar to avoid |
| 7797 | # matching constexpr functions. |
| 7798 | pattern = input_api.re.compile(r'^constexpr (?!inline )[^\(\)]*[={]') |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7799 | attribute_pattern = input_api.re.compile( |
| 7800 | r'(\[\[[a-zA-Z_:]+\]\]|[A-Z]+[A-Z_]+) ') |
Jan Keitel | 77be752 | 2023-10-12 20:40:49 | [diff] [blame] | 7801 | problems = [] |
| 7802 | for f in input_api.AffectedFiles(): |
| 7803 | if not _IsCPlusPlusHeaderFile(input_api, f.LocalPath()): |
| 7804 | continue |
| 7805 | |
| 7806 | for line_number, line in f.ChangedContents(): |
| 7807 | line = attribute_pattern.sub('', line) |
| 7808 | if pattern.search(line): |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7809 | problems.append(f"{f.LocalPath()}: {line_number}\n {line}") |
Jan Keitel | 77be752 | 2023-10-12 20:40:49 | [diff] [blame] | 7810 | |
| 7811 | if problems: |
| 7812 | return [ |
| 7813 | output_api.PresubmitPromptWarning( |
| 7814 | 'Consider inlining constexpr variable definitions in headers ' |
| 7815 | 'outside of classes to avoid unnecessary copies of the ' |
| 7816 | 'constant. See https://abseil.io/tips/168 for more details.', |
| 7817 | problems) |
| 7818 | ] |
| 7819 | else: |
| 7820 | return [] |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7821 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7822 | |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7823 | def CheckTodoBugReferences(input_api, output_api): |
| 7824 | """Checks that bugs in TODOs use updated issue tracker IDs.""" |
| 7825 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7826 | files_to_skip = [ |
| 7827 | 'PRESUBMIT_test.py', r"^third_party/rust/chromium_crates_io/vendor/.*" |
| 7828 | ] |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7829 | |
| 7830 | def _FilterFile(affected_file): |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7831 | return input_api.FilterSourceFile(affected_file, |
| 7832 | files_to_skip=files_to_skip) |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7833 | |
| 7834 | # Monorail bug IDs are all less than or equal to 1524553 so check that all |
| 7835 | # bugs in TODOs are greater than that value. |
Tom Sepez | 8e62858 | 2025-02-14 02:18:55 | [diff] [blame] | 7836 | pattern = input_api.re.compile(r'.*\bTODO\([^\)0-9]*([0-9]+)\).*') |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7837 | problems = [] |
| 7838 | for f in input_api.AffectedSourceFiles(_FilterFile): |
| 7839 | for line_number, line in f.ChangedContents(): |
| 7840 | match = pattern.match(line) |
| 7841 | if match and int(match.group(1)) <= 1524553: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7842 | problems.append(f"{f.LocalPath()}: {line_number}\n {line}") |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7843 | |
| 7844 | if problems: |
| 7845 | return [ |
| 7846 | output_api.PresubmitPromptWarning( |
Alison Gale | cb598de5 | 2024-04-26 17:03:25 | [diff] [blame] | 7847 | 'TODOs should use the new Chromium Issue Tracker IDs which can ' |
| 7848 | 'be found by navigating to the bug. See ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7849 | 'https://crbug.com/336778624 for more details.', problems) |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7850 | ] |
| 7851 | else: |
| 7852 | return [] |