22
22
build/bin/llvm-lit clang/test/Sema/ --no-progress-bar -v | python3 update-verify-tests.py
23
23
"""
24
24
25
+
25
26
class KnownException (Exception ):
26
27
pass
27
28
29
+
28
30
def parse_error_category (s ):
29
31
parts = s .split ("diagnostics" )
30
32
diag_category = parts [0 ]
31
- category_parts = parts [0 ].strip ().strip ("'" ).split ("-" )
33
+ category_parts = parts [0 ].strip ().strip ("'" ).split ("-" )
32
34
expected = category_parts [0 ]
33
35
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
+ )
35
39
diag_category = category_parts [1 ]
36
40
if "seen but not expected" in parts [1 ]:
37
41
seen = True
@@ -41,8 +45,11 @@ def parse_error_category(s):
41
45
raise KnownException (f"unexpected category '{ parts [1 ]} '" )
42
46
return (diag_category , seen )
43
47
48
+
44
49
diag_error_re = re .compile (r"File (\S+) Line (\d+): (.+)" )
45
50
diag_error_re2 = re .compile (r"File \S+ Line \d+ \(directive at (\S+):(\d+)\): (.+)" )
51
+
52
+
46
53
def parse_diag_error (s ):
47
54
m = diag_error_re2 .match (s )
48
55
if not m :
@@ -51,13 +58,15 @@ def parse_diag_error(s):
51
58
return None
52
59
return (m .group (1 ), int (m .group (2 )), m .group (3 ))
53
60
61
+
54
62
class Line :
55
63
def __init__ (self , content , line_n ):
56
64
self .content = content
57
65
self .diag = None
58
66
self .line_n = line_n
59
67
self .related_diags = []
60
68
self .targeting_diags = []
69
+
61
70
def update_line_n (self , n ):
62
71
if self .diag and not self .diag .line_is_absolute :
63
72
self .diag .orig_target_line_n += n - self .line_n
@@ -70,17 +79,29 @@ def update_line_n(self, n):
70
79
for diag in self .related_diags :
71
80
if not diag .line_is_absolute :
72
81
pass
82
+
73
83
def render (self ):
74
84
if not self .diag :
75
85
return self .content
76
- assert ( "{{DIAG}}" in self .content )
86
+ assert "{{DIAG}}" in self .content
77
87
res = self .content .replace ("{{DIAG}}" , self .diag .render ())
78
88
if not res .strip ():
79
89
return ""
80
90
return res
81
91
92
+
82
93
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
+ ):
84
105
self .diag_content = diag_content
85
106
self .category = category
86
107
self .orig_target_line_n = targeted_line_n
@@ -104,14 +125,14 @@ def absolute_target(self):
104
125
else :
105
126
res = self .line .line_n + self .orig_target_line_n
106
127
if self .target :
107
- assert ( self .line .line_n == res )
128
+ assert self .line .line_n == res
108
129
return res
109
130
110
131
def relative_target (self ):
111
132
return self .absolute_target () - self .line .line_n
112
133
113
134
def render (self ):
114
- assert ( self .count >= 0 )
135
+ assert self .count >= 0
115
136
if self .count == 0 :
116
137
return ""
117
138
line_location_s = ""
@@ -121,7 +142,9 @@ def render(self):
121
142
elif self .relative_target () > 0 :
122
143
line_location_s = f"@+{ self .relative_target ()} "
123
144
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
+ )
125
148
count_s = "" if self .count == 1 else f"{ self .count } "
126
149
re_s = "-re" if self .is_re else ""
127
150
if self .whitespace_strings :
@@ -138,16 +161,33 @@ def render(self):
138
161
whitespace3_s = " "
139
162
return f"//{ whitespace1_s } expected-{ self .category } { re_s } { whitespace2_s } { line_location_s } { whitespace3_s } { count_s } { whitespace4_s } {{{{{ self .diag_content } }}}}"
140
163
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
+
142
170
def parse_diag (line , filename , lines ):
143
171
s = line .content
144
172
ms = expected_diag_re .findall (s )
145
173
if not ms :
146
174
return None
147
175
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
+ )
149
179
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 ]
151
191
if not target_line_s :
152
192
target_line_n = 0
153
193
is_absolute = False
@@ -163,35 +203,58 @@ def parse_diag(line, filename, lines):
163
203
count = int (count_s ) if count_s else 1
164
204
line .content = expected_diag_re .sub ("{{DIAG}}" , s )
165
205
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
+
167
217
168
218
def link_line_diags (lines , diag ):
169
219
line_n = diag .line .line_n
170
220
target_line_n = diag .absolute_target ()
171
221
step = 1 if target_line_n < line_n else - 1
172
222
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
+
174
225
175
226
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 )
177
228
for i in range (new_line .line_n , len (lines )):
178
229
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
+
182
234
183
235
indent_re = re .compile (r"\s*" )
236
+
237
+
184
238
def get_indent (s ):
185
239
return indent_re .match (s ).group (0 )
186
240
241
+
187
242
def add_diag (line_n , diag_s , diag_category , lines ):
188
243
target = lines [line_n - 1 ]
189
244
for other in target .targeting_diags :
190
245
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
+ ]
195
258
targeting .sort (reverse = reverse , key = lambda d : d .relative_target ())
196
259
prev_offset = 0
197
260
prev_line = target
@@ -206,28 +269,35 @@ def add_diag(line_n, diag_s, diag_category, lines):
206
269
new_line_n = prev_line .line_n + 1
207
270
else :
208
271
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
210
273
211
274
new_line = Line (get_indent (prev_line .content ) + "{{DIAG}}\n " , new_line_n )
212
275
new_line .related_diags = list (prev_line .related_diags )
213
276
add_line (new_line , lines )
214
277
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
+ )
216
281
new_line .diag = new_diag
217
282
new_diag .target_line = target
218
- assert ( type (new_diag ) != str )
283
+ assert type (new_diag ) != str
219
284
target .targeting_diags .append (new_diag )
220
285
link_line_diags (lines , new_diag )
221
286
287
+
222
288
updated_test_files = set ()
289
+
290
+
223
291
def update_test_file (filename , diag_errors ):
224
292
print (f"updating test file { filename } " )
225
293
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
+ )
227
297
else :
228
298
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 ())]
231
301
for line in lines :
232
302
diag = parse_diag (line , filename , lines )
233
303
if diag :
@@ -236,37 +306,46 @@ def update_test_file(filename, diag_errors):
236
306
link_line_diags (lines , diag )
237
307
lines [diag .absolute_target () - 1 ].targeting_diags .append (diag )
238
308
239
- for ( line_n , diag_s , diag_category , seen ) in diag_errors :
309
+ for line_n , diag_s , diag_category , seen in diag_errors :
240
310
if seen :
241
311
continue
242
312
# this is a diagnostic expected but not seen
243
- assert ( lines [line_n - 1 ].diag )
313
+ assert lines [line_n - 1 ].diag
244
314
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
+ )
246
318
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
+ )
248
322
lines [line_n - 1 ].diag .count -= 1
249
323
diag_errors_left = []
250
324
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 :
252
326
if not seen :
253
327
continue
254
328
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
+ ]
256
334
other_diag = other_diags [0 ] if other_diags else None
257
335
if other_diag :
258
336
other_diag .count += 1
259
337
else :
260
338
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 :
262
340
add_diag (line_n , diag_s , diag_category , lines )
263
- with open (filename , 'w' ) as f :
341
+ with open (filename , "w" ) as f :
264
342
for line in lines :
265
343
f .write (line .render ())
266
344
345
+
267
346
def update_test_files (errors ):
268
347
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 :
270
349
if filename not in errors_by_file :
271
350
errors_by_file [filename ] = []
272
351
errors_by_file [filename ].append ((line , diag_s , diag_category , seen ))
@@ -276,6 +355,8 @@ def update_test_files(errors):
276
355
except KnownException as e :
277
356
print (f"{ filename } - ERROR: { e } " )
278
357
print ("continuing..." )
358
+
359
+
279
360
curr = []
280
361
curr_category = None
281
362
curr_run_line = None
@@ -297,9 +378,11 @@ def update_test_files(errors):
297
378
continue
298
379
if line .startswith ("error: " ):
299
380
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
+ )
301
384
continue
302
- curr_category = parse_error_category (line [len ("error: " ):])
385
+ curr_category = parse_error_category (line [len ("error: " ) :])
303
386
continue
304
387
305
388
diag_error = parse_diag_error (line .strip ())
0 commit comments