4848import re
4949import subprocess
5050import sys
51+ from typing import List , Tuple
5152
5253
53- def write_file (file_name , text ) :
54+ def write_file (file_name : str , text : str ) -> None :
5455 with open (file_name , "w" , encoding = "utf-8" ) as f :
5556 f .write (text )
5657 f .truncate ()
5758
5859
59- def try_run (args , raise_error = True ):
60+ def try_run (args : List [ str ] , raise_error : bool = True ) -> str :
6061 try :
6162 process_output = subprocess .check_output (args , stderr = subprocess .STDOUT ).decode (
6263 errors = "ignore"
@@ -71,12 +72,13 @@ def try_run(args, raise_error=True):
7172
7273# This class represents the appearance of a message prefix in a file.
7374class MessagePrefix :
74- def __init__ (self , label ):
75+
76+ def __init__ (self , label : str ) -> None :
7577 self .has_message = False
76- self .prefixes = []
78+ self .prefixes : List [ str ] = []
7779 self .label = label
7880
79- def check (self , file_check_suffix , input_text ) :
81+ def check (self , file_check_suffix : str , input_text : str ) -> bool :
8082 self .prefix = self .label + file_check_suffix
8183 self .has_message = self .prefix in input_text
8284 if self .has_message :
@@ -85,7 +87,8 @@ def check(self, file_check_suffix, input_text):
8587
8688
8789class CheckRunner :
88- def __init__ (self , args , extra_args ):
90+
91+ def __init__ (self , args : argparse .Namespace , extra_args : List [str ]) -> None :
8992 self .resource_dir = args .resource_dir
9093 self .assume_file_name = args .assume_filename
9194 self .input_file_name = args .input_file_name
@@ -143,11 +146,11 @@ def __init__(self, args, extra_args):
143146 if self .resource_dir is not None :
144147 self .clang_extra_args .append ("-resource-dir=%s" % self .resource_dir )
145148
146- def read_input (self ):
149+ def read_input (self ) -> None :
147150 with open (self .input_file_name , "r" , encoding = "utf-8" ) as input_file :
148151 self .input_text = input_file .read ()
149152
150- def get_prefixes (self ):
153+ def get_prefixes (self ) -> None :
151154 for suffix in self .check_suffix :
152155 if suffix and not re .match ("^[A-Z0-9\\ -]+$" , suffix ):
153156 sys .exit (
@@ -189,7 +192,7 @@ def get_prefixes(self):
189192 )
190193 assert expect_diagnosis or self .expect_no_diagnosis
191194
192- def prepare_test_inputs (self ):
195+ def prepare_test_inputs (self ) -> None :
193196 # Remove the contents of the CHECK lines to avoid CHECKs matching on
194197 # themselves. We need to keep the comments to preserve line numbers while
195198 # avoiding empty lines which could potentially trigger formatting-related
@@ -198,7 +201,7 @@ def prepare_test_inputs(self):
198201 write_file (self .temp_file_name , cleaned_test )
199202 write_file (self .original_file_name , cleaned_test )
200203
201- def run_clang_tidy (self ):
204+ def run_clang_tidy (self ) -> str :
202205 args = (
203206 [
204207 "clang-tidy" ,
@@ -238,11 +241,11 @@ def run_clang_tidy(self):
238241 print ("------------------------------------------------------------------" )
239242 return clang_tidy_output
240243
241- def check_no_diagnosis (self , clang_tidy_output ) :
244+ def check_no_diagnosis (self , clang_tidy_output : str ) -> None :
242245 if clang_tidy_output != "" :
243246 sys .exit ("No diagnostics were expected, but found the ones above" )
244247
245- def check_fixes (self ):
248+ def check_fixes (self ) -> None :
246249 if self .has_check_fixes :
247250 try_run (
248251 [
@@ -254,7 +257,7 @@ def check_fixes(self):
254257 ]
255258 )
256259
257- def check_messages (self , clang_tidy_output ) :
260+ def check_messages (self , clang_tidy_output : str ) -> None :
258261 if self .has_check_messages :
259262 messages_file = self .temp_file_name + ".msg"
260263 write_file (messages_file , clang_tidy_output )
@@ -268,7 +271,7 @@ def check_messages(self, clang_tidy_output):
268271 ]
269272 )
270273
271- def check_notes (self , clang_tidy_output ) :
274+ def check_notes (self , clang_tidy_output : str ) -> None :
272275 if self .has_check_notes :
273276 notes_file = self .temp_file_name + ".notes"
274277 filtered_output = [
@@ -287,7 +290,7 @@ def check_notes(self, clang_tidy_output):
287290 ]
288291 )
289292
290- def run (self ):
293+ def run (self ) -> None :
291294 self .read_input ()
292295 if self .export_fixes is None :
293296 self .get_prefixes ()
@@ -313,7 +316,7 @@ def run(self):
313316C_STANDARDS = ["c99" , ("c11" , "c1x" ), "c17" , ("c23" , "c2x" ), "c2y" ]
314317
315318
316- def expand_std (std ) :
319+ def expand_std (std : str ) -> List [ str ] :
317320 split_std , or_later , _ = std .partition ("-or-later" )
318321
319322 if not or_later :
@@ -335,11 +338,11 @@ def expand_std(std):
335338 return [std ]
336339
337340
338- def csv (string ) :
341+ def csv (string : str ) -> List [ str ] :
339342 return string .split ("," )
340343
341344
342- def parse_arguments ():
345+ def parse_arguments () -> Tuple [ argparse . Namespace , List [ str ]] :
343346 parser = argparse .ArgumentParser (
344347 prog = pathlib .Path (__file__ ).stem ,
345348 description = __doc__ ,
@@ -374,7 +377,7 @@ def parse_arguments():
374377 return parser .parse_known_args ()
375378
376379
377- def main ():
380+ def main () -> None :
378381 args , extra_args = parse_arguments ()
379382
380383 abbreviated_stds = args .std
0 commit comments