xref: /llvm-project/llvm/utils/UpdateTestChecks/common.py (revision ed1e565aaff6a2b6ad9064bcc58c50a46100a836)
1from __future__ import print_function
2
3import copy
4import glob
5import re
6import subprocess
7import sys
8
9if sys.version_info[0] > 2:
10  class string:
11    expandtabs = str.expandtabs
12else:
13  import string
14
15##### Common utilities for update_*test_checks.py
16
17
18_verbose = False
19
20def parse_commandline_args(parser):
21  parser.add_argument('--include-generated-funcs', action='store_true',
22                      help='Output checks for functions not in source')
23  parser.add_argument('-v', '--verbose', action='store_true',
24                      help='Show verbose output')
25  parser.add_argument('-u', '--update-only', action='store_true',
26                      help='Only update test if it was already autogened')
27  parser.add_argument('--force-update', action='store_true',
28                      help='Update test even if it was autogened by a different script')
29  parser.add_argument('--enable', action='store_true', dest='enabled', default=True,
30                       help='Activate CHECK line generation from this point forward')
31  parser.add_argument('--disable', action='store_false', dest='enabled',
32                      help='Deactivate CHECK line generation from this point forward')
33  args = parser.parse_args()
34  global _verbose
35  _verbose = args.verbose
36  return args
37
38
39class InputLineInfo(object):
40  def __init__(self, line, line_number, args, argv):
41    self.line = line
42    self.line_number = line_number
43    self.args = args
44    self.argv = argv
45
46
47class TestInfo(object):
48  def __init__(self, test, parser, script_name, input_lines, args, argv,
49               comment_prefix, argparse_callback):
50    self.parser = parser
51    self.argparse_callback = argparse_callback
52    self.path = test
53    self.args = args
54    self.argv = argv
55    self.input_lines = input_lines
56    self.run_lines = find_run_lines(test, self.input_lines)
57    self.comment_prefix = comment_prefix
58    if self.comment_prefix is None:
59      if self.path.endswith('.mir'):
60        self.comment_prefix = '#'
61      else:
62        self.comment_prefix = ';'
63    self.autogenerated_note_prefix = self.comment_prefix + ' ' + UTC_ADVERT
64    self.test_autogenerated_note = self.autogenerated_note_prefix + script_name
65    self.test_autogenerated_note += get_autogennote_suffix(parser, self.args)
66
67  def ro_iterlines(self):
68    for line_num, input_line in enumerate(self.input_lines):
69      args, argv = check_for_command(input_line, self.parser,
70                                     self.args, self.argv, self.argparse_callback)
71      yield InputLineInfo(input_line, line_num, args, argv)
72
73  def iterlines(self, output_lines):
74    output_lines.append(self.test_autogenerated_note)
75    for line_info in self.ro_iterlines():
76      input_line = line_info.line
77      # Discard any previous script advertising.
78      if input_line.startswith(self.autogenerated_note_prefix):
79        continue
80      self.args = line_info.args
81      self.argv = line_info.argv
82      if not self.args.enabled:
83        output_lines.append(input_line)
84        continue
85      yield line_info
86
87def itertests(test_patterns, parser, script_name, comment_prefix=None, argparse_callback=None):
88  for pattern in test_patterns:
89    # On Windows we must expand the patterns ourselves.
90    tests_list = glob.glob(pattern)
91    if not tests_list:
92      warn("Test file pattern '%s' was not found. Ignoring it." % (pattern,))
93      continue
94    for test in tests_list:
95      with open(test) as f:
96        input_lines = [l.rstrip() for l in f]
97      args = parser.parse_args()
98      if argparse_callback is not None:
99        argparse_callback(args)
100      argv = sys.argv[:]
101      first_line = input_lines[0] if input_lines else ""
102      if UTC_ADVERT in first_line:
103        if script_name not in first_line and not args.force_update:
104          warn("Skipping test which wasn't autogenerated by " + script_name, test)
105          continue
106        args, argv = check_for_command(first_line, parser, args, argv, argparse_callback)
107      elif args.update_only:
108        assert UTC_ADVERT not in first_line
109        warn("Skipping test which isn't autogenerated: " + test)
110        continue
111      yield TestInfo(test, parser, script_name, input_lines, args, argv,
112                     comment_prefix, argparse_callback)
113
114
115def should_add_line_to_output(input_line, prefix_set):
116  # Skip any blank comment lines in the IR.
117  if input_line.strip() == ';':
118    return False
119  # Skip any blank lines in the IR.
120  #if input_line.strip() == '':
121  #  return False
122  # And skip any CHECK lines. We're building our own.
123  m = CHECK_RE.match(input_line)
124  if m and m.group(1) in prefix_set:
125    return False
126
127  return True
128
129# Invoke the tool that is being tested.
130def invoke_tool(exe, cmd_args, ir):
131  with open(ir) as ir_file:
132    # TODO Remove the str form which is used by update_test_checks.py and
133    # update_llc_test_checks.py
134    # The safer list form is used by update_cc_test_checks.py
135    if isinstance(cmd_args, list):
136      stdout = subprocess.check_output([exe] + cmd_args, stdin=ir_file)
137    else:
138      stdout = subprocess.check_output(exe + ' ' + cmd_args,
139                                       shell=True, stdin=ir_file)
140    if sys.version_info[0] > 2:
141      stdout = stdout.decode()
142  # Fix line endings to unix CR style.
143  return stdout.replace('\r\n', '\n')
144
145##### LLVM IR parser
146RUN_LINE_RE = re.compile(r'^\s*(?://|[;#])\s*RUN:\s*(.*)$')
147CHECK_PREFIX_RE = re.compile(r'--?check-prefix(?:es)?[= ](\S+)')
148PREFIX_RE = re.compile('^[a-zA-Z0-9_-]+$')
149CHECK_RE = re.compile(r'^\s*(?://|[;#])\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL|-SAME|-EMPTY)?:')
150
151UTC_ARGS_KEY = 'UTC_ARGS:'
152UTC_ARGS_CMD = re.compile(r'.*' + UTC_ARGS_KEY + '\s*(?P<cmd>.*)\s*$')
153UTC_ADVERT = 'NOTE: Assertions have been autogenerated by '
154
155OPT_FUNCTION_RE = re.compile(
156    r'^(\s*;\s*Function\sAttrs:\s(?P<attrs>[\w\s]+?))?\s*define\s+(?:internal\s+)?[^@]*@(?P<func>[\w.$-]+?)\s*'
157    r'(?P<args_and_sig>\((\)|(.*?[\w.-]+?)\))[^{]*\{)\n(?P<body>.*?)^\}$',
158    flags=(re.M | re.S))
159
160ANALYZE_FUNCTION_RE = re.compile(
161    r'^\s*\'(?P<analysis>[\w\s-]+?)\'\s+for\s+function\s+\'(?P<func>[\w.$-]+?)\':'
162    r'\s*\n(?P<body>.*)$',
163    flags=(re.X | re.S))
164
165IR_FUNCTION_RE = re.compile(r'^\s*define\s+(?:internal\s+)?[^@]*@"?([\w.$-]+)"?\s*\(')
166TRIPLE_IR_RE = re.compile(r'^\s*target\s+triple\s*=\s*"([^"]+)"$')
167TRIPLE_ARG_RE = re.compile(r'-mtriple[= ]([^ ]+)')
168MARCH_ARG_RE = re.compile(r'-march[= ]([^ ]+)')
169
170SCRUB_LEADING_WHITESPACE_RE = re.compile(r'^(\s+)')
171SCRUB_WHITESPACE_RE = re.compile(r'(?!^(|  \w))[ \t]+', flags=re.M)
172SCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M)
173SCRUB_TRAILING_WHITESPACE_TEST_RE = SCRUB_TRAILING_WHITESPACE_RE
174SCRUB_TRAILING_WHITESPACE_AND_ATTRIBUTES_RE = re.compile(r'([ \t]|(#[0-9]+))+$', flags=re.M)
175SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n')
176SCRUB_LOOP_COMMENT_RE = re.compile(
177    r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M)
178SCRUB_TAILING_COMMENT_TOKEN_RE = re.compile(r'(?<=\S)+[ \t]*#$', flags=re.M)
179
180
181def error(msg, test_file=None):
182  if test_file:
183    msg = '{}: {}'.format(msg, test_file)
184  print('ERROR: {}'.format(msg), file=sys.stderr)
185
186def warn(msg, test_file=None):
187  if test_file:
188    msg = '{}: {}'.format(msg, test_file)
189  print('WARNING: {}'.format(msg), file=sys.stderr)
190
191def debug(*args, **kwargs):
192  # Python2 does not allow def debug(*args, file=sys.stderr, **kwargs):
193  if 'file' not in kwargs:
194    kwargs['file'] = sys.stderr
195  if _verbose:
196    print(*args, **kwargs)
197
198def find_run_lines(test, lines):
199  debug('Scanning for RUN lines in test file:', test)
200  raw_lines = [m.group(1)
201               for m in [RUN_LINE_RE.match(l) for l in lines] if m]
202  run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
203  for l in raw_lines[1:]:
204    if run_lines[-1].endswith('\\'):
205      run_lines[-1] = run_lines[-1].rstrip('\\') + ' ' + l
206    else:
207      run_lines.append(l)
208  debug('Found {} RUN lines in {}:'.format(len(run_lines), test))
209  for l in run_lines:
210    debug('  RUN: {}'.format(l))
211  return run_lines
212
213def scrub_body(body):
214  # Scrub runs of whitespace out of the assembly, but leave the leading
215  # whitespace in place.
216  body = SCRUB_WHITESPACE_RE.sub(r' ', body)
217  # Expand the tabs used for indentation.
218  body = string.expandtabs(body, 2)
219  # Strip trailing whitespace.
220  body = SCRUB_TRAILING_WHITESPACE_TEST_RE.sub(r'', body)
221  return body
222
223def do_scrub(body, scrubber, scrubber_args, extra):
224  if scrubber_args:
225    local_args = copy.deepcopy(scrubber_args)
226    local_args[0].extra_scrub = extra
227    return scrubber(body, *local_args)
228  return scrubber(body, *scrubber_args)
229
230# Build up a dictionary of all the function bodies.
231class function_body(object):
232  def __init__(self, string, extra, args_and_sig, attrs):
233    self.scrub = string
234    self.extrascrub = extra
235    self.args_and_sig = args_and_sig
236    self.attrs = attrs
237  def is_same_except_arg_names(self, extrascrub, args_and_sig, attrs):
238    arg_names = set()
239    def drop_arg_names(match):
240        arg_names.add(match.group(3))
241        return match.group(1) + match.group(match.lastindex)
242    def repl_arg_names(match):
243        if match.group(3) is not None and match.group(3) in arg_names:
244            return match.group(1) + match.group(match.lastindex)
245        return match.group(1) + match.group(2) + match.group(match.lastindex)
246    if self.attrs != attrs:
247      return False
248    ans0 = IR_VALUE_RE.sub(drop_arg_names, self.args_and_sig)
249    ans1 = IR_VALUE_RE.sub(drop_arg_names, args_and_sig)
250    if ans0 != ans1:
251        return False
252    es0 = IR_VALUE_RE.sub(repl_arg_names, self.extrascrub)
253    es1 = IR_VALUE_RE.sub(repl_arg_names, extrascrub)
254    es0 = SCRUB_IR_COMMENT_RE.sub(r'', es0)
255    es1 = SCRUB_IR_COMMENT_RE.sub(r'', es1)
256    return es0 == es1
257
258  def __str__(self):
259    return self.scrub
260
261def get_failed_prefixes(func_dict):
262  # This returns the list of those prefixes that failed to match any function,
263  # because there were conflicting bodies produced by different RUN lines, in
264  # all instances of the prefix. Effectively, this prefix is unused and should
265  # be removed.
266  for prefix in func_dict:
267    if func_dict[prefix] and (not [fct for fct in func_dict[prefix]
268         if func_dict[prefix][fct] is not None]):
269      yield prefix
270
271def warn_on_failed_prefixes(func_dict):
272  for prefix in get_failed_prefixes(func_dict):
273      warn('Prefix %s had conflicting output from different RUN lines for all functions' % (prefix,))
274
275def build_function_body_dictionary(function_re, scrubber, scrubber_args, raw_tool_output, prefixes, func_dict, func_order, verbose, record_args, check_attributes):
276  for m in function_re.finditer(raw_tool_output):
277    if not m:
278      continue
279    func = m.group('func')
280    body = m.group('body')
281    attrs = m.group('attrs') if check_attributes else ''
282    # Determine if we print arguments, the opening brace, or nothing after the function name
283    if record_args and 'args_and_sig' in m.groupdict():
284        args_and_sig = scrub_body(m.group('args_and_sig').strip())
285    elif 'args_and_sig' in m.groupdict():
286        args_and_sig = '('
287    else:
288        args_and_sig = ''
289    scrubbed_body = do_scrub(body, scrubber, scrubber_args, extra = False)
290    scrubbed_extra = do_scrub(body, scrubber, scrubber_args, extra = True)
291    if 'analysis' in m.groupdict():
292      analysis = m.group('analysis')
293      if analysis.lower() != 'cost model analysis':
294        warn('Unsupported analysis mode: %r!' % (analysis,))
295    if func.startswith('stress'):
296      # We only use the last line of the function body for stress tests.
297      scrubbed_body = '\n'.join(scrubbed_body.splitlines()[-1:])
298    if verbose:
299      print('Processing function: ' + func, file=sys.stderr)
300      for l in scrubbed_body.splitlines():
301        print('  ' + l, file=sys.stderr)
302    for prefix in prefixes:
303      if func in func_dict[prefix]:
304        if (func_dict[prefix][func] is None or
305            str(func_dict[prefix][func]) != scrubbed_body or
306            func_dict[prefix][func].args_and_sig != args_and_sig or
307                func_dict[prefix][func].attrs != attrs):
308          if (func_dict[prefix][func] is not None and
309              func_dict[prefix][func].is_same_except_arg_names(scrubbed_extra,
310                                                               args_and_sig,
311                                                               attrs)):
312            func_dict[prefix][func].scrub = scrubbed_extra
313            func_dict[prefix][func].args_and_sig = args_and_sig
314            continue
315          else:
316            # This means a previous RUN line produced a body for this function
317            # that is different from the one produced by this current RUN line,
318            # so the body can't be common accross RUN lines. We use None to
319            # indicate that.
320            func_dict[prefix][func] = None
321            continue
322
323      func_dict[prefix][func] = function_body(scrubbed_body, scrubbed_extra, args_and_sig, attrs)
324      func_order[prefix].append(func)
325
326class FunctionTestBuilder:
327  def __init__(self, run_list, flags, scrubber_args):
328    self._verbose = flags.verbose
329    self._record_args = flags.function_signature
330    self._check_attributes = flags.check_attributes
331    self._scrubber_args = scrubber_args
332    self._func_dict = {}
333    self._func_order = {}
334    for tuple in run_list:
335      for prefix in tuple[0]:
336        self._func_dict.update({prefix:dict()})
337        self._func_order.update({prefix: []})
338
339  def finish_and_get_func_dict(self):
340    warn_on_failed_prefixes(self._func_dict)
341    return self._func_dict
342
343  def func_order(self):
344    return self._func_order
345
346  def process_run_line(self, function_re, scrubber, raw_tool_output, prefixes):
347    build_function_body_dictionary(function_re, scrubber, self._scrubber_args,
348                                   raw_tool_output, prefixes, self._func_dict,
349                                   self._func_order, self._verbose,
350                                   self._record_args, self._check_attributes)
351
352##### Generator of LLVM IR CHECK lines
353
354SCRUB_IR_COMMENT_RE = re.compile(r'\s*;.*')
355
356# TODO: We should also derive check lines for global, debug, loop declarations, etc..
357
358class NamelessValue:
359    def __init__(self, check_prefix, ir_prefix, ir_regexp):
360        self.check_prefix = check_prefix
361        self.ir_prefix = ir_prefix
362        self.ir_regexp = ir_regexp
363
364# Description of the different "unnamed" values we match in the IR, e.g.,
365# (local) ssa values, (debug) metadata, etc.
366nameless_values = [
367    NamelessValue(r'TMP',   r'%',            r'[\w.-]+?'),
368    NamelessValue(r'GLOB',  r'@',            r'[0-9]+?'),
369    NamelessValue(r'ATTR',  r'#',            r'[0-9]+?'),
370    NamelessValue(r'DBG',   r'!dbg !',       r'[0-9]+?'),
371    NamelessValue(r'TBAA',  r'!tbaa !',      r'[0-9]+?'),
372    NamelessValue(r'RNG',   r'!range !',     r'[0-9]+?'),
373    NamelessValue(r'LOOP',  r'!llvm.loop !', r'[0-9]+?'),
374    NamelessValue(r'META',  r'metadata !',   r'[0-9]+?'),
375]
376
377# Build the regexp that matches an "IR value". This can be a local variable,
378# argument, global, or metadata, anything that is "named". It is important that
379# the PREFIX and SUFFIX below only contain a single group, if that changes
380# other locations will need adjustment as well.
381IR_VALUE_REGEXP_PREFIX = r'(\s+)'
382IR_VALUE_REGEXP_STRING = r''
383for nameless_value in nameless_values:
384    if IR_VALUE_REGEXP_STRING:
385        IR_VALUE_REGEXP_STRING += '|'
386    IR_VALUE_REGEXP_STRING += nameless_value.ir_prefix + r'(' + nameless_value.ir_regexp + r')'
387IR_VALUE_REGEXP_SUFFIX = r'([,\s\(\)]|\Z)'
388IR_VALUE_RE = re.compile(IR_VALUE_REGEXP_PREFIX + r'(' + IR_VALUE_REGEXP_STRING + r')' + IR_VALUE_REGEXP_SUFFIX)
389
390# The entire match is group 0, the prefix has one group (=1), the entire
391# IR_VALUE_REGEXP_STRING is one group (=2), and then the nameless values start.
392first_nameless_group_in_ir_value_match = 3
393
394# Check a match for IR_VALUE_RE and inspect it to determine if it was a local
395# value, %..., global @..., debug number !dbg !..., etc. See the PREFIXES above.
396def get_idx_from_ir_value_match(match):
397    for i in range(first_nameless_group_in_ir_value_match, match.lastindex):
398        if match.group(i) is not None:
399            return i - first_nameless_group_in_ir_value_match
400    error("Unable to identify the kind of IR value from the match!")
401    return 0;
402
403# See get_idx_from_ir_value_match
404def get_name_from_ir_value_match(match):
405    return match.group(get_idx_from_ir_value_match(match) + first_nameless_group_in_ir_value_match)
406
407# Return the nameless prefix we use for this kind or IR value, see also
408# get_idx_from_ir_value_match
409def get_nameless_check_prefix_from_ir_value_match(match):
410    return nameless_values[get_idx_from_ir_value_match(match)].check_prefix
411
412# Return the IR prefix we use for this kind or IR value, e.g., % for locals,
413# see also get_idx_from_ir_value_match
414def get_ir_prefix_from_ir_value_match(match):
415    return nameless_values[get_idx_from_ir_value_match(match)].ir_prefix
416
417# Return true if this kind or IR value is "local", basically if it matches '%{{.*}}'.
418def is_local_ir_value_match(match):
419    return nameless_values[get_idx_from_ir_value_match(match)].ir_prefix == '%'
420
421# Create a FileCheck variable name based on an IR name.
422def get_value_name(var, match):
423  if var.isdigit():
424    var = get_nameless_check_prefix_from_ir_value_match(match) + var
425  var = var.replace('.', '_')
426  var = var.replace('-', '_')
427  return var.upper()
428
429# Create a FileCheck variable from regex.
430def get_value_definition(var, match):
431  return '[[' + get_value_name(var, match) + ':' + get_ir_prefix_from_ir_value_match(match) + '.*]]'
432
433# Use a FileCheck variable.
434def get_value_use(var, match):
435  return '[[' + get_value_name(var, match) + ']]'
436
437# Replace IR value defs and uses with FileCheck variables.
438def generalize_check_lines(lines, is_analyze, vars_seen, global_vars_seen):
439  # This gets called for each match that occurs in
440  # a line. We transform variables we haven't seen
441  # into defs, and variables we have seen into uses.
442  def transform_line_vars(match):
443    pre = get_ir_prefix_from_ir_value_match(match)
444    var = get_name_from_ir_value_match(match)
445    for nameless_value in nameless_values:
446        if re.match(r'^' + nameless_value.check_prefix + r'[0-9]+?$', var, re.IGNORECASE):
447            warn("Change IR value name '%s' to prevent possible conflict with scripted FileCheck name." % (var,))
448    if (pre, var) in vars_seen or (pre, var) in global_vars_seen:
449      rv = get_value_use(var, match)
450    else:
451      if is_local_ir_value_match(match):
452         vars_seen.add((pre, var))
453      else:
454         global_vars_seen.add((pre, var))
455      rv = get_value_definition(var, match)
456    # re.sub replaces the entire regex match
457    # with whatever you return, so we have
458    # to make sure to hand it back everything
459    # including the commas and spaces.
460    return match.group(1) + rv + match.group(match.lastindex)
461
462  lines_with_def = []
463
464  for i, line in enumerate(lines):
465    # An IR variable named '%.' matches the FileCheck regex string.
466    line = line.replace('%.', '%dot')
467    # Ignore any comments, since the check lines will too.
468    scrubbed_line = SCRUB_IR_COMMENT_RE.sub(r'', line)
469    lines[i] = scrubbed_line
470    if not is_analyze:
471      # It can happen that two matches are back-to-back and for some reason sub
472      # will not replace both of them. For now we work around this by
473      # substituting until there is no more match.
474      changed = True
475      while changed:
476          (lines[i], changed) = IR_VALUE_RE.subn(transform_line_vars, lines[i], count=1)
477  return lines
478
479
480def add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, is_asm, is_analyze, global_vars_seen_dict):
481  # prefix_exclusions are prefixes we cannot use to print the function because it doesn't exist in run lines that use these prefixes as well.
482  prefix_exclusions = set()
483  printed_prefixes = []
484  for p in prefix_list:
485    checkprefixes = p[0]
486    # If not all checkprefixes of this run line produced the function we cannot check for it as it does not
487    # exist for this run line. A subset of the check prefixes might know about the function but only because
488    # other run lines created it.
489    if any(map(lambda checkprefix: func_name not in func_dict[checkprefix], checkprefixes)):
490        prefix_exclusions |= set(checkprefixes)
491        continue
492
493  # prefix_exclusions is constructed, we can now emit the output
494  for p in prefix_list:
495    checkprefixes = p[0]
496    for checkprefix in checkprefixes:
497      if checkprefix in printed_prefixes:
498        break
499
500      # Check if the prefix is excluded.
501      if checkprefix in prefix_exclusions:
502        continue
503
504      # If we do not have output for this prefix we skip it.
505      if not func_dict[checkprefix][func_name]:
506        continue
507
508      # Add some space between different check prefixes, but not after the last
509      # check line (before the test code).
510      if is_asm:
511        if len(printed_prefixes) != 0:
512          output_lines.append(comment_marker)
513
514      if checkprefix not in global_vars_seen_dict:
515          global_vars_seen_dict[checkprefix] = set()
516      global_vars_seen = global_vars_seen_dict[checkprefix]
517
518      vars_seen = set()
519      printed_prefixes.append(checkprefix)
520      attrs = str(func_dict[checkprefix][func_name].attrs)
521      attrs = '' if attrs == 'None' else attrs
522      if attrs:
523        output_lines.append('%s %s: Function Attrs: %s' % (comment_marker, checkprefix, attrs))
524      args_and_sig = str(func_dict[checkprefix][func_name].args_and_sig)
525      args_and_sig = generalize_check_lines([args_and_sig], is_analyze, vars_seen, global_vars_seen)[0]
526      if '[[' in args_and_sig:
527        output_lines.append(check_label_format % (checkprefix, func_name, ''))
528        output_lines.append('%s %s-SAME: %s' % (comment_marker, checkprefix, args_and_sig))
529      else:
530        output_lines.append(check_label_format % (checkprefix, func_name, args_and_sig))
531      func_body = str(func_dict[checkprefix][func_name]).splitlines()
532
533      # For ASM output, just emit the check lines.
534      if is_asm:
535        output_lines.append('%s %s:       %s' % (comment_marker, checkprefix, func_body[0]))
536        for func_line in func_body[1:]:
537          if func_line.strip() == '':
538            output_lines.append('%s %s-EMPTY:' % (comment_marker, checkprefix))
539          else:
540            output_lines.append('%s %s-NEXT:  %s' % (comment_marker, checkprefix, func_line))
541        break
542
543      # For IR output, change all defs to FileCheck variables, so we're immune
544      # to variable naming fashions.
545      func_body = generalize_check_lines(func_body, is_analyze, vars_seen, global_vars_seen)
546
547      # This could be selectively enabled with an optional invocation argument.
548      # Disabled for now: better to check everything. Be safe rather than sorry.
549
550      # Handle the first line of the function body as a special case because
551      # it's often just noise (a useless asm comment or entry label).
552      #if func_body[0].startswith("#") or func_body[0].startswith("entry:"):
553      #  is_blank_line = True
554      #else:
555      #  output_lines.append('%s %s:       %s' % (comment_marker, checkprefix, func_body[0]))
556      #  is_blank_line = False
557
558      is_blank_line = False
559
560      for func_line in func_body:
561        if func_line.strip() == '':
562          is_blank_line = True
563          continue
564        # Do not waste time checking IR comments.
565        func_line = SCRUB_IR_COMMENT_RE.sub(r'', func_line)
566
567        # Skip blank lines instead of checking them.
568        if is_blank_line:
569          output_lines.append('{} {}:       {}'.format(
570              comment_marker, checkprefix, func_line))
571        else:
572          output_lines.append('{} {}-NEXT:  {}'.format(
573              comment_marker, checkprefix, func_line))
574        is_blank_line = False
575
576      # Add space between different check prefixes and also before the first
577      # line of code in the test function.
578      output_lines.append(comment_marker)
579      break
580
581def add_ir_checks(output_lines, comment_marker, prefix_list, func_dict,
582                  func_name, preserve_names, function_sig, global_vars_seen_dict):
583  # Label format is based on IR string.
584  function_def_regex = 'define {{[^@]+}}' if function_sig else ''
585  check_label_format = '{} %s-LABEL: {}@%s%s'.format(comment_marker, function_def_regex)
586  add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name,
587             check_label_format, False, preserve_names, global_vars_seen_dict)
588
589def add_analyze_checks(output_lines, comment_marker, prefix_list, func_dict, func_name):
590  check_label_format = '{} %s-LABEL: \'%s%s\''.format(comment_marker)
591  global_vars_seen_dict = {}
592  add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name,
593             check_label_format, False, True, global_vars_seen_dict)
594
595
596def check_prefix(prefix):
597  if not PREFIX_RE.match(prefix):
598        hint = ""
599        if ',' in prefix:
600          hint = " Did you mean '--check-prefixes=" + prefix + "'?"
601        warn(("Supplied prefix '%s' is invalid. Prefix must contain only alphanumeric characters, hyphens and underscores." + hint) %
602             (prefix))
603
604
605def verify_filecheck_prefixes(fc_cmd):
606  fc_cmd_parts = fc_cmd.split()
607  for part in fc_cmd_parts:
608    if "check-prefix=" in part:
609      prefix = part.split('=', 1)[1]
610      check_prefix(prefix)
611    elif "check-prefixes=" in part:
612      prefixes = part.split('=', 1)[1].split(',')
613      for prefix in prefixes:
614        check_prefix(prefix)
615        if prefixes.count(prefix) > 1:
616          warn("Supplied prefix '%s' is not unique in the prefix list." % (prefix,))
617
618
619def get_autogennote_suffix(parser, args):
620  autogenerated_note_args = ''
621  for action in parser._actions:
622    if not hasattr(args, action.dest):
623      continue  # Ignore options such as --help that aren't included in args
624    # Ignore parameters such as paths to the binary or the list of tests
625    if action.dest in ('tests', 'update_only', 'opt_binary', 'llc_binary',
626                       'clang', 'opt', 'llvm_bin', 'verbose'):
627      continue
628    value = getattr(args, action.dest)
629    if action.const is not None:  # action stores a constant (usually True/False)
630      # Skip actions with different constant values (this happens with boolean
631      # --foo/--no-foo options)
632      if value != action.const:
633        continue
634    if parser.get_default(action.dest) == value:
635      continue  # Don't add default values
636    autogenerated_note_args += action.option_strings[0] + ' '
637    if action.const is None:  # action takes a parameter
638      autogenerated_note_args += '%s ' % value
639  if autogenerated_note_args:
640    autogenerated_note_args = ' %s %s' % (UTC_ARGS_KEY, autogenerated_note_args[:-1])
641  return autogenerated_note_args
642
643
644def check_for_command(line, parser, args, argv, argparse_callback):
645    cmd_m = UTC_ARGS_CMD.match(line)
646    if cmd_m:
647        cmd = cmd_m.group('cmd').strip().split(' ')
648        argv = argv + cmd
649        args = parser.parse_args(filter(lambda arg: arg not in args.tests, argv))
650        if argparse_callback is not None:
651          argparse_callback(args)
652    return args, argv
653
654def find_arg_in_test(test_info, get_arg_to_check, arg_string, is_global):
655  result = get_arg_to_check(test_info.args)
656  if not result and is_global:
657    # See if this has been specified via UTC_ARGS.  This is a "global" option
658    # that affects the entire generation of test checks.  If it exists anywhere
659    # in the test, apply it to everything.
660    saw_line = False
661    for line_info in test_info.ro_iterlines():
662      line = line_info.line
663      if not line.startswith(';') and line.strip() != '':
664        saw_line = True
665      result = get_arg_to_check(line_info.args)
666      if result:
667        if warn and saw_line:
668          # We saw the option after already reading some test input lines.
669          # Warn about it.
670          print('WARNING: Found {} in line following test start: '.format(arg_string)
671                + line, file=sys.stderr)
672          print('WARNING: Consider moving {} to top of file'.format(arg_string),
673                file=sys.stderr)
674        break
675  return result
676
677def dump_input_lines(output_lines, test_info, prefix_set, comment_string):
678  for input_line_info in test_info.iterlines(output_lines):
679    line = input_line_info.line
680    args = input_line_info.args
681    if line.strip() == comment_string:
682      continue
683    if line.lstrip().startswith(comment_string):
684      m = CHECK_RE.match(line)
685      if m and m.group(1) in prefix_set:
686        continue
687    output_lines.append(line.rstrip('\n'))
688
689def add_checks_at_end(output_lines, prefix_list, func_order,
690                      comment_string, check_generator):
691  added = set()
692  for prefix in prefix_list:
693    prefixes = prefix[0]
694    tool_args = prefix[1]
695    for prefix in prefixes:
696      for func in func_order[prefix]:
697        if added:
698          output_lines.append(comment_string)
699        added.add(func)
700
701        # The add_*_checks routines expect a run list whose items are
702        # tuples that have a list of prefixes as their first element and
703        # tool command args string as their second element.  They output
704        # checks for each prefix in the list of prefixes.  By doing so, it
705        # implicitly assumes that for each function every run line will
706        # generate something for that function.  That is not the case for
707        # generated functions as some run lines might not generate them
708        # (e.g. -fopenmp vs. no -fopenmp).
709        #
710        # Therefore, pass just the prefix we're interested in.  This has
711        # the effect of generating all of the checks for functions of a
712        # single prefix before moving on to the next prefix.  So checks
713        # are ordered by prefix instead of by function as in "normal"
714        # mode.
715        check_generator(output_lines,
716                        [([prefix], tool_args)],
717                        func)
718