blob: e021ab9797000db4c94e34fe1407b479c7c4ddbb [file] [log] [blame]
Ryan Harrisone87ac762022-04-06 15:37:27 -04001# Copyright 2022 The Dawn & Tint Authors
Corentin Wallez4c351012018-08-27 10:10:28 +02002#
Austin Engcc2516a2023-10-17 20:57:54 +00003# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are met:
Corentin Wallez4c351012018-08-27 10:10:28 +02005#
Austin Engcc2516a2023-10-17 20:57:54 +00006# 1. Redistributions of source code must retain the above copyright notice, this
7# list of conditions and the following disclaimer.
Corentin Wallez4c351012018-08-27 10:10:28 +02008#
Austin Engcc2516a2023-10-17 20:57:54 +00009# 2. Redistributions in binary form must reproduce the above copyright notice,
10# this list of conditions and the following disclaimer in the documentation
11# and/or other materials provided with the distribution.
12#
13# 3. Neither the name of the copyright holder nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Corentin Wallez4c351012018-08-27 10:10:28 +020027
dan sinclaird9496f32020-11-16 15:01:27 +000028import re
Corentin Wallez4c351012018-08-27 10:10:28 +020029
Ryan Harrison45aed4b2021-07-20 17:57:20 +000030USE_PYTHON3 = True
31
Ryan Harrisone87ac762022-04-06 15:37:27 -040032NONINCLUSIVE_REGEXES = [
dan sinclaird9496f32020-11-16 15:01:27 +000033 r"(?i)black[-_]?list",
34 r"(?i)white[-_]?list",
35 r"(?i)gr[ea]y[-_]?list",
36 r"(?i)(first class citizen)",
37 r"(?i)black[-_]?hat",
38 r"(?i)white[-_]?hat",
39 r"(?i)gr[ea]y[-_]?hat",
40 r"(?i)master",
41 r"(?i)slave",
42 r"(?i)\bhim\b",
43 r"(?i)\bhis\b",
44 r"(?i)\bshe\b",
45 r"(?i)\bher\b",
46 r"(?i)\bguys\b",
47 r"(?i)\bhers\b",
48 r"(?i)\bman\b",
49 r"(?i)\bwoman\b",
50 r"(?i)\she\s",
51 r"(?i)\she$",
52 r"(?i)^he\s",
53 r"(?i)^he$",
54 r"(?i)\she['|\u2019]d\s",
55 r"(?i)\she['|\u2019]d$",
56 r"(?i)^he['|\u2019]d\s",
57 r"(?i)^he['|\u2019]d$",
58 r"(?i)\she['|\u2019]s\s",
59 r"(?i)\she['|\u2019]s$",
60 r"(?i)^he['|\u2019]s\s",
61 r"(?i)^he['|\u2019]s$",
62 r"(?i)\she['|\u2019]ll\s",
63 r"(?i)\she['|\u2019]ll$",
64 r"(?i)^he['|\u2019]ll\s",
65 r"(?i)^he['|\u2019]ll$",
66 r"(?i)grandfather",
67 r"(?i)\bmitm\b",
68 r"(?i)\bcrazy\b",
69 r"(?i)\binsane\b",
70 r"(?i)\bblind\sto\b",
71 r"(?i)\bflying\sblind\b",
72 r"(?i)\bblind\seye\b",
73 r"(?i)\bcripple\b",
74 r"(?i)\bcrippled\b",
75 r"(?i)\bdumb\b",
76 r"(?i)\bdummy\b",
77 r"(?i)\bparanoid\b",
78 r"(?i)\bsane\b",
79 r"(?i)\bsanity\b",
80 r"(?i)red[-_]?line",
81]
82
Ryan Harrisone87ac762022-04-06 15:37:27 -040083NONINCLUSIVE_REGEX_LIST = []
84for reg in NONINCLUSIVE_REGEXES:
85 NONINCLUSIVE_REGEX_LIST.append(re.compile(reg))
dan sinclaird9496f32020-11-16 15:01:27 +000086
dan sinclairfcf56792022-04-11 13:05:40 +000087LINT_FILTERS = []
dan sinclairc4502a02022-04-08 12:08:56 +000088
Ryan Harrisone87ac762022-04-06 15:37:27 -040089
90def _CheckNonInclusiveLanguage(input_api, output_api, source_file_filter=None):
Ryan Harrison6d27f232021-07-20 17:16:31 +000091 """Checks the files for non-inclusive language."""
dan sinclaird9496f32020-11-16 15:01:27 +000092
Ryan Harrison6d27f232021-07-20 17:16:31 +000093 matches = []
94 for f in input_api.AffectedFiles(include_deletes=False,
95 file_filter=source_file_filter):
dan sinclairfb5a4922022-04-19 22:25:45 +000096 line_num = 0
97 for line in f.NewContents():
98 line_num += 1
Ryan Harrisone87ac762022-04-06 15:37:27 -040099 for reg in NONINCLUSIVE_REGEX_LIST:
Ryan Harrison6d27f232021-07-20 17:16:31 +0000100 match = reg.search(line)
101 if match:
102 matches.append(
103 "{} ({}): found non-inclusive language: {}".format(
104 f.LocalPath(), line_num, match.group(0)))
dan sinclaird9496f32020-11-16 15:01:27 +0000105
Ryan Harrison6d27f232021-07-20 17:16:31 +0000106 if len(matches):
107 return [
dan sinclairfcf56792022-04-11 13:05:40 +0000108 output_api.PresubmitPromptWarning("Non-inclusive language found:",
Ryan Harrison6d27f232021-07-20 17:16:31 +0000109 items=matches)
110 ]
dan sinclaird9496f32020-11-16 15:01:27 +0000111
Ryan Harrison6d27f232021-07-20 17:16:31 +0000112 return []
dan sinclaird9496f32020-11-16 15:01:27 +0000113
114
Ryan Harrisone87ac762022-04-06 15:37:27 -0400115def _NonInclusiveFileFilter(file):
116 filter_list = [
dan sinclairfb5a4922022-04-19 22:25:45 +0000117 "Doxyfile", # References to main pages
Ryan Harrisone87ac762022-04-06 15:37:27 -0400118 "PRESUBMIT.py", # Non-inclusive language check data
dan sinclairfb5a4922022-04-19 22:25:45 +0000119 "PRESUBMIT.py.tint", # Non-inclusive language check data
120 "docs/dawn/debug_markers.md", # External URL
121 "docs/dawn/infra.md", # Infra settings
Ryan Harrisone87ac762022-04-06 15:37:27 -0400122 "docs/tint/spirv-input-output-variables.md", # External URL
dan sinclairfb5a4922022-04-19 22:25:45 +0000123 "infra/config/global/generated/cr-buildbucket.cfg", # Infra settings
124 "infra/config/global/main.star", # Infra settings
125 "infra/kokoro/windows/build.bat", # External URL
126 "src/dawn/common/GPUInfo.cpp", # External URL
127 "src/dawn/native/metal/BackendMTL.mm", # OSX Constant
128 "src/dawn/native/vulkan/SamplerVk.cpp", # External URL
129 "src/dawn/native/vulkan/TextureVk.cpp", # External URL
Ben Clayton61b5aac2022-12-12 23:06:43 +0000130 "src/tools/src/cmd/run-cts/main.go", # Terminal type name
dan sinclairfb5a4922022-04-19 22:25:45 +0000131 "src/dawn/samples/ComputeBoids.cpp", # External URL
132 "src/dawn/tests/end2end/DepthBiasTests.cpp", # External URL
dan sinclair46c32d82022-07-20 15:53:14 +0000133 "src/tint/transform/canonicalize_entry_point_io.cc", # External URL
dan sinclairfb5a4922022-04-19 22:25:45 +0000134 "test/tint/samples/compute_boids.wgsl", # External URL
Antonio Maioranob94856d2023-06-22 21:30:50 +0000135 "third_party/gn/dxc/BUILD.gn", # Third party file
Antonio Maiorano83bdc7f2023-08-21 15:29:35 +0000136 "third_party/khronos/EGL-Registry/api/KHR/khrplatform.h", # Third party file
dan sinclairfb5a4922022-04-19 22:25:45 +0000137 "tools/roll-all", # Branch name
138 "tools/src/container/key.go", # External URL
dan sinclair41e4d9a2022-05-01 14:40:55 +0000139 "go.sum", # External URL
Ryan Harrisone87ac762022-04-06 15:37:27 -0400140 ]
Antonio Maiorano5e164162023-06-20 17:12:37 +0000141 return file.LocalPath().replace('\\', '/') not in filter_list
Ryan Harrisone87ac762022-04-06 15:37:27 -0400142
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000143
Ben Claytonbca96d92022-11-16 21:34:14 +0000144def _CheckNoStaleGen(input_api, output_api):
145 results = []
146 try:
147 go = input_api.os_path.join(input_api.change.RepositoryRoot(), "tools",
148 "golang", "bin", "go")
149 if input_api.is_windows:
150 go += '.exe'
151 input_api.subprocess.check_call_out(
152 [go, "run", "tools/src/cmd/gen/main.go", "--check-stale"],
153 stdout=input_api.subprocess.PIPE,
154 stderr=input_api.subprocess.PIPE,
155 cwd=input_api.change.RepositoryRoot())
156 except input_api.subprocess.CalledProcessError as e:
157 if input_api.is_committing:
158 results.append(output_api.PresubmitError('%s' % (e, )))
159 else:
160 results.append(output_api.PresubmitPromptWarning('%s' % (e, )))
161 return results
162
163
Corentin Wallez4c351012018-08-27 10:10:28 +0200164def _DoCommonChecks(input_api, output_api):
165 results = []
Joanna Wang77ce5592023-08-04 22:26:55 +0000166 results.extend(
167 input_api.canned_checks.CheckForCommitObjects(input_api, output_api))
Ben Claytonbca96d92022-11-16 21:34:14 +0000168 results.extend(_CheckNoStaleGen(input_api, output_api))
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000169 results.extend(
170 input_api.canned_checks.CheckChangedLUCIConfigs(input_api, output_api))
dan sinclair17f20472022-11-10 18:23:41 +0000171
172 result_factory = output_api.PresubmitPromptWarning
173 if input_api.is_committing:
174 result_factory = output_api.PresubmitError
175
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000176 results.extend(
dan sinclair17f20472022-11-10 18:23:41 +0000177 input_api.canned_checks.CheckPatchFormatted(
178 input_api,
179 output_api,
180 check_python=True,
181 result_factory=result_factory))
Ryan Harrisone87ac762022-04-06 15:37:27 -0400182 results.extend(
183 input_api.canned_checks.CheckChangeHasDescription(
184 input_api, output_api))
185 results.extend(
186 input_api.canned_checks.CheckGNFormatted(input_api, output_api))
187 results.extend(
188 input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(
189 input_api, output_api))
190 results.extend(
191 input_api.canned_checks.CheckChangeHasNoTabs(input_api, output_api))
192 results.extend(
193 input_api.canned_checks.CheckChangeTodoHasOwner(input_api, output_api))
194 results.extend(
195 input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
196 input_api, output_api))
197 results.extend(
198 input_api.canned_checks.CheckDoNotSubmit(input_api, output_api))
dan sinclair2a3d9942022-04-13 16:14:26 +0000199 # Note, the verbose_level here should match what is set in tools/lint so
200 # the same set of lint errors are reported on the CQ and Kokoro bots.
dan sinclairc4502a02022-04-08 12:08:56 +0000201 results.extend(
202 input_api.canned_checks.CheckChangeLintsClean(
dan sinclair2a3d9942022-04-13 16:14:26 +0000203 input_api, output_api, lint_filters=LINT_FILTERS, verbose_level=1))
Ryan Harrisone87ac762022-04-06 15:37:27 -0400204 results.extend(
205 _CheckNonInclusiveLanguage(input_api, output_api,
206 _NonInclusiveFileFilter))
Corentin Wallez4c351012018-08-27 10:10:28 +0200207 return results
Corentin Wallez99612ae2018-08-20 14:54:15 +0200208
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000209
Corentin Wallez99612ae2018-08-20 14:54:15 +0200210def CheckChangeOnUpload(input_api, output_api):
Corentin Wallez4c351012018-08-27 10:10:28 +0200211 return _DoCommonChecks(input_api, output_api)
Corentin Wallez99612ae2018-08-20 14:54:15 +0200212
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000213
Corentin Wallez99612ae2018-08-20 14:54:15 +0200214def CheckChangeOnCommit(input_api, output_api):
Corentin Wallez4c351012018-08-27 10:10:28 +0200215 return _DoCommonChecks(input_api, output_api)