3131
3232from  __future__ import  print_function 
3333
34+ from  sys  import  stderr 
35+ from  traceback  import  print_exc 
3436import  argparse 
3537import  os   # Used to advertise this file's name ("autogenerated_note"). 
3638import  sys 
3941from  UpdateTestChecks  import  common 
4042
4143
44+ def  update_test (opt_basename : str , ti : common .TestInfo ):
45+     triple_in_ir  =  None 
46+     for  l  in  ti .input_lines :
47+         m  =  common .TRIPLE_IR_RE .match (l )
48+         if  m :
49+             triple_in_ir  =  m .groups ()[0 ]
50+             break 
51+ 
52+     prefix_list  =  []
53+     for  l  in  ti .run_lines :
54+         if  "|"  not  in   l :
55+             common .warn ("Skipping unparsable RUN line: "  +  l )
56+             continue 
57+ 
58+         (tool_cmd , filecheck_cmd ) =  tuple ([cmd .strip () for  cmd  in  l .split ("|" , 1 )])
59+         common .verify_filecheck_prefixes (filecheck_cmd )
60+ 
61+         if  not  tool_cmd .startswith (opt_basename  +  " " ):
62+             common .warn ("WSkipping non-%s RUN line: %s"  %  (opt_basename , l ))
63+             continue 
64+ 
65+         if  not  filecheck_cmd .startswith ("FileCheck " ):
66+             common .warn ("Skipping non-FileChecked RUN line: "  +  l )
67+             continue 
68+ 
69+         tool_cmd_args  =  tool_cmd [len (opt_basename ) :].strip ()
70+         tool_cmd_args  =  tool_cmd_args .replace ("< %s" , "" ).replace ("%s" , "" ).strip ()
71+         check_prefixes  =  common .get_check_prefixes (filecheck_cmd )
72+ 
73+         # FIXME: We should use multiple check prefixes to common check lines. For 
74+         # now, we just ignore all but the last. 
75+         prefix_list .append ((check_prefixes , tool_cmd_args ))
76+ 
77+     ginfo  =  common .make_analyze_generalizer (version = 1 )
78+     builder  =  common .FunctionTestBuilder (
79+         run_list = prefix_list ,
80+         flags = type (
81+             "" ,
82+             (object ,),
83+             {
84+                 "verbose" : ti .args .verbose ,
85+                 "filters" : ti .args .filters ,
86+                 "function_signature" : False ,
87+                 "check_attributes" : False ,
88+                 "replace_value_regex" : [],
89+             },
90+         ),
91+         scrubber_args = [],
92+         path = ti .path ,
93+         ginfo = ginfo ,
94+     )
95+ 
96+     for  prefixes , opt_args  in  prefix_list :
97+         common .debug ("Extracted opt cmd:" , opt_basename , opt_args , file = sys .stderr )
98+         common .debug ("Extracted FileCheck prefixes:" , str (prefixes ), file = sys .stderr )
99+ 
100+         raw_tool_outputs  =  common .invoke_tool (ti .args .opt_binary , opt_args , ti .path )
101+ 
102+         if  re .search (r"Printing analysis " , raw_tool_outputs ) is  not   None :
103+             # Split analysis outputs by "Printing analysis " declarations. 
104+             for  raw_tool_output  in  re .split (r"Printing analysis " , raw_tool_outputs ):
105+                 builder .process_run_line (
106+                     common .ANALYZE_FUNCTION_RE ,
107+                     common .scrub_body ,
108+                     raw_tool_output ,
109+                     prefixes ,
110+                 )
111+         elif  (
112+             re .search (r"(LV|LDist): Checking a loop in " , raw_tool_outputs ) is  not   None 
113+         ):
114+             for  raw_tool_output  in  re .split (
115+                 r"(LV|LDist): Checking a loop in " , raw_tool_outputs 
116+             ):
117+                 builder .process_run_line (
118+                     common .LOOP_PASS_DEBUG_RE ,
119+                     common .scrub_body ,
120+                     raw_tool_output ,
121+                     prefixes ,
122+                 )
123+         else :
124+             common .warn ("Don't know how to deal with this output" )
125+             continue 
126+ 
127+         builder .processed_prefixes (prefixes )
128+ 
129+     func_dict  =  builder .finish_and_get_func_dict ()
130+     is_in_function  =  False 
131+     is_in_function_start  =  False 
132+     prefix_set  =  set ([prefix  for  prefixes , _  in  prefix_list  for  prefix  in  prefixes ])
133+     common .debug ("Rewriting FileCheck prefixes:" , str (prefix_set ), file = sys .stderr )
134+     output_lines  =  []
135+ 
136+     generated_prefixes  =  []
137+     for  input_info  in  ti .iterlines (output_lines ):
138+         input_line  =  input_info .line 
139+         args  =  input_info .args 
140+         if  is_in_function_start :
141+             if  input_line  ==  "" :
142+                 continue 
143+             if  input_line .lstrip ().startswith (";" ):
144+                 m  =  common .CHECK_RE .match (input_line )
145+                 if  not  m  or  m .group (1 ) not  in   prefix_set :
146+                     output_lines .append (input_line )
147+                     continue 
148+ 
149+             # Print out the various check lines here. 
150+             generated_prefixes .extend (
151+                 common .add_analyze_checks (
152+                     output_lines ,
153+                     ";" ,
154+                     prefix_list ,
155+                     func_dict ,
156+                     func_name ,
157+                     ginfo ,
158+                     is_filtered = builder .is_filtered (),
159+                 )
160+             )
161+             is_in_function_start  =  False 
162+ 
163+         if  is_in_function :
164+             if  common .should_add_line_to_output (input_line , prefix_set ):
165+                 # This input line of the function body will go as-is into the output. 
166+                 output_lines .append (input_line )
167+             else :
168+                 continue 
169+             if  input_line .strip () ==  "}" :
170+                 is_in_function  =  False 
171+             continue 
172+ 
173+         # If it's outside a function, it just gets copied to the output. 
174+         output_lines .append (input_line )
175+ 
176+         m  =  common .IR_FUNCTION_RE .match (input_line )
177+         if  not  m :
178+             continue 
179+         func_name  =  m .group (1 )
180+         if  ti .args .function  is  not   None  and  func_name  !=  ti .args .function :
181+             # When filtering on a specific function, skip all others. 
182+             continue 
183+         is_in_function  =  is_in_function_start  =  True 
184+ 
185+     if  ti .args .gen_unused_prefix_body :
186+         output_lines .extend (
187+             ti .get_checks_for_unused_prefixes (prefix_list , generated_prefixes )
188+         )
189+ 
190+     common .debug ("Writing %d lines to %s..."  %  (len (output_lines ), ti .path ))
191+ 
192+     with  open (ti .path , "wb" ) as  f :
193+         f .writelines (["{}\n " .format (l ).encode ("utf-8" ) for  l  in  output_lines ])
194+ 
195+ 
42196def  main ():
43197    from  argparse  import  RawTextHelpFormatter 
44198
@@ -61,164 +215,18 @@ def main():
61215        common .error ("Unexpected opt name: "  +  opt_basename )
62216        sys .exit (1 )
63217
218+     returncode  =  0 
64219    for  ti  in  common .itertests (
65220        initial_args .tests , parser , script_name = "utils/"  +  script_name 
66221    ):
67-         triple_in_ir  =  None 
68-         for  l  in  ti .input_lines :
69-             m  =  common .TRIPLE_IR_RE .match (l )
70-             if  m :
71-                 triple_in_ir  =  m .groups ()[0 ]
72-                 break 
73- 
74-         prefix_list  =  []
75-         for  l  in  ti .run_lines :
76-             if  "|"  not  in   l :
77-                 common .warn ("Skipping unparsable RUN line: "  +  l )
78-                 continue 
79- 
80-             (tool_cmd , filecheck_cmd ) =  tuple ([cmd .strip () for  cmd  in  l .split ("|" , 1 )])
81-             common .verify_filecheck_prefixes (filecheck_cmd )
82- 
83-             if  not  tool_cmd .startswith (opt_basename  +  " " ):
84-                 common .warn ("WSkipping non-%s RUN line: %s"  %  (opt_basename , l ))
85-                 continue 
86- 
87-             if  not  filecheck_cmd .startswith ("FileCheck " ):
88-                 common .warn ("Skipping non-FileChecked RUN line: "  +  l )
89-                 continue 
90- 
91-             tool_cmd_args  =  tool_cmd [len (opt_basename ) :].strip ()
92-             tool_cmd_args  =  tool_cmd_args .replace ("< %s" , "" ).replace ("%s" , "" ).strip ()
93-             check_prefixes  =  common .get_check_prefixes (filecheck_cmd )
94- 
95-             # FIXME: We should use multiple check prefixes to common check lines. For 
96-             # now, we just ignore all but the last. 
97-             prefix_list .append ((check_prefixes , tool_cmd_args ))
98- 
99-         ginfo  =  common .make_analyze_generalizer (version = 1 )
100-         builder  =  common .FunctionTestBuilder (
101-             run_list = prefix_list ,
102-             flags = type (
103-                 "" ,
104-                 (object ,),
105-                 {
106-                     "verbose" : ti .args .verbose ,
107-                     "filters" : ti .args .filters ,
108-                     "function_signature" : False ,
109-                     "check_attributes" : False ,
110-                     "replace_value_regex" : [],
111-                 },
112-             ),
113-             scrubber_args = [],
114-             path = ti .path ,
115-             ginfo = ginfo ,
116-         )
117- 
118-         for  prefixes , opt_args  in  prefix_list :
119-             common .debug ("Extracted opt cmd:" , opt_basename , opt_args , file = sys .stderr )
120-             common .debug (
121-                 "Extracted FileCheck prefixes:" , str (prefixes ), file = sys .stderr 
122-             )
123- 
124-             raw_tool_outputs  =  common .invoke_tool (ti .args .opt_binary , opt_args , ti .path )
125- 
126-             if  re .search (r"Printing analysis " , raw_tool_outputs ) is  not   None :
127-                 # Split analysis outputs by "Printing analysis " declarations. 
128-                 for  raw_tool_output  in  re .split (
129-                     r"Printing analysis " , raw_tool_outputs 
130-                 ):
131-                     builder .process_run_line (
132-                         common .ANALYZE_FUNCTION_RE ,
133-                         common .scrub_body ,
134-                         raw_tool_output ,
135-                         prefixes ,
136-                     )
137-             elif  (
138-                 re .search (r"(LV|LDist): Checking a loop in " , raw_tool_outputs )
139-                 is  not   None 
140-             ):
141-                 for  raw_tool_output  in  re .split (
142-                     r"(LV|LDist): Checking a loop in " , raw_tool_outputs 
143-                 ):
144-                     builder .process_run_line (
145-                         common .LOOP_PASS_DEBUG_RE ,
146-                         common .scrub_body ,
147-                         raw_tool_output ,
148-                         prefixes ,
149-                     )
150-             else :
151-                 common .warn ("Don't know how to deal with this output" )
152-                 continue 
153- 
154-             builder .processed_prefixes (prefixes )
155- 
156-         func_dict  =  builder .finish_and_get_func_dict ()
157-         is_in_function  =  False 
158-         is_in_function_start  =  False 
159-         prefix_set  =  set ([prefix  for  prefixes , _  in  prefix_list  for  prefix  in  prefixes ])
160-         common .debug ("Rewriting FileCheck prefixes:" , str (prefix_set ), file = sys .stderr )
161-         output_lines  =  []
162- 
163-         generated_prefixes  =  []
164-         for  input_info  in  ti .iterlines (output_lines ):
165-             input_line  =  input_info .line 
166-             args  =  input_info .args 
167-             if  is_in_function_start :
168-                 if  input_line  ==  "" :
169-                     continue 
170-                 if  input_line .lstrip ().startswith (";" ):
171-                     m  =  common .CHECK_RE .match (input_line )
172-                     if  not  m  or  m .group (1 ) not  in   prefix_set :
173-                         output_lines .append (input_line )
174-                         continue 
175- 
176-                 # Print out the various check lines here. 
177-                 generated_prefixes .extend (
178-                     common .add_analyze_checks (
179-                         output_lines ,
180-                         ";" ,
181-                         prefix_list ,
182-                         func_dict ,
183-                         func_name ,
184-                         ginfo ,
185-                         is_filtered = builder .is_filtered (),
186-                     )
187-                 )
188-                 is_in_function_start  =  False 
189- 
190-             if  is_in_function :
191-                 if  common .should_add_line_to_output (input_line , prefix_set ):
192-                     # This input line of the function body will go as-is into the output. 
193-                     output_lines .append (input_line )
194-                 else :
195-                     continue 
196-                 if  input_line .strip () ==  "}" :
197-                     is_in_function  =  False 
198-                 continue 
199- 
200-             # If it's outside a function, it just gets copied to the output. 
201-             output_lines .append (input_line )
202- 
203-             m  =  common .IR_FUNCTION_RE .match (input_line )
204-             if  not  m :
205-                 continue 
206-             func_name  =  m .group (1 )
207-             if  ti .args .function  is  not   None  and  func_name  !=  ti .args .function :
208-                 # When filtering on a specific function, skip all others. 
209-                 continue 
210-             is_in_function  =  is_in_function_start  =  True 
211- 
212-         if  ti .args .gen_unused_prefix_body :
213-             output_lines .extend (
214-                 ti .get_checks_for_unused_prefixes (prefix_list , generated_prefixes )
215-             )
216- 
217-         common .debug ("Writing %d lines to %s..."  %  (len (output_lines ), ti .path ))
218- 
219-         with  open (ti .path , "wb" ) as  f :
220-             f .writelines (["{}\n " .format (l ).encode ("utf-8" ) for  l  in  output_lines ])
222+         try :
223+             update_test (opt_basename , ti )
224+         except  Exception :
225+             stderr .write (f"Error: Failed to update test { ti .path } \n " )
226+             print_exc ()
227+             returncode  =  1 
228+     return  returncode 
221229
222230
223231if  __name__  ==  "__main__" :
224-     main ()
232+     sys . exit ( main () )
0 commit comments