]> git.lizzy.rs Git - rust.git/blob - src/grammar/testparser.py
Auto merge of #31077 - nagisa:mir-temp-promotion, r=dotdash
[rust.git] / src / grammar / testparser.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2015 The Rust Project Developers. See the COPYRIGHT
4 # file at the top-level directory of this distribution and at
5 # http://rust-lang.org/COPYRIGHT.
6 #
7 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 # option. This file may not be copied, modified, or distributed
11 # except according to those terms.
12
13 # ignore-tidy-linelength
14
15 import sys
16
17 import os
18 import subprocess
19 import argparse
20
21 # usage: testparser.py [-h] [-p PARSER [PARSER ...]] -s SOURCE_DIR
22
23 # Parsers should read from stdin and return exit status 0 for a
24 # successful parse, and nonzero for an unsuccessful parse
25
26 parser = argparse.ArgumentParser()
27 parser.add_argument('-p', '--parser', nargs='+')
28 parser.add_argument('-s', '--source-dir', nargs=1, required=True)
29 args = parser.parse_args(sys.argv[1:])
30
31 total = 0
32 ok = {}
33 bad = {}
34 for parser in args.parser:
35     ok[parser] = 0
36     bad[parser] = []
37 devnull = open(os.devnull, 'w')
38 print("\n")
39
40 for base, dirs, files in os.walk(args.source_dir[0]):
41     for f in filter(lambda p: p.endswith('.rs'), files):
42         p = os.path.join(base, f)
43         parse_fail = 'parse-fail' in p
44         if sys.version_info.major == 3:
45             lines = open(p, encoding='utf-8').readlines()
46         else:
47             lines = open(p).readlines()
48         if any('ignore-test' in line or 'ignore-lexer-test' in line for line in lines):
49             continue
50         total += 1
51         for parser in args.parser:
52             if subprocess.call(parser, stdin=open(p), stderr=subprocess.STDOUT, stdout=devnull) == 0:
53                 if parse_fail:
54                     bad[parser].append(p)
55                 else:
56                     ok[parser] += 1
57             else:
58                 if parse_fail:
59                     ok[parser] += 1
60                 else:
61                     bad[parser].append(p)
62         parser_stats = ', '.join(['{}: {}'.format(parser, ok[parser]) for parser in args.parser])
63         sys.stdout.write("\033[K\r total: {}, {}, scanned {}"
64                          .format(total, os.path.relpath(parser_stats), os.path.relpath(p)))
65
66 devnull.close()
67
68 print("\n")
69
70 for parser in args.parser:
71     filename = os.path.basename(parser) + '.bad'
72     print("writing {} files that did not yield the correct result with {} to {}".format(len(bad[parser]), parser, filename))
73     with open(filename, "w") as f:
74         for p in bad[parser]:
75             f.write(p)
76             f.write("\n")