3 """A script to generate FileCheck statements for 'opt' regression tests.
5 This script is a utility to update LLVM opt test cases with new
6 FileCheck patterns. It can either update all of the tests in the file or
7 a single test function.
11 # Default to using `opt` as found in your PATH.
12 $ update_test_checks.py test/foo.ll
14 # Override the path lookup.
15 $ update_test_checks.py --tool-binary=../bin/opt test/foo.ll
17 # Use a custom tool instead of `opt`.
18 $ update_test_checks.py --tool=yourtool test/foo.ll
21 1. Make a compiler patch that requires updating some number of FileCheck lines
22 in regression test files.
23 2. Save the patch and revert it from your local work area.
24 3. Update the RUN-lines in the affected regression tests to look canonical.
25 Example: "; RUN: opt < %s -instcombine -S | FileCheck %s"
26 4. Refresh the FileCheck lines for either the entire file or select functions by
28 5. Commit the fresh baseline of checks.
29 6. Apply your patch from step 1 and rebuild your local binaries.
30 7. Re-run this script on affected regression tests.
31 8. Check the diffs to ensure the script has done something reasonable.
32 9. Submit a patch including the regression test diffs for review.
35 from __future__
import print_function
38 import os
# Used to advertise this file's name ("autogenerated_note").
42 from UpdateTestChecks
import common
46 from argparse
import RawTextHelpFormatter
47 parser
= argparse
.ArgumentParser(description
=__doc__
, formatter_class
=RawTextHelpFormatter
)
48 parser
.add_argument('--tool', default
='opt',
49 help='The name of the tool used to generate the test case (defaults to "opt")')
50 parser
.add_argument('--tool-binary', '--opt-binary',
51 help='The tool binary used to generate the test case')
53 '--function', help='The function in the test file to update')
54 parser
.add_argument('-p', '--preserve-names', action
='store_true',
55 help='Do not scrub IR names')
56 parser
.add_argument('--function-signature', action
='store_true',
57 help='Keep function signature information around for the check line')
58 parser
.add_argument('--scrub-attributes', action
='store_true',
59 help='Remove attribute annotations (#0) from the end of check line')
60 parser
.add_argument('--check-attributes', action
='store_true',
61 help='Check "Function Attributes" for functions')
62 parser
.add_argument('--check-globals', action
='store_true',
63 help='Check global entries (global variables, metadata, attribute sets, ...) for functions')
64 parser
.add_argument('tests', nargs
='+')
65 initial_args
= common
.parse_commandline_args(parser
)
67 script_name
= os
.path
.basename(__file__
)
69 if initial_args
.tool_binary
:
70 tool_basename
= os
.path
.basename(initial_args
.tool_binary
)
71 if not re
.match(r
'^%s(-\d+)?(\.exe)?$' % (initial_args
.tool
), tool_basename
):
72 common
.error('Unexpected tool name: ' + tool_basename
)
75 for ti
in common
.itertests(initial_args
.tests
, parser
,
76 script_name
='utils/' + script_name
):
77 # If requested we scrub trailing attribute annotations, e.g., '#0', together with whitespaces
78 if ti
.args
.scrub_attributes
:
79 common
.SCRUB_TRAILING_WHITESPACE_TEST_RE
= common
.SCRUB_TRAILING_WHITESPACE_AND_ATTRIBUTES_RE
81 common
.SCRUB_TRAILING_WHITESPACE_TEST_RE
= common
.SCRUB_TRAILING_WHITESPACE_RE
83 tool_basename
= ti
.args
.tool
86 for l
in ti
.run_lines
:
88 common
.warn('Skipping unparsable RUN line: ' + l
)
91 commands
= [cmd
.strip() for cmd
in l
.split('|')]
92 assert len(commands
) >= 2
95 preprocess_cmd
= " | ".join(commands
[:-2])
96 tool_cmd
= commands
[-2]
97 filecheck_cmd
= commands
[-1]
98 common
.verify_filecheck_prefixes(filecheck_cmd
)
99 if not tool_cmd
.startswith(tool_basename
+ ' '):
100 common
.warn('Skipping non-%s RUN line: %s' % (tool_basename
, l
))
103 if not filecheck_cmd
.startswith('FileCheck '):
104 common
.warn('Skipping non-FileChecked RUN line: ' + l
)
107 tool_cmd_args
= tool_cmd
[len(tool_basename
):].strip()
108 tool_cmd_args
= tool_cmd_args
.replace('< %s', '').replace('%s', '').strip()
109 check_prefixes
= common
.get_check_prefixes(filecheck_cmd
)
111 # FIXME: We should use multiple check prefixes to common check lines. For
112 # now, we just ignore all but the last.
113 prefix_list
.append((check_prefixes
, tool_cmd_args
, preprocess_cmd
))
115 global_vars_seen_dict
= {}
116 builder
= common
.FunctionTestBuilder(
117 run_list
=prefix_list
,
122 tool_binary
= ti
.args
.tool_binary
124 tool_binary
= tool_basename
126 for prefixes
, tool_args
, preprocess_cmd
in prefix_list
:
127 common
.debug('Extracted tool cmd: ' + tool_basename
+ ' ' + tool_args
)
128 common
.debug('Extracted FileCheck prefixes: ' + str(prefixes
))
130 raw_tool_output
= common
.invoke_tool(tool_binary
, tool_args
,
131 ti
.path
, preprocess_cmd
=preprocess_cmd
,
132 verbose
=ti
.args
.verbose
)
133 builder
.process_run_line(common
.OPT_FUNCTION_RE
, common
.scrub_body
,
134 raw_tool_output
, prefixes
, False)
135 builder
.processed_prefixes(prefixes
)
137 func_dict
= builder
.finish_and_get_func_dict()
138 is_in_function
= False
139 is_in_function_start
= False
140 has_checked_pre_function_globals
= False
141 prefix_set
= set([prefix
for prefixes
, _
, _
in prefix_list
for prefix
in prefixes
])
142 common
.debug('Rewriting FileCheck prefixes:', str(prefix_set
))
145 include_generated_funcs
= common
.find_arg_in_test(ti
,
146 lambda args
: ti
.args
.include_generated_funcs
,
147 '--include-generated-funcs',
149 generated_prefixes
= []
150 if include_generated_funcs
:
151 # Generate the appropriate checks for each function. We need to emit
152 # these in the order according to the generated output so that CHECK-LABEL
153 # works properly. func_order provides that.
155 # We can't predict where various passes might insert functions so we can't
156 # be sure the input function order is maintained. Therefore, first spit
157 # out all the source lines.
158 common
.dump_input_lines(output_lines
, ti
, prefix_set
, ';')
161 if args
.check_globals
:
162 generated_prefixes
.extend(
163 common
.add_global_checks(builder
.global_var_dict(), ';',
164 prefix_list
, output_lines
,
165 global_vars_seen_dict
, args
.preserve_names
,
168 # Now generate all the checks.
169 generated_prefixes
.extend(
170 common
.add_checks_at_end(
171 output_lines
, prefix_list
, builder
.func_order(), ';',
172 lambda my_output_lines
, prefixes
, func
: common
.add_ir_checks(
179 args
.function_signature
,
181 global_vars_seen_dict
,
182 is_filtered
=builder
.is_filtered())))
185 for input_line_info
in ti
.iterlines(output_lines
):
186 input_line
= input_line_info
.line
187 args
= input_line_info
.args
188 if is_in_function_start
:
191 if input_line
.lstrip().startswith(';'):
192 m
= common
.CHECK_RE
.match(input_line
)
193 if not m
or m
.group(1) not in prefix_set
:
194 output_lines
.append(input_line
)
197 # Print out the various check lines here.
198 generated_prefixes
.extend(
199 common
.add_ir_checks(
206 args
.function_signature
,
208 global_vars_seen_dict
,
209 is_filtered
=builder
.is_filtered()))
210 is_in_function_start
= False
212 m
= common
.IR_FUNCTION_RE
.match(input_line
)
213 if m
and not has_checked_pre_function_globals
:
214 if args
.check_globals
:
215 generated_prefixes
.extend(
216 common
.add_global_checks(builder
.global_var_dict(), ';',
217 prefix_list
, output_lines
,
218 global_vars_seen_dict
,
219 args
.preserve_names
, True))
220 has_checked_pre_function_globals
= True
222 if common
.should_add_line_to_output(input_line
, prefix_set
, not is_in_function
):
223 # This input line of the function body will go as-is into the output.
224 # Except make leading whitespace uniform: 2 spaces.
225 input_line
= common
.SCRUB_LEADING_WHITESPACE_RE
.sub(r
' ', input_line
)
226 output_lines
.append(input_line
)
227 if input_line
.strip() == '}':
228 is_in_function
= False
234 m
= common
.IR_FUNCTION_RE
.match(input_line
)
237 func_name
= m
.group(1)
238 if args
.function
is not None and func_name
!= args
.function
:
239 # When filtering on a specific function, skip all others.
241 is_in_function
= is_in_function_start
= True
243 if args
.check_globals
:
244 generated_prefixes
.extend(
245 common
.add_global_checks(builder
.global_var_dict(), ';', prefix_list
,
246 output_lines
, global_vars_seen_dict
,
247 args
.preserve_names
, False))
248 if ti
.args
.gen_unused_prefix_body
:
249 output_lines
.extend(ti
.get_checks_for_unused_prefixes(
250 prefix_list
, generated_prefixes
))
251 common
.debug('Writing %d lines to %s...' % (len(output_lines
), ti
.path
))
253 with
open(ti
.path
, 'wb') as f
:
254 f
.writelines(['{}\n'.format(l
).encode('utf-8') for l
in output_lines
])
257 if __name__
== '__main__':