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
261class FunctionTestBuilder:
262  def __init__(self, run_list, flags, scrubber_args):
263    self._verbose = flags.verbose
264    self._record_args = flags.function_signature
265    self._check_attributes = flags.check_attributes
266    self._scrubber_args = scrubber_args
267    self._func_dict = {}
268    self._func_order = {}
269    for tuple in run_list:
270      for prefix in tuple[0]:
271        self._func_dict.update({prefix:dict()})
272        self._func_order.update({prefix: []})
273
274  def finish_and_get_func_dict(self):
275    for prefix in self._get_failed_prefixes():
276      warn('Prefix %s had conflicting output from different RUN lines for all functions' % (prefix,))
277    return self._func_dict
278
279  def func_order(self):
280    return self._func_order
281
282  def process_run_line(self, function_re, scrubber, raw_tool_output, prefixes):
283    for m in function_re.finditer(raw_tool_output):
284      if not m:
285        continue
286      func = m.group('func')
287      body = m.group('body')
288      attrs = m.group('attrs') if self._check_attributes else ''
289      # Determine if we print arguments, the opening brace, or nothing after the
290      # function name
291      if self._record_args and 'args_and_sig' in m.groupdict():
292          args_and_sig = scrub_body(m.group('args_and_sig').strip())
293      elif 'args_and_sig' in m.groupdict():
294          args_and_sig = '('
295      else:
296          args_and_sig = ''
297      scrubbed_body = do_scrub(body, scrubber, self._scrubber_args,
298                               extra=False)
299      scrubbed_extra = do_scrub(body, scrubber, self._scrubber_args,
300                                extra=True)
301      if 'analysis' in m.groupdict():
302        analysis = m.group('analysis')
303        if analysis.lower() != 'cost model analysis':
304          warn('Unsupported analysis mode: %r!' % (analysis,))
305      if func.startswith('stress'):
306        # We only use the last line of the function body for stress tests.
307        scrubbed_body = '\n'.join(scrubbed_body.splitlines()[-1:])
308      if self._verbose:
309        print('Processing function: ' + func, file=sys.stderr)
310        for l in scrubbed_body.splitlines():
311          print('  ' + l, file=sys.stderr)
312      for prefix in prefixes:
313        if func in self._func_dict[prefix]:
314          if (self._func_dict[prefix][func] is None or
315              str(self._func_dict[prefix][func]) != scrubbed_body or
316              self._func_dict[prefix][func].args_and_sig != args_and_sig or
317                  self._func_dict[prefix][func].attrs != attrs):
318            if (self._func_dict[prefix][func] is not None and
319                self._func_dict[prefix][func].is_same_except_arg_names(
320                scrubbed_extra,
321                args_and_sig,
322                attrs)):
323              self._func_dict[prefix][func].scrub = scrubbed_extra
324              self._func_dict[prefix][func].args_and_sig = args_and_sig
325              continue
326            else:
327              # This means a previous RUN line produced a body for this function
328              # that is different from the one produced by this current RUN line,
329              # so the body can't be common accross RUN lines. We use None to
330              # indicate that.
331              self._func_dict[prefix][func] = None
332              continue
333
334        self._func_dict[prefix][func] = function_body(
335            scrubbed_body, scrubbed_extra, args_and_sig, attrs)
336        self._func_order[prefix].append(func)
337
338  def _get_failed_prefixes(self):
339    # This returns the list of those prefixes that failed to match any function,
340    # because there were conflicting bodies produced by different RUN lines, in
341    # all instances of the prefix. Effectively, this prefix is unused and should
342    # be removed.
343    for prefix in self._func_dict:
344      if (self._func_dict[prefix] and
345          (not [fct for fct in self._func_dict[prefix]
346                if self._func_dict[prefix][fct] is not None])):
347        yield prefix
348
349
350##### Generator of LLVM IR CHECK lines
351
352SCRUB_IR_COMMENT_RE = re.compile(r'\s*;.*')
353
354# TODO: We should also derive check lines for global, debug, loop declarations, etc..
355
356class NamelessValue:
357    def __init__(self, check_prefix, ir_prefix, ir_regexp):
358        self.check_prefix = check_prefix
359        self.ir_prefix = ir_prefix
360        self.ir_regexp = ir_regexp
361
362# Description of the different "unnamed" values we match in the IR, e.g.,
363# (local) ssa values, (debug) metadata, etc.
364nameless_values = [
365    NamelessValue(r'TMP',   r'%',            r'[\w.-]+?'),
366    NamelessValue(r'GLOB',  r'@',            r'[0-9]+?'),
367    NamelessValue(r'ATTR',  r'#',            r'[0-9]+?'),
368    NamelessValue(r'DBG',   r'!dbg !',       r'[0-9]+?'),
369    NamelessValue(r'TBAA',  r'!tbaa !',      r'[0-9]+?'),
370    NamelessValue(r'RNG',   r'!range !',     r'[0-9]+?'),
371    NamelessValue(r'LOOP',  r'!llvm.loop !', r'[0-9]+?'),
372    NamelessValue(r'META',  r'metadata !',   r'[0-9]+?'),
373]
374
375# Build the regexp that matches an "IR value". This can be a local variable,
376# argument, global, or metadata, anything that is "named". It is important that
377# the PREFIX and SUFFIX below only contain a single group, if that changes
378# other locations will need adjustment as well.
379IR_VALUE_REGEXP_PREFIX = r'(\s+)'
380IR_VALUE_REGEXP_STRING = r''
381for nameless_value in nameless_values:
382    if IR_VALUE_REGEXP_STRING:
383        IR_VALUE_REGEXP_STRING += '|'
384    IR_VALUE_REGEXP_STRING += nameless_value.ir_prefix + r'(' + nameless_value.ir_regexp + r')'
385IR_VALUE_REGEXP_SUFFIX = r'([,\s\(\)]|\Z)'
386IR_VALUE_RE = re.compile(IR_VALUE_REGEXP_PREFIX + r'(' + IR_VALUE_REGEXP_STRING + r')' + IR_VALUE_REGEXP_SUFFIX)
387
388# The entire match is group 0, the prefix has one group (=1), the entire
389# IR_VALUE_REGEXP_STRING is one group (=2), and then the nameless values start.
390first_nameless_group_in_ir_value_match = 3
391
392# Check a match for IR_VALUE_RE and inspect it to determine if it was a local
393# value, %..., global @..., debug number !dbg !..., etc. See the PREFIXES above.
394def get_idx_from_ir_value_match(match):
395    for i in range(first_nameless_group_in_ir_value_match, match.lastindex):
396        if match.group(i) is not None:
397            return i - first_nameless_group_in_ir_value_match
398    error("Unable to identify the kind of IR value from the match!")
399    return 0;
400
401# See get_idx_from_ir_value_match
402def get_name_from_ir_value_match(match):
403    return match.group(get_idx_from_ir_value_match(match) + first_nameless_group_in_ir_value_match)
404
405# Return the nameless prefix we use for this kind or IR value, see also
406# get_idx_from_ir_value_match
407def get_nameless_check_prefix_from_ir_value_match(match):
408    return nameless_values[get_idx_from_ir_value_match(match)].check_prefix
409
410# Return the IR prefix we use for this kind or IR value, e.g., % for locals,
411# see also get_idx_from_ir_value_match
412def get_ir_prefix_from_ir_value_match(match):
413    return nameless_values[get_idx_from_ir_value_match(match)].ir_prefix
414
415# Return true if this kind or IR value is "local", basically if it matches '%{{.*}}'.
416def is_local_ir_value_match(match):
417    return nameless_values[get_idx_from_ir_value_match(match)].ir_prefix == '%'
418
419# Create a FileCheck variable name based on an IR name.
420def get_value_name(var, match):
421  if var.isdigit():
422    var = get_nameless_check_prefix_from_ir_value_match(match) + var
423  var = var.replace('.', '_')
424  var = var.replace('-', '_')
425  return var.upper()
426
427# Create a FileCheck variable from regex.
428def get_value_definition(var, match):
429  return '[[' + get_value_name(var, match) + ':' + get_ir_prefix_from_ir_value_match(match) + '.*]]'
430
431# Use a FileCheck variable.
432def get_value_use(var, match):
433  return '[[' + get_value_name(var, match) + ']]'
434
435# Replace IR value defs and uses with FileCheck variables.
436def generalize_check_lines(lines, is_analyze, vars_seen, global_vars_seen):
437  # This gets called for each match that occurs in
438  # a line. We transform variables we haven't seen
439  # into defs, and variables we have seen into uses.
440  def transform_line_vars(match):
441    pre = get_ir_prefix_from_ir_value_match(match)
442    var = get_name_from_ir_value_match(match)
443    for nameless_value in nameless_values:
444        if re.match(r'^' + nameless_value.check_prefix + r'[0-9]+?$', var, re.IGNORECASE):
445            warn("Change IR value name '%s' to prevent possible conflict with scripted FileCheck name." % (var,))
446    if (pre, var) in vars_seen or (pre, var) in global_vars_seen:
447      rv = get_value_use(var, match)
448    else:
449      if is_local_ir_value_match(match):
450         vars_seen.add((pre, var))
451      else:
452         global_vars_seen.add((pre, var))
453      rv = get_value_definition(var, match)
454    # re.sub replaces the entire regex match
455    # with whatever you return, so we have
456    # to make sure to hand it back everything
457    # including the commas and spaces.
458    return match.group(1) + rv + match.group(match.lastindex)
459
460  lines_with_def = []
461
462  for i, line in enumerate(lines):
463    # An IR variable named '%.' matches the FileCheck regex string.
464    line = line.replace('%.', '%dot')
465    # Ignore any comments, since the check lines will too.
466    scrubbed_line = SCRUB_IR_COMMENT_RE.sub(r'', line)
467    lines[i] = scrubbed_line
468    if not is_analyze:
469      # It can happen that two matches are back-to-back and for some reason sub
470      # will not replace both of them. For now we work around this by
471      # substituting until there is no more match.
472      changed = True
473      while changed:
474          (lines[i], changed) = IR_VALUE_RE.subn(transform_line_vars, lines[i], count=1)
475  return lines
476
477
478def add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, is_asm, is_analyze, global_vars_seen_dict):
479  # 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.
480  prefix_exclusions = set()
481  printed_prefixes = []
482  for p in prefix_list:
483    checkprefixes = p[0]
484    # If not all checkprefixes of this run line produced the function we cannot check for it as it does not
485    # exist for this run line. A subset of the check prefixes might know about the function but only because
486    # other run lines created it.
487    if any(map(lambda checkprefix: func_name not in func_dict[checkprefix], checkprefixes)):
488        prefix_exclusions |= set(checkprefixes)
489        continue
490
491  # prefix_exclusions is constructed, we can now emit the output
492  for p in prefix_list:
493    checkprefixes = p[0]
494    for checkprefix in checkprefixes:
495      if checkprefix in printed_prefixes:
496        break
497
498      # Check if the prefix is excluded.
499      if checkprefix in prefix_exclusions:
500        continue
501
502      # If we do not have output for this prefix we skip it.
503      if not func_dict[checkprefix][func_name]:
504        continue
505
506      # Add some space between different check prefixes, but not after the last
507      # check line (before the test code).
508      if is_asm:
509        if len(printed_prefixes) != 0:
510          output_lines.append(comment_marker)
511
512      if checkprefix not in global_vars_seen_dict:
513          global_vars_seen_dict[checkprefix] = set()
514      global_vars_seen = global_vars_seen_dict[checkprefix]
515
516      vars_seen = set()
517      printed_prefixes.append(checkprefix)
518      attrs = str(func_dict[checkprefix][func_name].attrs)
519      attrs = '' if attrs == 'None' else attrs
520      if attrs:
521        output_lines.append('%s %s: Function Attrs: %s' % (comment_marker, checkprefix, attrs))
522      args_and_sig = str(func_dict[checkprefix][func_name].args_and_sig)
523      args_and_sig = generalize_check_lines([args_and_sig], is_analyze, vars_seen, global_vars_seen)[0]
524      if '[[' in args_and_sig:
525        output_lines.append(check_label_format % (checkprefix, func_name, ''))
526        output_lines.append('%s %s-SAME: %s' % (comment_marker, checkprefix, args_and_sig))
527      else:
528        output_lines.append(check_label_format % (checkprefix, func_name, args_and_sig))
529      func_body = str(func_dict[checkprefix][func_name]).splitlines()
530
531      # For ASM output, just emit the check lines.
532      if is_asm:
533        output_lines.append('%s %s:       %s' % (comment_marker, checkprefix, func_body[0]))
534        for func_line in func_body[1:]:
535          if func_line.strip() == '':
536            output_lines.append('%s %s-EMPTY:' % (comment_marker, checkprefix))
537          else:
538            output_lines.append('%s %s-NEXT:  %s' % (comment_marker, checkprefix, func_line))
539        break
540
541      # For IR output, change all defs to FileCheck variables, so we're immune
542      # to variable naming fashions.
543      func_body = generalize_check_lines(func_body, is_analyze, vars_seen, global_vars_seen)
544
545      # This could be selectively enabled with an optional invocation argument.
546      # Disabled for now: better to check everything. Be safe rather than sorry.
547
548      # Handle the first line of the function body as a special case because
549      # it's often just noise (a useless asm comment or entry label).
550      #if func_body[0].startswith("#") or func_body[0].startswith("entry:"):
551      #  is_blank_line = True
552      #else:
553      #  output_lines.append('%s %s:       %s' % (comment_marker, checkprefix, func_body[0]))
554      #  is_blank_line = False
555
556      is_blank_line = False
557
558      for func_line in func_body:
559        if func_line.strip() == '':
560          is_blank_line = True
561          continue
562        # Do not waste time checking IR comments.
563        func_line = SCRUB_IR_COMMENT_RE.sub(r'', func_line)
564
565        # Skip blank lines instead of checking them.
566        if is_blank_line:
567          output_lines.append('{} {}:       {}'.format(
568              comment_marker, checkprefix, func_line))
569        else:
570          output_lines.append('{} {}-NEXT:  {}'.format(
571              comment_marker, checkprefix, func_line))
572        is_blank_line = False
573
574      # Add space between different check prefixes and also before the first
575      # line of code in the test function.
576      output_lines.append(comment_marker)
577      break
578
579def add_ir_checks(output_lines, comment_marker, prefix_list, func_dict,
580                  func_name, preserve_names, function_sig, global_vars_seen_dict):
581  # Label format is based on IR string.
582  function_def_regex = 'define {{[^@]+}}' if function_sig else ''
583  check_label_format = '{} %s-LABEL: {}@%s%s'.format(comment_marker, function_def_regex)
584  add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name,
585             check_label_format, False, preserve_names, global_vars_seen_dict)
586
587def add_analyze_checks(output_lines, comment_marker, prefix_list, func_dict, func_name):
588  check_label_format = '{} %s-LABEL: \'%s%s\''.format(comment_marker)
589  global_vars_seen_dict = {}
590  add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name,
591             check_label_format, False, True, global_vars_seen_dict)
592
593
594def check_prefix(prefix):
595  if not PREFIX_RE.match(prefix):
596        hint = ""
597        if ',' in prefix:
598          hint = " Did you mean '--check-prefixes=" + prefix + "'?"
599        warn(("Supplied prefix '%s' is invalid. Prefix must contain only alphanumeric characters, hyphens and underscores." + hint) %
600             (prefix))
601
602
603def verify_filecheck_prefixes(fc_cmd):
604  fc_cmd_parts = fc_cmd.split()
605  for part in fc_cmd_parts:
606    if "check-prefix=" in part:
607      prefix = part.split('=', 1)[1]
608      check_prefix(prefix)
609    elif "check-prefixes=" in part:
610      prefixes = part.split('=', 1)[1].split(',')
611      for prefix in prefixes:
612        check_prefix(prefix)
613        if prefixes.count(prefix) > 1:
614          warn("Supplied prefix '%s' is not unique in the prefix list." % (prefix,))
615
616
617def get_autogennote_suffix(parser, args):
618  autogenerated_note_args = ''
619  for action in parser._actions:
620    if not hasattr(args, action.dest):
621      continue  # Ignore options such as --help that aren't included in args
622    # Ignore parameters such as paths to the binary or the list of tests
623    if action.dest in ('tests', 'update_only', 'opt_binary', 'llc_binary',
624                       'clang', 'opt', 'llvm_bin', 'verbose'):
625      continue
626    value = getattr(args, action.dest)
627    if action.const is not None:  # action stores a constant (usually True/False)
628      # Skip actions with different constant values (this happens with boolean
629      # --foo/--no-foo options)
630      if value != action.const:
631        continue
632    if parser.get_default(action.dest) == value:
633      continue  # Don't add default values
634    autogenerated_note_args += action.option_strings[0] + ' '
635    if action.const is None:  # action takes a parameter
636      autogenerated_note_args += '%s ' % value
637  if autogenerated_note_args:
638    autogenerated_note_args = ' %s %s' % (UTC_ARGS_KEY, autogenerated_note_args[:-1])
639  return autogenerated_note_args
640
641
642def check_for_command(line, parser, args, argv, argparse_callback):
643    cmd_m = UTC_ARGS_CMD.match(line)
644    if cmd_m:
645        cmd = cmd_m.group('cmd').strip().split(' ')
646        argv = argv + cmd
647        args = parser.parse_args(filter(lambda arg: arg not in args.tests, argv))
648        if argparse_callback is not None:
649          argparse_callback(args)
650    return args, argv
651
652def find_arg_in_test(test_info, get_arg_to_check, arg_string, is_global):
653  result = get_arg_to_check(test_info.args)
654  if not result and is_global:
655    # See if this has been specified via UTC_ARGS.  This is a "global" option
656    # that affects the entire generation of test checks.  If it exists anywhere
657    # in the test, apply it to everything.
658    saw_line = False
659    for line_info in test_info.ro_iterlines():
660      line = line_info.line
661      if not line.startswith(';') and line.strip() != '':
662        saw_line = True
663      result = get_arg_to_check(line_info.args)
664      if result:
665        if warn and saw_line:
666          # We saw the option after already reading some test input lines.
667          # Warn about it.
668          print('WARNING: Found {} in line following test start: '.format(arg_string)
669                + line, file=sys.stderr)
670          print('WARNING: Consider moving {} to top of file'.format(arg_string),
671                file=sys.stderr)
672        break
673  return result
674
675def dump_input_lines(output_lines, test_info, prefix_set, comment_string):
676  for input_line_info in test_info.iterlines(output_lines):
677    line = input_line_info.line
678    args = input_line_info.args
679    if line.strip() == comment_string:
680      continue
681    if line.lstrip().startswith(comment_string):
682      m = CHECK_RE.match(line)
683      if m and m.group(1) in prefix_set:
684        continue
685    output_lines.append(line.rstrip('\n'))
686
687def add_checks_at_end(output_lines, prefix_list, func_order,
688                      comment_string, check_generator):
689  added = set()
690  for prefix in prefix_list:
691    prefixes = prefix[0]
692    tool_args = prefix[1]
693    for prefix in prefixes:
694      for func in func_order[prefix]:
695        if added:
696          output_lines.append(comment_string)
697        added.add(func)
698
699        # The add_*_checks routines expect a run list whose items are
700        # tuples that have a list of prefixes as their first element and
701        # tool command args string as their second element.  They output
702        # checks for each prefix in the list of prefixes.  By doing so, it
703        # implicitly assumes that for each function every run line will
704        # generate something for that function.  That is not the case for
705        # generated functions as some run lines might not generate them
706        # (e.g. -fopenmp vs. no -fopenmp).
707        #
708        # Therefore, pass just the prefix we're interested in.  This has
709        # the effect of generating all of the checks for functions of a
710        # single prefix before moving on to the next prefix.  So checks
711        # are ordered by prefix instead of by function as in "normal"
712        # mode.
713        check_generator(output_lines,
714                        [([prefix], tool_args)],
715                        func)
716