[clang] [update_cc_test_checks] Use lit's shell to run commands (PR #65333)

Thomas Preud'homme via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 6 05:57:11 PDT 2023


================
@@ -306,52 +276,79 @@ def main():
         run_list = []
         line2func_list = collections.defaultdict(list)
 
-        subs = {
-            "%s": ti.path,
-            "%t": tempfile.NamedTemporaryFile().name,
-            "%S": os.path.dirname(ti.path),
-        }
+        subs = [
+            ("%s", ti.path),
+            ("%t", tempfile.NamedTemporaryFile().name),
+            ("%S", os.path.dirname(ti.path)),
+        ]
 
         for l in ti.run_lines:
-            commands = [cmd.strip() for cmd in l.split("|")]
-
+            pipeline = ShParser(l, win32Escapes=False, pipefail=False).parse()
+            if not isinstance(pipeline, Pipeline):
+                # Could be a sequence separated by &&/||/; but we don't handle that yet.
+                print(
+                    "WARNING: RUN: line is too complex for this script: ",
+                    pipeline,
+                    file=sys.stderr,
+                )
+                continue
             triple_in_cmd = None
-            m = common.TRIPLE_ARG_RE.search(commands[0])
+            clang_cmd: Command = pipeline.commands[0]
+            m = common.TRIPLE_ARG_RE.search(" ".join(clang_cmd.args))
             if m:
                 triple_in_cmd = m.groups()[0]
 
+            # Do lit-like substitutions on the command and redirects.
+            for cmd in pipeline.commands:
+                if cmd.args[0] == "opt":
+                    if ti.args.opt is None:
+                        sys.exit(
+                            ti.path + " needs to run opt. "
+                            "Please specify --llvm-bin or --opt"
+                        )
+                    cmd.args[0] = ti.args.opt
+                cmd.args = [common.applySubstitutions(i, subs) for i in cmd.args]
+                for i, redirect in enumerate(cmd.redirects):
+                    cmd.redirects[i] = redirect[0], common.applySubstitutions(
+                        redirect[1], subs
+                    )
+
             # Parse executable args.
-            exec_args = shlex.split(commands[0])
             # Execute non-clang runline.
-            if exec_args[0] not in SUBST:
-                # Do lit-like substitutions.
-                for s in subs:
-                    exec_args = [
-                        i.replace(s, subs[s]) if s in i else i for i in exec_args
-                    ]
-                run_list.append((None, exec_args, None, None))
+            if clang_cmd.args[0] not in SUBST:
+                # Ignore FileCheck-only 'RUN: lines'
+                if pipeline.commands[0].args[0] == "FileCheck":
+                    print(
+                        "NOTE: Skipping FileCheck-only RUN line: ",
+                        pipeline,
+                        file=sys.stderr,
+                    )
+                    continue
+                run_list.append((None, pipeline, None))
                 continue
             # This is a clang runline, apply %clang substitution rule, do lit-like substitutions,
             # and append args.clang_args
-            clang_args = exec_args
-            clang_args[0:1] = SUBST[clang_args[0]]
-            for s in subs:
-                clang_args = [
-                    i.replace(s, subs[s]) if s in i else i for i in clang_args
-                ]
-            clang_args += ti.args.clang_args
+            clang_cmd.args[0:1] = SUBST[clang_cmd.args[0]]
+            print(clang_cmd)
+            clang_cmd.args.insert(0, ti.args.clang)
+            clang_cmd.args += ti.args.clang_args
+            # Remove all -verify arguments since they could cause the IR generation to fail
+            clang_cmd.args = [x for x in clang_cmd.args if not x.startswith("-verify")]
----------------
RoboTux wrote:

Wouldn't you want the tool to fail then?

https://github.com/llvm/llvm-project/pull/65333


More information about the cfe-commits mailing list