[UpdateTestChecks] Share the code to parse RUN: lines between all scripts

Summary:
This commit also introduces a common.debug() function to avoid many
`if args.verbose:` statements. Depends on D70428.

Reviewers: xbolva00, MaskRay, jdoerfert

Reviewed By: MaskRay

Subscribers: llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D70432
diff --git a/llvm/utils/update_cc_test_checks.py b/llvm/utils/update_cc_test_checks.py
index a3227c6..414056f 100755
--- a/llvm/utils/update_cc_test_checks.py
+++ b/llvm/utils/update_cc_test_checks.py
@@ -29,7 +29,6 @@
 ADVERT = '// NOTE: Assertions have been autogenerated by '
 
 CHECK_RE = re.compile(r'^\s*//\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:')
-RUN_LINE_RE = re.compile(r'^//\s*RUN:\s*(.*)$')
 
 SUBST = {
     '%clang': [],
@@ -38,9 +37,6 @@
 }
 
 def get_line2spell_and_mangled(args, clang_args):
-  def debug_mangled(*print_args, **kwargs):
-    if args.verbose:
-      print(*print_args, file=sys.stderr, **kwargs)
   ret = {}
   # Use clang's JSON AST dump to get the mangled name
   json_dump_args = [args.clang, *clang_args, '-fsyntax-only', '-o', '-']
@@ -49,7 +45,7 @@
     # -Xclang -ast-dump=json instead:
     json_dump_args.append('-Xclang')
   json_dump_args.append('-ast-dump=json')
-  debug_mangled('Running', ' '.join(json_dump_args))
+  common.debug('Running', ' '.join(json_dump_args))
   status = subprocess.run(json_dump_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   if status.returncode != 0:
     sys.stderr.write('Failed to run ' + ' '.join(json_dump_args) + '\n')
@@ -67,20 +63,19 @@
     if node['kind'] != 'FunctionDecl':
       continue
     if node.get('isImplicit') is True and node.get('storageClass') == 'extern':
-      debug_mangled('Skipping builtin function:', node['name'], '@', node['loc'])
+      common.debug('Skipping builtin function:', node['name'], '@', node['loc'])
       continue
-    debug_mangled('Found function:', node['kind'], node['name'], '@', node['loc'])
+    common.debug('Found function:', node['kind'], node['name'], '@', node['loc'])
     line = node['loc'].get('line')
     # If there is no line it is probably a builtin function -> skip
     if line is None:
-      debug_mangled('Skipping function without line number:', node['name'], '@', node['loc'])
+      common.debug('Skipping function without line number:', node['name'], '@', node['loc'])
       continue
     spell = node['name']
     mangled = node.get('mangledName', spell)
     ret[int(line)-1] = (spell, mangled)
-  if args.verbose:
-    for line, func_name in sorted(ret.items()):
-      print('line {}: found function {}'.format(line+1, func_name), file=sys.stderr)
+  for line, func_name in sorted(ret.items()):
+    common.debug('line {}: found function {}'.format(line+1, func_name), file=sys.stderr)
   if not ret:
     common.warn('Did not find any functions using', ' '.join(json_dump_args))
   return ret
@@ -191,19 +186,7 @@
         continue
 
     # Extract RUN lines.
-    raw_lines = [m.group(1)
-                 for m in [RUN_LINE_RE.match(l) for l in input_lines] if m]
-    run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
-    for l in raw_lines[1:]:
-      if run_lines[-1].endswith("\\"):
-        run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
-      else:
-        run_lines.append(l)
-
-    if args.verbose:
-      print('Found {} RUN lines:'.format(len(run_lines)), file=sys.stderr)
-      for l in run_lines:
-        print('  RUN: ' + l, file=sys.stderr)
+    run_lines = common.find_run_lines(filename, input_lines)
 
     # Build a list of clang command lines and check prefixes from RUN lines.
     run_list = []
@@ -260,9 +243,8 @@
       for prefix in prefixes:
         func_dict.update({prefix: dict()})
     for prefixes, clang_args, extra_commands, triple_in_cmd in run_list:
-      if args.verbose:
-        print('Extracted clang cmd: clang {}'.format(clang_args), file=sys.stderr)
-        print('Extracted FileCheck prefixes: {}'.format(prefixes), file=sys.stderr)
+      common.debug('Extracted clang cmd: clang {}'.format(clang_args))
+      common.debug('Extracted FileCheck prefixes: {}'.format(prefixes))
 
       get_function_body(args, filename, clang_args, extra_commands, prefixes, triple_in_cmd, func_dict)