[test] Fix tests when run on windows after SVN r369426. NFC.
[llvm-core.git] / utils / update_llc_test_checks.py
blob1f7d939155e2a636444961df5459d9bc3bf81730
1 #!/usr/bin/env python
3 """A test case update script.
5 This script is a utility to update LLVM 'llc' based test cases with new
6 FileCheck patterns. It can either update all of the tests in the file or
7 a single test function.
8 """
10 from __future__ import print_function
12 import argparse
13 import glob
14 import os # Used to advertise this file's name ("autogenerated_note").
15 import string
16 import subprocess
17 import sys
18 import re
20 from UpdateTestChecks import asm, common
22 ADVERT = '; NOTE: Assertions have been autogenerated by '
25 def main():
26 parser = argparse.ArgumentParser(description=__doc__)
27 parser.add_argument('-v', '--verbose', action='store_true',
28 help='Show verbose output')
29 parser.add_argument('--llc-binary', default='llc',
30 help='The "llc" binary to use to generate the test case')
31 parser.add_argument(
32 '--function', help='The function in the test file to update')
33 parser.add_argument(
34 '--extra_scrub', action='store_true',
35 help='Always use additional regex to further reduce diffs between various subtargets')
36 parser.add_argument(
37 '--x86_scrub_rip', action='store_true', default=True,
38 help='Use more regex for x86 matching to reduce diffs between various subtargets')
39 parser.add_argument(
40 '--no_x86_scrub_rip', action='store_false', dest='x86_scrub_rip')
41 parser.add_argument('-u', '--update-only', action='store_true',
42 help='Only update test if it was already autogened')
43 parser.add_argument('tests', nargs='+')
44 args = parser.parse_args()
46 script_name = os.path.basename(__file__)
47 autogenerated_note = (ADVERT + 'utils/' + script_name)
49 test_paths = [test for pattern in args.tests for test in glob.glob(pattern)]
50 for test in test_paths:
51 if args.verbose:
52 print('Scanning for RUN lines in test file: %s' % (test,), file=sys.stderr)
53 with open(test) as f:
54 input_lines = [l.rstrip() for l in f]
56 first_line = input_lines[0] if input_lines else ""
57 if 'autogenerated' in first_line and script_name not in first_line:
58 common.warn("Skipping test which wasn't autogenerated by " + script_name, test)
59 continue
61 if args.update_only:
62 if not first_line or 'autogenerated' not in first_line:
63 common.warn("Skipping test which isn't autogenerated: " + test)
64 continue
66 triple_in_ir = None
67 for l in input_lines:
68 m = common.TRIPLE_IR_RE.match(l)
69 if m:
70 triple_in_ir = m.groups()[0]
71 break
73 raw_lines = [m.group(1)
74 for m in [common.RUN_LINE_RE.match(l) for l in input_lines] if m]
75 run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
76 for l in raw_lines[1:]:
77 if run_lines[-1].endswith("\\"):
78 run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
79 else:
80 run_lines.append(l)
82 if args.verbose:
83 print('Found %d RUN lines:' % (len(run_lines),), file=sys.stderr)
84 for l in run_lines:
85 print(' RUN: ' + l, file=sys.stderr)
87 run_list = []
88 for l in run_lines:
89 if '|' not in l:
90 common.warn('Skipping unparseable RUN line: ' + l)
91 continue
93 commands = [cmd.strip() for cmd in l.split('|', 1)]
94 llc_cmd = commands[0]
96 triple_in_cmd = None
97 m = common.TRIPLE_ARG_RE.search(llc_cmd)
98 if m:
99 triple_in_cmd = m.groups()[0]
101 march_in_cmd = None
102 m = common.MARCH_ARG_RE.search(llc_cmd)
103 if m:
104 march_in_cmd = m.groups()[0]
106 filecheck_cmd = ''
107 if len(commands) > 1:
108 filecheck_cmd = commands[1]
109 common.verify_filecheck_prefixes(filecheck_cmd)
110 if not llc_cmd.startswith('llc '):
111 common.warn('Skipping non-llc RUN line: ' + l)
112 continue
114 if not filecheck_cmd.startswith('FileCheck '):
115 common.warn('Skipping non-FileChecked RUN line: ' + l)
116 continue
118 llc_cmd_args = llc_cmd[len('llc'):].strip()
119 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
121 check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd)
122 for item in m.group(1).split(',')]
123 if not check_prefixes:
124 check_prefixes = ['CHECK']
126 # FIXME: We should use multiple check prefixes to common check lines. For
127 # now, we just ignore all but the last.
128 run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd, march_in_cmd))
130 func_dict = {}
131 for p in run_list:
132 prefixes = p[0]
133 for prefix in prefixes:
134 func_dict.update({prefix: dict()})
135 for prefixes, llc_args, triple_in_cmd, march_in_cmd in run_list:
136 if args.verbose:
137 print('Extracted LLC cmd: llc ' + llc_args, file=sys.stderr)
138 print('Extracted FileCheck prefixes: ' + str(prefixes), file=sys.stderr)
140 raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test)
141 triple = triple_in_cmd or triple_in_ir
142 if not triple:
143 triple = asm.get_triple_from_march(march_in_cmd)
145 asm.build_function_body_dictionary_for_triple(args, raw_tool_output,
146 triple, prefixes, func_dict)
148 is_in_function = False
149 is_in_function_start = False
150 func_name = None
151 prefix_set = set([prefix for p in run_list for prefix in p[0]])
152 if args.verbose:
153 print('Rewriting FileCheck prefixes: %s' % (prefix_set,), file=sys.stderr)
154 output_lines = []
155 output_lines.append(autogenerated_note)
157 for input_line in input_lines:
158 if is_in_function_start:
159 if input_line == '':
160 continue
161 if input_line.lstrip().startswith(';'):
162 m = common.CHECK_RE.match(input_line)
163 if not m or m.group(1) not in prefix_set:
164 output_lines.append(input_line)
165 continue
167 # Print out the various check lines here.
168 asm.add_asm_checks(output_lines, ';', run_list, func_dict, func_name)
169 is_in_function_start = False
171 if is_in_function:
172 if common.should_add_line_to_output(input_line, prefix_set):
173 # This input line of the function body will go as-is into the output.
174 output_lines.append(input_line)
175 else:
176 continue
177 if input_line.strip() == '}':
178 is_in_function = False
179 continue
181 # Discard any previous script advertising.
182 if input_line.startswith(ADVERT):
183 continue
185 # If it's outside a function, it just gets copied to the output.
186 output_lines.append(input_line)
188 m = common.IR_FUNCTION_RE.match(input_line)
189 if not m:
190 continue
191 func_name = m.group(1)
192 if args.function is not None and func_name != args.function:
193 # When filtering on a specific function, skip all others.
194 continue
195 is_in_function = is_in_function_start = True
197 if args.verbose:
198 print('Writing %d lines to %s...' % (len(output_lines), test), file=sys.stderr)
200 with open(test, 'wb') as f:
201 f.writelines(['{}\n'.format(l).encode('utf-8') for l in output_lines])
204 if __name__ == '__main__':
205 main()