xref: /llvm-project/llvm/utils/reduce_pipeline_test/fake_opt.py (revision b71edfaa4ec3c998aadb35255ce2f60bba2940b0)
1#!/usr/bin/env python3
2
3# Automatically formatted with yapf (https://github.com/google/yapf)
4
5# Fake 'opt' program that can be made to crash on request. For testing
6# the 'reduce_pipeline.py' automatic 'opt' NPM pipeline reducer.
7
8import argparse
9import os
10import shutil
11import signal
12
13parser = argparse.ArgumentParser()
14parser.add_argument("-passes", action="store", dest="passes", required=True)
15parser.add_argument(
16    "-print-pipeline-passes", dest="print_pipeline_passes", action="store_true"
17)
18parser.add_argument("-crash-seq", action="store", dest="crash_seq", required=True)
19parser.add_argument("-o", action="store", dest="output")
20parser.add_argument("input")
21[args, unknown_args] = parser.parse_known_args()
22
23# Expand pipeline if '-print-pipeline-passes'.
24if args.print_pipeline_passes:
25    if args.passes == "EXPAND_a_to_f":
26        print("a,b,c,d,e,f")
27    else:
28        print(args.passes)
29    exit(0)
30
31# Parse '-crash-seq'.
32crash_seq = []
33tok = ""
34for c in args.crash_seq:
35    if c == ",":
36        if tok != "":
37            crash_seq.append(tok)
38        tok = ""
39    else:
40        tok += c
41if tok != "":
42    crash_seq.append(tok)
43print(crash_seq)
44
45# Parse '-passes' and see if we need to crash.
46tok = ""
47for c in args.passes:
48    if c == ",":
49        if len(crash_seq) > 0 and crash_seq[0] == tok:
50            crash_seq.pop(0)
51        tok = ""
52    elif c == "(":
53        tok = ""
54    elif c == ")":
55        if len(crash_seq) > 0 and crash_seq[0] == tok:
56            crash_seq.pop(0)
57        tok = ""
58    else:
59        tok += c
60if len(crash_seq) > 0 and crash_seq[0] == tok:
61    crash_seq.pop(0)
62
63# Copy input to output.
64if args.output:
65    shutil.copy(args.input, args.output)
66
67# Crash if all 'crash_seq' passes occurred in right order.
68if len(crash_seq) == 0:
69    print("crash")
70    os.kill(os.getpid(), signal.SIGKILL)
71else:
72    print("no crash")
73    exit(0)
74