48
48
import re
49
49
import subprocess
50
50
import sys
51
+ from typing import List , Tuple
51
52
52
53
53
- def write_file (file_name , text ) :
54
+ def write_file (file_name : str , text : str ) -> None :
54
55
with open (file_name , "w" , encoding = "utf-8" ) as f :
55
56
f .write (text )
56
57
f .truncate ()
57
58
58
59
59
- def try_run (args , raise_error = True ):
60
+ def try_run (args : List [ str ] , raise_error : bool = True ) -> str :
60
61
try :
61
62
process_output = subprocess .check_output (args , stderr = subprocess .STDOUT ).decode (
62
63
errors = "ignore"
@@ -71,12 +72,13 @@ def try_run(args, raise_error=True):
71
72
72
73
# This class represents the appearance of a message prefix in a file.
73
74
class MessagePrefix :
74
- def __init__ (self , label ):
75
+
76
+ def __init__ (self , label : str ) -> None :
75
77
self .has_message = False
76
- self .prefixes = []
78
+ self .prefixes : List [ str ] = []
77
79
self .label = label
78
80
79
- def check (self , file_check_suffix , input_text ) :
81
+ def check (self , file_check_suffix : str , input_text : str ) -> bool :
80
82
self .prefix = self .label + file_check_suffix
81
83
self .has_message = self .prefix in input_text
82
84
if self .has_message :
@@ -85,7 +87,8 @@ def check(self, file_check_suffix, input_text):
85
87
86
88
87
89
class CheckRunner :
88
- def __init__ (self , args , extra_args ):
90
+
91
+ def __init__ (self , args : argparse .Namespace , extra_args : List [str ]) -> None :
89
92
self .resource_dir = args .resource_dir
90
93
self .assume_file_name = args .assume_filename
91
94
self .input_file_name = args .input_file_name
@@ -143,11 +146,11 @@ def __init__(self, args, extra_args):
143
146
if self .resource_dir is not None :
144
147
self .clang_extra_args .append ("-resource-dir=%s" % self .resource_dir )
145
148
146
- def read_input (self ):
149
+ def read_input (self ) -> None :
147
150
with open (self .input_file_name , "r" , encoding = "utf-8" ) as input_file :
148
151
self .input_text = input_file .read ()
149
152
150
- def get_prefixes (self ):
153
+ def get_prefixes (self ) -> None :
151
154
for suffix in self .check_suffix :
152
155
if suffix and not re .match ("^[A-Z0-9\\ -]+$" , suffix ):
153
156
sys .exit (
@@ -189,7 +192,7 @@ def get_prefixes(self):
189
192
)
190
193
assert expect_diagnosis or self .expect_no_diagnosis
191
194
192
- def prepare_test_inputs (self ):
195
+ def prepare_test_inputs (self ) -> None :
193
196
# Remove the contents of the CHECK lines to avoid CHECKs matching on
194
197
# themselves. We need to keep the comments to preserve line numbers while
195
198
# avoiding empty lines which could potentially trigger formatting-related
@@ -198,7 +201,7 @@ def prepare_test_inputs(self):
198
201
write_file (self .temp_file_name , cleaned_test )
199
202
write_file (self .original_file_name , cleaned_test )
200
203
201
- def run_clang_tidy (self ):
204
+ def run_clang_tidy (self ) -> str :
202
205
args = (
203
206
[
204
207
"clang-tidy" ,
@@ -238,11 +241,11 @@ def run_clang_tidy(self):
238
241
print ("------------------------------------------------------------------" )
239
242
return clang_tidy_output
240
243
241
- def check_no_diagnosis (self , clang_tidy_output ) :
244
+ def check_no_diagnosis (self , clang_tidy_output : str ) -> None :
242
245
if clang_tidy_output != "" :
243
246
sys .exit ("No diagnostics were expected, but found the ones above" )
244
247
245
- def check_fixes (self ):
248
+ def check_fixes (self ) -> None :
246
249
if self .has_check_fixes :
247
250
try_run (
248
251
[
@@ -254,7 +257,7 @@ def check_fixes(self):
254
257
]
255
258
)
256
259
257
- def check_messages (self , clang_tidy_output ) :
260
+ def check_messages (self , clang_tidy_output : str ) -> None :
258
261
if self .has_check_messages :
259
262
messages_file = self .temp_file_name + ".msg"
260
263
write_file (messages_file , clang_tidy_output )
@@ -268,7 +271,7 @@ def check_messages(self, clang_tidy_output):
268
271
]
269
272
)
270
273
271
- def check_notes (self , clang_tidy_output ) :
274
+ def check_notes (self , clang_tidy_output : str ) -> None :
272
275
if self .has_check_notes :
273
276
notes_file = self .temp_file_name + ".notes"
274
277
filtered_output = [
@@ -287,7 +290,7 @@ def check_notes(self, clang_tidy_output):
287
290
]
288
291
)
289
292
290
- def run (self ):
293
+ def run (self ) -> None :
291
294
self .read_input ()
292
295
if self .export_fixes is None :
293
296
self .get_prefixes ()
@@ -313,7 +316,7 @@ def run(self):
313
316
C_STANDARDS = ["c99" , ("c11" , "c1x" ), "c17" , ("c23" , "c2x" ), "c2y" ]
314
317
315
318
316
- def expand_std (std ) :
319
+ def expand_std (std : str ) -> List [ str ] :
317
320
split_std , or_later , _ = std .partition ("-or-later" )
318
321
319
322
if not or_later :
@@ -335,11 +338,11 @@ def expand_std(std):
335
338
return [std ]
336
339
337
340
338
- def csv (string ) :
341
+ def csv (string : str ) -> List [ str ] :
339
342
return string .split ("," )
340
343
341
344
342
- def parse_arguments ():
345
+ def parse_arguments () -> Tuple [ argparse . Namespace , List [ str ]] :
343
346
parser = argparse .ArgumentParser (
344
347
prog = pathlib .Path (__file__ ).stem ,
345
348
description = __doc__ ,
@@ -374,7 +377,7 @@ def parse_arguments():
374
377
return parser .parse_known_args ()
375
378
376
379
377
- def main ():
380
+ def main () -> None :
378
381
args , extra_args = parse_arguments ()
379
382
380
383
abbreviated_stds = args .std
0 commit comments