1#!/usr/bin/env python 2'''A utility to update LLVM IR CHECK lines in C/C++ FileCheck test files. 3 4Example RUN lines in .c/.cc test files: 5 6// RUN: %clang -emit-llvm -S %s -o - -O2 | FileCheck %s 7// RUN: %clangxx -emit-llvm -S %s -o - -O2 | FileCheck -check-prefix=CHECK-A %s 8 9Usage: 10 11% utils/update_cc_test_checks.py --llvm-bin=release/bin test/a.cc 12% utils/update_cc_test_checks.py --clang=release/bin/clang /tmp/c/a.cc 13''' 14 15from __future__ import print_function 16 17import argparse 18import collections 19import distutils.spawn 20import json 21import os 22import re 23import shlex 24import subprocess 25import sys 26import tempfile 27 28from UpdateTestChecks import common 29 30SUBST = { 31 '%clang': [], 32 '%clang_cc1': ['-cc1'], 33 '%clangxx': ['--driver-mode=g++'], 34} 35 36def get_line2spell_and_mangled(args, clang_args): 37 ret = {} 38 # Use clang's JSON AST dump to get the mangled name 39 json_dump_args = [args.clang] + clang_args + ['-fsyntax-only', '-o', '-'] 40 if '-cc1' not in json_dump_args: 41 # For tests that invoke %clang instead if %clang_cc1 we have to use 42 # -Xclang -ast-dump=json instead: 43 json_dump_args.append('-Xclang') 44 json_dump_args.append('-ast-dump=json') 45 common.debug('Running', ' '.join(json_dump_args)) 46 47 popen = subprocess.Popen(json_dump_args, stdout=subprocess.PIPE, 48 stderr=subprocess.PIPE, universal_newlines=True) 49 stdout, stderr = popen.communicate() 50 if popen.returncode != 0: 51 sys.stderr.write('Failed to run ' + ' '.join(json_dump_args) + '\n') 52 sys.stderr.write(stderr) 53 sys.stderr.write(stdout) 54 sys.exit(2) 55 56 # Parse the clang JSON and add all children of type FunctionDecl. 57 # TODO: Should we add checks for global variables being emitted? 58 def parse_clang_ast_json(node): 59 node_kind = node['kind'] 60 # Recurse for the following nodes that can contain nested function decls: 61 if node_kind in ('NamespaceDecl', 'LinkageSpecDecl', 'TranslationUnitDecl', 62 'CXXRecordDecl'): 63 if 'inner' in node: 64 for inner in node['inner']: 65 parse_clang_ast_json(inner) 66 # Otherwise we ignore everything except functions: 67 if node_kind not in ('FunctionDecl', 'CXXMethodDecl', 'CXXConstructorDecl', 68 'CXXDestructorDecl', 'CXXConversionDecl'): 69 return 70 if node.get('isImplicit') is True and node.get('storageClass') == 'extern': 71 common.debug('Skipping builtin function:', node['name'], '@', node['loc']) 72 return 73 common.debug('Found function:', node['kind'], node['name'], '@', node['loc']) 74 line = node['loc'].get('line') 75 # If there is no line it is probably a builtin function -> skip 76 if line is None: 77 common.debug('Skipping function without line number:', node['name'], '@', node['loc']) 78 return 79 80 # If there is no 'inner' object, it is a function declaration and we can 81 # skip it. However, function declarations may also contain an 'inner' list, 82 # but in that case it will only contains ParmVarDecls. If we find an entry 83 # that is not a ParmVarDecl, we know that this is a function definition. 84 has_body = False 85 if 'inner' in node: 86 for i in node['inner']: 87 if i.get('kind', 'ParmVarDecl') != 'ParmVarDecl': 88 has_body = True 89 break 90 if not has_body: 91 common.debug('Skipping function without body:', node['name'], '@', node['loc']) 92 return 93 spell = node['name'] 94 mangled = node.get('mangledName', spell) 95 ret[int(line)-1] = (spell, mangled) 96 97 ast = json.loads(stdout) 98 if ast['kind'] != 'TranslationUnitDecl': 99 common.error('Clang AST dump JSON format changed?') 100 sys.exit(2) 101 parse_clang_ast_json(ast) 102 103 for line, func_name in sorted(ret.items()): 104 common.debug('line {}: found function {}'.format(line+1, func_name), file=sys.stderr) 105 if not ret: 106 common.warn('Did not find any functions using', ' '.join(json_dump_args)) 107 return ret 108 109 110def str_to_commandline(value): 111 if not value: 112 return [] 113 return shlex.split(value) 114 115 116def infer_dependent_args(args): 117 if not args.clang: 118 if not args.llvm_bin: 119 args.clang = 'clang' 120 else: 121 args.clang = os.path.join(args.llvm_bin, 'clang') 122 if not args.opt: 123 if not args.llvm_bin: 124 args.opt = 'opt' 125 else: 126 args.opt = os.path.join(args.llvm_bin, 'opt') 127 128 129def config(): 130 parser = argparse.ArgumentParser( 131 description=__doc__, 132 formatter_class=argparse.RawTextHelpFormatter) 133 parser.add_argument('--llvm-bin', help='llvm $prefix/bin path') 134 parser.add_argument('--clang', 135 help='"clang" executable, defaults to $llvm_bin/clang') 136 parser.add_argument('--clang-args', default=[], type=str_to_commandline, 137 help='Space-separated extra args to clang, e.g. --clang-args=-v') 138 parser.add_argument('--opt', 139 help='"opt" executable, defaults to $llvm_bin/opt') 140 parser.add_argument( 141 '--functions', nargs='+', help='A list of function name regexes. ' 142 'If specified, update CHECK lines for functions matching at least one regex') 143 parser.add_argument( 144 '--x86_extra_scrub', action='store_true', 145 help='Use more regex for x86 matching to reduce diffs between various subtargets') 146 parser.add_argument('--function-signature', action='store_true', 147 help='Keep function signature information around for the check line') 148 parser.add_argument('--check-attributes', action='store_true', 149 help='Check "Function Attributes" for functions') 150 parser.add_argument('tests', nargs='+') 151 args = common.parse_commandline_args(parser) 152 infer_dependent_args(args) 153 154 if not distutils.spawn.find_executable(args.clang): 155 print('Please specify --llvm-bin or --clang', file=sys.stderr) 156 sys.exit(1) 157 158 # Determine the builtin includes directory so that we can update tests that 159 # depend on the builtin headers. See get_clang_builtin_include_dir() and 160 # use_clang() in llvm/utils/lit/lit/llvm/config.py. 161 try: 162 builtin_include_dir = subprocess.check_output( 163 [args.clang, '-print-file-name=include']).decode().strip() 164 SUBST['%clang_cc1'] = ['-cc1', '-internal-isystem', builtin_include_dir, 165 '-nostdsysteminc'] 166 except subprocess.CalledProcessError: 167 common.warn('Could not determine clang builtins directory, some tests ' 168 'might not update correctly.') 169 170 if not distutils.spawn.find_executable(args.opt): 171 # Many uses of this tool will not need an opt binary, because it's only 172 # needed for updating a test that runs clang | opt | FileCheck. So we 173 # defer this error message until we find that opt is actually needed. 174 args.opt = None 175 176 return args, parser 177 178 179def get_function_body(args, filename, clang_args, extra_commands, prefixes, 180 triple_in_cmd, func_dict, func_order): 181 # TODO Clean up duplication of asm/common build_function_body_dictionary 182 # Invoke external tool and extract function bodies. 183 raw_tool_output = common.invoke_tool(args.clang, clang_args, filename) 184 for extra_command in extra_commands: 185 extra_args = shlex.split(extra_command) 186 with tempfile.NamedTemporaryFile() as f: 187 f.write(raw_tool_output.encode()) 188 f.flush() 189 if extra_args[0] == 'opt': 190 if args.opt is None: 191 print(filename, 'needs to run opt. ' 192 'Please specify --llvm-bin or --opt', file=sys.stderr) 193 sys.exit(1) 194 extra_args[0] = args.opt 195 raw_tool_output = common.invoke_tool(extra_args[0], 196 extra_args[1:], f.name) 197 if '-emit-llvm' in clang_args: 198 common.build_function_body_dictionary( 199 common.OPT_FUNCTION_RE, common.scrub_body, [], 200 raw_tool_output, prefixes, func_dict, func_order, args.verbose, 201 args.function_signature, args.check_attributes) 202 else: 203 print('The clang command line should include -emit-llvm as asm tests ' 204 'are discouraged in Clang testsuite.', file=sys.stderr) 205 sys.exit(1) 206 207 208def main(): 209 initial_args, parser = config() 210 script_name = os.path.basename(__file__) 211 212 for ti in common.itertests(initial_args.tests, parser, 'utils/' + script_name, 213 comment_prefix='//', argparse_callback=infer_dependent_args): 214 # Build a list of clang command lines and check prefixes from RUN lines. 215 run_list = [] 216 line2spell_and_mangled_list = collections.defaultdict(list) 217 for l in ti.run_lines: 218 commands = [cmd.strip() for cmd in l.split('|')] 219 220 triple_in_cmd = None 221 m = common.TRIPLE_ARG_RE.search(commands[0]) 222 if m: 223 triple_in_cmd = m.groups()[0] 224 225 # Apply %clang substitution rule, replace %s by `filename`, and append args.clang_args 226 clang_args = shlex.split(commands[0]) 227 if clang_args[0] not in SUBST: 228 print('WARNING: Skipping non-clang RUN line: ' + l, file=sys.stderr) 229 continue 230 clang_args[0:1] = SUBST[clang_args[0]] 231 clang_args = [ti.path if i == '%s' else i for i in clang_args] + ti.args.clang_args 232 233 # Permit piping the output through opt 234 if not (len(commands) == 2 or 235 (len(commands) == 3 and commands[1].startswith('opt'))): 236 print('WARNING: Skipping non-clang RUN line: ' + l, file=sys.stderr) 237 238 # Extract -check-prefix in FileCheck args 239 filecheck_cmd = commands[-1] 240 common.verify_filecheck_prefixes(filecheck_cmd) 241 if not filecheck_cmd.startswith('FileCheck '): 242 print('WARNING: Skipping non-FileChecked RUN line: ' + l, file=sys.stderr) 243 continue 244 check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd) 245 for item in m.group(1).split(',')] 246 if not check_prefixes: 247 check_prefixes = ['CHECK'] 248 run_list.append((check_prefixes, clang_args, commands[1:-1], triple_in_cmd)) 249 250 # Execute clang, generate LLVM IR, and extract functions. 251 func_dict = {} 252 func_order = {} 253 for p in run_list: 254 prefixes = p[0] 255 for prefix in prefixes: 256 func_dict.update({prefix: dict()}) 257 func_order.update({prefix: []}) 258 for prefixes, clang_args, extra_commands, triple_in_cmd in run_list: 259 common.debug('Extracted clang cmd: clang {}'.format(clang_args)) 260 common.debug('Extracted FileCheck prefixes: {}'.format(prefixes)) 261 262 get_function_body(ti.args, ti.path, clang_args, extra_commands, prefixes, 263 triple_in_cmd, func_dict, func_order) 264 265 # Invoke clang -Xclang -ast-dump=json to get mapping from start lines to 266 # mangled names. Forward all clang args for now. 267 for k, v in get_line2spell_and_mangled(ti.args, clang_args).items(): 268 line2spell_and_mangled_list[k].append(v) 269 270 global_vars_seen_dict = {} 271 prefix_set = set([prefix for p in run_list for prefix in p[0]]) 272 output_lines = [] 273 274 include_generated_funcs = common.find_arg_in_test(ti, 275 lambda args: ti.args.include_generated_funcs, 276 '--include-generated-funcs', 277 True) 278 279 if include_generated_funcs: 280 # Generate the appropriate checks for each function. We need to emit 281 # these in the order according to the generated output so that CHECK-LABEL 282 # works properly. func_order provides that. 283 284 # It turns out that when clang generates functions (for example, with 285 # -fopenmp), it can sometimes cause functions to be re-ordered in the 286 # output, even functions that exist in the source file. Therefore we 287 # can't insert check lines before each source function and instead have to 288 # put them at the end. So the first thing to do is dump out the source 289 # lines. 290 common.dump_input_lines(output_lines, ti, prefix_set, '//') 291 292 # Now generate all the checks. 293 def check_generator(my_output_lines, prefixes, func): 294 if '-emit-llvm' in clang_args: 295 common.add_ir_checks(my_output_lines, '//', 296 prefixes, 297 func_dict, func, False, 298 ti.args.function_signature, 299 global_vars_seen_dict) 300 else: 301 asm.add_asm_checks(my_output_lines, '//', 302 prefixes, 303 func_dict, func) 304 305 common.add_checks_at_end(output_lines, run_list, func_order, '//', 306 lambda my_output_lines, prefixes, func: 307 check_generator(my_output_lines, 308 prefixes, func)) 309 else: 310 # Normal mode. Put checks before each source function. 311 for line_info in ti.iterlines(output_lines): 312 idx = line_info.line_number 313 line = line_info.line 314 args = line_info.args 315 include_line = True 316 m = common.CHECK_RE.match(line) 317 if m and m.group(1) in prefix_set: 318 continue # Don't append the existing CHECK lines 319 if idx in line2spell_and_mangled_list: 320 added = set() 321 for spell, mangled in line2spell_and_mangled_list[idx]: 322 # One line may contain multiple function declarations. 323 # Skip if the mangled name has been added before. 324 # The line number may come from an included file, 325 # we simply require the spelling name to appear on the line 326 # to exclude functions from other files. 327 if mangled in added or spell not in line: 328 continue 329 if args.functions is None or any(re.search(regex, spell) for regex in args.functions): 330 last_line = output_lines[-1].strip() 331 while last_line == '//': 332 # Remove the comment line since we will generate a new comment 333 # line as part of common.add_ir_checks() 334 output_lines.pop() 335 last_line = output_lines[-1].strip() 336 if added: 337 output_lines.append('//') 338 added.add(mangled) 339 common.add_ir_checks(output_lines, '//', run_list, func_dict, mangled, 340 False, args.function_signature, global_vars_seen_dict) 341 if line.rstrip('\n') == '//': 342 include_line = False 343 344 if include_line: 345 output_lines.append(line.rstrip('\n')) 346 347 common.debug('Writing %d lines to %s...' % (len(output_lines), ti.path)) 348 with open(ti.path, 'wb') as f: 349 f.writelines(['{}\n'.format(l).encode('utf-8') for l in output_lines]) 350 351 return 0 352 353 354if __name__ == '__main__': 355 sys.exit(main()) 356