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,12 @@ 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
+ def __init__ (self , label : str ) -> None :
75
76
self .has_message = False
76
- self .prefixes = []
77
+ self .prefixes : List [ str ] = []
77
78
self .label = label
78
79
79
- def check (self , file_check_suffix , input_text ) :
80
+ def check (self , file_check_suffix : str , input_text : str ) -> bool :
80
81
self .prefix = self .label + file_check_suffix
81
82
self .has_message = self .prefix in input_text
82
83
if self .has_message :
@@ -85,7 +86,7 @@ def check(self, file_check_suffix, input_text):
85
86
86
87
87
88
class CheckRunner :
88
- def __init__ (self , args , extra_args ) :
89
+ def __init__ (self , args : argparse . Namespace , extra_args : List [ str ]) -> None :
89
90
self .resource_dir = args .resource_dir
90
91
self .assume_file_name = args .assume_filename
91
92
self .input_file_name = args .input_file_name
@@ -143,11 +144,11 @@ def __init__(self, args, extra_args):
143
144
if self .resource_dir is not None :
144
145
self .clang_extra_args .append ("-resource-dir=%s" % self .resource_dir )
145
146
146
- def read_input (self ):
147
+ def read_input (self ) -> None :
147
148
with open (self .input_file_name , "r" , encoding = "utf-8" ) as input_file :
148
149
self .input_text = input_file .read ()
149
150
150
- def get_prefixes (self ):
151
+ def get_prefixes (self ) -> None :
151
152
for suffix in self .check_suffix :
152
153
if suffix and not re .match ("^[A-Z0-9\\ -]+$" , suffix ):
153
154
sys .exit (
@@ -189,7 +190,7 @@ def get_prefixes(self):
189
190
)
190
191
assert expect_diagnosis or self .expect_no_diagnosis
191
192
192
- def prepare_test_inputs (self ):
193
+ def prepare_test_inputs (self ) -> None :
193
194
# Remove the contents of the CHECK lines to avoid CHECKs matching on
194
195
# themselves. We need to keep the comments to preserve line numbers while
195
196
# avoiding empty lines which could potentially trigger formatting-related
@@ -198,7 +199,7 @@ def prepare_test_inputs(self):
198
199
write_file (self .temp_file_name , cleaned_test )
199
200
write_file (self .original_file_name , cleaned_test )
200
201
201
- def run_clang_tidy (self ):
202
+ def run_clang_tidy (self ) -> str :
202
203
args = (
203
204
[
204
205
"clang-tidy" ,
@@ -238,11 +239,11 @@ def run_clang_tidy(self):
238
239
print ("------------------------------------------------------------------" )
239
240
return clang_tidy_output
240
241
241
- def check_no_diagnosis (self , clang_tidy_output ) :
242
+ def check_no_diagnosis (self , clang_tidy_output : str ) -> None :
242
243
if clang_tidy_output != "" :
243
244
sys .exit ("No diagnostics were expected, but found the ones above" )
244
245
245
- def check_fixes (self ):
246
+ def check_fixes (self ) -> None :
246
247
if self .has_check_fixes :
247
248
try_run (
248
249
[
@@ -254,7 +255,7 @@ def check_fixes(self):
254
255
]
255
256
)
256
257
257
- def check_messages (self , clang_tidy_output ) :
258
+ def check_messages (self , clang_tidy_output : str ) -> None :
258
259
if self .has_check_messages :
259
260
messages_file = self .temp_file_name + ".msg"
260
261
write_file (messages_file , clang_tidy_output )
@@ -268,7 +269,7 @@ def check_messages(self, clang_tidy_output):
268
269
]
269
270
)
270
271
271
- def check_notes (self , clang_tidy_output ) :
272
+ def check_notes (self , clang_tidy_output : str ) -> None :
272
273
if self .has_check_notes :
273
274
notes_file = self .temp_file_name + ".notes"
274
275
filtered_output = [
@@ -287,7 +288,7 @@ def check_notes(self, clang_tidy_output):
287
288
]
288
289
)
289
290
290
- def run (self ):
291
+ def run (self ) -> None :
291
292
self .read_input ()
292
293
if self .export_fixes is None :
293
294
self .get_prefixes ()
@@ -313,7 +314,7 @@ def run(self):
313
314
C_STANDARDS = ["c99" , ("c11" , "c1x" ), "c17" , ("c23" , "c2x" ), "c2y" ]
314
315
315
316
316
- def expand_std (std ) :
317
+ def expand_std (std : str ) -> List [ str ] :
317
318
split_std , or_later , _ = std .partition ("-or-later" )
318
319
319
320
if not or_later :
@@ -335,11 +336,11 @@ def expand_std(std):
335
336
return [std ]
336
337
337
338
338
- def csv (string ) :
339
+ def csv (string : str ) -> List [ str ] :
339
340
return string .split ("," )
340
341
341
342
342
- def parse_arguments ():
343
+ def parse_arguments () -> Tuple [ argparse . Namespace , List [ str ]] :
343
344
parser = argparse .ArgumentParser (
344
345
prog = pathlib .Path (__file__ ).stem ,
345
346
description = __doc__ ,
@@ -374,7 +375,7 @@ def parse_arguments():
374
375
return parser .parse_known_args ()
375
376
376
377
377
- def main ():
378
+ def main () -> None :
378
379
args , extra_args = parse_arguments ()
379
380
380
381
abbreviated_stds = args .std
0 commit comments