[UpdateTestChecks] Update tests option

Summary:
Port of new feature introduced https://reviews.llvm.org/D65610 to other update scripts.

- update_*_checks.py: add an alias -u for --update-only
- port --update-only to other update_*_test_checks.py scripts
- update script aborts if the test file was generated by another update_*_test_checks.py utility

Reviewers: lebedev.ri, RKSimon, MaskRay, reames, gbedwell

Reviewed By: MaskRay

Subscribers: llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D65793

llvm-svn: 368174
diff --git a/llvm/utils/update_mir_test_checks.py b/llvm/utils/update_mir_test_checks.py
index 7a54cc3..83199c8 100755
--- a/llvm/utils/update_mir_test_checks.py
+++ b/llvm/utils/update_mir_test_checks.py
@@ -85,12 +85,6 @@
         print(msg, file=sys.stderr)
 
 
-def warn(msg, test_file=None):
-    if test_file:
-        msg = '{}: {}'.format(test_file, msg)
-    print('WARNING: {}'.format(msg), file=sys.stderr)
-
-
 def find_triple_in_ir(lines, verbose=False):
     for l in lines:
         m = common.TRIPLE_IR_RE.match(l)
@@ -119,16 +113,20 @@
     run_list = []
     all_prefixes = []
     for l in run_lines:
+        if '|' not in l:
+            common.warn('Skipping unparseable RUN line: ' + l)
+            continue
+
         commands = [cmd.strip() for cmd in l.split('|', 1)]
         llc_cmd = commands[0]
         filecheck_cmd = commands[1] if len(commands) > 1 else ''
         common.verify_filecheck_prefixes(filecheck_cmd)
 
         if not llc_cmd.startswith('llc '):
-            warn('Skipping non-llc RUN line: {}'.format(l), test_file=test)
+            common.warn('Skipping non-llc RUN line: {}'.format(l), test_file=test)
             continue
         if not filecheck_cmd.startswith('FileCheck '):
-            warn('Skipping non-FileChecked RUN line: {}'.format(l),
+            common.warn('Skipping non-FileChecked RUN line: {}'.format(l),
                  test_file=test)
             continue
 
@@ -193,7 +191,7 @@
                 log('  {}'.format(l))
         for prefix in prefixes:
             if func in func_dict[prefix] and func_dict[prefix][func] != body:
-                warn('Found conflicting asm for prefix: {}'.format(prefix),
+                common.warn('Found conflicting asm for prefix: {}'.format(prefix),
                      test_file=test)
             func_dict[prefix][func] = body
 
@@ -225,7 +223,7 @@
         func_body.pop(0)
 
     if not func_body:
-        warn('Function has no instructions to check: {}'.format(func_name),
+        common.warn('Function has no instructions to check: {}'.format(func_name),
              test_file=test)
         return
 
@@ -294,49 +292,60 @@
     return True
 
 
-def update_test_file(llc, test, remove_common_prefixes=False, verbose=False):
-    log('Scanning for RUN lines in test file: {}'.format(test), verbose)
+def update_test_file(args, test):
+    log('Scanning for RUN lines in test file: {}'.format(test), args.verbose)
     with open(test) as fd:
         input_lines = [l.rstrip() for l in fd]
 
-    triple_in_ir = find_triple_in_ir(input_lines, verbose)
-    run_lines = find_run_lines(test, input_lines, verbose)
-    run_list, common_prefixes = build_run_list(test, run_lines, verbose)
+    script_name = os.path.basename(__file__)
+    first_line = input_lines[0] if input_lines else ""
+    if 'autogenerated' in first_line and script_name not in first_line:
+        common.warn("Skipping test which wasn't autogenerated by " +
+                    script_name + ": " + test)
+        return
 
-    simple_functions = find_functions_with_one_bb(input_lines, verbose)
+    if args.update_only:
+      if not first_line or 'autogenerated' not in first_line:
+        common.warn("Skipping test which isn't autogenerated: " + test)
+        return
+
+    triple_in_ir = find_triple_in_ir(input_lines, args.verbose)
+    run_lines = find_run_lines(test, input_lines, args.verbose)
+    run_list, common_prefixes = build_run_list(test, run_lines, args.verbose)
+
+    simple_functions = find_functions_with_one_bb(input_lines, args.verbose)
 
     func_dict = {}
     for run in run_list:
         for prefix in run.prefixes:
             func_dict.update({prefix: dict()})
     for prefixes, llc_args, triple_in_cmd in run_list:
-        log('Extracted LLC cmd: llc {}'.format(llc_args), verbose)
-        log('Extracted FileCheck prefixes: {}'.format(prefixes), verbose)
+        log('Extracted LLC cmd: llc {}'.format(llc_args), args.verbose)
+        log('Extracted FileCheck prefixes: {}'.format(prefixes), args.verbose)
 
-        raw_tool_output = llc(llc_args, test)
+        raw_tool_output = args.llc(llc_args, test)
         if not triple_in_cmd and not triple_in_ir:
-            warn('No triple found: skipping file', test_file=test)
+            common.warn('No triple found: skipping file', test_file=test)
             return
 
         build_function_body_dictionary(test, raw_tool_output,
                                        triple_in_cmd or triple_in_ir,
-                                       prefixes, func_dict, verbose)
+                                       prefixes, func_dict, args.verbose)
 
     state = 'toplevel'
     func_name = None
     prefix_set = set([prefix for run in run_list for prefix in run.prefixes])
-    log('Rewriting FileCheck prefixes: {}'.format(prefix_set), verbose)
+    log('Rewriting FileCheck prefixes: {}'.format(prefix_set), args.verbose)
 
-    if remove_common_prefixes:
+    if args.remove_common_prefixes:
         prefix_set.update(common_prefixes)
     elif common_prefixes:
-        warn('Ignoring common prefixes: {}'.format(common_prefixes),
+        common.warn('Ignoring common prefixes: {}'.format(common_prefixes),
              test_file=test)
 
     comment_char = '#' if test.endswith('.mir') else ';'
     autogenerated_note = ('{} NOTE: Assertions have been autogenerated by '
-                          'utils/{}'.format(comment_char,
-                                            os.path.basename(__file__)))
+                          'utils/{}'.format(comment_char, script_name))
     output_lines = []
     output_lines.append(autogenerated_note)
 
@@ -374,14 +383,14 @@
                 state = 'mir function body'
                 add_checks_for_function(test, output_lines, run_list,
                                         func_dict, func_name, single_bb=False,
-                                        verbose=verbose)
+                                        verbose=args.verbose)
         elif state == 'mir function prefix':
             m = MIR_PREFIX_DATA_RE.match(input_line)
             if not m:
                 state = 'mir function body'
                 add_checks_for_function(test, output_lines, run_list,
                                         func_dict, func_name, single_bb=True,
-                                        verbose=verbose)
+                                        verbose=args.verbose)
 
             if should_add_line_to_output(input_line, prefix_set):
                 output_lines.append(input_line)
@@ -397,7 +406,7 @@
                 state = 'ir function body'
                 add_checks_for_function(test, output_lines, run_list,
                                         func_dict, func_name, single_bb=False,
-                                        verbose=verbose)
+                                        verbose=args.verbose)
 
             if should_add_line_to_output(input_line, prefix_set):
                 output_lines.append(input_line)
@@ -409,7 +418,7 @@
                 output_lines.append(input_line)
 
 
-    log('Writing {} lines to {}...'.format(len(output_lines), test), verbose)
+    log('Writing {} lines to {}...'.format(len(output_lines), test), args.verbose)
 
     with open(test, 'wb') as fd:
         fd.writelines(['{}\n'.format(l).encode('utf-8') for l in output_lines])
@@ -425,16 +434,17 @@
     parser.add_argument('--remove-common-prefixes', action='store_true',
                         help='Remove existing check lines whose prefixes are '
                              'shared between multiple commands')
+    parser.add_argument('-u', '--update-only', action='store_true',
+                        help='Only update test if it was already autogened')
     parser.add_argument('tests', nargs='+')
     args = parser.parse_args()
 
     test_paths = [test for pattern in args.tests for test in glob.glob(pattern)]
     for test in test_paths:
         try:
-            update_test_file(args.llc, test, args.remove_common_prefixes,
-                             verbose=args.verbose)
+            update_test_file(args, test)
         except Exception:
-            warn('Error processing file', test_file=test)
+            common.warn('Error processing file', test_file=test)
             raise