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('--check-globals', action='store_true',
151                      help='Check global entries (global variables, metadata, attribute sets, ...) for functions')
152  parser.add_argument('tests', nargs='+')
153  args = common.parse_commandline_args(parser)
154  infer_dependent_args(args)
155
156  if not distutils.spawn.find_executable(args.clang):
157    print('Please specify --llvm-bin or --clang', file=sys.stderr)
158    sys.exit(1)
159
160  # Determine the builtin includes directory so that we can update tests that
161  # depend on the builtin headers. See get_clang_builtin_include_dir() and
162  # use_clang() in llvm/utils/lit/lit/llvm/config.py.
163  try:
164    builtin_include_dir = subprocess.check_output(
165      [args.clang, '-print-file-name=include']).decode().strip()
166    SUBST['%clang_cc1'] = ['-cc1', '-internal-isystem', builtin_include_dir,
167                           '-nostdsysteminc']
168  except subprocess.CalledProcessError:
169    common.warn('Could not determine clang builtins directory, some tests '
170                'might not update correctly.')
171
172  if not distutils.spawn.find_executable(args.opt):
173    # Many uses of this tool will not need an opt binary, because it's only
174    # needed for updating a test that runs clang | opt | FileCheck. So we
175    # defer this error message until we find that opt is actually needed.
176    args.opt = None
177
178  return args, parser
179
180
181def get_function_body(builder, args, filename, clang_args, extra_commands,
182                      prefixes):
183  # TODO Clean up duplication of asm/common build_function_body_dictionary
184  # Invoke external tool and extract function bodies.
185  raw_tool_output = common.invoke_tool(args.clang, clang_args, filename)
186  for extra_command in extra_commands:
187    extra_args = shlex.split(extra_command)
188    with tempfile.NamedTemporaryFile() as f:
189      f.write(raw_tool_output.encode())
190      f.flush()
191      if extra_args[0] == 'opt':
192        if args.opt is None:
193          print(filename, 'needs to run opt. '
194                'Please specify --llvm-bin or --opt', file=sys.stderr)
195          sys.exit(1)
196        extra_args[0] = args.opt
197      raw_tool_output = common.invoke_tool(extra_args[0],
198                                           extra_args[1:], f.name)
199  if '-emit-llvm' in clang_args:
200    builder.process_run_line(
201            common.OPT_FUNCTION_RE, common.scrub_body, raw_tool_output,
202            prefixes)
203  else:
204    print('The clang command line should include -emit-llvm as asm tests '
205          'are discouraged in Clang testsuite.', file=sys.stderr)
206    sys.exit(1)
207
208def exec_run_line(exe):
209  popen = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
210  stdout, stderr = popen.communicate()
211  if popen.returncode != 0:
212    sys.stderr.write('Failed to run ' + ' '.join(exe) + '\n')
213    sys.stderr.write(stderr)
214    sys.stderr.write(stdout)
215    sys.exit(3)
216
217def main():
218  initial_args, parser = config()
219  script_name = os.path.basename(__file__)
220
221  for ti in common.itertests(initial_args.tests, parser, 'utils/' + script_name,
222                             comment_prefix='//', argparse_callback=infer_dependent_args):
223    # Build a list of filechecked and non-filechecked RUN lines.
224    run_list = []
225    line2spell_and_mangled_list = collections.defaultdict(list)
226
227    subs = {
228      '%s' : ti.path,
229      '%t' : tempfile.NamedTemporaryFile().name,
230      '%S' : os.getcwd(),
231    }
232
233    for l in ti.run_lines:
234      commands = [cmd.strip() for cmd in l.split('|')]
235
236      triple_in_cmd = None
237      m = common.TRIPLE_ARG_RE.search(commands[0])
238      if m:
239        triple_in_cmd = m.groups()[0]
240
241      # Parse executable args.
242      exec_args = shlex.split(commands[0])
243      # Execute non-clang runline.
244      if exec_args[0] not in SUBST:
245        # Do lit-like substitutions.
246        for s in subs:
247          exec_args = [i.replace(s, subs[s]) if s in i else i for i in exec_args]
248        run_list.append((None, exec_args, None, None))
249        continue
250      # This is a clang runline, apply %clang substitution rule, do lit-like substitutions,
251      # and append args.clang_args
252      clang_args = exec_args
253      clang_args[0:1] = SUBST[clang_args[0]]
254      for s in subs:
255        clang_args = [i.replace(s, subs[s]) if s in i else i for i in clang_args]
256      clang_args += ti.args.clang_args
257
258      # Extract -check-prefix in FileCheck args
259      filecheck_cmd = commands[-1]
260      common.verify_filecheck_prefixes(filecheck_cmd)
261      if not filecheck_cmd.startswith('FileCheck '):
262        # Execute non-filechecked clang runline.
263        exe = [ti.args.clang] + clang_args
264        run_list.append((None, exe, None, None))
265        continue
266
267      check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd)
268                               for item in m.group(1).split(',')]
269      if not check_prefixes:
270        check_prefixes = ['CHECK']
271      run_list.append((check_prefixes, clang_args, commands[1:-1], triple_in_cmd))
272
273    # Execute clang, generate LLVM IR, and extract functions.
274
275    # Store only filechecked runlines.
276    filecheck_run_list = [i for i in run_list if i[0]]
277    builder = common.FunctionTestBuilder(
278      run_list=filecheck_run_list,
279      flags=ti.args,
280      scrubber_args=[],
281      path=ti.path)
282
283    for prefixes, args, extra_commands, triple_in_cmd in run_list:
284      # Execute non-filechecked runline.
285      if not prefixes:
286        print('NOTE: Executing non-FileChecked RUN line: ' + ' '.join(args), file=sys.stderr)
287        exec_run_line(args)
288        continue
289
290      clang_args = args
291      common.debug('Extracted clang cmd: clang {}'.format(clang_args))
292      common.debug('Extracted FileCheck prefixes: {}'.format(prefixes))
293
294      get_function_body(builder, ti.args, ti.path, clang_args, extra_commands,
295                        prefixes)
296
297      # Invoke clang -Xclang -ast-dump=json to get mapping from start lines to
298      # mangled names. Forward all clang args for now.
299      for k, v in get_line2spell_and_mangled(ti.args, clang_args).items():
300        line2spell_and_mangled_list[k].append(v)
301
302    func_dict = builder.finish_and_get_func_dict()
303    global_vars_seen_dict = {}
304    prefix_set = set([prefix for p in filecheck_run_list for prefix in p[0]])
305    output_lines = []
306    has_checked_pre_function_globals = False
307
308    include_generated_funcs = common.find_arg_in_test(ti,
309                                                      lambda args: ti.args.include_generated_funcs,
310                                                      '--include-generated-funcs',
311                                                      True)
312
313    if include_generated_funcs:
314      # Generate the appropriate checks for each function.  We need to emit
315      # these in the order according to the generated output so that CHECK-LABEL
316      # works properly.  func_order provides that.
317
318      # It turns out that when clang generates functions (for example, with
319      # -fopenmp), it can sometimes cause functions to be re-ordered in the
320      # output, even functions that exist in the source file.  Therefore we
321      # can't insert check lines before each source function and instead have to
322      # put them at the end.  So the first thing to do is dump out the source
323      # lines.
324      common.dump_input_lines(output_lines, ti, prefix_set, '//')
325
326      # Now generate all the checks.
327      def check_generator(my_output_lines, prefixes, func):
328        if '-emit-llvm' in clang_args:
329          common.add_ir_checks(my_output_lines, '//',
330                               prefixes,
331                               func_dict, func, False,
332                               ti.args.function_signature,
333                               global_vars_seen_dict)
334        else:
335          asm.add_asm_checks(my_output_lines, '//',
336                             prefixes,
337                             func_dict, func)
338
339      if ti.args.check_globals:
340        common.add_global_checks(builder.global_var_dict(), '//', run_list,
341                                 output_lines, global_vars_seen_dict, True,
342                                 True)
343      common.add_checks_at_end(output_lines, filecheck_run_list, builder.func_order(),
344                               '//', lambda my_output_lines, prefixes, func:
345                               check_generator(my_output_lines,
346                                               prefixes, func))
347    else:
348      # Normal mode.  Put checks before each source function.
349      for line_info in ti.iterlines(output_lines):
350        idx = line_info.line_number
351        line = line_info.line
352        args = line_info.args
353        include_line = True
354        m = common.CHECK_RE.match(line)
355        if m and m.group(1) in prefix_set:
356          continue  # Don't append the existing CHECK lines
357        # Skip special separator comments added by commmon.add_global_checks.
358        if line.strip() == '//' + common.SEPARATOR:
359          continue
360        if idx in line2spell_and_mangled_list:
361          added = set()
362          for spell, mangled in line2spell_and_mangled_list[idx]:
363            # One line may contain multiple function declarations.
364            # Skip if the mangled name has been added before.
365            # The line number may come from an included file,
366            # we simply require the spelling name to appear on the line
367            # to exclude functions from other files.
368            if mangled in added or spell not in line:
369              continue
370            if args.functions is None or any(re.search(regex, spell) for regex in args.functions):
371              last_line = output_lines[-1].strip()
372              while last_line == '//':
373                # Remove the comment line since we will generate a new  comment
374                # line as part of common.add_ir_checks()
375                output_lines.pop()
376                last_line = output_lines[-1].strip()
377              if ti.args.check_globals and not has_checked_pre_function_globals:
378                common.add_global_checks(builder.global_var_dict(), '//',
379                                         run_list, output_lines,
380                                         global_vars_seen_dict, True, True)
381                has_checked_pre_function_globals = True
382              if added:
383                output_lines.append('//')
384              added.add(mangled)
385              common.add_ir_checks(output_lines, '//', filecheck_run_list, func_dict, mangled,
386                                   False, args.function_signature, global_vars_seen_dict)
387              if line.rstrip('\n') == '//':
388                include_line = False
389
390        if include_line:
391          output_lines.append(line.rstrip('\n'))
392
393    if ti.args.check_globals:
394      common.add_global_checks(builder.global_var_dict(), '//', run_list,
395                               output_lines, global_vars_seen_dict, True, False)
396    common.debug('Writing %d lines to %s...' % (len(output_lines), ti.path))
397    with open(ti.path, 'wb') as f:
398      f.writelines(['{}\n'.format(l).encode('utf-8') for l in output_lines])
399
400  return 0
401
402
403if __name__ == '__main__':
404  sys.exit(main())
405