arichardson created this revision.
arichardson added reviewers: vitalybuka, jdoerfert, MaskRay.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.

Not passing --clang would result in a python exception after this change:
(TypeError: expected str, bytes or os.PathLike object, not NoneType)
because the --clang argument default was only being populated in the
initial argument parsing pass but not later on.
Fix this by adding an argparse callback to set the default values.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84511

Files:
  clang/test/utils/update_cc_test_checks/basic-cplusplus.test
  clang/test/utils/update_cc_test_checks/lit.local.cfg
  llvm/utils/UpdateTestChecks/common.py
  llvm/utils/update_cc_test_checks.py

Index: llvm/utils/update_cc_test_checks.py
===================================================================
--- llvm/utils/update_cc_test_checks.py
+++ llvm/utils/update_cc_test_checks.py
@@ -112,6 +112,20 @@
     return []
   return shlex.split(value)
 
+
+def infer_dependent_args(args):
+  if args.clang is None:
+    if args.llvm_bin is None:
+      args.clang = 'clang'
+    else:
+      args.clang = os.path.join(args.llvm_bin, 'clang')
+  if args.opt is None:
+    if args.llvm_bin is None:
+      args.opt = 'opt'
+    else:
+      args.opt = os.path.join(args.llvm_bin, 'opt')
+
+
 def config():
   parser = argparse.ArgumentParser(
       description=__doc__,
@@ -135,12 +149,8 @@
                       help='Check "Function Attributes" for functions')
   parser.add_argument('tests', nargs='+')
   args = common.parse_commandline_args(parser)
+  infer_dependent_args(args)
 
-  if args.clang is None:
-    if args.llvm_bin is None:
-      args.clang = 'clang'
-    else:
-      args.clang = os.path.join(args.llvm_bin, 'clang')
   if not distutils.spawn.find_executable(args.clang):
     print('Please specify --llvm-bin or --clang', file=sys.stderr)
     sys.exit(1)
@@ -157,11 +167,6 @@
     common.warn('Could not determine clang builtins directory, some tests '
                 'might not update correctly.')
 
-  if args.opt is None:
-    if args.llvm_bin is None:
-      args.opt = 'opt'
-    else:
-      args.opt = os.path.join(args.llvm_bin, 'opt')
   if not distutils.spawn.find_executable(args.opt):
     # Many uses of this tool will not need an opt binary, because it's only
     # needed for updating a test that runs clang | opt | FileCheck. So we
@@ -203,7 +208,7 @@
   script_name = os.path.basename(__file__)
 
   for ti in common.itertests(initial_args.tests, parser, 'utils/' + script_name,
-                             comment_prefix='//'):
+                             comment_prefix='//', argparse_callback=infer_dependent_args):
     # Build a list of clang command lines and check prefixes from RUN lines.
     run_list = []
     line2spell_and_mangled_list = collections.defaultdict(list)
Index: llvm/utils/UpdateTestChecks/common.py
===================================================================
--- llvm/utils/UpdateTestChecks/common.py
+++ llvm/utils/UpdateTestChecks/common.py
@@ -44,8 +44,9 @@
 
 class TestInfo(object):
   def __init__(self, test, parser, script_name, input_lines, args, argv,
-               comment_prefix):
+               comment_prefix, argparse_callback):
     self.parser = parser
+    self.argparse_callback = argparse_callback
     self.path = test
     self.args = args
     self.argv = argv
@@ -68,14 +69,14 @@
       if input_line.startswith(self.autogenerated_note_prefix):
         continue
       self.args, self.argv = check_for_command(input_line, self.parser,
-                                               self.args, self.argv)
+                                               self.args, self.argv, self.argparse_callback)
       if not self.args.enabled:
         output_lines.append(input_line)
         continue
       yield InputLineInfo(input_line, line_num, self.args, self.argv)
 
 
-def itertests(test_patterns, parser, script_name, comment_prefix=None):
+def itertests(test_patterns, parser, script_name, comment_prefix=None, argparse_callback=None):
   for pattern in test_patterns:
     # On Windows we must expand the patterns ourselves.
     tests_list = glob.glob(pattern)
@@ -86,19 +87,21 @@
       with open(test) as f:
         input_lines = [l.rstrip() for l in f]
       args = parser.parse_args()
+      if argparse_callback is not None:
+        argparse_callback(args)
       argv = sys.argv[:]
       first_line = input_lines[0] if input_lines else ""
       if UTC_ADVERT in first_line:
         if script_name not in first_line and not args.force_update:
           warn("Skipping test which wasn't autogenerated by " + script_name, test)
           continue
-        args, argv = check_for_command(first_line, parser, args, argv)
+        args, argv = check_for_command(first_line, parser, args, argv, argparse_callback)
       elif args.update_only:
         assert UTC_ADVERT not in first_line
         warn("Skipping test which isn't autogenerated: " + test)
         continue
       yield TestInfo(test, parser, script_name, input_lines, args, argv,
-                     comment_prefix)
+                     comment_prefix, argparse_callback)
 
 
 def should_add_line_to_output(input_line, prefix_set):
@@ -510,10 +513,12 @@
   return autogenerated_note_args
 
 
-def check_for_command(line, parser, args, argv):
+def check_for_command(line, parser, args, argv, argparse_callback):
     cmd_m = UTC_ARGS_CMD.match(line)
     if cmd_m:
         cmd = cmd_m.group('cmd').strip().split(' ')
         argv = argv + cmd
         args = parser.parse_args(filter(lambda arg: arg not in args.tests, argv))
+        if argparse_callback is not None:
+          argparse_callback(args)
     return args, argv
Index: clang/test/utils/update_cc_test_checks/lit.local.cfg
===================================================================
--- clang/test/utils/update_cc_test_checks/lit.local.cfg
+++ clang/test/utils/update_cc_test_checks/lit.local.cfg
@@ -19,7 +19,14 @@
 script_path = os.path.join(config.llvm_src_root, 'utils',
                            'update_cc_test_checks.py')
 assert os.path.isfile(script_path)
+# This substitution is used for check that using --llmv-bin instead of --clang
+# works as expected
+config.substitutions.append(
+    ('%update_cc_test_checks_llvm_bin', "%s %s %s" % (
+        shell_quote(config.python_executable), shell_quote(script_path),
+        '--llvm-bin ' + shell_quote(config.clang_tools_dir))))
 config.substitutions.append(
     ('%update_cc_test_checks', "%s %s %s" % (
         shell_quote(config.python_executable), shell_quote(script_path),
         extra_args)))
+
Index: clang/test/utils/update_cc_test_checks/basic-cplusplus.test
===================================================================
--- clang/test/utils/update_cc_test_checks/basic-cplusplus.test
+++ clang/test/utils/update_cc_test_checks/basic-cplusplus.test
@@ -2,6 +2,10 @@
 
 # RUN: cp %S/Inputs/basic-cplusplus.cpp %t.cpp && %update_cc_test_checks %t.cpp
 # RUN: diff -u %S/Inputs/basic-cplusplus.cpp.expected %t.cpp
+## Check that it also works with the --llvm-bin flag instead of --clang
+# RUN: cp %S/Inputs/basic-cplusplus.cpp %t.cpp && %update_cc_test_checks_llvm_bin %t.cpp
+# RUN: diff -u %S/Inputs/basic-cplusplus.cpp.expected %t.cpp
 ## Check that re-running update_cc_test_checks doesn't change the output
 # RUN: %update_cc_test_checks %t.cpp
 # RUN: diff -u %S/Inputs/basic-cplusplus.cpp.expected %t.cpp
+
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to