1 #!/usr/bin/env python2.7
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.
11 import os # Used to advertise this file's name ("autogenerated_note").
17 from UpdateTestChecks import asm, common
19 ADVERT = '; NOTE: Assertions have been autogenerated by '
23 parser = argparse.ArgumentParser(description=__doc__)
24 parser.add_argument('-v', '--verbose', action='store_true',
25 help='Show verbose output')
26 parser.add_argument('--llc-binary', default='llc',
27 help='The "llc" binary to use to generate the test case')
29 '--function', help='The function in the test file to update')
31 '--extra_scrub', action='store_true',
32 help='Always use additional regex to further reduce diffs between various subtargets')
33 parser.add_argument('tests', nargs='+')
34 args = parser.parse_args()
36 autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__))
38 for test in args.tests:
40 print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,)
42 input_lines = [l.rstrip() for l in f]
46 m = common.TRIPLE_IR_RE.match(l)
48 triple_in_ir = m.groups()[0]
51 raw_lines = [m.group(1)
52 for m in [common.RUN_LINE_RE.match(l) for l in input_lines] if m]
53 run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
54 for l in raw_lines[1:]:
55 if run_lines[-1].endswith("\\"):
56 run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
61 print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),)
63 print >>sys.stderr, ' RUN: ' + l
67 commands = [cmd.strip() for cmd in l.split('|', 1)]
71 m = common.TRIPLE_ARG_RE.search(llc_cmd)
73 triple_in_cmd = m.groups()[0]
77 filecheck_cmd = commands[1]
78 if not llc_cmd.startswith('llc '):
79 print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l
82 if not filecheck_cmd.startswith('FileCheck '):
83 print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l
86 llc_cmd_args = llc_cmd[len('llc'):].strip()
87 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
89 check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd)
90 for item in m.group(1).split(',')]
91 if not check_prefixes:
92 check_prefixes = ['CHECK']
94 # FIXME: We should use multiple check prefixes to common check lines. For
95 # now, we just ignore all but the last.
96 run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd))
101 for prefix in prefixes:
102 func_dict.update({prefix: dict()})
103 for prefixes, llc_args, triple_in_cmd in run_list:
105 print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args
106 print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes)
108 raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test)
109 if not (triple_in_cmd or triple_in_ir):
110 print >>sys.stderr, "Cannot find a triple. Assume 'x86'"
112 asm.build_function_body_dictionary_for_triple(args, raw_tool_output,
113 triple_in_cmd or triple_in_ir or 'x86', prefixes, func_dict)
115 is_in_function = False
116 is_in_function_start = False
118 prefix_set = set([prefix for p in run_list for prefix in p[0]])
120 print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,)
122 output_lines.append(autogenerated_note)
124 for input_line in input_lines:
125 if is_in_function_start:
128 if input_line.lstrip().startswith(';'):
129 m = common.CHECK_RE.match(input_line)
130 if not m or m.group(1) not in prefix_set:
131 output_lines.append(input_line)
134 # Print out the various check lines here.
135 asm.add_asm_checks(output_lines, ';', run_list, func_dict, func_name)
136 is_in_function_start = False
139 if common.should_add_line_to_output(input_line, prefix_set):
140 # This input line of the function body will go as-is into the output.
141 output_lines.append(input_line)
144 if input_line.strip() == '}':
145 is_in_function = False
148 # Discard any previous script advertising.
149 if input_line.startswith(ADVERT):
152 # If it's outside a function, it just gets copied to the output.
153 output_lines.append(input_line)
155 m = common.IR_FUNCTION_RE.match(input_line)
158 func_name = m.group(1)
159 if args.function is not None and func_name != args.function:
160 # When filtering on a specific function, skip all others.
162 is_in_function = is_in_function_start = True
165 print>>sys.stderr, 'Writing %d lines to %s...' % (len(output_lines), test)
167 with open(test, 'wb') as f:
168 f.writelines([l + '\n' for l in output_lines])
171 if __name__ == '__main__':