xref: /netbsd-src/external/apache2/llvm/dist/llvm/utils/update_cc_test_checks.py (revision 82d56013d7b633d116a93943de88e08335357a7c)
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(builder, args, filename, clang_args, extra_commands,
180                      prefixes):
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    builder.process_run_line(
199            common.OPT_FUNCTION_RE, common.scrub_body, raw_tool_output,
200            prefixes)
201  else:
202    print('The clang command line should include -emit-llvm as asm tests '
203          'are discouraged in Clang testsuite.', file=sys.stderr)
204    sys.exit(1)
205
206def exec_run_line(exe):
207  popen = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
208  stdout, stderr = popen.communicate()
209  if popen.returncode != 0:
210    sys.stderr.write('Failed to run ' + ' '.join(exe) + '\n')
211    sys.stderr.write(stderr)
212    sys.stderr.write(stdout)
213    sys.exit(3)
214
215def main():
216  initial_args, parser = config()
217  script_name = os.path.basename(__file__)
218
219  for ti in common.itertests(initial_args.tests, parser, 'utils/' + script_name,
220                             comment_prefix='//', argparse_callback=infer_dependent_args):
221    # Build a list of filechecked and non-filechecked RUN lines.
222    run_list = []
223    line2spell_and_mangled_list = collections.defaultdict(list)
224
225    subs = {
226      '%s' : ti.path,
227      '%t' : tempfile.NamedTemporaryFile().name,
228      '%S' : os.getcwd(),
229    }
230
231    for l in ti.run_lines:
232      commands = [cmd.strip() for cmd in l.split('|')]
233
234      triple_in_cmd = None
235      m = common.TRIPLE_ARG_RE.search(commands[0])
236      if m:
237        triple_in_cmd = m.groups()[0]
238
239      # Parse executable args.
240      exec_args = shlex.split(commands[0])
241      # Execute non-clang runline.
242      if exec_args[0] not in SUBST:
243        # Do lit-like substitutions.
244        for s in subs:
245          exec_args = [i.replace(s, subs[s]) if s in i else i for i in exec_args]
246        run_list.append((None, exec_args, None, None))
247        continue
248      # This is a clang runline, apply %clang substitution rule, do lit-like substitutions,
249      # and append args.clang_args
250      clang_args = exec_args
251      clang_args[0:1] = SUBST[clang_args[0]]
252      for s in subs:
253        clang_args = [i.replace(s, subs[s]) if s in i else i for i in clang_args]
254      clang_args += ti.args.clang_args
255
256      # Extract -check-prefix in FileCheck args
257      filecheck_cmd = commands[-1]
258      common.verify_filecheck_prefixes(filecheck_cmd)
259      if not filecheck_cmd.startswith('FileCheck '):
260        # Execute non-filechecked clang runline.
261        exe = [ti.args.clang] + clang_args
262        run_list.append((None, exe, None, None))
263        continue
264
265      check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd)
266                               for item in m.group(1).split(',')]
267      if not check_prefixes:
268        check_prefixes = ['CHECK']
269      run_list.append((check_prefixes, clang_args, commands[1:-1], triple_in_cmd))
270
271    # Execute clang, generate LLVM IR, and extract functions.
272
273    # Store only filechecked runlines.
274    filecheck_run_list = [i for i in run_list if i[0]]
275    builder = common.FunctionTestBuilder(
276      run_list=filecheck_run_list,
277      flags=ti.args,
278      scrubber_args=[])
279
280    for prefixes, args, extra_commands, triple_in_cmd in run_list:
281      # Execute non-filechecked runline.
282      if not prefixes:
283        print('NOTE: Executing non-FileChecked RUN line: ' + ' '.join(args), file=sys.stderr)
284        exec_run_line(args)
285        continue
286
287      clang_args = args
288      common.debug('Extracted clang cmd: clang {}'.format(clang_args))
289      common.debug('Extracted FileCheck prefixes: {}'.format(prefixes))
290
291      get_function_body(builder, ti.args, ti.path, clang_args, extra_commands,
292                        prefixes)
293
294      # Invoke clang -Xclang -ast-dump=json to get mapping from start lines to
295      # mangled names. Forward all clang args for now.
296      for k, v in get_line2spell_and_mangled(ti.args, clang_args).items():
297        line2spell_and_mangled_list[k].append(v)
298
299    func_dict = builder.finish_and_get_func_dict()
300    global_vars_seen_dict = {}
301    prefix_set = set([prefix for p in filecheck_run_list for prefix in p[0]])
302    output_lines = []
303
304    include_generated_funcs = common.find_arg_in_test(ti,
305                                                      lambda args: ti.args.include_generated_funcs,
306                                                      '--include-generated-funcs',
307                                                      True)
308
309    if include_generated_funcs:
310      # Generate the appropriate checks for each function.  We need to emit
311      # these in the order according to the generated output so that CHECK-LABEL
312      # works properly.  func_order provides that.
313
314      # It turns out that when clang generates functions (for example, with
315      # -fopenmp), it can sometimes cause functions to be re-ordered in the
316      # output, even functions that exist in the source file.  Therefore we
317      # can't insert check lines before each source function and instead have to
318      # put them at the end.  So the first thing to do is dump out the source
319      # lines.
320      common.dump_input_lines(output_lines, ti, prefix_set, '//')
321
322      # Now generate all the checks.
323      def check_generator(my_output_lines, prefixes, func):
324        if '-emit-llvm' in clang_args:
325          common.add_ir_checks(my_output_lines, '//',
326                               prefixes,
327                               func_dict, func, False,
328                               ti.args.function_signature,
329                               global_vars_seen_dict)
330        else:
331          asm.add_asm_checks(my_output_lines, '//',
332                             prefixes,
333                             func_dict, func)
334
335      common.add_checks_at_end(output_lines, filecheck_run_list, builder.func_order(),
336                               '//', lambda my_output_lines, prefixes, func:
337                               check_generator(my_output_lines,
338                                               prefixes, func))
339    else:
340      # Normal mode.  Put checks before each source function.
341      for line_info in ti.iterlines(output_lines):
342        idx = line_info.line_number
343        line = line_info.line
344        args = line_info.args
345        include_line = True
346        m = common.CHECK_RE.match(line)
347        if m and m.group(1) in prefix_set:
348          continue  # Don't append the existing CHECK lines
349        if idx in line2spell_and_mangled_list:
350          added = set()
351          for spell, mangled in line2spell_and_mangled_list[idx]:
352            # One line may contain multiple function declarations.
353            # Skip if the mangled name has been added before.
354            # The line number may come from an included file,
355            # we simply require the spelling name to appear on the line
356            # to exclude functions from other files.
357            if mangled in added or spell not in line:
358              continue
359            if args.functions is None or any(re.search(regex, spell) for regex in args.functions):
360              last_line = output_lines[-1].strip()
361              while last_line == '//':
362                # Remove the comment line since we will generate a new  comment
363                # line as part of common.add_ir_checks()
364                output_lines.pop()
365                last_line = output_lines[-1].strip()
366              if added:
367                output_lines.append('//')
368              added.add(mangled)
369              common.add_ir_checks(output_lines, '//', filecheck_run_list, func_dict, mangled,
370                                   False, args.function_signature, global_vars_seen_dict)
371              if line.rstrip('\n') == '//':
372                include_line = False
373
374        if include_line:
375          output_lines.append(line.rstrip('\n'))
376
377    common.debug('Writing %d lines to %s...' % (len(output_lines), ti.path))
378    with open(ti.path, 'wb') as f:
379      f.writelines(['{}\n'.format(l).encode('utf-8') for l in output_lines])
380
381  return 0
382
383
384if __name__ == '__main__':
385  sys.exit(main())
386