Index: utils/update_cc_test_checks.py =================================================================== --- /dev/null +++ utils/update_cc_test_checks.py @@ -0,0 +1,214 @@ +#!/usr/bin/env python3 +'''A utility to update LLVM IR or assembly CHECK lines in C/C++ FileCheck test files. + +Example RUN lines in .c/.cc test files: + +// RUN: %clang -S %s -o - -O2 | FileCheck %s +// RUN: %clang -emit-llvm -S %s -o - -O2 | FileCheck %s + +Usage: + +% utils/update_cc_test_checks.py --llvm-bin=release/bin test/a.cc +% utils/update_cc_test_checks.py --c-index-test=release/bin/c-index-test \ + --clang=release/bin/clang /tmp/c/a.cc +''' + +import argparse +import distutils.spawn +import os # Used to advertise this file's name ("autogenerated_note"). +import shlex +import string +import subprocess +import sys +import re +import tempfile + +from UpdateTestChecks import asm, common + +ADVERT = '// NOTE: Assertions have been autogenerated by ' + +CHECK_RE = re.compile(r'^\s*//\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:') +RUN_LINE_RE = re.compile('^//\s*RUN:\s*(.*)$') + +def get_line2spell_and_mangled(args, filename, clang_args): + ret = {} + with tempfile.NamedTemporaryFile() as f: + # TODO Make c-index-test print mangled names without circumventing through precompiled headers + subprocess.check_call([args.c_index_test, + '-write-pch', f.name, filename, *shlex.split(clang_args), *args.c_index_test_args]) + output = subprocess.check_output([args.c_index_test, + '-test-print-mangle', f.name]) + if sys.version_info[0] > 2: + output = output.decode() + + RE = re.compile(r'^FunctionDecl=(\w+):(\d+):\d+ \(Definition\) \[mangled=([^]]+)\]') + for line in output.splitlines(): + m = RE.match(line) + if not m: continue + spell, line, mangled = m.groups() + ret[int(line)-1] = (spell, mangled) + if args.verbose: + for line, func_name in ret.items(): + print('line {}: found function {}'.format(line+1, func_name), file=sys.stderr) + return ret + + +def main(): + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawTextHelpFormatter) + parser.add_argument('-v', '--verbose', action='store_true') + parser.add_argument('--llvm-bin', help='llvm $prefix/bin path') + parser.add_argument('--clang', + help='"clang" executable, defaults to $llvm_bin/clang') + parser.add_argument('--clang-args', + help='Space-separated extra args to clang') + parser.add_argument('--c-index-test', + help='"c-index-test" executable, defaults to $llvm_bin/c-index-test') + parser.add_argument('--c-index-test-args', + help='Space-separated extra args to c-index-test, e.g. -resource-dir clang/7.0.0') + parser.add_argument( + '--functions', nargs='+', help='A list of function name regexes. ' + 'If specified, update CHECK lines for functions matching at least one regex') + parser.add_argument( + '--x86_extra_scrub', action='store_true', + help='Use more regex for x86 matching to reduce diffs between various subtargets') + parser.add_argument('tests', nargs='+') + args = parser.parse_args() + args.c_index_test_args = shlex.split(args.c_index_test_args or '') + + if args.clang is None: + if args.llvm_bin is None: + args.clang = 'clang' + else: + args.clang = os.path.join(args.llvm_bin, 'clang') + if not distutils.spawn.find_executable(args.clang): + print('Please specify --llvm-bin or --clang-exe', file=sys.stderr) + return 1 + if args.c_index_test is None: + if args.llvm_bin is None: + args.c_index_test = 'c-index-test' + else: + args.c_index_test = os.path.join(args.llvm_bin, 'c-index-test') + if not distutils.spawn.find_executable(args.c_index_test): + print('Please specify --llvm-bin or --c-index-test-exe', file=sys.stderr) + return 1 + + autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__)) + + for filename in args.tests: + with open(filename) as f: + input_lines = [l.rstrip() for l in f] + + # Parse RUN lines and get `prefix_set`. + raw_lines = [m.group(1) + for m in [RUN_LINE_RE.match(l) for l in input_lines] if m] + run_lines = [raw_lines[0]] if len(raw_lines) > 0 else [] + for l in raw_lines[1:]: + if run_lines[-1].endswith("\\"): + run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l + else: + run_lines.append(l) + + if args.verbose: + print('Found {} RUN lines:'.format(len(run_lines)), file=sys.stderr) + for l in run_lines: + print(' RUN: ' + l, file=sys.stderr) + + # Build a list of clang command lines from RUN lines. + run_list = [] + line2spell_and_mangled = {} + for l in run_lines: + commands = [cmd.strip() for cmd in l.split('|', 1)] + clang_cmd = commands[0] + + triple_in_cmd = None + m = common.TRIPLE_ARG_RE.search(clang_cmd) + if m: + triple_in_cmd = m.groups()[0] + + filecheck_cmd = commands[-1] + if clang_cmd.startswith('%clang '): + clang_args = clang_cmd[len('%clang '):].strip() + #clang_args = clang_cmd[len('%clang '):].replace('%s', test).strip() + elif clang_cmd.startswith('%clang_cc1 '): + #clang_args = clang_cmd.replace('%clang_cc1 ', '-cc1 ').replace('%s', test).strip() + clang_args = clang_cmd.replace('%clang_cc1 ', '-cc1 ').strip() + else: + print('WARNING: Skipping non-clang RUN line: ' + l, file=sys.stderr) + continue + + if not filecheck_cmd.startswith('FileCheck '): + print('WARNING: Skipping non-FileChecked RUN line: ' + l, file=sys.stderr) + continue + + # Invoke c-index-test to get mapping from start lines to mangled names. + # Forward all clang args for now. + line2spell_and_mangled.update(get_line2spell_and_mangled(args, filename, clang_args)) + + check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd) + for item in m.group(1).split(',')] + if not check_prefixes: + check_prefixes = ['CHECK'] + run_list.append((check_prefixes, clang_args.replace('%s', filename), triple_in_cmd)) + + # Execute clang, generate assembly, and extract functions. + func_dict = {} + for p in run_list: + prefixes = p[0] + for prefix in prefixes: + func_dict.update({prefix: dict()}) + for prefixes, clang_args, triple_in_cmd in run_list: + if args.verbose: + print('Extracted clang cmd: clang {}'.format(clang_args), file=sys.stderr) + print('Extracted FileCheck prefixes: {}'.format(prefixes), file=sys.stderr) + + if args.clang_args: + clang_args += ' ' + args.clang_args + raw_tool_output = common.invoke_tool(args.clang, + clang_args, + filename) + + if '-emit-llvm' in clang_args: + common.build_function_body_dictionary( + common.OPT_FUNCTION_RE, common.scrub_body, [], + raw_tool_output, prefixes, func_dict, args.verbose) + else: + asm.build_function_body_dictionary_for_triple(args, raw_tool_output, + triple_in_cmd or 'x86', prefixes, func_dict) + + # Strip CHECK lines which are in `prefix_set`, update test file. + prefix_set = set([prefix for p in run_list for prefix in p[0]]) + input_lines = [] + with open(filename, 'r+') as f: + for line in f: + m = CHECK_RE.match(line) + if not (m and m.group(1) in prefix_set) and line != '//\n': + input_lines.append(line) + f.seek(0) + f.writelines(input_lines) + f.truncate() + + output_lines = [autogenerated_note] + for idx, line in enumerate(input_lines): + # Discard any previous script advertising. + if line.startswith(ADVERT): + continue + if idx in line2spell_and_mangled: + spell, mangled = line2spell_and_mangled[idx] + # If --functions is unspecified or one regex matches the spelling name. + if args.functions is None or any(re.search(regex, spell) + for regex in args.functions): + asm.add_asm_checks(output_lines, '//', run_list, func_dict, mangled) + output_lines.append(line.rstrip('\n')) + + # Update the test file. + with open(filename, 'w') as f: + for line in output_lines: + f.write(line + '\n') + + return 0 + + +if __name__ == '__main__': + sys.exit(main())