llvm-project
73 строки · 1.8 Кб
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 argparse9import os10import shutil11import signal12
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:25if args.passes == "EXPAND_a_to_f":26print("a,b,c,d,e,f")27else:28print(args.passes)29exit(0)30
31# Parse '-crash-seq'.
32crash_seq = []33tok = ""34for c in args.crash_seq:35if c == ",":36if tok != "":37crash_seq.append(tok)38tok = ""39else:40tok += c41if tok != "":42crash_seq.append(tok)43print(crash_seq)44
45# Parse '-passes' and see if we need to crash.
46tok = ""47for c in args.passes:48if c == ",":49if len(crash_seq) > 0 and crash_seq[0] == tok:50crash_seq.pop(0)51tok = ""52elif c == "(":53tok = ""54elif c == ")":55if len(crash_seq) > 0 and crash_seq[0] == tok:56crash_seq.pop(0)57tok = ""58else:59tok += c60if len(crash_seq) > 0 and crash_seq[0] == tok:61crash_seq.pop(0)62
63# Copy input to output.
64if args.output:65shutil.copy(args.input, args.output)66
67# Crash if all 'crash_seq' passes occurred in right order.
68if len(crash_seq) == 0:69print("crash")70os.kill(os.getpid(), signal.SIGKILL)71else:72print("no crash")73exit(0)74