[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_test_checks.py b/llvm/utils/update_test_checks.py
index 31122b2..3fd8dd7 100755
--- a/llvm/utils/update_test_checks.py
+++ b/llvm/utils/update_test_checks.py
@@ -84,8 +84,6 @@
   # On Windows we must expand the patterns ourselves.
   test_paths = [test for pattern in args.tests for test in glob.glob(pattern)]
   for test in test_paths:
-    if args.verbose:
-      print('Scanning for RUN lines in test file: ' + test, file=sys.stderr)
     with open(test) as f:
       input_lines = [l.rstrip() for l in f]
 
@@ -99,20 +97,7 @@
         common.warn("Skipping test which isn't autogenerated: " + test)
         continue
 
-    raw_lines = [m.group(1)
-                 for m in [common.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 %d RUN lines:' % (len(run_lines),), file=sys.stderr)
-      for l in run_lines:
-        print('  RUN: ' + l, file=sys.stderr)
-
+    run_lines = common.find_run_lines(test, input_lines)
     prefix_list = []
     for l in run_lines:
       if '|' not in l:
@@ -146,9 +131,8 @@
       for prefix in prefixes:
         func_dict.update({prefix: dict()})
     for prefixes, opt_args in prefix_list:
-      if args.verbose:
-        print('Extracted opt cmd: ' + opt_basename + ' ' + opt_args, file=sys.stderr)
-        print('Extracted FileCheck prefixes: ' + str(prefixes), file=sys.stderr)
+      common.debug('Extracted opt cmd: ' + opt_basename + ' ' + opt_args)
+      common.debug('Extracted FileCheck prefixes: ' + str(prefixes))
 
       raw_tool_output = common.invoke_tool(args.opt_binary, opt_args, test)
       common.build_function_body_dictionary(
@@ -159,8 +143,7 @@
     is_in_function = False
     is_in_function_start = False
     prefix_set = set([prefix for prefixes, _ in prefix_list for prefix in prefixes])
-    if args.verbose:
-      print('Rewriting FileCheck prefixes: %s' % (prefix_set,), file=sys.stderr)
+    common.debug('Rewriting FileCheck prefixes:', str(prefix_set))
     output_lines = []
     output_lines.append(autogenerated_note)
 
@@ -207,8 +190,7 @@
         continue
       is_in_function = is_in_function_start = True
 
-    if args.verbose:
-      print('Writing %d lines to %s...' % (len(output_lines), test), file=sys.stderr)
+    common.debug('Writing %d lines to %s...' % (len(output_lines), test))
 
     with open(test, 'wb') as f:
       f.writelines(['{}\n'.format(l).encode('utf-8') for l in output_lines])