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