Avi Drissman | 2497659 | 2022-09-12 15:24:31 | [diff] [blame] | 1 | # Copyright 2012 The Chromium Authors |
maruel@chromium.org | ca8d1984 | 2009-02-19 16:33:12 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
maruel@chromium.org | ca8d1984 | 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. |
maruel@chromium.org | ca8d1984 | 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 typingimportCallable |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 11 | from typingimportOptional |
| 12 | from typingimportSequence |
mikt | 19226ff2 | 2024-08-27 05:28:21 | [diff] [blame] | 13 | from typingimportTuple |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 14 | from dataclassesimport dataclass |
| 15 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 16 | PRESUBMIT_VERSION='2.0.0' |
joi@chromium.org | eea609a | 2011-11-18 13:10:12 | [diff] [blame] | 17 | |
maruel@chromium.org | 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/.*", |
maruel@chromium.org | 3e4eb11 | 2011-01-18 03:29:54 | [diff] [blame] | 39 | r".*MakeFile$", |
gman@chromium.org | 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$", |
maruel@chromium.org | 430641764 | 2009-06-11 00:33:40 | [diff] [blame] | 53 | ) |
maruel@chromium.org | ca8d1984 | 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 | |
joi@chromium.org | 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 | |
joi@chromium.org | 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/.*', |
darin@chromium.org | 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/.*', |
lliabraa@chromium.org | 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/*') |
maruel@chromium.org | ca8d1984 | 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 | |
joi@chromium.org | 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).') |
joi@chromium.org | 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 | classBanRule: |
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 |
marja@chromium.org | cf9b78f | 2012-11-14 11:40:28 | [diff] [blame] | 142 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 143 | |
Daniel Cheng | 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 ' |
Vikram Pasupathy | b052c89 | 2025-07-08 18:10:00 | [diff] [blame] | 232 | 'should either be passed in explicitly or retrieved from an existing ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 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( |
Sirisha Kavuluru | 042e477 | 2025-07-03 02:41:09 | [diff] [blame] | 266 | pattern=(r'/((DeviceInfo\.isDesktop\()|IS_DESKTOP_ANDROID)'), |
Nate Fischer | d541ff8 | 2025-03-11 21:34:19 | [diff] [blame] | 267 | explanation=( |
Sirisha Kavuluru | 042e477 | 2025-07-03 02:41:09 | [diff] [blame] | 268 | 'Do not add new uses of IS_DESKTOP_ANDROID build flag or ' |
| 269 | 'DeviceInfo.isDesktop() until you have the approval of tedchoc@ or ' |
| 270 | 'twellington@. Background: it is highly important to reduce the ' |
| 271 | 'divergence of features across platforms. ' |
Eric Lok | c26a4666 | 2025-05-02 01:04:03 | [diff] [blame] | 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, |
avi@chromium.org | 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, |
avi@chromium.org | 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, |
avi@chromium.org | 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, |
avi@chromium.org | 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, |
avi@chromium.org | 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, |
avi@chromium.org | 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, |
avi@chromium.org | 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, |
avi@chromium.org | 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 | ), |
avi@chromium.org | 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 | ), |
avi@chromium.org | 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 | (), |
enne@chromium.org | 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 | (), |
enne@chromium.org | 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 | (), |
enne@chromium.org | 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 | (), |
enne@chromium.org | 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 | (), |
mark@chromium.org | 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 | ), |
mark@chromium.org | 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\.*',), |
jochen@chromium.org | 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 cxx@chromium.org 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. |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 838 | r'.+_fuzzer\.cc$', |
| 839 | r'.+_fuzzertest\.cc$', |
| 840 | # TODO(https://crbug.com/1380528): These are all unsanctioned uses of |
| 841 | # the standard library's random number generators, and should be |
| 842 | # migrated to the //base equivalent. |
| 843 | r'ash/ambient/model/ambient_topic_queue\.cc', |
| 844 | r'base/allocator/partition_allocator/src/partition_alloc/partition_alloc_unittest\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 845 | r'base/test/launcher/test_launcher\.cc', |
| 846 | r'cc/metrics/video_playback_roughness_reporter_unittest\.cc', |
| 847 | r'chrome/browser/apps/app_service/metrics/website_metrics\.cc', |
| 848 | r'chrome/browser/ash/power/auto_screen_brightness/monotone_cubic_spline_unittest\.cc', |
| 849 | r'chrome/browser/ash/printing/zeroconf_printer_detector_unittest\.cc', |
| 850 | r'chrome/browser/nearby_sharing/contacts/nearby_share_contact_manager_impl_unittest\.cc', |
| 851 | r'chrome/browser/nearby_sharing/contacts/nearby_share_contacts_sorter_unittest\.cc', |
| 852 | r'chrome/browser/privacy_budget/mesa_distribution_unittest\.cc', |
| 853 | r'chrome/browser/web_applications/test/web_app_test_utils\.cc', |
| 854 | r'chrome/browser/web_applications/test/web_app_test_utils\.cc', |
| 855 | r'chrome/browser/win/conflicts/module_blocklist_cache_util_unittest\.cc', |
| 856 | r'chromeos/ash/components/memory/userspace_swap/swap_storage_unittest\.cc', |
| 857 | r'chromeos/ash/components/memory/userspace_swap/userspace_swap\.cc', |
| 858 | r'components/metrics/metrics_state_manager\.cc', |
| 859 | r'components/omnibox/browser/history_quick_provider_performance_unittest\.cc', |
| 860 | r'components/zucchini/disassembler_elf_unittest\.cc', |
| 861 | r'content/browser/webid/federated_auth_request_impl\.cc', |
| 862 | r'content/browser/webid/federated_auth_request_impl\.cc', |
| 863 | r'media/cast/test/utility/udp_proxy\.h', |
| 864 | r'sql/recover_module/module_unittest\.cc', |
Nicolas Dossou-Gbete | af9023d | 2025-04-07 17:44:38 | [diff] [blame] | 865 | r'components/regional_capabilities/regional_capabilities_utils.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 866 | # Do not add new entries to this list. If you have a use case which is |
| 867 | # not satisfied by the current APIs (i.e. you need an explicitly-seeded |
| 868 | # sequence, or stability of some sort is required), please contact |
| 869 | # cxx@chromium.org. |
| 870 | ], |
Daniel Cheng | 192683f | 2022-11-01 20:52:44 | [diff] [blame] | 871 | ), |
| 872 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 873 | r'/\b(absl,std)::bind_front\b', |
| 874 | ('{absl,std}::bind_front() are banned. Use base::Bind{Once,Repeating}() ' |
| 875 | 'instead.',), |
| 876 | True, |
| 877 | [_THIRD_PARTY_EXCEPT_BLINK],# Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 878 | ), |
| 879 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 880 | r'/\bABSL_FLAG\b', |
| 881 | ('ABSL_FLAG is banned. Use base::CommandLine instead.',), |
| 882 | True, |
| 883 | [_THIRD_PARTY_EXCEPT_BLINK],# Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 884 | ), |
| 885 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 886 | r'/\babsl::c_', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 887 | ('Abseil container utilities are banned. Use std::ranges:: instead.', |
| 888 | ), |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 889 | True, |
| 890 | [_THIRD_PARTY_EXCEPT_BLINK],# Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 891 | ), |
| 892 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 893 | r'/\babsl::FixedArray\b', |
| 894 | ('absl::FixedArray is banned. Use base::FixedArray instead.',), |
| 895 | True, |
| 896 | [ |
| 897 | # base::FixedArray provides canonical access. |
| 898 | r'^base/types/fixed_array.h', |
| 899 | # Not an error in third_party folders. |
| 900 | _THIRD_PARTY_EXCEPT_BLINK, |
| 901 | ], |
Peter Kasting | 431239a | 2023-09-29 03:11:44 | [diff] [blame] | 902 | ), |
| 903 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 904 | r'/\babsl::FunctionRef\b', |
| 905 | ('absl::FunctionRef is banned. Use base::FunctionRef instead.',), |
| 906 | True, |
| 907 | [ |
| 908 | # base::Bind{Once,Repeating} references absl::FunctionRef to disallow |
| 909 | # interoperability. |
| 910 | r'^base/functional/bind_internal\.h', |
| 911 | # base::FunctionRef is implemented on top of absl::FunctionRef. |
| 912 | r'^base/functional/function_ref.*\..+', |
| 913 | # Not an error in third_party folders. |
| 914 | _THIRD_PARTY_EXCEPT_BLINK, |
| 915 | ], |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 916 | ), |
| 917 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 918 | r'/\babsl::(Insecure)?BitGen\b', |
| 919 | ('absl random number generators are banned. Use the helpers in ' |
| 920 | 'base/rand_util.h instead, e.g. base::RandBytes() or ', |
| 921 | 'base::RandomBitGenerator.'), |
| 922 | True, |
| 923 | [_THIRD_PARTY_EXCEPT_BLINK],# Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 924 | ), |
| 925 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 926 | pattern=r'/\babsl::(optional|nullopt|make_optional)\b', |
Peter Kasting | 3b77a0c | 2024-08-22 00:22:26 | [diff] [blame] | 927 | explanation=('absl::optional is banned. Use std::optional instead.',), |
| 928 | treat_as_error=True, |
| 929 | excluded_paths=[ |
| 930 | _THIRD_PARTY_EXCEPT_BLINK, |
| 931 | ]), |
| 932 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 933 | r'/(\babsl::Span\b|#include <span>|\bstd::span\b)', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 934 | ('absl::Span and std::span are banned. Use base::span instead.',), |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 935 | True, |
| 936 | [ |
| 937 | # Included for conversions between base and std. |
| 938 | r'base/containers/span.h', |
| 939 | # Test base::span<> compatibility against std::span<>. |
| 940 | r'base/containers/span_unittest.cc', |
| 941 | # //base/numerics can't use base or absl. So it uses std. |
| 942 | r'base/numerics/.*' |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 943 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 944 | # Needed to use QUICHE API. |
Ciara McMullin | c029c8e | 2024-08-21 14:22:32 | [diff] [blame] | 945 | r'android_webview/browser/ip_protection/.*', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 946 | r'chrome/browser/ip_protection/.*', |
| 947 | r'components/ip_protection/.*', |
| 948 | r'net/third_party/quiche/overrides/quiche_platform_impl/quiche_stack_trace_impl\.*', |
| 949 | r'services/network/web_transport\.cc', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 950 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 951 | # Not an error in third_party folders. |
| 952 | _THIRD_PARTY_EXCEPT_BLINK, |
| 953 | ], |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 954 | ), |
| 955 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 956 | r'/\babsl::StatusOr\b', |
| 957 | ('absl::StatusOr is banned. Use base::expected instead.',), |
| 958 | True, |
| 959 | [ |
| 960 | # Needed to use liburlpattern API. |
| 961 | r'components/url_pattern/.*', |
| 962 | r'services/network/shared_dictionary/simple_url_pattern_matcher\.cc', |
| 963 | r'third_party/blink/renderer/core/url_pattern/.*', |
| 964 | r'third_party/blink/renderer/modules/manifest/manifest_parser\.cc', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 965 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 966 | # Needed to use QUICHE API. |
Ciara McMullin | c029c8e | 2024-08-21 14:22:32 | [diff] [blame] | 967 | r'android_webview/browser/ip_protection/.*', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 968 | r'chrome/browser/ip_protection/.*', |
| 969 | r'components/ip_protection/.*', |
Victor Vasiliev | 435ec10 | 2025-05-07 08:33:39 | [diff] [blame] | 970 | r'net/quic/dedicated_web_transport_http3_client\.cc', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 971 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 972 | # Needed to use MediaPipe API. |
| 973 | r'components/media_effects/.*\.cc', |
| 974 | # Not an error in third_party folders. |
| 975 | _THIRD_PARTY_EXCEPT_BLINK |
| 976 | ], |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 977 | ), |
| 978 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 979 | r'/\babsl::(StrSplit|StrJoin|StrCat|StrAppend|Substitute|StrContains)\b', |
| 980 | ('Abseil string utilities are banned. Use base/strings instead.',), |
| 981 | True, |
| 982 | [_THIRD_PARTY_EXCEPT_BLINK],# Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 983 | ), |
| 984 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 985 | r'/\babsl::(Mutex|CondVar|Notification|Barrier|BlockingCounter)\b', |
| 986 | ( |
| 987 | 'Abseil synchronization primitives are banned. Use', |
| 988 | 'base/synchronization instead.', |
| 989 | ), |
| 990 | True, |
| 991 | [_THIRD_PARTY_EXCEPT_BLINK],# Not an error in third_party folders. |
Peter Kasting | 4f35bfc | 2022-10-18 18:39:12 | [diff] [blame] | 992 | ), |
| 993 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 994 | r'/\babsl::(Duration|Time|TimeZone|CivilDay)\b', |
| 995 | ('Abseil\'s time library is banned. Use base/time instead.',), |
| 996 | True, |
| 997 | [ |
| 998 | # Needed to use QUICHE API. |
Ciara McMullin | c029c8e | 2024-08-21 14:22:32 | [diff] [blame] | 999 | r'android_webview/browser/ip_protection/.*', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1000 | r'chrome/browser/ip_protection/.*', |
| 1001 | r'components/ip_protection/.*', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 1002 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1003 | # Needed to integrate with //third_party/nearby |
| 1004 | r'components/cross_device/nearby/system_clock.cc', |
| 1005 | _THIRD_PARTY_EXCEPT_BLINK# Not an error in third_party folders. |
| 1006 | ], |
| 1007 | ), |
| 1008 | BanRule( |
Victor Hugo Vianna Silva | 6e84e8d4 | 2025-03-19 00:32:00 | [diff] [blame] | 1009 | r'/absl::(bad_variant_access|get|holds_alternative|monostate|variant|' |
| 1010 | r'visit)', |
| 1011 | ('Abseil\'s variant library is banned, use std.',), |
Victor Hugo Vianna Silva | d9139fbe | 2025-03-19 20:59:08 | [diff] [blame] | 1012 | True, |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1013 | [_THIRD_PARTY_EXCEPT_BLINK], |
Victor Hugo Vianna Silva | 6e84e8d4 | 2025-03-19 00:32:00 | [diff] [blame] | 1014 | ), |
| 1015 | BanRule( |
| 1016 | r'/absl::(apply|exchange|forward|in_place|index_sequence|' |
| 1017 | r'integer_sequence|make_from_tuple|make_index_sequence|' |
| 1018 | r'make_integer_sequence|move)', |
| 1019 | ('Abseil\'s util library is banned, use std.',), |
Victor Hugo Vianna Silva | d9139fbe | 2025-03-19 20:59:08 | [diff] [blame] | 1020 | True, |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1021 | [_THIRD_PARTY_EXCEPT_BLINK], |
Victor Hugo Vianna Silva | 6e84e8d4 | 2025-03-19 00:32:00 | [diff] [blame] | 1022 | ), |
| 1023 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1024 | r'/#include <chrono>', |
| 1025 | ('<chrono> is banned. Use base/time instead.',), |
| 1026 | True, |
| 1027 | [ |
| 1028 | # Not an error in third_party folders: |
| 1029 | _THIRD_PARTY_EXCEPT_BLINK, |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1030 | # This uses openscreen API depending on std::chrono. |
| 1031 | "components/openscreen_platform/task_runner.cc", |
| 1032 | ]), |
| 1033 | BanRule( |
| 1034 | r'/#include <exception>', |
| 1035 | ('Exceptions are banned and disabled in Chromium.',), |
| 1036 | True, |
| 1037 | [_THIRD_PARTY_EXCEPT_BLINK],# Not an error in third_party folders. |
| 1038 | ), |
| 1039 | BanRule( |
| 1040 | r'/\bstd::function\b', |
| 1041 | ('std::function is banned. Use base::{Once,Repeating}Callback instead.', |
| 1042 | ), |
| 1043 | True, |
| 1044 | [ |
| 1045 | # Has tests that template trait helpers don't unintentionally match |
| 1046 | # std::function. |
| 1047 | r'base/functional/callback_helpers_unittest\.cc', |
| 1048 | # Required to implement interfaces from the third-party perfetto |
| 1049 | # library. |
| 1050 | r'base/tracing/perfetto_task_runner\.cc', |
| 1051 | r'base/tracing/perfetto_task_runner\.h', |
| 1052 | # Needed for interop with the third-party nearby library type |
| 1053 | # location::nearby::connections::ResultCallback. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1054 | r'chrome/services/sharing/nearby/nearby_connections_conversions\.cc' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1055 | # Needed for interop with the internal libassistant library. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1056 | r'chromeos/ash/services/libassistant/callback_utils\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1057 | # Needed for interop with Fuchsia fidl APIs. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1058 | r'fuchsia_web/webengine/browser/context_impl_browsertest\.cc', |
| 1059 | r'fuchsia_web/webengine/browser/cookie_manager_impl_unittest\.cc', |
| 1060 | r'fuchsia_web/webengine/browser/media_player_impl_unittest\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1061 | # Required to interop with interfaces from the third-party ChromeML |
| 1062 | # library API. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1063 | r'services/on_device_model/ml/chrome_ml_api\.h', |
| 1064 | r'services/on_device_model/ml/on_device_model_executor\.cc', |
| 1065 | r'services/on_device_model/ml/on_device_model_executor\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1066 | # Required to interop with interfaces from the third-party perfetto |
| 1067 | # library. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1068 | r'components/tracing/common/etw_consumer_win_unittest\.cc', |
| 1069 | r'services/tracing/public/cpp/perfetto/custom_event_recorder\.cc', |
| 1070 | r'services/tracing/public/cpp/perfetto/perfetto_traced_process\.cc', |
| 1071 | r'services/tracing/public/cpp/perfetto/perfetto_traced_process\.h', |
| 1072 | r'services/tracing/public/cpp/perfetto/perfetto_tracing_backend\.cc', |
| 1073 | r'services/tracing/public/cpp/perfetto/producer_client\.cc', |
| 1074 | r'services/tracing/public/cpp/perfetto/producer_client\.h', |
| 1075 | r'services/tracing/public/cpp/perfetto/producer_test_utils\.cc', |
| 1076 | r'services/tracing/public/cpp/perfetto/producer_test_utils\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1077 | # Required for interop with the third-party webrtc library. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1078 | r'third_party/blink/renderer/modules/peerconnection/mock_peer_connection_impl\.cc', |
| 1079 | r'third_party/blink/renderer/modules/peerconnection/mock_peer_connection_impl\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1080 | # TODO(https://crbug.com/1364577): Various uses that should be |
| 1081 | # migrated to something else. |
| 1082 | # Should use base::OnceCallback or base::RepeatingCallback. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1083 | r'base/allocator/dispatcher/initializer_unittest\.cc', |
| 1084 | r'chrome/browser/ash/accessibility/speech_monitor\.cc', |
| 1085 | r'chrome/browser/ash/accessibility/speech_monitor\.h', |
| 1086 | r'chrome/browser/ash/login/ash_hud_login_browsertest\.cc', |
| 1087 | r'chromecast/base/observer_unittest\.cc', |
| 1088 | r'chromecast/browser/cast_web_view\.h', |
| 1089 | r'chromecast/public/cast_media_shlib\.h', |
| 1090 | r'device/bluetooth/floss/exported_callback_manager\.h', |
| 1091 | r'device/bluetooth/floss/floss_dbus_client\.h', |
| 1092 | r'device/fido/cable/v2_handshake_unittest\.cc', |
| 1093 | r'device/fido/pin\.cc', |
| 1094 | r'services/tracing/perfetto/test_utils\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1095 | # Should use base::FunctionRef. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1096 | r'chrome/browser/media/webrtc/test_stats_dictionary\.cc', |
| 1097 | r'chrome/browser/media/webrtc/test_stats_dictionary\.h', |
| 1098 | r'chromeos/ash/services/libassistant/device_settings_controller\.cc', |
| 1099 | r'components/browser_ui/client_certificate/android/ssl_client_certificate_request\.cc', |
| 1100 | r'components/gwp_asan/client/sampling_malloc_shims_unittest\.cc', |
| 1101 | r'content/browser/font_unique_name_lookup/font_unique_name_lookup_unittest\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1102 | # Does not need std::function at all. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1103 | r'components/omnibox/browser/autocomplete_result\.cc', |
| 1104 | r'device/fido/win/webauthn_api\.cc', |
| 1105 | r'media/audio/alsa/alsa_util\.cc', |
| 1106 | r'media/remoting/stream_provider\.h', |
| 1107 | r'sql/vfs_wrapper\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1108 | # TODO(https://crbug.com/1364585): Remove usage and exception list |
| 1109 | # entries. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1110 | r'extensions/renderer/api/automation/automation_internal_custom_bindings\.cc', |
| 1111 | r'extensions/renderer/api/automation/automation_internal_custom_bindings\.h', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1112 | # TODO(https://crbug.com/1364579): Remove usage and exception list |
| 1113 | # entry. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1114 | r'ui/views/controls/focus_ring\.h', |
Lei Zhang | 1f9d9ec4 | 2024-06-20 18:42:27 | [diff] [blame] | 1115 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1116 | # Various pre-existing uses in //tools that is low-priority to fix. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1117 | r'tools/binary_size/libsupersize/viewer/caspian/diff\.cc', |
| 1118 | r'tools/binary_size/libsupersize/viewer/caspian/model\.cc', |
| 1119 | r'tools/binary_size/libsupersize/viewer/caspian/model\.h', |
| 1120 | r'tools/binary_size/libsupersize/viewer/caspian/tree_builder\.h', |
| 1121 | r'tools/clang/base_bind_rewriters/BaseBindRewriters\.cpp', |
Daniel Cheng | e5583e3c | 2022-09-22 00:19:41 | [diff] [blame] | 1122 | |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1123 | # Not an error in third_party folders. |
| 1124 | _THIRD_PARTY_EXCEPT_BLINK |
| 1125 | ], |
Daniel Bratell | 609102be | 2019-03-27 20:53:21 | [diff] [blame] | 1126 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1127 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1128 | r'/#include <X11/', |
| 1129 | ('Do not use Xlib. Use xproto (from //ui/gfx/x:xproto) instead.',), |
| 1130 | True, |
| 1131 | [_THIRD_PARTY_EXCEPT_BLINK],# Not an error in third_party folders. |
Tom Anderson | a95e1204 | 2020-09-09 23:08:00 | [diff] [blame] | 1132 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1133 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1134 | r'/\bstd::ratio\b', |
| 1135 | ('std::ratio is banned by the Google Style Guide.',), |
| 1136 | True, |
| 1137 | [_THIRD_PARTY_EXCEPT_BLINK],# Not an error in third_party folders. |
Daniel Bratell | 69334cc | 2019-03-26 11:07:45 | [diff] [blame] | 1138 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1139 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1140 | r'/\bstd::aligned_alloc\b', |
| 1141 | ( |
| 1142 | 'std::aligned_alloc() is not yet allowed (crbug.com/1412818). Use ', |
| 1143 | 'base::AlignedAlloc() instead.', |
| 1144 | ), |
| 1145 | True, |
| 1146 | [_THIRD_PARTY_EXCEPT_BLINK],# Don't warn in third_party folders. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1147 | ), |
| 1148 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1149 | r'/#include <(barrier|latch|semaphore|stop_token)>', |
| 1150 | ('The thread support library is banned. Use base/synchronization ' |
| 1151 | 'instead.',), |
| 1152 | True, |
| 1153 | [_THIRD_PARTY_EXCEPT_BLINK],# Don't warn in third_party folders. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1154 | ), |
| 1155 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1156 | r'/\bstd::execution::(par|seq)\b', |
| 1157 | ('std::execution::(par|seq) is banned; they do not fit into ' |
| 1158 | ' Chrome\'s threading model, and libc++ doesn\'t have full ' |
mikt | 19226ff2 | 2024-08-27 05:28:21 | [diff] [blame] | 1159 | 'support.',), |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1160 | True, |
| 1161 | [_THIRD_PARTY_EXCEPT_BLINK], |
Helmut Januschka | 7cc8a84f | 2024-02-07 22:50:41 | [diff] [blame] | 1162 | ), |
| 1163 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1164 | r'/\bstd::bit_cast\b', |
| 1165 | ('std::bit_cast is banned; use base::bit_cast instead for values and ' |
| 1166 | 'standard C++ casting when pointers are involved.',), |
| 1167 | True, |
| 1168 | [ |
| 1169 | # Don't warn in third_party folders. |
| 1170 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1171 | # //base/numerics can't use base or absl. |
| 1172 | r'base/numerics/.*' |
| 1173 | ], |
Avi Drissman | 70cb7f7 | 2023-12-12 17:44:37 | [diff] [blame] | 1174 | ), |
| 1175 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1176 | r'/\bstd::(c8rtomb|mbrtoc8)\b', |
| 1177 | ('std::c8rtomb() and std::mbrtoc8() are banned.',), |
| 1178 | True, |
| 1179 | [_THIRD_PARTY_EXCEPT_BLINK],# Don't warn in third_party folders. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1180 | ), |
| 1181 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1182 | r'/\bchar8_t|std::u8string\b', |
| 1183 | ( |
| 1184 | 'char8_t and std::u8string are not yet allowed. Can you use [unsigned]', |
| 1185 | ' char and std::string instead?', |
| 1186 | ), |
| 1187 | True, |
| 1188 | [ |
| 1189 | # The demangler does not use this type but needs to know about it. |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 1190 | r'base/third_party/symbolize/demangle\.cc', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1191 | # Don't warn in third_party folders. |
| 1192 | _THIRD_PARTY_EXCEPT_BLINK |
| 1193 | ], |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1194 | ), |
| 1195 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1196 | r'/(\b(co_await|co_return|co_yield)\b|#include <coroutine>)', |
| 1197 | ('Coroutines are not yet allowed (https://crbug.com/1403840).',), |
| 1198 | True, |
| 1199 | [_THIRD_PARTY_EXCEPT_BLINK],# Don't warn in third_party folders. |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1200 | ), |
| 1201 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1202 | r'/^\s*(export\s|import\s+["<:\w]|module(;|\s+[:\w]))', |
| 1203 | ('Modules are disallowed for now due to lack of toolchain support.',), |
| 1204 | True, |
| 1205 | [_THIRD_PARTY_EXCEPT_BLINK],# Don't warn in third_party folders. |
Peter Kasting | 69357dc | 2023-03-14 01:34:29 | [diff] [blame] | 1206 | ), |
| 1207 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1208 | r'/\[\[(\w*::)?no_unique_address\]\]', |
| 1209 | ( |
| 1210 | '[[no_unique_address]] does not work as expected on Windows ', |
| 1211 | '(https://crbug.com/1414621). Use NO_UNIQUE_ADDRESS instead.', |
| 1212 | ), |
| 1213 | True, |
| 1214 | [ |
| 1215 | # NO_UNIQUE_ADDRESS / PA_NO_UNIQUE_ADDRESS provide canonical access. |
| 1216 | r'^base/compiler_specific\.h', |
| 1217 | r'^base/allocator/partition_allocator/src/partition_alloc/partition_alloc_base/compiler_specific\.h', |
| 1218 | # Not an error in third_party folders. |
| 1219 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1220 | ], |
Peter Kasting | 8bc046d2 | 2023-11-14 00:38:03 | [diff] [blame] | 1221 | ), |
| 1222 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1223 | r'/#include <format>', |
| 1224 | ('<format> is not yet allowed. Use base::StringPrintf() instead.',), |
| 1225 | True, |
| 1226 | [_THIRD_PARTY_EXCEPT_BLINK],# Don't warn in third_party folders. |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1227 | ), |
| 1228 | BanRule( |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1229 | pattern='std::views', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1230 | explanation=('Use of std::views is banned in Chrome. If you need this ' |
| 1231 | 'functionality, please contact cxx@chromium.org.',), |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1232 | treat_as_error=True, |
| 1233 | excluded_paths=[ |
| 1234 | # Don't warn in third_party folders. |
| 1235 | _THIRD_PARTY_EXCEPT_BLINK |
| 1236 | ], |
| 1237 | ), |
| 1238 | BanRule( |
| 1239 | # Ban everything except specifically allowlisted constructs. |
Daniel Cheng | 70a3527 | 2025-05-06 16:41:34 | [diff] [blame] | 1240 | pattern=r'/std::ranges::(?!(?:'+'|'.join(( |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1241 | # From https://en.cppreference.com/w/cpp/ranges: |
| 1242 | # Range access |
| 1243 | 'begin', |
| 1244 | 'end', |
| 1245 | 'cbegin', |
| 1246 | 'cend', |
| 1247 | 'rbegin', |
| 1248 | 'rend', |
| 1249 | 'crbegin', |
| 1250 | 'crend', |
| 1251 | 'size', |
| 1252 | 'ssize', |
| 1253 | 'empty', |
| 1254 | 'data', |
| 1255 | 'cdata', |
| 1256 | # Range primitives |
| 1257 | 'iterator_t', |
| 1258 | 'const_iterator_t', |
| 1259 | 'sentinel_t', |
| 1260 | 'const_sentinel_t', |
| 1261 | 'range_difference_t', |
| 1262 | 'range_size_t', |
| 1263 | 'range_value_t', |
| 1264 | 'range_reference_t', |
| 1265 | 'range_const_reference_t', |
| 1266 | 'range_rvalue_reference_t', |
| 1267 | 'range_common_reference_t', |
| 1268 | # Dangling iterator handling |
| 1269 | 'dangling', |
| 1270 | 'borrowed_iterator_t', |
| 1271 | # Banned: borrowed_subrange_t |
| 1272 | # Range concepts |
| 1273 | 'range', |
| 1274 | 'borrowed_range', |
| 1275 | 'sized_range', |
| 1276 | 'view', |
| 1277 | 'input_range', |
| 1278 | 'output_range', |
| 1279 | 'forward_range', |
| 1280 | 'bidirectional_range', |
| 1281 | 'random_access_range', |
| 1282 | 'contiguous_range', |
| 1283 | 'common_range', |
| 1284 | 'viewable_range', |
| 1285 | 'constant_range', |
Michael Tang | 626d898 | 2025-05-08 23:24:29 | [diff] [blame] | 1286 | # Views |
| 1287 | 'subrange', |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1288 | # Banned: Range factories |
| 1289 | # Banned: Range adaptors |
Peter Kasting | a7f9375 | 2024-10-24 22:15:40 | [diff] [blame] | 1290 | # Incidentally listed on |
| 1291 | # https://en.cppreference.com/w/cpp/header/ranges: |
| 1292 | 'enable_borrowed_range', |
| 1293 | 'enable_view', |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1294 | # From https://en.cppreference.com/w/cpp/algorithm/ranges: |
| 1295 | # Constrained algorithms: non-modifying sequence operations |
| 1296 | 'all_of', |
| 1297 | 'any_of', |
| 1298 | 'none_of', |
| 1299 | 'for_each', |
| 1300 | 'for_each_n', |
| 1301 | 'count', |
| 1302 | 'count_if', |
| 1303 | 'mismatch', |
| 1304 | 'equal', |
| 1305 | 'lexicographical_compare', |
| 1306 | 'find', |
| 1307 | 'find_if', |
| 1308 | 'find_if_not', |
| 1309 | 'find_end', |
| 1310 | 'find_first_of', |
| 1311 | 'adjacent_find', |
| 1312 | 'search', |
| 1313 | 'search_n', |
| 1314 | # Constrained algorithms: modifying sequence operations |
| 1315 | 'copy', |
| 1316 | 'copy_if', |
| 1317 | 'copy_n', |
| 1318 | 'copy_backward', |
| 1319 | 'move', |
| 1320 | 'move_backward', |
| 1321 | 'fill', |
| 1322 | 'fill_n', |
| 1323 | 'transform', |
| 1324 | 'generate', |
| 1325 | 'generate_n', |
| 1326 | 'remove', |
| 1327 | 'remove_if', |
| 1328 | 'remove_copy', |
| 1329 | 'remove_copy_if', |
| 1330 | 'replace', |
| 1331 | 'replace_if', |
| 1332 | 'replace_copy', |
| 1333 | 'replace_copy_if', |
| 1334 | 'swap_ranges', |
| 1335 | 'reverse', |
| 1336 | 'reverse_copy', |
| 1337 | 'rotate', |
| 1338 | 'rotate_copy', |
| 1339 | 'shuffle', |
| 1340 | 'sample', |
| 1341 | 'unique', |
| 1342 | 'unique_copy', |
| 1343 | # Constrained algorithms: partitioning operations |
| 1344 | 'is_partitioned', |
| 1345 | 'partition', |
| 1346 | 'partition_copy', |
| 1347 | 'stable_partition', |
| 1348 | 'partition_point', |
| 1349 | # Constrained algorithms: sorting operations |
| 1350 | 'is_sorted', |
| 1351 | 'is_sorted_until', |
| 1352 | 'sort', |
| 1353 | 'partial_sort', |
| 1354 | 'partial_sort_copy', |
| 1355 | 'stable_sort', |
| 1356 | 'nth_element', |
| 1357 | # Constrained algorithms: binary search operations (on sorted ranges) |
| 1358 | 'lower_bound', |
| 1359 | 'upper_bound', |
| 1360 | 'binary_search', |
| 1361 | 'equal_range', |
| 1362 | # Constrained algorithms: set operations (on sorted ranges) |
| 1363 | 'merge', |
| 1364 | 'inplace_merge', |
| 1365 | 'includes', |
| 1366 | 'set_difference', |
| 1367 | 'set_intersection', |
| 1368 | 'set_symmetric_difference', |
| 1369 | 'set_union', |
| 1370 | # Constrained algorithms: heap operations |
| 1371 | 'is_heap', |
| 1372 | 'is_heap_until', |
| 1373 | 'make_heap', |
| 1374 | 'push_heap', |
| 1375 | 'pop_heap', |
| 1376 | 'sort_heap', |
| 1377 | # Constrained algorithms: minimum/maximum operations |
| 1378 | 'max', |
| 1379 | 'max_element', |
| 1380 | 'min', |
| 1381 | 'min_element', |
| 1382 | 'minmax', |
| 1383 | 'minmax_element', |
| 1384 | 'clamp', |
| 1385 | # Constrained algorithms: permutation operations |
| 1386 | 'is_permutation', |
| 1387 | 'next_permutation', |
| 1388 | 'prev_premutation', |
| 1389 | # Constrained uninitialized memory algorithms |
| 1390 | 'uninitialized_copy', |
| 1391 | 'uninitialized_copy_n', |
| 1392 | 'uninitialized_fill', |
| 1393 | 'uninitialized_fill_n', |
| 1394 | 'uninitialized_move', |
| 1395 | 'uninitialized_move_n', |
| 1396 | 'uninitialized_default_construct', |
| 1397 | 'uninitialized_default_construct_n', |
| 1398 | 'uninitialized_value_construct', |
| 1399 | 'uninitialized_value_construct_n', |
| 1400 | 'destroy', |
| 1401 | 'destroy_n', |
| 1402 | 'destroy_at', |
| 1403 | 'construct_at', |
| 1404 | # Return types |
| 1405 | 'in_fun_result', |
| 1406 | 'in_in_result', |
| 1407 | 'in_out_result', |
| 1408 | 'in_in_out_result', |
| 1409 | 'in_out_out_result', |
| 1410 | 'min_max_result', |
| 1411 | 'in_found_result', |
Peter Kasting | f379c02 | 2025-01-13 14:01:00 | [diff] [blame] | 1412 | # From https://en.cppreference.com/w/cpp/header/functional |
| 1413 | 'equal_to', |
| 1414 | 'not_equal_to', |
| 1415 | 'greater', |
| 1416 | 'less', |
| 1417 | 'greater_equal', |
| 1418 | 'less_equal', |
danakj | 91c715bb | 2024-07-10 13:24:26 | [diff] [blame] | 1419 | # From https://en.cppreference.com/w/cpp/iterator |
| 1420 | 'advance', |
| 1421 | 'distance', |
| 1422 | 'next', |
| 1423 | 'prev', |
Daniel Cheng | 70a3527 | 2025-05-06 16:41:34 | [diff] [blame] | 1424 | # Require a word boundary at the end of negative lookahead |
| 1425 | # assertion, e.g. to ensure that even though `view` is allowed (and |
| 1426 | # should not match this regex), `views` is still treated as |
| 1427 | # disallowed (and matches the regex). |
| 1428 | ))+ r')\b)\w+', |
Daniel Cheng | 8971922 | 2024-07-04 04:59:29 | [diff] [blame] | 1429 | explanation=( |
| 1430 | 'Use of range views and associated helpers is banned in Chrome. ' |
| 1431 | 'If you need this functionality, please contact cxx@chromium.org.', |
| 1432 | ), |
| 1433 | treat_as_error=True, |
| 1434 | excluded_paths=[ |
| 1435 | # Don't warn in third_party folders. |
| 1436 | _THIRD_PARTY_EXCEPT_BLINK |
| 1437 | ], |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1438 | ), |
| 1439 | BanRule( |
Peter Kasting | 31879d8 | 2024-10-07 20:18:39 | [diff] [blame] | 1440 | r'/#include <regex>', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1441 | ('<regex> is not allowed. Use third_party/re2 instead.',), |
Peter Kasting | 31879d8 | 2024-10-07 20:18:39 | [diff] [blame] | 1442 | True, |
| 1443 | [_THIRD_PARTY_EXCEPT_BLINK],# Don't warn in third_party folders. |
| 1444 | ), |
| 1445 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1446 | r'/#include <source_location>', |
| 1447 | ('<source_location> is not yet allowed. Use base/location.h instead.', |
| 1448 | ), |
| 1449 | True, |
| 1450 | [_THIRD_PARTY_EXCEPT_BLINK],# Don't warn in third_party folders. |
Peter Kasting | e2c5ee8 | 2023-02-15 17:23:08 | [diff] [blame] | 1451 | ), |
| 1452 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1453 | r'/\bstd::to_address\b', |
| 1454 | ( |
| 1455 | 'std::to_address is banned because it is not guaranteed to be', |
| 1456 | 'SFINAE-compatible. Use base::to_address from base/types/to_address.h', |
| 1457 | 'instead.', |
| 1458 | ), |
| 1459 | True, |
| 1460 | [ |
| 1461 | # Needed in base::to_address implementation. |
| 1462 | r'base/types/to_address.h', |
| 1463 | _THIRD_PARTY_EXCEPT_BLINK |
| 1464 | ],# Not an error in third_party folders. |
Nick Diego Yamane | e522ae8 | 2024-02-27 04:23:22 | [diff] [blame] | 1465 | ), |
| 1466 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1467 | r'/#include <syncstream>', |
| 1468 | ('<syncstream> is banned.',), |
| 1469 | True, |
| 1470 | [_THIRD_PARTY_EXCEPT_BLINK],# Don't warn in third_party folders. |
Peter Kasting | 6d77e9d | 2023-02-09 21:58:18 | [diff] [blame] | 1471 | ), |
| 1472 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1473 | r'/\bRunMessageLoop\b', |
| 1474 | ('RunMessageLoop is deprecated, use RunLoop instead.',), |
| 1475 | False, |
| 1476 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1477 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1478 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1479 | 'RunAllPendingInMessageLoop()', |
| 1480 | ( |
| 1481 | "Prefer RunLoop over RunAllPendingInMessageLoop, please contact gab@", |
| 1482 | "if you're convinced you need this.", |
| 1483 | ), |
| 1484 | False, |
| 1485 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1486 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1487 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1488 | 'RunAllPendingInMessageLoop(BrowserThread', |
| 1489 | ( |
| 1490 | 'RunAllPendingInMessageLoop is deprecated. Use RunLoop for', |
| 1491 | 'BrowserThread::UI, BrowserTaskEnvironment::RunIOThreadUntilIdle', |
| 1492 | 'for BrowserThread::IO, and prefer RunLoop::QuitClosure to observe', |
| 1493 | 'async events instead of flushing threads.', |
| 1494 | ), |
| 1495 | False, |
| 1496 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1497 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1498 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1499 | r'MessageLoopRunner', |
| 1500 | ('MessageLoopRunner is deprecated, use RunLoop instead.',), |
| 1501 | False, |
| 1502 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1503 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1504 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1505 | 'GetDeferredQuitTaskForRunLoop', |
| 1506 | ( |
| 1507 | "GetDeferredQuitTaskForRunLoop shouldn't be needed, please contact", |
| 1508 | "gab@ if you found a use case where this is the only solution.", |
| 1509 | ), |
| 1510 | False, |
| 1511 | (), |
Gabriel Charette | 147335ea | 2018-03-22 15:59:19 | [diff] [blame] | 1512 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1513 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1514 | 'sqlite3_initialize(', |
| 1515 | ( |
| 1516 | 'Instead of calling sqlite3_initialize(), depend on //sql, ', |
| 1517 | '#include "sql/initialize.h" and use sql::EnsureSqliteInitialized().', |
| 1518 | ), |
| 1519 | True, |
| 1520 | ( |
| 1521 | r'^sql/initialization\.(cc|h)$', |
| 1522 | r'^third_party/sqlite/.*\.(c|cc|h)$', |
| 1523 | ), |
Victor Costan | 3653df6 | 2018-02-08 21:38:16 | [diff] [blame] | 1524 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1525 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1526 | 'CREATE VIEW', |
| 1527 | ( |
| 1528 | 'SQL views are disabled in Chromium feature code', |
| 1529 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/sql#no-views', |
| 1530 | ), |
| 1531 | True, |
| 1532 | ( |
| 1533 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1534 | # sql/ itself uses views when using memory-mapped IO. |
| 1535 | r'^sql/.*', |
| 1536 | # Various performance tools that do not build as part of Chrome. |
| 1537 | r'^infra/.*', |
| 1538 | r'^tools/perf.*', |
| 1539 | r'.*perfetto.*', |
| 1540 | ), |
Austin Sullivan | d661ab5 | 2022-11-16 08:55:15 | [diff] [blame] | 1541 | ), |
| 1542 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1543 | 'CREATE VIRTUAL TABLE', |
| 1544 | ( |
| 1545 | 'SQL virtual tables are disabled in Chromium feature code', |
| 1546 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/sql#no-virtual-tables', |
| 1547 | ), |
| 1548 | True, |
| 1549 | ( |
| 1550 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1551 | # sql/ itself uses virtual tables in the recovery module and tests. |
| 1552 | r'^sql/.*', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1553 | # Various performance tools that do not build as part of Chrome. |
| 1554 | r'^tools/perf.*', |
| 1555 | r'.*perfetto.*', |
| 1556 | ), |
Austin Sullivan | d661ab5 | 2022-11-16 08:55:15 | [diff] [blame] | 1557 | ), |
| 1558 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1559 | 'std::random_shuffle', |
| 1560 | ('std::random_shuffle is deprecated in C++14, and removed in C++17. Use', |
| 1561 | 'base::RandomShuffle instead.'), |
| 1562 | True, |
| 1563 | (), |
tzik | 5de2157f | 2018-05-08 03:42:47 | [diff] [blame] | 1564 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1565 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1566 | 'ios/web/public/test/http_server', |
| 1567 | ('web::HTTPserver is deprecated use net::EmbeddedTestServer instead.', |
| 1568 | ), |
| 1569 | False, |
| 1570 | (), |
Javier Ernesto Flores Robles | 749e6c2 | 2018-10-08 09:36:24 | [diff] [blame] | 1571 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1572 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1573 | 'GetAddressOf', |
| 1574 | ('Improper use of Microsoft::WRL::ComPtr<T>::GetAddressOf() has been ', |
| 1575 | 'implicated in a few leaks. ReleaseAndGetAddressOf() is safe but ', |
| 1576 | 'operator& is generally recommended. So always use operator& instead. ', |
| 1577 | 'See http://crbug.com/914910 for more conversion guidance.'), |
| 1578 | True, |
| 1579 | (), |
Robert Liao | 764c949 | 2019-01-24 18:46:28 | [diff] [blame] | 1580 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1581 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1582 | 'SHFileOperation', |
| 1583 | ('SHFileOperation was deprecated in Windows Vista, and there are less ', |
| 1584 | 'complex functions to achieve the same goals. Use IFileOperation for ', |
| 1585 | 'any esoteric actions instead.'), |
| 1586 | True, |
| 1587 | (), |
Ben Lewis | a951460 | 2019-04-29 17:53:05 | [diff] [blame] | 1588 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1589 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1590 | 'StringFromGUID2', |
| 1591 | ('StringFromGUID2 introduces an unnecessary dependency on ole32.dll.', |
| 1592 | 'Use base::win::WStringFromGUID instead.'), |
| 1593 | True, |
| 1594 | (r'/base/win/win_util_unittest.cc',), |
Cliff Smolinsky | 8195164 | 2019-04-30 21:39:51 | [diff] [blame] | 1595 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1596 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1597 | 'StringFromCLSID', |
| 1598 | ('StringFromCLSID introduces an unnecessary dependency on ole32.dll.', |
| 1599 | 'Use base::win::WStringFromGUID instead.'), |
| 1600 | True, |
| 1601 | (r'/base/win/win_util_unittest.cc',), |
Cliff Smolinsky | 8195164 | 2019-04-30 21:39:51 | [diff] [blame] | 1602 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1603 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1604 | 'kCFAllocatorNull', |
| 1605 | ( |
| 1606 | 'The use of kCFAllocatorNull with the NoCopy creation of ', |
| 1607 | 'CoreFoundation types is prohibited.', |
| 1608 | ), |
| 1609 | True, |
| 1610 | (), |
Avi Drissman | 7382afa0 | 2019-04-29 23:27:13 | [diff] [blame] | 1611 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1612 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1613 | 'mojo::ConvertTo', |
| 1614 | ('mojo::ConvertTo and TypeConverter are deprecated. Please consider', |
| 1615 | 'StructTraits / UnionTraits / EnumTraits / ArrayTraits / MapTraits /', |
| 1616 | 'StringTraits if you would like to convert between custom types and', |
| 1617 | 'the wire format of mojom types.'), |
| 1618 | False, |
| 1619 | ( |
| 1620 | r'^fuchsia_web/webengine/browser/url_request_rewrite_rules_manager\.cc$', |
| 1621 | r'^fuchsia_web/webengine/url_request_rewrite_type_converters\.cc$', |
| 1622 | r'^third_party/blink/.*\.(cc|h)$', |
| 1623 | r'^content/renderer/.*\.(cc|h)$', |
| 1624 | ), |
Oksana Zhuravlova | fd24777 | 2019-05-16 16:57:29 | [diff] [blame] | 1625 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1626 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1627 | 'GetInterfaceProvider', |
| 1628 | ('InterfaceProvider is deprecated.', |
| 1629 | 'Please use ExecutionContext::GetBrowserInterfaceBroker and overrides', |
| 1630 | 'or Platform::GetBrowserInterfaceBroker.'), |
| 1631 | False, |
| 1632 | (), |
Oksana Zhuravlova | c8222d2 | 2019-12-19 19:21:16 | [diff] [blame] | 1633 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1634 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1635 | 'CComPtr', |
| 1636 | ('New code should use Microsoft::WRL::ComPtr from wrl/client.h as a ', |
| 1637 | 'replacement for CComPtr from ATL. See http://crbug.com/5027 for more ', |
| 1638 | 'details.'), |
| 1639 | False, |
| 1640 | (), |
Robert Liao | 1d78df5 | 2019-11-11 20:02:01 | [diff] [blame] | 1641 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1642 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1643 | r'/\b(IFACE|STD)METHOD_?\(', |
| 1644 | ('IFACEMETHOD() and STDMETHOD() make code harder to format and read.', |
| 1645 | 'Instead, always use IFACEMETHODIMP in the declaration.'), |
| 1646 | False, |
| 1647 | [_THIRD_PARTY_EXCEPT_BLINK],# Not an error in third_party folders. |
Xiaohan Wang | 72bd2ba | 2020-02-18 21:38:20 | [diff] [blame] | 1648 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1649 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1650 | 'RemoveAllChildViewsWithoutDeleting', |
| 1651 | ('RemoveAllChildViewsWithoutDeleting is deprecated.', |
| 1652 | 'This method is deemed dangerous as, unless raw pointers are re-added,', |
| 1653 | 'calls to this method introduce memory leaks.'), |
| 1654 | False, |
| 1655 | (), |
Peter Boström | 7ff4152 | 2021-07-29 03:43:27 | [diff] [blame] | 1656 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1657 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1658 | r'/\bTRACE_EVENT_ASYNC_', |
| 1659 | ( |
| 1660 | 'Please use TRACE_EVENT_NESTABLE_ASYNC_.. macros instead', |
| 1661 | 'of TRACE_EVENT_ASYNC_.. (crbug.com/1038710).', |
| 1662 | ), |
| 1663 | False, |
| 1664 | ( |
| 1665 | r'^base/trace_event/.*', |
| 1666 | r'^base/tracing/.*', |
| 1667 | ), |
Eric Seckler | be6f48d | 2020-05-06 18:09:12 | [diff] [blame] | 1668 | ), |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 1669 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1670 | 'RoInitialize', |
| 1671 | ('Improper use of [base::win]::RoInitialize() has been implicated in a ', |
| 1672 | 'few COM initialization leaks. Use base::win::ScopedWinrtInitializer ', |
| 1673 | 'instead. See http://crbug.com/1197722 for more information.'), |
| 1674 | True, |
| 1675 | ( |
| 1676 | r'^base/win/scoped_winrt_initializer\.cc$', |
| 1677 | r'^third_party/abseil-cpp/absl/.*', |
| 1678 | ), |
Robert Liao | 22f66a5 | 2021-04-10 00:57:52 | [diff] [blame] | 1679 | ), |
Patrick Monette | c343bb98 | 2022-06-01 17:18:45 | [diff] [blame] | 1680 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1681 | r'base::Watchdog', |
| 1682 | ( |
| 1683 | 'base::Watchdog is deprecated because it creates its own thread.', |
| 1684 | 'Instead, manually start a timer on a SequencedTaskRunner.', |
| 1685 | ), |
| 1686 | False, |
| 1687 | (), |
Patrick Monette | c343bb98 | 2022-06-01 17:18:45 | [diff] [blame] | 1688 | ), |
Andrew Rayskiy | 04a51ce | 2022-06-07 11:47:09 | [diff] [blame] | 1689 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1690 | 'base::Passed', |
| 1691 | ('Do not use base::Passed. It is a legacy helper for capturing ', |
| 1692 | 'move-only types with base::BindRepeating, but invoking the ', |
| 1693 | 'resulting RepeatingCallback moves the captured value out of ', |
| 1694 | 'the callback storage, and subsequent invocations may pass the ', |
| 1695 | 'value in a valid but undefined state. Prefer base::BindOnce().', |
| 1696 | 'See http://crbug.com/1326449 for context.'), |
| 1697 | False, |
| 1698 | ( |
| 1699 | # False positive, but it is also fine to let bind internals reference |
| 1700 | # base::Passed. |
| 1701 | r'^base[\\/]functional[\\/]bind\.h', |
| 1702 | r'^base[\\/]functional[\\/]bind_internal\.h', |
| 1703 | ), |
Andrew Rayskiy | 04a51ce | 2022-06-07 11:47:09 | [diff] [blame] | 1704 | ), |
Daniel Cheng | 2248b33 | 2022-07-27 06:16:59 | [diff] [blame] | 1705 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1706 | r'base::Feature k', |
| 1707 | ('Please use BASE_DECLARE_FEATURE() or BASE_FEATURE() instead of ', |
| 1708 | 'directly declaring/defining features.'), |
| 1709 | True, |
| 1710 | [ |
| 1711 | # Implements BASE_DECLARE_FEATURE(). |
| 1712 | r'^base/feature_list\.h', |
| 1713 | ], |
Daniel Cheng | ba3bc2e | 2022-10-03 02:45:43 | [diff] [blame] | 1714 | ), |
Robert Ogden | 92101dcb | 2022-10-19 23:49:36 | [diff] [blame] | 1715 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1716 | r'/\bchartorune\b', |
| 1717 | ('chartorune is not memory-safe, unless you can guarantee the input ', |
| 1718 | 'string is always null-terminated. Otherwise, please use charntorune ', |
| 1719 | 'from libphonenumber instead.'), |
| 1720 | True, |
| 1721 | [ |
| 1722 | _THIRD_PARTY_EXCEPT_BLINK, |
| 1723 | # Exceptions to this rule should have a fuzzer. |
| 1724 | ], |
Robert Ogden | 92101dcb | 2022-10-19 23:49:36 | [diff] [blame] | 1725 | ), |
Arthur Sonzogni | 1da65fa | 2023-03-27 16:01:52 | [diff] [blame] | 1726 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1727 | r'/\b#include "base/atomicops\.h"\b', |
| 1728 | ('Do not use base::subtle atomics, but std::atomic, which are simpler ' |
| 1729 | 'to use, have better understood, clearer and richer semantics, and are ' |
| 1730 | 'harder to mis-use. See details in base/atomicops.h.',), |
| 1731 | False, |
| 1732 | [_THIRD_PARTY_EXCEPT_BLINK],# Not an error in third_party folders. |
Benoit Lize | 79cf059 | 2023-01-27 10:01:57 | [diff] [blame] | 1733 | ), |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1734 | BanRule(r'CrossThreadPersistent<',( |
Arthur Sonzogni | 60348572e | 2023-04-07 10:22:52 | [diff] [blame] | 1735 | 'Do not use blink::CrossThreadPersistent, but ' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1736 | 'blink::CrossThreadHandle. It is harder to mis-use.','More info: ' |
Arthur Sonzogni | 60348572e | 2023-04-07 10:22:52 | [diff] [blame] | 1737 | 'https://docs.google.com/document/d/1GIT0ysdQ84sGhIo1r9EscF_fFt93lmNVM_q4vvHj2FQ/edit#heading=h.3e4d6y61tgs', |
| 1738 | 'Please contact platform-architecture-dev@ before adding new instances.' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1739 | ),False,[]), |
| 1740 | BanRule(r'CrossThreadWeakPersistent<',( |
Arthur Sonzogni | 60348572e | 2023-04-07 10:22:52 | [diff] [blame] | 1741 | 'Do not use blink::CrossThreadWeakPersistent, but ' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1742 | 'blink::CrossThreadWeakHandle. It is harder to mis-use.','More info: ' |
Arthur Sonzogni | 60348572e | 2023-04-07 10:22:52 | [diff] [blame] | 1743 | 'https://docs.google.com/document/d/1GIT0ysdQ84sGhIo1r9EscF_fFt93lmNVM_q4vvHj2FQ/edit#heading=h.3e4d6y61tgs', |
| 1744 | 'Please contact platform-architecture-dev@ before adding new instances.' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1745 | ),False,[]), |
| 1746 | BanRule(r'objc/objc.h',( |
Avi Drissman | 491617c | 2023-04-13 17:33:15 | [diff] [blame] | 1747 | 'Do not include <objc/objc.h>. It defines away ARC lifetime ' |
| 1748 | 'annotations, and is thus dangerous.', |
| 1749 | 'Please use the pimpl pattern; search for `ObjCStorage` for examples.', |
| 1750 | 'For further reading on how to safely mix C++ and Obj-C, see', |
| 1751 | '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] | 1752 | ),True,[]), |
| 1753 | BanRule( |
| 1754 | r'/#include <filesystem>', |
| 1755 | ('libc++ <filesystem> is banned per the Google C++ styleguide.',), |
| 1756 | True, |
| 1757 | # This fuzzing framework is a standalone open source project and |
| 1758 | # cannot rely on Chromium base. |
| 1759 | (r'third_party/centipede'), |
Avi Drissman | 491617c | 2023-04-13 17:33:15 | [diff] [blame] | 1760 | ), |
Grace Park | 8d59b54b | 2023-04-26 17:53:35 | [diff] [blame] | 1761 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1762 | r'TopDocument()', |
| 1763 | ('TopDocument() does not work correctly with out-of-process iframes. ' |
| 1764 | 'Please do not introduce new uses.',), |
| 1765 | True, |
| 1766 | ( |
| 1767 | # TODO(crbug.com/617677): Remove all remaining uses. |
| 1768 | r'^third_party/blink/renderer/core/dom/document\.cc', |
| 1769 | r'^third_party/blink/renderer/core/dom/document\.h', |
| 1770 | r'^third_party/blink/renderer/core/dom/element\.cc', |
| 1771 | r'^third_party/blink/renderer/core/exported/web_disallow_transition_scope_test\.cc', |
| 1772 | r'^third_party/blink/renderer/core/exported/web_document_test\.cc', |
| 1773 | r'^third_party/blink/renderer/core/html/html_anchor_element\.cc', |
| 1774 | r'^third_party/blink/renderer/core/html/html_dialog_element\.cc', |
| 1775 | r'^third_party/blink/renderer/core/html/html_element\.cc', |
| 1776 | r'^third_party/blink/renderer/core/html/html_frame_owner_element\.cc', |
| 1777 | r'^third_party/blink/renderer/core/html/media/video_wake_lock\.cc', |
| 1778 | r'^third_party/blink/renderer/core/loader/anchor_element_interaction_tracker\.cc', |
| 1779 | r'^third_party/blink/renderer/core/page/scrolling/root_scroller_controller\.cc', |
| 1780 | r'^third_party/blink/renderer/core/page/scrolling/top_document_root_scroller_controller\.cc', |
| 1781 | r'^third_party/blink/renderer/core/page/scrolling/top_document_root_scroller_controller\.h', |
| 1782 | r'^third_party/blink/renderer/core/script/classic_pending_script\.cc', |
| 1783 | r'^third_party/blink/renderer/core/script/script_loader\.cc', |
| 1784 | ), |
Grace Park | 8d59b54b | 2023-04-26 17:53:35 | [diff] [blame] | 1785 | ), |
Daniel Cheng | 72153e0 | 2023-05-18 21:18:14 | [diff] [blame] | 1786 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1787 | pattern=r'base::raw_ptr<', |
| 1788 | explanation=('Do not use base::raw_ptr, use raw_ptr.',), |
| 1789 | treat_as_error=True, |
| 1790 | excluded_paths=( |
| 1791 | '^base/', |
| 1792 | '^tools/', |
| 1793 | ), |
Daniel Cheng | 72153e0 | 2023-05-18 21:18:14 | [diff] [blame] | 1794 | ), |
Arthur Sonzogni | f0eea30 | 2023-08-18 19:20:31 | [diff] [blame] | 1795 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1796 | pattern=r'base:raw_ref<', |
| 1797 | explanation=('Do not use base::raw_ref, use raw_ref.',), |
| 1798 | treat_as_error=True, |
| 1799 | excluded_paths=( |
| 1800 | '^base/', |
| 1801 | '^tools/', |
| 1802 | ), |
Arthur Sonzogni | f0eea30 | 2023-08-18 19:20:31 | [diff] [blame] | 1803 | ), |
| 1804 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1805 | pattern=r'/raw_ptr<[^;}]*\w{};', |
| 1806 | explanation=( |
| 1807 | 'Do not use {} for raw_ptr initialization, use = nullptr instead.', |
| 1808 | ), |
| 1809 | treat_as_error=True, |
| 1810 | excluded_paths=( |
| 1811 | '^base/', |
| 1812 | '^tools/', |
| 1813 | ), |
Arthur Sonzogni | f0eea30 | 2023-08-18 19:20:31 | [diff] [blame] | 1814 | ), |
Anton Maliev | 6675181 | 2023-08-24 16:28:13 | [diff] [blame] | 1815 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1816 | pattern=r'/#include "base/allocator/.*/raw_' |
| 1817 | r'(ptr|ptr_cast|ptr_exclusion|ref).h"', |
| 1818 | explanation=( |
| 1819 | 'Please include the corresponding facade headers:', |
| 1820 | '- #include "base/memory/raw_ptr.h"', |
| 1821 | '- #include "base/memory/raw_ptr_cast.h"', |
| 1822 | '- #include "base/memory/raw_ptr_exclusion.h"', |
| 1823 | '- #include "base/memory/raw_ref.h"', |
| 1824 | ), |
| 1825 | treat_as_error=True, |
| 1826 | excluded_paths=( |
| 1827 | '^base/', |
| 1828 | '^tools/', |
| 1829 | ), |
Tom Sepez | 41eb158d | 2023-09-12 16:16:22 | [diff] [blame] | 1830 | ), |
| 1831 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1832 | pattern=r'ContentSettingsType::COOKIES', |
| 1833 | explanation= |
| 1834 | ('Do not use ContentSettingsType::COOKIES to check whether cookies are ' |
| 1835 | 'supported in the provided context. Instead rely on the ' |
| 1836 | 'content_settings::CookieSettings API. If you are using ' |
| 1837 | 'ContentSettingsType::COOKIES to check the user preference setting ' |
| 1838 | 'specifically, disregard this warning.',), |
| 1839 | treat_as_error=False, |
| 1840 | excluded_paths=( |
| 1841 | '^chrome/browser/ui/content_settings/', |
| 1842 | '^components/content_settings/', |
| 1843 | '^services/network/cookie_settings.cc', |
| 1844 | '.*test.cc', |
| 1845 | ), |
Arthur Sonzogni | 48c6aea2 | 2023-09-04 22:25:20 | [diff] [blame] | 1846 | ), |
| 1847 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1848 | pattern=r'ContentSettingsType::TRACKING_PROTECTION', |
| 1849 | explanation= |
| 1850 | ('Do not directly use ContentSettingsType::TRACKING_PROTECTION to check ' |
| 1851 | 'for tracking protection exceptions. Instead rely on the ' |
| 1852 | 'privacy_sandbox::TrackingProtectionSettings API.',), |
| 1853 | treat_as_error=False, |
| 1854 | excluded_paths=( |
| 1855 | '^chrome/browser/ui/content_settings/', |
| 1856 | '^components/content_settings/', |
| 1857 | '^components/privacy_sandbox/tracking_protection_settings.cc', |
| 1858 | '.*test.cc', |
| 1859 | ), |
Anton Maliev | 6675181 | 2023-08-24 16:28:13 | [diff] [blame] | 1860 | ), |
Tom Anderson | cd52207 | 2023-10-03 00:52:35 | [diff] [blame] | 1861 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1862 | pattern=r'/\bg_signal_connect', |
| 1863 | explanation=('Use ScopedGSignal instead of g_signal_connect*()',), |
| 1864 | treat_as_error=True, |
| 1865 | excluded_paths=('^ui/base/glib/scoped_gsignal.h',), |
Michelle Abreo | 6b743782 | 2024-04-26 17:29:04 | [diff] [blame] | 1866 | ), |
| 1867 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1868 | pattern=r'features::kIsolatedWebApps', |
| 1869 | explanation=( |
| 1870 | 'Do not use `features::kIsolatedWebApps` directly to guard Isolated ', |
| 1871 | 'Web App code. ', |
Andrew Rayskiy | 7ae2b5d | 2025-02-28 16:59:38 | [diff] [blame] | 1872 | 'Use `content::AreIsolatedWebAppsEnabled()` in the browser process ' |
| 1873 | 'or check the `kEnableIsolatedWebAppsInRenderer` command line flag ' |
| 1874 | 'in the renderer process.', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1875 | ), |
| 1876 | treat_as_error=True, |
Andrew Rayskiy | 7ae2b5d | 2025-02-28 16:59:38 | [diff] [blame] | 1877 | excluded_paths=_TEST_CODE_EXCLUDED_PATHS+( |
| 1878 | '^chrome/browser/about_flags.cc', |
| 1879 | '^chrome/browser/component_updater/iwa_key_distribution_component_installer.cc', |
| 1880 | '^chrome/browser/web_applications/isolated_web_apps/chrome_content_browser_client_isolated_web_apps_part.cc', |
| 1881 | '^chrome/browser/ui/startup/bad_flags_prompt.cc', |
| 1882 | '^content/shell/browser/shell_content_browser_client.cc', |
| 1883 | )), |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1884 | BanRule( |
| 1885 | pattern=r'features::kIsolatedWebAppDevMode', |
| 1886 | explanation=( |
| 1887 | 'Do not use `features::kIsolatedWebAppDevMode` directly to guard code ', |
| 1888 | 'related to Isolated Web App Developer Mode. ', |
| 1889 | 'Use `web_app::IsIwaDevModeEnabled()` instead.', |
| 1890 | ), |
| 1891 | treat_as_error=True, |
| 1892 | excluded_paths=_TEST_CODE_EXCLUDED_PATHS+( |
| 1893 | '^chrome/browser/about_flags.cc', |
| 1894 | '^chrome/browser/web_applications/isolated_web_apps/isolated_web_app_features.cc', |
| 1895 | '^chrome/browser/ui/startup/bad_flags_prompt.cc', |
| 1896 | )), |
| 1897 | BanRule( |
| 1898 | pattern=r'features::kIsolatedWebAppUnmanagedInstall', |
| 1899 | explanation=( |
| 1900 | 'Do not use `features::kIsolatedWebAppUnmanagedInstall` directly to ', |
| 1901 | 'guard code related to unmanaged install flow for Isolated Web Apps. ', |
| 1902 | 'Use `web_app::IsIwaUnmanagedInstallEnabled()` instead.', |
| 1903 | ), |
| 1904 | treat_as_error=True, |
| 1905 | excluded_paths=_TEST_CODE_EXCLUDED_PATHS+( |
| 1906 | '^chrome/browser/about_flags.cc', |
| 1907 | '^chrome/browser/web_applications/isolated_web_apps/isolated_web_app_features.cc', |
| 1908 | )), |
| 1909 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1910 | pattern='/(CUIAutomation|AccessibleObjectFromWindow)', |
| 1911 | explanation= |
| 1912 | ('Direct usage of UIAutomation or IAccessible2 in client code is ' |
| 1913 | 'discouraged in Chromium, as it is not an assistive technology and ' |
| 1914 | 'should not rely on accessibility APIs directly. These APIs can ' |
| 1915 | 'introduce significant performance overhead. However, if you believe ' |
| 1916 | 'your use case warrants an exception, please discuss it with an ' |
| 1917 | 'accessibility owner before proceeding. For more information on the ' |
| 1918 | 'performance implications, see https://docs.google.com/document/d/1jN4itpCe_bDXF0BhFaYwv4xVLsCWkL9eULdzjmLzkuk/edit#heading=h.pwth3nbwdub0.', |
| 1919 | ), |
| 1920 | treat_as_error=False, |
Andrew Rayskiy | cdd45e73 | 2024-03-20 14:32:39 | [diff] [blame] | 1921 | ), |
| 1922 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1923 | pattern=r'/WIDGET_OWNS_NATIVE_WIDGET|' |
| 1924 | r'NATIVE_WIDGET_OWNS_WIDGET', |
| 1925 | explanation= |
| 1926 | ('WIDGET_OWNS_NATIVE_WIDGET and NATIVE_WIDGET_OWNS_WIDGET are in the ' |
| 1927 | 'process of being deprecated. Consider using the new ' |
| 1928 | 'CLIENT_OWNS_WIDGET ownership model. Eventually, this will be the only ' |
| 1929 | 'available ownership model available and the associated enumeration' |
| 1930 | 'will be removed.',), |
| 1931 | treat_as_error=False, |
Andrew Rayskiy | cdd45e73 | 2024-03-20 14:32:39 | [diff] [blame] | 1932 | ), |
| 1933 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1934 | pattern='ProfileManager::GetLastUsedProfile', |
| 1935 | explanation= |
| 1936 | ('Most code should already be scoped to a Profile. Pass in a Profile* ' |
Vikram Pasupathy | b052c89 | 2025-07-08 18:10:00 | [diff] [blame] | 1937 | 'or retrieve from an existing entity with a reference to the Profile ' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1938 | '(e.g. WebContents).',), |
| 1939 | treat_as_error=False, |
Arthur Sonzogni | 5cbd3e3 | 2024-02-08 17:51:32 | [diff] [blame] | 1940 | ), |
Helmut Januschka | b3f71ab5 | 2024-03-12 02:48:05 | [diff] [blame] | 1941 | BanRule( |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1942 | pattern=(r'/FindBrowserWithUiElementContext|' |
| 1943 | r'FindBrowserWithTab|' |
| 1944 | r'FindBrowserWithGroup|' |
| 1945 | r'FindTabbedBrowser|' |
| 1946 | r'FindAnyBrowser|' |
| 1947 | r'FindBrowserWithProfile|' |
Erik Chen | 5f02eb4c | 2024-08-23 06:30:44 | [diff] [blame] | 1948 | r'FindLastActive|' |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1949 | r'FindBrowserWithActiveWindow'), |
| 1950 | explanation= |
| 1951 | ('Most code should already be scoped to a Browser. Pass in a Browser* ' |
Vikram Pasupathy | b052c89 | 2025-07-08 18:10:00 | [diff] [blame] | 1952 | 'or retrieve from an existing entity with a reference to the Browser.', |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 1953 | ), |
| 1954 | treat_as_error=False, |
Helmut Januschka | b3f71ab5 | 2024-03-12 02:48:05 | [diff] [blame] | 1955 | ), |
| 1956 | BanRule( |
Tom Sepez | d3272cd | 2025-02-21 19:11:31 | [diff] [blame] | 1957 | pattern=r'subspan(0u,', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1958 | explanation=( |
| 1959 | 'Prefer first(n) over subspan(0u, n) as it is shorter, and the ' |
| 1960 | 'compiler may have to emit a branch for the n == dynamic_extent ' |
| 1961 | 'case of subspan().',), |
Tom Sepez | d3272cd | 2025-02-21 19:11:31 | [diff] [blame] | 1962 | treat_as_error=False, |
| 1963 | ), |
| 1964 | BanRule( |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 1965 | pattern=r'UNSAFE_TODO(', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1966 | explanation=( |
| 1967 | 'Do not use UNSAFE_TODO() to write new unsafe code. Use only when ' |
| 1968 | 'removing a pre-existing file-wide allow_unsafe_buffers pragma, or ' |
| 1969 | 'when incrementally converting code off of unsafe interfaces',), |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 1970 | treat_as_error=False, |
| 1971 | ), |
| 1972 | BanRule( |
Claudio DeSouza | ad1d1f25 | 2025-04-22 23:38:58 | [diff] [blame] | 1973 | pattern='#pragma allow_unsafe_buffers', |
| 1974 | explanation= |
| 1975 | ('Do not use allow_unsafe_buffers to write new unsafe code. Use only ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1976 | 'when enabling unsafe buffers checks under a new uncovered path.',), |
Claudio DeSouza | ad1d1f25 | 2025-04-22 23:38:58 | [diff] [blame] | 1977 | treat_as_error=False, |
| 1978 | ), |
| 1979 | BanRule( |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 1980 | pattern=r'UNSAFE_BUFFERS(', |
| 1981 | explanation= |
Tom Sepez | a90f92b | 2024-08-15 16:01:35 | [diff] [blame] | 1982 | ('Try to avoid using UNSAFE_BUFFERS() if at all possible. Otherwise, ' |
| 1983 | 'be sure to justify in a // SAFETY comment why other options are not ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1984 | 'available, and why the code is safe.',), |
Tom Sepez | ea67b6e | 2024-08-08 18:17:27 | [diff] [blame] | 1985 | treat_as_error=False, |
| 1986 | ), |
Erik Chen | d086ae0 | 2024-08-20 22:53:33 | [diff] [blame] | 1987 | BanRule( |
| 1988 | pattern='BrowserWithTestWindowTest', |
| 1989 | explanation= |
| 1990 | ('Do not use BrowserWithTestWindowTest. By instantiating an instance ' |
| 1991 | 'of class Browser, the test is no longer a unit test but is instead a ' |
| 1992 | 'browser test. The class BrowserWithTestWindowTest forces production ' |
| 1993 | 'logic to take on test-only conditionals, which is an anti-pattern. ' |
| 1994 | 'Features should be performing dependency injection rather than ' |
| 1995 | 'directly using class Browser. See ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 1996 | 'docs/chrome_browser_design_principles.md for more details.',), |
Erik Chen | d086ae0 | 2024-08-20 22:53:33 | [diff] [blame] | 1997 | treat_as_error=False, |
| 1998 | ), |
Erik Chen | 8cf3a65 | 2024-08-23 17:13:30 | [diff] [blame] | 1999 | BanRule( |
Erik Chen | 959cdd7 | 2024-08-29 02:11:21 | [diff] [blame] | 2000 | pattern='TestWithBrowserView', |
| 2001 | explanation= |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2002 | ('Do not use TestWithBrowserView. See ' |
| 2003 | 'docs/chrome_browser_design_principles.md for details. If you want ' |
| 2004 | 'to write a test that has both a Browser and a BrowserView, create ' |
| 2005 | 'a browser_test. If you want to write a unit_test, your code must ' |
| 2006 | 'not reference Browser*.',), |
Erik Chen | 959cdd7 | 2024-08-29 02:11:21 | [diff] [blame] | 2007 | treat_as_error=False, |
| 2008 | ), |
| 2009 | BanRule( |
Erik Chen | e89ebe3 | 2025-02-22 02:46:49 | [diff] [blame] | 2010 | pattern='CreateBrowserWithTestWindow', |
| 2011 | explanation= |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2012 | ('Do not use CreateBrowserWithTestWindow. See ' |
| 2013 | 'docs/chrome_browser_design_principles.md for details. If you want ' |
| 2014 | 'to write a test that has a Browser, create a browser_test. If you ' |
| 2015 | 'want to write a unit_test, your code must not reference Browser*.', |
Erik Chen | e89ebe3 | 2025-02-22 02:46:49 | [diff] [blame] | 2016 | ), |
| 2017 | treat_as_error=False, |
| 2018 | ), |
| 2019 | BanRule( |
Erik Chen | f12a0664 | 2025-03-13 23:30:34 | [diff] [blame] | 2020 | pattern='CreateBrowserWithTestWindowForParams', |
| 2021 | explanation= |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2022 | ('Do not use CreateBrowserWithTestWindowForParams. See ' |
| 2023 | 'docs/chrome_browser_design_principles.md for details. If you want ' |
| 2024 | 'to write a test that has a Browser, create a browser_test and use ' |
| 2025 | 'Browser::Browser. If you want to write a unit_test, your code must ' |
| 2026 | 'not reference Browser*.',), |
Erik Chen | f12a0664 | 2025-03-13 23:30:34 | [diff] [blame] | 2027 | treat_as_error=False, |
| 2028 | ), |
| 2029 | BanRule( |
Mikita Kuchyn | 383c9bab | 2025-05-23 15:15:23 | [diff] [blame] | 2030 | pattern='TestBrowserWindow', |
| 2031 | explanation=( |
| 2032 | 'Do not use TestBrowserWindow. See ' |
| 2033 | 'docs/chrome_browser_design_principles.md for details. If you want ' |
| 2034 | 'to write a test that has a Browser, create a browser_test. If you' |
| 2035 | 'want to write a unit_test, your code should not reference Browser' |
| 2036 | 'or BrowserWindow.', |
| 2037 | ), |
| 2038 | treat_as_error=False, |
| 2039 | ), |
| 2040 | BanRule( |
Erik Chen | 8cf3a65 | 2024-08-23 17:13:30 | [diff] [blame] | 2041 | pattern='RunUntilIdle', |
| 2042 | explanation= |
| 2043 | ('Do not RunUntilIdle. If possible, explicitly quit the run loop using ' |
| 2044 | 'run_loop.Quit() or run_loop.QuitClosure() if completion can be ' |
| 2045 | 'observed using a lambda or callback. Otherwise, wait for the ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2046 | 'condition to be true via base::test::RunUntil().',), |
Erik Chen | 8cf3a65 | 2024-08-23 17:13:30 | [diff] [blame] | 2047 | treat_as_error=False, |
| 2048 | ), |
Daniel Cheng | ddde13a | 2024-09-05 21:39:28 | [diff] [blame] | 2049 | BanRule( |
| 2050 | pattern=r'/\bstd::(literals|string_literals|string_view_literals)\b', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2051 | explanation=( |
Daniel Cheng | ddde13a | 2024-09-05 21:39:28 | [diff] [blame] | 2052 | 'User-defined literals are banned by the Google C++ style guide. ' |
| 2053 | 'Exceptions are provided in Chrome for string and string_view ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2054 | 'literals that embed \\0.',), |
Daniel Cheng | ddde13a | 2024-09-05 21:39:28 | [diff] [blame] | 2055 | treat_as_error=True, |
| 2056 | excluded_paths=( |
| 2057 | # Various tests or test helpers that embed NUL in strings or |
| 2058 | # string_views. |
Daniel Cheng | ddde13a | 2024-09-05 21:39:28 | [diff] [blame] | 2059 | r'^base/strings/string_util_unittest\.cc', |
| 2060 | r'^base/strings/utf_string_conversions_unittest\.cc', |
Hidehiko Abe | 5160181 | 2025-01-12 16:17:35 | [diff] [blame] | 2061 | r'^chromeos/ash/experiences/arc/session/serial_number_util_unittest\.cc', |
Daniel Cheng | ddde13a | 2024-09-05 21:39:28 | [diff] [blame] | 2062 | r'^components/history/core/browser/visit_annotations_database\.cc', |
| 2063 | r'^components/history/core/browser/visit_annotations_database_unittest\.cc', |
| 2064 | r'^components/os_crypt/sync/os_crypt_unittest\.cc', |
| 2065 | r'^components/password_manager/core/browser/credentials_cleaner_unittest\.cc', |
| 2066 | r'^content/browser/file_system_access/file_system_access_file_writer_impl_unittest\.cc', |
| 2067 | r'^net/cookies/parsed_cookie_unittest\.cc', |
| 2068 | r'^third_party/blink/renderer/modules/webcodecs/test_helpers\.cc', |
| 2069 | r'^third_party/blink/renderer/modules/websockets/websocket_channel_impl_test\.cc', |
| 2070 | ), |
Erik Chen | ba8b0cd3 | 2024-10-01 08:36:36 | [diff] [blame] | 2071 | ), |
| 2072 | BanRule( |
| 2073 | pattern='BUILDFLAG(GOOGLE_CHROME_BRANDING)', |
| 2074 | explanation= |
| 2075 | ('Code gated by GOOGLE_CHROME_BRANDING is effectively untested. This ' |
| 2076 | 'is typically wrong. Valid use cases are glue for private modules ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2077 | 'shipped alongside Chrome, and installation-related logic.',), |
Erik Chen | ba8b0cd3 | 2024-10-01 08:36:36 | [diff] [blame] | 2078 | treat_as_error=False, |
| 2079 | ), |
| 2080 | BanRule( |
| 2081 | pattern='defined(OFFICIAL_BUILD)', |
| 2082 | explanation= |
| 2083 | ('Code gated by OFFICIAL_BUILD is effectively untested. This ' |
| 2084 | 'is typically wrong. One valid use case is low-level code that ' |
| 2085 | 'handles subtleties related to high-levels of optimizations that come ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2086 | 'with OFFICIAL_BUILD.',), |
Erik Chen | ba8b0cd3 | 2024-10-01 08:36:36 | [diff] [blame] | 2087 | treat_as_error=False, |
| 2088 | ), |
Erik Chen | 95b9c78 | 2024-11-08 03:26:27 | [diff] [blame] | 2089 | BanRule( |
| 2090 | pattern='WebContentsDestroyed', |
| 2091 | explanation= |
| 2092 | ('Do not use this method. It is invoked half-way through the ' |
| 2093 | 'destructor of WebContentsImpl and using it often results in crashes ' |
| 2094 | 'or surprising behavior. Conceptually, this is only necessary by ' |
| 2095 | 'objects that depend on, but outlive the WebContents. These objects ' |
| 2096 | 'should instead coordinate with the owner of the WebContents which is ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2097 | 'responsible for destroying the WebContents.',), |
Erik Chen | 95b9c78 | 2024-11-08 03:26:27 | [diff] [blame] | 2098 | treat_as_error=False, |
| 2099 | ), |
Maksim Sisov | c98fdfa | 2024-11-16 20:12:27 | [diff] [blame] | 2100 | BanRule( |
Georg Neis | a7f94e6 | 2025-02-28 07:01:48 | [diff] [blame] | 2101 | pattern='IS_CHROMEOS_ASH', |
Maksim Sisov | c98fdfa | 2024-11-16 20:12:27 | [diff] [blame] | 2102 | explanation= |
Georg Neis | a7f94e6 | 2025-02-28 07:01:48 | [diff] [blame] | 2103 | ('IS_CHROMEOS_ASH is deprecated. Please use the equivalent IS_CHROMEOS ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2104 | 'instead (Lacros is gone).',), |
Maksim Sisov | c98fdfa | 2024-11-16 20:12:27 | [diff] [blame] | 2105 | treat_as_error=False, |
| 2106 | ), |
Erik Chen | 1396bbe | 2025-01-27 23:39:36 | [diff] [blame] | 2107 | BanRule( |
| 2108 | pattern=(r'namespace {'), |
| 2109 | explanation= |
| 2110 | ('Anonymous namespaces are disallowed in C++ header files. See ' |
| 2111 | 'https://google.github.io/styleguide/cppguide.html#Internal_Linkage ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2112 | ' for details.',), |
Erik Chen | 1396bbe | 2025-01-27 23:39:36 | [diff] [blame] | 2113 | treat_as_error=False, |
| 2114 | excluded_paths=[ |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2115 | _THIRD_PARTY_EXCEPT_BLINK,# Don't warn in third_party folders. |
| 2116 | r'^(?!.*\.h$).*$',# Exclude all files except those that end in .h |
Erik Chen | 1396bbe | 2025-01-27 23:39:36 | [diff] [blame] | 2117 | ], |
| 2118 | ), |
Keren Zhu | f06d757d | 2025-03-04 05:32:36 | [diff] [blame] | 2119 | BanRule( |
| 2120 | pattern=('AddChildViewRaw'), |
| 2121 | explanation=( |
| 2122 | 'Do not use AddChildViewRaw. It is prone to memory leaks and ' |
| 2123 | 'use-after-free bugs. Instead, use AddChildView(std::unique_ptr). ' |
| 2124 | 'See https://crbug.com/40485510 for more details.',), |
| 2125 | treat_as_error=False, |
| 2126 | ), |
Nate Fischer | d541ff8 | 2025-03-11 21:34:19 | [diff] [blame] | 2127 | BanRule( |
| 2128 | pattern=(r'IS_DESKTOP_ANDROID'), |
| 2129 | explanation=( |
Eric Lok | c26a4666 | 2025-05-02 01:04:03 | [diff] [blame] | 2130 | 'Do not add new uses of IS_DESKTOP_ANDROID build flag until you ' |
| 2131 | 'have the approval of tedchoc@ or twellington@. ' |
| 2132 | 'Background: it is highly important to reduce the divergence of ' |
| 2133 | 'features across platforms. ' |
| 2134 | 'Allowances may be granted to only the directories below: ' |
| 2135 | '[build/, chrome/, components/, extensions/, infra/, tools/] ', |
| 2136 | 'Note: in particular we need to avoid components shared with ' |
| 2137 | 'WebView.', |
| 2138 | ), |
Nate Fischer | d541ff8 | 2025-03-11 21:34:19 | [diff] [blame] | 2139 | treat_as_error=False, |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 2140 | surface_as_gerrit_lint=True, |
Nate Fischer | d541ff8 | 2025-03-11 21:34:19 | [diff] [blame] | 2141 | ), |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2142 | ) |
| 2143 | |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2144 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING=( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2145 | 'Used a predicate related to signin::ConsentLevel::kSync which will always ' |
| 2146 | 'return false in the future (crbug.com/40066949). Prefer using a predicate ' |
| 2147 | 'that also supports signin::ConsentLevel::kSignin when appropriate. It is ' |
| 2148 | 'safe to ignore this warning if you are just moving an existing call, or if ' |
| 2149 | 'you want special handling for users in the legacy state. In doubt, reach ' |
| 2150 | 'out to //components/sync/OWNERS.',) |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2151 | |
| 2152 | # C++ functions related to signin::ConsentLevel::kSync which are deprecated. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2153 | _DEPRECATED_SYNC_CONSENT_CPP_FUNCTIONS:Sequence[BanRule]=( |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2154 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2155 | 'HasSyncConsent', |
| 2156 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2157 | False, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2158 | ), |
| 2159 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2160 | 'CanSyncFeatureStart', |
| 2161 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2162 | False, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2163 | ), |
| 2164 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2165 | 'IsSyncFeatureEnabled', |
| 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 | 'IsSyncFeatureActive', |
| 2171 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2172 | False, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2173 | ), |
| 2174 | ) |
| 2175 | |
| 2176 | # Java functions related to signin::ConsentLevel::kSync which are deprecated. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2177 | _DEPRECATED_SYNC_CONSENT_JAVA_FUNCTIONS:Sequence[BanRule]=( |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2178 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2179 | 'hasSyncConsent', |
| 2180 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2181 | False, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2182 | ), |
| 2183 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2184 | 'canSyncFeatureStart', |
| 2185 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2186 | False, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2187 | ), |
| 2188 | BanRule( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2189 | 'isSyncFeatureEnabled', |
| 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 | 'isSyncFeatureActive', |
| 2195 | _DEPRECATED_SYNC_CONSENT_FUNCTION_WARNING, |
| 2196 | False, |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2197 | ), |
| 2198 | ) |
| 2199 | |
Justin Lulejian | 09fd0687 | 2025-04-01 22:03:28 | [diff] [blame] | 2200 | _BANNED_MOJOM_PATTERNS:Sequence[BanRule]=( |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 2201 | BanRule( |
| 2202 | 'handle<shared_buffer>', |
| 2203 | ( |
Justin Lulejian | 09fd0687 | 2025-04-01 22:03:28 | [diff] [blame] | 2204 | 'Please use one of the more specific shared memory types instead:', |
| 2205 | ' mojo_base.mojom.ReadOnlySharedMemoryRegion', |
| 2206 | ' mojo_base.mojom.WritableSharedMemoryRegion', |
| 2207 | ' mojo_base.mojom.UnsafeSharedMemoryRegion', |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 2208 | ), |
| 2209 | True, |
| 2210 | ), |
Justin Lulejian | 09fd0687 | 2025-04-01 22:03:28 | [diff] [blame] | 2211 | BanRule( |
| 2212 | 'string extension_id', |
| 2213 | ( |
| 2214 | 'Please use the extensions::mojom::ExtensionId struct when ' |
| 2215 | 'passing extensions::ExtensionIds as mojom messages in order to ', |
| 2216 | 'provide message validation.', |
| 2217 | ), |
| 2218 | True, |
| 2219 | # Only apply this to (mojom) files in a subdirectory of extensions. |
| 2220 | excluded_paths=(r'^((?!extensions/).)*$',), |
| 2221 | ), |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 2222 | ) |
| 2223 | |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 2224 | _IPC_ENUM_TRAITS_DEPRECATED=( |
| 2225 | '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] | 2226 | 'See http://www.chromium.org/Home/chromium-security/education/' |
| 2227 | 'security-tips-for-ipc') |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 2228 | |
Stephen Martinis | 97a39414 | 2018-06-07 23:06:05 | [diff] [blame] | 2229 | _LONG_PATH_ERROR=( |
| 2230 | 'Some files included in this CL have file names that are too long (> 200' |
| 2231 | ' characters). If committed, these files will cause issues on Windows. See' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2232 | ' https://crbug.com/612667 for more details.') |
Stephen Martinis | 97a39414 | 2018-06-07 23:06:05 | [diff] [blame] | 2233 | |
Shenghua Zhang | bfaa38b8 | 2017-11-16 21:58:02 | [diff] [blame] | 2234 | _JAVA_MULTIPLE_DEFINITION_EXCLUDED_PATHS=[ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2235 | r".*/BuildHooksAndroidImpl\.java", |
| 2236 | r".*/LicenseContentProvider\.java", |
| 2237 | r".*/PlatformServiceBridgeImpl.java", |
| 2238 | r".*chrome/android/feed/dummy/.*\.java", |
Shenghua Zhang | bfaa38b8 | 2017-11-16 21:58:02 | [diff] [blame] | 2239 | ] |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2240 | |
Mohamed Heikal | d048240a | 2019-11-12 16:57:37 | [diff] [blame] | 2241 | # List of image extensions that are used as resources in chromium. |
| 2242 | _IMAGE_EXTENSIONS=['.svg','.png','.webp'] |
| 2243 | |
Sean Kau | 46e29bc | 2017-08-28 16:31:16 | [diff] [blame] | 2244 | # These paths contain test data and other known invalid JSON files. |
Erik Staab | 2dd72b1 | 2020-04-16 15:03:40 | [diff] [blame] | 2245 | _KNOWN_TEST_DATA_AND_INVALID_JSON_FILE_PATTERNS=[ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2246 | r'test/data/', |
| 2247 | r'testing/buildbot/', |
| 2248 | r'^components/policy/resources/policy_templates\.json$', |
| 2249 | r'^third_party/protobuf/', |
Camillo Bruni | 1411a35 | 2023-05-24 12:39:03 | [diff] [blame] | 2250 | r'^third_party/blink/perf_tests/speedometer.*/resources/todomvc/learn\.json', |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2251 | r'^third_party/blink/renderer/devtools/protocol\.json$', |
| 2252 | r'^third_party/blink/web_tests/external/wpt/', |
| 2253 | r'^tools/perf/', |
| 2254 | r'^tools/traceline/svgui/startup-release.json', |
Daniel Cheng | 2d4c2d19 | 2022-07-01 01:38:31 | [diff] [blame] | 2255 | # vscode configuration files allow comments |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 2256 | r'^tools/vscode/', |
Sean Kau | 46e29bc | 2017-08-28 16:31:16 | [diff] [blame] | 2257 | ] |
| 2258 | |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2259 | # These are not checked on the public chromium-presubmit trybot. |
| 2260 | # 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] | 2261 | # checkouts. |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 2262 | _ANDROID_SPECIFIC_PYDEPS_FILES=[ |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2263 | 'chrome/android/features/create_stripped_java_factory.pydeps', |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2264 | ] |
| 2265 | |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2266 | _GENERIC_PYDEPS_FILES=[ |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2267 | 'android_webview/tools/run_cts.pydeps', |
Andrew Grieve | 4c4cede | 2020-11-20 22:09:36 | [diff] [blame] | 2268 | 'build/android/apk_operations.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2269 | 'build/android/devil_chromium.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2270 | 'build/android/gyp/aar.pydeps', |
| 2271 | 'build/android/gyp/aidl.pydeps', |
| 2272 | 'build/android/gyp/apkbuilder.pydeps', |
Andrew Grieve | a417ad30 | 2019-02-06 19:54:38 | [diff] [blame] | 2273 | 'build/android/gyp/assert_static_initializers.pydeps', |
Mohamed Heikal | 133e1f2 | 2023-04-18 20:04:37 | [diff] [blame] | 2274 | 'build/android/gyp/binary_baseline_profile.pydeps', |
Robbie McElrath | 360e54d | 2020-11-12 20:38:02 | [diff] [blame] | 2275 | 'build/android/gyp/bytecode_rewriter.pydeps', |
Mohamed Heikal | 6305bcc | 2021-03-15 15:34:22 | [diff] [blame] | 2276 | 'build/android/gyp/check_flag_expectations.pydeps', |
Andrew Grieve | acac424 | 2024-12-20 19:39:42 | [diff] [blame] | 2277 | 'build/android/gyp/check_for_missing_direct_deps.pydeps', |
Andrew Grieve | 8d083ea | 2019-12-13 06:49:11 | [diff] [blame] | 2278 | 'build/android/gyp/compile_java.pydeps', |
Peter Wen | eaa963f | 2023-01-20 19:40:30 | [diff] [blame] | 2279 | 'build/android/gyp/compile_kt.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2280 | 'build/android/gyp/compile_resources.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2281 | 'build/android/gyp/copy_ex.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2282 | 'build/android/gyp/create_apk_operations_script.pydeps', |
Andrew Grieve | 8d083ea | 2019-12-13 06:49:11 | [diff] [blame] | 2283 | 'build/android/gyp/create_app_bundle.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2284 | 'build/android/gyp/create_app_bundle_apks.pydeps', |
| 2285 | 'build/android/gyp/create_bundle_wrapper_script.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2286 | 'build/android/gyp/create_java_binary_script.pydeps', |
Mohamed Heikal | adbe4e48 | 2020-07-09 19:25:12 | [diff] [blame] | 2287 | 'build/android/gyp/create_r_java.pydeps', |
Mohamed Heikal | 8cd763a5 | 2021-02-01 23:32:09 | [diff] [blame] | 2288 | 'build/android/gyp/create_r_txt.pydeps', |
Andrew Grieve | b838d83 | 2019-02-11 16:55:22 | [diff] [blame] | 2289 | 'build/android/gyp/create_size_info_files.pydeps', |
Andrew Grieve | 2d972e5f | 2025-01-28 18:28:14 | [diff] [blame] | 2290 | 'build/android/gyp/create_stub_manifest.pydeps', |
Peter Wen | e6e017e | 2022-07-27 21:40:40 | [diff] [blame] | 2291 | 'build/android/gyp/create_test_apk_wrapper_script.pydeps', |
Andrew Grieve | 5a01ad3 | 2020-06-25 18:06:00 | [diff] [blame] | 2292 | 'build/android/gyp/create_ui_locale_resources.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2293 | 'build/android/gyp/dex.pydeps', |
| 2294 | 'build/android/gyp/dist_aar.pydeps', |
Andrew Grieve | 651ddb3 | 2025-01-23 03:27:34 | [diff] [blame] | 2295 | 'build/android/gyp/errorprone.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2296 | 'build/android/gyp/filter_zip.pydeps', |
Mohamed Heikal | 21e1994b | 2021-11-12 21:37:21 | [diff] [blame] | 2297 | 'build/android/gyp/flatc_java.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2298 | 'build/android/gyp/gcc_preprocess.pydeps', |
Christopher Grant | 99e0e2006 | 2018-11-21 21:22:36 | [diff] [blame] | 2299 | 'build/android/gyp/generate_linker_version_script.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2300 | 'build/android/gyp/ijar.pydeps', |
Yun Liu | eb4075ddf | 2019-05-13 19:47:58 | [diff] [blame] | 2301 | 'build/android/gyp/jacoco_instr.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2302 | 'build/android/gyp/java_cpp_enum.pydeps', |
Nate Fischer | ac07b262 | 2020-10-01 20:20:14 | [diff] [blame] | 2303 | 'build/android/gyp/java_cpp_features.pydeps', |
Ian Vollick | b99472e | 2019-03-07 21:35:26 | [diff] [blame] | 2304 | 'build/android/gyp/java_cpp_strings.pydeps', |
Andrew Grieve | 0945791 | 2021-04-27 15:22:47 | [diff] [blame] | 2305 | 'build/android/gyp/java_google_api_keys.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2306 | 'build/android/gyp/jinja_template.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2307 | 'build/android/gyp/lint.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2308 | 'build/android/gyp/merge_manifest.pydeps', |
Bruce Dawson | 853b739e6 | 2022-05-03 23:03:10 | [diff] [blame] | 2309 | 'build/android/gyp/optimize_resources.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2310 | 'build/android/gyp/prepare_resources.pydeps', |
Mohamed Heikal | f85138b | 2020-10-06 15:43:22 | [diff] [blame] | 2311 | 'build/android/gyp/process_native_prebuilt.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2312 | 'build/android/gyp/proguard.pydeps', |
Mohamed Heikal | dd52b45 | 2024-09-10 17:10:50 | [diff] [blame] | 2313 | 'build/android/gyp/rename_java_classes.pydeps', |
Andrew Grieve | e3a775ab | 2022-05-16 15:59:22 | [diff] [blame] | 2314 | 'build/android/gyp/system_image_apks.pydeps', |
Bruce Dawson | 853b739e6 | 2022-05-03 23:03:10 | [diff] [blame] | 2315 | 'build/android/gyp/trace_event_bytecode_rewriter.pydeps', |
Andrew Grieve | 170b978 | 2025-02-03 15:54:53 | [diff] [blame] | 2316 | 'build/android/gyp/tracereferences.pydeps', |
Peter Wen | 578730b | 2020-03-19 19:55:46 | [diff] [blame] | 2317 | 'build/android/gyp/turbine.pydeps', |
Mohamed Heikal | 246710c | 2021-06-14 15:34:30 | [diff] [blame] | 2318 | 'build/android/gyp/unused_resources.pydeps', |
Eric Stevenson | a82cf608 | 2019-07-24 14:35:24 | [diff] [blame] | 2319 | 'build/android/gyp/validate_static_library_dex_references.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2320 | 'build/android/gyp/write_build_config.pydeps', |
Tibor Goldschwendt | c4caae9 | 2019-07-12 00:33:46 | [diff] [blame] | 2321 | 'build/android/gyp/write_native_libraries_java.pydeps', |
Andrew Grieve | 9ff1779 | 2018-11-30 04:55:56 | [diff] [blame] | 2322 | 'build/android/gyp/zip.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2323 | 'build/android/incremental_install/generate_android_manifest.pydeps', |
| 2324 | 'build/android/incremental_install/write_installer_json.pydeps', |
Stephanie Kim | 392913b45 | 2022-06-15 17:25:32 | [diff] [blame] | 2325 | 'build/android/pylib/results/presentation/test_results_presentation.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2326 | 'build/android/resource_sizes.pydeps', |
| 2327 | 'build/android/test_runner.pydeps', |
| 2328 | 'build/android/test_wrapper/logdog_wrapper.pydeps', |
zijiehe-google-com | 356980f | 2025-05-08 20:58:15 | [diff] [blame] | 2329 | 'build/fuchsia/test/component_storage_test.pydeps', |
David 'Digit' Turner | 0006f473 | 2018-08-07 07:12:36 | [diff] [blame] | 2330 | 'build/protoc_java.pydeps', |
Peter Kotwicz | 64667b0 | 2020-10-18 06:43:32 | [diff] [blame] | 2331 | 'chrome/android/monochrome/scripts/monochrome_python_tests.pydeps', |
Peter Wen | efb56c7 | 2020-06-04 15:12:27 | [diff] [blame] | 2332 | 'chrome/test/chromedriver/log_replay/client_replay_unittest.pydeps', |
| 2333 | 'chrome/test/chromedriver/test/run_py_tests.pydeps', |
Bailey Myers-Morgan | bd12213 | 2025-03-26 23:09:16 | [diff] [blame] | 2334 | 'chrome/test/media_router/performance/performance_test.pydeps', |
Junbo Ke | dcd3a45 | 2021-03-19 17:55:04 | [diff] [blame] | 2335 | 'chromecast/resource_sizes/chromecast_resource_sizes.pydeps', |
Mohannad Farrag | 1910274 | 2023-12-01 01:16:30 | [diff] [blame] | 2336 | 'components/cronet/tools/check_combined_proguard_file.pydeps', |
| 2337 | 'components/cronet/tools/generate_proguard_file.pydeps', |
Andrew Grieve | 5a01ad3 | 2020-06-25 18:06:00 | [diff] [blame] | 2338 | 'components/cronet/tools/jar_src.pydeps', |
Andrew Grieve | b773bad | 2020-06-05 18:00:38 | [diff] [blame] | 2339 | 'components/module_installer/android/module_desc_java.pydeps', |
Andrew Grieve | 5a01ad3 | 2020-06-25 18:06:00 | [diff] [blame] | 2340 | 'content/public/android/generate_child_service.pydeps', |
Hzj_jie | 77bdb80 | 2024-07-22 18:14:51 | [diff] [blame] | 2341 | 'fuchsia_web/av_testing/av_sync_tests.pydeps', |
Peter Kotwicz | 3c339f3 | 2020-10-19 19:59:18 | [diff] [blame] | 2342 | 'testing/scripts/run_isolated_script_test.pydeps', |
Stephanie Kim | c94072c | 2022-03-22 22:31:41 | [diff] [blame] | 2343 | 'testing/merge_scripts/standard_isolated_script_merge.pydeps', |
| 2344 | 'testing/merge_scripts/standard_gtest_merge.pydeps', |
| 2345 | 'testing/merge_scripts/code_coverage/merge_results.pydeps', |
| 2346 | 'testing/merge_scripts/code_coverage/merge_steps.pydeps', |
Samuel Huang | c2f5d6bb | 2020-08-17 23:46:04 | [diff] [blame] | 2347 | 'third_party/android_platform/development/scripts/stack.pydeps', |
Hitoshi Yoshida | 0f228c4 | 2019-08-07 09:37:42 | [diff] [blame] | 2348 | 'third_party/blink/renderer/bindings/scripts/build_web_idl_database.pydeps', |
Yuki Shiino | 38eeaad1 | 2022-08-11 06:40:25 | [diff] [blame] | 2349 | 'third_party/blink/renderer/bindings/scripts/check_generated_file_list.pydeps', |
Hitoshi Yoshida | 0f228c4 | 2019-08-07 09:37:42 | [diff] [blame] | 2350 | 'third_party/blink/renderer/bindings/scripts/collect_idl_files.pydeps', |
Yuki Shiino | e7827aa | 2019-09-13 12:26:13 | [diff] [blame] | 2351 | 'third_party/blink/renderer/bindings/scripts/generate_bindings.pydeps', |
Yuki Shiino | ea477d3 | 2023-08-21 06:24:34 | [diff] [blame] | 2352 | 'third_party/blink/renderer/bindings/scripts/generate_event_interface_names.pydeps', |
Canon Mukai | f32f8f59 | 2021-04-23 18:56:50 | [diff] [blame] | 2353 | 'third_party/blink/renderer/bindings/scripts/validate_web_idl.pydeps', |
Stephanie Kim | c94072c | 2022-03-22 22:31:41 | [diff] [blame] | 2354 | 'third_party/blink/tools/blinkpy/web_tests/merge_results.pydeps', |
| 2355 | 'third_party/blink/tools/merge_web_test_results.pydeps', |
John Budorick | bc3571aa | 2019-04-25 02:20:06 | [diff] [blame] | 2356 | 'tools/binary_size/sizes.pydeps', |
Andrew Grieve | a7f1ee90 | 2018-05-18 16:17:22 | [diff] [blame] | 2357 | 'tools/binary_size/supersize.pydeps', |
Peter Wen | 2dcfa6e | 2025-03-04 22:42:52 | [diff] [blame] | 2358 | 'tools/cygprofile/generate_orderfile.pydeps', |
Ben Pastene | 028104a | 2022-08-10 19:17:45 | [diff] [blame] | 2359 | 'tools/perf/process_perf_results.pydeps', |
Peter Wen | ce103e1 | 2024-10-09 19:23:51 | [diff] [blame] | 2360 | 'tools/pgo/generate_profile.pydeps', |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 2361 | ] |
| 2362 | |
| 2363 | _ALL_PYDEPS_FILES= _ANDROID_SPECIFIC_PYDEPS_FILES+ _GENERIC_PYDEPS_FILES |
| 2364 | |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 2365 | # Bypass the AUTHORS check for these accounts. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2366 | _KNOWN_ROBOTS= set()| set('%s@appspot.gserviceaccount.com'% sfor sin( |
| 2367 | 'findit-for-me','luci-bisection','predator-for-me-staging', |
| 2368 | 'predator-for-me'))| set( |
| 2369 | '%s@developer.gserviceaccount.com'% s |
| 2370 | for sin('3su6n15k.default',))| set( |
| 2371 | '%s@chops-service-accounts.iam.gserviceaccount.com'% s |
| 2372 | for sin('bling-autoroll-builder','v8-ci-autoroll-builder', |
| 2373 | 'wpt-autoroller','chrome-weblayer-builder', |
| 2374 | 'skylab-test-cros-roller','infra-try-recipes-tester', |
| 2375 | 'chrome-automated-expectation', |
| 2376 | 'chromium-automated-expectation','chrome-branch-day', |
| 2377 | 'chrome-cherry-picker','chromium-autosharder') |
| 2378 | )| set( |
| 2379 | '%s@skia-public.iam.gserviceaccount.com'% s |
| 2380 | for sin('chromium-autoroll','chromium-release-autoroll'))| set( |
| 2381 | '%s@skia-corp.google.com.iam.gserviceaccount.com'% s |
| 2382 | for sin('chromium-internal-autoroll',) |
| 2383 | )| set( |
| 2384 | '%s@system.gserviceaccount.com'% |
| 2385 | sfor sin('chrome-screen-ai-releaser','crash-eng','crash') |
| 2386 | )| set( |
| 2387 | '%s@owners-cleanup-prod.google.com.iam.gserviceaccount.com'% s |
| 2388 | for sin('swarming-tasks',))| set( |
| 2389 | '%s@fuchsia-infra.iam.gserviceaccount.com'% s |
| 2390 | for sin('global-integration-try-builder', |
Rachael Newitt | e5664ef9 | 2025-05-08 14:00:23 | [diff] [blame] | 2391 | 'global-integration-ci-builder') |
| 2392 | )| set('%s@prod.google.com'% sfor sin( |
| 2393 | 'chops-security-borg', |
| 2394 | 'chops-security-cronjobs-cpesuggest'))| set( |
| 2395 | '%s@chromeos-release-bot.iam.gserviceaccount.com'% s |
Joey Scarr | f55d5c4 | 2025-07-07 22:03:06 | [diff] [blame] | 2396 | for sin('chromeos-ci-release',))| set( |
| 2397 | '%s@chromeos-bot.iam.gserviceaccount.com'% s |
| 2398 | for sin('chromeos-ci-prod',)) |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 2399 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2400 | _INVALID_GRD_FILE_LINE=[(r'<file lang=.* path=.*', |
| 2401 | 'Path should come before lang in GRD files.')] |
| 2402 | |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 2403 | |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2404 | def_IsCPlusPlusFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2405 | """Returns True if this file contains C++-like code (and not Python, |
| 2406 | Go, Java, MarkDown, ...)""" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2407 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2408 | ext= input_api.os_path.splitext(file_path)[1] |
| 2409 | # This list is compatible with CppChecker.IsCppFile but we should |
| 2410 | # consider adding ".c" to it. If we do that we can use this function |
| 2411 | # at more places in the code. |
| 2412 | return extin( |
| 2413 | '.h', |
| 2414 | '.cc', |
| 2415 | '.cpp', |
| 2416 | '.m', |
| 2417 | '.mm', |
| 2418 | ) |
| 2419 | |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2420 | |
| 2421 | def_IsCPlusPlusHeaderFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2422 | return input_api.os_path.splitext(file_path)[1]==".h" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2423 | |
| 2424 | |
| 2425 | def_IsJavaFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2426 | return input_api.os_path.splitext(file_path)[1]==".java" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2427 | |
| 2428 | |
| 2429 | def_IsProtoFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2430 | return input_api.os_path.splitext(file_path)[1]==".proto" |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 2431 | |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2432 | |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 2433 | def_IsXmlOrGrdFile(input_api, file_path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2434 | ext= input_api.os_path.splitext(file_path)[1] |
| 2435 | return extin('.grd','.xml') |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 2436 | |
| 2437 | |
Sven Zheng | 76a79ea | 2022-12-21 21:25:24 | [diff] [blame] | 2438 | def_IsMojomFile(input_api, file_path): |
| 2439 | return input_api.os_path.splitext(file_path)[1]==".mojom" |
| 2440 | |
| 2441 | |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2442 | defCheckNoUpstreamDepsOnClank(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2443 | """Prevent additions of dependencies from the upstream repo on //clank.""" |
| 2444 | # clank can depend on clank |
| 2445 | if input_api.change.RepositoryRoot().endswith('clank'): |
| 2446 | return[] |
| 2447 | build_file_patterns=[ |
| 2448 | r'(.+/)?BUILD\.gn', |
| 2449 | r'.+\.gni', |
| 2450 | ] |
| 2451 | excluded_files=[r'build[/\\]config[/\\]android[/\\]config\.gni'] |
| 2452 | bad_pattern= input_api.re.compile(r'^[^#]*//clank') |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2453 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2454 | error_message='Disallowed import on //clank in an upstream build file:' |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2455 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2456 | defFilterFile(affected_file): |
| 2457 | return input_api.FilterSourceFile(affected_file, |
| 2458 | files_to_check=build_file_patterns, |
| 2459 | files_to_skip=excluded_files) |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2460 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2461 | problems=[] |
| 2462 | for fin input_api.AffectedSourceFiles(FilterFile): |
| 2463 | local_path= f.LocalPath() |
| 2464 | for line_number, linein f.ChangedContents(): |
| 2465 | if(bad_pattern.search(line)): |
| 2466 | problems.append('%s:%d\n %s'% |
| 2467 | (local_path, line_number, line.strip())) |
| 2468 | if problems: |
| 2469 | return[output_api.PresubmitPromptOrNotify(error_message, problems)] |
| 2470 | else: |
| 2471 | return[] |
Mohamed Heikal | 5e5b792 | 2020-10-29 18:57:59 | [diff] [blame] | 2472 | |
| 2473 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2474 | defCheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2475 | """Attempts to prevent use of functions intended only for testing in |
| 2476 | non-testing code. For now this is just a best-effort implementation |
| 2477 | that ignores header files and may have some false positives. A |
| 2478 | better implementation would probably need a proper C++ parser. |
| 2479 | """ |
| 2480 | # We only scan .cc files and the like, as the declaration of |
| 2481 | # for-testing functions in header files are hard to distinguish from |
| 2482 | # calls to such functions without a proper C++ parser. |
| 2483 | file_inclusion_pattern=[r'.+%s'% _IMPLEMENTATION_EXTENSIONS] |
joi@chromium.org | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2484 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2485 | base_function_pattern= r'[ :]test::[^\s]+|ForTest(s|ing)?|for_test(s|ing)?' |
| 2486 | inclusion_pattern= input_api.re.compile(r'(%s)\s*\('% |
| 2487 | base_function_pattern) |
| 2488 | comment_pattern= input_api.re.compile(r'//.*(%s)'% base_function_pattern) |
| 2489 | allowlist_pattern= input_api.re.compile(r'// IN-TEST$') |
| 2490 | exclusion_pattern= input_api.re.compile( |
| 2491 | r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{'% |
| 2492 | (base_function_pattern, base_function_pattern)) |
| 2493 | # Avoid a false positive in this case, where the method name, the ::, and |
| 2494 | # the closing { are all on different lines due to line wrapping. |
| 2495 | # HelperClassForTesting:: |
| 2496 | # HelperClassForTesting( |
| 2497 | # args) |
| 2498 | # : member(0) {} |
| 2499 | method_defn_pattern= input_api.re.compile(r'[A-Za-z0-9_]+::$') |
joi@chromium.org | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2500 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2501 | defFilterFile(affected_file): |
| 2502 | files_to_skip=(_EXCLUDED_PATHS+ _TEST_CODE_EXCLUDED_PATHS+ |
| 2503 | input_api.DEFAULT_FILES_TO_SKIP) |
| 2504 | return input_api.FilterSourceFile( |
| 2505 | affected_file, |
| 2506 | files_to_check=file_inclusion_pattern, |
| 2507 | files_to_skip=files_to_skip) |
joi@chromium.org | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2508 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2509 | problems=[] |
| 2510 | for fin input_api.AffectedSourceFiles(FilterFile): |
| 2511 | local_path= f.LocalPath() |
| 2512 | in_method_defn=False |
| 2513 | for line_number, linein f.ChangedContents(): |
| 2514 | if(inclusion_pattern.search(line) |
| 2515 | andnot comment_pattern.search(line) |
| 2516 | andnot exclusion_pattern.search(line) |
| 2517 | andnot allowlist_pattern.search(line) |
| 2518 | andnot in_method_defn): |
| 2519 | problems.append('%s:%d\n %s'% |
| 2520 | (local_path, line_number, line.strip())) |
| 2521 | in_method_defn= method_defn_pattern.search(line) |
joi@chromium.org | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2522 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2523 | if problems: |
| 2524 | return[ |
| 2525 | output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems) |
| 2526 | ] |
| 2527 | else: |
| 2528 | return[] |
joi@chromium.org | 5545985 | 2011-08-10 15:17:19 | [diff] [blame] | 2529 | |
| 2530 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2531 | defCheckNoProductionCodeUsingTestOnlyFunctionsJava(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2532 | """This is a simplified version of |
| 2533 | CheckNoProductionCodeUsingTestOnlyFunctions for Java files. |
| 2534 | """ |
| 2535 | javadoc_start_re= input_api.re.compile(r'^\s*/\*\*') |
| 2536 | javadoc_end_re= input_api.re.compile(r'^\s*\*/') |
| 2537 | name_pattern= r'ForTest(s|ing)?' |
| 2538 | # Describes an occurrence of "ForTest*" inside a // comment. |
| 2539 | comment_re= input_api.re.compile(r'//.*%s'% name_pattern) |
| 2540 | # Describes @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED) |
| 2541 | annotation_re= input_api.re.compile(r'@VisibleForTesting\(') |
| 2542 | # Catch calls. |
| 2543 | inclusion_re= input_api.re.compile(r'(%s)\s*\('% name_pattern) |
| 2544 | # Ignore definitions. (Comments are ignored separately.) |
| 2545 | exclusion_re= input_api.re.compile(r'(%s)[^;]+\{'% name_pattern) |
Andrew Grieve | 40f451d | 2023-07-06 19:46:51 | [diff] [blame] | 2546 | allowlist_re= input_api.re.compile(r'// IN-TEST$') |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 2547 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2548 | problems=[] |
| 2549 | sources=lambda x: input_api.FilterSourceFile( |
| 2550 | x, |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2551 | files_to_skip=( |
| 2552 | ('(?i).*test', r'.*\/junit\/')+ input_api.DEFAULT_FILES_TO_SKIP), |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2553 | files_to_check=[r'.*\.java$']) |
| 2554 | for fin input_api.AffectedFiles(include_deletes=False, |
| 2555 | file_filter=sources): |
| 2556 | local_path= f.LocalPath() |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 2557 | is_inside_javadoc=False |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2558 | for line_number, linein f.ChangedContents(): |
| 2559 | if is_inside_javadocand javadoc_end_re.search(line): |
| 2560 | is_inside_javadoc=False |
| 2561 | ifnot is_inside_javadocand javadoc_start_re.search(line): |
| 2562 | is_inside_javadoc=True |
| 2563 | if is_inside_javadoc: |
| 2564 | continue |
| 2565 | if(inclusion_re.search(line)andnot comment_re.search(line) |
| 2566 | andnot annotation_re.search(line) |
Andrew Grieve | 40f451d | 2023-07-06 19:46:51 | [diff] [blame] | 2567 | andnot allowlist_re.search(line) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2568 | andnot exclusion_re.search(line)): |
| 2569 | problems.append('%s:%d\n %s'% |
| 2570 | (local_path, line_number, line.strip())) |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 2571 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2572 | if problems: |
| 2573 | return[ |
| 2574 | output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems) |
| 2575 | ] |
| 2576 | else: |
| 2577 | return[] |
Vaclav Brozek | 7dbc28c | 2018-03-27 08:35:23 | [diff] [blame] | 2578 | |
| 2579 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2580 | defCheckNoIOStreamInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2581 | """Checks to make sure no .h files include <iostream>.""" |
| 2582 | files=[] |
| 2583 | pattern= input_api.re.compile(r'^#include\s*<iostream>', |
| 2584 | input_api.re.MULTILINE) |
| 2585 | for fin input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 2586 | ifnot f.LocalPath().endswith('.h'): |
| 2587 | continue |
| 2588 | contents= input_api.ReadFile(f) |
| 2589 | if pattern.search(contents): |
| 2590 | files.append(f) |
thakis@chromium.org | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 2591 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2592 | if len(files): |
| 2593 | return[ |
| 2594 | output_api.PresubmitError( |
| 2595 | 'Do not #include <iostream> in header files, since it inserts static ' |
| 2596 | 'initialization into every file including the header. Instead, ' |
| 2597 | '#include <ostream>. See http://crbug.com/94794', files) |
| 2598 | ] |
| 2599 | return[] |
| 2600 | |
thakis@chromium.org | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 2601 | |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 2602 | defCheckNoStrCatRedefines(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2603 | """Checks no windows headers with StrCat redefined are included directly.""" |
| 2604 | files=[] |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 2605 | files_to_check=(r'.+%s'% _HEADER_EXTENSIONS, |
| 2606 | r'.+%s'% _IMPLEMENTATION_EXTENSIONS) |
| 2607 | files_to_skip=(input_api.DEFAULT_FILES_TO_SKIP+ |
| 2608 | _NON_BASE_DEPENDENT_PATHS) |
| 2609 | sources_filter=lambda f: input_api.FilterSourceFile( |
| 2610 | f, files_to_check=files_to_check, files_to_skip=files_to_skip) |
| 2611 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2612 | pattern_deny= input_api.re.compile( |
| 2613 | r'^#include\s*[<"](shlwapi|atlbase|propvarutil|sphelper).h[">]', |
| 2614 | input_api.re.MULTILINE) |
| 2615 | pattern_allow= input_api.re.compile( |
| 2616 | r'^#include\s"base/win/windows_defines.inc"', input_api.re.MULTILINE) |
Aleksey Khoroshilov | 9b28c03 | 2022-06-03 16:35:32 | [diff] [blame] | 2617 | for fin input_api.AffectedSourceFiles(sources_filter): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2618 | contents= input_api.ReadFile(f) |
| 2619 | if pattern_deny.search( |
| 2620 | contents)andnot pattern_allow.search(contents): |
| 2621 | files.append(f.LocalPath()) |
Danil Chapovalov | 3518f36 | 2018-08-11 16:13:43 | [diff] [blame] | 2622 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2623 | if len(files): |
| 2624 | return[ |
| 2625 | output_api.PresubmitError( |
| 2626 | 'Do not #include shlwapi.h, atlbase.h, propvarutil.h or sphelper.h ' |
| 2627 | 'directly since they pollute code with StrCat macro. Instead, ' |
| 2628 | 'include matching header from base/win. See http://crbug.com/856536', |
| 2629 | files) |
| 2630 | ] |
| 2631 | return[] |
Danil Chapovalov | 3518f36 | 2018-08-11 16:13:43 | [diff] [blame] | 2632 | |
thakis@chromium.org | 10689ca | 2011-09-02 02:31:54 | [diff] [blame] | 2633 | |
Andrew Williams | c9f69b48 | 2023-07-10 16:07:36 | [diff] [blame] | 2634 | def_CheckNoUNIT_TESTInSourceFiles(input_api, f): |
| 2635 | problems=[] |
| 2636 | |
| 2637 | unit_test_macro= input_api.re.compile( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2638 | r'^\s*#.*(?:ifn?def\s+UNIT_TEST|defined\s*\(?\s*UNIT_TEST\s*\)?)(?:$|\s+)' |
| 2639 | ) |
Andrew Williams | c9f69b48 | 2023-07-10 16:07:36 | [diff] [blame] | 2640 | for line_num, linein f.ChangedContents(): |
| 2641 | if unit_test_macro.match(line): |
| 2642 | problems.append(' %s:%d'%(f.LocalPath(), line_num)) |
| 2643 | |
| 2644 | return problems |
| 2645 | |
| 2646 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2647 | defCheckNoUNIT_TESTInSourceFiles(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2648 | """Checks to make sure no source files use UNIT_TEST.""" |
| 2649 | problems=[] |
| 2650 | for fin input_api.AffectedFiles(): |
| 2651 | if(not f.LocalPath().endswith(('.cc','.mm'))): |
| 2652 | continue |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2653 | problems.extend(_CheckNoUNIT_TESTInSourceFiles(input_api, f)) |
jam@chromium.org | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 2654 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2655 | ifnot problems: |
| 2656 | return[] |
| 2657 | return[ |
| 2658 | output_api.PresubmitPromptWarning('UNIT_TEST is only for headers.\n'+ |
| 2659 | '\n'.join(problems)) |
| 2660 | ] |
| 2661 | |
jam@chromium.org | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 2662 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2663 | defCheckNoDISABLETypoInTests(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2664 | """Checks to prevent attempts to disable tests with DISABLE_ prefix. |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2665 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2666 | This test warns if somebody tries to disable a test with the DISABLE_ prefix |
| 2667 | instead of DISABLED_. To filter false positives, reports are only generated |
| 2668 | if a corresponding MAYBE_ line exists. |
| 2669 | """ |
| 2670 | problems=[] |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2671 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2672 | # The following two patterns are looked for in tandem - is a test labeled |
| 2673 | # as MAYBE_ followed by a DISABLE_ (instead of the correct DISABLED) |
| 2674 | maybe_pattern= input_api.re.compile(r'MAYBE_([a-zA-Z0-9_]+)') |
| 2675 | disable_pattern= input_api.re.compile(r'DISABLE_([a-zA-Z0-9_]+)') |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2676 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2677 | # This is for the case that a test is disabled on all platforms. |
| 2678 | full_disable_pattern= input_api.re.compile( |
| 2679 | r'^\s*TEST[^(]*\([a-zA-Z0-9_]+,\s*DISABLE_[a-zA-Z0-9_]+\)', |
| 2680 | input_api.re.MULTILINE) |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2681 | |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 2682 | for fin input_api.AffectedFiles(include_deletes=False): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2683 | ifnot'test'in f.LocalPath()ornot f.LocalPath().endswith('.cc'): |
| 2684 | continue |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2685 | |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 2686 | # Search for MAYBE_, DISABLE_ pairs. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2687 | disable_lines={}# Maps of test name to line number. |
| 2688 | maybe_lines={} |
| 2689 | for line_num, linein f.ChangedContents(): |
| 2690 | disable_match= disable_pattern.search(line) |
| 2691 | if disable_match: |
| 2692 | disable_lines[disable_match.group(1)]= line_num |
| 2693 | maybe_match= maybe_pattern.search(line) |
| 2694 | if maybe_match: |
| 2695 | maybe_lines[maybe_match.group(1)]= line_num |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2696 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2697 | # Search for DISABLE_ occurrences within a TEST() macro. |
| 2698 | disable_tests= set(disable_lines.keys()) |
| 2699 | maybe_tests= set(maybe_lines.keys()) |
| 2700 | for testin disable_tests.intersection(maybe_tests): |
| 2701 | problems.append(' %s:%d'%(f.LocalPath(), disable_lines[test])) |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2702 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2703 | contents= input_api.ReadFile(f) |
| 2704 | full_disable_match= full_disable_pattern.search(contents) |
| 2705 | if full_disable_match: |
| 2706 | problems.append(' %s'% f.LocalPath()) |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2707 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2708 | ifnot problems: |
| 2709 | return[] |
| 2710 | return[ |
| 2711 | output_api.PresubmitPromptWarning( |
| 2712 | 'Attempt to disable a test with DISABLE_ instead of DISABLED_?\n'+ |
| 2713 | '\n'.join(problems)) |
| 2714 | ] |
| 2715 | |
Dominic Battre | 03353105 | 2018-09-24 15:45:34 | [diff] [blame] | 2716 | |
Nina Satragno | f766053 | 2021-09-20 18:03:35 | [diff] [blame] | 2717 | defCheckForgettingMAYBEInTests(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2718 | """Checks to make sure tests disabled conditionally are not missing a |
| 2719 | corresponding MAYBE_ prefix. |
| 2720 | """ |
| 2721 | # Expect at least a lowercase character in the test name. This helps rule out |
| 2722 | # false positives with macros wrapping the actual tests name. |
| 2723 | define_maybe_pattern= input_api.re.compile( |
| 2724 | r'^\#define MAYBE_(?P<test_name>\w*[a-z]\w*)') |
Bruce Dawson | ffc5529 | 2022-04-20 04:18:19 | [diff] [blame] | 2725 | # The test_maybe_pattern needs to handle all of these forms. The standard: |
| 2726 | # IN_PROC_TEST_F(SyncTest, MAYBE_Start) { |
| 2727 | # With a wrapper macro around the test name: |
| 2728 | # IN_PROC_TEST_F(SyncTest, E2E_ENABLED(MAYBE_Start)) { |
| 2729 | # And the odd-ball NACL_BROWSER_TEST_f format: |
| 2730 | # NACL_BROWSER_TEST_F(NaClBrowserTest, SimpleLoad, { |
| 2731 | # The optional E2E_ENABLED-style is handled with (\w*\()? |
| 2732 | # The NACL_BROWSER_TEST_F pattern is handled by allowing a trailing comma or |
| 2733 | # trailing ')'. |
| 2734 | test_maybe_pattern=( |
| 2735 | r'^\s*\w*TEST[^(]*\(\s*\w+,\s*(\w*\()?MAYBE_{test_name}[\),]') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2736 | suite_maybe_pattern= r'^\s*\w*TEST[^(]*\(\s*MAYBE_{test_name}[\),]' |
| 2737 | warnings=[] |
Nina Satragno | f766053 | 2021-09-20 18:03:35 | [diff] [blame] | 2738 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2739 | # Read the entire files. We can't just read the affected lines, forgetting to |
| 2740 | # add MAYBE_ on a change would not show up otherwise. |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 2741 | for fin input_api.AffectedFiles(include_deletes=False): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2742 | ifnot'test'in f.LocalPath()ornot f.LocalPath().endswith('.cc'): |
| 2743 | continue |
| 2744 | contents= input_api.ReadFile(f) |
| 2745 | lines= contents.splitlines(True) |
| 2746 | current_position=0 |
| 2747 | warning_test_names= set() |
| 2748 | for line_num, linein enumerate(lines, start=1): |
| 2749 | current_position+= len(line) |
| 2750 | maybe_match= define_maybe_pattern.search(line) |
| 2751 | if maybe_match: |
| 2752 | test_name= maybe_match.group('test_name') |
| 2753 | # Do not warn twice for the same test. |
| 2754 | if(test_namein warning_test_names): |
| 2755 | continue |
| 2756 | warning_test_names.add(test_name) |
Nina Satragno | f766053 | 2021-09-20 18:03:35 | [diff] [blame] | 2757 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2758 | # Attempt to find the corresponding MAYBE_ test or suite, starting from |
| 2759 | # the current position. |
| 2760 | test_match= input_api.re.compile( |
| 2761 | test_maybe_pattern.format(test_name=test_name), |
| 2762 | input_api.re.MULTILINE).search(contents, current_position) |
| 2763 | suite_match= input_api.re.compile( |
| 2764 | suite_maybe_pattern.format(test_name=test_name), |
| 2765 | input_api.re.MULTILINE).search(contents, current_position) |
| 2766 | ifnot test_matchandnot suite_match: |
| 2767 | warnings.append( |
| 2768 | output_api.PresubmitPromptWarning( |
| 2769 | '%s:%d found MAYBE_ defined without corresponding test %s' |
| 2770 | %(f.LocalPath(), line_num, test_name))) |
| 2771 | return warnings |
| 2772 | |
jam@chromium.org | 72df4e78 | 2012-06-21 16:28:18 | [diff] [blame] | 2773 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2774 | defCheckDCHECK_IS_ONHasBraces(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2775 | """Checks to make sure DCHECK_IS_ON() does not skip the parentheses.""" |
| 2776 | errors=[] |
Kalvin Lee | 4a3b79de | 2022-05-26 16:00:16 | [diff] [blame] | 2777 | pattern= input_api.re.compile(r'\bDCHECK_IS_ON\b(?!\(\))', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2778 | input_api.re.MULTILINE) |
| 2779 | for fin input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 2780 | if(not f.LocalPath().endswith(('.cc','.mm','.h'))): |
| 2781 | continue |
| 2782 | for lnum, linein f.ChangedContents(): |
| 2783 | if input_api.re.search(pattern, line): |
| 2784 | errors.append( |
| 2785 | output_api.PresubmitError(( |
| 2786 | '%s:%d: Use of DCHECK_IS_ON() must be written as "#if ' |
| 2787 | +'DCHECK_IS_ON()", not forgetting the parentheses.')% |
| 2788 | (f.LocalPath(), lnum))) |
| 2789 | return errors |
danakj | 61c1aa2 | 2015-10-26 19:55:52 | [diff] [blame] | 2790 | |
| 2791 | |
Weilun Shi | a487fad | 2020-10-28 00:10:34 | [diff] [blame] | 2792 | # TODO(crbug/1138055): Reimplement CheckUmaHistogramChangesOnUpload check in a |
| 2793 | # more reliable way. See |
| 2794 | # https://chromium-review.googlesource.com/c/chromium/src/+/2500269 |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 2795 | |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2796 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2797 | defCheckFlakyTestUsage(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2798 | """Check that FlakyTest annotation is our own instead of the android one""" |
| 2799 | pattern= input_api.re.compile(r'import android.test.FlakyTest;') |
| 2800 | files=[] |
| 2801 | for fin input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 2802 | if f.LocalPath().endswith('Test.java'): |
| 2803 | if pattern.search(input_api.ReadFile(f)): |
| 2804 | files.append(f) |
| 2805 | if len(files): |
| 2806 | return[ |
| 2807 | output_api.PresubmitError( |
| 2808 | 'Use org.chromium.base.test.util.FlakyTest instead of ' |
| 2809 | 'android.test.FlakyTest', files) |
| 2810 | ] |
| 2811 | return[] |
mcasas | b7440c28 | 2015-02-04 14:52:19 | [diff] [blame] | 2812 | |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2813 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2814 | defCheckNoDEPSGIT(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2815 | """Make sure .DEPS.git is never modified manually.""" |
| 2816 | if any(f.LocalPath().endswith('.DEPS.git') |
| 2817 | for fin input_api.AffectedFiles()): |
| 2818 | return[ |
| 2819 | output_api.PresubmitError( |
| 2820 | 'Never commit changes to .DEPS.git. This file is maintained by an\n' |
| 2821 | 'automated system based on what\'s in DEPS and your changes will be\n' |
| 2822 | 'overwritten.\n' |
| 2823 | 'See https://sites.google.com/a/chromium.org/dev/developers/how-tos/' |
| 2824 | 'get-the-code#Rolling_DEPS\n' |
| 2825 | 'for more information') |
| 2826 | ] |
| 2827 | return[] |
maruel@chromium.org | 2a8ac9c | 2011-10-19 17:20:44 | [diff] [blame] | 2828 | |
| 2829 | |
Sven Zheng | 76a79ea | 2022-12-21 21:25:24 | [diff] [blame] | 2830 | defCheckCrosApiNeedBrowserTest(input_api, output_api): |
| 2831 | """Check new crosapi should add browser test.""" |
| 2832 | has_new_crosapi=False |
| 2833 | has_browser_test=False |
| 2834 | for fin input_api.AffectedFiles(): |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 2835 | path= f.UnixLocalPath() |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2836 | if(path.startswith('chromeos/crosapi/mojom') |
| 2837 | and_IsMojomFile(input_api, path)and f.Action()=='A'): |
Sven Zheng | 76a79ea | 2022-12-21 21:25:24 | [diff] [blame] | 2838 | has_new_crosapi=True |
| 2839 | if path.endswith('browsertest.cc')or path.endswith('browser_test.cc'): |
| 2840 | has_browser_test=True |
| 2841 | if has_new_crosapiandnot has_browser_test: |
| 2842 | return[ |
| 2843 | output_api.PresubmitPromptWarning( |
| 2844 | 'You are adding a new crosapi, but there is no file ends with ' |
| 2845 | 'browsertest.cc file being added or modified. It is important ' |
| 2846 | 'to add crosapi browser test coverage to avoid version ' |
| 2847 | ' skew issues.\n' |
| 2848 | 'Check //docs/lacros/test_instructions.md for more information.' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2849 | ) |
Sven Zheng | 76a79ea | 2022-12-21 21:25:24 | [diff] [blame] | 2850 | ] |
| 2851 | return[] |
| 2852 | |
| 2853 | |
Mario Sanchez Prada | 2472cab | 2019-09-18 10:58:31 | [diff] [blame] | 2854 | def_GetMessageForMatchingType(input_api, affected_file, line_number, line, |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2855 | ban_rule): |
Allen Bauer | 8477868 | 2022-09-22 16:28:56 | [diff] [blame] | 2856 | """Helper method for checking for banned constructs. |
Mario Sanchez Prada | 2472cab | 2019-09-18 10:58:31 | [diff] [blame] | 2857 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2858 | Returns an string composed of the name of the file, the line number where the |
| 2859 | match has been found and the additional text passed as |message| in case the |
| 2860 | target type name matches the text inside the line passed as parameter. |
| 2861 | """ |
| 2862 | result=[] |
Peng Huang | 9c5949a0 | 2020-06-11 19:20:54 | [diff] [blame] | 2863 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2864 | # Ignore comments about banned types. |
| 2865 | if input_api.re.search(r"^ *//", line): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2866 | return result |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2867 | # A // nocheck comment will bypass this error. |
| 2868 | if line.endswith(" nocheck"): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2869 | return result |
| 2870 | |
| 2871 | matched=False |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2872 | if ban_rule.pattern[0:1]=='/': |
| 2873 | regex= ban_rule.pattern[1:] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2874 | if input_api.re.search(regex, line): |
| 2875 | matched=True |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2876 | elif ban_rule.patternin line: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2877 | matched=True |
| 2878 | |
| 2879 | if matched: |
| 2880 | result.append(' %s:%d:'%(affected_file.LocalPath(), line_number)) |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2881 | for linein ban_rule.explanation: |
| 2882 | result.append(' %s'% line) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2883 | |
danakj | d18e889 | 2020-12-17 17:42:01 | [diff] [blame] | 2884 | return result |
Mario Sanchez Prada | 2472cab | 2019-09-18 10:58:31 | [diff] [blame] | 2885 | |
| 2886 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 2887 | defCheckNoBannedFunctions(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2888 | """Make sure that banned functions are not used.""" |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2889 | results=[] |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2890 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2891 | defIsExcludedFile(affected_file, excluded_paths): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2892 | ifnot excluded_paths: |
| 2893 | returnFalse |
| 2894 | |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 2895 | local_path= affected_file.UnixLocalPath() |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2896 | for itemin excluded_paths: |
| 2897 | if input_api.re.match(item, local_path): |
| 2898 | returnTrue |
| 2899 | returnFalse |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2900 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2901 | defIsIosObjcFile(affected_file): |
| 2902 | local_path= affected_file.LocalPath() |
| 2903 | if input_api.os_path.splitext(local_path)[-1]notin('.mm','.m', |
| 2904 | '.h'): |
| 2905 | returnFalse |
| 2906 | basename= input_api.os_path.basename(local_path) |
| 2907 | if'ios'in basename.split('_'): |
| 2908 | returnTrue |
| 2909 | for sepin(input_api.os_path.sep, input_api.os_path.altsep): |
| 2910 | if sepand'ios'in local_path.split(sep): |
| 2911 | returnTrue |
| 2912 | returnFalse |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 2913 | |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2914 | defCheckForMatch(affected_file, line_num: int, line: str, |
| 2915 | ban_rule:BanRule): |
| 2916 | ifIsExcludedFile(affected_file, ban_rule.excluded_paths): |
| 2917 | return |
| 2918 | |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 2919 | message=_GetMessageForMatchingType(input_api, f, line_num, line, |
| 2920 | ban_rule) |
| 2921 | if message: |
| 2922 | result_loc=[] |
| 2923 | if ban_rule.surface_as_gerrit_lint: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2924 | result_loc.append( |
| 2925 | output_api.PresubmitResultLocation( |
| 2926 | file_path=affected_file.LocalPath(), |
| 2927 | start_line=line_num, |
| 2928 | end_line=line_num, |
| 2929 | )) |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2930 | if ban_rule.treat_as_errorisnotNoneand ban_rule.treat_as_error: |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 2931 | results.append( |
| 2932 | output_api.PresubmitError('A banned function was used.\n'+ |
| 2933 | '\n'.join(message), |
| 2934 | locations=result_loc)) |
| 2935 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2936 | else: |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 2937 | results.append( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2938 | output_api.PresubmitPromptWarning( |
| 2939 | 'A banned function was used.\n'+'\n'.join(message), |
| 2940 | locations=result_loc)) |
wnwen | bdc444e | 2016-05-25 13:44:15 | [diff] [blame] | 2941 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2942 | file_filter=lambda f: f.LocalPath().endswith(('.java')) |
| 2943 | for fin input_api.AffectedFiles(file_filter=file_filter): |
| 2944 | for line_num, linein f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2945 | for ban_rulein _BANNED_JAVA_FUNCTIONS: |
| 2946 | CheckForMatch(f, line_num, line, ban_rule) |
Eric Stevenson | a9a98097 | 2017-09-23 00:04:41 | [diff] [blame] | 2947 | |
Clement Yan | 9b330cb | 2022-11-17 05:25:29 | [diff] [blame] | 2948 | file_filter=lambda f: f.LocalPath().endswith(('.js','.ts')) |
| 2949 | for fin input_api.AffectedFiles(file_filter=file_filter): |
| 2950 | for line_num, linein f.ChangedContents(): |
| 2951 | for ban_rulein _BANNED_JAVASCRIPT_FUNCTIONS: |
| 2952 | CheckForMatch(f, line_num, line, ban_rule) |
| 2953 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2954 | file_filter=lambda f: f.LocalPath().endswith(('.mm','.m','.h')) |
| 2955 | for fin input_api.AffectedFiles(file_filter=file_filter): |
| 2956 | for line_num, linein f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2957 | for ban_rulein _BANNED_OBJC_FUNCTIONS: |
| 2958 | CheckForMatch(f, line_num, line, ban_rule) |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2959 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2960 | for fin input_api.AffectedFiles(file_filter=IsIosObjcFile): |
| 2961 | for line_num, linein f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2962 | for ban_rulein _BANNED_IOS_OBJC_FUNCTIONS: |
| 2963 | CheckForMatch(f, line_num, line, ban_rule) |
Sylvain Defresne | a8b73d25 | 2018-02-28 15:45:54 | [diff] [blame] | 2964 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2965 | egtest_filter=lambda f: f.LocalPath().endswith(('_egtest.mm')) |
| 2966 | for fin input_api.AffectedFiles(file_filter=egtest_filter): |
| 2967 | for line_num, linein f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2968 | for ban_rulein _BANNED_IOS_EGTEST_FUNCTIONS: |
| 2969 | CheckForMatch(f, line_num, line, ban_rule) |
Peter K. Lee | 6c03ccff | 2019-07-15 14:40:05 | [diff] [blame] | 2970 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 2971 | file_filter=lambda f: f.LocalPath().endswith(('.cc','.mm','.h')) |
| 2972 | for fin input_api.AffectedFiles(file_filter=file_filter): |
| 2973 | for line_num, linein f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 2974 | for ban_rulein _BANNED_CPP_FUNCTIONS: |
| 2975 | CheckForMatch(f, line_num, line, ban_rule) |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 2976 | |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2977 | # As of 05/2024, iOS fully migrated ConsentLevel::kSync to kSignin, and |
| 2978 | # Android is in the process of preventing new users from entering kSync. |
| 2979 | # So the warning is restricted to those platforms. |
Riley Wong | 49be8a88 | 2025-02-27 00:38:23 | [diff] [blame] | 2980 | ios_pattern= input_api.re.compile(r'(^|[\W_])ios[\W_]') |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 2981 | file_filter=lambda f:( |
| 2982 | f.LocalPath().endswith(('.cc','.mm','.h'))and |
| 2983 | ('android'in f.LocalPath()or |
| 2984 | # Simply checking for an 'ios' substring would |
| 2985 | # catch unrelated cases, use a regex. |
| 2986 | ios_pattern.search(f.LocalPath()))) |
Victor Hugo Vianna Silva | dbe8154 | 2024-05-21 11:09:15 | [diff] [blame] | 2987 | for fin input_api.AffectedFiles(file_filter=file_filter): |
| 2988 | for line_num, linein f.ChangedContents(): |
| 2989 | for ban_rulein _DEPRECATED_SYNC_CONSENT_CPP_FUNCTIONS: |
| 2990 | CheckForMatch(f, line_num, line, ban_rule) |
| 2991 | |
| 2992 | file_filter=lambda f: f.LocalPath().endswith(('.java')) |
| 2993 | for fin input_api.AffectedFiles(file_filter=file_filter): |
| 2994 | for line_num, linein f.ChangedContents(): |
| 2995 | for ban_rulein _DEPRECATED_SYNC_CONSENT_JAVA_FUNCTIONS: |
| 2996 | CheckForMatch(f, line_num, line, ban_rule) |
| 2997 | |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 2998 | file_filter=lambda f: f.LocalPath().endswith(('.mojom')) |
| 2999 | for fin input_api.AffectedFiles(file_filter=file_filter): |
| 3000 | for line_num, linein f.ChangedContents(): |
| 3001 | for ban_rulein _BANNED_MOJOM_PATTERNS: |
| 3002 | CheckForMatch(f, line_num, line, ban_rule) |
| 3003 | |
Ben Pastene | e79d6611 | 2025-04-23 19:46:15 | [diff] [blame] | 3004 | return results |
Daniel Cheng | 92c15e3 | 2022-03-16 17:48:22 | [diff] [blame] | 3005 | |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 3006 | |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 3007 | def_CheckAndroidNoBannedImports(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3008 | """Make sure that banned java imports are not used.""" |
| 3009 | errors=[] |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 3010 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3011 | file_filter=lambda f: f.LocalPath().endswith(('.java')) |
| 3012 | for fin input_api.AffectedFiles(file_filter=file_filter): |
| 3013 | for line_num, linein f.ChangedContents(): |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3014 | for ban_rulein _BANNED_JAVA_IMPORTS: |
| 3015 | # Consider merging this into the above function. There is no |
| 3016 | # real difference anymore other than helping with a little |
| 3017 | # bit of boilerplate text. Doing so means things like |
| 3018 | # `treat_as_error` will also be uniformly handled. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3019 | problems=_GetMessageForMatchingType(input_api, f, line_num, |
Daniel Cheng | a44a1bcd | 2022-03-15 20:00:15 | [diff] [blame] | 3020 | line, ban_rule) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3021 | if problems: |
| 3022 | errors.extend(problems) |
| 3023 | result=[] |
| 3024 | if(errors): |
| 3025 | result.append( |
| 3026 | output_api.PresubmitError('Banned imports were used.\n'+ |
| 3027 | '\n'.join(errors))) |
| 3028 | return result |
Michael Thiessen | 4445764 | 2020-02-06 00:24:15 | [diff] [blame] | 3029 | |
| 3030 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3031 | defCheckNoPragmaOnce(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3032 | """Make sure that banned functions are not used.""" |
| 3033 | files=[] |
| 3034 | pattern= input_api.re.compile(r'^#pragma\s+once', input_api.re.MULTILINE) |
| 3035 | for fin input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 3036 | ifnot f.LocalPath().endswith('.h'): |
| 3037 | continue |
Bruce Dawson | 4c4c292 | 2022-05-02 18:07:33 | [diff] [blame] | 3038 | if f.LocalPath().endswith('com_imported_mstscax.h'): |
| 3039 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3040 | contents= input_api.ReadFile(f) |
| 3041 | if pattern.search(contents): |
| 3042 | files.append(f) |
dcheng@chromium.org | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 3043 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3044 | if files: |
| 3045 | return[ |
| 3046 | output_api.PresubmitError( |
| 3047 | 'Do not use #pragma once in header files.\n' |
| 3048 | 'See http://www.chromium.org/developers/coding-style#TOC-File-headers', |
| 3049 | files) |
| 3050 | ] |
| 3051 | return[] |
dcheng@chromium.org | 6c063c6 | 2012-07-11 19:11:06 | [diff] [blame] | 3052 | |
avi@chromium.org | 127f18ec | 2012-06-16 05:05:59 | [diff] [blame] | 3053 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3054 | defCheckNoTrinaryTrueFalse(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3055 | """Checks to make sure we don't introduce use of foo ? true : false.""" |
| 3056 | problems=[] |
| 3057 | pattern= input_api.re.compile(r'\?\s*(true|false)\s*:\s*(true|false)') |
| 3058 | for fin input_api.AffectedFiles(): |
| 3059 | ifnot f.LocalPath().endswith(('.cc','.h','.inl','.m','.mm')): |
| 3060 | continue |
thestig@chromium.org | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 3061 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3062 | for line_num, linein f.ChangedContents(): |
| 3063 | if pattern.match(line): |
| 3064 | problems.append(' %s:%d'%(f.LocalPath(), line_num)) |
thestig@chromium.org | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 3065 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3066 | ifnot problems: |
| 3067 | return[] |
| 3068 | return[ |
| 3069 | output_api.PresubmitPromptWarning( |
| 3070 | 'Please consider avoiding the "? true : false" pattern if possible.\n' |
| 3071 | +'\n'.join(problems)) |
| 3072 | ] |
thestig@chromium.org | e747905 | 2012-09-19 00:26:12 | [diff] [blame] | 3073 | |
| 3074 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3075 | defCheckUnwantedDependencies(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3076 | """Runs checkdeps on #include and import statements added in this |
| 3077 | change. Breaking - rules is an error, breaking ! rules is a |
| 3078 | warning. |
| 3079 | """ |
| 3080 | # Return early if no relevant file types were modified. |
| 3081 | for fin input_api.AffectedFiles(): |
| 3082 | path= f.LocalPath() |
| 3083 | if(_IsCPlusPlusFile(input_api, path)or_IsProtoFile(input_api, path) |
| 3084 | or_IsJavaFile(input_api, path)): |
| 3085 | break |
joi@chromium.org | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 3086 | else: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3087 | return[] |
rhalavati | 08acd23 | 2017-04-03 07:23:28 | [diff] [blame] | 3088 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3089 | import sys |
| 3090 | # We need to wait until we have an input_api object and use this |
| 3091 | # roundabout construct to import checkdeps because this file is |
| 3092 | # eval-ed and thus doesn't have __file__. |
| 3093 | original_sys_path= sys.path |
| 3094 | try: |
| 3095 | sys.path= sys.path+[ |
| 3096 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 3097 | 'buildtools','checkdeps') |
| 3098 | ] |
| 3099 | import checkdeps |
| 3100 | from rulesimportRule |
| 3101 | finally: |
| 3102 | # Restore sys.path to what it was before. |
| 3103 | sys.path= original_sys_path |
joi@chromium.org | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 3104 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3105 | added_includes=[] |
| 3106 | added_imports=[] |
| 3107 | added_java_imports=[] |
| 3108 | for fin input_api.AffectedFiles(): |
| 3109 | if_IsCPlusPlusFile(input_api, f.LocalPath()): |
| 3110 | changed_lines=[linefor _, linein f.ChangedContents()] |
| 3111 | added_includes.append([f.AbsoluteLocalPath(), changed_lines]) |
| 3112 | elif_IsProtoFile(input_api, f.LocalPath()): |
| 3113 | changed_lines=[linefor _, linein f.ChangedContents()] |
| 3114 | added_imports.append([f.AbsoluteLocalPath(), changed_lines]) |
| 3115 | elif_IsJavaFile(input_api, f.LocalPath()): |
| 3116 | changed_lines=[linefor _, linein f.ChangedContents()] |
| 3117 | added_java_imports.append([f.AbsoluteLocalPath(), changed_lines]) |
Jinsuk Kim | 5a09267 | 2017-10-24 22:42:24 | [diff] [blame] | 3118 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3119 | deps_checker= checkdeps.DepsChecker(input_api.PresubmitLocalPath()) |
| 3120 | |
| 3121 | error_descriptions=[] |
| 3122 | warning_descriptions=[] |
| 3123 | error_subjects= set() |
| 3124 | warning_subjects= set() |
| 3125 | |
| 3126 | for path, rule_type, rule_descriptionin deps_checker.CheckAddedCppIncludes( |
| 3127 | added_includes): |
| 3128 | path= input_api.os_path.relpath(path, input_api.PresubmitLocalPath()) |
| 3129 | description_with_path='%s\n %s'%(path, rule_description) |
| 3130 | if rule_type==Rule.DISALLOW: |
| 3131 | error_descriptions.append(description_with_path) |
| 3132 | error_subjects.add("#includes") |
| 3133 | else: |
| 3134 | warning_descriptions.append(description_with_path) |
| 3135 | warning_subjects.add("#includes") |
| 3136 | |
| 3137 | for path, rule_type, rule_descriptionin deps_checker.CheckAddedProtoImports( |
| 3138 | added_imports): |
| 3139 | path= input_api.os_path.relpath(path, input_api.PresubmitLocalPath()) |
| 3140 | description_with_path='%s\n %s'%(path, rule_description) |
| 3141 | if rule_type==Rule.DISALLOW: |
| 3142 | error_descriptions.append(description_with_path) |
| 3143 | error_subjects.add("imports") |
| 3144 | else: |
| 3145 | warning_descriptions.append(description_with_path) |
| 3146 | warning_subjects.add("imports") |
| 3147 | |
| 3148 | for path, rule_type, rule_descriptionin deps_checker.CheckAddedJavaImports( |
| 3149 | added_java_imports, _JAVA_MULTIPLE_DEFINITION_EXCLUDED_PATHS): |
| 3150 | path= input_api.os_path.relpath(path, input_api.PresubmitLocalPath()) |
| 3151 | description_with_path='%s\n %s'%(path, rule_description) |
| 3152 | if rule_type==Rule.DISALLOW: |
| 3153 | error_descriptions.append(description_with_path) |
| 3154 | error_subjects.add("imports") |
| 3155 | else: |
| 3156 | warning_descriptions.append(description_with_path) |
| 3157 | warning_subjects.add("imports") |
| 3158 | |
| 3159 | results=[] |
| 3160 | if error_descriptions: |
| 3161 | results.append( |
| 3162 | output_api.PresubmitError( |
| 3163 | 'You added one or more %s that violate checkdeps rules.'% |
| 3164 | " and ".join(error_subjects), error_descriptions)) |
| 3165 | if warning_descriptions: |
| 3166 | results.append( |
| 3167 | output_api.PresubmitPromptOrNotify( |
| 3168 | 'You added one or more %s of files that are temporarily\n' |
| 3169 | 'allowed but being removed. Can you avoid introducing the\n' |
| 3170 | '%s? See relevant DEPS file(s) for details and contacts.'% |
| 3171 | (" and ".join(warning_subjects),"/".join(warning_subjects)), |
| 3172 | warning_descriptions)) |
| 3173 | return results |
joi@chromium.org | 55f9f38 | 2012-07-31 11:02:18 | [diff] [blame] | 3174 | |
| 3175 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3176 | defCheckFilePermissions(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3177 | """Check that all files have their permissions properly set.""" |
| 3178 | if input_api.platform=='win32': |
| 3179 | return[] |
| 3180 | checkperms_tool= input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 3181 | 'tools','checkperms', |
| 3182 | 'checkperms.py') |
| 3183 | args=[ |
Bruce Dawson | 8a43cf7 | 2022-05-13 17:10:32 | [diff] [blame] | 3184 | input_api.python3_executable, checkperms_tool,'--root', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3185 | input_api.change.RepositoryRoot() |
| 3186 | ] |
| 3187 | with input_api.CreateTemporaryFile()as file_list: |
| 3188 | for fin input_api.AffectedFiles(): |
| 3189 | # checkperms.py file/directory arguments must be relative to the |
| 3190 | # repository. |
| 3191 | file_list.write((f.LocalPath()+'\n').encode('utf8')) |
| 3192 | file_list.close() |
| 3193 | args+=['--file-list', file_list.name] |
| 3194 | try: |
| 3195 | input_api.subprocess.check_output(args) |
| 3196 | return[] |
| 3197 | except input_api.subprocess.CalledProcessErroras error: |
| 3198 | return[ |
| 3199 | output_api.PresubmitError('checkperms.py failed:', |
| 3200 | long_text=error.output.decode( |
| 3201 | 'utf-8','ignore')) |
| 3202 | ] |
csharp@chromium.org | fbcafe5a | 2012-08-08 15:31:22 | [diff] [blame] | 3203 | |
| 3204 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3205 | defCheckNoAuraWindowPropertyHInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3206 | """Makes sure we don't include ui/aura/window_property.h |
| 3207 | in header files. |
| 3208 | """ |
| 3209 | pattern= input_api.re.compile(r'^#include\s*"ui/aura/window_property.h"') |
| 3210 | errors=[] |
| 3211 | for fin input_api.AffectedFiles(): |
| 3212 | ifnot f.LocalPath().endswith('.h'): |
| 3213 | continue |
| 3214 | for line_num, linein f.ChangedContents(): |
| 3215 | if pattern.match(line): |
| 3216 | errors.append(' %s:%d'%(f.LocalPath(), line_num)) |
oshima@chromium.org | c8278b3 | 2012-10-30 20:35:49 | [diff] [blame] | 3217 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3218 | results=[] |
| 3219 | if errors: |
| 3220 | results.append( |
| 3221 | output_api.PresubmitError( |
| 3222 | 'Header files should not include ui/aura/window_property.h', |
| 3223 | errors)) |
| 3224 | return results |
oshima@chromium.org | c8278b3 | 2012-10-30 20:35:49 | [diff] [blame] | 3225 | |
| 3226 | |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 3227 | defCheckNoInternalHeapIncludes(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3228 | """Makes sure we don't include any headers from |
| 3229 | third_party/blink/renderer/platform/heap/impl or |
| 3230 | third_party/blink/renderer/platform/heap/v8_wrapper from files outside of |
| 3231 | third_party/blink/renderer/platform/heap |
| 3232 | """ |
| 3233 | impl_pattern= input_api.re.compile( |
| 3234 | r'^\s*#include\s*"third_party/blink/renderer/platform/heap/impl/.*"') |
| 3235 | v8_wrapper_pattern= input_api.re.compile( |
| 3236 | r'^\s*#include\s*"third_party/blink/renderer/platform/heap/v8_wrapper/.*"' |
| 3237 | ) |
| 3238 | file_filter=lambda f:not input_api.re.match( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3239 | r"^third_party/blink/renderer/platform/heap/.*", f.UnixLocalPath()) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3240 | errors=[] |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 3241 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3242 | for fin input_api.AffectedFiles(file_filter=file_filter): |
| 3243 | for line_num, linein f.ChangedContents(): |
| 3244 | if impl_pattern.match(line)or v8_wrapper_pattern.match(line): |
| 3245 | errors.append(' %s:%d'%(f.LocalPath(), line_num)) |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 3246 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3247 | results=[] |
| 3248 | if errors: |
| 3249 | results.append( |
| 3250 | output_api.PresubmitError( |
| 3251 | 'Do not include files from third_party/blink/renderer/platform/heap/impl' |
| 3252 | ' or third_party/blink/renderer/platform/heap/v8_wrapper. Use the ' |
| 3253 | 'relevant counterparts from third_party/blink/renderer/platform/heap', |
| 3254 | errors)) |
| 3255 | return results |
Omer Katz | cc77ea9 | 2021-04-26 10:23:28 | [diff] [blame] | 3256 | |
| 3257 | |
dbeam@chromium.org | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3258 | def_CheckForVersionControlConflictsInFile(input_api, f): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3259 | pattern= input_api.re.compile('^(?:<<<<<<<|>>>>>>>) |^=======$') |
| 3260 | errors=[] |
| 3261 | for line_num, linein f.ChangedContents(): |
| 3262 | if f.LocalPath().endswith(('.md','.rst','.txt')): |
| 3263 | # First-level headers in markdown look a lot like version control |
| 3264 | # conflict markers. http://daringfireball.net/projects/markdown/basics |
| 3265 | continue |
| 3266 | if pattern.match(line): |
| 3267 | errors.append(' %s:%d %s'%(f.LocalPath(), line_num, line)) |
| 3268 | return errors |
dbeam@chromium.org | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3269 | |
| 3270 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3271 | defCheckForVersionControlConflicts(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3272 | """Usually this is not intentional and will cause a compile failure.""" |
| 3273 | errors=[] |
| 3274 | for fin input_api.AffectedFiles(): |
| 3275 | errors.extend(_CheckForVersionControlConflictsInFile(input_api, f)) |
dbeam@chromium.org | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3276 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3277 | results=[] |
| 3278 | if errors: |
| 3279 | results.append( |
| 3280 | output_api.PresubmitError( |
| 3281 | 'Version control conflict markers found, please resolve.', |
| 3282 | errors)) |
| 3283 | return results |
dbeam@chromium.org | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3284 | |
Wei-Yin Chen (陳威尹) | f799d44 | 2018-07-31 02:20:20 | [diff] [blame] | 3285 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3286 | defCheckGoogleSupportAnswerUrlOnUpload(input_api, output_api): |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 3287 | pattern= input_api.re.compile(r'support\.google\.com\/chrome.*/answer') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3288 | errors=[] |
| 3289 | for fin input_api.AffectedFiles(): |
| 3290 | for line_num, linein f.ChangedContents(): |
| 3291 | if pattern.search(line): |
| 3292 | errors.append(' %s:%d %s'%(f.LocalPath(), line_num, line)) |
estade | e17314a0 | 2017-01-12 16:22:16 | [diff] [blame] | 3293 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3294 | results=[] |
| 3295 | if errors: |
| 3296 | results.append( |
| 3297 | output_api.PresubmitPromptWarning( |
| 3298 | 'Found Google support URL addressed by answer number. Please replace ' |
| 3299 | 'with a p= identifier instead. See crbug.com/679462\n', |
| 3300 | errors)) |
| 3301 | return results |
estade | e17314a0 | 2017-01-12 16:22:16 | [diff] [blame] | 3302 | |
dbeam@chromium.org | 70ca7775 | 2012-11-20 03:45:03 | [diff] [blame] | 3303 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3304 | defCheckHardcodedGoogleHostsInLowerLayers(input_api, output_api): |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3305 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3306 | defFilterFile(affected_file): |
| 3307 | """Filter function for use with input_api.AffectedSourceFiles, |
| 3308 | below. This filters out everything except non-test files from |
| 3309 | top-level directories that generally speaking should not hard-code |
| 3310 | service URLs (e.g. src/android_webview/, src/content/ and others). |
| 3311 | """ |
| 3312 | return input_api.FilterSourceFile( |
| 3313 | affected_file, |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3314 | files_to_check=[r'^(android_webview|base|content|net)/.*'], |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3315 | files_to_skip=(_EXCLUDED_PATHS+ _TEST_CODE_EXCLUDED_PATHS+ |
| 3316 | input_api.DEFAULT_FILES_TO_SKIP)) |
joi@chromium.org | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 3317 | |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 3318 | base_pattern=(r'"[^"]*(google|googleapis|googlezip|googledrive|appspot)' |
| 3319 | r'\.(com|net)[^"]*"') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3320 | comment_pattern= input_api.re.compile('//.*%s'% base_pattern) |
| 3321 | pattern= input_api.re.compile(base_pattern) |
| 3322 | problems=[]# items are (filename, line_number, line) |
| 3323 | for fin input_api.AffectedSourceFiles(FilterFile): |
| 3324 | for line_num, linein f.ChangedContents(): |
| 3325 | ifnot comment_pattern.search(line)and pattern.search(line): |
| 3326 | problems.append((f.LocalPath(), line_num, line)) |
joi@chromium.org | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 3327 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3328 | if problems: |
| 3329 | return[ |
| 3330 | output_api.PresubmitPromptOrNotify( |
| 3331 | 'Most layers below src/chrome/ should not hardcode service URLs.\n' |
| 3332 | 'Are you sure this is correct?',[ |
| 3333 | ' %s:%d: %s'%(problem[0], problem[1], problem[2]) |
| 3334 | for problemin problems |
| 3335 | ]) |
| 3336 | ] |
| 3337 | else: |
| 3338 | return[] |
joi@chromium.org | 06e6d0ff | 2012-12-11 01:36:44 | [diff] [blame] | 3339 | |
| 3340 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3341 | defCheckChromeOsSyncedPrefRegistration(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3342 | """Warns if Chrome OS C++ files register syncable prefs as browser prefs.""" |
James Cook | 6b6597c | 2019-11-06 22:05:29 | [diff] [blame] | 3343 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3344 | defFileFilter(affected_file): |
| 3345 | """Includes directories known to be Chrome OS only.""" |
| 3346 | return input_api.FilterSourceFile( |
| 3347 | affected_file, |
| 3348 | files_to_check=( |
| 3349 | '^ash/', |
| 3350 | '^chromeos/',# Top-level src/chromeos. |
| 3351 | '.*/chromeos/',# Any path component. |
| 3352 | '^components/arc', |
| 3353 | '^components/exo'), |
| 3354 | files_to_skip=(input_api.DEFAULT_FILES_TO_SKIP)) |
James Cook | 6b6597c | 2019-11-06 22:05:29 | [diff] [blame] | 3355 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3356 | prefs=[] |
| 3357 | priority_prefs=[] |
| 3358 | for fin input_api.AffectedFiles(file_filter=FileFilter): |
| 3359 | for line_num, linein f.ChangedContents(): |
| 3360 | if input_api.re.search('PrefRegistrySyncable::SYNCABLE_PREF', |
| 3361 | line): |
| 3362 | prefs.append(' %s:%d:'%(f.LocalPath(), line_num)) |
| 3363 | prefs.append(' %s'% line) |
| 3364 | if input_api.re.search( |
| 3365 | 'PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF', line): |
| 3366 | priority_prefs.append(' %s:%d'%(f.LocalPath(), line_num)) |
| 3367 | priority_prefs.append(' %s'% line) |
| 3368 | |
| 3369 | results=[] |
| 3370 | if(prefs): |
| 3371 | results.append( |
| 3372 | output_api.PresubmitPromptWarning( |
| 3373 | 'Preferences were registered as SYNCABLE_PREF and will be controlled ' |
| 3374 | 'by browser sync settings. If these prefs should be controlled by OS ' |
| 3375 | 'sync settings use SYNCABLE_OS_PREF instead.\n'+ |
| 3376 | '\n'.join(prefs))) |
| 3377 | if(priority_prefs): |
| 3378 | results.append( |
| 3379 | output_api.PresubmitPromptWarning( |
| 3380 | 'Preferences were registered as SYNCABLE_PRIORITY_PREF and will be ' |
| 3381 | 'controlled by browser sync settings. If these prefs should be ' |
| 3382 | 'controlled by OS sync settings use SYNCABLE_OS_PRIORITY_PREF ' |
| 3383 | 'instead.\n'+'\n'.join(prefs))) |
| 3384 | return results |
James Cook | 6b6597c | 2019-11-06 22:05:29 | [diff] [blame] | 3385 | |
| 3386 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3387 | defCheckNoAbbreviationInPngFileName(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3388 | """Makes sure there are no abbreviations in the name of PNG files. |
| 3389 | The native_client_sdk directory is excluded because it has auto-generated PNG |
| 3390 | files for documentation. |
| 3391 | """ |
| 3392 | errors=[] |
Yuanqing Zhu | 9eef0283 | 2022-12-04 14:42:17 | [diff] [blame] | 3393 | files_to_check=[r'.*\.png$'] |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3394 | files_to_skip=[ |
| 3395 | r'^native_client_sdk/', |
| 3396 | r'^services/test/', |
| 3397 | r'^third_party/blink/web_tests/', |
| 3398 | ] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3399 | file_filter=lambda f: input_api.FilterSourceFile( |
| 3400 | f, files_to_check=files_to_check, files_to_skip=files_to_skip) |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 3401 | abbreviation= input_api.re.compile(r'.+_[a-z]\.png|.+_[a-z]_.*\.png') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3402 | for fin input_api.AffectedFiles(include_deletes=False, |
| 3403 | file_filter=file_filter): |
Yuanqing Zhu | 9eef0283 | 2022-12-04 14:42:17 | [diff] [blame] | 3404 | file_name= input_api.os_path.split(f.LocalPath())[1] |
| 3405 | if abbreviation.search(file_name): |
| 3406 | errors.append(' %s'% f.LocalPath()) |
oshima@chromium.org | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 3407 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3408 | results=[] |
| 3409 | if errors: |
| 3410 | results.append( |
| 3411 | output_api.PresubmitError( |
| 3412 | 'The name of PNG files should not have abbreviations. \n' |
| 3413 | 'Use _hover.png, _center.png, instead of _h.png, _c.png.\n' |
| 3414 | 'Contact oshima@chromium.org if you have questions.', errors)) |
| 3415 | return results |
oshima@chromium.org | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 3416 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3417 | |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 3418 | defCheckNoProductIconsAddedToPublicRepo(input_api, output_api): |
| 3419 | """Heuristically identifies product icons based on their file name and reminds |
| 3420 | contributors not to add them to the Chromium repository. |
| 3421 | """ |
Peter Kotwicz | f634d07 | 2025-04-28 22:48:15 | [diff] [blame] | 3422 | |
| 3423 | if input_api.change.RepositoryRoot().endswith('clank'): |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3424 | # TODO(crbug.com/414435241): Change check to compute whether change |
| 3425 | # belongs to internal repository instead of relying on string matching. |
| 3426 | return[] |
Peter Kotwicz | f634d07 | 2025-04-28 22:48:15 | [diff] [blame] | 3427 | |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 3428 | errors=[] |
| 3429 | files_to_check=[r'.*google.*\.png$|.*google.*\.svg$|.*google.*\.icon$'] |
| 3430 | file_filter=lambda f: input_api.FilterSourceFile( |
| 3431 | f, files_to_check=files_to_check) |
| 3432 | for fin input_api.AffectedFiles(include_deletes=False, |
| 3433 | file_filter=file_filter): |
| 3434 | errors.append(' %s'% f.LocalPath()) |
| 3435 | |
| 3436 | results=[] |
| 3437 | if errors: |
Bruce Dawson | 3bcf0c9 | 2022-08-12 00:03:08 | [diff] [blame] | 3438 | # Give warnings instead of errors on presubmit --all and presubmit |
| 3439 | # --files. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3440 | message_type=(output_api.PresubmitNotifyResult |
| 3441 | if input_api.no_diffselse output_api.PresubmitError) |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 3442 | results.append( |
Bruce Dawson | 3bcf0c9 | 2022-08-12 00:03:08 | [diff] [blame] | 3443 | message_type( |
Evan Stade | 7cd4a2c | 2022-08-04 23:37:25 | [diff] [blame] | 3444 | 'Trademarked images should not be added to the public repo. ' |
| 3445 | 'See crbug.com/944754', errors)) |
| 3446 | return results |
| 3447 | |
oshima@chromium.org | d253001 | 2013-01-25 16:39:27 | [diff] [blame] | 3448 | |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3449 | def_ExtractAddRulesFromParsedDeps(parsed_deps): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3450 | """Extract the rules that add dependencies from a parsed DEPS file. |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3451 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3452 | Args: |
| 3453 | parsed_deps: the locals dictionary from evaluating the DEPS file.""" |
| 3454 | add_rules= set() |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3455 | add_rules.update([ |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3456 | rule[1:]for rulein parsed_deps.get('include_rules',[]) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3457 | if rule.startswith('+')or rule.startswith('!') |
| 3458 | ]) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3459 | for _, rulesin parsed_deps.get('specific_include_rules',{}).items(): |
| 3460 | add_rules.update([ |
| 3461 | rule[1:]for rulein rules |
| 3462 | if rule.startswith('+')or rule.startswith('!') |
| 3463 | ]) |
| 3464 | return add_rules |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3465 | |
| 3466 | |
| 3467 | def_ParseDeps(contents): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3468 | """Simple helper for parsing DEPS files.""" |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3469 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3470 | # Stubs for handling special syntax in the root DEPS file. |
| 3471 | class_VarImpl: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3472 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3473 | def __init__(self, local_scope): |
| 3474 | self._local_scope= local_scope |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3475 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3476 | defLookup(self, var_name): |
| 3477 | """Implements the Var syntax.""" |
| 3478 | try: |
| 3479 | return self._local_scope['vars'][var_name] |
| 3480 | exceptKeyError: |
| 3481 | raiseException('Var is not defined: %s'% var_name) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3482 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3483 | local_scope={} |
| 3484 | global_scope={ |
| 3485 | 'Var':_VarImpl(local_scope).Lookup, |
| 3486 | 'Str': str, |
| 3487 | } |
Dirk Pranke | 1b9e0638 | 2021-05-14 01:16:22 | [diff] [blame] | 3488 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3489 | exec(contents, global_scope, local_scope) |
| 3490 | return local_scope |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3491 | |
| 3492 | |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3493 | def_FindAllDepsFilesForSubpath(input_api, subpath): |
| 3494 | ret=[] |
| 3495 | while subpath: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3496 | cur= input_api.os_path.join(input_api.change.RepositoryRoot(), |
| 3497 | subpath,'DEPS') |
Joanna Wang | 130e7bdd | 2024-12-10 17:39:03 | [diff] [blame] | 3498 | if input_api.os_path.isfile(cur): |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3499 | ret.append(cur) |
| 3500 | subpath= input_api.os_path.dirname(subpath) |
| 3501 | return ret |
| 3502 | |
| 3503 | |
| 3504 | def_FindAddedDepsThatRequireReview(input_api, depended_on_paths): |
| 3505 | """Filters to those whose DEPS set new_usages_require_review=True""" |
| 3506 | ret= set() |
| 3507 | cache={} |
| 3508 | for target_pathin depended_on_paths: |
| 3509 | for subpathin_FindAllDepsFilesForSubpath(input_api, target_path): |
| 3510 | config= cache.get(subpath) |
| 3511 | if configisNone: |
| 3512 | config=_ParseDeps(input_api.ReadFile(subpath)) |
| 3513 | cache[subpath]= config |
| 3514 | if config.get('new_usages_require_review'): |
| 3515 | ret.add(target_path) |
| 3516 | break |
| 3517 | return ret |
| 3518 | |
| 3519 | |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3520 | def_CalculateAddedDeps(os_path, old_contents, new_contents): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3521 | """Helper method for CheckAddedDepsHaveTargetApprovals. Returns |
| 3522 | a set of DEPS entries that we should look up. |
joi@chromium.org | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 3523 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3524 | For a directory (rather than a specific filename) we fake a path to |
| 3525 | a specific filename by adding /DEPS. This is chosen as a file that |
| 3526 | will seldom or never be subject to per-file include_rules. |
| 3527 | """ |
| 3528 | # We ignore deps entries on auto-generated directories. |
| 3529 | AUTO_GENERATED_DIRS=['grit','jni'] |
tony@chromium.org | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 3530 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3531 | old_deps=_ExtractAddRulesFromParsedDeps(_ParseDeps(old_contents)) |
| 3532 | new_deps=_ExtractAddRulesFromParsedDeps(_ParseDeps(new_contents)) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3533 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3534 | added_deps= new_deps.difference(old_deps) |
Daniel Cheng | 4dcdb6b | 2017-04-13 08:30:17 | [diff] [blame] | 3535 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3536 | results= set() |
| 3537 | for added_depin added_deps: |
| 3538 | if added_dep.split('/')[0]in AUTO_GENERATED_DIRS: |
| 3539 | continue |
| 3540 | # Assume that a rule that ends in .h is a rule for a specific file. |
| 3541 | if added_dep.endswith('.h'): |
| 3542 | results.add(added_dep) |
| 3543 | else: |
| 3544 | results.add(os_path.join(added_dep,'DEPS')) |
| 3545 | return results |
tony@chromium.org | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 3546 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3547 | |
Stephanie Kim | ec4f55a | 2024-04-24 16:54:02 | [diff] [blame] | 3548 | defCheckForNewDEPSDownloadFromGoogleStorageHooks(input_api, output_api): |
| 3549 | """Checks that there are no new download_from_google_storage hooks""" |
| 3550 | for fin input_api.AffectedFiles(include_deletes=False): |
| 3551 | if f.LocalPath()=='DEPS': |
| 3552 | old_hooks=_ParseDeps('\n'.join(f.OldContents()))['hooks'] |
| 3553 | new_hooks=_ParseDeps('\n'.join(f.NewContents()))['hooks'] |
| 3554 | old_name_to_hook={hook['name']: hookfor hookin old_hooks} |
| 3555 | new_name_to_hook={hook['name']: hookfor hookin new_hooks} |
| 3556 | added_hook_names= set(new_name_to_hook.keys())- set( |
| 3557 | old_name_to_hook.keys()) |
| 3558 | ifnot added_hook_names: |
| 3559 | return[] |
| 3560 | new_download_from_google_storage_hooks=[] |
| 3561 | for new_hookin added_hook_names: |
| 3562 | hook= new_name_to_hook[new_hook] |
| 3563 | action_cmd= hook['action'] |
| 3564 | if any('download_from_google_storage'in arg |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3565 | for argin action_cmd): |
Stephanie Kim | ec4f55a | 2024-04-24 16:54:02 | [diff] [blame] | 3566 | new_download_from_google_storage_hooks.append(new_hook) |
| 3567 | if new_download_from_google_storage_hooks: |
| 3568 | return[ |
| 3569 | output_api.PresubmitError( |
| 3570 | 'Please do not add new download_from_google_storage ' |
| 3571 | 'hooks. Instead, add a `gcs` dep_type entry to `deps`. ' |
| 3572 | 'See https://chromium.googlesource.com/chromium/src.git' |
| 3573 | '/+/refs/heads/main/docs/gcs_dependencies.md for more ' |
| 3574 | 'info. Added hooks:', |
| 3575 | items=new_download_from_google_storage_hooks) |
| 3576 | ] |
| 3577 | return[] |
| 3578 | |
tony@chromium.org | f32e2d1e | 2013-07-26 21:39:08 | [diff] [blame] | 3579 | |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3580 | defCheckEachPerfettoTestDataFileHasDepsEntry(input_api, output_api): |
| 3581 | test_data_filter=lambda f: input_api.FilterSourceFile( |
Rasika Navarange | 08e54216 | 2024-05-31 13:31:26 | [diff] [blame] | 3582 | f, files_to_check=[r'^base/tracing/test/data_sha256/.*\.sha256']) |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3583 | ifnot any(input_api.AffectedFiles(file_filter=test_data_filter)): |
| 3584 | return[] |
| 3585 | |
| 3586 | # Find DEPS entry |
| 3587 | deps_entry=[] |
Rasika Navarange | 277cd66 | 2024-06-04 10:14:59 | [diff] [blame] | 3588 | old_deps_entry=[] |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3589 | for fin input_api.AffectedFiles(include_deletes=False): |
| 3590 | if f.LocalPath()=='DEPS': |
| 3591 | new_deps=_ParseDeps('\n'.join(f.NewContents()))['deps'] |
| 3592 | deps_entry= new_deps['src/base/tracing/test/data'] |
Rasika Navarange | 277cd66 | 2024-06-04 10:14:59 | [diff] [blame] | 3593 | old_deps=_ParseDeps('\n'.join(f.OldContents()))['deps'] |
| 3594 | old_deps_entry= old_deps['src/base/tracing/test/data'] |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3595 | ifnot deps_entry: |
Rasika Navarange | 08e54216 | 2024-05-31 13:31:26 | [diff] [blame] | 3596 | # TODO(312895063):Add back error when .sha256 files have been moved. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3597 | return[ |
| 3598 | output_api.PresubmitError( |
| 3599 | 'You must update the DEPS file when you update a ' |
| 3600 | '.sha256 file in base/tracing/test/data_sha256') |
| 3601 | ] |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3602 | |
| 3603 | output=[] |
| 3604 | for fin input_api.AffectedFiles(file_filter=test_data_filter): |
| 3605 | objects= deps_entry['objects'] |
| 3606 | ifnot f.NewContents(): |
| 3607 | # Deleted file so check that DEPS entry removed |
| 3608 | sha256_from_file= f.OldContents()[0] |
| 3609 | object_entry= next( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3610 | (item |
| 3611 | for itemin objectsif item["sha256sum"]== sha256_from_file), |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3612 | None) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3613 | old_entry= next((itemfor itemin old_deps_entry['objects'] |
| 3614 | if item["sha256sum"]== sha256_from_file),None) |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3615 | if object_entry: |
Rasika Navarange | 277cd66 | 2024-06-04 10:14:59 | [diff] [blame] | 3616 | # Allow renaming of objects with the same hash |
| 3617 | if object_entry['object_name']!= old_entry['object_name']: |
| 3618 | continue |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3619 | output.append( |
| 3620 | output_api.PresubmitError( |
| 3621 | 'You deleted %s so you must also remove the corresponding DEPS entry.' |
| 3622 | % f.LocalPath())) |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3623 | continue |
| 3624 | |
| 3625 | sha256_from_file= f.NewContents()[0] |
| 3626 | object_entry= next( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3627 | (item |
| 3628 | for itemin objectsif item["sha256sum"]== sha256_from_file), |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3629 | None) |
| 3630 | ifnot object_entry: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3631 | output.append( |
| 3632 | output_api.PresubmitError( |
| 3633 | 'No corresponding DEPS entry found for %s. ' |
| 3634 | 'Run `base/tracing/test/test_data.py get_deps --filepath %s` ' |
| 3635 | 'to generate the DEPS entry.'% |
| 3636 | (f.LocalPath(), f.LocalPath()))) |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3637 | |
| 3638 | if output: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3639 | output.append( |
| 3640 | output_api.PresubmitError( |
| 3641 | 'The DEPS entry for `src/base/tracing/test/data` in the DEPS file has not been ' |
| 3642 | 'updated properly. Run `base/tracing/test/test_data.py get_all_deps` to see what ' |
| 3643 | 'the DEPS entry should look like.')) |
Rasika Navarange | c2d33d2 | 2024-05-23 15:19:02 | [diff] [blame] | 3644 | return output |
| 3645 | |
| 3646 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3647 | defCheckAddedDepsHaveTargetApprovals(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3648 | """When a dependency prefixed with + is added to a DEPS file, we |
| 3649 | want to make sure that the change is reviewed by an OWNER of the |
| 3650 | target file or directory, to avoid layering violations from being |
| 3651 | introduced. This check verifies that this happens. |
| 3652 | """ |
| 3653 | # We rely on Gerrit's code-owners to check approvals. |
| 3654 | # input_api.gerrit is always set for Chromium, but other projects |
| 3655 | # might not use Gerrit. |
Bruce Dawson | 344ab26 | 2022-06-04 11:35:10 | [diff] [blame] | 3656 | ifnot input_api.gerritor input_api.no_diffs: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3657 | return[] |
Bruce Dawson | b357aeb | 2022-08-09 15:38:30 | [diff] [blame] | 3658 | if'PRESUBMIT_SKIP_NETWORK'in input_api.environ: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3659 | return[] |
Bruce Dawson | b357aeb | 2022-08-09 15:38:30 | [diff] [blame] | 3660 | try: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3661 | if(input_api.change.issue |
| 3662 | and input_api.gerrit.IsOwnersOverrideApproved( |
| 3663 | input_api.change.issue)): |
Bruce Dawson | b357aeb | 2022-08-09 15:38:30 | [diff] [blame] | 3664 | # Skip OWNERS check when Owners-Override label is approved. This is |
| 3665 | # intended for global owners, trusted bots, and on-call sheriffs. |
| 3666 | # Review is still required for these changes. |
| 3667 | return[] |
| 3668 | exceptExceptionas e: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3669 | return[ |
| 3670 | output_api.PresubmitPromptWarning( |
| 3671 | 'Failed to retrieve owner override status - %s'% str(e)) |
| 3672 | ] |
Edward Lesmes | 6fba5108 | 2021-01-20 04:20:23 | [diff] [blame] | 3673 | |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3674 | # A set of paths (that might not exist) that are being added as DEPS |
| 3675 | # (via lines like "+foo/bar/baz"). |
| 3676 | depended_on_paths= set() |
jochen | 53efcdd | 2016-01-29 05:09:24 | [diff] [blame] | 3677 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3678 | file_filter=lambda f:not input_api.re.match(r"^third_party/blink/.*", |
| 3679 | f.UnixLocalPath()) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3680 | for fin input_api.AffectedFiles(include_deletes=False, |
| 3681 | file_filter=file_filter): |
| 3682 | filename= input_api.os_path.basename(f.LocalPath()) |
| 3683 | if filename=='DEPS': |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3684 | depended_on_paths.update( |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3685 | _CalculateAddedDeps(input_api.os_path, |
| 3686 | '\n'.join(f.OldContents()), |
| 3687 | '\n'.join(f.NewContents()))) |
joi@chromium.org | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3688 | |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3689 | # Requiring reviews is opt-in as of https://crbug.com/365797506 |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3690 | depended_on_paths=_FindAddedDepsThatRequireReview( |
| 3691 | input_api, depended_on_paths) |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3692 | ifnot depended_on_paths: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3693 | return[] |
joi@chromium.org | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3694 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3695 | if input_api.is_committing: |
| 3696 | if input_api.tbr: |
| 3697 | return[ |
| 3698 | output_api.PresubmitNotifyResult( |
| 3699 | '--tbr was specified, skipping OWNERS check for DEPS additions' |
| 3700 | ) |
| 3701 | ] |
Daniel Cheng | 3008dc1 | 2022-05-13 04:02:11 | [diff] [blame] | 3702 | # TODO(dcheng): Make this generate an error on dry runs if the reviewer |
| 3703 | # is not added, to prevent review serialization. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3704 | if input_api.dry_run: |
| 3705 | return[ |
| 3706 | output_api.PresubmitNotifyResult( |
| 3707 | 'This is a dry run, skipping OWNERS check for DEPS additions' |
| 3708 | ) |
| 3709 | ] |
| 3710 | ifnot input_api.change.issue: |
| 3711 | return[ |
| 3712 | output_api.PresubmitError( |
| 3713 | "DEPS approval by OWNERS check failed: this change has " |
| 3714 | "no change number, so we can't check it for approvals.") |
| 3715 | ] |
| 3716 | output= output_api.PresubmitError |
joi@chromium.org | 14a6131c | 2014-01-08 01:15:41 | [diff] [blame] | 3717 | else: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3718 | output= output_api.PresubmitNotifyResult |
joi@chromium.org | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3719 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3720 | owner_email, reviewers=( |
| 3721 | input_api.canned_checks.GetCodereviewOwnerAndReviewers( |
| 3722 | input_api,None, approval_needed=input_api.is_committing)) |
joi@chromium.org | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3723 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3724 | owner_email= owner_emailor input_api.change.author_email |
| 3725 | |
| 3726 | approval_status= input_api.owners_client.GetFilesApprovalStatus( |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3727 | depended_on_paths, reviewers.union([owner_email]),[]) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3728 | missing_files=[ |
Andrew Grieve | b77ac76 | 2024-11-29 15:01:48 | [diff] [blame] | 3729 | pfor pin depended_on_paths |
| 3730 | if approval_status[p]!= input_api.owners_client.APPROVED |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3731 | ] |
| 3732 | |
| 3733 | # We strip the /DEPS part that was added by |
| 3734 | # _FilesToCheckForIncomingDeps to fake a path to a file in a |
| 3735 | # directory. |
| 3736 | defStripDeps(path): |
| 3737 | start_deps= path.rfind('/DEPS') |
| 3738 | if start_deps!=-1: |
| 3739 | return path[:start_deps] |
| 3740 | else: |
| 3741 | return path |
| 3742 | |
Scott Lee | bf6a094 | 2024-06-26 22:59:39 | [diff] [blame] | 3743 | submodule_paths= set(input_api.ListSubmodules()) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 3744 | |
Scott Lee | bf6a094 | 2024-06-26 22:59:39 | [diff] [blame] | 3745 | def is_from_submodules(path, submodule_paths): |
| 3746 | path= input_api.os_path.normpath(path) |
| 3747 | while path: |
| 3748 | if pathin submodule_paths: |
| 3749 | returnTrue |
| 3750 | |
| 3751 | # All deps should be a relative path from the checkout. |
| 3752 | # i.e., shouldn't start with "/" or "c:\", for example. |
| 3753 | # |
| 3754 | # That said, this is to prevent an infinite loop, just in case |
| 3755 | # an input dep path starts with "/", because |
| 3756 | # os.path.dirname("/") => "/" |
| 3757 | parent= input_api.os_path.dirname(path) |
| 3758 | if parent== path: |
| 3759 | break |
| 3760 | path= parent |
| 3761 | |
| 3762 | returnFalse |
| 3763 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3764 | unapproved_dependencies=[ |
| 3765 | "'+%s',"%StripDeps(path)for pathin missing_files |
Scott Lee | bf6a094 | 2024-06-26 22:59:39 | [diff] [blame] | 3766 | # if a newly added dep is from a submodule, it becomes trickier |
| 3767 | # to get suggested owners, especially it is from a different host. |
| 3768 | # |
| 3769 | # skip the review enforcement for cross-repo deps. |
| 3770 | ifnot is_from_submodules(path, submodule_paths) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3771 | ] |
| 3772 | |
| 3773 | if unapproved_dependencies: |
| 3774 | output_list=[ |
| 3775 | output( |
| 3776 | 'You need LGTM from owners of depends-on paths in DEPS that were ' |
| 3777 | 'modified in this CL:\n %s'% |
| 3778 | '\n '.join(sorted(unapproved_dependencies))) |
| 3779 | ] |
| 3780 | suggested_owners= input_api.owners_client.SuggestOwners( |
| 3781 | missing_files, exclude=[owner_email]) |
| 3782 | output_list.append( |
| 3783 | output('Suggested missing target path OWNERS:\n %s'% |
| 3784 | '\n '.join(suggested_ownersor[]))) |
| 3785 | return output_list |
| 3786 | |
| 3787 | return[] |
joi@chromium.org | e871964c | 2013-05-13 14:14:55 | [diff] [blame] | 3788 | |
| 3789 | |
Wei-Yin Chen (陳威尹) | dca729a | 2018-07-31 21:35:49 | [diff] [blame] | 3790 | # TODO: add unit tests. |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3791 | defCheckSpamLogging(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3792 | file_inclusion_pattern=[r'.+%s'% _IMPLEMENTATION_EXTENSIONS] |
| 3793 | files_to_skip=( |
| 3794 | _EXCLUDED_PATHS+ _TEST_CODE_EXCLUDED_PATHS+ |
| 3795 | input_api.DEFAULT_FILES_TO_SKIP+( |
Jaewon Jung | 2f323bb | 2022-12-07 23:55:01 | [diff] [blame] | 3796 | r"^base/fuchsia/scoped_fx_logger\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3797 | r"^base/logging\.h$", |
| 3798 | r"^base/logging\.cc$", |
| 3799 | r"^base/task/thread_pool/task_tracker\.cc$", |
| 3800 | r"^chrome/app/chrome_main_delegate\.cc$", |
Yao Li | 359937b | 2023-02-15 23:43:03 | [diff] [blame] | 3801 | r"^chrome/browser/ash/arc/enterprise/cert_store/arc_cert_installer\.cc$", |
| 3802 | r"^chrome/browser/ash/policy/remote_commands/user_command_arc_job\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3803 | r"^chrome/browser/chrome_browser_main\.cc$", |
| 3804 | r"^chrome/browser/ui/startup/startup_browser_creator\.cc$", |
| 3805 | r"^chrome/browser/browser_switcher/bho/.*", |
| 3806 | r"^chrome/browser/diagnostics/diagnostics_writer\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3807 | r"^chrome/chrome_elf/dll_hash/dll_hash_main\.cc$", |
| 3808 | r"^chrome/installer/setup/.*", |
Daniel Rubery | ad36eea | 2024-08-01 01:38:32 | [diff] [blame] | 3809 | # crdmg runs as a separate binary which intentionally does |
| 3810 | # not depend on base logging. |
| 3811 | r"^chrome/utility/safe_browsing/mac/crdmg\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3812 | r"^chromecast/", |
Vigen Issahhanjan | e2d9382 | 2023-06-30 15:57:20 | [diff] [blame] | 3813 | r"^components/cast", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3814 | r"^components/media_control/renderer/media_playback_options\.cc$", |
Salma Elmahallawy | 5297645 | 2023-01-27 17:04:49 | [diff] [blame] | 3815 | r"^components/policy/core/common/policy_logger\.cc$", |
Tomek Jurkiewicz | 06fd6a7 | 2025-06-18 15:24:28 | [diff] [blame] | 3816 | r"^components/supervised_user/core/browser/android/content_filters_observer_bridge\.cc", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3817 | r"^components/viz/service/display/" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3818 | r"overlay_strategy_underlay_cast\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3819 | r"^components/zucchini/.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3820 | # TODO(peter): Remove exception. https://crbug.com/534537 |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3821 | r"^content/browser/notifications/" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3822 | r"notification_event_dispatcher_impl\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3823 | r"^content/common/gpu/client/gl_helper_benchmark\.cc$", |
| 3824 | r"^courgette/courgette_minimal_tool\.cc$", |
| 3825 | r"^courgette/courgette_tool\.cc$", |
| 3826 | r"^extensions/renderer/logging_native_handler\.cc$", |
| 3827 | r"^fuchsia_web/common/init_logging\.cc$", |
| 3828 | r"^fuchsia_web/runners/common/web_component\.cc$", |
Caroline Liu | a705013 | 2023-02-13 22:23:15 | [diff] [blame] | 3829 | r"^fuchsia_web/shell/.*\.cc$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3830 | r"^headless/app/headless_shell\.cc$", |
| 3831 | r"^ipc/ipc_logging\.cc$", |
| 3832 | r"^native_client_sdk/", |
| 3833 | r"^remoting/base/logging\.h$", |
| 3834 | r"^remoting/host/.*", |
| 3835 | r"^sandbox/linux/.*", |
Austin Sullivan | a6054e0 | 2024-05-20 16:31:29 | [diff] [blame] | 3836 | r"^services/webnn/tflite/graph_impl_tflite\.cc$", |
| 3837 | r"^services/webnn/coreml/graph_impl_coreml\.mm$", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3838 | r"^storage/browser/file_system/dump_file_system\.cc$", |
Steinar H. Gunderson | e5689e4 | 2024-08-07 18:17:19 | [diff] [blame] | 3839 | r"^testing/perf/", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 3840 | r"^tools/", |
| 3841 | r"^ui/base/resource/data_pack\.cc$", |
| 3842 | r"^ui/aura/bench/bench_main\.cc$", |
| 3843 | r"^ui/ozone/platform/cast/", |
| 3844 | r"^ui/base/x/xwmstartupcheck/" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3845 | r"xwmstartupcheck\.cc$")) |
| 3846 | source_file_filter=lambda x: input_api.FilterSourceFile( |
| 3847 | x, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip) |
thakis@chromium.org | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3848 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3849 | log_info= set([]) |
| 3850 | printf= set([]) |
thakis@chromium.org | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3851 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3852 | for fin input_api.AffectedSourceFiles(source_file_filter): |
| 3853 | for _, linein f.ChangedContents(): |
| 3854 | if input_api.re.search(r"\bD?LOG\s*\(\s*INFO\s*\)", line): |
| 3855 | log_info.add(f.LocalPath()) |
| 3856 | elif input_api.re.search(r"\bD?LOG_IF\s*\(\s*INFO\s*,", line): |
| 3857 | log_info.add(f.LocalPath()) |
jln@chromium.org | 18b466b | 2013-12-02 22:01:37 | [diff] [blame] | 3858 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3859 | if input_api.re.search(r"\bprintf\(", line): |
| 3860 | printf.add(f.LocalPath()) |
| 3861 | elif input_api.re.search(r"\bfprintf\((stdout|stderr)", line): |
| 3862 | printf.add(f.LocalPath()) |
thakis@chromium.org | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3863 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3864 | if log_info: |
| 3865 | return[ |
| 3866 | output_api.PresubmitError( |
| 3867 | 'These files spam the console log with LOG(INFO):', |
| 3868 | items=log_info) |
| 3869 | ] |
| 3870 | if printf: |
| 3871 | return[ |
| 3872 | output_api.PresubmitError( |
| 3873 | 'These files spam the console log with printf/fprintf:', |
| 3874 | items=printf) |
| 3875 | ] |
| 3876 | return[] |
thakis@chromium.org | 8521856 | 2013-11-22 07:41:40 | [diff] [blame] | 3877 | |
| 3878 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3879 | defCheckForAnonymousVariables(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3880 | """These types are all expected to hold locks while in scope and |
| 3881 | so should never be anonymous (which causes them to be immediately |
| 3882 | destroyed).""" |
| 3883 | they_who_must_be_named=[ |
| 3884 | 'base::AutoLock', |
| 3885 | 'base::AutoReset', |
| 3886 | 'base::AutoUnlock', |
| 3887 | 'SkAutoAlphaRestore', |
| 3888 | 'SkAutoBitmapShaderInstall', |
| 3889 | 'SkAutoBlitterChoose', |
| 3890 | 'SkAutoBounderCommit', |
| 3891 | 'SkAutoCallProc', |
| 3892 | 'SkAutoCanvasRestore', |
| 3893 | 'SkAutoCommentBlock', |
| 3894 | 'SkAutoDescriptor', |
| 3895 | 'SkAutoDisableDirectionCheck', |
| 3896 | 'SkAutoDisableOvalCheck', |
| 3897 | 'SkAutoFree', |
| 3898 | 'SkAutoGlyphCache', |
| 3899 | 'SkAutoHDC', |
| 3900 | 'SkAutoLockColors', |
| 3901 | 'SkAutoLockPixels', |
| 3902 | 'SkAutoMalloc', |
| 3903 | 'SkAutoMaskFreeImage', |
| 3904 | 'SkAutoMutexAcquire', |
| 3905 | 'SkAutoPathBoundsUpdate', |
| 3906 | 'SkAutoPDFRelease', |
| 3907 | 'SkAutoRasterClipValidate', |
| 3908 | 'SkAutoRef', |
| 3909 | 'SkAutoTime', |
| 3910 | 'SkAutoTrace', |
| 3911 | 'SkAutoUnref', |
| 3912 | ] |
| 3913 | anonymous= r'(%s)\s*[({]'%'|'.join(they_who_must_be_named) |
| 3914 | # bad: base::AutoLock(lock.get()); |
| 3915 | # not bad: base::AutoLock lock(lock.get()); |
| 3916 | bad_pattern= input_api.re.compile(anonymous) |
| 3917 | # good: new base::AutoLock(lock.get()) |
| 3918 | good_pattern= input_api.re.compile(r'\bnew\s*'+ anonymous) |
| 3919 | errors=[] |
enne@chromium.org | 49aa76a | 2013-12-04 06:59:16 | [diff] [blame] | 3920 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3921 | for fin input_api.AffectedFiles(): |
| 3922 | ifnot f.LocalPath().endswith(('.cc','.h','.inl','.m','.mm')): |
| 3923 | continue |
| 3924 | for linenum, linein f.ChangedContents(): |
| 3925 | if bad_pattern.search(line)andnot good_pattern.search(line): |
| 3926 | errors.append('%s:%d'%(f.LocalPath(), linenum)) |
enne@chromium.org | 49aa76a | 2013-12-04 06:59:16 | [diff] [blame] | 3927 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3928 | if errors: |
| 3929 | return[ |
| 3930 | output_api.PresubmitError( |
| 3931 | 'These lines create anonymous variables that need to be named:', |
| 3932 | items=errors) |
| 3933 | ] |
| 3934 | return[] |
enne@chromium.org | 49aa76a | 2013-12-04 06:59:16 | [diff] [blame] | 3935 | |
| 3936 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 3937 | defCheckUniquePtrOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3938 | # 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] | 3939 | # and U, or is invalid due to mismatched angle bracket pairs. Assumes that |
| 3940 | # |template_str| is already in the form <...>. |
| 3941 | defHasMoreThanOneArgOrInvalid(template_str): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3942 | # Level of <...> nesting. |
| 3943 | nesting=0 |
| 3944 | for cin template_str: |
| 3945 | if c=='<': |
| 3946 | nesting+=1 |
| 3947 | elif c=='>': |
| 3948 | nesting-=1 |
| 3949 | elif c==','and nesting==1: |
| 3950 | returnTrue |
Glen Robertson | 9142ffd7 | 2024-05-16 01:37:47 | [diff] [blame] | 3951 | if nesting!=0: |
Daniel Cheng | 566634ff | 2024-06-29 14:56:53 | [diff] [blame] | 3952 | # Invalid. |
| 3953 | returnTrue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3954 | returnFalse |
Vaclav Brozek | b7fadb69 | 2018-08-30 06:39:53 | [diff] [blame] | 3955 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3956 | file_inclusion_pattern=[r'.+%s'% _IMPLEMENTATION_EXTENSIONS] |
| 3957 | sources=lambda affected_file: input_api.FilterSourceFile( |
| 3958 | affected_file, |
| 3959 | files_to_skip=(_EXCLUDED_PATHS+ _TEST_CODE_EXCLUDED_PATHS+ input_api. |
| 3960 | DEFAULT_FILES_TO_SKIP), |
| 3961 | files_to_check=file_inclusion_pattern) |
Vaclav Brozek | a54c528b | 2018-04-06 19:23:55 | [diff] [blame] | 3962 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3963 | # Pattern to capture a single "<...>" block of template arguments. It can |
| 3964 | # handle linearly nested blocks, such as "<std::vector<std::set<T>>>", but |
| 3965 | # cannot handle branching structures, such as "<pair<set<T>,set<U>>". The |
| 3966 | # latter would likely require counting that < and > match, which is not |
| 3967 | # expressible in regular languages. Should the need arise, one can introduce |
| 3968 | # limited counting (matching up to a total number of nesting depth), which |
| 3969 | # should cover all practical cases for already a low nesting limit. |
| 3970 | template_arg_pattern=( |
| 3971 | r'<[^>]*'# Opening block of <. |
| 3972 | r'>([^<]*>)?')# Closing block of >. |
| 3973 | # Prefix expressing that whatever follows is not already inside a <...> |
| 3974 | # block. |
| 3975 | not_inside_template_arg_pattern= r'(^|[^<,\s]\s*)' |
| 3976 | null_construct_pattern= input_api.re.compile( |
| 3977 | not_inside_template_arg_pattern+ r'\bstd::unique_ptr'+ |
| 3978 | template_arg_pattern+ r'\(\)') |
Vaclav Brozek | a54c528b | 2018-04-06 19:23:55 | [diff] [blame] | 3979 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3980 | # Same as template_arg_pattern, but excluding type arrays, e.g., <T[]>. |
| 3981 | template_arg_no_array_pattern=( |
| 3982 | r'<[^>]*[^]]'# Opening block of <. |
| 3983 | r'>([^(<]*[^]]>)?')# Closing block of >. |
| 3984 | # Prefix saying that what follows is the start of an expression. |
| 3985 | start_of_expr_pattern= r'(=|\breturn|^)\s*' |
| 3986 | # Suffix saying that what follows are call parentheses with a non-empty list |
| 3987 | # of arguments. |
| 3988 | nonempty_arg_list_pattern= r'\(([^)]|$)' |
| 3989 | # Put the template argument into a capture group for deeper examination later. |
| 3990 | return_construct_pattern= input_api.re.compile( |
| 3991 | start_of_expr_pattern+ r'std::unique_ptr'+'(?P<template_arg>'+ |
| 3992 | template_arg_no_array_pattern+')'+ nonempty_arg_list_pattern) |
Vaclav Brozek | a54c528b | 2018-04-06 19:23:55 | [diff] [blame] | 3993 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 3994 | problems_constructor=[] |
| 3995 | problems_nullptr=[] |
| 3996 | for fin input_api.AffectedSourceFiles(sources): |
| 3997 | for line_number, linein f.ChangedContents(): |
| 3998 | # Disallow: |
| 3999 | # return std::unique_ptr<T>(foo); |
| 4000 | # bar = std::unique_ptr<T>(foo); |
| 4001 | # But allow: |
| 4002 | # return std::unique_ptr<T[]>(foo); |
| 4003 | # bar = std::unique_ptr<T[]>(foo); |
| 4004 | # And also allow cases when the second template argument is present. Those |
| 4005 | # cases cannot be handled by std::make_unique: |
| 4006 | # return std::unique_ptr<T, U>(foo); |
| 4007 | # bar = std::unique_ptr<T, U>(foo); |
| 4008 | local_path= f.LocalPath() |
| 4009 | return_construct_result= return_construct_pattern.search(line) |
Glen Robertson | 9142ffd7 | 2024-05-16 01:37:47 | [diff] [blame] | 4010 | if return_construct_resultandnotHasMoreThanOneArgOrInvalid( |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4011 | return_construct_result.group('template_arg')): |
| 4012 | problems_constructor.append( |
| 4013 | '%s:%d\n %s'%(local_path, line_number, line.strip())) |
| 4014 | # Disallow: |
| 4015 | # std::unique_ptr<T>() |
| 4016 | if null_construct_pattern.search(line): |
| 4017 | problems_nullptr.append( |
| 4018 | '%s:%d\n %s'%(local_path, line_number, line.strip())) |
Vaclav Brozek | 851d960 | 2018-04-04 16:13:05 | [diff] [blame] | 4019 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4020 | errors=[] |
| 4021 | if problems_nullptr: |
| 4022 | errors.append( |
| 4023 | output_api.PresubmitPromptWarning( |
| 4024 | 'The following files use std::unique_ptr<T>(). Use nullptr instead.', |
| 4025 | problems_nullptr)) |
| 4026 | if problems_constructor: |
| 4027 | errors.append( |
| 4028 | output_api.PresubmitError( |
| 4029 | 'The following files use explicit std::unique_ptr constructor. ' |
| 4030 | 'Use std::make_unique<T>() instead, or use base::WrapUnique if ' |
| 4031 | 'std::make_unique is not an option.', problems_constructor)) |
| 4032 | return errors |
Peter Kasting | 4844e46e | 2018-02-23 07:27:10 | [diff] [blame] | 4033 | |
| 4034 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 4035 | defCheckUserActionUpdate(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4036 | """Checks if any new user action has been added.""" |
| 4037 | if any('actions.xml'== input_api.os_path.basename(f) |
| 4038 | for fin input_api.LocalPaths()): |
| 4039 | # If actions.xml is already included in the changelist, the PRESUBMIT |
| 4040 | # for actions.xml will do a more complete presubmit check. |
| 4041 | return[] |
| 4042 | |
| 4043 | file_inclusion_pattern=[r'.*\.(cc|mm)$'] |
| 4044 | files_to_skip=(_EXCLUDED_PATHS+ _TEST_CODE_EXCLUDED_PATHS+ |
| 4045 | input_api.DEFAULT_FILES_TO_SKIP) |
| 4046 | file_filter=lambda f: input_api.FilterSourceFile( |
| 4047 | f, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip) |
| 4048 | |
| 4049 | action_re= r'[^a-zA-Z]UserMetricsAction\("([^"]*)' |
| 4050 | current_actions=None |
| 4051 | for fin input_api.AffectedFiles(file_filter=file_filter): |
| 4052 | for line_num, linein f.ChangedContents(): |
| 4053 | match= input_api.re.search(action_re, line) |
| 4054 | if match: |
| 4055 | # Loads contents in tools/metrics/actions/actions.xml to memory. It's |
| 4056 | # loaded only once. |
| 4057 | ifnot current_actions: |
Bruce Dawson | 6cb2d4d | 2023-03-01 21:35:09 | [diff] [blame] | 4058 | with open('tools/metrics/actions/actions.xml', |
| 4059 | encoding='utf-8')as actions_f: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4060 | current_actions= actions_f.read() |
| 4061 | # Search for the matched user action name in |current_actions|. |
| 4062 | for action_namein match.groups(): |
| 4063 | action='name="{0}"'.format(action_name) |
| 4064 | if actionnotin current_actions: |
| 4065 | return[ |
| 4066 | output_api.PresubmitPromptWarning( |
| 4067 | 'File %s line %d: %s is missing in ' |
| 4068 | 'tools/metrics/actions/actions.xml. Please run ' |
| 4069 | 'tools/metrics/actions/extract_actions.py to update.' |
| 4070 | %(f.LocalPath(), line_num, action_name)) |
| 4071 | ] |
yiyaoliu@chromium.org | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 4072 | return[] |
| 4073 | |
yiyaoliu@chromium.org | 999261d | 2014-03-03 20:08:08 | [diff] [blame] | 4074 | |
Daniel Cheng | 13ca61a88 | 2017-08-25 15:11:25 | [diff] [blame] | 4075 | def_ImportJSONCommentEater(input_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4076 | import sys |
| 4077 | sys.path= sys.path+[ |
| 4078 | input_api.os_path.join(input_api.PresubmitLocalPath(),'tools', |
| 4079 | 'json_comment_eater') |
| 4080 | ] |
| 4081 | import json_comment_eater |
| 4082 | return json_comment_eater |
Daniel Cheng | 13ca61a88 | 2017-08-25 15:11:25 | [diff] [blame] | 4083 | |
| 4084 | |
yoz@chromium.org | 99171a9 | 2014-06-03 08:44:47 | [diff] [blame] | 4085 | def_GetJSONParseError(input_api, filename, eat_comments=True): |
dcheng | e07de81 | 2016-06-20 19:27:17 | [diff] [blame] | 4086 | try: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4087 | contents= input_api.ReadFile(filename) |
| 4088 | if eat_comments: |
| 4089 | json_comment_eater=_ImportJSONCommentEater(input_api) |
| 4090 | contents= json_comment_eater.Nom(contents) |
dcheng | e07de81 | 2016-06-20 19:27:17 | [diff] [blame] | 4091 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4092 | input_api.json.loads(contents) |
| 4093 | exceptValueErroras e: |
| 4094 | return e |
Andrew Grieve | 4deedb1 | 2022-02-03 21:34:50 | [diff] [blame] | 4095 | returnNone |
| 4096 | |
| 4097 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4098 | def_GetIDLParseError(input_api, filename): |
| 4099 | try: |
| 4100 | contents= input_api.ReadFile(filename) |
Devlin Cronin | f7582a1 | 2022-04-21 21:14:28 | [diff] [blame] | 4101 | for i, charin enumerate(contents): |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4102 | ifnot char.isascii(): |
| 4103 | return( |
| 4104 | 'Non-ascii character "%s" (ord %d) found at offset %d.'% |
| 4105 | (char, ord(char), i)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4106 | idl_schema= input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 4107 | 'tools','json_schema_compiler', |
| 4108 | 'idl_schema.py') |
| 4109 | process= input_api.subprocess.Popen( |
Bruce Dawson | 679fb08 | 2022-04-14 00:47:28 | [diff] [blame] | 4110 | [input_api.python3_executable, idl_schema], |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4111 | stdin=input_api.subprocess.PIPE, |
| 4112 | stdout=input_api.subprocess.PIPE, |
| 4113 | stderr=input_api.subprocess.PIPE, |
| 4114 | universal_newlines=True) |
| 4115 | (_, error)= process.communicate(input=contents) |
| 4116 | return errororNone |
| 4117 | exceptValueErroras e: |
| 4118 | return e |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4119 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4120 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4121 | defCheckParseErrors(input_api, output_api): |
| 4122 | """Check that IDL and JSON files do not contain syntax errors.""" |
| 4123 | actions={ |
| 4124 | '.idl':_GetIDLParseError, |
| 4125 | '.json':_GetJSONParseError, |
| 4126 | } |
| 4127 | # Most JSON files are preprocessed and support comments, but these do not. |
| 4128 | json_no_comments_patterns=[ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4129 | r'^testing/', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4130 | ] |
| 4131 | # Only run IDL checker on files in these directories. |
| 4132 | idl_included_patterns=[ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4133 | r'^chrome/common/extensions/api/', |
| 4134 | r'^extensions/common/api/', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4135 | ] |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4136 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4137 | def get_action(affected_file): |
| 4138 | filename= affected_file.LocalPath() |
| 4139 | return actions.get(input_api.os_path.splitext(filename)[1]) |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4140 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4141 | defFilterFile(affected_file): |
| 4142 | action= get_action(affected_file) |
| 4143 | ifnot action: |
| 4144 | returnFalse |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4145 | path= affected_file.UnixLocalPath() |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 4146 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4147 | if_MatchesFile(input_api, |
| 4148 | _KNOWN_TEST_DATA_AND_INVALID_JSON_FILE_PATTERNS, path): |
| 4149 | returnFalse |
| 4150 | |
| 4151 | if(action==_GetIDLParseError |
| 4152 | andnot_MatchesFile(input_api, idl_included_patterns, path)): |
| 4153 | returnFalse |
| 4154 | returnTrue |
| 4155 | |
| 4156 | results=[] |
| 4157 | for affected_filein input_api.AffectedFiles(file_filter=FilterFile, |
| 4158 | include_deletes=False): |
| 4159 | action= get_action(affected_file) |
| 4160 | kwargs={} |
| 4161 | if(action==_GetJSONParseError |
| 4162 | and_MatchesFile(input_api, json_no_comments_patterns, |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4163 | affected_file.UnixLocalPath())): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4164 | kwargs['eat_comments']=False |
| 4165 | parse_error= action(input_api, affected_file.AbsoluteLocalPath(), |
| 4166 | **kwargs) |
| 4167 | if parse_error: |
| 4168 | results.append( |
| 4169 | output_api.PresubmitError( |
| 4170 | '%s could not be parsed: %s'% |
| 4171 | (affected_file.LocalPath(), parse_error))) |
| 4172 | return results |
| 4173 | |
| 4174 | |
| 4175 | defCheckJavaStyle(input_api, output_api): |
| 4176 | """Runs checkstyle on changed java files and returns errors if any exist.""" |
| 4177 | |
| 4178 | # Return early if no java files were modified. |
| 4179 | ifnot any( |
| 4180 | _IsJavaFile(input_api, f.LocalPath()) |
| 4181 | for fin input_api.AffectedFiles()): |
| 4182 | return[] |
| 4183 | |
| 4184 | import sys |
| 4185 | original_sys_path= sys.path |
| 4186 | try: |
| 4187 | sys.path= sys.path+[ |
| 4188 | input_api.os_path.join(input_api.PresubmitLocalPath(),'tools', |
| 4189 | 'android','checkstyle') |
| 4190 | ] |
| 4191 | import checkstyle |
| 4192 | finally: |
| 4193 | # Restore sys.path to what it was before. |
| 4194 | sys.path= original_sys_path |
| 4195 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 4196 | return checkstyle.run_presubmit(input_api, |
| 4197 | output_api, |
| 4198 | files_to_skip=_EXCLUDED_PATHS+ |
| 4199 | input_api.DEFAULT_FILES_TO_SKIP) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4200 | |
| 4201 | |
| 4202 | defCheckPythonDevilInit(input_api, output_api): |
| 4203 | """Checks to make sure devil is initialized correctly in python scripts.""" |
| 4204 | script_common_initialize_pattern= input_api.re.compile( |
| 4205 | r'script_common\.InitializeEnvironment\(') |
| 4206 | devil_env_config_initialize= input_api.re.compile( |
| 4207 | r'devil_env\.config\.Initialize\(') |
| 4208 | |
| 4209 | errors=[] |
| 4210 | |
| 4211 | sources=lambda affected_file: input_api.FilterSourceFile( |
| 4212 | affected_file, |
| 4213 | files_to_skip=(_EXCLUDED_PATHS+ input_api.DEFAULT_FILES_TO_SKIP+( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4214 | r'^build/android/devil_chromium\.py', |
Sven Zheng | 8e07956 | 2024-05-10 20:11:06 | [diff] [blame] | 4215 | r'^tools/bisect-builds\.py', |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4216 | r'^third_party/.*', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4217 | )), |
| 4218 | files_to_check=[r'.*\.py$']) |
| 4219 | |
| 4220 | for fin input_api.AffectedSourceFiles(sources): |
| 4221 | for line_num, linein f.ChangedContents(): |
| 4222 | if(script_common_initialize_pattern.search(line) |
| 4223 | or devil_env_config_initialize.search(line)): |
| 4224 | errors.append("%s:%d"%(f.LocalPath(), line_num)) |
| 4225 | |
| 4226 | results=[] |
| 4227 | |
| 4228 | if errors: |
| 4229 | results.append( |
| 4230 | output_api.PresubmitError( |
| 4231 | 'Devil initialization should always be done using ' |
| 4232 | 'devil_chromium.Initialize() in the chromium project, to use better ' |
| 4233 | 'defaults for dependencies (ex. up-to-date version of adb).', |
| 4234 | errors)) |
| 4235 | |
| 4236 | return results |
| 4237 | |
| 4238 | |
| 4239 | def_MatchesFile(input_api, patterns, path): |
| 4240 | for patternin patterns: |
| 4241 | if input_api.re.search(pattern, path): |
| 4242 | returnTrue |
| 4243 | returnFalse |
| 4244 | |
| 4245 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4246 | def_ChangeHasSecurityReviewer(input_api, owners_file): |
| 4247 | """Returns True iff the CL has a reviewer from SECURITY_OWNERS. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4248 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4249 | Args: |
| 4250 | input_api: The presubmit input API. |
| 4251 | owners_file: OWNERS file with required reviewers. Typically, this is |
| 4252 | something like ipc/SECURITY_OWNERS. |
| 4253 | |
| 4254 | Note: if the presubmit is running for commit rather than for upload, this |
| 4255 | only returns True if a security reviewer has also approved the CL. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4256 | """ |
Daniel Cheng | d8824447 | 2022-05-16 09:08:47 | [diff] [blame] | 4257 | # Owners-Override should bypass all additional OWNERS enforcement checks. |
| 4258 | # A CR+1 vote will still be required to land this change. |
| 4259 | if(input_api.change.issueand input_api.gerrit.IsOwnersOverrideApproved( |
| 4260 | input_api.change.issue)): |
| 4261 | returnTrue |
| 4262 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4263 | owner_email, reviewers=( |
| 4264 | input_api.canned_checks.GetCodereviewOwnerAndReviewers( |
Daniel Cheng | 3008dc1 | 2022-05-13 04:02:11 | [diff] [blame] | 4265 | input_api, |
| 4266 | None, |
| 4267 | approval_needed=input_api.is_committingandnot input_api.dry_run)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4268 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4269 | security_owners= input_api.owners_client.ListOwners(owners_file) |
| 4270 | return any(ownerin reviewersfor ownerin security_owners) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4271 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4272 | |
| 4273 | @dataclass |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4274 | class_SecurityProblemWithItems: |
| 4275 | problem: str |
| 4276 | items:Sequence[str] |
| 4277 | |
| 4278 | |
| 4279 | @dataclass |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4280 | class_MissingSecurityOwnersResult: |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4281 | owners_file_problems:Sequence[_SecurityProblemWithItems] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4282 | has_security_sensitive_files: bool |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4283 | missing_reviewer_problem:Optional[_SecurityProblemWithItems] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4284 | |
| 4285 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 4286 | def_FindMissingSecurityOwners( |
| 4287 | input_api, |
| 4288 | output_api, |
| 4289 | file_patterns:Sequence[str], |
| 4290 | excluded_patterns:Sequence[str], |
| 4291 | required_owners_file: str, |
| 4292 | custom_rule_function:Optional[Callable]=None |
| 4293 | )->_MissingSecurityOwnersResult: |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4294 | """Find OWNERS files missing per-file rules for security-sensitive files. |
| 4295 | |
| 4296 | Args: |
| 4297 | input_api: the PRESUBMIT input API object. |
| 4298 | output_api: the PRESUBMIT output API object. |
| 4299 | file_patterns: basename patterns that require a corresponding per-file |
| 4300 | security restriction. |
| 4301 | excluded_patterns: path patterns that should be exempted from |
| 4302 | requiring a security restriction. |
| 4303 | required_owners_file: path to the required OWNERS file, e.g. |
| 4304 | ipc/SECURITY_OWNERS |
| 4305 | cc_alias: If not None, email that will be CCed automatically if the |
| 4306 | change contains security-sensitive files, as determined by |
| 4307 | `file_patterns` and `excluded_patterns`. |
| 4308 | custom_rule_function: If not None, will be called with `input_api` and |
| 4309 | the current file under consideration. Returning True will add an |
| 4310 | exact match per-file rule check for the current file. |
| 4311 | """ |
| 4312 | |
| 4313 | # `to_check` is a mapping of an OWNERS file path to Patterns. |
| 4314 | # |
| 4315 | # Patterns is a dictionary mapping glob patterns (suitable for use in |
| 4316 | # per-file rules) to a PatternEntry. |
| 4317 | # |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4318 | # PatternEntry is a dictionary with two keys: |
| 4319 | # - 'files': the files that are matched by this pattern |
| 4320 | # - 'rules': the per-file rules needed for this pattern |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4321 | # |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4322 | # For example, if we expect OWNERS file to contain rules for *.mojom and |
| 4323 | # *_struct_traits*.*, Patterns might look like this: |
| 4324 | # { |
| 4325 | # '*.mojom': { |
| 4326 | # 'files': ..., |
| 4327 | # 'rules': [ |
| 4328 | # 'per-file *.mojom=set noparent', |
| 4329 | # 'per-file *.mojom=file://ipc/SECURITY_OWNERS', |
| 4330 | # ], |
| 4331 | # }, |
| 4332 | # '*_struct_traits*.*': { |
| 4333 | # 'files': ..., |
| 4334 | # 'rules': [ |
| 4335 | # 'per-file *_struct_traits*.*=set noparent', |
| 4336 | # 'per-file *_struct_traits*.*=file://ipc/SECURITY_OWNERS', |
| 4337 | # ], |
| 4338 | # }, |
| 4339 | # } |
| 4340 | to_check={} |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4341 | files_to_review=[] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4342 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4343 | defAddPatternToCheck(file, pattern): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4344 | owners_file= input_api.os_path.join( |
Daniel Cheng | d8824447 | 2022-05-16 09:08:47 | [diff] [blame] | 4345 | input_api.os_path.dirname(file.LocalPath()),'OWNERS') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4346 | if owners_filenotin to_check: |
| 4347 | to_check[owners_file]={} |
| 4348 | if patternnotin to_check[owners_file]: |
| 4349 | to_check[owners_file][pattern]={ |
| 4350 | 'files':[], |
| 4351 | 'rules':[ |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4352 | f'per-file {pattern}=set noparent', |
| 4353 | f'per-file {pattern}=file://{required_owners_file}', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4354 | ] |
| 4355 | } |
Daniel Cheng | ed57a16 | 2022-05-25 02:56:34 | [diff] [blame] | 4356 | to_check[owners_file][pattern]['files'].append(file.LocalPath()) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4357 | files_to_review.append(file.LocalPath()) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4358 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4359 | # Only enforce security OWNERS rules for a directory if that directory has a |
| 4360 | # file that matches `file_patterns`. For example, if a directory only |
| 4361 | # contains *.mojom files and no *_messages*.h files, the check should only |
| 4362 | # ensure that rules for *.mojom files are present. |
| 4363 | for filein input_api.AffectedFiles(include_deletes=False): |
| 4364 | file_basename= input_api.os_path.basename(file.LocalPath()) |
| 4365 | if custom_rule_functionisnotNoneand custom_rule_function( |
| 4366 | input_api, file): |
| 4367 | AddPatternToCheck(file, file_basename) |
| 4368 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4369 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4370 | if any( |
| 4371 | input_api.fnmatch.fnmatch(file.LocalPath(), pattern) |
| 4372 | for patternin excluded_patterns): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4373 | continue |
| 4374 | |
| 4375 | for patternin file_patterns: |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4376 | # Unlike `excluded_patterns`, `file_patterns` is checked only against the |
| 4377 | # file's basename. |
| 4378 | if input_api.fnmatch.fnmatch(file_basename, pattern): |
| 4379 | AddPatternToCheck(file, pattern) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4380 | break |
| 4381 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4382 | has_security_sensitive_files= bool(to_check) |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4383 | |
| 4384 | # Check if any newly added lines in OWNERS files intersect with required |
| 4385 | # per-file OWNERS lines. If so, ensure that a security reviewer is included. |
| 4386 | # This is a hack, but is needed because the OWNERS check (by design) ignores |
| 4387 | # new OWNERS entries; otherwise, a non-owner could add someone as a new |
| 4388 | # OWNER and have that newly-added OWNER self-approve their own addition. |
| 4389 | newly_covered_files=[] |
| 4390 | for filein input_api.AffectedFiles(include_deletes=False): |
| 4391 | ifnot file.LocalPath()in to_check: |
| 4392 | continue |
| 4393 | for _, linein file.ChangedContents(): |
| 4394 | for _, entryin to_check[file.LocalPath()].items(): |
| 4395 | if linein entry['rules']: |
| 4396 | newly_covered_files.extend(entry['files']) |
| 4397 | |
| 4398 | missing_reviewer_problems=None |
| 4399 | if newly_covered_filesandnot_ChangeHasSecurityReviewer( |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4400 | input_api, required_owners_file): |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4401 | missing_reviewer_problems=_SecurityProblemWithItems( |
| 4402 | f'Review from an owner in {required_owners_file} is required for ' |
| 4403 | 'the following newly-added files:', |
| 4404 | [f'{file}'for filein sorted(set(newly_covered_files))]) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4405 | |
| 4406 | # Go through the OWNERS files to check, filtering out rules that are already |
| 4407 | # present in that OWNERS file. |
| 4408 | for owners_file, patternsin to_check.items(): |
| 4409 | try: |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4410 | lines= set( |
| 4411 | input_api.ReadFile( |
| 4412 | input_api.os_path.join(input_api.change.RepositoryRoot(), |
| 4413 | owners_file)).splitlines()) |
| 4414 | for entryin patterns.values(): |
| 4415 | entry['rules']=[ |
| 4416 | rulefor rulein entry['rules']if rulenotin lines |
| 4417 | ] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4418 | exceptIOError: |
| 4419 | # No OWNERS file, so all the rules are definitely missing. |
| 4420 | continue |
| 4421 | |
| 4422 | # 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] | 4423 | owners_file_problems=[] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4424 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4425 | for owners_file, patternsin to_check.items(): |
| 4426 | missing_lines=[] |
| 4427 | files=[] |
| 4428 | for _, entryin patterns.items(): |
Daniel Cheng | ed57a16 | 2022-05-25 02:56:34 | [diff] [blame] | 4429 | files.extend(entry['files']) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4430 | missing_lines.extend(entry['rules']) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4431 | if missing_lines: |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4432 | joined_missing_lines='\n'.join(linefor linein missing_lines) |
| 4433 | owners_file_problems.append( |
| 4434 | _SecurityProblemWithItems( |
| 4435 | 'Found missing OWNERS lines for security-sensitive files. ' |
| 4436 | f'Please add the following lines to {owners_file}:\n' |
| 4437 | f'{joined_missing_lines}\n\nTo ensure security review for:', |
| 4438 | files)) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4439 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4440 | return_MissingSecurityOwnersResult(owners_file_problems, |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4441 | has_security_sensitive_files, |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4442 | missing_reviewer_problems) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4443 | |
| 4444 | |
| 4445 | def_CheckChangeForIpcSecurityOwners(input_api, output_api): |
| 4446 | # Whether or not a file affects IPC is (mostly) determined by a simple list |
| 4447 | # of filename patterns. |
| 4448 | file_patterns=[ |
| 4449 | # Legacy IPC: |
| 4450 | '*_messages.cc', |
| 4451 | '*_messages*.h', |
| 4452 | '*_param_traits*.*', |
| 4453 | # Mojo IPC: |
| 4454 | '*.mojom', |
| 4455 | '*_mojom_traits*.*', |
| 4456 | '*_type_converter*.*', |
| 4457 | # Android native IPC: |
| 4458 | '*.aidl', |
| 4459 | ] |
| 4460 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4461 | excluded_patterns=[ |
Daniel Cheng | 518943f | 2022-05-12 22:15:46 | [diff] [blame] | 4462 | # These third_party directories do not contain IPCs, but contain files |
| 4463 | # matching the above patterns, which trigger false positives. |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4464 | 'third_party/crashpad/*', |
| 4465 | 'third_party/blink/renderer/platform/bindings/*', |
Evan Stade | 23a77da | 2025-02-06 21:15:31 | [diff] [blame] | 4466 | 'third_party/protobuf/*', |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4467 | 'third_party/win_build_output/*', |
Daniel Cheng | d8824447 | 2022-05-16 09:08:47 | [diff] [blame] | 4468 | # Enum-only mojoms used for web metrics, so no security review needed. |
| 4469 | 'third_party/blink/public/mojom/use_counter/metrics/*', |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4470 | # These files are just used to communicate between class loaders running |
| 4471 | # in the same process. |
| 4472 | 'weblayer/browser/java/org/chromium/weblayer_private/interfaces/*', |
| 4473 | 'weblayer/browser/java/org/chromium/weblayer_private/test_interfaces/*', |
| 4474 | ] |
| 4475 | |
| 4476 | defIsMojoServiceManifestFile(input_api, file): |
Dirk Pranke | e4df2797 | 2025-02-26 18:39:35 | [diff] [blame] | 4477 | manifest_pattern= input_api.re.compile(r'manifests?\.(cc|h)$') |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 4478 | test_manifest_pattern= input_api.re.compile( |
| 4479 | r'test_manifests?\.(cc|h)') |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4480 | ifnot manifest_pattern.search(file.LocalPath()): |
| 4481 | returnFalse |
| 4482 | |
| 4483 | if test_manifest_pattern.search(file.LocalPath()): |
| 4484 | returnFalse |
| 4485 | |
| 4486 | # All actual service manifest files should contain at least one |
| 4487 | # qualified reference to service_manager::Manifest. |
| 4488 | return any('service_manager::Manifest'in line |
| 4489 | for linein file.NewContents()) |
| 4490 | |
| 4491 | return_FindMissingSecurityOwners( |
| 4492 | input_api, |
| 4493 | output_api, |
| 4494 | file_patterns, |
| 4495 | excluded_patterns, |
| 4496 | 'ipc/SECURITY_OWNERS', |
| 4497 | custom_rule_function=IsMojoServiceManifestFile) |
| 4498 | |
| 4499 | |
| 4500 | def_CheckChangeForFuchsiaSecurityOwners(input_api, output_api): |
| 4501 | file_patterns=[ |
| 4502 | # Component specifications. |
| 4503 | '*.cml',# Component Framework v2. |
| 4504 | '*.cmx',# Component Framework v1. |
| 4505 | |
| 4506 | # Fuchsia IDL protocol specifications. |
| 4507 | '*.fidl', |
| 4508 | ] |
| 4509 | |
| 4510 | # Don't check for owners files for changes in these directories. |
| 4511 | excluded_patterns=[ |
| 4512 | 'third_party/crashpad/*', |
| 4513 | ] |
| 4514 | |
| 4515 | return_FindMissingSecurityOwners(input_api, output_api, file_patterns, |
| 4516 | excluded_patterns, |
| 4517 | 'build/fuchsia/SECURITY_OWNERS') |
| 4518 | |
| 4519 | |
| 4520 | defCheckSecurityOwners(input_api, output_api): |
| 4521 | """Checks that various security-sensitive files have an IPC OWNERS rule.""" |
| 4522 | ipc_results=_CheckChangeForIpcSecurityOwners(input_api, output_api) |
| 4523 | fuchsia_results=_CheckChangeForFuchsiaSecurityOwners( |
| 4524 | input_api, output_api) |
| 4525 | |
| 4526 | if ipc_results.has_security_sensitive_files: |
| 4527 | output_api.AppendCC('ipc-security-reviews@chromium.org') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4528 | |
| 4529 | results=[] |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4530 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4531 | missing_reviewer_problems=[] |
| 4532 | if ipc_results.missing_reviewer_problem: |
| 4533 | missing_reviewer_problems.append(ipc_results.missing_reviewer_problem) |
| 4534 | if fuchsia_results.missing_reviewer_problem: |
| 4535 | missing_reviewer_problems.append( |
| 4536 | fuchsia_results.missing_reviewer_problem) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4537 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4538 | # Missing reviewers are an error unless there's no issue number |
| 4539 | # associated with this branch; in that case, the presubmit is being run |
| 4540 | # with --all or --files. |
| 4541 | # |
| 4542 | # Note that upload should never be an error; otherwise, it would be |
| 4543 | # impossible to upload changes at all. |
| 4544 | if input_api.is_committingand input_api.change.issue: |
| 4545 | make_presubmit_message= output_api.PresubmitError |
| 4546 | else: |
| 4547 | make_presubmit_message= output_api.PresubmitNotifyResult |
| 4548 | for problemin missing_reviewer_problems: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4549 | results.append( |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4550 | make_presubmit_message(problem.problem, items=problem.items)) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4551 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4552 | owners_file_problems=[] |
| 4553 | owners_file_problems.extend(ipc_results.owners_file_problems) |
| 4554 | owners_file_problems.extend(fuchsia_results.owners_file_problems) |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4555 | |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4556 | for problemin owners_file_problems: |
Daniel Cheng | 3008dc1 | 2022-05-13 04:02:11 | [diff] [blame] | 4557 | # Missing per-file rules are always an error. While swarming and caching |
| 4558 | # means that uploading a patchset with updated OWNERS files and sending |
| 4559 | # it to the CQ again should not have a large incremental cost, it is |
| 4560 | # still frustrating to discover the error only after the change has |
| 4561 | # already been uploaded. |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4562 | results.append( |
Daniel Cheng | 171dad8d | 2022-05-21 00:40:25 | [diff] [blame] | 4563 | output_api.PresubmitError(problem.problem, items=problem.items)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4564 | |
| 4565 | return results |
| 4566 | |
| 4567 | |
| 4568 | def_GetFilesUsingSecurityCriticalFunctions(input_api): |
| 4569 | """Checks affected files for changes to security-critical calls. This |
| 4570 | function checks the full change diff, to catch both additions/changes |
| 4571 | and removals. |
| 4572 | |
| 4573 | Returns a dict keyed by file name, and the value is a set of detected |
| 4574 | functions. |
| 4575 | """ |
| 4576 | # Map of function pretty name (displayed in an error) to the pattern to |
| 4577 | # match it with. |
| 4578 | _PATTERNS_TO_CHECK={ |
| 4579 | 'content::GetServiceSandboxType<>()':'GetServiceSandboxType\\<' |
| 4580 | } |
| 4581 | _PATTERNS_TO_CHECK={ |
| 4582 | k: input_api.re.compile(v) |
| 4583 | for k, vin _PATTERNS_TO_CHECK.items() |
| 4584 | } |
| 4585 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4586 | # We don't want to trigger on strings within this file. |
| 4587 | def presubmit_file_filter(f): |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4588 | return'PRESUBMIT.py'!= input_api.os_path.split(f.LocalPath())[1] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4589 | |
| 4590 | # Scan all affected files for changes touching _FUNCTIONS_TO_CHECK. |
| 4591 | files_to_functions={} |
| 4592 | for fin input_api.AffectedFiles(file_filter=presubmit_file_filter): |
| 4593 | diff= f.GenerateScmDiff() |
| 4594 | for linein diff.split('\n'): |
| 4595 | # Not using just RightHandSideLines() because removing a |
| 4596 | # call to a security-critical function can be just as important |
| 4597 | # as adding or changing the arguments. |
| 4598 | if line.startswith('-')or(line.startswith('+') |
| 4599 | andnot line.startswith('++')): |
| 4600 | for name, patternin _PATTERNS_TO_CHECK.items(): |
| 4601 | if pattern.search(line): |
| 4602 | path= f.LocalPath() |
| 4603 | ifnot pathin files_to_functions: |
| 4604 | files_to_functions[path]= set() |
| 4605 | files_to_functions[path].add(name) |
| 4606 | return files_to_functions |
| 4607 | |
| 4608 | |
| 4609 | defCheckSecurityChanges(input_api, output_api): |
| 4610 | """Checks that changes involving security-critical functions are reviewed |
| 4611 | by the security team. |
| 4612 | """ |
| 4613 | files_to_functions=_GetFilesUsingSecurityCriticalFunctions(input_api) |
| 4614 | ifnot len(files_to_functions): |
| 4615 | return[] |
| 4616 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4617 | owners_file='ipc/SECURITY_OWNERS' |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4618 | if_ChangeHasSecurityReviewer(input_api, owners_file): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4619 | return[] |
| 4620 | |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 4621 | msg='The following files change calls to security-sensitive functions\n' \ |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4622 | 'that need to be reviewed by {}.\n'.format(owners_file) |
| 4623 | for path, namesin files_to_functions.items(): |
| 4624 | msg+=' {}\n'.format(path) |
| 4625 | for namein names: |
| 4626 | msg+=' {}\n'.format(name) |
| 4627 | msg+='\n' |
| 4628 | |
| 4629 | if input_api.is_committing: |
| 4630 | output= output_api.PresubmitError |
Mohamed Heikal | e217fc85 | 2020-07-06 19:44:03 | [diff] [blame] | 4631 | else: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4632 | output= output_api.PresubmitNotifyResult |
| 4633 | return[output(msg)] |
| 4634 | |
| 4635 | |
| 4636 | defCheckSetNoParent(input_api, output_api): |
| 4637 | """Checks that set noparent is only used together with an OWNERS file in |
| 4638 | //build/OWNERS.setnoparent (see also |
| 4639 | //docs/code_reviews.md#owners-files-details) |
| 4640 | """ |
| 4641 | # Return early if no OWNERS files were modified. |
| 4642 | ifnot any(f.LocalPath().endswith('OWNERS') |
| 4643 | for fin input_api.AffectedFiles(include_deletes=False)): |
| 4644 | return[] |
| 4645 | |
| 4646 | errors=[] |
| 4647 | |
| 4648 | allowed_owners_files_file='build/OWNERS.setnoparent' |
| 4649 | allowed_owners_files= set() |
Bruce Dawson | 58a45d2 | 2023-02-27 11:24:16 | [diff] [blame] | 4650 | with open(allowed_owners_files_file,'r', encoding='utf-8')as f: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4651 | for linein f: |
| 4652 | line= line.strip() |
| 4653 | ifnot lineor line.startswith('#'): |
| 4654 | continue |
| 4655 | allowed_owners_files.add(line) |
| 4656 | |
| 4657 | per_file_pattern= input_api.re.compile('per-file (.+)=(.+)') |
| 4658 | |
| 4659 | for fin input_api.AffectedFiles(include_deletes=False): |
| 4660 | ifnot f.LocalPath().endswith('OWNERS'): |
| 4661 | continue |
| 4662 | |
| 4663 | found_owners_files= set() |
| 4664 | found_set_noparent_lines= dict() |
| 4665 | |
| 4666 | # Parse the OWNERS file. |
| 4667 | for lineno, linein enumerate(f.NewContents(),1): |
| 4668 | line= line.strip() |
| 4669 | if line.startswith('set noparent'): |
| 4670 | found_set_noparent_lines['']= lineno |
| 4671 | if line.startswith('file://'): |
| 4672 | if linein allowed_owners_files: |
| 4673 | found_owners_files.add('') |
| 4674 | if line.startswith('per-file'): |
| 4675 | match= per_file_pattern.match(line) |
| 4676 | if match: |
| 4677 | glob= match.group(1).strip() |
| 4678 | directive= match.group(2).strip() |
| 4679 | if directive=='set noparent': |
| 4680 | found_set_noparent_lines[glob]= lineno |
| 4681 | if directive.startswith('file://'): |
| 4682 | if directivein allowed_owners_files: |
| 4683 | found_owners_files.add(glob) |
| 4684 | |
| 4685 | # Check that every set noparent line has a corresponding file:// line |
| 4686 | # listed in build/OWNERS.setnoparent. An exception is made for top level |
| 4687 | # directories since src/OWNERS shouldn't review them. |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4688 | linux_path= f.UnixLocalPath() |
Bruce Dawson | 6bb0d67 | 2022-04-06 15:13:49 | [diff] [blame] | 4689 | if(linux_path.count('/')!=1 |
| 4690 | and(not linux_pathin _EXCLUDED_SET_NO_PARENT_PATHS)): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4691 | for set_noparent_linein found_set_noparent_lines: |
| 4692 | if set_noparent_linein found_owners_files: |
| 4693 | continue |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 4694 | errors.append( |
| 4695 | ' %s:%d'% |
| 4696 | (linux_path, found_set_noparent_lines[set_noparent_line])) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4697 | |
| 4698 | results=[] |
| 4699 | if errors: |
| 4700 | if input_api.is_committing: |
| 4701 | output= output_api.PresubmitError |
| 4702 | else: |
| 4703 | output= output_api.PresubmitPromptWarning |
| 4704 | results.append( |
| 4705 | output( |
| 4706 | 'Found the following "set noparent" restrictions in OWNERS files that ' |
| 4707 | 'do not include owners from build/OWNERS.setnoparent:', |
| 4708 | long_text='\n\n'.join(errors))) |
| 4709 | return results |
| 4710 | |
| 4711 | |
| 4712 | defCheckUselessForwardDeclarations(input_api, output_api): |
| 4713 | """Checks that added or removed lines in non third party affected |
| 4714 | header files do not lead to new useless class or struct forward |
| 4715 | declaration. |
| 4716 | """ |
| 4717 | results=[] |
| 4718 | class_pattern= input_api.re.compile(r'^class\s+(\w+);$', |
| 4719 | input_api.re.MULTILINE) |
| 4720 | struct_pattern= input_api.re.compile(r'^struct\s+(\w+);$', |
| 4721 | input_api.re.MULTILINE) |
| 4722 | for fin input_api.AffectedFiles(include_deletes=False): |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4723 | local_path= f.UnixLocalPath() |
| 4724 | if(local_path.startswith('third_party') |
| 4725 | andnot local_path.startswith('third_party/blink')): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4726 | continue |
| 4727 | |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 4728 | ifnot local_path.endswith('.h'): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4729 | continue |
| 4730 | |
| 4731 | contents= input_api.ReadFile(f) |
| 4732 | fwd_decls= input_api.re.findall(class_pattern, contents) |
| 4733 | fwd_decls.extend(input_api.re.findall(struct_pattern, contents)) |
| 4734 | |
| 4735 | useless_fwd_decls=[] |
| 4736 | for declin fwd_decls: |
| 4737 | count= sum(1for _in input_api.re.finditer( |
| 4738 | r'\b%s\b'% input_api.re.escape(decl), contents)) |
| 4739 | if count==1: |
| 4740 | useless_fwd_decls.append(decl) |
| 4741 | |
| 4742 | ifnot useless_fwd_decls: |
| 4743 | continue |
| 4744 | |
| 4745 | for linein f.GenerateScmDiff().splitlines(): |
| 4746 | if(line.startswith('-')andnot line.startswith('--') |
| 4747 | or line.startswith('+')andnot line.startswith('++')): |
| 4748 | for declin useless_fwd_decls: |
| 4749 | if input_api.re.search(r'\b%s\b'% decl, line[1:]): |
| 4750 | results.append( |
| 4751 | output_api.PresubmitPromptWarning( |
| 4752 | '%s: %s forward declaration is no longer needed' |
| 4753 | %(f.LocalPath(), decl))) |
| 4754 | useless_fwd_decls.remove(decl) |
| 4755 | |
| 4756 | return results |
| 4757 | |
| 4758 | |
| 4759 | def_CheckAndroidDebuggableBuild(input_api, output_api): |
| 4760 | """Checks that code uses BuildInfo.isDebugAndroid() instead of |
| 4761 | Build.TYPE.equals('') or ''.equals(Build.TYPE) to check if |
| 4762 | this is a debuggable build of Android. |
| 4763 | """ |
| 4764 | build_type_check_pattern= input_api.re.compile( |
| 4765 | r'\bBuild\.TYPE\.equals\(|\.equals\(\s*\bBuild\.TYPE\)') |
| 4766 | |
| 4767 | errors=[] |
| 4768 | |
| 4769 | sources=lambda affected_file: input_api.FilterSourceFile( |
| 4770 | affected_file, |
| 4771 | files_to_skip=( |
| 4772 | _EXCLUDED_PATHS+ _TEST_CODE_EXCLUDED_PATHS+ input_api. |
| 4773 | DEFAULT_FILES_TO_SKIP+( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4774 | r"^android_webview/support_library/boundary_interfaces/", |
| 4775 | r"^chrome/android/webapk/.*", |
| 4776 | r'^third_party/.*', |
| 4777 | r"tools/android/customtabs_benchmark/.*", |
| 4778 | r"webview/chromium/License.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4779 | )), |
| 4780 | files_to_check=[r'.*\.java$']) |
| 4781 | |
| 4782 | for fin input_api.AffectedSourceFiles(sources): |
| 4783 | for line_num, linein f.ChangedContents(): |
| 4784 | if build_type_check_pattern.search(line): |
| 4785 | errors.append("%s:%d"%(f.LocalPath(), line_num)) |
| 4786 | |
| 4787 | results=[] |
| 4788 | |
| 4789 | if errors: |
| 4790 | results.append( |
| 4791 | output_api.PresubmitPromptWarning( |
| 4792 | 'Build.TYPE.equals or .equals(Build.TYPE) usage is detected.' |
| 4793 | ' Please use BuildInfo.isDebugAndroid() instead.', errors)) |
| 4794 | |
| 4795 | return results |
| 4796 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 4797 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4798 | # TODO: add unit tests |
| 4799 | def_CheckAndroidToastUsage(input_api, output_api): |
| 4800 | """Checks that code uses org.chromium.ui.widget.Toast instead of |
| 4801 | android.widget.Toast (Chromium Toast doesn't force hardware |
| 4802 | acceleration on low-end devices, saving memory). |
| 4803 | """ |
| 4804 | toast_import_pattern= input_api.re.compile( |
| 4805 | r'^import android\.widget\.Toast;$') |
| 4806 | |
| 4807 | errors=[] |
| 4808 | |
| 4809 | sources=lambda affected_file: input_api.FilterSourceFile( |
| 4810 | affected_file, |
| 4811 | files_to_skip=(_EXCLUDED_PATHS+ _TEST_CODE_EXCLUDED_PATHS+ input_api. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 4812 | DEFAULT_FILES_TO_SKIP+ |
| 4813 | (r'^chromecast/.*', r'^remoting/.*')), |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4814 | files_to_check=[r'.*\.java$']) |
| 4815 | |
| 4816 | for fin input_api.AffectedSourceFiles(sources): |
| 4817 | for line_num, linein f.ChangedContents(): |
| 4818 | if toast_import_pattern.search(line): |
| 4819 | errors.append("%s:%d"%(f.LocalPath(), line_num)) |
| 4820 | |
| 4821 | results=[] |
| 4822 | |
| 4823 | if errors: |
| 4824 | results.append( |
| 4825 | output_api.PresubmitError( |
| 4826 | 'android.widget.Toast usage is detected. Android toasts use hardware' |
| 4827 | ' acceleration, and can be\ncostly on low-end devices. Please use' |
| 4828 | ' org.chromium.ui.widget.Toast instead.\n' |
| 4829 | 'Contact dskiba@chromium.org if you have any questions.', |
| 4830 | errors)) |
| 4831 | |
| 4832 | return results |
| 4833 | |
| 4834 | |
| 4835 | def_CheckAndroidCrLogUsage(input_api, output_api): |
| 4836 | """Checks that new logs using org.chromium.base.Log: |
| 4837 | - Are using 'TAG' as variable name for the tags (warn) |
| 4838 | - Are using a tag that is shorter than 20 characters (error) |
| 4839 | """ |
| 4840 | |
| 4841 | # Do not check format of logs in the given files |
| 4842 | cr_log_check_excluded_paths=[ |
| 4843 | # //chrome/android/webapk cannot depend on //base |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4844 | r"^chrome/android/webapk/.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4845 | # WebView license viewer code cannot depend on //base; used in stub APK. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4846 | r"^android_webview/glue/java/src/com/android/" |
| 4847 | r"webview/chromium/License.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4848 | # The customtabs_benchmark is a small app that does not depend on Chromium |
| 4849 | # java pieces. |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 4850 | r"tools/android/customtabs_benchmark/.*", |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4851 | ] |
| 4852 | |
| 4853 | cr_log_import_pattern= input_api.re.compile( |
| 4854 | r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE) |
| 4855 | class_in_base_pattern= input_api.re.compile( |
| 4856 | r'^package org\.chromium\.base;$', input_api.re.MULTILINE) |
| 4857 | has_some_log_import_pattern= input_api.re.compile(r'^import .*\.Log;$', |
| 4858 | input_api.re.MULTILINE) |
| 4859 | # Extract the tag from lines like `Log.d(TAG, "*");` or `Log.d("TAG", "*");` |
| 4860 | log_call_pattern= input_api.re.compile(r'\bLog\.\w\((?P<tag>\"?\w+)') |
| 4861 | log_decl_pattern= input_api.re.compile( |
| 4862 | r'static final String TAG = "(?P<name>(.*))"') |
| 4863 | rough_log_decl_pattern= input_api.re.compile(r'\bString TAG\s*=') |
| 4864 | |
| 4865 | REF_MSG=('See docs/android_logging.md for more info.') |
| 4866 | sources=lambda x: input_api.FilterSourceFile( |
| 4867 | x, |
| 4868 | files_to_check=[r'.*\.java$'], |
| 4869 | files_to_skip=cr_log_check_excluded_paths) |
| 4870 | |
| 4871 | tag_decl_errors=[] |
Andrew Grieve | d3a35d8 | 2024-01-02 21:24:38 | [diff] [blame] | 4872 | tag_length_errors=[] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4873 | tag_errors=[] |
| 4874 | tag_with_dot_errors=[] |
| 4875 | util_log_errors=[] |
| 4876 | |
| 4877 | for fin input_api.AffectedSourceFiles(sources): |
| 4878 | file_content= input_api.ReadFile(f) |
| 4879 | has_modified_logs=False |
| 4880 | # Per line checks |
| 4881 | if(cr_log_import_pattern.search(file_content) |
| 4882 | or(class_in_base_pattern.search(file_content) |
| 4883 | andnot has_some_log_import_pattern.search(file_content))): |
| 4884 | # Checks to run for files using cr log |
| 4885 | for line_num, linein f.ChangedContents(): |
| 4886 | if rough_log_decl_pattern.search(line): |
| 4887 | has_modified_logs=True |
| 4888 | |
| 4889 | # Check if the new line is doing some logging |
| 4890 | match= log_call_pattern.search(line) |
| 4891 | if match: |
| 4892 | has_modified_logs=True |
| 4893 | |
| 4894 | # Make sure it uses "TAG" |
| 4895 | ifnot match.group('tag')=='TAG': |
| 4896 | tag_errors.append("%s:%d"%(f.LocalPath(), line_num)) |
| 4897 | else: |
| 4898 | # Report non cr Log function calls in changed lines |
| 4899 | for line_num, linein f.ChangedContents(): |
| 4900 | if log_call_pattern.search(line): |
| 4901 | util_log_errors.append("%s:%d"%(f.LocalPath(), line_num)) |
| 4902 | |
| 4903 | # Per file checks |
| 4904 | if has_modified_logs: |
| 4905 | # Make sure the tag is using the "cr" prefix and is not too long |
| 4906 | match= log_decl_pattern.search(file_content) |
| 4907 | tag_name= match.group('name')if matchelseNone |
| 4908 | ifnot tag_name: |
| 4909 | tag_decl_errors.append(f.LocalPath()) |
Andrew Grieve | d3a35d8 | 2024-01-02 21:24:38 | [diff] [blame] | 4910 | elif len(tag_name)>20: |
| 4911 | tag_length_errors.append(f.LocalPath()) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4912 | elif'.'in tag_name: |
| 4913 | tag_with_dot_errors.append(f.LocalPath()) |
| 4914 | |
| 4915 | results=[] |
| 4916 | if tag_decl_errors: |
| 4917 | results.append( |
| 4918 | output_api.PresubmitPromptWarning( |
| 4919 | 'Please define your tags using the suggested format: .\n' |
| 4920 | '"private static final String TAG = "<package tag>".\n' |
| 4921 | 'They will be prepended with "cr_" automatically.\n'+ REF_MSG, |
| 4922 | tag_decl_errors)) |
| 4923 | |
Andrew Grieve | d3a35d8 | 2024-01-02 21:24:38 | [diff] [blame] | 4924 | if tag_length_errors: |
| 4925 | results.append( |
| 4926 | output_api.PresubmitError( |
| 4927 | 'The tag length is restricted by the system to be at most ' |
| 4928 | '20 characters.\n'+ REF_MSG, tag_length_errors)) |
| 4929 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4930 | if tag_errors: |
| 4931 | results.append( |
| 4932 | output_api.PresubmitPromptWarning( |
| 4933 | 'Please use a variable named "TAG" for your log tags.\n'+ |
| 4934 | REF_MSG, tag_errors)) |
| 4935 | |
| 4936 | if util_log_errors: |
| 4937 | results.append( |
| 4938 | output_api.PresubmitPromptWarning( |
| 4939 | 'Please use org.chromium.base.Log for new logs.\n'+ REF_MSG, |
| 4940 | util_log_errors)) |
| 4941 | |
| 4942 | if tag_with_dot_errors: |
| 4943 | results.append( |
| 4944 | output_api.PresubmitPromptWarning( |
| 4945 | 'Dot in log tags cause them to be elided in crash reports.\n'+ |
| 4946 | REF_MSG, tag_with_dot_errors)) |
| 4947 | |
| 4948 | return results |
| 4949 | |
| 4950 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4951 | def_CheckAndroidTestAnnotationUsage(input_api, output_api): |
| 4952 | """Checks that android.test.suitebuilder.annotation.* is no longer used.""" |
| 4953 | deprecated_annotation_import_pattern= input_api.re.compile( |
| 4954 | r'^import android\.test\.suitebuilder\.annotation\..*;', |
| 4955 | input_api.re.MULTILINE) |
| 4956 | sources=lambda x: input_api.FilterSourceFile( |
| 4957 | x, files_to_check=[r'.*\.java$'], files_to_skip=None) |
| 4958 | errors=[] |
| 4959 | for fin input_api.AffectedFiles(file_filter=sources): |
| 4960 | for line_num, linein f.ChangedContents(): |
| 4961 | if deprecated_annotation_import_pattern.search(line): |
| 4962 | errors.append("%s:%d"%(f.LocalPath(), line_num)) |
| 4963 | |
| 4964 | results=[] |
| 4965 | if errors: |
| 4966 | results.append( |
| 4967 | output_api.PresubmitError( |
| 4968 | 'Annotations in android.test.suitebuilder.annotation have been' |
Mohamed Heikal | 3d7a94c | 2023-03-28 16:55:24 | [diff] [blame] | 4969 | ' deprecated since API level 24. Please use androidx.test.filters' |
| 4970 | ' from //third_party/androidx:androidx_test_runner_java instead.' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4971 | ' Contact yolandyan@chromium.org if you have any questions.', |
| 4972 | errors)) |
| 4973 | return results |
| 4974 | |
| 4975 | |
| 4976 | def_CheckAndroidNewMdpiAssetLocation(input_api, output_api): |
| 4977 | """Checks if MDPI assets are placed in a correct directory.""" |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 4978 | file_filter=lambda f:(f.UnixLocalPath().endswith('.png')and |
| 4979 | ('/res/drawable/'in f.UnixLocalPath()or |
| 4980 | '/res/drawable-ldrtl/'in f.UnixLocalPath())) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 4981 | errors=[] |
| 4982 | for fin input_api.AffectedFiles(include_deletes=False, |
| 4983 | file_filter=file_filter): |
| 4984 | errors.append(' %s'% f.LocalPath()) |
| 4985 | |
| 4986 | results=[] |
| 4987 | if errors: |
| 4988 | results.append( |
| 4989 | output_api.PresubmitError( |
| 4990 | 'MDPI assets should be placed in /res/drawable-mdpi/ or ' |
| 4991 | '/res/drawable-ldrtl-mdpi/\ninstead of /res/drawable/ and' |
| 4992 | '/res/drawable-ldrtl/.\n' |
| 4993 | 'Contact newt@chromium.org if you have questions.', errors)) |
| 4994 | return results |
| 4995 | |
| 4996 | |
| 4997 | def_CheckAndroidWebkitImports(input_api, output_api): |
| 4998 | """Checks that code uses org.chromium.base.Callback instead of |
| 4999 | android.webview.ValueCallback except in the WebView glue layer |
| 5000 | and WebLayer. |
| 5001 | """ |
| 5002 | valuecallback_import_pattern= input_api.re.compile( |
| 5003 | r'^import android\.webkit\.ValueCallback;$') |
| 5004 | |
| 5005 | errors=[] |
| 5006 | |
| 5007 | sources=lambda affected_file: input_api.FilterSourceFile( |
| 5008 | affected_file, |
| 5009 | files_to_skip=(_EXCLUDED_PATHS+ _TEST_CODE_EXCLUDED_PATHS+ input_api. |
| 5010 | DEFAULT_FILES_TO_SKIP+( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5011 | r'^android_webview/glue/.*', |
elabadysayed | cbbaea7 | 2024-08-01 16:10:42 | [diff] [blame] | 5012 | r'^android_webview/support_library/.*', |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5013 | r'^weblayer/.*', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5014 | )), |
| 5015 | files_to_check=[r'.*\.java$']) |
| 5016 | |
| 5017 | for fin input_api.AffectedSourceFiles(sources): |
| 5018 | for line_num, linein f.ChangedContents(): |
| 5019 | if valuecallback_import_pattern.search(line): |
| 5020 | errors.append("%s:%d"%(f.LocalPath(), line_num)) |
| 5021 | |
| 5022 | results=[] |
| 5023 | |
| 5024 | if errors: |
| 5025 | results.append( |
| 5026 | output_api.PresubmitError( |
| 5027 | 'android.webkit.ValueCallback usage is detected outside of the glue' |
| 5028 | ' layer. To stay compatible with the support library, android.webkit.*' |
| 5029 | ' classes should only be used inside the glue layer and' |
| 5030 | ' org.chromium.base.Callback should be used instead.', errors)) |
| 5031 | |
| 5032 | return results |
| 5033 | |
| 5034 | |
| 5035 | def_CheckAndroidXmlStyle(input_api, output_api, is_check_on_upload): |
| 5036 | """Checks Android XML styles """ |
| 5037 | |
| 5038 | # Return early if no relevant files were modified. |
| 5039 | ifnot any( |
| 5040 | _IsXmlOrGrdFile(input_api, f.LocalPath()) |
| 5041 | for fin input_api.AffectedFiles(include_deletes=False)): |
| 5042 | return[] |
| 5043 | |
| 5044 | import sys |
| 5045 | original_sys_path= sys.path |
| 5046 | try: |
| 5047 | sys.path= sys.path+[ |
| 5048 | input_api.os_path.join(input_api.PresubmitLocalPath(),'tools', |
| 5049 | 'android','checkxmlstyle') |
| 5050 | ] |
| 5051 | import checkxmlstyle |
| 5052 | finally: |
| 5053 | # Restore sys.path to what it was before. |
| 5054 | sys.path= original_sys_path |
| 5055 | |
| 5056 | if is_check_on_upload: |
| 5057 | return checkxmlstyle.CheckStyleOnUpload(input_api, output_api) |
| 5058 | else: |
| 5059 | return checkxmlstyle.CheckStyleOnCommit(input_api, output_api) |
| 5060 | |
| 5061 | |
| 5062 | def_CheckAndroidInfoBarDeprecation(input_api, output_api): |
| 5063 | """Checks Android Infobar Deprecation """ |
| 5064 | |
| 5065 | import sys |
| 5066 | original_sys_path= sys.path |
| 5067 | try: |
| 5068 | sys.path= sys.path+[ |
| 5069 | input_api.os_path.join(input_api.PresubmitLocalPath(),'tools', |
| 5070 | 'android','infobar_deprecation') |
| 5071 | ] |
| 5072 | import infobar_deprecation |
| 5073 | finally: |
| 5074 | # Restore sys.path to what it was before. |
| 5075 | sys.path= original_sys_path |
| 5076 | |
| 5077 | return infobar_deprecation.CheckDeprecationOnUpload(input_api, output_api) |
| 5078 | |
| 5079 | |
| 5080 | class_PydepsCheckerResult: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5081 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5082 | def __init__(self, cmd, pydeps_path, process, old_contents): |
| 5083 | self._cmd= cmd |
| 5084 | self._pydeps_path= pydeps_path |
| 5085 | self._process= process |
| 5086 | self._old_contents= old_contents |
| 5087 | |
| 5088 | defGetError(self): |
| 5089 | """Returns an error message, or None.""" |
| 5090 | import difflib |
Andrew Grieve | d27620b6 | 2023-07-13 16:35:07 | [diff] [blame] | 5091 | new_contents= self._process.stdout.read().splitlines()[2:] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5092 | if self._process.wait()!=0: |
| 5093 | # STDERR should already be printed. |
| 5094 | return'Command failed: '+ self._cmd |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5095 | if self._old_contents!= new_contents: |
| 5096 | diff='\n'.join( |
| 5097 | difflib.context_diff(self._old_contents, new_contents)) |
| 5098 | return('File is stale: {}\n' |
| 5099 | 'Diff (apply to fix):\n' |
| 5100 | '{}\n' |
| 5101 | 'To regenerate, run:\n\n' |
| 5102 | ' {}').format(self._pydeps_path, diff, self._cmd) |
| 5103 | returnNone |
| 5104 | |
| 5105 | |
| 5106 | classPydepsChecker: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5107 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5108 | def __init__(self, input_api, pydeps_files): |
| 5109 | self._file_cache={} |
| 5110 | self._input_api= input_api |
| 5111 | self._pydeps_files= pydeps_files |
| 5112 | |
| 5113 | def_LoadFile(self, path): |
| 5114 | """Returns the list of paths within a .pydeps file relative to //.""" |
| 5115 | if pathnotin self._file_cache: |
| 5116 | with open(path, encoding='utf-8')as f: |
| 5117 | self._file_cache[path]= f.read() |
| 5118 | return self._file_cache[path] |
| 5119 | |
| 5120 | def_ComputeNormalizedPydepsEntries(self, pydeps_path): |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 5121 | """Returns an iterable of paths within the .pydep, relativized to //.""" |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5122 | pydeps_data= self._LoadFile(pydeps_path) |
| 5123 | uses_gn_paths='--gn-paths'in pydeps_data |
| 5124 | entries=(lfor lin pydeps_data.splitlines() |
| 5125 | ifnot l.startswith('#')) |
| 5126 | if uses_gn_paths: |
| 5127 | # Paths look like: //foo/bar/baz |
| 5128 | return(e[2:]for ein entries) |
| 5129 | else: |
| 5130 | # Paths look like: path/relative/to/file.pydeps |
| 5131 | os_path= self._input_api.os_path |
| 5132 | pydeps_dir= os_path.dirname(pydeps_path) |
| 5133 | return(os_path.normpath(os_path.join(pydeps_dir, e)) |
| 5134 | for ein entries) |
| 5135 | |
| 5136 | def_CreateFilesToPydepsMap(self): |
| 5137 | """Returns a map of local_path -> list_of_pydeps.""" |
| 5138 | ret={} |
| 5139 | for pydep_local_pathin self._pydeps_files: |
| 5140 | for pathin self._ComputeNormalizedPydepsEntries(pydep_local_path): |
| 5141 | ret.setdefault(path,[]).append(pydep_local_path) |
| 5142 | return ret |
| 5143 | |
| 5144 | defComputeAffectedPydeps(self): |
| 5145 | """Returns an iterable of .pydeps files that might need regenerating.""" |
| 5146 | affected_pydeps= set() |
| 5147 | file_to_pydeps_map=None |
| 5148 | for fin self._input_api.AffectedFiles(include_deletes=True): |
| 5149 | local_path= f.LocalPath() |
| 5150 | # Changes to DEPS can lead to .pydeps changes if any .py files are in |
| 5151 | # subrepositories. We can't figure out which files change, so re-check |
| 5152 | # all files. |
| 5153 | # Changes to print_python_deps.py affect all .pydeps. |
| 5154 | if local_pathin('DEPS','PRESUBMIT.py' |
| 5155 | )or local_path.endswith('print_python_deps.py'): |
| 5156 | return self._pydeps_files |
| 5157 | elif local_path.endswith('.pydeps'): |
| 5158 | if local_pathin self._pydeps_files: |
| 5159 | affected_pydeps.add(local_path) |
| 5160 | elif local_path.endswith('.py'): |
| 5161 | if file_to_pydeps_mapisNone: |
| 5162 | file_to_pydeps_map= self._CreateFilesToPydepsMap() |
| 5163 | affected_pydeps.update(file_to_pydeps_map.get(local_path,())) |
| 5164 | return affected_pydeps |
| 5165 | |
| 5166 | defDetermineIfStaleAsync(self, pydeps_path): |
| 5167 | """Runs print_python_deps.py to see if the files is stale.""" |
| 5168 | import os |
| 5169 | |
| 5170 | old_pydeps_data= self._LoadFile(pydeps_path).splitlines() |
| 5171 | if old_pydeps_data: |
| 5172 | cmd= old_pydeps_data[1][1:].strip() |
| 5173 | if'--output'notin cmd: |
| 5174 | cmd+=' --output '+ pydeps_path |
| 5175 | old_contents= old_pydeps_data[2:] |
| 5176 | else: |
| 5177 | # A default cmd that should work in most cases (as long as pydeps filename |
| 5178 | # matches the script name) so that PRESUBMIT.py does not crash if pydeps |
| 5179 | # file is empty/new. |
| 5180 | cmd='build/print_python_deps.py {} --root={} --output={}'.format( |
| 5181 | pydeps_path[:-4], os.path.dirname(pydeps_path), pydeps_path) |
| 5182 | old_contents=[] |
| 5183 | env= dict(os.environ) |
| 5184 | env['PYTHONDONTWRITEBYTECODE']='1' |
| 5185 | process= self._input_api.subprocess.Popen( |
| 5186 | cmd+' --output ""', |
| 5187 | shell=True, |
| 5188 | env=env, |
| 5189 | stdout=self._input_api.subprocess.PIPE, |
| 5190 | encoding='utf-8') |
| 5191 | return_PydepsCheckerResult(cmd, pydeps_path, process, old_contents) |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 5192 | |
| 5193 | |
Tibor Goldschwendt | 360793f7 | 2019-06-25 18:23:49 | [diff] [blame] | 5194 | def_ParseGclientArgs(): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5195 | args={} |
| 5196 | with open('build/config/gclient_args.gni','r')as f: |
| 5197 | for linein f: |
| 5198 | line= line.strip() |
| 5199 | ifnot lineor line.startswith('#'): |
| 5200 | continue |
| 5201 | attribute, value= line.split('=') |
| 5202 | args[attribute.strip()]= value.strip() |
| 5203 | return args |
Tibor Goldschwendt | 360793f7 | 2019-06-25 18:23:49 | [diff] [blame] | 5204 | |
| 5205 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5206 | defCheckPydepsNeedsUpdating(input_api, output_api, checker_for_tests=None): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5207 | """Checks if a .pydeps file needs to be regenerated.""" |
| 5208 | # This check is for Python dependency lists (.pydeps files), and involves |
| 5209 | # paths not only in the PRESUBMIT.py, but also in the .pydeps files. It |
| 5210 | # doesn't work on Windows and Mac, so skip it on other platforms. |
| 5211 | ifnot input_api.platform.startswith('linux'): |
| 5212 | return[] |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 5213 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5214 | results=[] |
| 5215 | # First, check for new / deleted .pydeps. |
| 5216 | for fin input_api.AffectedFiles(include_deletes=True): |
| 5217 | # Check whether we are running the presubmit check for a file in src. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5218 | if f.LocalPath().endswith('.pydeps'): |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 5219 | # f.LocalPath is relative to repo (src, or internal repo). |
| 5220 | # os_path.exists is relative to src repo. |
| 5221 | # Therefore if os_path.exists is true, it means f.LocalPath is relative |
| 5222 | # to src and we can conclude that the pydeps is in src. |
| 5223 | exists= input_api.os_path.exists(f.LocalPath()) |
| 5224 | if f.Action()=='D'and f.LocalPath()in _ALL_PYDEPS_FILES: |
| 5225 | results.append( |
| 5226 | output_api.PresubmitError( |
| 5227 | 'Please update _ALL_PYDEPS_FILES within //PRESUBMIT.py to ' |
| 5228 | 'remove %s'% f.LocalPath())) |
| 5229 | elif(f.Action()!='D'and exists |
| 5230 | and f.LocalPath()notin _ALL_PYDEPS_FILES): |
| 5231 | results.append( |
| 5232 | output_api.PresubmitError( |
| 5233 | 'Please update _ALL_PYDEPS_FILES within //PRESUBMIT.py to ' |
| 5234 | 'include %s'% f.LocalPath())) |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 5235 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5236 | if results: |
| 5237 | return results |
| 5238 | |
Gavin Mak | 2388440 | 2024-07-25 20:39:26 | [diff] [blame] | 5239 | try: |
| 5240 | parsed_args=_ParseGclientArgs() |
| 5241 | exceptFileNotFoundError: |
| 5242 | message=( |
| 5243 | 'build/config/gclient_args.gni not found. Please make sure your ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5244 | 'workspace has been initialized with gclient sync.') |
Gavin Mak | 2388440 | 2024-07-25 20:39:26 | [diff] [blame] | 5245 | import sys |
| 5246 | original_sys_path= sys.path |
| 5247 | try: |
| 5248 | sys.path= sys.path+[ |
| 5249 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5250 | 'third_party','depot_tools') |
Gavin Mak | 2388440 | 2024-07-25 20:39:26 | [diff] [blame] | 5251 | ] |
| 5252 | import gclient_utils |
| 5253 | if gclient_utils.IsEnvCog(): |
| 5254 | # Users will always hit this when they run presubmits before cog |
| 5255 | # workspace initialization finishes. The check shouldn't fail in |
| 5256 | # this case. This is an unavoidable workaround that's needed for |
| 5257 | # good presubmit UX for cog. |
| 5258 | results.append(output_api.PresubmitPromptWarning(message)) |
| 5259 | else: |
| 5260 | results.append(output_api.PresubmitError(message)) |
| 5261 | return results |
| 5262 | finally: |
| 5263 | # Restore sys.path to what it was before. |
| 5264 | sys.path= original_sys_path |
| 5265 | |
| 5266 | is_android= parsed_args.get('checkout_android','false')=='true' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5267 | checker= checker_for_testsorPydepsChecker(input_api, _ALL_PYDEPS_FILES) |
| 5268 | affected_pydeps= set(checker.ComputeAffectedPydeps()) |
| 5269 | affected_android_pydeps= affected_pydeps.intersection( |
| 5270 | set(_ANDROID_SPECIFIC_PYDEPS_FILES)) |
| 5271 | if affected_android_pydepsandnot is_android: |
| 5272 | results.append( |
| 5273 | output_api.PresubmitPromptOrNotify( |
| 5274 | 'You have changed python files that may affect pydeps for android\n' |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 5275 | 'specific scripts. However, the relevant presubmit check cannot be\n' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5276 | 'run because you are not using an Android checkout. To validate that\n' |
| 5277 | 'the .pydeps are correct, re-run presubmit in an Android checkout, or\n' |
| 5278 | 'use the android-internal-presubmit optional trybot.\n' |
| 5279 | 'Possibly stale pydeps files:\n{}'.format( |
| 5280 | '\n'.join(affected_android_pydeps)))) |
| 5281 | |
| 5282 | all_pydeps= _ALL_PYDEPS_FILESif is_androidelse _GENERIC_PYDEPS_FILES |
| 5283 | pydeps_to_check= affected_pydeps.intersection(all_pydeps) |
| 5284 | # Process these concurrently, as each one takes 1-2 seconds. |
| 5285 | pydep_results=[checker.DetermineIfStaleAsync(p)for pin pydeps_to_check] |
| 5286 | for resultin pydep_results: |
| 5287 | error_msg= result.GetError() |
| 5288 | if error_msg: |
| 5289 | results.append(output_api.PresubmitError(error_msg)) |
| 5290 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 5291 | return results |
| 5292 | |
agrieve | f32bcc7 | 2016-04-04 14:57:40 | [diff] [blame] | 5293 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5294 | defCheckSingletonInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5295 | """Checks to make sure no header files have |Singleton<|.""" |
| 5296 | |
| 5297 | defFileFilter(affected_file): |
| 5298 | # It's ok for base/memory/singleton.h to have |Singleton<|. |
| 5299 | files_to_skip=(_EXCLUDED_PATHS+ input_api.DEFAULT_FILES_TO_SKIP+ |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5300 | (r"^base/memory/singleton\.h$", |
| 5301 | r"^net/quic/platform/impl/quic_singleton_impl\.h$")) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5302 | return input_api.FilterSourceFile(affected_file, |
| 5303 | files_to_skip=files_to_skip) |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 5304 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5305 | pattern= input_api.re.compile(r'(?<!class\sbase::)Singleton\s*<') |
| 5306 | files=[] |
| 5307 | for fin input_api.AffectedSourceFiles(FileFilter): |
| 5308 | if(f.LocalPath().endswith('.h')or f.LocalPath().endswith('.hxx') |
| 5309 | or f.LocalPath().endswith('.hpp') |
| 5310 | or f.LocalPath().endswith('.inl')): |
| 5311 | contents= input_api.ReadFile(f) |
| 5312 | for linein contents.splitlines(False): |
| 5313 | if(not line.lstrip().startswith('//') |
| 5314 | and# Strip C++ comment. |
| 5315 | pattern.search(line)): |
| 5316 | files.append(f) |
| 5317 | break |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 5318 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5319 | if files: |
| 5320 | return[ |
| 5321 | output_api.PresubmitError( |
| 5322 | 'Found base::Singleton<T> in the following header files.\n'+ |
| 5323 | 'Please move them to an appropriate source file so that the '+ |
| 5324 | 'template gets instantiated in a single compilation unit.', |
| 5325 | files) |
| 5326 | ] |
| 5327 | return[] |
glider | e61efad | 2015-02-18 17:39:43 | [diff] [blame] | 5328 | |
| 5329 | |
jchaffraix@chromium.org | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 5330 | _DEPRECATED_CSS=[ |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5331 | # Values |
| 5332 | ("-webkit-box","flex"), |
| 5333 | ("-webkit-inline-box","inline-flex"), |
| 5334 | ("-webkit-flex","flex"), |
| 5335 | ("-webkit-inline-flex","inline-flex"), |
| 5336 | ("-webkit-min-content","min-content"), |
| 5337 | ("-webkit-max-content","max-content"), |
jchaffraix@chromium.org | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 5338 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5339 | # Properties |
| 5340 | ("-webkit-background-clip","background-clip"), |
| 5341 | ("-webkit-background-origin","background-origin"), |
| 5342 | ("-webkit-background-size","background-size"), |
| 5343 | ("-webkit-box-shadow","box-shadow"), |
| 5344 | ("-webkit-user-select","user-select"), |
jchaffraix@chromium.org | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 5345 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5346 | # Functions |
| 5347 | ("-webkit-gradient","gradient"), |
| 5348 | ("-webkit-repeating-gradient","repeating-gradient"), |
| 5349 | ("-webkit-linear-gradient","linear-gradient"), |
| 5350 | ("-webkit-repeating-linear-gradient","repeating-linear-gradient"), |
| 5351 | ("-webkit-radial-gradient","radial-gradient"), |
| 5352 | ("-webkit-repeating-radial-gradient","repeating-radial-gradient"), |
jchaffraix@chromium.org | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 5353 | ] |
| 5354 | |
Wei-Yin Chen (陳威尹) | f799d44 | 2018-07-31 02:20:20 | [diff] [blame] | 5355 | |
Wei-Yin Chen (陳威尹) | dca729a | 2018-07-31 21:35:49 | [diff] [blame] | 5356 | # TODO: add unit tests |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5357 | defCheckNoDeprecatedCss(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5358 | """ Make sure that we don't use deprecated CSS |
| 5359 | properties, functions or values. Our external |
| 5360 | documentation and iOS CSS for dom distiller |
| 5361 | (reader mode) are ignored by the hooks as it |
| 5362 | needs to be consumed by WebKit. """ |
| 5363 | results=[] |
| 5364 | file_inclusion_pattern=[r".+\.css$"] |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5365 | files_to_skip=( |
| 5366 | _EXCLUDED_PATHS+ _TEST_CODE_EXCLUDED_PATHS+ |
| 5367 | input_api.DEFAULT_FILES_TO_SKIP+ |
| 5368 | (# Legacy CSS file using deprecated CSS. |
| 5369 | r"^chrome/browser/resources/chromeos/arc_support/cr_overlay.css$", |
| 5370 | r"^chrome/common/extensions/docs", |
| 5371 | r"^chrome/docs", |
| 5372 | r"^native_client_sdk", |
| 5373 | # The NTP team prefers reserving -webkit-line-clamp for |
| 5374 | # ellipsis effect which can only be used with -webkit-box. |
| 5375 | r"ui/webui/resources/cr_components/most_visited/.*\.css$")) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5376 | file_filter=lambda f: input_api.FilterSourceFile( |
| 5377 | f, files_to_check=file_inclusion_pattern, files_to_skip=files_to_skip) |
| 5378 | for fpathin input_api.AffectedFiles(file_filter=file_filter): |
| 5379 | for line_num, linein fpath.ChangedContents(): |
| 5380 | for(deprecated_value, value)in _DEPRECATED_CSS: |
| 5381 | if deprecated_valuein line: |
| 5382 | results.append( |
| 5383 | output_api.PresubmitError( |
| 5384 | "%s:%d: Use of deprecated CSS %s, use %s instead"% |
| 5385 | (fpath.LocalPath(), line_num, deprecated_value, |
| 5386 | value))) |
| 5387 | return results |
jchaffraix@chromium.org | fd20b90 | 2014-05-09 02:14:53 | [diff] [blame] | 5388 | |
mohan.reddy | f21db96 | 2014-10-16 12:26:47 | [diff] [blame] | 5389 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5390 | defCheckForRelativeIncludes(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5391 | bad_files={} |
| 5392 | for fin input_api.AffectedFiles(include_deletes=False): |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 5393 | if(f.UnixLocalPath().startswith('third_party') |
| 5394 | andnot f.LocalPath().startswith('third_party/blink')): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5395 | continue |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5396 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5397 | ifnot_IsCPlusPlusFile(input_api, f.LocalPath()): |
| 5398 | continue |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5399 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5400 | relative_includes=[ |
| 5401 | linefor _, linein f.ChangedContents() |
| 5402 | if"#include"in lineand"../"in line |
| 5403 | ] |
| 5404 | ifnot relative_includes: |
| 5405 | continue |
| 5406 | bad_files[f.LocalPath()]= relative_includes |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5407 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5408 | ifnot bad_files: |
| 5409 | return[] |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5410 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5411 | error_descriptions=[] |
| 5412 | for file_path, bad_linesin bad_files.items(): |
| 5413 | error_description= file_path |
| 5414 | for linein bad_lines: |
| 5415 | error_description+='\n '+ line |
| 5416 | error_descriptions.append(error_description) |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5417 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5418 | results=[] |
| 5419 | results.append( |
| 5420 | output_api.PresubmitError( |
| 5421 | 'You added one or more relative #include paths (including "../").\n' |
| 5422 | 'These shouldn\'t be used because they can be used to include headers\n' |
| 5423 | 'from code that\'s not correctly specified as a dependency in the\n' |
| 5424 | 'relevant BUILD.gn file(s).', error_descriptions)) |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5425 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5426 | return results |
rlanday | 6802cf63 | 2017-05-30 17:48:36 | [diff] [blame] | 5427 | |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5428 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5429 | defCheckForCcIncludes(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5430 | """Check that nobody tries to include a cc file. It's a relatively |
| 5431 | common error which results in duplicate symbols in object |
| 5432 | files. This may not always break the build until someone later gets |
| 5433 | very confusing linking errors.""" |
| 5434 | results=[] |
| 5435 | for fin input_api.AffectedFiles(include_deletes=False): |
| 5436 | # We let third_party code do whatever it wants |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 5437 | if(f.UnixLocalPath().startswith('third_party') |
| 5438 | andnot f.LocalPath().startswith('third_party/blink')): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5439 | continue |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 5440 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5441 | ifnot_IsCPlusPlusFile(input_api, f.LocalPath()): |
| 5442 | continue |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 5443 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5444 | for _, linein f.ChangedContents(): |
| 5445 | if line.startswith('#include "'): |
| 5446 | included_file= line.split('"')[1] |
| 5447 | if_IsCPlusPlusFile(input_api, included_file): |
| 5448 | # The most common naming for external files with C++ code, |
| 5449 | # apart from standard headers, is to call them foo.inc, but |
| 5450 | # Chromium sometimes uses foo-inc.cc so allow that as well. |
| 5451 | ifnot included_file.endswith(('.h','-inc.cc')): |
| 5452 | results.append( |
| 5453 | output_api.PresubmitError( |
| 5454 | 'Only header files or .inc files should be included in other\n' |
| 5455 | 'C++ files. Compiling the contents of a cc file more than once\n' |
| 5456 | 'will cause duplicate information in the build which may later\n' |
| 5457 | 'result in strange link_errors.\n'+ |
| 5458 | f.LocalPath()+':\n '+ line)) |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 5459 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5460 | return results |
Daniel Bratell | 65b03326 | 2019-04-23 08:17:06 | [diff] [blame] | 5461 | |
| 5462 | |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5463 | def_CheckWatchlistDefinitionsEntrySyntax(key, value, ast): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5464 | ifnot isinstance(key, ast.Str): |
| 5465 | return'Key at line %d must be a string literal'% key.lineno |
| 5466 | ifnot isinstance(value, ast.Dict): |
| 5467 | return'Value at line %d must be a dict'% value.lineno |
| 5468 | if len(value.keys)!=1: |
| 5469 | return'Dict at line %d must have single entry'% value.lineno |
| 5470 | ifnot isinstance(value.keys[0], ast.Str)or value.keys[0].s!='filepath': |
| 5471 | return( |
| 5472 | 'Entry at line %d must have a string literal \'filepath\' as key'% |
| 5473 | value.lineno) |
| 5474 | returnNone |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5475 | |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5476 | |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 5477 | def_CheckWatchlistsEntrySyntax(key, value, ast, email_regex): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5478 | ifnot isinstance(key, ast.Str): |
| 5479 | return'Key at line %d must be a string literal'% key.lineno |
| 5480 | ifnot isinstance(value, ast.List): |
| 5481 | return'Value at line %d must be a list'% value.lineno |
| 5482 | for elementin value.elts: |
| 5483 | ifnot isinstance(element, ast.Str): |
| 5484 | return'Watchlist elements on line %d is not a string'% key.lineno |
| 5485 | ifnot email_regex.match(element.s): |
| 5486 | return('Watchlist element on line %d doesn\'t look like a valid ' |
| 5487 | +'email: %s')%(key.lineno, element.s) |
| 5488 | returnNone |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5489 | |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5490 | |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 5491 | def_CheckWATCHLISTSEntries(wd_dict, w_dict, input_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5492 | mismatch_template=( |
| 5493 | 'Mismatch between WATCHLIST_DEFINITIONS entry (%s) and WATCHLISTS ' |
| 5494 | 'entry (%s)') |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5495 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5496 | email_regex= input_api.re.compile( |
| 5497 | r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+$") |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 5498 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5499 | ast= input_api.ast |
| 5500 | i=0 |
| 5501 | last_key='' |
| 5502 | whileTrue: |
| 5503 | if i>= len(wd_dict.keys): |
| 5504 | if i>= len(w_dict.keys): |
| 5505 | returnNone |
| 5506 | return mismatch_template%('missing', |
| 5507 | 'line %d'% w_dict.keys[i].lineno) |
| 5508 | elif i>= len(w_dict.keys): |
| 5509 | return(mismatch_template% |
| 5510 | ('line %d'% wd_dict.keys[i].lineno,'missing')) |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5511 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5512 | wd_key= wd_dict.keys[i] |
| 5513 | w_key= w_dict.keys[i] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5514 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5515 | result=_CheckWatchlistDefinitionsEntrySyntax(wd_key, |
| 5516 | wd_dict.values[i], ast) |
| 5517 | if resultisnotNone: |
| 5518 | return'Bad entry in WATCHLIST_DEFINITIONS dict: %s'% result |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5519 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5520 | result=_CheckWatchlistsEntrySyntax(w_key, w_dict.values[i], ast, |
| 5521 | email_regex) |
| 5522 | if resultisnotNone: |
| 5523 | return'Bad entry in WATCHLISTS dict: %s'% result |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5524 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5525 | if wd_key.s!= w_key.s: |
| 5526 | return mismatch_template%('%s at line %d'% |
| 5527 | (wd_key.s, wd_key.lineno), |
| 5528 | '%s at line %d'% |
| 5529 | (w_key.s, w_key.lineno)) |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5530 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5531 | if wd_key.s< last_key: |
| 5532 | return( |
| 5533 | 'WATCHLISTS dict is not sorted lexicographically at line %d and %d' |
| 5534 | %(wd_key.lineno, w_key.lineno)) |
| 5535 | last_key= wd_key.s |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5536 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5537 | i= i+1 |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5538 | |
| 5539 | |
Sergey Ulanov | 4af1605 | 2018-11-08 02:41:46 | [diff] [blame] | 5540 | def_CheckWATCHLISTSSyntax(expression, input_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5541 | ast= input_api.ast |
| 5542 | ifnot isinstance(expression, ast.Expression): |
| 5543 | return'WATCHLISTS file must contain a valid expression' |
| 5544 | dictionary= expression.body |
| 5545 | ifnot isinstance(dictionary, ast.Dict)or len(dictionary.keys)!=2: |
| 5546 | return'WATCHLISTS file must have single dict with exactly two entries' |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5547 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5548 | first_key= dictionary.keys[0] |
| 5549 | first_value= dictionary.values[0] |
| 5550 | second_key= dictionary.keys[1] |
| 5551 | second_value= dictionary.values[1] |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5552 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5553 | if(not isinstance(first_key, ast.Str) |
| 5554 | or first_key.s!='WATCHLIST_DEFINITIONS' |
| 5555 | ornot isinstance(first_value, ast.Dict)): |
| 5556 | return('The first entry of the dict in WATCHLISTS file must be ' |
| 5557 | 'WATCHLIST_DEFINITIONS dict') |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5558 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5559 | if(not isinstance(second_key, ast.Str)or second_key.s!='WATCHLISTS' |
| 5560 | ornot isinstance(second_value, ast.Dict)): |
| 5561 | return('The second entry of the dict in WATCHLISTS file must be ' |
| 5562 | 'WATCHLISTS dict') |
Takeshi Yoshino | 3a8f9cb5 | 2017-08-10 11:32:20 | [diff] [blame] | 5563 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5564 | return_CheckWATCHLISTSEntries(first_value, second_value, input_api) |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5565 | |
| 5566 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5567 | defCheckWATCHLISTS(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5568 | for fin input_api.AffectedFiles(include_deletes=False): |
| 5569 | if f.LocalPath()=='WATCHLISTS': |
| 5570 | contents= input_api.ReadFile(f,'r') |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5571 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5572 | try: |
| 5573 | # First, make sure that it can be evaluated. |
| 5574 | input_api.ast.literal_eval(contents) |
| 5575 | # Get an AST tree for it and scan the tree for detailed style checking. |
| 5576 | expression= input_api.ast.parse(contents, |
| 5577 | filename='WATCHLISTS', |
| 5578 | mode='eval') |
| 5579 | exceptValueErroras e: |
| 5580 | return[ |
| 5581 | output_api.PresubmitError('Cannot parse WATCHLISTS file', |
| 5582 | long_text=repr(e)) |
| 5583 | ] |
| 5584 | exceptSyntaxErroras e: |
| 5585 | return[ |
| 5586 | output_api.PresubmitError('Cannot parse WATCHLISTS file', |
| 5587 | long_text=repr(e)) |
| 5588 | ] |
| 5589 | exceptTypeErroras e: |
| 5590 | return[ |
| 5591 | output_api.PresubmitError('Cannot parse WATCHLISTS file', |
| 5592 | long_text=repr(e)) |
| 5593 | ] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5594 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5595 | result=_CheckWATCHLISTSSyntax(expression, input_api) |
| 5596 | if resultisnotNone: |
| 5597 | return[output_api.PresubmitError(result)] |
| 5598 | break |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5599 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5600 | return[] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5601 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5602 | |
Sean Kau | cb7c9b3 | 2022-10-25 21:25:52 | [diff] [blame] | 5603 | defCheckGnRebasePath(input_api, output_api): |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 5604 | """Checks that target_gen_dir is not used with "//" in rebase_path(). |
Sean Kau | cb7c9b3 | 2022-10-25 21:25:52 | [diff] [blame] | 5605 | |
| 5606 | Developers should use root_build_dir instead of "//" when using target_gen_dir because |
| 5607 | Chromium is sometimes built outside of the source tree. |
| 5608 | """ |
| 5609 | |
| 5610 | def gn_files(f): |
| 5611 | return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gn',)) |
| 5612 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5613 | rebase_path_regex= input_api.re.compile( |
| 5614 | r'rebase_path\(("\$target_gen_dir"|target_gen_dir), ("/"|"//")\)') |
Sean Kau | cb7c9b3 | 2022-10-25 21:25:52 | [diff] [blame] | 5615 | problems=[] |
| 5616 | for fin input_api.AffectedSourceFiles(gn_files): |
| 5617 | for line_num, linein f.ChangedContents(): |
| 5618 | if rebase_path_regex.search(line): |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5619 | problems.append('Absolute path in rebase_path() in %s:%d'% |
| 5620 | (f.LocalPath(), line_num)) |
Sean Kau | cb7c9b3 | 2022-10-25 21:25:52 | [diff] [blame] | 5621 | |
| 5622 | if problems: |
| 5623 | return[ |
| 5624 | output_api.PresubmitPromptWarning( |
| 5625 | 'Using an absolute path in rebase_path()', |
| 5626 | items=sorted(problems), |
| 5627 | long_text=( |
| 5628 | 'rebase_path() should use root_build_dir instead of "/" ', |
| 5629 | 'since builds can be initiated from outside of the source ', |
| 5630 | 'root.')) |
| 5631 | ] |
| 5632 | return[] |
Takeshi Yoshino | e387aa3 | 2017-08-02 13:16:13 | [diff] [blame] | 5633 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5634 | |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5635 | defCheckGnGlobForward(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5636 | """Checks that forward_variables_from(invoker, "*") follows best practices. |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5637 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5638 | As documented at //build/docs/writing_gn_templates.md |
| 5639 | """ |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5640 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5641 | def gn_files(f): |
| 5642 | return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gni',)) |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5643 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5644 | problems=[] |
| 5645 | for fin input_api.AffectedSourceFiles(gn_files): |
| 5646 | for line_num, linein f.ChangedContents(): |
| 5647 | if'forward_variables_from(invoker, "*")'in line: |
| 5648 | problems.append( |
| 5649 | 'Bare forward_variables_from(invoker, "*") in %s:%d'% |
| 5650 | (f.LocalPath(), line_num)) |
| 5651 | |
| 5652 | if problems: |
| 5653 | return[ |
| 5654 | output_api.PresubmitPromptWarning( |
| 5655 | 'forward_variables_from("*") without exclusions', |
| 5656 | items=sorted(problems), |
| 5657 | long_text=( |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 5658 | 'The variables "visibility" and "test_only" should be ' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5659 | 'explicitly listed in forward_variables_from(). For more ' |
| 5660 | 'details, see:\n' |
| 5661 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/' |
| 5662 | 'build/docs/writing_gn_templates.md' |
| 5663 | '#Using-forward_variables_from')) |
| 5664 | ] |
| 5665 | return[] |
Andrew Grieve | 1b290e4a2 | 2020-11-24 20:07:01 | [diff] [blame] | 5666 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5667 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5668 | defCheckNewHeaderWithoutGnChangeOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5669 | """Checks that newly added header files have corresponding GN changes. |
| 5670 | Note that this is only a heuristic. To be precise, run script: |
| 5671 | build/check_gn_headers.py. |
| 5672 | """ |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5673 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5674 | def headers(f): |
| 5675 | return input_api.FilterSourceFile( |
| 5676 | f, files_to_check=(r'.+%s'% _HEADER_EXTENSIONS,)) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5677 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5678 | new_headers=[] |
| 5679 | for fin input_api.AffectedSourceFiles(headers): |
| 5680 | if f.Action()!='A': |
| 5681 | continue |
| 5682 | new_headers.append(f.LocalPath()) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5683 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5684 | def gn_files(f): |
| 5685 | return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gn',)) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5686 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5687 | all_gn_changed_contents='' |
| 5688 | for fin input_api.AffectedSourceFiles(gn_files): |
| 5689 | for _, linein f.ChangedContents(): |
| 5690 | all_gn_changed_contents+= line |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5691 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5692 | problems=[] |
| 5693 | for headerin new_headers: |
| 5694 | basename= input_api.os_path.basename(header) |
| 5695 | if basenamenotin all_gn_changed_contents: |
| 5696 | problems.append(header) |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5697 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5698 | if problems: |
| 5699 | return[ |
| 5700 | output_api.PresubmitPromptWarning( |
| 5701 | 'Missing GN changes for new header files', |
| 5702 | items=sorted(problems), |
| 5703 | long_text= |
| 5704 | 'Please double check whether newly added header files need ' |
| 5705 | 'corresponding changes in gn or gni files.\nThis checking is only a ' |
| 5706 | 'heuristic. Run build/check_gn_headers.py to be precise.\n' |
| 5707 | 'Read https://crbug.com/661774 for more info.') |
| 5708 | ] |
| 5709 | return[] |
Wei-Yin Chen (陳威尹) | c0624d00 | 2018-07-30 18:22:19 | [diff] [blame] | 5710 | |
| 5711 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5712 | defCheckCorrectProductNameInMessages(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5713 | """Check that Chromium-branded strings don't include "Chrome" or vice versa. |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5714 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5715 | This assumes we won't intentionally reference one product from the other |
| 5716 | product. |
| 5717 | """ |
| 5718 | all_problems=[] |
| 5719 | test_cases=[{ |
| 5720 | "filename_postfix":"google_chrome_strings.grd", |
| 5721 | "correct_name":"Chrome", |
| 5722 | "incorrect_name":"Chromium", |
| 5723 | },{ |
Thiago Perrotta | 099034f | 2023-06-05 18:10:20 | [diff] [blame] | 5724 | "filename_postfix":"google_chrome_strings.grd", |
| 5725 | "correct_name":"Chrome", |
| 5726 | "incorrect_name":"Chrome for Testing", |
| 5727 | },{ |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5728 | "filename_postfix":"chromium_strings.grd", |
| 5729 | "correct_name":"Chromium", |
| 5730 | "incorrect_name":"Chrome", |
| 5731 | }] |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5732 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5733 | for test_casein test_cases: |
| 5734 | problems=[] |
| 5735 | filename_filter=lambda x: x.LocalPath().endswith(test_case[ |
| 5736 | "filename_postfix"]) |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5737 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5738 | # Check each new line. Can yield false positives in multiline comments, but |
| 5739 | # easier than trying to parse the XML because messages can have nested |
| 5740 | # children, and associating message elements with affected lines is hard. |
| 5741 | for fin input_api.AffectedSourceFiles(filename_filter): |
| 5742 | for line_num, linein f.ChangedContents(): |
| 5743 | if"<message"in lineor"<!--"in lineor"-->"in line: |
| 5744 | continue |
| 5745 | if test_case["incorrect_name"]in line: |
Thiago Perrotta | 099034f | 2023-06-05 18:10:20 | [diff] [blame] | 5746 | # 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] | 5747 | if(test_case["correct_name"]=="Chromium" |
| 5748 | and line.count("Chrome") |
| 5749 | == line.count("Chrome for Testing")): |
Thiago Perrotta | 099034f | 2023-06-05 18:10:20 | [diff] [blame] | 5750 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5751 | problems.append("Incorrect product name in %s:%d"% |
| 5752 | (f.LocalPath(), line_num)) |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5753 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5754 | if problems: |
| 5755 | message=( |
| 5756 | "Strings in %s-branded string files should reference \"%s\", not \"%s\"" |
| 5757 | %(test_case["correct_name"], test_case["correct_name"], |
| 5758 | test_case["incorrect_name"])) |
| 5759 | all_problems.append( |
| 5760 | output_api.PresubmitPromptWarning(message, items=problems)) |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5761 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5762 | return all_problems |
Michael Giuffrida | d3bc867 | 2018-10-25 22:48:02 | [diff] [blame] | 5763 | |
| 5764 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5765 | defCheckForTooLargeFiles(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5766 | """Avoid large files, especially binary files, in the repository since |
| 5767 | git doesn't scale well for those. They will be in everyone's repo |
| 5768 | clones forever, forever making Chromium slower to clone and work |
| 5769 | with.""" |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5770 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5771 | # Uploading files to cloud storage is not trivial so we don't want |
| 5772 | # to set the limit too low, but the upper limit for "normal" large |
| 5773 | # files seems to be 1-2 MB, with a handful around 5-8 MB, so |
| 5774 | # anything over 20 MB is exceptional. |
Bruce Dawson | bb414db | 2022-12-27 20:21:25 | [diff] [blame] | 5775 | TOO_LARGE_FILE_SIZE_LIMIT=20*1024*1024 |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5776 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5777 | too_large_files=[] |
| 5778 | for fin input_api.AffectedFiles(): |
| 5779 | # Check both added and modified files (but not deleted files). |
| 5780 | if f.Action()in('A','M'): |
| 5781 | size= input_api.os_path.getsize(f.AbsoluteLocalPath()) |
Joe DeBlasio | 10a832f | 2023-04-21 20:20:18 | [diff] [blame] | 5782 | if size> TOO_LARGE_FILE_SIZE_LIMIT: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5783 | too_large_files.append("%s: %d bytes"%(f.LocalPath(), size)) |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5784 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5785 | if too_large_files: |
| 5786 | message=( |
| 5787 | 'Do not commit large files to git since git scales badly for those.\n' |
| 5788 | + |
| 5789 | 'Instead put the large files in cloud storage and use DEPS to\n'+ |
| 5790 | 'fetch them.\n'+'\n'.join(too_large_files)) |
| 5791 | return[ |
| 5792 | output_api.PresubmitError('Too large files found in commit', |
| 5793 | long_text=message+'\n') |
| 5794 | ] |
| 5795 | else: |
| 5796 | return[] |
Daniel Bratell | 93eb6c6 | 2019-04-29 20:13:36 | [diff] [blame] | 5797 | |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5798 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5799 | defCheckFuzzTargetsOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5800 | """Checks specific for fuzz target sources.""" |
| 5801 | EXPORTED_SYMBOLS=[ |
| 5802 | 'LLVMFuzzerInitialize', |
| 5803 | 'LLVMFuzzerCustomMutator', |
| 5804 | 'LLVMFuzzerCustomCrossOver', |
| 5805 | 'LLVMFuzzerMutate', |
| 5806 | ] |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5807 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5808 | REQUIRED_HEADER='#include "testing/libfuzzer/libfuzzer_exports.h"' |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5809 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5810 | defFilterFile(affected_file): |
| 5811 | """Ignore libFuzzer source code.""" |
| 5812 | files_to_check= r'.*fuzz.*\.(h|hpp|hcc|cc|cpp|cxx)$' |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5813 | files_to_skip= r"^third_party/libFuzzer" |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5814 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5815 | return input_api.FilterSourceFile(affected_file, |
| 5816 | files_to_check=[files_to_check], |
| 5817 | files_to_skip=[files_to_skip]) |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5818 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5819 | files_with_missing_header=[] |
| 5820 | for fin input_api.AffectedSourceFiles(FilterFile): |
| 5821 | contents= input_api.ReadFile(f,'r') |
| 5822 | if REQUIRED_HEADERin contents: |
| 5823 | continue |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5824 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5825 | if any(symbolin contentsfor symbolin EXPORTED_SYMBOLS): |
| 5826 | files_with_missing_header.append(f.LocalPath()) |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5827 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5828 | ifnot files_with_missing_header: |
| 5829 | return[] |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5830 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5831 | long_text=( |
| 5832 | 'If you define any of the libFuzzer optional functions (%s), it is ' |
| 5833 | 'recommended to add \'%s\' directive. Otherwise, the fuzz target may ' |
| 5834 | 'work incorrectly on Mac (crbug.com/687076).\nNote that ' |
| 5835 | 'LLVMFuzzerInitialize should not be used, unless your fuzz target needs ' |
| 5836 | 'to access command line arguments passed to the fuzzer. Instead, prefer ' |
| 5837 | 'static initialization and shared resources as documented in ' |
| 5838 | 'https://chromium.googlesource.com/chromium/src/+/main/testing/' |
| 5839 | 'libfuzzer/efficient_fuzzing.md#simplifying-initialization_cleanup.\n' |
| 5840 | %(', '.join(EXPORTED_SYMBOLS), REQUIRED_HEADER)) |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5841 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5842 | return[ |
| 5843 | output_api.PresubmitPromptWarning(message="Missing '%s' in:"% |
| 5844 | REQUIRED_HEADER, |
| 5845 | items=files_with_missing_header, |
| 5846 | long_text=long_text) |
| 5847 | ] |
Max Moroz | b47503b | 2019-08-08 21:03:27 | [diff] [blame] | 5848 | |
| 5849 | |
Mohamed Heikal | d048240a | 2019-11-12 16:57:37 | [diff] [blame] | 5850 | def_CheckNewImagesWarning(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5851 | """ |
| 5852 | Warns authors who add images into the repo to make sure their images are |
| 5853 | optimized before committing. |
| 5854 | """ |
| 5855 | images_added=False |
| 5856 | image_paths=[] |
| 5857 | errors=[] |
| 5858 | filter_lambda=lambda x: input_api.FilterSourceFile( |
| 5859 | x, |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5860 | files_to_skip=( |
| 5861 | ('(?i).*test', r'.*\/junit\/')+ input_api.DEFAULT_FILES_TO_SKIP), |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5862 | files_to_check=[r'.*\/(drawable|mipmap)']) |
| 5863 | for fin input_api.AffectedFiles(include_deletes=False, |
| 5864 | file_filter=filter_lambda): |
| 5865 | local_path= f.LocalPath().lower() |
| 5866 | if any( |
| 5867 | local_path.endswith(extension) |
| 5868 | for extensionin _IMAGE_EXTENSIONS): |
| 5869 | images_added=True |
| 5870 | image_paths.append(f) |
| 5871 | if images_added: |
| 5872 | errors.append( |
| 5873 | output_api.PresubmitPromptWarning( |
| 5874 | 'It looks like you are trying to commit some images. If these are ' |
| 5875 | 'non-test-only images, please make sure to read and apply the tips in ' |
| 5876 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/docs/speed/' |
| 5877 | 'binary_size/optimization_advice.md#optimizing-images\nThis check is ' |
| 5878 | 'FYI only and will not block your CL on the CQ.', image_paths)) |
| 5879 | return errors |
Mohamed Heikal | d048240a | 2019-11-12 16:57:37 | [diff] [blame] | 5880 | |
| 5881 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5882 | defChecksAndroidSpecificOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5883 | """Groups upload checks that target android code.""" |
| 5884 | results=[] |
| 5885 | results.extend(_CheckAndroidCrLogUsage(input_api, output_api)) |
| 5886 | results.extend(_CheckAndroidDebuggableBuild(input_api, output_api)) |
| 5887 | results.extend(_CheckAndroidNewMdpiAssetLocation(input_api, output_api)) |
| 5888 | results.extend(_CheckAndroidToastUsage(input_api, output_api)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5889 | results.extend(_CheckAndroidTestAnnotationUsage(input_api, output_api)) |
| 5890 | results.extend(_CheckAndroidWebkitImports(input_api, output_api)) |
| 5891 | results.extend(_CheckAndroidXmlStyle(input_api, output_api,True)) |
| 5892 | results.extend(_CheckNewImagesWarning(input_api, output_api)) |
| 5893 | results.extend(_CheckAndroidNoBannedImports(input_api, output_api)) |
| 5894 | results.extend(_CheckAndroidInfoBarDeprecation(input_api, output_api)) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5895 | results.extend(_CheckAndroidNullAwayAnnotatedClasses( |
| 5896 | input_api, output_api)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5897 | return results |
| 5898 | |
Becky Zhou | 7c69b5099 | 2018-12-10 19:37:57 | [diff] [blame] | 5899 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5900 | defChecksAndroidSpecificOnCommit(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5901 | """Groups commit checks that target android code.""" |
| 5902 | results=[] |
| 5903 | results.extend(_CheckAndroidXmlStyle(input_api, output_api,False)) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5904 | results.extend(_CheckAndroidNullAwayAnnotatedClasses( |
| 5905 | input_api, output_api)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5906 | return results |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 5907 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5908 | |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5909 | # TODO(chrishall): could we additionally match on any path owned by |
| 5910 | # ui/accessibility/OWNERS ? |
| 5911 | _ACCESSIBILITY_PATHS=( |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5912 | r"^chrome/browser.*/accessibility/", |
| 5913 | r"^chrome/browser/extensions/api/automation.*/", |
| 5914 | r"^chrome/renderer/extensions/accessibility_.*", |
| 5915 | r"^chrome/tests/data/accessibility/", |
| 5916 | r"^content/browser/accessibility/", |
| 5917 | r"^content/renderer/accessibility/", |
| 5918 | r"^content/tests/data/accessibility/", |
| 5919 | r"^extensions/renderer/api/automation/", |
Katie Dektar | 58ef07b | 2022-09-27 13:19:17 | [diff] [blame] | 5920 | r"^services/accessibility/", |
Abigail Klein | 7a63c57 | 2024-02-28 20:45:09 | [diff] [blame] | 5921 | r"^services/screen_ai/", |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 5922 | r"^ui/accessibility/", |
| 5923 | r"^ui/views/accessibility/", |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5924 | ) |
| 5925 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5926 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 5927 | defCheckAccessibilityRelnotesField(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5928 | """Checks that commits to accessibility code contain an AX-Relnotes field in |
| 5929 | their commit message.""" |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5930 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5931 | defFileFilter(affected_file): |
| 5932 | paths= _ACCESSIBILITY_PATHS |
| 5933 | return input_api.FilterSourceFile(affected_file, files_to_check=paths) |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5934 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5935 | # Only consider changes affecting accessibility paths. |
| 5936 | ifnot any(input_api.AffectedFiles(file_filter=FileFilter)): |
| 5937 | return[] |
Akihiro Ota | 08108e54 | 2020-05-20 15:30:53 | [diff] [blame] | 5938 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5939 | # AX-Relnotes can appear in either the description or the footer. |
| 5940 | # When searching the description, require 'AX-Relnotes:' to appear at the |
| 5941 | # beginning of a line. |
| 5942 | ax_regex= input_api.re.compile('ax-relnotes[:=]') |
| 5943 | description_has_relnotes= any( |
| 5944 | ax_regex.match(line) |
| 5945 | for linein input_api.change.DescriptionText().lower().splitlines()) |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5946 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5947 | footer_relnotes= input_api.change.GitFootersFromDescription().get( |
| 5948 | 'AX-Relnotes',[]) |
| 5949 | if description_has_relnotesor footer_relnotes: |
| 5950 | return[] |
Chris Hall | 59f8d0c7 | 2020-05-01 07:31:19 | [diff] [blame] | 5951 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 5952 | # TODO(chrishall): link to Relnotes documentation in message. |
| 5953 | message=( |
| 5954 | "Missing 'AX-Relnotes:' field required for accessibility changes" |
| 5955 | "\n please add 'AX-Relnotes: [release notes].' to describe any " |
| 5956 | "user-facing changes" |
| 5957 | "\n otherwise add 'AX-Relnotes: n/a.' if this change has no " |
| 5958 | "user-facing effects" |
| 5959 | "\n if this is confusing or annoying then please contact members " |
| 5960 | "of ui/accessibility/OWNERS.") |
| 5961 | |
| 5962 | return[output_api.PresubmitNotifyResult(message)] |
dgn | aa68d5e | 2015-06-10 10:08:22 | [diff] [blame] | 5963 | |
Mark Schillaci | 44c90b4 | 2024-11-22 20:44:38 | [diff] [blame] | 5964 | |
| 5965 | _ACCESSIBILITY_ARIA_METHOD_CANDIDATES_PATTERNS= r'(\-\>|\.)(get|has|FastGet|FastHas)Attribute\(' |
| 5966 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5967 | _ACCESSIBILITY_ARIA_BAD_PARAMS_PATTERNS=(r"\(html_names::kAria(.*)Attr\)", |
| 5968 | r"\(html_names::kRoleAttr\)") |
Mark Schillaci | 44c90b4 | 2024-11-22 20:44:38 | [diff] [blame] | 5969 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5970 | _ACCESSIBILITY_ARIA_FILE_CANDIDATES_PATTERNS=(r".*/accessibility/.*.(cc|h)", |
| 5971 | r".*/ax_.*.(cc|h)") |
| 5972 | |
Mark Schillaci | 44c90b4 | 2024-11-22 20:44:38 | [diff] [blame] | 5973 | |
| 5974 | defCheckAccessibilityAriaElementAttributeGetters(input_api, output_api): |
| 5975 | """Checks that the blink accessibility code follows the defined patterns |
| 5976 | for checking aria attributes, so that ElementInternals is not bypassed.""" |
| 5977 | |
| 5978 | # Limit to accessibility-related files. |
| 5979 | defFileFilter(affected_file): |
| 5980 | paths= _ACCESSIBILITY_ARIA_FILE_CANDIDATES_PATTERNS |
| 5981 | return input_api.FilterSourceFile(affected_file, files_to_check=paths) |
| 5982 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5983 | aria_method_regex= input_api.re.compile( |
| 5984 | _ACCESSIBILITY_ARIA_METHOD_CANDIDATES_PATTERNS) |
Mark Schillaci | 44c90b4 | 2024-11-22 20:44:38 | [diff] [blame] | 5985 | aria_bad_params_regex= input_api.re.compile( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5986 | "|".join(_ACCESSIBILITY_ARIA_BAD_PARAMS_PATTERNS)) |
Mark Schillaci | 44c90b4 | 2024-11-22 20:44:38 | [diff] [blame] | 5987 | problems=[] |
| 5988 | |
| 5989 | for fin input_api.AffectedSourceFiles(FileFilter): |
| 5990 | for line_num, linein f.ChangedContents(): |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 5991 | if aria_method_regex.search(line)and aria_bad_params_regex.search( |
| 5992 | line): |
| 5993 | problems.append( |
| 5994 | f"{f.LocalPath()}:{line_num}\n {line.strip()}") |
Mark Schillaci | 44c90b4 | 2024-11-22 20:44:38 | [diff] [blame] | 5995 | |
| 5996 | if problems: |
| 5997 | return[ |
| 5998 | output_api.PresubmitPromptWarning( |
| 5999 | "Accessibility code should not use element methods to get or check" |
| 6000 | "\nthe presence of aria attributes" |
| 6001 | "\nPlease use ARIA-specific attribute access, e.g. HasAriaAttribute()," |
| 6002 | "\nAriaTokenAttribute(), AriaBoolAttribute(), AriaBooleanAttribute()," |
| 6003 | "\nAriaFloatAttribute().", |
| 6004 | problems, |
| 6005 | ) |
| 6006 | ] |
| 6007 | return[] |
| 6008 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6009 | |
seanmccullough | 4a935625 | 2021-04-08 19:54:09 | [diff] [blame] | 6010 | # string pattern, sequence of strings to show when pattern matches, |
| 6011 | # error flag. True if match is a presubmit error, otherwise it's a warning. |
| 6012 | _NON_INCLUSIVE_TERMS=( |
| 6013 | ( |
| 6014 | # Note that \b pattern in python re is pretty particular. In this |
| 6015 | # regexp, 'class WhiteList ...' will match, but 'class FooWhiteList |
| 6016 | # ...' will not. This may require some tweaking to catch these cases |
| 6017 | # without triggering a lot of false positives. Leaving it naive and |
| 6018 | # less matchy for now. |
Josip Sokcevic | 9d2806a0 | 2023-12-13 03:04:02 | [diff] [blame] | 6019 | r'/(?i)\b((black|white)list|master|slave)\b',# nocheck |
seanmccullough | 4a935625 | 2021-04-08 19:54:09 | [diff] [blame] | 6020 | ( |
| 6021 | 'Please don\'t use blacklist, whitelist, '# nocheck |
| 6022 | 'or slave in your',# nocheck |
| 6023 | 'code and make every effort to use other terms. Using "// nocheck"', |
| 6024 | '"# nocheck" or "<!-- nocheck -->"', |
| 6025 | 'at the end of the offending line will bypass this PRESUBMIT error', |
| 6026 | 'but avoid using this whenever possible. Reach out to', |
| 6027 | 'community@chromium.org if you have questions'), |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6028 | True),) |
| 6029 | |
seanmccullough | 4a935625 | 2021-04-08 19:54:09 | [diff] [blame] | 6030 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6031 | defChecksCommon(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6032 | """Checks common to both upload and commit.""" |
| 6033 | results=[] |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 6034 | results.extend( |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6035 | input_api.canned_checks.PanProjectChecks( |
| 6036 | input_api, output_api, excluded_paths=_EXCLUDED_PATHS)) |
Eric Boren | 6fd2b93 | 2018-01-25 15:05:08 | [diff] [blame] | 6037 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6038 | author= input_api.change.author_email |
| 6039 | if authorand authornotin _KNOWN_ROBOTS: |
| 6040 | results.extend( |
| 6041 | input_api.canned_checks.CheckAuthorizedAuthor( |
| 6042 | input_api, output_api)) |
marja@chromium.org | 2299dcf | 2012-11-15 19:56:24 | [diff] [blame] | 6043 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6044 | results.extend( |
| 6045 | input_api.canned_checks.CheckChangeHasNoTabs( |
| 6046 | input_api, |
| 6047 | output_api, |
| 6048 | source_file_filter=lambda x: x.LocalPath().endswith('.grd'))) |
| 6049 | results.extend( |
| 6050 | input_api.RunTests( |
| 6051 | input_api.canned_checks.CheckVPythonSpec(input_api, output_api))) |
Edward Lesmes | ce51df5 | 2020-08-04 22:10:17 | [diff] [blame] | 6052 | |
Ben Pastene | 30dc5708 | 2025-06-02 22:19:20 | [diff] [blame] | 6053 | dirmd='dirmd.bat'if input_api.is_windowselse'dirmd' |
| 6054 | dirmd_bin= input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 6055 | 'third_party','depot_tools', dirmd) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6056 | results.extend( |
| 6057 | input_api.RunTests( |
| 6058 | input_api.canned_checks.CheckDirMetadataFormat( |
Ben Pastene | 30dc5708 | 2025-06-02 22:19:20 | [diff] [blame] | 6059 | input_api, output_api, dirmd_bin))) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6060 | results.extend( |
| 6061 | input_api.canned_checks.CheckOwnersDirMetadataExclusive( |
| 6062 | input_api, output_api)) |
| 6063 | results.extend( |
| 6064 | input_api.canned_checks.CheckNoNewMetadataInOwners( |
| 6065 | input_api, output_api)) |
| 6066 | results.extend( |
| 6067 | input_api.canned_checks.CheckInclusiveLanguage( |
| 6068 | input_api, |
| 6069 | output_api, |
| 6070 | excluded_directories_relative_path=[ |
| 6071 | 'infra','inclusive_language_presubmit_exempt_dirs.txt' |
| 6072 | ], |
| 6073 | non_inclusive_terms=_NON_INCLUSIVE_TERMS)) |
Yiwei Zhang | 5341bf0 | 2025-03-20 16:34:13 | [diff] [blame] | 6074 | results.extend( |
| 6075 | input_api.canned_checks.CheckNewDEPSHooksHasRequiredReviewers( |
| 6076 | input_api, output_api)) |
Jie Sheng | b0c86f0 | 2025-06-12 21:36:14 | [diff] [blame] | 6077 | results.extend( |
| 6078 | input_api.canned_checks.CheckValidHostsInDEPSOnUpload( |
| 6079 | input_api, output_api)) |
Dirk Pranke | e3c9c62d | 2021-05-18 18:35:59 | [diff] [blame] | 6080 | |
Aleksey Khoroshilov | 2978c94 | 2022-06-13 16:14:12 | [diff] [blame] | 6081 | presubmit_py_filter=lambda f: input_api.FilterSourceFile( |
Daniel Cheng | 6f3d1ae1 | 2025-04-07 17:11:27 | [diff] [blame] | 6082 | f, files_to_check=[r'.*PRESUBMIT(?:_test)?\.py$']) |
| 6083 | potential_paths= set( |
| 6084 | map( |
| 6085 | lambda f: input_api.os_path.dirname(f.AbsoluteLocalPath()), |
| 6086 | input_api.AffectedFiles(include_deletes=False, |
| 6087 | file_filter=presubmit_py_filter))) |
| 6088 | for full_pathin potential_paths: |
Aleksey Khoroshilov | 2978c94 | 2022-06-13 16:14:12 | [diff] [blame] | 6089 | test_file= input_api.os_path.join(full_path,'PRESUBMIT_test.py') |
| 6090 | # The PRESUBMIT.py file (and the directory containing it) might have |
| 6091 | # been affected by being moved or removed, so only try to run the tests |
| 6092 | # if they still exist. |
| 6093 | ifnot input_api.os_path.exists(test_file): |
| 6094 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6095 | |
Aleksey Khoroshilov | 2978c94 | 2022-06-13 16:14:12 | [diff] [blame] | 6096 | results.extend( |
| 6097 | input_api.canned_checks.RunUnitTestsInDirectory( |
| 6098 | input_api, |
| 6099 | output_api, |
| 6100 | full_path, |
Takuto Ikuta | 40def48 | 2023-06-02 02:23:49 | [diff] [blame] | 6101 | files_to_check=[r'^PRESUBMIT_test\.py$'])) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6102 | return results |
maruel@chromium.org | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 6103 | |
maruel@chromium.org | b337cb5b | 2011-01-23 21:24:05 | [diff] [blame] | 6104 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6105 | defCheckPatchFiles(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6106 | problems=[ |
| 6107 | f.LocalPath()for fin input_api.AffectedFiles() |
| 6108 | if f.LocalPath().endswith(('.orig','.rej')) |
| 6109 | ] |
| 6110 | # Cargo.toml.orig files are part of third-party crates downloaded from |
| 6111 | # crates.io and should be included. |
| 6112 | problems=[ffor fin problemsifnot f.endswith('Cargo.toml.orig')] |
| 6113 | if problems: |
| 6114 | return[ |
| 6115 | output_api.PresubmitError("Don't commit .rej and .orig files.", |
| 6116 | problems) |
| 6117 | ] |
| 6118 | else: |
| 6119 | return[] |
enne@chromium.org | b8079ae4a | 2012-12-05 19:56:49 | [diff] [blame] | 6120 | |
| 6121 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6122 | defCheckBuildConfigMacrosWithoutInclude(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6123 | # Excludes OS_CHROMEOS, which is not defined in build_config.h. |
| 6124 | macro_re= input_api.re.compile( |
| 6125 | r'^\s*#(el)?if.*\bdefined\(((COMPILER_|ARCH_CPU_|WCHAR_T_IS_)[^)]*)') |
| 6126 | include_re= input_api.re.compile(r'^#include\s+"build/build_config.h"', |
| 6127 | input_api.re.MULTILINE) |
| 6128 | extension_re= input_api.re.compile(r'\.[a-z]+$') |
| 6129 | errors=[] |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 6130 | config_h_file= input_api.os_path.join('build','build_config.h') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6131 | for fin input_api.AffectedFiles(include_deletes=False): |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 6132 | # The build-config macros are allowed to be used in build_config.h |
| 6133 | # without including itself. |
| 6134 | if f.LocalPath()== config_h_file: |
| 6135 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6136 | ifnot f.LocalPath().endswith( |
| 6137 | ('.h','.c','.cc','.cpp','.m','.mm')): |
| 6138 | continue |
Arthur Sonzogni | a3dec41 | 2024-04-29 12:05:37 | [diff] [blame] | 6139 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6140 | found_line_number=None |
| 6141 | found_macro=None |
| 6142 | all_lines= input_api.ReadFile(f,'r').splitlines() |
| 6143 | for line_num, linein enumerate(all_lines): |
| 6144 | match= macro_re.search(line) |
| 6145 | if match: |
| 6146 | found_line_number= line_num |
| 6147 | found_macro= match.group(2) |
| 6148 | break |
| 6149 | ifnot found_line_number: |
| 6150 | continue |
Kent Tamura | 5a8755d | 2017-06-29 23:37:07 | [diff] [blame] | 6151 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6152 | found_include_line=-1 |
| 6153 | for line_num, linein enumerate(all_lines): |
| 6154 | if include_re.search(line): |
| 6155 | found_include_line= line_num |
| 6156 | break |
| 6157 | if found_include_line>=0and found_include_line< found_line_number: |
| 6158 | continue |
Kent Tamura | 5a8755d | 2017-06-29 23:37:07 | [diff] [blame] | 6159 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6160 | ifnot f.LocalPath().endswith('.h'): |
| 6161 | primary_header_path= extension_re.sub('.h', f.AbsoluteLocalPath()) |
| 6162 | try: |
| 6163 | content= input_api.ReadFile(primary_header_path,'r') |
| 6164 | if include_re.search(content): |
| 6165 | continue |
| 6166 | exceptIOError: |
| 6167 | pass |
| 6168 | errors.append('%s:%d %s macro is used without first including build/' |
| 6169 | 'build_config.h.'% |
| 6170 | (f.LocalPath(), found_line_number, found_macro)) |
| 6171 | if errors: |
| 6172 | return[output_api.PresubmitPromptWarning('\n'.join(errors))] |
| 6173 | return[] |
Kent Tamura | 5a8755d | 2017-06-29 23:37:07 | [diff] [blame] | 6174 | |
| 6175 | |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6176 | defCheckForSuperfluousStlIncludesInHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6177 | stl_include_re= input_api.re.compile(r'^#include\s+<(' |
| 6178 | r'algorithm|' |
| 6179 | r'array|' |
| 6180 | r'limits|' |
| 6181 | r'list|' |
| 6182 | r'map|' |
| 6183 | r'memory|' |
| 6184 | r'queue|' |
| 6185 | r'set|' |
| 6186 | r'string|' |
| 6187 | r'unordered_map|' |
| 6188 | r'unordered_set|' |
| 6189 | r'utility|' |
| 6190 | r'vector)>') |
| 6191 | std_namespace_re= input_api.re.compile(r'std::') |
| 6192 | errors=[] |
| 6193 | for fin input_api.AffectedFiles(): |
| 6194 | ifnot_IsCPlusPlusHeaderFile(input_api, f.LocalPath()): |
| 6195 | continue |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6196 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6197 | uses_std_namespace=False |
| 6198 | has_stl_include=False |
| 6199 | for linein f.NewContents(): |
| 6200 | if has_stl_includeand uses_std_namespace: |
| 6201 | break |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6202 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6203 | ifnot has_stl_includeand stl_include_re.search(line): |
| 6204 | has_stl_include=True |
| 6205 | continue |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6206 | |
Bruce Dawson | 4a5579a | 2022-04-08 17:11:36 | [diff] [blame] | 6207 | ifnot uses_std_namespaceand(std_namespace_re.search(line) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6208 | or'no-std-usage-because-pch-file' |
| 6209 | in line): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6210 | uses_std_namespace=True |
| 6211 | continue |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6212 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6213 | if has_stl_includeandnot uses_std_namespace: |
| 6214 | errors.append( |
| 6215 | '%s: Includes STL header(s) but does not reference std::'% |
| 6216 | f.LocalPath()) |
| 6217 | if errors: |
| 6218 | return[output_api.PresubmitPromptWarning('\n'.join(errors))] |
| 6219 | return[] |
Lei Zhang | 1c12a22f | 2021-05-12 11:28:45 | [diff] [blame] | 6220 | |
| 6221 | |
Xiaohan Wang | 42d96c2 | 2022-01-20 17:23:11 | [diff] [blame] | 6222 | def_CheckForDeprecatedOSMacrosInFile(input_api, f): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6223 | """Check for sensible looking, totally invalid OS macros.""" |
| 6224 | preprocessor_statement= input_api.re.compile(r'^\s*#') |
| 6225 | os_macro= input_api.re.compile(r'defined\(OS_([^)]+)\)') |
| 6226 | results=[] |
| 6227 | for lnum, linein f.ChangedContents(): |
| 6228 | if preprocessor_statement.search(line): |
| 6229 | for matchin os_macro.finditer(line): |
| 6230 | results.append( |
| 6231 | ' %s:%d: %s'% |
| 6232 | (f.LocalPath(), lnum,'defined(OS_'+ match.group(1)+ |
| 6233 | ') -> BUILDFLAG(IS_'+ match.group(1)+')')) |
| 6234 | return results |
dbeam@chromium.org | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6235 | |
| 6236 | |
Xiaohan Wang | 42d96c2 | 2022-01-20 17:23:11 | [diff] [blame] | 6237 | defCheckForDeprecatedOSMacros(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6238 | """Check all affected files for invalid OS macros.""" |
| 6239 | bad_macros=[] |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 6240 | # The OS_ macros are allowed to be used in build/build_config.h. |
| 6241 | config_h_file= input_api.os_path.join('build','build_config.h') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6242 | for fin input_api.AffectedSourceFiles(None): |
Bruce Dawson | f767920 | 2022-08-09 20:24:00 | [diff] [blame] | 6243 | ifnot f.LocalPath().endswith(('.py','.js','.html','.css','.md')) \ |
| 6244 | and f.LocalPath()!= config_h_file: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6245 | bad_macros.extend(_CheckForDeprecatedOSMacrosInFile(input_api, f)) |
dbeam@chromium.org | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6246 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6247 | ifnot bad_macros: |
| 6248 | return[] |
dbeam@chromium.org | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6249 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6250 | return[ |
| 6251 | output_api.PresubmitError( |
| 6252 | 'OS macros have been deprecated. Please use BUILDFLAGs instead (still ' |
| 6253 | 'defined in build_config.h):', bad_macros) |
| 6254 | ] |
dbeam@chromium.org | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6255 | |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6256 | |
| 6257 | def_CheckForInvalidIfDefinedMacrosInFile(input_api, f): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6258 | """Check all affected files for invalid "if defined" macros.""" |
| 6259 | ALWAYS_DEFINED_MACROS=( |
| 6260 | "TARGET_CPU_PPC", |
| 6261 | "TARGET_CPU_PPC64", |
| 6262 | "TARGET_CPU_68K", |
| 6263 | "TARGET_CPU_X86", |
| 6264 | "TARGET_CPU_ARM", |
| 6265 | "TARGET_CPU_MIPS", |
| 6266 | "TARGET_CPU_SPARC", |
| 6267 | "TARGET_CPU_ALPHA", |
| 6268 | "TARGET_IPHONE_SIMULATOR", |
| 6269 | "TARGET_OS_EMBEDDED", |
| 6270 | "TARGET_OS_IPHONE", |
| 6271 | "TARGET_OS_MAC", |
| 6272 | "TARGET_OS_UNIX", |
| 6273 | "TARGET_OS_WIN32", |
| 6274 | ) |
| 6275 | ifdef_macro= input_api.re.compile( |
| 6276 | r'^\s*#.*(?:ifdef\s|defined\()([^\s\)]+)') |
| 6277 | results=[] |
| 6278 | for lnum, linein f.ChangedContents(): |
| 6279 | for matchin ifdef_macro.finditer(line): |
| 6280 | if match.group(1)in ALWAYS_DEFINED_MACROS: |
| 6281 | always_defined=' %s is always defined. '% match.group(1) |
| 6282 | did_you_mean='Did you mean \'#if %s\'?'% match.group(1) |
| 6283 | results.append( |
| 6284 | ' %s:%d %s\n\t%s'% |
| 6285 | (f.LocalPath(), lnum, always_defined, did_you_mean)) |
| 6286 | return results |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6287 | |
| 6288 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6289 | defCheckForInvalidIfDefinedMacros(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6290 | """Check all affected files for invalid "if defined" macros.""" |
Arthur Sonzogni | 4fd14fd | 2024-06-02 18:42:52 | [diff] [blame] | 6291 | SKIPPED_PATHS=[ |
| 6292 | 'base/allocator/partition_allocator/src/partition_alloc/build_config.h', |
| 6293 | 'build/build_config.h', |
| 6294 | 'third_party/abseil-cpp/', |
| 6295 | 'third_party/sqlite/', |
| 6296 | ] |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6297 | |
Arthur Sonzogni | 4fd14fd | 2024-06-02 18:42:52 | [diff] [blame] | 6298 | def affected_files_filter(f): |
| 6299 | # Normalize the local path to Linux-style path separators so that the |
| 6300 | # path comparisons work on Windows as well. |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 6301 | path= f.UnixLocalPath() |
Arthur Sonzogni | 4fd14fd | 2024-06-02 18:42:52 | [diff] [blame] | 6302 | |
| 6303 | for skipped_pathin SKIPPED_PATHS: |
| 6304 | if path.startswith(skipped_path): |
| 6305 | returnFalse |
| 6306 | |
| 6307 | return path.endswith(('.h','.c','.cc','.m','.mm')) |
| 6308 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6309 | bad_macros=[] |
Arthur Sonzogni | 4fd14fd | 2024-06-02 18:42:52 | [diff] [blame] | 6310 | for fin input_api.AffectedSourceFiles(affected_files_filter): |
| 6311 | bad_macros.extend(_CheckForInvalidIfDefinedMacrosInFile(input_api, f)) |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6312 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6313 | ifnot bad_macros: |
| 6314 | return[] |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6315 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6316 | return[ |
| 6317 | output_api.PresubmitError( |
| 6318 | 'Found ifdef check on always-defined macro[s]. Please fix your code\n' |
| 6319 | 'or check the list of ALWAYS_DEFINED_MACROS in src/PRESUBMIT.py.', |
| 6320 | bad_macros) |
| 6321 | ] |
lliabraa | 35bab393 | 2014-10-01 12:16:44 | [diff] [blame] | 6322 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6323 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6324 | defCheckForIPCRules(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6325 | """Check for same IPC rules described in |
| 6326 | http://www.chromium.org/Home/chromium-security/education/security-tips-for-ipc |
| 6327 | """ |
| 6328 | base_pattern= r'IPC_ENUM_TRAITS\(' |
| 6329 | inclusion_pattern= input_api.re.compile(r'(%s)'% base_pattern) |
| 6330 | comment_pattern= input_api.re.compile(r'//.*(%s)'% base_pattern) |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 6331 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6332 | problems=[] |
| 6333 | for fin input_api.AffectedSourceFiles(None): |
| 6334 | local_path= f.LocalPath() |
| 6335 | ifnot local_path.endswith('.h'): |
| 6336 | continue |
| 6337 | for line_number, linein f.ChangedContents(): |
| 6338 | if inclusion_pattern.search( |
| 6339 | line)andnot comment_pattern.search(line): |
| 6340 | problems.append('%s:%d\n %s'% |
| 6341 | (local_path, line_number, line.strip())) |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 6342 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6343 | if problems: |
| 6344 | return[ |
| 6345 | output_api.PresubmitPromptWarning(_IPC_ENUM_TRAITS_DEPRECATED, |
| 6346 | problems) |
| 6347 | ] |
| 6348 | else: |
| 6349 | return[] |
mlamouri | a8227262 | 2014-09-16 18:45:04 | [diff] [blame] | 6350 | |
dbeam@chromium.org | b00342e7f | 2013-03-26 16:21:54 | [diff] [blame] | 6351 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6352 | defCheckForLongPathnames(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6353 | """Check to make sure no files being submitted have long paths. |
| 6354 | This causes issues on Windows. |
| 6355 | """ |
| 6356 | problems=[] |
| 6357 | for fin input_api.AffectedTestableFiles(): |
| 6358 | local_path= f.LocalPath() |
| 6359 | # Windows has a path limit of 260 characters. Limit path length to 200 so |
| 6360 | # 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] | 6361 | if(local_path.startswith('third_party/blink/web_tests/platform/') |
| 6362 | andnot local_path.startswith( |
| 6363 | 'third_party/blink/web_tests/platform/win')): |
Weizhong Xia | 8b461f1 | 2024-06-21 21:46:33 | [diff] [blame] | 6364 | # Do not check length of the path for files not used by Windows |
| 6365 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6366 | if len(local_path)>200: |
| 6367 | problems.append(local_path) |
Stephen Martinis | 97a39414 | 2018-06-07 23:06:05 | [diff] [blame] | 6368 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6369 | if problems: |
| 6370 | return[output_api.PresubmitError(_LONG_PATH_ERROR, problems)] |
| 6371 | else: |
| 6372 | return[] |
Stephen Martinis | 97a39414 | 2018-06-07 23:06:05 | [diff] [blame] | 6373 | |
| 6374 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6375 | defCheckForIncludeGuards(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6376 | """Check that header files have proper guards against multiple inclusion. |
| 6377 | 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] | 6378 | should include the string "no-include-guard-because-multiply-included" or |
| 6379 | "no-include-guard-because-pch-file". |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6380 | """ |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6381 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6382 | def is_chromium_header_file(f): |
| 6383 | # We only check header files under the control of the Chromium |
mikt | 84d6c71 | 2024-03-27 13:29:03 | [diff] [blame] | 6384 | # project. This excludes: |
| 6385 | # - third_party/*, except blink. |
| 6386 | # - base/allocator/partition_allocator/: PartitionAlloc is a standalone |
| 6387 | # library used outside of Chrome. Includes are referenced from its |
| 6388 | # own base directory. It has its own `CheckForIncludeGuards` |
| 6389 | # PRESUBMIT.py check. |
| 6390 | # - *_message_generator.h: They use include guards in a special, |
| 6391 | # non-typical way. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6392 | file_with_path= input_api.os_path.normpath(f.LocalPath()) |
| 6393 | return(file_with_path.endswith('.h') |
| 6394 | andnot file_with_path.endswith('_message_generator.h') |
Bruce Dawson | 4c4c292 | 2022-05-02 18:07:33 | [diff] [blame] | 6395 | andnot file_with_path.endswith('com_imported_mstscax.h') |
Peter Kasting | 66c1f75 | 2024-12-02 15:28:37 | [diff] [blame] | 6396 | andnot file_with_path.startswith( |
| 6397 | input_api.os_path.join('base','allocator', |
| 6398 | 'partition_allocator')) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6399 | and(not file_with_path.startswith('third_party') |
| 6400 | or file_with_path.startswith( |
| 6401 | input_api.os_path.join('third_party','blink')))) |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6402 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6403 | def replace_special_with_underscore(string): |
| 6404 | return input_api.re.sub(r'[+\\/.-]','_', string) |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6405 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6406 | errors=[] |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6407 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6408 | for fin input_api.AffectedSourceFiles(is_chromium_header_file): |
| 6409 | guard_name=None |
| 6410 | guard_line_number=None |
| 6411 | seen_guard_end=False |
Lei Zhang | d84f951 | 2024-05-28 19:43:30 | [diff] [blame] | 6412 | bypass_checks_at_end_of_file=False |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6413 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6414 | file_with_path= input_api.os_path.normpath(f.LocalPath()) |
| 6415 | base_file_name= input_api.os_path.splitext( |
| 6416 | input_api.os_path.basename(file_with_path))[0] |
| 6417 | upper_base_file_name= base_file_name.upper() |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6418 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6419 | expected_guard= replace_special_with_underscore( |
| 6420 | file_with_path.upper()+'_') |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6421 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6422 | # For "path/elem/file_name.h" we should really only accept |
| 6423 | # PATH_ELEM_FILE_NAME_H_ per coding style. Unfortunately there |
| 6424 | # are too many (1000+) files with slight deviations from the |
| 6425 | # coding style. The most important part is that the include guard |
| 6426 | # is there, and that it's unique, not the name so this check is |
| 6427 | # forgiving for existing files. |
| 6428 | # |
| 6429 | # As code becomes more uniform, this could be made stricter. |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6430 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6431 | guard_name_pattern_list=[ |
| 6432 | # Anything with the right suffix (maybe with an extra _). |
| 6433 | r'\w+_H__?', |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6434 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6435 | # To cover include guards with old Blink style. |
| 6436 | r'\w+_h', |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6437 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6438 | # Anything including the uppercase name of the file. |
| 6439 | r'\w*'+ input_api.re.escape( |
| 6440 | replace_special_with_underscore(upper_base_file_name))+ |
| 6441 | r'\w*', |
| 6442 | ] |
| 6443 | guard_name_pattern='|'.join(guard_name_pattern_list) |
| 6444 | guard_pattern= input_api.re.compile(r'#ifndef\s+('+ |
| 6445 | guard_name_pattern+')') |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6446 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6447 | for line_number, linein enumerate(f.NewContents()): |
Bruce Dawson | 4a5579a | 2022-04-08 17:11:36 | [diff] [blame] | 6448 | if('no-include-guard-because-multiply-included'in line |
| 6449 | or'no-include-guard-because-pch-file'in line): |
Lei Zhang | d84f951 | 2024-05-28 19:43:30 | [diff] [blame] | 6450 | bypass_checks_at_end_of_file=True |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6451 | break |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6452 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6453 | if guard_nameisNone: |
| 6454 | match= guard_pattern.match(line) |
| 6455 | if match: |
| 6456 | guard_name= match.group(1) |
| 6457 | guard_line_number= line_number |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6458 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6459 | # We allow existing files to use include guards whose names |
| 6460 | # don't match the chromium style guide, but new files should |
| 6461 | # get it right. |
Bruce Dawson | 6cc154e | 2022-04-12 20:39:49 | [diff] [blame] | 6462 | if guard_name!= expected_guard: |
Bruce Dawson | 95eb756 | 2022-09-14 15:27:16 | [diff] [blame] | 6463 | if f.Action()=='A':# If file was just 'A'dded |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6464 | errors.append( |
| 6465 | output_api.PresubmitPromptWarning( |
| 6466 | 'Header using the wrong include guard name %s' |
| 6467 | % guard_name,[ |
| 6468 | '%s:%d'% |
| 6469 | (f.LocalPath(), line_number+1) |
| 6470 | ],'Expected: %r\nFound: %r'% |
| 6471 | (expected_guard, guard_name))) |
| 6472 | else: |
| 6473 | # The line after #ifndef should have a #define of the same name. |
| 6474 | if line_number== guard_line_number+1: |
| 6475 | expected_line='#define %s'% guard_name |
| 6476 | if line!= expected_line: |
| 6477 | errors.append( |
| 6478 | output_api.PresubmitPromptWarning( |
| 6479 | 'Missing "%s" for include guard'% |
| 6480 | expected_line, |
| 6481 | ['%s:%d'%(f.LocalPath(), line_number+1)], |
| 6482 | 'Expected: %r\nGot: %r'% |
| 6483 | (expected_line, line))) |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6484 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6485 | ifnot seen_guard_endand line=='#endif // %s'% guard_name: |
| 6486 | seen_guard_end=True |
| 6487 | elif seen_guard_end: |
| 6488 | if line.strip()!='': |
| 6489 | errors.append( |
| 6490 | output_api.PresubmitPromptWarning( |
| 6491 | 'Include guard %s not covering the whole file' |
| 6492 | %(guard_name),[f.LocalPath()])) |
| 6493 | break# Nothing else to check and enough to warn once. |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6494 | |
Lei Zhang | d84f951 | 2024-05-28 19:43:30 | [diff] [blame] | 6495 | if bypass_checks_at_end_of_file: |
| 6496 | continue |
| 6497 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6498 | if guard_nameisNone: |
| 6499 | errors.append( |
| 6500 | output_api.PresubmitPromptWarning( |
Bruce Dawson | 32114b6 | 2022-04-11 16:45:49 | [diff] [blame] | 6501 | 'Missing include guard in %s\n' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6502 | 'Recommended name: %s\n' |
| 6503 | 'This check can be disabled by having the string\n' |
Bruce Dawson | 4a5579a | 2022-04-08 17:11:36 | [diff] [blame] | 6504 | '"no-include-guard-because-multiply-included" or\n' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6505 | '"no-include-guard-because-pch-file" in the header.'% |
| 6506 | (f.LocalPath(), expected_guard))) |
Lei Zhang | d84f951 | 2024-05-28 19:43:30 | [diff] [blame] | 6507 | elifnot seen_guard_end: |
| 6508 | errors.append( |
| 6509 | output_api.PresubmitPromptWarning( |
| 6510 | 'Incorrect or missing include guard #endif in %s\n' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6511 | 'Recommended #endif comment: // %s'% |
| 6512 | (f.LocalPath(), expected_guard))) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6513 | |
| 6514 | return errors |
Daniel Bratell | 8ba5272 | 2018-03-02 16:06:14 | [diff] [blame] | 6515 | |
| 6516 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6517 | defCheckForWindowsLineEndings(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6518 | """Check source code and known ascii text files for Windows style line |
| 6519 | endings. |
| 6520 | """ |
Bruce Dawson | 5efbdc65 | 2022-04-11 19:29:51 | [diff] [blame] | 6521 | known_text_files= r'.*\.(txt|html|htm|py|gyp|gypi|gn|isolate|icon)$' |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6522 | |
dpapad | fd421fb | 2025-02-13 00:47:32 | [diff] [blame] | 6523 | _WEBUI_FILES_EXTENSIONS= r'\.(css|html|js|ts|svg)$' |
| 6524 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6525 | file_inclusion_pattern=(known_text_files, |
| 6526 | r'.+%s'% _IMPLEMENTATION_EXTENSIONS, |
dpapad | fd421fb | 2025-02-13 00:47:32 | [diff] [blame] | 6527 | r'.+%s'% _HEADER_EXTENSIONS, |
| 6528 | r'.+%s'% _WEBUI_FILES_EXTENSIONS) |
| 6529 | |
| 6530 | # Exclude folder that contains .ts files that are actually binary video |
| 6531 | # format and not TypeScript. |
| 6532 | file_exclusion_pattern=(r'media/test/data/') |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6533 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6534 | problems=[] |
| 6535 | source_file_filter=lambda f: input_api.FilterSourceFile( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6536 | f, |
| 6537 | files_to_check=file_inclusion_pattern, |
dpapad | fd421fb | 2025-02-13 00:47:32 | [diff] [blame] | 6538 | files_to_skip=file_exclusion_pattern) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6539 | for fin input_api.AffectedSourceFiles(source_file_filter): |
Bruce Dawson | 5efbdc65 | 2022-04-11 19:29:51 | [diff] [blame] | 6540 | # Ignore test files that contain crlf intentionally. |
| 6541 | if f.LocalPath().endswith('crlf.txt'): |
Daniel Cheng | a37c03db | 2022-05-12 17:20:34 | [diff] [blame] | 6542 | continue |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6543 | include_file=False |
| 6544 | for linein input_api.ReadFile(f,'r').splitlines(True): |
| 6545 | if line.endswith('\r\n'): |
| 6546 | include_file=True |
| 6547 | if include_file: |
| 6548 | problems.append(f.LocalPath()) |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6549 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6550 | if problems: |
| 6551 | return[ |
| 6552 | output_api.PresubmitPromptWarning( |
| 6553 | 'Are you sure that you want ' |
| 6554 | 'these files to contain Windows style line endings?\n'+ |
| 6555 | '\n'.join(problems)) |
| 6556 | ] |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6557 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6558 | return[] |
| 6559 | |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6560 | |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 6561 | defCheckIconFilesForLicenseHeaders(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6562 | """Check that .icon files (which are fragments of C++) have license headers. |
| 6563 | """ |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 6564 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6565 | icon_files=(r'.*\.icon$',) |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 6566 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6567 | icons=lambda x: input_api.FilterSourceFile(x, files_to_check=icon_files) |
| 6568 | return input_api.canned_checks.CheckLicense(input_api, |
| 6569 | output_api, |
| 6570 | source_file_filter=icons) |
| 6571 | |
Evan Stade | 6cfc964c1 | 2021-05-18 20:21:16 | [diff] [blame] | 6572 | |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6573 | defCheckForUseOfChromeAppsDeprecations(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6574 | """Check source code for use of Chrome App technologies being |
| 6575 | deprecated. |
| 6576 | """ |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6577 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6578 | def_CheckForDeprecatedTech(input_api, |
| 6579 | output_api, |
| 6580 | detection_list, |
| 6581 | files_to_check=None, |
| 6582 | files_to_skip=None): |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6583 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6584 | if(files_to_checkor files_to_skip): |
| 6585 | source_file_filter=lambda f: input_api.FilterSourceFile( |
| 6586 | f, files_to_check=files_to_check, files_to_skip=files_to_skip) |
| 6587 | else: |
| 6588 | source_file_filter=None |
| 6589 | |
| 6590 | problems=[] |
| 6591 | |
| 6592 | for fin input_api.AffectedSourceFiles(source_file_filter): |
| 6593 | if f.Action()=='D': |
| 6594 | continue |
| 6595 | for _, linein f.ChangedContents(): |
| 6596 | if any(detectin linefor detectin detection_list): |
| 6597 | problems.append(f.LocalPath()) |
| 6598 | |
| 6599 | return problems |
| 6600 | |
| 6601 | # to avoid this presubmit script triggering warnings |
| 6602 | files_to_skip=['PRESUBMIT.py','PRESUBMIT_test.py'] |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6603 | |
| 6604 | problems=[] |
| 6605 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6606 | # NMF: any files with extensions .nmf or NMF |
| 6607 | _NMF_FILES= r'\.(nmf|NMF)$' |
| 6608 | problems+=_CheckForDeprecatedTech( |
| 6609 | input_api, |
| 6610 | output_api, |
| 6611 | detection_list=[''],# any change to the file will trigger warning |
| 6612 | files_to_check=[r'.+%s'% _NMF_FILES]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6613 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6614 | # MANIFEST: any manifest.json that in its diff includes "app": |
| 6615 | _MANIFEST_FILES= r'(manifest\.json)$' |
| 6616 | problems+=_CheckForDeprecatedTech( |
| 6617 | input_api, |
| 6618 | output_api, |
| 6619 | detection_list=['"app":'], |
| 6620 | files_to_check=[r'.*%s'% _MANIFEST_FILES]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6621 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6622 | # NaCl / PNaCl: any file that in its diff contains the strings in the list |
| 6623 | problems+=_CheckForDeprecatedTech( |
| 6624 | input_api, |
| 6625 | output_api, |
| 6626 | detection_list=['config=nacl','enable-nacl','cpu=pnacl','nacl_io'], |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 6627 | files_to_skip=files_to_skip+[r"^native_client_sdk/"]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6628 | |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 6629 | # PPAPI: any C/C++ file that in its diff includes a ppapi library |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6630 | problems+=_CheckForDeprecatedTech( |
| 6631 | input_api, |
| 6632 | output_api, |
| 6633 | detection_list=['#include "ppapi','#include <ppapi'], |
| 6634 | files_to_check=(r'.+%s'% _HEADER_EXTENSIONS, |
| 6635 | r'.+%s'% _IMPLEMENTATION_EXTENSIONS), |
Bruce Dawson | 40fece6 | 2022-09-16 19:58:31 | [diff] [blame] | 6636 | files_to_skip=[r"^ppapi/"]) |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6637 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6638 | if problems: |
| 6639 | return[ |
| 6640 | output_api.PresubmitPromptWarning( |
| 6641 | 'You are adding/modifying code' |
| 6642 | 'related to technologies which will soon be deprecated (Chrome Apps, NaCl,' |
| 6643 | ' PNaCl, PPAPI). See this blog post for more details:\n' |
| 6644 | 'https://blog.chromium.org/2020/08/changes-to-chrome-app-support-timeline.html\n' |
| 6645 | 'and this documentation for options to replace these technologies:\n' |
| 6646 | 'https://developer.chrome.com/docs/apps/migration/\n'+ |
| 6647 | '\n'.join(problems)) |
| 6648 | ] |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6649 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6650 | return[] |
Jose Magana | 2b456f2 | 2021-03-09 23:26:40 | [diff] [blame] | 6651 | |
mostynb | b639aca5 | 2015-01-07 20:31:23 | [diff] [blame] | 6652 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6653 | defCheckSyslogUseWarningOnUpload(input_api, output_api, src_file_filter=None): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6654 | """Checks that all source files use SYSLOG properly.""" |
| 6655 | syslog_files=[] |
| 6656 | for fin input_api.AffectedSourceFiles(src_file_filter): |
| 6657 | for line_number, linein f.ChangedContents(): |
| 6658 | if'SYSLOG'in line: |
| 6659 | syslog_files.append(f.LocalPath()+':'+ str(line_number)) |
pastarmovj | 032ba5bc | 2017-01-12 10:41:56 | [diff] [blame] | 6660 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6661 | if syslog_files: |
| 6662 | return[ |
| 6663 | output_api.PresubmitPromptWarning( |
| 6664 | 'Please make sure there are no privacy sensitive bits of data in SYSLOG' |
| 6665 | ' calls.\nFiles to check:\n', |
| 6666 | items=syslog_files) |
| 6667 | ] |
| 6668 | return[] |
pastarmovj | 89f7ee1 | 2016-09-20 14:58:13 | [diff] [blame] | 6669 | |
| 6670 | |
maruel@chromium.org | 1f7b417 | 2010-01-28 01:17:34 | [diff] [blame] | 6671 | defCheckChangeOnUpload(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6672 | if input_api.version<[2,0,0]: |
| 6673 | return[ |
| 6674 | output_api.PresubmitError( |
| 6675 | "Your depot_tools is out of date. " |
| 6676 | "This PRESUBMIT.py requires at least presubmit_support version 2.0.0, " |
| 6677 | "but your version is %d.%d.%d"% tuple(input_api.version)) |
| 6678 | ] |
| 6679 | results=[] |
| 6680 | results.extend( |
| 6681 | input_api.canned_checks.CheckPatchFormatted(input_api, output_api)) |
| 6682 | return results |
maruel@chromium.org | ca8d1984 | 2009-02-19 16:33:12 | [diff] [blame] | 6683 | |
| 6684 | |
| 6685 | defCheckChangeOnCommit(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6686 | if input_api.version<[2,0,0]: |
| 6687 | return[ |
| 6688 | output_api.PresubmitError( |
| 6689 | "Your depot_tools is out of date. " |
| 6690 | "This PRESUBMIT.py requires at least presubmit_support version 2.0.0, " |
| 6691 | "but your version is %d.%d.%d"% tuple(input_api.version)) |
| 6692 | ] |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6693 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6694 | results=[] |
| 6695 | # Make sure the tree is 'open'. |
| 6696 | results.extend( |
| 6697 | input_api.canned_checks.CheckTreeIsOpen( |
| 6698 | input_api, |
| 6699 | output_api, |
| 6700 | json_url='http://chromium-status.appspot.com/current?format=json')) |
maruel@chromium.org | 806e98e | 2010-03-19 17:49:27 | [diff] [blame] | 6701 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6702 | results.extend( |
| 6703 | input_api.canned_checks.CheckPatchFormatted(input_api, output_api)) |
| 6704 | results.extend( |
| 6705 | input_api.canned_checks.CheckChangeHasBugField(input_api, output_api)) |
| 6706 | results.extend( |
| 6707 | input_api.canned_checks.CheckChangeHasNoUnwantedTags( |
| 6708 | input_api, output_api)) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6709 | return results |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6710 | |
| 6711 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 6712 | defCheckStrings(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6713 | """Check string ICU syntax validity and if translation screenshots exist.""" |
| 6714 | # Skip translation screenshots check if a SkipTranslationScreenshotsCheck |
| 6715 | # footer is set to true. |
| 6716 | git_footers= input_api.change.GitFootersFromDescription() |
| 6717 | skip_screenshot_check_footer=[ |
| 6718 | footer.lower()for footerin git_footers.get( |
| 6719 | u'Skip-Translation-Screenshots-Check',[]) |
| 6720 | ] |
| 6721 | run_screenshot_check= u'true'notin skip_screenshot_check_footer |
Edward Lesmes | f7c5c6d | 2020-05-14 23:30:02 | [diff] [blame] | 6722 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6723 | import os |
| 6724 | import re |
| 6725 | import sys |
| 6726 | from ioimportStringIO |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6727 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6728 | new_or_added_paths= set(f.LocalPath()for fin input_api.AffectedFiles() |
| 6729 | if(f.Action()=='A'or f.Action()=='M')) |
| 6730 | removed_paths= set(f.LocalPath() |
| 6731 | for fin input_api.AffectedFiles(include_deletes=True) |
| 6732 | if f.Action()=='D') |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6733 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6734 | affected_grds=[ |
| 6735 | ffor fin input_api.AffectedFiles() |
| 6736 | if f.LocalPath().endswith(('.grd','.grdp')) |
| 6737 | ] |
| 6738 | affected_grds=[ |
| 6739 | ffor fin affected_grdsifnot'testdata'in f.LocalPath() |
| 6740 | ] |
| 6741 | ifnot affected_grds: |
| 6742 | return[] |
meacer | 8c0d383 | 2019-12-26 21:46:16 | [diff] [blame] | 6743 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6744 | affected_png_paths=[ |
Andrew Grieve | 713b89b | 2024-10-15 20:20:08 | [diff] [blame] | 6745 | f.LocalPath()for fin input_api.AffectedFiles() |
| 6746 | if f.LocalPath().endswith('.png') |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6747 | ] |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6748 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6749 | # Check for screenshots. Developers can upload screenshots using |
| 6750 | # tools/translation/upload_screenshots.py which finds and uploads |
| 6751 | # images associated with .grd files (e.g. test_grd/IDS_STRING.png for the |
| 6752 | # message named IDS_STRING in test.grd) and produces a .sha1 file (e.g. |
| 6753 | # test_grd/IDS_STRING.png.sha1) for each png when the upload is successful. |
| 6754 | # |
| 6755 | # The logic here is as follows: |
| 6756 | # |
| 6757 | # - If the CL has a .png file under the screenshots directory for a grd |
| 6758 | # file, warn the developer. Actual images should never be checked into the |
| 6759 | # Chrome repo. |
| 6760 | # |
| 6761 | # - If the CL contains modified or new messages in grd files and doesn't |
| 6762 | # contain the corresponding .sha1 files, warn the developer to add images |
| 6763 | # and upload them via tools/translation/upload_screenshots.py. |
| 6764 | # |
| 6765 | # - If the CL contains modified or new messages in grd files and the |
| 6766 | # corresponding .sha1 files, everything looks good. |
| 6767 | # |
| 6768 | # - If the CL contains removed messages in grd files but the corresponding |
| 6769 | # .sha1 files aren't removed, warn the developer to remove them. |
| 6770 | unnecessary_screenshots=[] |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6771 | invalid_sha1=[] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6772 | missing_sha1=[] |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6773 | missing_sha1_modified=[] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6774 | unnecessary_sha1_files=[] |
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 | # This checks verifies that the ICU syntax of messages this CL touched is |
| 6777 | # valid, and reports any found syntax errors. |
| 6778 | # Without this presubmit check, ICU syntax errors in Chromium strings can land |
| 6779 | # without developers being aware of them. Later on, such ICU syntax errors |
| 6780 | # break message extraction for translation, hence would block Chromium |
| 6781 | # translations until they are fixed. |
| 6782 | icu_syntax_errors=[] |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6783 | sha1_pattern= input_api.re.compile(r'^[a-fA-F0-9]{40}$', |
| 6784 | input_api.re.MULTILINE) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6785 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6786 | def_CheckScreenshotAdded(screenshots_dir, message_id): |
| 6787 | sha1_path= input_api.os_path.join(screenshots_dir, |
| 6788 | message_id+'.png.sha1') |
| 6789 | if sha1_pathnotin new_or_added_paths: |
| 6790 | missing_sha1.append(sha1_path) |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6791 | elifnot_CheckValidSha1(sha1_path): |
Sam Maier | b926c58c | 2023-08-08 19:58:25 | [diff] [blame] | 6792 | invalid_sha1.append(sha1_path) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6793 | |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6794 | def_CheckScreenshotModified(screenshots_dir, message_id): |
| 6795 | sha1_path= input_api.os_path.join(screenshots_dir, |
| 6796 | message_id+'.png.sha1') |
| 6797 | if sha1_pathnotin new_or_added_paths: |
| 6798 | missing_sha1_modified.append(sha1_path) |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6799 | elifnot_CheckValidSha1(sha1_path): |
Sam Maier | b926c58c | 2023-08-08 19:58:25 | [diff] [blame] | 6800 | invalid_sha1.append(sha1_path) |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 6801 | |
| 6802 | def_CheckValidSha1(sha1_path): |
Sam Maier | b926c58c | 2023-08-08 19:58:25 | [diff] [blame] | 6803 | return sha1_pattern.search( |
| 6804 | next("\n".join(f.NewContents())for fin input_api.AffectedFiles() |
| 6805 | if f.LocalPath()== sha1_path)) |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 6806 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6807 | def_CheckScreenshotRemoved(screenshots_dir, message_id): |
| 6808 | sha1_path= input_api.os_path.join(screenshots_dir, |
| 6809 | message_id+'.png.sha1') |
| 6810 | if input_api.os_path.exists( |
| 6811 | sha1_path)and sha1_pathnotin removed_paths: |
| 6812 | unnecessary_sha1_files.append(sha1_path) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6813 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6814 | def_ValidateIcuSyntax(text, level, signatures): |
| 6815 | """Validates ICU syntax of a text string. |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 6816 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6817 | Check if text looks similar to ICU and checks for ICU syntax correctness |
| 6818 | in this case. Reports various issues with ICU syntax and values of |
| 6819 | variants. Supports checking of nested messages. Accumulate information of |
| 6820 | each ICU messages found in the text for further checking. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6821 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6822 | Args: |
| 6823 | text: a string to check. |
| 6824 | level: a number of current nesting level. |
| 6825 | signatures: an accumulator, a list of tuple of (level, variable, |
| 6826 | kind, variants). |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6827 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6828 | Returns: |
| 6829 | None if a string is not ICU or no issue detected. |
| 6830 | A tuple of (message, start index, end index) if an issue detected. |
| 6831 | """ |
| 6832 | valid_types={ |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6833 | 'plural':(frozenset([ |
| 6834 | '=0','=1','=2','=3','zero','one','two','few','many', |
| 6835 | 'other' |
| 6836 | ]), frozenset(['=1','other'])), |
| 6837 | 'selectordinal':(frozenset([ |
| 6838 | '=0','=1','=2','=3','zero','one','two','few','many', |
| 6839 | 'other' |
| 6840 | ]), frozenset(['one','other'])), |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6841 | 'select':(frozenset(), frozenset(['other'])), |
| 6842 | } |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6843 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6844 | # Check if the message looks like an attempt to use ICU |
| 6845 | # plural. If yes - check if its syntax strictly matches ICU format. |
| 6846 | like= re.match(r'^[^{]*\{[^{]*\b(plural|selectordinal|select)\b', |
| 6847 | text) |
| 6848 | ifnot like: |
| 6849 | signatures.append((level,None,None,None)) |
| 6850 | return |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6851 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6852 | # Check for valid prefix and suffix |
| 6853 | m= re.match( |
| 6854 | r'^([^{]*\{)([a-zA-Z0-9_]+),\s*' |
| 6855 | r'(plural|selectordinal|select),\s*' |
| 6856 | r'(?:offset:\d+)?\s*(.*)', text, re.DOTALL) |
| 6857 | ifnot m: |
| 6858 | return(('This message looks like an ICU plural, ' |
| 6859 | 'but does not follow ICU syntax.'), like.start(), |
| 6860 | like.end()) |
| 6861 | starting, variable, kind, variant_pairs= m.groups() |
| 6862 | variants, depth, last_pos=_ParseIcuVariants(variant_pairs, |
| 6863 | m.start(4)) |
| 6864 | if depth: |
| 6865 | return('Invalid ICU format. Unbalanced opening bracket', last_pos, |
| 6866 | len(text)) |
| 6867 | first= text[0] |
| 6868 | ending= text[last_pos:] |
| 6869 | ifnot starting: |
| 6870 | return('Invalid ICU format. No initial opening bracket', |
| 6871 | last_pos-1, last_pos) |
| 6872 | ifnot endingor'}'notin ending: |
| 6873 | return('Invalid ICU format. No final closing bracket', |
| 6874 | last_pos-1, last_pos) |
| 6875 | elif first!='{': |
| 6876 | return(( |
| 6877 | 'Invalid ICU format. Extra characters at the start of a complex ' |
| 6878 | 'message (go/icu-message-migration): "%s"')% starting,0, |
| 6879 | len(starting)) |
| 6880 | elif ending!='}': |
| 6881 | return(( |
| 6882 | 'Invalid ICU format. Extra characters at the end of a complex ' |
| 6883 | 'message (go/icu-message-migration): "%s"')% ending, |
| 6884 | last_pos-1, len(text)-1) |
| 6885 | if kindnotin valid_types: |
| 6886 | return(('Unknown ICU message type %s. ' |
| 6887 | 'Valid types are: plural, select, selectordinal')% kind, |
| 6888 | 0,0) |
| 6889 | known, required= valid_types[kind] |
| 6890 | defined_variants= set() |
| 6891 | for variant, variant_range, value, value_rangein variants: |
| 6892 | start, end= variant_range |
| 6893 | if variantin defined_variants: |
| 6894 | return('Variant "%s" is defined more than once'% variant, |
| 6895 | start, end) |
| 6896 | elif knownand variantnotin known: |
| 6897 | return('Variant "%s" is not valid for %s message'% |
| 6898 | (variant, kind), start, end) |
| 6899 | defined_variants.add(variant) |
| 6900 | # Check for nested structure |
| 6901 | res=_ValidateIcuSyntax(value[1:-1], level+1, signatures) |
| 6902 | if res: |
| 6903 | return(res[0], res[1]+ value_range[0]+1, |
| 6904 | res[2]+ value_range[0]+1) |
| 6905 | missing= required- defined_variants |
| 6906 | if missing: |
| 6907 | return('Required variants missing: %s'%', '.join(missing),0, |
| 6908 | len(text)) |
| 6909 | signatures.append((level, variable, kind, defined_variants)) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6910 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6911 | def_ParseIcuVariants(text, offset=0): |
| 6912 | """Parse variants part of ICU complex message. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6913 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6914 | Builds a tuple of variant names and values, as well as |
| 6915 | their offsets in the input string. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6916 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6917 | Args: |
| 6918 | text: a string to parse |
| 6919 | offset: additional offset to add to positions in the text to get correct |
| 6920 | position in the complete ICU string. |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6921 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6922 | Returns: |
| 6923 | List of tuples, each tuple consist of four fields: variant name, |
| 6924 | variant name span (tuple of two integers), variant value, value |
| 6925 | span (tuple of two integers). |
| 6926 | """ |
| 6927 | depth, start, end=0,-1,-1 |
| 6928 | variants=[] |
| 6929 | key=None |
| 6930 | for idx, charin enumerate(text): |
| 6931 | if char=='{': |
| 6932 | ifnot depth: |
| 6933 | start= idx |
| 6934 | chunk= text[end+1:start] |
| 6935 | key= chunk.strip() |
| 6936 | pos= offset+ end+1+ chunk.find(key) |
| 6937 | span=(pos, pos+ len(key)) |
| 6938 | depth+=1 |
| 6939 | elif char=='}': |
| 6940 | ifnot depth: |
| 6941 | return variants, depth, offset+ idx |
| 6942 | depth-=1 |
| 6943 | ifnot depth: |
| 6944 | end= idx |
| 6945 | variants.append((key, span, text[start:end+1], |
| 6946 | (offset+ start, offset+ end+1))) |
| 6947 | return variants, depth, offset+ end+1 |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6948 | |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 6949 | old_sys_path= sys.path |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6950 | try: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6951 | sys.path= sys.path+[ |
| 6952 | input_api.os_path.join(input_api.PresubmitLocalPath(),'tools', |
| 6953 | 'translation') |
| 6954 | ] |
| 6955 | from helperimport grd_helper |
| 6956 | finally: |
| 6957 | sys.path= old_sys_path |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6958 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6959 | for fin affected_grds: |
| 6960 | file_path= f.LocalPath() |
| 6961 | old_id_to_msg_map={} |
| 6962 | new_id_to_msg_map={} |
| 6963 | # Note that this code doesn't check if the file has been deleted. This is |
| 6964 | # OK because it only uses the old and new file contents and doesn't load |
| 6965 | # the file via its path. |
| 6966 | # It's also possible that a file's content refers to a renamed or deleted |
| 6967 | # file via a <part> tag, such as <part file="now-deleted-file.grdp">. This |
| 6968 | # is OK as well, because grd_helper ignores <part> tags when loading .grd or |
| 6969 | # .grdp files. |
| 6970 | if file_path.endswith('.grdp'): |
| 6971 | if f.OldContents(): |
| 6972 | old_id_to_msg_map= grd_helper.GetGrdpMessagesFromString( |
| 6973 | '\n'.join(f.OldContents())) |
| 6974 | if f.NewContents(): |
| 6975 | new_id_to_msg_map= grd_helper.GetGrdpMessagesFromString( |
| 6976 | '\n'.join(f.NewContents())) |
| 6977 | else: |
| 6978 | file_dir= input_api.os_path.dirname(file_path)or'.' |
| 6979 | if f.OldContents(): |
| 6980 | old_id_to_msg_map= grd_helper.GetGrdMessages( |
| 6981 | StringIO('\n'.join(f.OldContents())), file_dir) |
| 6982 | if f.NewContents(): |
| 6983 | new_id_to_msg_map= grd_helper.GetGrdMessages( |
| 6984 | StringIO('\n'.join(f.NewContents())), file_dir) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6985 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6986 | grd_name, ext= input_api.os_path.splitext( |
| 6987 | input_api.os_path.basename(file_path)) |
| 6988 | screenshots_dir= input_api.os_path.join( |
| 6989 | input_api.os_path.dirname(file_path), |
| 6990 | grd_name+ ext.replace('.','_')) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 6991 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 6992 | # Compute added, removed and modified message IDs. |
| 6993 | old_ids= set(old_id_to_msg_map) |
| 6994 | new_ids= set(new_id_to_msg_map) |
| 6995 | added_ids= new_ids- old_ids |
| 6996 | removed_ids= old_ids- new_ids |
| 6997 | modified_ids= set([]) |
| 6998 | for keyin old_ids.intersection(new_ids): |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 6999 | if(old_id_to_msg_map[key].ContentsAsXml('',True) |
| 7000 | != new_id_to_msg_map[key].ContentsAsXml('',True)): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7001 | # The message content itself changed. Require an updated screenshot. |
| 7002 | modified_ids.add(key) |
| 7003 | elif old_id_to_msg_map[key].attrs['meaning']!= \ |
| 7004 | new_id_to_msg_map[key].attrs['meaning']: |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 7005 | # The message meaning changed. We later check for a screenshot. |
| 7006 | modified_ids.add(key) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7007 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7008 | if run_screenshot_check: |
| 7009 | # Check the screenshot directory for .png files. Warn if there is any. |
| 7010 | for png_pathin affected_png_paths: |
| 7011 | if png_path.startswith(screenshots_dir): |
| 7012 | unnecessary_screenshots.append(png_path) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7013 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7014 | for added_idin added_ids: |
| 7015 | _CheckScreenshotAdded(screenshots_dir, added_id) |
Rainhard Findling | d8d0437 | 2020-08-13 13:30:09 | [diff] [blame] | 7016 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7017 | for modified_idin modified_ids: |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 7018 | _CheckScreenshotModified(screenshots_dir, modified_id) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7019 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7020 | for removed_idin removed_ids: |
| 7021 | _CheckScreenshotRemoved(screenshots_dir, removed_id) |
| 7022 | |
| 7023 | # Check new and changed strings for ICU syntax errors. |
| 7024 | for keyin added_ids.union(modified_ids): |
| 7025 | msg= new_id_to_msg_map[key].ContentsAsXml('',True) |
| 7026 | err=_ValidateIcuSyntax(msg,0,[]) |
| 7027 | if errisnotNone: |
| 7028 | icu_syntax_errors.append(str(key)+': '+ str(err[0])) |
| 7029 | |
| 7030 | results=[] |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 7031 | if run_screenshot_check: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7032 | if unnecessary_screenshots: |
| 7033 | results.append( |
| 7034 | output_api.PresubmitError( |
| 7035 | 'Do not include actual screenshots in the changelist. Run ' |
| 7036 | 'tools/translate/upload_screenshots.py to upload them instead:', |
| 7037 | sorted(unnecessary_screenshots))) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7038 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7039 | if missing_sha1: |
| 7040 | results.append( |
| 7041 | output_api.PresubmitError( |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 7042 | 'You are adding UI strings.\n' |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7043 | 'To ensure the best translations, take screenshots of the relevant UI ' |
| 7044 | '(https://g.co/chrome/translation) and add these files to your ' |
| 7045 | 'changelist:', sorted(missing_sha1))) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7046 | |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 7047 | if invalid_sha1: |
| 7048 | results.append( |
| 7049 | output_api.PresubmitError( |
| 7050 | 'The following files do not seem to contain valid sha1 hashes. ' |
| 7051 | 'Make sure they contain hashes created by ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7052 | 'tools/translate/upload_screenshots.py:', |
| 7053 | sorted(invalid_sha1))) |
Jens Mueller | 054652c | 2023-05-10 15:12:30 | [diff] [blame] | 7054 | |
Bruce Dawson | 55776c4 | 2022-12-09 17:23:47 | [diff] [blame] | 7055 | if missing_sha1_modified: |
| 7056 | results.append( |
| 7057 | output_api.PresubmitError( |
| 7058 | 'You are modifying UI strings or their meanings.\n' |
| 7059 | 'To ensure the best translations, take screenshots of the relevant UI ' |
| 7060 | '(https://g.co/chrome/translation) and add these files to your ' |
| 7061 | 'changelist:', sorted(missing_sha1_modified))) |
| 7062 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7063 | if unnecessary_sha1_files: |
| 7064 | results.append( |
| 7065 | output_api.PresubmitError( |
| 7066 | 'You removed strings associated with these files. Remove:', |
| 7067 | sorted(unnecessary_sha1_files))) |
| 7068 | else: |
| 7069 | results.append( |
| 7070 | output_api.PresubmitPromptOrNotify('Skipping translation ' |
| 7071 | 'screenshots check.')) |
Mustafa Emre Acer | 29bf6ac9 | 2018-07-30 21:42:14 | [diff] [blame] | 7072 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7073 | if icu_syntax_errors: |
| 7074 | results.append( |
| 7075 | output_api.PresubmitPromptWarning( |
| 7076 | 'ICU syntax errors were found in the following strings (problems or ' |
| 7077 | 'feedback? Contact rainhard@chromium.org):', |
| 7078 | items=icu_syntax_errors)) |
Rainhard Findling | fc31844c5 | 2020-05-15 09:58:26 | [diff] [blame] | 7079 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7080 | return results |
Mustafa Emre Acer | 51f2f74 | 2020-03-09 19:41:12 | [diff] [blame] | 7081 | |
| 7082 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7083 | defCheckTranslationExpectations(input_api, |
| 7084 | output_api, |
| 7085 | repo_root=None, |
| 7086 | translation_expectations_path=None, |
| 7087 | grd_files=None): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7088 | import sys |
| 7089 | affected_grds=[ |
| 7090 | ffor fin input_api.AffectedFiles() |
| 7091 | if(f.LocalPath().endswith('.grd')or f.LocalPath().endswith('.grdp')) |
| 7092 | ] |
| 7093 | ifnot affected_grds: |
| 7094 | return[] |
| 7095 | |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7096 | old_sys_path= sys.path |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7097 | try: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7098 | sys.path= sys.path+[ |
| 7099 | input_api.os_path.join(input_api.PresubmitLocalPath(),'tools', |
| 7100 | 'translation') |
| 7101 | ] |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7102 | sys.path= sys.path+[ |
| 7103 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 7104 | 'third_party','depot_tools') |
| 7105 | ] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7106 | from helperimport git_helper |
| 7107 | from helperimport translation_helper |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7108 | import gclient_utils |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7109 | finally: |
| 7110 | sys.path= old_sys_path |
| 7111 | |
| 7112 | # Check that translation expectations can be parsed and we can get a list of |
| 7113 | # translatable grd files. |repo_root| and |translation_expectations_path| are |
| 7114 | # only passed by tests. |
| 7115 | ifnot repo_root: |
| 7116 | repo_root= input_api.PresubmitLocalPath() |
| 7117 | ifnot translation_expectations_path: |
| 7118 | translation_expectations_path= input_api.os_path.join( |
| 7119 | repo_root,'tools','gritsettings','translation_expectations.pyl') |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7120 | is_cog= gclient_utils.IsEnvCog() |
| 7121 | # Git is not available in cog workspaces. |
| 7122 | ifnot grd_filesandnot is_cog: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7123 | grd_files= git_helper.list_grds_in_repository(repo_root) |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7124 | ifnot grd_files: |
| 7125 | grd_files=[] |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7126 | |
| 7127 | # Ignore bogus grd files used only for testing |
Gao Sheng | a79ebd4 | 2022-08-08 17:25:59 | [diff] [blame] | 7128 | # ui/webui/resources/tools/generate_grd.py. |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7129 | ignore_path= input_api.os_path.join('ui','webui','resources','tools', |
| 7130 | 'tests') |
| 7131 | grd_files=[pfor pin grd_filesif ignore_pathnotin p] |
| 7132 | |
Ben Mason | 5d4c324 | 2025-04-15 20:28:37 | [diff] [blame] | 7133 | # Ensure no duplicate basenames. |
| 7134 | basename_to_src_paths={} |
| 7135 | for grd_pathin grd_files: |
| 7136 | basename= input_api.os_path.basename(grd_path) |
| 7137 | basename_to_src_paths.setdefault(basename,[]) |
| 7138 | basename_to_src_paths[basename].append(grd_path) |
| 7139 | for src_pathsin basename_to_src_paths.values(): |
| 7140 | if len(src_paths)>1: |
| 7141 | return[ |
| 7142 | output_api.PresubmitNotifyResult( |
| 7143 | 'Multiple string files have the same basename. This will result in ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7144 | 'missing translations. Files: %s'%', '.join(src_paths)) |
Ben Mason | 5d4c324 | 2025-04-15 20:28:37 | [diff] [blame] | 7145 | ] |
| 7146 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7147 | try: |
| 7148 | translation_helper.get_translatable_grds( |
Terrence Reilly | 313f44ff | 2025-01-22 15:10:14 | [diff] [blame] | 7149 | repo_root, grd_files, translation_expectations_path, is_cog) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7150 | exceptExceptionas e: |
| 7151 | return[ |
| 7152 | output_api.PresubmitNotifyResult( |
| 7153 | 'Failed to get a list of translatable grd files. This happens when:\n' |
| 7154 | ' - One of the modified grd or grdp files cannot be parsed or\n' |
| 7155 | ' - %s is not updated.\n' |
| 7156 | 'Stack:\n%s'%(translation_expectations_path, str(e))) |
| 7157 | ] |
Mustafa Emre Acer | 51f2f74 | 2020-03-09 19:41:12 | [diff] [blame] | 7158 | return[] |
| 7159 | |
Ken Rockot | c31f483 | 2020-05-29 18:58:51 | [diff] [blame] | 7160 | |
Saagar Sanghavi | fceeaae | 2020-08-12 16:40:36 | [diff] [blame] | 7161 | defCheckStableMojomChanges(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7162 | """Changes to [Stable] mojom types must preserve backward-compatibility.""" |
| 7163 | changed_mojoms= input_api.AffectedFiles( |
| 7164 | include_deletes=True, |
| 7165 | file_filter=lambda f: f.LocalPath().endswith(('.mojom'))) |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 7166 | |
Bruce Dawson | 344ab26 | 2022-06-04 11:35:10 | [diff] [blame] | 7167 | ifnot changed_mojomsor input_api.no_diffs: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7168 | return[] |
| 7169 | |
| 7170 | delta=[] |
| 7171 | for mojomin changed_mojoms: |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7172 | delta.append({ |
| 7173 | 'filename': mojom.LocalPath(), |
| 7174 | 'old':'\n'.join(mojom.OldContents())orNone, |
| 7175 | 'new':'\n'.join(mojom.NewContents())orNone, |
| 7176 | }) |
| 7177 | |
| 7178 | process= input_api.subprocess.Popen([ |
Takuto Ikuta | dca1022 | 2022-04-13 02:51:21 | [diff] [blame] | 7179 | input_api.python3_executable, |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7180 | input_api.os_path.join( |
| 7181 | input_api.PresubmitLocalPath(),'mojo','public','tools','mojom', |
| 7182 | 'check_stable_mojom_compatibility.py'),'--src-root', |
| 7183 | input_api.PresubmitLocalPath() |
| 7184 | ], |
| 7185 | stdin=input_api.subprocess.PIPE, |
| 7186 | stdout=input_api.subprocess.PIPE, |
| 7187 | stderr=input_api.subprocess.PIPE, |
| 7188 | universal_newlines=True) |
| 7189 | (x, error)= process.communicate(input=input_api.json.dumps(delta)) |
| 7190 | if process.returncode: |
| 7191 | return[ |
| 7192 | output_api.PresubmitError( |
| 7193 | 'One or more [Stable] mojom definitions appears to have been changed ' |
Alex Gough | c9992165 | 2024-02-15 22:59:12 | [diff] [blame] | 7194 | 'in a way that is not backward-compatible. See ' |
| 7195 | 'https://chromium.googlesource.com/chromium/src/+/HEAD/mojo/public/tools/bindings/README.md#versioning' |
| 7196 | ' for details.', |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7197 | long_text=error) |
| 7198 | ] |
Erik Staab | c734cd7a | 2021-11-23 03:11:52 | [diff] [blame] | 7199 | return[] |
| 7200 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7201 | |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7202 | defCheckDeprecationOfPreferences(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7203 | """Removing a preference should come with a deprecation.""" |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7204 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7205 | defFilterFile(affected_file): |
| 7206 | """Accept only .cc files and the like.""" |
| 7207 | file_inclusion_pattern=[r'.+%s'% _IMPLEMENTATION_EXTENSIONS] |
| 7208 | files_to_skip=(_EXCLUDED_PATHS+ _TEST_CODE_EXCLUDED_PATHS+ |
| 7209 | input_api.DEFAULT_FILES_TO_SKIP) |
| 7210 | return input_api.FilterSourceFile( |
| 7211 | affected_file, |
| 7212 | files_to_check=file_inclusion_pattern, |
| 7213 | files_to_skip=files_to_skip) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7214 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7215 | defModifiedLines(affected_file): |
| 7216 | """Returns a list of tuples (line number, line text) of added and removed |
| 7217 | lines. |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7218 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7219 | Deleted lines share the same line number as the previous line. |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7220 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7221 | This relies on the scm diff output describing each changed code section |
| 7222 | with a line of the form |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7223 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7224 | ^@@ <old line num>,<old size> <new line num>,<new size> @@$ |
| 7225 | """ |
| 7226 | line_num=0 |
| 7227 | modified_lines=[] |
| 7228 | for linein affected_file.GenerateScmDiff().splitlines(): |
| 7229 | # Extract <new line num> of the patch fragment (see format above). |
| 7230 | m= input_api.re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', |
| 7231 | line) |
| 7232 | if m: |
| 7233 | line_num= int(m.groups(1)[0]) |
| 7234 | continue |
| 7235 | if((line.startswith('+')andnot line.startswith('++')) |
| 7236 | or(line.startswith('-')andnot line.startswith('--'))): |
| 7237 | modified_lines.append((line_num, line)) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7238 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7239 | ifnot line.startswith('-'): |
| 7240 | line_num+=1 |
| 7241 | return modified_lines |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7242 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7243 | defFindLineWith(lines, needle): |
| 7244 | """Returns the line number (i.e. index + 1) in `lines` containing `needle`. |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7245 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7246 | If 0 or >1 lines contain `needle`, -1 is returned. |
| 7247 | """ |
| 7248 | matching_line_numbers=[ |
| 7249 | # + 1 for 1-based counting of line numbers. |
| 7250 | i+1for i, linein enumerate(lines)if needlein line |
| 7251 | ] |
| 7252 | return matching_line_numbers[0]if len( |
| 7253 | matching_line_numbers)==1else-1 |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7254 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7255 | defModifiedPrefMigration(affected_file): |
| 7256 | """Returns whether the MigrateObsolete.*Pref functions were modified.""" |
| 7257 | # Determine first and last lines of MigrateObsolete.*Pref functions. |
| 7258 | new_contents= affected_file.NewContents() |
| 7259 | range_1=(FindLineWith(new_contents, |
| 7260 | 'BEGIN_MIGRATE_OBSOLETE_LOCAL_STATE_PREFS'), |
| 7261 | FindLineWith(new_contents, |
| 7262 | 'END_MIGRATE_OBSOLETE_LOCAL_STATE_PREFS')) |
| 7263 | range_2=(FindLineWith(new_contents, |
| 7264 | 'BEGIN_MIGRATE_OBSOLETE_PROFILE_PREFS'), |
| 7265 | FindLineWith(new_contents, |
| 7266 | 'END_MIGRATE_OBSOLETE_PROFILE_PREFS')) |
| 7267 | if(-1in range_1+ range_2): |
| 7268 | raiseException( |
| 7269 | 'Broken .*MIGRATE_OBSOLETE_.*_PREFS markers in browser_prefs.cc.' |
| 7270 | ) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7271 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7272 | # Check whether any of the modified lines are part of the |
| 7273 | # MigrateObsolete.*Pref functions. |
| 7274 | for line_nr, lineinModifiedLines(affected_file): |
| 7275 | if(range_1[0]<= line_nr<= range_1[1] |
| 7276 | or range_2[0]<= line_nr<= range_2[1]): |
| 7277 | returnTrue |
| 7278 | returnFalse |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7279 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7280 | register_pref_pattern= input_api.re.compile(r'Register.+Pref') |
| 7281 | browser_prefs_file_pattern= input_api.re.compile( |
| 7282 | r'chrome/browser/prefs/browser_prefs.cc') |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7283 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7284 | changes= input_api.AffectedFiles(include_deletes=True, |
| 7285 | file_filter=FilterFile) |
| 7286 | potential_problems=[] |
| 7287 | for fin changes: |
| 7288 | for linein f.GenerateScmDiff().splitlines(): |
| 7289 | # Check deleted lines for pref registrations. |
| 7290 | if(line.startswith('-')andnot line.startswith('--') |
| 7291 | and register_pref_pattern.search(line)): |
| 7292 | potential_problems.append('%s: %s'%(f.LocalPath(), line)) |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7293 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7294 | if browser_prefs_file_pattern.search(f.LocalPath()): |
| 7295 | # If the developer modified the MigrateObsolete.*Prefs() functions, we |
| 7296 | # assume that they knew that they have to deprecate preferences and don't |
| 7297 | # warn. |
| 7298 | try: |
| 7299 | ifModifiedPrefMigration(f): |
| 7300 | return[] |
| 7301 | exceptExceptionas e: |
| 7302 | return[output_api.PresubmitError(str(e))] |
Dominic Battre | 645d4234 | 2020-12-04 16:14:10 | [diff] [blame] | 7303 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7304 | if potential_problems: |
| 7305 | return[ |
| 7306 | output_api.PresubmitPromptWarning( |
| 7307 | 'Discovered possible removal of preference registrations.\n\n' |
| 7308 | 'Please make sure to properly deprecate preferences by clearing their\n' |
| 7309 | 'value for a couple of milestones before finally removing the code.\n' |
| 7310 | 'Otherwise data may stay in the preferences files forever. See\n' |
| 7311 | 'Migrate*Prefs() in chrome/browser/prefs/browser_prefs.cc and\n' |
| 7312 | 'chrome/browser/prefs/README.md for examples.\n' |
| 7313 | 'This may be a false positive warning (e.g. if you move preference\n' |
| 7314 | 'registrations to a different place).\n', potential_problems) |
| 7315 | ] |
| 7316 | return[] |
| 7317 | |
Matt Stark | 6ef0887 | 2021-07-29 01:21:46 | [diff] [blame] | 7318 | |
| 7319 | defCheckConsistentGrdChanges(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7320 | """Changes to GRD files must be consistent for tools to read them.""" |
| 7321 | changed_grds= input_api.AffectedFiles( |
| 7322 | include_deletes=False, |
| 7323 | file_filter=lambda f: f.LocalPath().endswith(('.grd'))) |
| 7324 | errors=[] |
| 7325 | invalid_file_regexes=[(input_api.re.compile(matcher), msg) |
| 7326 | for matcher, msgin _INVALID_GRD_FILE_LINE] |
| 7327 | for grdin changed_grds: |
| 7328 | for i, linein enumerate(grd.NewContents()): |
| 7329 | for matcher, msgin invalid_file_regexes: |
| 7330 | if matcher.search(line): |
| 7331 | errors.append( |
| 7332 | output_api.PresubmitError( |
| 7333 | 'Problem on {grd}:{i} - {msg}'.format( |
| 7334 | grd=grd.LocalPath(), i=i+1, msg=msg))) |
| 7335 | return errors |
| 7336 | |
Kevin McNee | 967dd2d2 | 2021-11-15 16:09:29 | [diff] [blame] | 7337 | |
Henrique Ferreiro | 2a4b5594 | 2021-11-29 23:45:36 | [diff] [blame] | 7338 | defCheckAssertAshOnlyCode(input_api, output_api): |
| 7339 | """Errors if a BUILD.gn file in an ash/ directory doesn't include |
Georg Neis | 94f87f0 | 2024-10-22 08:20:13 | [diff] [blame] | 7340 | assert(is_chromeos). |
| 7341 | For a transition period, assert(is_chromeos_ash) is also accepted. |
Henrique Ferreiro | 2a4b5594 | 2021-11-29 23:45:36 | [diff] [blame] | 7342 | """ |
| 7343 | |
| 7344 | defFileFilter(affected_file): |
| 7345 | """Includes directories known to be Ash only.""" |
| 7346 | return input_api.FilterSourceFile( |
| 7347 | affected_file, |
| 7348 | files_to_check=( |
| 7349 | r'^ash/.*BUILD\.gn',# Top-level src/ash/. |
| 7350 | r'.*/ash/.*BUILD\.gn'),# Any path component. |
| 7351 | files_to_skip=(input_api.DEFAULT_FILES_TO_SKIP)) |
| 7352 | |
| 7353 | errors=[] |
Georg Neis | 94f87f0 | 2024-10-22 08:20:13 | [diff] [blame] | 7354 | pattern= input_api.re.compile(r'assert\(is_chromeos(_ash)?\b') |
Jameson Thies | 0ce669f | 2021-12-09 15:56:56 | [diff] [blame] | 7355 | for fin input_api.AffectedFiles(include_deletes=False, |
| 7356 | file_filter=FileFilter): |
Henrique Ferreiro | 2a4b5594 | 2021-11-29 23:45:36 | [diff] [blame] | 7357 | if(not pattern.search(input_api.ReadFile(f))): |
| 7358 | errors.append( |
| 7359 | output_api.PresubmitError( |
Georg Neis | 94f87f0 | 2024-10-22 08:20:13 | [diff] [blame] | 7360 | 'Please add assert(is_chromeos) to %s. If that\'s not ' |
| 7361 | 'possible, please create an issue and add a comment such ' |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7362 | 'as:\n # TODO(crbug.com/XXX): add ' |
Georg Neis | 94f87f0 | 2024-10-22 08:20:13 | [diff] [blame] | 7363 | 'assert(is_chromeos) when ...'% f.LocalPath())) |
Henrique Ferreiro | 2a4b5594 | 2021-11-29 23:45:36 | [diff] [blame] | 7364 | return errors |
Lukasz Anforowicz | 7016d05e | 2021-11-30 03:56:27 | [diff] [blame] | 7365 | |
| 7366 | |
Kalvin Lee | 84ad17a | 2023-09-25 11:14:41 | [diff] [blame] | 7367 | def_IsMiraclePtrDisallowed(input_api, affected_file): |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 7368 | path= affected_file.UnixLocalPath() |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7369 | ifnot_IsCPlusPlusFile(input_api, path): |
| 7370 | returnFalse |
| 7371 | |
Bartek Nowierski | 49b1a45 | 2024-06-08 00:24:35 | [diff] [blame] | 7372 | # Renderer-only code is generally allowed to use MiraclePtr. These |
| 7373 | # directories, however, are specifically disallowed, for perf reasons. |
Kalvin Lee | 84ad17a | 2023-09-25 11:14:41 | [diff] [blame] | 7374 | if("third_party/blink/renderer/core/"in path |
| 7375 | or"third_party/blink/renderer/platform/heap/"in path |
Bartek Nowierski | 49b1a45 | 2024-06-08 00:24:35 | [diff] [blame] | 7376 | or"third_party/blink/renderer/platform/wtf/"in path |
| 7377 | or"third_party/blink/renderer/platform/fonts/"in path): |
| 7378 | returnTrue |
| 7379 | |
| 7380 | # The below paths are an explicitly listed subset of Renderer-only code, |
| 7381 | # because the plan is to Oilpanize it. |
| 7382 | # TODO(crbug.com/330759291): Remove once Oilpanization is completed or |
| 7383 | # abandoned. |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7384 | if("third_party/blink/renderer/core/paint/"in pathor |
| 7385 | "third_party/blink/renderer/platform/graphics/compositing/"in path |
Bartek Nowierski | 49b1a45 | 2024-06-08 00:24:35 | [diff] [blame] | 7386 | or"third_party/blink/renderer/platform/graphics/paint/"in path): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7387 | returnTrue |
| 7388 | |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7389 | # We assume that everything else may be used outside of Renderer processes. |
Lukasz Anforowicz | 7016d05e | 2021-11-30 03:56:27 | [diff] [blame] | 7390 | returnFalse |
| 7391 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7392 | |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7393 | # TODO(crbug.com/40206238): Remove these checks, once they are replaced |
Lukasz Anforowicz | 7016d05e | 2021-11-30 03:56:27 | [diff] [blame] | 7394 | # by the Chromium Clang Plugin (which will be preferable because it will |
| 7395 | # 1) report errors earlier - at compile-time and 2) cover more rules). |
| 7396 | defCheckRawPtrUsage(input_api, output_api): |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7397 | """Rough checks that raw_ptr<T> usage guidelines are followed.""" |
| 7398 | errors=[] |
| 7399 | # The regex below matches "raw_ptr<" following a word boundary, but not in a |
| 7400 | # C++ comment. |
| 7401 | raw_ptr_matcher= input_api.re.compile(r'^((?!//).)*\braw_ptr<') |
Kalvin Lee | 84ad17a | 2023-09-25 11:14:41 | [diff] [blame] | 7402 | file_filter=lambda f:_IsMiraclePtrDisallowed(input_api, f) |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7403 | for f, line_num, linein input_api.RightHandSideLines(file_filter): |
| 7404 | if raw_ptr_matcher.search(line): |
| 7405 | errors.append( |
| 7406 | output_api.PresubmitError( |
| 7407 | 'Problem on {path}:{line} - '\ |
Kalvin Lee | 84ad17a | 2023-09-25 11:14:41 | [diff] [blame] | 7408 | 'raw_ptr<T> should not be used in this renderer code '\ |
Sam Maier | a6e76d7 | 2022-02-11 21:43:50 | [diff] [blame] | 7409 | '(as documented in the "Pointers to unprotected memory" '\ |
| 7410 | 'section in //base/memory/raw_ptr.md)'.format( |
| 7411 | path=f.LocalPath(), line=line_num))) |
| 7412 | return errors |
Henrique Ferreiro | f9819f2e3 | 2021-11-30 13:31:56 | [diff] [blame] | 7413 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7414 | |
mikt | 9337567c | 2023-09-08 18:38:17 | [diff] [blame] | 7415 | defCheckAdvancedMemorySafetyChecksUsage(input_api, output_api): |
| 7416 | """Checks that ADVANCED_MEMORY_SAFETY_CHECKS() macro is neither added nor |
| 7417 | removed as it is managed by the memory safety team internally. |
| 7418 | Do not add / remove it manually.""" |
| 7419 | paths= set([]) |
| 7420 | # The regex below matches "ADVANCED_MEMORY_SAFETY_CHECKS(" following a word |
| 7421 | # boundary, but not in a C++ comment. |
| 7422 | macro_matcher= input_api.re.compile( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7423 | r'^((?!//).)*\bADVANCED_MEMORY_SAFETY_CHECKS\(', |
| 7424 | input_api.re.MULTILINE) |
mikt | 9337567c | 2023-09-08 18:38:17 | [diff] [blame] | 7425 | for fin input_api.AffectedFiles(): |
| 7426 | ifnot_IsCPlusPlusFile(input_api, f.LocalPath()): |
| 7427 | continue |
| 7428 | if macro_matcher.search(f.GenerateScmDiff()): |
| 7429 | paths.add(f.LocalPath()) |
| 7430 | ifnot paths: |
| 7431 | return[] |
| 7432 | return[output_api.PresubmitPromptWarning( |
| 7433 | 'ADVANCED_MEMORY_SAFETY_CHECKS() macro is managed by ' \ |
| 7434 | 'the memory safety team (chrome-memory-safety@). ' \ |
| 7435 | 'Please contact us to add/delete the uses of the macro.', |
| 7436 | paths)] |
Henrique Ferreiro | f9819f2e3 | 2021-11-30 13:31:56 | [diff] [blame] | 7437 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7438 | |
Henrique Ferreiro | f9819f2e3 | 2021-11-30 13:31:56 | [diff] [blame] | 7439 | defCheckPythonShebang(input_api, output_api): |
| 7440 | """Checks that python scripts use #!/usr/bin/env instead of hardcoding a |
| 7441 | system-wide python. |
| 7442 | """ |
| 7443 | errors=[] |
| 7444 | sources=lambda affected_file: input_api.FilterSourceFile( |
| 7445 | affected_file, |
| 7446 | files_to_skip=((_THIRD_PARTY_EXCEPT_BLINK, |
| 7447 | r'third_party/blink/web_tests/external/')+ input_api. |
| 7448 | DEFAULT_FILES_TO_SKIP), |
| 7449 | files_to_check=[r'.*\.py$']) |
| 7450 | for fin input_api.AffectedSourceFiles(sources): |
Takuto Ikuta | 3697651 | 2021-11-30 23:15:27 | [diff] [blame] | 7451 | for line_num, linein f.ChangedContents(): |
| 7452 | if line_num==1and line.startswith('#!/usr/bin/python'): |
| 7453 | errors.append(f.LocalPath()) |
| 7454 | break |
Henrique Ferreiro | f9819f2e3 | 2021-11-30 13:31:56 | [diff] [blame] | 7455 | |
| 7456 | result=[] |
| 7457 | for filein errors: |
| 7458 | result.append( |
| 7459 | output_api.PresubmitError( |
| 7460 | "Please use '#!/usr/bin/env python/2/3' as the shebang of %s"% |
| 7461 | file)) |
| 7462 | return result |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7463 | |
| 7464 | |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7465 | defCheckAndroidTestAnnotations(input_api, output_api): |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7466 | """Checks that tests have either @Batch or @DoNotBatch annotation. If this |
| 7467 | is not an instrumentation test, disregard.""" |
| 7468 | |
| 7469 | batch_annotation= input_api.re.compile(r'^\s*@Batch') |
| 7470 | do_not_batch_annotation= input_api.re.compile(r'^\s*@DoNotBatch') |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7471 | robolectric_test= input_api.re.compile( |
| 7472 | r'@RunWith\((.*?)RobolectricTestRunner') |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7473 | test_class_declaration= input_api.re.compile(r'^\s*public\sclass.*Test') |
| 7474 | uiautomator_test= input_api.re.compile(r'[uU]i[aA]utomator') |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7475 | test_annotation_declaration= input_api.re.compile( |
| 7476 | r'^\s*public\s@interface\s.*{') |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7477 | |
ckitagawa | e8fd23b | 2022-06-17 15:29:38 | [diff] [blame] | 7478 | missing_annotation_errors=[] |
| 7479 | extra_annotation_errors=[] |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7480 | wrong_robolectric_test_runner_errors=[] |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7481 | |
| 7482 | def_FilterFile(affected_file): |
| 7483 | return input_api.FilterSourceFile( |
| 7484 | affected_file, |
| 7485 | files_to_skip=input_api.DEFAULT_FILES_TO_SKIP, |
| 7486 | files_to_check=[r'.*Test\.java$']) |
| 7487 | |
| 7488 | for fin input_api.AffectedSourceFiles(_FilterFile): |
| 7489 | batch_matched=None |
| 7490 | do_not_batch_matched=None |
| 7491 | is_instrumentation_test=True |
Mark Schillaci | 8ef0d87 | 2023-07-18 22:07:59 | [diff] [blame] | 7492 | test_annotation_declaration_matched=None |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7493 | has_base_robolectric_rule=False |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7494 | for linein f.NewContents(): |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7495 | if'BaseRobolectricTestRule'in line: |
| 7496 | has_base_robolectric_rule=True |
| 7497 | continue |
| 7498 | if m:= robolectric_test.search(line): |
| 7499 | is_instrumentation_test=False |
| 7500 | if m.group(1)==''andnot has_base_robolectric_rule: |
Yiwei Zhang | 5341bf0 | 2025-03-20 16:34:13 | [diff] [blame] | 7501 | path= str(f.LocalPath()) |
| 7502 | # These two spots cannot use it. |
| 7503 | if'webapk'notin pathand'build'notin path: |
| 7504 | wrong_robolectric_test_runner_errors.append(path) |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7505 | break |
| 7506 | if uiautomator_test.search(line): |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7507 | is_instrumentation_test=False |
| 7508 | break |
| 7509 | ifnot batch_matched: |
| 7510 | batch_matched= batch_annotation.search(line) |
| 7511 | ifnot do_not_batch_matched: |
| 7512 | do_not_batch_matched= do_not_batch_annotation.search(line) |
| 7513 | test_class_declaration_matched= test_class_declaration.search( |
| 7514 | line) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7515 | test_annotation_declaration_matched= test_annotation_declaration.search( |
| 7516 | line) |
Mark Schillaci | 8ef0d87 | 2023-07-18 22:07:59 | [diff] [blame] | 7517 | if test_class_declaration_matchedor test_annotation_declaration_matched: |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7518 | break |
Mark Schillaci | 8ef0d87 | 2023-07-18 22:07:59 | [diff] [blame] | 7519 | if test_annotation_declaration_matched: |
| 7520 | continue |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7521 | if(is_instrumentation_testandnot batch_matched |
| 7522 | andnot do_not_batch_matched): |
Sam Maier | 4cef924 | 2022-10-03 14:21:24 | [diff] [blame] | 7523 | missing_annotation_errors.append(str(f.LocalPath())) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7524 | if(not is_instrumentation_test |
| 7525 | and(batch_matchedor do_not_batch_matched)): |
Sam Maier | 4cef924 | 2022-10-03 14:21:24 | [diff] [blame] | 7526 | extra_annotation_errors.append(str(f.LocalPath())) |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7527 | |
| 7528 | results=[] |
| 7529 | |
ckitagawa | e8fd23b | 2022-06-17 15:29:38 | [diff] [blame] | 7530 | if missing_annotation_errors: |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7531 | results.append( |
| 7532 | output_api.PresubmitPromptWarning( |
| 7533 | """ |
Andrew Grieve | 43a5cf8 | 2023-09-08 15:09:46 | [diff] [blame] | 7534 | A change was made to an on-device test that has neither been annotated with |
| 7535 | @Batch nor @DoNotBatch. If this is a new test, please add the annotation. If |
| 7536 | this is an existing test, please consider adding it if you are sufficiently |
| 7537 | familiar with the test (but do so as a separate change). |
| 7538 | |
Jens Mueller | 2085ff8 | 2023-02-27 11:54:49 | [diff] [blame] | 7539 | 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] | 7540 | """, missing_annotation_errors)) |
| 7541 | if extra_annotation_errors: |
| 7542 | results.append( |
| 7543 | output_api.PresubmitPromptWarning( |
| 7544 | """ |
| 7545 | Robolectric tests do not need a @Batch or @DoNotBatch annotations. |
| 7546 | """, extra_annotation_errors)) |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7547 | if wrong_robolectric_test_runner_errors: |
| 7548 | results.append( |
| 7549 | output_api.PresubmitPromptWarning( |
| 7550 | """ |
Wenyu Fu | 0005ab8 | 2025-01-03 18:13:26 | [diff] [blame] | 7551 | Robolectric tests should use either @RunWith(BaseRobolectricTestRunner.class) (or |
Andrew Grieve | 5a66ae7 | 2024-12-13 15:21:53 | [diff] [blame] | 7552 | a subclass of it), or use "@Rule BaseRobolectricTestRule". |
| 7553 | """, wrong_robolectric_test_runner_errors)) |
James Shen | 81cc0e2 | 2022-06-15 21:10:45 | [diff] [blame] | 7554 | |
| 7555 | return results |
Sam Maier | 4cef924 | 2022-10-03 14:21:24 | [diff] [blame] | 7556 | |
| 7557 | |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7558 | def_CheckAndroidNullAwayAnnotatedClasses(input_api, output_api): |
| 7559 | """Checks that Java classes/interfaces/annotations are null-annotated.""" |
| 7560 | |
Henrique Nakashima | 2bdd8ad | 2025-04-08 18:24:57 | [diff] [blame] | 7561 | # Temporary, crbug.com/389129271 |
| 7562 | if input_api.change.RepositoryRoot().endswith('clank'): |
| 7563 | return[] |
| 7564 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7565 | nullmarked_annotation= input_api.re.compile( |
| 7566 | r'^\s*@(NullMarked|NullUnmarked)') |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7567 | |
| 7568 | missing_annotation_errors=[] |
| 7569 | |
| 7570 | def_FilterFile(affected_file): |
| 7571 | return input_api.FilterSourceFile( |
| 7572 | affected_file, |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7573 | files_to_skip=( |
| 7574 | _EXCLUDED_PATHS+ _TEST_CODE_EXCLUDED_PATHS+ |
| 7575 | input_api.DEFAULT_FILES_TO_SKIP+( |
| 7576 | r'.*Test.*\.java', |
| 7577 | r'^android_webview/.*',# Temporary, crbug.com/389129271 |
| 7578 | r'^build/.*', |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7579 | r'^chromecast/.*', |
| 7580 | r'^components/cronet/.*', |
| 7581 | r'^tools/.*', |
| 7582 | )), |
| 7583 | files_to_check=[r'.*\.java$']) |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7584 | |
| 7585 | for fin input_api.AffectedSourceFiles(_FilterFile): |
Henrique Nakashima | c660543 | 2025-04-24 18:11:59 | [diff] [blame] | 7586 | if f.Action()!='A': |
| 7587 | continue |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7588 | for linein f.NewContents(): |
| 7589 | if nullmarked_annotation.search(line): |
| 7590 | break |
| 7591 | else: |
| 7592 | missing_annotation_errors.append(str(f.LocalPath())) |
| 7593 | |
| 7594 | results=[] |
| 7595 | |
| 7596 | if missing_annotation_errors: |
| 7597 | results.append( |
Henrique Nakashima | 8bafbc5 | 2025-04-22 19:38:42 | [diff] [blame] | 7598 | output_api.PresubmitError( |
Henrique Nakashima | 224ee248 | 2025-03-21 18:35:02 | [diff] [blame] | 7599 | """ |
| 7600 | Please add @NullMarked and fix the NullAway warnings in the following files |
| 7601 | (see https://chromium.googlesource.com/chromium/src/+/main/styleguide/java/nullaway.md): |
| 7602 | """, missing_annotation_errors)) |
| 7603 | |
| 7604 | return results |
| 7605 | |
| 7606 | |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7607 | defCheckNoJsInIos(input_api, output_api): |
| 7608 | """Checks to make sure that JavaScript files are not used on iOS.""" |
| 7609 | |
| 7610 | def_FilterFile(affected_file): |
| 7611 | return input_api.FilterSourceFile( |
| 7612 | affected_file, |
| 7613 | files_to_skip=input_api.DEFAULT_FILES_TO_SKIP+ |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7614 | (r'^ios/third_party/*', r'^ios/tools/*', r'^third_party/*', |
| 7615 | r'^components/autofill/ios/form_util/resources/*'), |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7616 | files_to_check=[r'^ios/.*\.js$', r'.*/ios/.*\.js$']) |
| 7617 | |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7618 | deleted_files=[] |
| 7619 | |
| 7620 | # Collect filenames of all removed JS files. |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 7621 | for fin input_api.AffectedFiles(file_filter=_FilterFile): |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7622 | local_path= f.LocalPath() |
| 7623 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7624 | if input_api.os_path.splitext( |
| 7625 | local_path)[1]=='.js'and f.Action()=='D': |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7626 | deleted_files.append(input_api.os_path.basename(local_path)) |
| 7627 | |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7628 | error_paths=[] |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7629 | moved_paths=[] |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7630 | warning_paths=[] |
| 7631 | |
| 7632 | for fin input_api.AffectedSourceFiles(_FilterFile): |
| 7633 | local_path= f.LocalPath() |
| 7634 | |
| 7635 | if input_api.os_path.splitext(local_path)[1]=='.js': |
| 7636 | if f.Action()=='A': |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7637 | if input_api.os_path.basename(local_path)in deleted_files: |
| 7638 | # This script was probably moved rather than newly created. |
| 7639 | # Present a warning instead of an error for these cases. |
| 7640 | moved_paths.append(local_path) |
| 7641 | else: |
| 7642 | error_paths.append(local_path) |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7643 | elif f.Action()!='D': |
| 7644 | warning_paths.append(local_path) |
| 7645 | |
| 7646 | results=[] |
| 7647 | |
| 7648 | if warning_paths: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7649 | results.append( |
| 7650 | output_api.PresubmitPromptWarning( |
| 7651 | 'TypeScript is now fully supported for iOS feature scripts. ' |
| 7652 | 'Consider converting JavaScript files to TypeScript. See ' |
| 7653 | '//ios/web/public/js_messaging/README.md for more details.', |
| 7654 | warning_paths)) |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7655 | |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7656 | if moved_paths: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7657 | results.append( |
| 7658 | output_api.PresubmitPromptWarning( |
| 7659 | 'Do not use JavaScript on iOS for new files as TypeScript is ' |
| 7660 | 'fully supported. (If this is a moved file, you may leave the ' |
| 7661 | 'script unconverted.) See //ios/web/public/js_messaging/README.md ' |
| 7662 | 'for help using scripts on iOS.', moved_paths)) |
Mike Dougherty | 4d1050b | 2023-03-14 15:59:53 | [diff] [blame] | 7663 | |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7664 | if error_paths: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7665 | results.append( |
| 7666 | output_api.PresubmitError( |
| 7667 | 'Do not use JavaScript on iOS as TypeScript is fully supported. ' |
| 7668 | 'See //ios/web/public/js_messaging/README.md for help using ' |
| 7669 | 'scripts on iOS.', error_paths)) |
Mike Dougherty | 1b8be71 | 2022-10-20 00:15:13 | [diff] [blame] | 7670 | |
| 7671 | return results |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7672 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7673 | |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7674 | defCheckLibcxxRevisionsMatch(input_api, output_api): |
| 7675 | """Check to make sure the libc++ version matches across deps files.""" |
Andrew Grieve | 21bb679 | 2023-03-27 19:06:48 | [diff] [blame] | 7676 | # Disable check for changes to sub-repositories. |
| 7677 | if input_api.PresubmitLocalPath()!= input_api.change.RepositoryRoot(): |
Sam Maier | b926c58c | 2023-08-08 19:58:25 | [diff] [blame] | 7678 | return[] |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7679 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7680 | DEPS_FILES=['DEPS','buildtools/deps_revisions.gni'] |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7681 | |
Anton Bershanskyi | 425334948 | 2025-02-11 21:01:27 | [diff] [blame] | 7682 | file_filter=lambda f: f.UnixLocalPath()in DEPS_FILES |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7683 | changed_deps_files= input_api.AffectedFiles(file_filter=file_filter) |
| 7684 | ifnot changed_deps_files: |
| 7685 | return[] |
| 7686 | |
| 7687 | defLibcxxRevision(file): |
| 7688 | file= input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 7689 | *file.split('/')) |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7690 | return input_api.re.search(r'libcxx_revision.*[:=].*[\'"](\w+)[\'"]', |
| 7691 | input_api.ReadFile(file)).group(1) |
Hans Wennborg | 23a81d5 | 2023-03-24 16:38:13 | [diff] [blame] | 7692 | |
| 7693 | if len(set([LibcxxRevision(f)for fin DEPS_FILES]))==1: |
| 7694 | return[] |
| 7695 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7696 | return[ |
| 7697 | output_api.PresubmitError( |
| 7698 | 'libcxx_revision not equal across %s'%', '.join(DEPS_FILES), |
| 7699 | changed_deps_files) |
| 7700 | ] |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7701 | |
| 7702 | |
| 7703 | defCheckDanglingUntriaged(input_api, output_api): |
| 7704 | """Warn developers adding DanglingUntriaged raw_ptr.""" |
| 7705 | |
| 7706 | # Ignore during git presubmit --all. |
| 7707 | # |
| 7708 | # This would be too costly, because this would check every lines of every |
| 7709 | # C++ files. Check from _BANNED_CPP_FUNCTIONS are also reading the whole |
| 7710 | # source code, but only once to apply every checks. It seems the bots like |
| 7711 | # `win-presubmit` are particularly sensitive to reading the files. Adding |
| 7712 | # this check caused the bot to run 2x longer. See https://crbug.com/1486612. |
| 7713 | if input_api.no_diffs: |
Arthur Sonzogni | 9eafd22 | 2023-11-10 08:50:39 | [diff] [blame] | 7714 | return[] |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7715 | |
| 7716 | defFilterFile(file): |
| 7717 | return input_api.FilterSourceFile( |
| 7718 | file, |
| 7719 | files_to_check=[r".*\.(h|cc|cpp|cxx|m|mm)$"], |
| 7720 | files_to_skip=[r"^base/allocator.*"], |
| 7721 | ) |
| 7722 | |
| 7723 | count=0 |
Arthur Sonzogni | c66e9c8 | 2024-04-23 07:53:04 | [diff] [blame] | 7724 | for fin input_api.AffectedFiles(file_filter=FilterFile): |
Arthur Sonzogni | 9eafd22 | 2023-11-10 08:50:39 | [diff] [blame] | 7725 | count-= sum([l.count("DanglingUntriaged")for lin f.OldContents()]) |
| 7726 | count+= sum([l.count("DanglingUntriaged")for lin f.NewContents()]) |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7727 | |
| 7728 | # Most likely, nothing changed: |
| 7729 | if count==0: |
| 7730 | return[] |
| 7731 | |
| 7732 | # Congrats developers for improving it: |
| 7733 | if count<0: |
Arthur Sonzogni | 9eafd22 | 2023-11-10 08:50:39 | [diff] [blame] | 7734 | message= f"DanglingUntriaged pointers removed: {-count}\nThank you!" |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7735 | return[output_api.PresubmitNotifyResult(message)] |
| 7736 | |
| 7737 | # Check for 'DanglingUntriaged-notes' in the description: |
| 7738 | notes_regex= input_api.re.compile("DanglingUntriaged-notes[:=]") |
| 7739 | if any( |
| 7740 | notes_regex.match(line) |
| 7741 | for linein input_api.change.DescriptionText().splitlines()): |
| 7742 | return[] |
| 7743 | |
| 7744 | # Check for DanglingUntriaged-notes in the git footer: |
| 7745 | if input_api.change.GitFootersFromDescription().get( |
| 7746 | "DanglingUntriaged-notes",[]): |
| 7747 | return[] |
| 7748 | |
| 7749 | message=( |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7750 | "Unexpected new occurrences of `DanglingUntriaged` detected. Please\n" |
| 7751 | +"avoid adding new ones\n"+"\n"+"See documentation:\n"+ |
| 7752 | "https://chromium.googlesource.com/chromium/src/+/main/docs/dangling_ptr.md\n" |
| 7753 | +"\n"+"See also the guide to fix dangling pointers:\n"+ |
| 7754 | "https://chromium.googlesource.com/chromium/src/+/main/docs/dangling_ptr_guide.md\n" |
| 7755 | +"\n"+ |
Arthur Sonzogni | 9eafd22 | 2023-11-10 08:50:39 | [diff] [blame] | 7756 | "To disable this warning, please add in the commit description:\n"+ |
Alex Gough | 26dcd85 | 2023-12-22 16:47:19 | [diff] [blame] | 7757 | "DanglingUntriaged-notes: <rationale for new untriaged dangling "+ |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7758 | "pointers>") |
Arthur Sonzogni | 7109bd3 | 2023-10-03 10:34:42 | [diff] [blame] | 7759 | return[output_api.PresubmitPromptWarning(message)] |
Jan Keitel | 77be752 | 2023-10-12 20:40:49 | [diff] [blame] | 7760 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7761 | |
Jan Keitel | 77be752 | 2023-10-12 20:40:49 | [diff] [blame] | 7762 | defCheckInlineConstexprDefinitionsInHeaders(input_api, output_api): |
| 7763 | """Checks that non-static constexpr definitions in headers are inline.""" |
| 7764 | # In a properly formatted file, constexpr definitions inside classes or |
| 7765 | # structs will have additional whitespace at the beginning of the line. |
| 7766 | # The pattern looks for variables initialized as constexpr kVar = ...; or |
| 7767 | # constexpr kVar{...}; |
| 7768 | # The pattern does not match expressions that have braces in kVar to avoid |
| 7769 | # matching constexpr functions. |
| 7770 | pattern= input_api.re.compile(r'^constexpr (?!inline )[^\(\)]*[={]') |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7771 | attribute_pattern= input_api.re.compile( |
| 7772 | r'(\[\[[a-zA-Z_:]+\]\]|[A-Z]+[A-Z_]+) ') |
Jan Keitel | 77be752 | 2023-10-12 20:40:49 | [diff] [blame] | 7773 | problems=[] |
| 7774 | for fin input_api.AffectedFiles(): |
| 7775 | ifnot_IsCPlusPlusHeaderFile(input_api, f.LocalPath()): |
| 7776 | continue |
| 7777 | |
| 7778 | for line_number, linein f.ChangedContents(): |
| 7779 | line= attribute_pattern.sub('', line) |
| 7780 | if pattern.search(line): |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7781 | problems.append(f"{f.LocalPath()}: {line_number}\n {line}") |
Jan Keitel | 77be752 | 2023-10-12 20:40:49 | [diff] [blame] | 7782 | |
| 7783 | if problems: |
| 7784 | return[ |
| 7785 | output_api.PresubmitPromptWarning( |
| 7786 | 'Consider inlining constexpr variable definitions in headers ' |
| 7787 | 'outside of classes to avoid unnecessary copies of the ' |
| 7788 | 'constant. See https://abseil.io/tips/168 for more details.', |
| 7789 | problems) |
| 7790 | ] |
| 7791 | else: |
| 7792 | return[] |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7793 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7794 | |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7795 | defCheckTodoBugReferences(input_api, output_api): |
| 7796 | """Checks that bugs in TODOs use updated issue tracker IDs.""" |
| 7797 | |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7798 | files_to_skip=[ |
| 7799 | 'PRESUBMIT_test.py', r"^third_party/rust/chromium_crates_io/vendor/.*" |
| 7800 | ] |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7801 | |
| 7802 | def_FilterFile(affected_file): |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7803 | return input_api.FilterSourceFile(affected_file, |
| 7804 | files_to_skip=files_to_skip) |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7805 | |
| 7806 | # Monorail bug IDs are all less than or equal to 1524553 so check that all |
| 7807 | # bugs in TODOs are greater than that value. |
Tom Sepez | 8e62858 | 2025-02-14 02:18:55 | [diff] [blame] | 7808 | pattern= input_api.re.compile(r'.*\bTODO\([^\)0-9]*([0-9]+)\).*') |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7809 | problems=[] |
| 7810 | for fin input_api.AffectedSourceFiles(_FilterFile): |
| 7811 | for line_number, linein f.ChangedContents(): |
| 7812 | match= pattern.match(line) |
| 7813 | if matchand int(match.group(1))<=1524553: |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7814 | problems.append(f"{f.LocalPath()}: {line_number}\n {line}") |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7815 | |
| 7816 | if problems: |
| 7817 | return[ |
| 7818 | output_api.PresubmitPromptWarning( |
Alison Gale | cb598de5 | 2024-04-26 17:03:25 | [diff] [blame] | 7819 | 'TODOs should use the new Chromium Issue Tracker IDs which can ' |
| 7820 | 'be found by navigating to the bug. See ' |
Daniel Cheng | 6303eed | 2025-05-03 00:12:33 | [diff] [blame] | 7821 | 'https://crbug.com/336778624 for more details.', problems) |
Alison Gale | d6b25fe | 2024-04-17 13:59:04 | [diff] [blame] | 7822 | ] |
| 7823 | else: |
| 7824 | return[] |
Mikita Kuchyn | e9cd0e29f | 2025-05-26 11:47:25 | [diff] [blame] | 7825 | |
| 7826 | defCheckNoBrowserStarInUnittests(input_api, output_api): |
| 7827 | """Checks that unit-tests don't contain Browser* variables. |
| 7828 | """ |
| 7829 | problems=[] |
| 7830 | |
| 7831 | defFileFilter(affected_file): |
| 7832 | """Check unit-tests only""" |
| 7833 | return input_api.FilterSourceFile( |
| 7834 | affected_file, |
| 7835 | files_to_check=( |
| 7836 | r'.*unittest\.cc$', |
| 7837 | r'.*unittest\.h$' |
| 7838 | ), |
| 7839 | files_to_skip=input_api.DEFAULT_FILES_TO_SKIP, |
| 7840 | ) |
| 7841 | |
| 7842 | browser_star_pattern= input_api.re.compile(r'\bBrowser\s*\*') |
| 7843 | |
| 7844 | for fin input_api.AffectedFiles(include_deletes=False, |
| 7845 | file_filter=FileFilter): |
| 7846 | for line_num, linein f.ChangedContents(): |
| 7847 | match= browser_star_pattern.search(line) |
| 7848 | if match: |
| 7849 | problems.append(' %s:%d:%s'% |
| 7850 | (f.LocalPath(), line_num, match.group(0))) |
| 7851 | |
| 7852 | ifnot problems: |
| 7853 | return[] |
| 7854 | |
| 7855 | WARNING_MSG="""Do not use "Browser*" type in unittest files (e.g., |
| 7856 | "*unittest.cc" or "*unittest.h"). Unit tests should generally |
| 7857 | not depend on the full Browser class or related components. Consider |
| 7858 | refactoring to mock dependencies, use test-specific fakes, |
| 7859 | or determine if a browser_test is more appropriate. |
| 7860 | """ |
| 7861 | return[output_api.PresubmitPromptWarning(WARNING_MSG, items=problems)] |