Skip to content

Commit ee01323

Browse files
committed
[UVT] format with darker
1 parent 1ac1960 commit ee01323

File tree

1 file changed

+120
-37
lines changed

1 file changed

+120
-37
lines changed

clang/utils/update-verify-tests.py

Lines changed: 120 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@
2222
build/bin/llvm-lit clang/test/Sema/ --no-progress-bar -v | python3 update-verify-tests.py
2323
"""
2424

25+
2526
class KnownException(Exception):
2627
pass
2728

29+
2830
def parse_error_category(s):
2931
parts = s.split("diagnostics")
3032
diag_category = parts[0]
31-
category_parts = parts[0].strip().strip("'").split("-")
33+
category_parts = parts[0].strip().strip("'").split("-")
3234
expected = category_parts[0]
3335
if expected != "expected":
34-
raise Exception(f"expected 'expected', but found '{expected}'. Custom verify prefixes are not supported.")
36+
raise Exception(
37+
f"expected 'expected', but found '{expected}'. Custom verify prefixes are not supported."
38+
)
3539
diag_category = category_parts[1]
3640
if "seen but not expected" in parts[1]:
3741
seen = True
@@ -41,8 +45,11 @@ def parse_error_category(s):
4145
raise KnownException(f"unexpected category '{parts[1]}'")
4246
return (diag_category, seen)
4347

48+
4449
diag_error_re = re.compile(r"File (\S+) Line (\d+): (.+)")
4550
diag_error_re2 = re.compile(r"File \S+ Line \d+ \(directive at (\S+):(\d+)\): (.+)")
51+
52+
4653
def parse_diag_error(s):
4754
m = diag_error_re2.match(s)
4855
if not m:
@@ -51,13 +58,15 @@ def parse_diag_error(s):
5158
return None
5259
return (m.group(1), int(m.group(2)), m.group(3))
5360

61+
5462
class Line:
5563
def __init__(self, content, line_n):
5664
self.content = content
5765
self.diag = None
5866
self.line_n = line_n
5967
self.related_diags = []
6068
self.targeting_diags = []
69+
6170
def update_line_n(self, n):
6271
if self.diag and not self.diag.line_is_absolute:
6372
self.diag.orig_target_line_n += n - self.line_n
@@ -70,17 +79,29 @@ def update_line_n(self, n):
7079
for diag in self.related_diags:
7180
if not diag.line_is_absolute:
7281
pass
82+
7383
def render(self):
7484
if not self.diag:
7585
return self.content
76-
assert("{{DIAG}}" in self.content)
86+
assert "{{DIAG}}" in self.content
7787
res = self.content.replace("{{DIAG}}", self.diag.render())
7888
if not res.strip():
7989
return ""
8090
return res
8191

92+
8293
class Diag:
83-
def __init__(self, diag_content, category, targeted_line_n, line_is_absolute, count, line, is_re, whitespace_strings):
94+
def __init__(
95+
self,
96+
diag_content,
97+
category,
98+
targeted_line_n,
99+
line_is_absolute,
100+
count,
101+
line,
102+
is_re,
103+
whitespace_strings,
104+
):
84105
self.diag_content = diag_content
85106
self.category = category
86107
self.orig_target_line_n = targeted_line_n
@@ -104,14 +125,14 @@ def absolute_target(self):
104125
else:
105126
res = self.line.line_n + self.orig_target_line_n
106127
if self.target:
107-
assert(self.line.line_n == res)
128+
assert self.line.line_n == res
108129
return res
109130

110131
def relative_target(self):
111132
return self.absolute_target() - self.line.line_n
112133

113134
def render(self):
114-
assert(self.count >= 0)
135+
assert self.count >= 0
115136
if self.count == 0:
116137
return ""
117138
line_location_s = ""
@@ -121,7 +142,9 @@ def render(self):
121142
elif self.relative_target() > 0:
122143
line_location_s = f"@+{self.relative_target()}"
123144
else:
124-
line_location_s = f"@{self.relative_target()}" # the minus sign is implicit
145+
line_location_s = (
146+
f"@{self.relative_target()}" # the minus sign is implicit
147+
)
125148
count_s = "" if self.count == 1 else f"{self.count}"
126149
re_s = "-re" if self.is_re else ""
127150
if self.whitespace_strings:
@@ -138,16 +161,33 @@ def render(self):
138161
whitespace3_s = " "
139162
return f"//{whitespace1_s}expected-{self.category}{re_s}{whitespace2_s}{line_location_s}{whitespace3_s}{count_s}{whitespace4_s}{{{{{self.diag_content}}}}}"
140163

141-
expected_diag_re = re.compile(r"//(\s*)expected-(note|warning|error)(-re)?(\s*)(@[+-]?\d+)?(\s*)(\d+)?(\s*)\{\{(.*)\}\}")
164+
165+
expected_diag_re = re.compile(
166+
r"//(\s*)expected-(note|warning|error)(-re)?(\s*)(@[+-]?\d+)?(\s*)(\d+)?(\s*)\{\{(.*)\}\}"
167+
)
168+
169+
142170
def parse_diag(line, filename, lines):
143171
s = line.content
144172
ms = expected_diag_re.findall(s)
145173
if not ms:
146174
return None
147175
if len(ms) > 1:
148-
print(f"multiple diags on line {filename}:{line.line_n}. Aborting due to missing implementation.")
176+
print(
177+
f"multiple diags on line {filename}:{line.line_n}. Aborting due to missing implementation."
178+
)
149179
sys.exit(1)
150-
[whitespace1_s, category_s, re_s, whitespace2_s, target_line_s, whitespace3_s, count_s, whitespace4_s, diag_s] = ms[0]
180+
[
181+
whitespace1_s,
182+
category_s,
183+
re_s,
184+
whitespace2_s,
185+
target_line_s,
186+
whitespace3_s,
187+
count_s,
188+
whitespace4_s,
189+
diag_s,
190+
] = ms[0]
151191
if not target_line_s:
152192
target_line_n = 0
153193
is_absolute = False
@@ -163,35 +203,58 @@ def parse_diag(line, filename, lines):
163203
count = int(count_s) if count_s else 1
164204
line.content = expected_diag_re.sub("{{DIAG}}", s)
165205

166-
return Diag(diag_s, category_s, target_line_n, is_absolute, count, line, bool(re_s), [whitespace1_s, whitespace2_s, whitespace3_s, whitespace4_s])
206+
return Diag(
207+
diag_s,
208+
category_s,
209+
target_line_n,
210+
is_absolute,
211+
count,
212+
line,
213+
bool(re_s),
214+
[whitespace1_s, whitespace2_s, whitespace3_s, whitespace4_s],
215+
)
216+
167217

168218
def link_line_diags(lines, diag):
169219
line_n = diag.line.line_n
170220
target_line_n = diag.absolute_target()
171221
step = 1 if target_line_n < line_n else -1
172222
for i in range(target_line_n, line_n, step):
173-
lines[i-1].related_diags.append(diag)
223+
lines[i - 1].related_diags.append(diag)
224+
174225

175226
def add_line(new_line, lines):
176-
lines.insert(new_line.line_n-1, new_line)
227+
lines.insert(new_line.line_n - 1, new_line)
177228
for i in range(new_line.line_n, len(lines)):
178229
line = lines[i]
179-
assert(line.line_n == i)
180-
line.update_line_n(i+1)
181-
assert(all(line.line_n == i+1 for i, line in enumerate(lines)))
230+
assert line.line_n == i
231+
line.update_line_n(i + 1)
232+
assert all(line.line_n == i + 1 for i, line in enumerate(lines))
233+
182234

183235
indent_re = re.compile(r"\s*")
236+
237+
184238
def get_indent(s):
185239
return indent_re.match(s).group(0)
186240

241+
187242
def add_diag(line_n, diag_s, diag_category, lines):
188243
target = lines[line_n - 1]
189244
for other in target.targeting_diags:
190245
if other.is_re:
191-
raise KnownException("mismatching diag on line with regex matcher. Skipping due to missing implementation")
192-
reverse = True if [other for other in target.targeting_diags if other.relative_target() < 0] else False
193-
194-
targeting = [other for other in target.targeting_diags if not other.line_is_absolute]
246+
raise KnownException(
247+
"mismatching diag on line with regex matcher. Skipping due to missing implementation"
248+
)
249+
reverse = (
250+
True
251+
if [other for other in target.targeting_diags if other.relative_target() < 0]
252+
else False
253+
)
254+
255+
targeting = [
256+
other for other in target.targeting_diags if not other.line_is_absolute
257+
]
195258
targeting.sort(reverse=reverse, key=lambda d: d.relative_target())
196259
prev_offset = 0
197260
prev_line = target
@@ -206,28 +269,35 @@ def add_diag(line_n, diag_s, diag_category, lines):
206269
new_line_n = prev_line.line_n + 1
207270
else:
208271
new_line_n = prev_line.line_n
209-
assert(new_line_n == line_n + (not reverse) - total_offset)
272+
assert new_line_n == line_n + (not reverse) - total_offset
210273

211274
new_line = Line(get_indent(prev_line.content) + "{{DIAG}}\n", new_line_n)
212275
new_line.related_diags = list(prev_line.related_diags)
213276
add_line(new_line, lines)
214277

215-
new_diag = Diag(diag_s, diag_category, total_offset, False, 1, new_line, False, None)
278+
new_diag = Diag(
279+
diag_s, diag_category, total_offset, False, 1, new_line, False, None
280+
)
216281
new_line.diag = new_diag
217282
new_diag.target_line = target
218-
assert(type(new_diag) != str)
283+
assert type(new_diag) != str
219284
target.targeting_diags.append(new_diag)
220285
link_line_diags(lines, new_diag)
221286

287+
222288
updated_test_files = set()
289+
290+
223291
def update_test_file(filename, diag_errors):
224292
print(f"updating test file {filename}")
225293
if filename in updated_test_files:
226-
print(f"{filename} already updated, but got new output - expect incorrect results")
294+
print(
295+
f"{filename} already updated, but got new output - expect incorrect results"
296+
)
227297
else:
228298
updated_test_files.add(filename)
229-
with open(filename, 'r') as f:
230-
lines = [Line(line, i+1) for i, line in enumerate(f.readlines())]
299+
with open(filename, "r") as f:
300+
lines = [Line(line, i + 1) for i, line in enumerate(f.readlines())]
231301
for line in lines:
232302
diag = parse_diag(line, filename, lines)
233303
if diag:
@@ -236,37 +306,46 @@ def update_test_file(filename, diag_errors):
236306
link_line_diags(lines, diag)
237307
lines[diag.absolute_target() - 1].targeting_diags.append(diag)
238308

239-
for (line_n, diag_s, diag_category, seen) in diag_errors:
309+
for line_n, diag_s, diag_category, seen in diag_errors:
240310
if seen:
241311
continue
242312
# this is a diagnostic expected but not seen
243-
assert(lines[line_n - 1].diag)
313+
assert lines[line_n - 1].diag
244314
if diag_s != lines[line_n - 1].diag.diag_content:
245-
raise KnownException(f"{filename}:{line_n} - found diag {lines[line_n - 1].diag.diag_content} but expected {diag_s}")
315+
raise KnownException(
316+
f"{filename}:{line_n} - found diag {lines[line_n - 1].diag.diag_content} but expected {diag_s}"
317+
)
246318
if diag_category != lines[line_n - 1].diag.category:
247-
raise KnownException(f"{filename}:{line_n} - found {lines[line_n - 1].diag.category} diag but expected {diag_category}")
319+
raise KnownException(
320+
f"{filename}:{line_n} - found {lines[line_n - 1].diag.category} diag but expected {diag_category}"
321+
)
248322
lines[line_n - 1].diag.count -= 1
249323
diag_errors_left = []
250324
diag_errors.sort(reverse=True, key=lambda t: t[0])
251-
for (line_n, diag_s, diag_category, seen) in diag_errors:
325+
for line_n, diag_s, diag_category, seen in diag_errors:
252326
if not seen:
253327
continue
254328
target = lines[line_n - 1]
255-
other_diags = [d for d in target.targeting_diags if d.diag_content == diag_s and d.category == diag_category]
329+
other_diags = [
330+
d
331+
for d in target.targeting_diags
332+
if d.diag_content == diag_s and d.category == diag_category
333+
]
256334
other_diag = other_diags[0] if other_diags else None
257335
if other_diag:
258336
other_diag.count += 1
259337
else:
260338
diag_errors_left.append((line_n, diag_s, diag_category))
261-
for (line_n, diag_s, diag_category) in diag_errors_left:
339+
for line_n, diag_s, diag_category in diag_errors_left:
262340
add_diag(line_n, diag_s, diag_category, lines)
263-
with open(filename, 'w') as f:
341+
with open(filename, "w") as f:
264342
for line in lines:
265343
f.write(line.render())
266344

345+
267346
def update_test_files(errors):
268347
errors_by_file = {}
269-
for ((filename, line, diag_s), (diag_category, seen)) in errors:
348+
for (filename, line, diag_s), (diag_category, seen) in errors:
270349
if filename not in errors_by_file:
271350
errors_by_file[filename] = []
272351
errors_by_file[filename].append((line, diag_s, diag_category, seen))
@@ -276,6 +355,8 @@ def update_test_files(errors):
276355
except KnownException as e:
277356
print(f"{filename} - ERROR: {e}")
278357
print("continuing...")
358+
359+
279360
curr = []
280361
curr_category = None
281362
curr_run_line = None
@@ -297,9 +378,11 @@ def update_test_files(errors):
297378
continue
298379
if line.startswith("error: "):
299380
if "no expected directives found" in line:
300-
print(f"no expected directives found for RUN line '{curr_run_line.strip()}'. Add 'expected-no-diagnostics' manually if this is intended.")
381+
print(
382+
f"no expected directives found for RUN line '{curr_run_line.strip()}'. Add 'expected-no-diagnostics' manually if this is intended."
383+
)
301384
continue
302-
curr_category = parse_error_category(line[len("error: "):])
385+
curr_category = parse_error_category(line[len("error: ") :])
303386
continue
304387

305388
diag_error = parse_diag_error(line.strip())

0 commit comments

Comments
 (0)