blob: 63f39ef81e5531dfc9f95d0a0d1c7d2262bd99bb [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#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
dan sinclaird9496f32020-11-16 15:01:27 +000015import re
Corentin Wallez4c351012018-08-27 10:10:28 +020016
Ryan Harrison45aed4b2021-07-20 17:57:20 +000017USE_PYTHON3 = True
18
Ryan Harrisone87ac762022-04-06 15:37:27 -040019NONINCLUSIVE_REGEXES = [
dan sinclaird9496f32020-11-16 15:01:27 +000020 r"(?i)black[-_]?list",
21 r"(?i)white[-_]?list",
22 r"(?i)gr[ea]y[-_]?list",
23 r"(?i)(first class citizen)",
24 r"(?i)black[-_]?hat",
25 r"(?i)white[-_]?hat",
26 r"(?i)gr[ea]y[-_]?hat",
27 r"(?i)master",
28 r"(?i)slave",
29 r"(?i)\bhim\b",
30 r"(?i)\bhis\b",
31 r"(?i)\bshe\b",
32 r"(?i)\bher\b",
33 r"(?i)\bguys\b",
34 r"(?i)\bhers\b",
35 r"(?i)\bman\b",
36 r"(?i)\bwoman\b",
37 r"(?i)\she\s",
38 r"(?i)\she$",
39 r"(?i)^he\s",
40 r"(?i)^he$",
41 r"(?i)\she['|\u2019]d\s",
42 r"(?i)\she['|\u2019]d$",
43 r"(?i)^he['|\u2019]d\s",
44 r"(?i)^he['|\u2019]d$",
45 r"(?i)\she['|\u2019]s\s",
46 r"(?i)\she['|\u2019]s$",
47 r"(?i)^he['|\u2019]s\s",
48 r"(?i)^he['|\u2019]s$",
49 r"(?i)\she['|\u2019]ll\s",
50 r"(?i)\she['|\u2019]ll$",
51 r"(?i)^he['|\u2019]ll\s",
52 r"(?i)^he['|\u2019]ll$",
53 r"(?i)grandfather",
54 r"(?i)\bmitm\b",
55 r"(?i)\bcrazy\b",
56 r"(?i)\binsane\b",
57 r"(?i)\bblind\sto\b",
58 r"(?i)\bflying\sblind\b",
59 r"(?i)\bblind\seye\b",
60 r"(?i)\bcripple\b",
61 r"(?i)\bcrippled\b",
62 r"(?i)\bdumb\b",
63 r"(?i)\bdummy\b",
64 r"(?i)\bparanoid\b",
65 r"(?i)\bsane\b",
66 r"(?i)\bsanity\b",
67 r"(?i)red[-_]?line",
68]
69
Ryan Harrisone87ac762022-04-06 15:37:27 -040070NONINCLUSIVE_REGEX_LIST = []
71for reg in NONINCLUSIVE_REGEXES:
72 NONINCLUSIVE_REGEX_LIST.append(re.compile(reg))
dan sinclaird9496f32020-11-16 15:01:27 +000073
dan sinclairfcf56792022-04-11 13:05:40 +000074LINT_FILTERS = []
dan sinclairc4502a02022-04-08 12:08:56 +000075
Ryan Harrisone87ac762022-04-06 15:37:27 -040076
77def _CheckNonInclusiveLanguage(input_api, output_api, source_file_filter=None):
Ryan Harrison6d27f232021-07-20 17:16:31 +000078 """Checks the files for non-inclusive language."""
dan sinclaird9496f32020-11-16 15:01:27 +000079
Ryan Harrison6d27f232021-07-20 17:16:31 +000080 matches = []
81 for f in input_api.AffectedFiles(include_deletes=False,
82 file_filter=source_file_filter):
dan sinclairfb5a4922022-04-19 22:25:45 +000083 line_num = 0
84 for line in f.NewContents():
85 line_num += 1
Ryan Harrisone87ac762022-04-06 15:37:27 -040086 for reg in NONINCLUSIVE_REGEX_LIST:
Ryan Harrison6d27f232021-07-20 17:16:31 +000087 match = reg.search(line)
88 if match:
89 matches.append(
90 "{} ({}): found non-inclusive language: {}".format(
91 f.LocalPath(), line_num, match.group(0)))
dan sinclaird9496f32020-11-16 15:01:27 +000092
Ryan Harrison6d27f232021-07-20 17:16:31 +000093 if len(matches):
94 return [
dan sinclairfcf56792022-04-11 13:05:40 +000095 output_api.PresubmitPromptWarning("Non-inclusive language found:",
Ryan Harrison6d27f232021-07-20 17:16:31 +000096 items=matches)
97 ]
dan sinclaird9496f32020-11-16 15:01:27 +000098
Ryan Harrison6d27f232021-07-20 17:16:31 +000099 return []
dan sinclaird9496f32020-11-16 15:01:27 +0000100
101
Ryan Harrisone87ac762022-04-06 15:37:27 -0400102def _NonInclusiveFileFilter(file):
103 filter_list = [
dan sinclairfb5a4922022-04-19 22:25:45 +0000104 "Doxyfile", # References to main pages
Ryan Harrisone87ac762022-04-06 15:37:27 -0400105 "PRESUBMIT.py", # Non-inclusive language check data
dan sinclairfb5a4922022-04-19 22:25:45 +0000106 "PRESUBMIT.py.tint", # Non-inclusive language check data
107 "docs/dawn/debug_markers.md", # External URL
108 "docs/dawn/infra.md", # Infra settings
Ryan Harrisone87ac762022-04-06 15:37:27 -0400109 "docs/tint/spirv-input-output-variables.md", # External URL
dan sinclairfb5a4922022-04-19 22:25:45 +0000110 "infra/config/global/generated/cr-buildbucket.cfg", # Infra settings
111 "infra/config/global/main.star", # Infra settings
112 "infra/kokoro/windows/build.bat", # External URL
113 "src/dawn/common/GPUInfo.cpp", # External URL
114 "src/dawn/native/metal/BackendMTL.mm", # OSX Constant
115 "src/dawn/native/vulkan/SamplerVk.cpp", # External URL
116 "src/dawn/native/vulkan/TextureVk.cpp", # External URL
Ben Clayton61b5aac2022-12-12 23:06:43 +0000117 "src/tools/src/cmd/run-cts/main.go", # Terminal type name
dan sinclairfb5a4922022-04-19 22:25:45 +0000118 "src/dawn/samples/ComputeBoids.cpp", # External URL
119 "src/dawn/tests/end2end/DepthBiasTests.cpp", # External URL
dan sinclair46c32d82022-07-20 15:53:14 +0000120 "src/tint/transform/canonicalize_entry_point_io.cc", # External URL
dan sinclairfb5a4922022-04-19 22:25:45 +0000121 "test/tint/samples/compute_boids.wgsl", # External URL
122 "third_party/khronos/KHR/khrplatform.h", # Third party file
123 "tools/roll-all", # Branch name
124 "tools/src/container/key.go", # External URL
dan sinclair41e4d9a2022-05-01 14:40:55 +0000125 "go.sum", # External URL
Ryan Harrisone87ac762022-04-06 15:37:27 -0400126 ]
dan sinclairfb5a4922022-04-19 22:25:45 +0000127 return file.LocalPath() not in filter_list
Ryan Harrisone87ac762022-04-06 15:37:27 -0400128
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000129
Ben Claytonbca96d92022-11-16 21:34:14 +0000130def _CheckNoStaleGen(input_api, output_api):
131 results = []
132 try:
133 go = input_api.os_path.join(input_api.change.RepositoryRoot(), "tools",
134 "golang", "bin", "go")
135 if input_api.is_windows:
136 go += '.exe'
137 input_api.subprocess.check_call_out(
138 [go, "run", "tools/src/cmd/gen/main.go", "--check-stale"],
139 stdout=input_api.subprocess.PIPE,
140 stderr=input_api.subprocess.PIPE,
141 cwd=input_api.change.RepositoryRoot())
142 except input_api.subprocess.CalledProcessError as e:
143 if input_api.is_committing:
144 results.append(output_api.PresubmitError('%s' % (e, )))
145 else:
146 results.append(output_api.PresubmitPromptWarning('%s' % (e, )))
147 return results
148
149
Corentin Wallez4c351012018-08-27 10:10:28 +0200150def _DoCommonChecks(input_api, output_api):
151 results = []
Ben Claytonbca96d92022-11-16 21:34:14 +0000152 results.extend(_CheckNoStaleGen(input_api, output_api))
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000153 results.extend(
154 input_api.canned_checks.CheckChangedLUCIConfigs(input_api, output_api))
dan sinclair17f20472022-11-10 18:23:41 +0000155
156 result_factory = output_api.PresubmitPromptWarning
157 if input_api.is_committing:
158 result_factory = output_api.PresubmitError
159
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000160 results.extend(
dan sinclair17f20472022-11-10 18:23:41 +0000161 input_api.canned_checks.CheckPatchFormatted(
162 input_api,
163 output_api,
164 check_python=True,
165 result_factory=result_factory))
Ryan Harrisone87ac762022-04-06 15:37:27 -0400166 results.extend(
167 input_api.canned_checks.CheckChangeHasDescription(
168 input_api, output_api))
169 results.extend(
170 input_api.canned_checks.CheckGNFormatted(input_api, output_api))
171 results.extend(
172 input_api.canned_checks.CheckChangeHasNoCrAndHasOnlyOneEol(
173 input_api, output_api))
174 results.extend(
175 input_api.canned_checks.CheckChangeHasNoTabs(input_api, output_api))
176 results.extend(
177 input_api.canned_checks.CheckChangeTodoHasOwner(input_api, output_api))
178 results.extend(
179 input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
180 input_api, output_api))
181 results.extend(
182 input_api.canned_checks.CheckDoNotSubmit(input_api, output_api))
dan sinclair2a3d9942022-04-13 16:14:26 +0000183 # Note, the verbose_level here should match what is set in tools/lint so
184 # the same set of lint errors are reported on the CQ and Kokoro bots.
dan sinclairc4502a02022-04-08 12:08:56 +0000185 results.extend(
186 input_api.canned_checks.CheckChangeLintsClean(
dan sinclair2a3d9942022-04-13 16:14:26 +0000187 input_api, output_api, lint_filters=LINT_FILTERS, verbose_level=1))
Ryan Harrisone87ac762022-04-06 15:37:27 -0400188 results.extend(
189 _CheckNonInclusiveLanguage(input_api, output_api,
190 _NonInclusiveFileFilter))
Corentin Wallez4c351012018-08-27 10:10:28 +0200191 return results
Corentin Wallez99612ae2018-08-20 14:54:15 +0200192
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000193
Corentin Wallez99612ae2018-08-20 14:54:15 +0200194def CheckChangeOnUpload(input_api, output_api):
Corentin Wallez4c351012018-08-27 10:10:28 +0200195 return _DoCommonChecks(input_api, output_api)
Corentin Wallez99612ae2018-08-20 14:54:15 +0200196
Kai Ninomiya01aeca22020-07-15 19:51:17 +0000197
Corentin Wallez99612ae2018-08-20 14:54:15 +0200198def CheckChangeOnCommit(input_api, output_api):
Corentin Wallez4c351012018-08-27 10:10:28 +0200199 return _DoCommonChecks(input_api, output_api)