mirrored from https://chromium.googlesource.com/angle/angle
-
Notifications
You must be signed in to change notification settings - Fork 759
Expand file tree
/
Copy pathPRESUBMIT.py
More file actions
941 lines (783 loc) · 36.7 KB
/
Copy pathPRESUBMIT.py
File metadata and controls
941 lines (783 loc) · 36.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
# Copyright 2019 The ANGLE Project Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Top-level presubmit script for code generation.
See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details on the presubmit API built into depot_tools.
"""
import dataclasses
from typing import Optional
from typing import Sequence
from typing import Tuple
import itertools
import os
import re
import shutil
import subprocess
import sys
import tempfile
import textwrap
import pathlib
# This line is 'magic' in that git-cl looks for it to decide whether to
# use Python3 instead of Python2 when running the code in this file.
USE_PYTHON3 = True
# Fragment of a regular expression that matches C/C++ and Objective-C++ implementation files and headers.
_IMPLEMENTATION_AND_HEADER_EXTENSIONS = r'\.(c|cc|cpp|cxx|mm|h|hpp|hxx)$'
# Fragment of a regular expression that matches C++ and Objective-C++ header files.
_HEADER_EXTENSIONS = r'\.(h|hpp|hxx)$'
# Copied from Chrome's BanRule.
@dataclasses.dataclass
class BanRule:
# String pattern. If the pattern begins with a slash, the pattern will be
# treated as a regular expression instead.
pattern: str
# Explanation as a sequence of strings. Each string in the sequence will be
# printed on its own line.
explanation: Tuple[str, ...]
# Whether or not to treat this ban as a fatal error.
treat_as_error: bool = False
# Paths that should be excluded from the ban check. Each string is a regular
# expression that will be matched against the path of the file being checked
# relative to the root of the source tree.
excluded_paths: Optional[Sequence[str]] = None
# If True, surfaces any violation as a Gerrit comment on the CL after
# running the CQ.
surface_as_gerrit_lint: Optional[bool] = None
# Configuration for banned patterns checks.
_BANNED_CPP_PATTERNS: Sequence[BanRule] = (
BanRule(
pattern=r'/\bANGLE_UNSAFE_TODO\b',
explanation=(
'Do not introduce new instances of ANGLE_UNSAFE_TODO. ',
'Use ANGLE_UNSAFE_BUFFERS with a // SAFETY: comment instead, ',
'or rewrite to be safe.',
),
treat_as_error=False,
surface_as_gerrit_lint=True,
),
BanRule(
pattern=r'/#pragma\s+allow_unsafe_buffers\b',
explanation=(
'#pragma allow_unsafe_buffers is discouraged. Prefer using ',
'ANGLE_UNSAFE_BUFFERS with a // SAFETY: comment for ',
'specific blocks, or rewrite to be safe.',
),
treat_as_error=False,
surface_as_gerrit_lint=True,
),
)
_PRIMARY_EXPORT_TARGETS = [
'//:libEGL',
'//:libGLESv1_CM',
'//:libGLESv2',
'//:translator',
]
def _SplitIntoMultipleCommits(description_text):
paragraph_split_pattern = r"(?m)(^\s*$\n)"
multiple_paragraphs = re.split(paragraph_split_pattern, description_text)
multiple_commits = [""]
change_id_pattern = re.compile(r"(?m)^Change-Id: [a-zA-Z0-9]*$")
for paragraph in multiple_paragraphs:
multiple_commits[-1] += paragraph
if change_id_pattern.search(paragraph):
multiple_commits.append("")
if multiple_commits[-1] == "":
multiple_commits.pop()
return multiple_commits
def _CheckCommitMessageFormatting(input_api, output_api):
def _IsLineBlank(line):
return line.isspace() or line == ""
def _PopBlankLines(lines, reverse=False):
if reverse:
while len(lines) > 0 and _IsLineBlank(lines[-1]):
lines.pop()
else:
while len(lines) > 0 and _IsLineBlank(lines[0]):
lines.pop(0)
def _IsTagLine(line):
return ":" in line
def _CheckTabInCommit(lines):
return all([line.find("\t") == -1 for line in lines])
allowlist_strings = ['Revert', 'Roll', 'Manual roll', 'Reland', 'Re-land']
summary_linelength_warning_lower_limit = 65
summary_linelength_warning_upper_limit = 70
description_linelength_limit = 72
git_output = input_api.change.DescriptionText()
multiple_commits = _SplitIntoMultipleCommits(git_output)
errors = []
for k in range(len(multiple_commits)):
commit_msg_lines = multiple_commits[k].splitlines()
commit_number = len(multiple_commits) - k
commit_tag = "Commit " + str(commit_number) + ":"
commit_msg_line_numbers = {}
for i in range(len(commit_msg_lines)):
commit_msg_line_numbers[commit_msg_lines[i]] = i + 1
_PopBlankLines(commit_msg_lines, True)
_PopBlankLines(commit_msg_lines, False)
allowlisted = False
if len(commit_msg_lines) > 0:
for allowlist_string in allowlist_strings:
if commit_msg_lines[0].startswith(allowlist_string):
allowlisted = True
break
if allowlisted:
continue
if not _CheckTabInCommit(commit_msg_lines):
errors.append(
output_api.PresubmitError(commit_tag + "Tabs are not allowed in commit message."))
# the tags paragraph is at the end of the message
# the break between the tags paragraph is the first line without ":"
# this is sufficient because if a line is blank, it will not have ":"
last_paragraph_line_count = 0
while len(commit_msg_lines) > 0 and _IsTagLine(commit_msg_lines[-1]):
last_paragraph_line_count += 1
commit_msg_lines.pop()
if last_paragraph_line_count == 0:
errors.append(
output_api.PresubmitError(
commit_tag +
"Please ensure that there are tags (e.g., Bug:, Test:) in your description."))
if len(commit_msg_lines) > 0:
if not _IsLineBlank(commit_msg_lines[-1]):
output_api.PresubmitError(commit_tag +
"Please ensure that there exists 1 blank line " +
"between tags and description body.")
else:
# pop the blank line between tag paragraph and description body
commit_msg_lines.pop()
if len(commit_msg_lines) > 0 and _IsLineBlank(commit_msg_lines[-1]):
errors.append(
output_api.PresubmitError(
commit_tag + 'Please ensure that there exists only 1 blank line '
'between tags and description body.'))
# pop all the remaining blank lines between tag and description body
_PopBlankLines(commit_msg_lines, True)
if len(commit_msg_lines) == 0:
errors.append(
output_api.PresubmitError(commit_tag +
'Please ensure that your description summary'
' and description body are not blank.'))
continue
if summary_linelength_warning_lower_limit <= len(commit_msg_lines[0]) \
<= summary_linelength_warning_upper_limit:
errors.append(
output_api.PresubmitPromptWarning(
commit_tag + "Your description summary should be on one line of " +
str(summary_linelength_warning_lower_limit - 1) + " or less characters."))
elif len(commit_msg_lines[0]) > summary_linelength_warning_upper_limit:
errors.append(
output_api.PresubmitError(
commit_tag + "Please ensure that your description summary is on one line of " +
str(summary_linelength_warning_lower_limit - 1) + " or less characters."))
commit_msg_lines.pop(0) # get rid of description summary
if len(commit_msg_lines) == 0:
continue
if not _IsLineBlank(commit_msg_lines[0]):
errors.append(
output_api.PresubmitError(commit_tag +
'Please ensure the summary is only 1 line and '
'there is 1 blank line between the summary '
'and description body.'))
else:
commit_msg_lines.pop(0) # pop first blank line
if len(commit_msg_lines) == 0:
continue
if _IsLineBlank(commit_msg_lines[0]):
errors.append(
output_api.PresubmitError(commit_tag +
'Please ensure that there exists only 1 blank line '
'between description summary and description body.'))
# pop all the remaining blank lines between
# description summary and description body
_PopBlankLines(commit_msg_lines)
# loop through description body
while len(commit_msg_lines) > 0:
line = commit_msg_lines.pop(0)
# lines starting with 4 spaces, quotes or lines without space(urls)
# are exempt from length check
if line.startswith(" ") or line.startswith("> ") or " " not in line:
continue
if len(line) > description_linelength_limit:
errors.append(
output_api.PresubmitError(
commit_tag + 'Line ' + str(commit_msg_line_numbers[line]) +
' is too long.\n' + '"' + line + '"\n' + 'Please wrap it to ' +
str(description_linelength_limit) + ' characters. ' +
"Lines without spaces or lines starting with 4 spaces are exempt."))
break
return errors
def _CheckChangeHasBugField(input_api, output_api):
"""Requires that the changelist have a Bug: field from a known project."""
bugs = input_api.change.BugsFromDescription()
# The bug must be in the form of "project:number". None is also accepted, which is used by
# rollers as well as in very minor changes.
if len(bugs) == 1 and bugs[0] == 'None':
return []
projects = [
'angleproject:', 'chromium:', 'dawn:', 'fuchsia:', 'skia:', 'swiftshader:', 'tint:', 'b/'
]
bug_regex = re.compile(r"([a-z]+[:/])(\d+)")
errors = []
extra_help = False
if not bugs:
errors.append('Please ensure that your description contains\n'
'Bug: bugtag\n'
'directly above the Change-Id tag (no empty line in-between)')
extra_help = True
for bug in bugs:
if bug == 'None':
errors.append('Invalid bug tag "None" in presence of other bug tags.')
continue
match = re.match(bug_regex, bug)
if match == None or bug != match.group(0) or match.group(1) not in projects:
errors.append('Incorrect bug tag "' + bug + '".')
extra_help = True
if extra_help:
change_ids = re.findall('^Change-Id:', input_api.change.FullDescriptionText(), re.M)
if len(change_ids) > 1:
errors.append('Note: multiple Change-Id tags found in description')
errors.append('''Acceptable bugtags:
project:bugnumber - where project is one of ({projects})
b/bugnumber - for Buganizer/IssueTracker bugs
'''.format(projects=', '.join(p[:-1] for p in projects if p != 'b/')))
return [output_api.PresubmitError('\n\n'.join(errors))] if errors else []
def _CheckCodeGeneration(input_api, output_api):
class Msg(output_api.PresubmitError):
"""Specialized error message"""
def __init__(self, message, **kwargs):
super(output_api.PresubmitError, self).__init__(
message,
long_text='Please ensure your ANGLE repositiory is synced to tip-of-tree\n'
'and all ANGLE DEPS are fully up-to-date by running gclient sync.\n'
'\n'
'If that fails, run scripts/run_code_generation.py to refresh generated hashes.\n'
'\n'
'If you are building ANGLE inside Chromium you must bootstrap ANGLE\n'
'before gclient sync. See the DevSetup documentation for more details.\n',
**kwargs)
code_gen_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
'scripts/run_code_generation.py')
cmd_name = 'run_code_generation'
cmd = [input_api.python3_executable, code_gen_path, '--verify-no-dirty']
test_cmd = input_api.Command(name=cmd_name, cmd=cmd, kwargs={}, message=Msg)
if input_api.verbose:
print('Running ' + cmd_name)
return input_api.RunTests([test_cmd])
# Taken directly from Chromium's PRESUBMIT.py
def _CheckNewHeaderWithoutGnChange(input_api, output_api):
"""Checks that newly added header files have corresponding GN changes.
Note that this is only a heuristic. To be precise, run script:
build/check_gn_headers.py.
"""
def headers(f):
return input_api.FilterSourceFile(f, files_to_check=(r'.+%s' % _HEADER_EXTENSIONS,))
new_headers = []
for f in input_api.AffectedSourceFiles(headers):
if f.Action() != 'A':
continue
new_headers.append(f.LocalPath())
def gn_files(f):
return input_api.FilterSourceFile(f, files_to_check=(r'.+\.gn',))
all_gn_changed_contents = ''
for f in input_api.AffectedSourceFiles(gn_files):
for _, line in f.ChangedContents():
all_gn_changed_contents += line
problems = []
for header in new_headers:
basename = input_api.os_path.basename(header)
if basename not in all_gn_changed_contents:
problems.append(header)
if problems:
return [
output_api.PresubmitPromptWarning(
'Missing GN changes for new header files',
items=sorted(problems),
long_text='Please double check whether newly added header files need '
'corresponding changes in gn or gni files.\nThis checking is only a '
'heuristic. Run build/check_gn_headers.py to be precise.\n'
'Read https://crbug.com/661774 for more info.')
]
return []
def _CheckExportValidity(input_api, output_api):
outdir = tempfile.mkdtemp()
# shell=True is necessary on Windows, as otherwise subprocess fails to find
# either 'gn' or 'vpython3' even if they are findable via PATH.
use_shell = input_api.is_windows
try:
try:
subprocess.check_output(
[sys.executable, 'third_party/depot_tools/gn.py', 'gen', outdir], shell=use_shell)
except subprocess.CalledProcessError as e:
return [
output_api.PresubmitError('Unable to run gn gen for export_targets.py: %s' %
e.output.decode())
]
export_target_script = os.path.join(input_api.PresubmitLocalPath(), 'scripts',
'export_targets.py')
try:
subprocess.check_output(
['vpython3', export_target_script, outdir] + _PRIMARY_EXPORT_TARGETS,
stderr=subprocess.STDOUT,
shell=use_shell)
except subprocess.CalledProcessError as e:
if input_api.is_committing:
return [
output_api.PresubmitError('export_targets.py failed: %s' % e.output.decode())
]
return [
output_api.PresubmitPromptWarning(
'export_targets.py failed, this may just be due to your local checkout: %s' %
e.output.decode())
]
return []
finally:
shutil.rmtree(outdir)
def _CheckTabsInSourceFiles(input_api, output_api):
"""Forbids tab characters in source files due to a WebKit repo requirement."""
def implementation_and_headers_including_third_party(f):
# Check third_party files too, because WebKit's checks don't make exceptions.
return input_api.FilterSourceFile(
f,
files_to_check=(r'.+%s' % _IMPLEMENTATION_AND_HEADER_EXTENSIONS,),
files_to_skip=[f for f in input_api.DEFAULT_FILES_TO_SKIP if not "third_party" in f])
files_with_tabs = []
for f in input_api.AffectedSourceFiles(implementation_and_headers_including_third_party):
for (num, line) in f.ChangedContents():
if '\t' in line:
files_with_tabs.append(f)
break
if files_with_tabs:
return [
output_api.PresubmitError(
'Tab characters in source files.',
items=sorted(files_with_tabs),
long_text=
'Tab characters are forbidden in ANGLE source files because WebKit\'s Subversion\n'
'repository does not allow tab characters in source files.\n'
'Please remove tab characters from these files.')
]
return []
# https://stackoverflow.com/a/196392
def is_ascii(s):
return all(ord(c) < 128 for c in s)
def _CheckNonAsciiInSourceFiles(input_api, output_api):
"""Forbids non-ascii characters in source files."""
def implementation_and_headers(f):
return input_api.FilterSourceFile(
f, files_to_check=(r'.+%s' % _IMPLEMENTATION_AND_HEADER_EXTENSIONS,))
files_with_non_ascii = []
for f in input_api.AffectedSourceFiles(implementation_and_headers):
for (num, line) in f.ChangedContents():
if not is_ascii(line):
files_with_non_ascii.append("%s: %s" % (f, line))
break
if files_with_non_ascii:
return [
output_api.PresubmitError(
'Non-ASCII characters in source files.',
items=sorted(files_with_non_ascii),
long_text='Non-ASCII characters are forbidden in ANGLE source files.\n'
'Please remove non-ASCII characters from these files.')
]
return []
def _CheckCommentBeforeTestInTestFiles(input_api, output_api):
"""Require a comment before TEST_P() and other tests."""
def test_files(f):
return input_api.FilterSourceFile(
f, files_to_check=(r'^src/tests/.+\.cpp$', r'^src/.+_unittest\.cpp$'))
tests_with_no_comment = []
for f in input_api.AffectedSourceFiles(test_files):
diff = f.GenerateScmDiff()
last_line_was_comment = False
for line in diff.splitlines():
# Skip removed lines
if line.startswith('-'):
continue
# Note: we don't always get the context of the diff, so if a test already has a comment
# but is only renamed, the diff looks like:
#
# @@ <line info>
# -TEST_P(OLD, NAME)
# +TEST_P(NEW, NAME)
#
# Treat @@ as if it was a comment in that case, assuming the test already had a comment
# previously.
new_line_is_comment = (
line.startswith(' //') or line.startswith('+//') or line.startswith('@@'))
new_line_is_test_declaration = (
line.startswith('+TEST_P(') or line.startswith('+TEST(') or
line.startswith('+TYPED_TEST('))
if new_line_is_test_declaration and not last_line_was_comment:
tests_with_no_comment.append(line[1:])
last_line_was_comment = new_line_is_comment
if tests_with_no_comment:
return [
output_api.PresubmitError(
'Tests without comment.',
items=sorted(tests_with_no_comment),
long_text='ANGLE requires a comment describing what a test does.')
]
return []
def _CheckWildcardInTestExpectationFiles(input_api, output_api):
"""Require wildcard as API tag (i.e. in foo.bar/*) in expectations when no additional feature is
enabled."""
def expectation_files(f):
return input_api.FilterSourceFile(
f, files_to_check=[r'^src/tests/angle_end2end_tests_expectations.txt$'])
expectation_pattern = re.compile(r'^.*:\s*[a-zA-Z0-9._*]+\/([^ ]*)\s*=.*$')
expectations_without_wildcard = []
for f in input_api.AffectedSourceFiles(expectation_files):
diff = f.GenerateScmDiff()
for line in diff.splitlines():
# Only look at new lines
if not line.startswith('+'):
continue
match = re.match(expectation_pattern, line[1:].strip())
if match is None:
continue
tag = match.group(1)
# The tag is in the following general form:
#
# FRONTENDAPI_BACKENDAPI[_FEATURE]*
#
# Any part of the above may be a wildcard. Warn about usage of FRONTEND_BACKENDAPI as
# the tag. Instead, the backend should be specified before the : and `*` used as the
# tag. If any additional tags are present, it's a specific expectation that should
# remain specific (and not wildcarded). NoFixture is an exception as X_Y_NoFixture is
# the generic form of the tags of tests that don't use the fixture.
sections = [section for section in tag.split('_') if section != 'NoFixture']
# Allow '*_...', or 'FRONTENDAPI_*_...'.
if '*' in sections[0] or (len(sections) > 1 and '*' in sections[1]):
continue
# Warn if no additional tags are present
if len(sections) == 2:
expectations_without_wildcard.append(line[1:])
if expectations_without_wildcard:
return [
output_api.PresubmitError(
'Use wildcard in API tags (after /) in angle_end2end_tests_expectations.txt.',
items=expectations_without_wildcard,
long_text="""ANGLE prefers end2end expections to use the following form:
1234 MAC OPENGL : Foo.Bar/* = SKIP
instead of:
1234 MAC OPENGL : Foo.Bar/ES2_OpenGL = SKIP
1234 MAC OPENGL : Foo.Bar/ES3_OpenGL = SKIP
Expectatations that are specific (such as Foo.Bar/ES2_OpenGL_SomeFeature) are allowed.""")
]
return []
def _CheckShaderVersionInShaderLangHeader(input_api, output_api):
"""Requires an update to ANGLE_SH_VERSION when ShaderLang.h or ShaderVars.h change."""
def headers(f):
return input_api.FilterSourceFile(
f,
files_to_check=(r'^include/GLSLANG/ShaderLang.h$', r'^include/GLSLANG/ShaderVars.h$'))
headers_changed = input_api.AffectedSourceFiles(headers)
if len(headers_changed) == 0:
return []
# Skip this check for reverts and rolls. Unlike
# _CheckCommitMessageFormatting, relands are still checked because the
# original change might have incremented the version correctly, but the
# rebase over a new version could accidentally remove that (because another
# change in the meantime identically incremented it).
git_output = input_api.change.DescriptionText()
multiple_commits = _SplitIntoMultipleCommits(git_output)
for commit in multiple_commits:
if commit.startswith('Revert') or commit.startswith('Roll'):
return []
diffs = '\n'.join(f.GenerateScmDiff() for f in headers_changed)
versions = dict(re.findall(r'^([-+])#define ANGLE_SH_VERSION\s+(\d+)', diffs, re.M))
if len(versions) != 2 or int(versions['+']) <= int(versions['-']):
return [
output_api.PresubmitError(
'ANGLE_SH_VERSION should be incremented when ShaderLang.h or ShaderVars.h change.',
)
]
return []
def _CheckGClientExists(input_api, output_api, search_limit=None):
presubmit_path = pathlib.Path(input_api.PresubmitLocalPath())
for current_path in itertools.chain([presubmit_path], presubmit_path.parents):
gclient_path = current_path.joinpath('.gclient')
if gclient_path.exists() and gclient_path.is_file():
return []
# search_limit parameter is used in unit tests to prevent searching all the way to root
# directory for reproducibility.
elif search_limit != None and current_path == search_limit:
break
return [
output_api.PresubmitError(
'Missing .gclient file.',
long_text=textwrap.fill(
width=100,
text='The top level directory of the repository must contain a .gclient file.'
' You can follow the steps outlined in the link below to get set up for ANGLE'
' development:') +
'\n\nhttps://chromium.googlesource.com/angle/angle/+/refs/heads/main/doc/DevSetup.md')
]
def _CheckRestrictedTraces(input_api, output_api):
import json
json_path = 'src/tests/restricted_traces/restricted_traces.json'
trace_file = None
for f in input_api.AffectedFiles():
if f.LocalPath() == json_path:
trace_file = f
break
# If the traces JSON file was not modified in this CL, skip the check.
if not trace_file:
return []
abs_path = input_api.os_path.join(input_api.PresubmitLocalPath(), json_path)
try:
with open(abs_path, 'r') as f:
json_data = json.load(f)
except Exception as e:
return [output_api.PresubmitError(f'Failed to parse {json_path}: {e}')]
if 'traces' not in json_data:
return [output_api.PresubmitError(f'{json_path} is missing the "traces" key.')]
old_traces = []
old_contents = trace_file.OldContents()
if old_contents:
try:
old_data = json.loads('\n'.join(old_contents))
old_traces = [t.split(' ')[0] for t in old_data.get('traces', [])]
except Exception as e:
return [output_api.PresubmitError(f'Failed to parse old version of {json_path}: {e}')]
raw_trace_parts = [trace.split(' ') for trace in json_data['traces']]
cq_extra_traces = [
p[0] for p in raw_trace_parts if 'ci' not in p[2:] and 'representative' not in p[2:]
]
TAG_ORDER = ['ci', 'representative', 'smoke']
def get_sort_key(tag):
if tag in TAG_ORDER:
return (0, TAG_ORDER.index(tag))
else:
return (1, tag)
for p in raw_trace_parts:
name = p[0]
tags = p[2:]
if name not in old_traces and tags:
return [
output_api.PresubmitError(
f'New trace "{name}" has tags: {tags}. '
f'New traces must not have any tags initially so they are tested on CQ.')
]
sorted_tags = sorted(tags, key=get_sort_key)
if tags != sorted_tags:
return [
output_api.PresubmitError(
f'Trace "{name}" has unsorted tags: {tags}. '
f'Expected order: {sorted_tags} (broadest first: ci, representative, smoke).')
]
LIMIT_N = 10
if len(cq_extra_traces) > LIMIT_N:
return [
output_api.PresubmitError(
f'Too many CQ extra traces ({len(cq_extra_traces)}). Limit is {LIMIT_N}.\n'
'Please move some traces to conditional checkout by adding the "ci" tag.')
]
return []
def _CheckUnwrappedVulkanCalls(input_api, output_api):
"""Runs find_unwrapped_vk_calls.py to detect unwrapped calls."""
vulkan_dir = input_api.os_path.join('src', 'libANGLE', 'renderer', 'vulkan')
vulkan_dir_re = vulkan_dir.replace('\\', '/')
results = []
# Only run if Vulkan renderer files are affected
def vulkan_source_files(f):
return input_api.FilterSourceFile(f, files_to_check=[rf'^{vulkan_dir_re}/.*\.(h|cpp|mm)$'])
if not input_api.AffectedSourceFiles(vulkan_source_files):
return results
# First, run tests if the script or test files are changed
def find_unwrapped_vk_calls_files(f):
return input_api.FilterSourceFile(
f,
files_to_check=[
rf'^{vulkan_dir_re}/find_unwrapped_vk_calls_test/.*$',
rf'^{vulkan_dir_re}/find_unwrapped_vk_calls\.py$'
])
if input_api.AffectedSourceFiles(find_unwrapped_vk_calls_files):
cmd_name = 'find_unwrapped_vk_calls TESTS'
cmd = [
input_api.python3_executable,
input_api.os_path.join(input_api.PresubmitLocalPath(), vulkan_dir,
'find_unwrapped_vk_calls_test', 'run_tests.py')
]
test_cmd = input_api.Command(
name=cmd_name, cmd=cmd, kwargs={}, message=output_api.PresubmitError)
if input_api.verbose:
print('Running ' + cmd_name)
results.extend(input_api.RunTests([test_cmd]))
# Do not run the script if tests fail
for result in results:
if isinstance(result, output_api.PresubmitError):
return results
# Finally, run the main script
cmd_name = 'find_unwrapped_vk_calls'
cmd = [
input_api.python3_executable,
input_api.os_path.join(input_api.PresubmitLocalPath(), vulkan_dir,
'find_unwrapped_vk_calls.py')
]
test_cmd = input_api.Command(
name=cmd_name, cmd=cmd, kwargs={}, message=output_api.PresubmitError)
if input_api.verbose:
print('Running ' + cmd_name)
results.extend(input_api.RunTests([test_cmd]))
return results
def _CheckPresubmitTests(input_api, output_api):
"""Test PRESUBMIT.py during presubmit."""
return input_api.RunTests(
input_api.canned_checks.GetUnitTestsInDirectory(
input_api,
output_api,
input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts'),
files_to_check=[r'^angle_presubmit_utils_unittest\.py$']))
def _CheckUnsafeBuffersSafetyComments(input_api, output_api):
"""Checks that ANGLE_UNSAFE_BUFFERS is accompanied by a
// SAFETY: comment.
"""
# We only check C++ source files.
exts = ('.h', '.cc', '.cpp', '.mm')
file_filter = lambda f: f.LocalPath().endswith(exts)
unsafe_buffers_regex = re.compile(r'\bANGLE_UNSAFE_BUFFERS\b')
safety_comment_regex = re.compile(r'//.*\bSAFETY\b')
problems = []
for f in input_api.AffectedSourceFiles(file_filter):
lines = f.NewContents()
for line_num, line in enumerate(lines, start=1):
if line.strip().startswith('//'):
continue
if unsafe_buffers_regex.search(line):
# Check if safety comment is on the same line.
if safety_comment_regex.search(line):
continue
# Check preceding lines for a SAFETY comment.
has_safety = False
for check_line_num in range(line_num - 1, 0, -1):
check_line = lines[check_line_num - 1].strip()
if not check_line:
continue
if check_line.startswith('//'):
if safety_comment_regex.search(check_line):
has_safety = True
break
else:
# Not a comment line. If it looks like the end of a statement, stop searching.
if check_line.endswith(';') or check_line.endswith(
'{') or check_line.endswith('}'):
break
if not has_safety:
problems.append(f"{f.LocalPath()}:{line_num}: "
"ANGLE_UNSAFE_BUFFERS usage must be accompanied by a "
"// SAFETY: comment.")
if problems:
return [
output_api.PresubmitPromptWarning(
"ANGLE_UNSAFE_BUFFERS usages must be accompanied by a "
"// SAFETY: comment explaining why they are safe.",
items=problems)
]
return []
# Copied from Chrome's _GetMessageForMatchingType.
def _GetMessageForMatchingType(input_api, affected_file, line_number, line, ban_rule):
"""
Helper method for checking for banned constructs.
Returns an string composed of the name of the file, the line number
where the match has been found and the additional text passed as
|message| in case the target type name matches the text inside the
line passed as parameter.
"""
result = []
# Ignore comments about banned types.
if input_api.re.search(r'^ *//', line):
return result
# A // nocheck comment will bypass this error.
if line.endswith(' nocheck'):
return result
matched = False
if ban_rule.pattern[0:1] == '/':
regex = ban_rule.pattern[1:]
if input_api.re.search(regex, line):
matched = True
elif ban_rule.pattern in line:
matched = True
if matched:
result.append(' %s:%d:' % (affected_file.LocalPath(), line_number))
for line in ban_rule.explanation:
result.append(' %s' % line)
return result
# Copied from Chrome's CheckNoBannedPatterns with modifications.
def _CheckNoBannedPatterns(input_api, output_api):
"""Make sure that banned patterns are not used."""
results = []
def IsExcludedFile(affected_file, excluded_paths):
if not excluded_paths:
return False
local_path = affected_file.UnixLocalPath()
for item in excluded_paths:
if input_api.re.match(item, local_path):
return True
return False
def CheckForMatch(affected_file, line_num, line, ban_rule):
if IsExcludedFile(affected_file, ban_rule.excluded_paths):
return
message = _GetMessageForMatchingType(input_api, affected_file, line_num, line, ban_rule)
if message:
result_loc = []
if ban_rule.surface_as_gerrit_lint:
if hasattr(output_api, 'PresubmitResultLocation'):
result_loc.append(
output_api.PresubmitResultLocation(
file_path=affected_file.LocalPath(),
start_line=line_num,
end_line=line_num,
))
if ban_rule.treat_as_error:
if result_loc:
results.append(
output_api.PresubmitError(
'A banned pattern was used.\n' + '\n'.join(message),
locations=result_loc))
else:
results.append(
output_api.PresubmitError('A banned pattern was used.\n' +
'\n'.join(message)))
else:
if result_loc:
results.append(
output_api.PresubmitPromptWarning(
'A banned pattern was used.\n' + '\n'.join(message),
locations=result_loc))
else:
results.append(
output_api.PresubmitPromptWarning('A banned pattern was used.\n' +
'\n'.join(message)))
file_filter = lambda f: f.LocalPath().endswith(('.cc', '.mm', '.cpp', '.h'))
for f in input_api.AffectedSourceFiles(file_filter):
for line_num, line in f.ChangedContents():
for ban_rule in _BANNED_CPP_PATTERNS:
CheckForMatch(f, line_num, line, ban_rule)
return results
def CheckChangeOnUpload(input_api, output_api):
results = []
results.extend(_CheckPresubmitTests(input_api, output_api))
results.extend(_CheckUnsafeBuffersSafetyComments(input_api, output_api))
results.extend(_CheckNoBannedPatterns(input_api, output_api))
results.extend(input_api.canned_checks.CheckForCommitObjects(input_api, output_api))
results.extend(_CheckTabsInSourceFiles(input_api, output_api))
results.extend(_CheckNonAsciiInSourceFiles(input_api, output_api))
results.extend(_CheckCommentBeforeTestInTestFiles(input_api, output_api))
results.extend(_CheckWildcardInTestExpectationFiles(input_api, output_api))
results.extend(_CheckShaderVersionInShaderLangHeader(input_api, output_api))
results.extend(_CheckCodeGeneration(input_api, output_api))
results.extend(_CheckChangeHasBugField(input_api, output_api))
results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api))
results.extend(_CheckNewHeaderWithoutGnChange(input_api, output_api))
results.extend(_CheckExportValidity(input_api, output_api))
results.extend(
input_api.canned_checks.CheckPatchFormatted(
input_api, output_api, result_factory=output_api.PresubmitError))
results.extend(_CheckCommitMessageFormatting(input_api, output_api))
results.extend(_CheckGClientExists(input_api, output_api))
results.extend(_CheckRestrictedTraces(input_api, output_api))
results.extend(_CheckUnwrappedVulkanCalls(input_api, output_api))
return results
def CheckChangeOnCommit(input_api, output_api):
return CheckChangeOnUpload(input_api, output_api)