]> git.lizzy.rs Git - rust.git/blob - src/etc/errorck.py
Rollup merge of #21964 - semarie:openbsd-env, r=alexcrichton
[rust.git] / src / etc / errorck.py
1 # Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 # file at the top-level directory of this distribution and at
3 # http://rust-lang.org/COPYRIGHT.
4 #
5 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 # option. This file may not be copied, modified, or distributed
9 # except according to those terms.
10
11 # Digs error codes out of files named 'diagnostics.rs' across
12 # the tree, and ensures thare are no duplicates.
13
14 import sys
15 import os
16 import re
17
18 if len(sys.argv) < 2:
19     print "usage: errorck.py <src-dir>"
20     sys.exit(1)
21
22 src_dir = sys.argv[1]
23 errcode_map = {}
24 error_re = re.compile("(E\d\d\d\d)")
25
26 for (dirpath, dirnames, filenames) in os.walk(src_dir):
27     if "src/test" in dirpath or "src/llvm" in dirpath:
28         # Short circuit for fast
29         continue
30
31     for filename in filenames:
32         if filename != "diagnostics.rs":
33             continue
34
35         path = os.path.join(dirpath, filename)
36
37         with open(path, 'r') as f:
38             for line_num, line in enumerate(f, start=1):
39                 match = error_re.search(line)
40                 if match:
41                     errcode = match.group(1)
42                     new_record = [(errcode, path, line_num, line)]
43                     existing = errcode_map.get(errcode)
44                     if existing is not None:
45                         # This is a dupe
46                         errcode_map[errcode] = existing + new_record
47                     else:
48                         errcode_map[errcode] = new_record
49
50 errors = False
51 all_errors = []
52
53 for errcode, entries in errcode_map.items():
54     all_errors.append(entries[0][0])
55     if len(entries) > 1:
56         print("error: duplicate error code " + errcode)
57         for entry in entries:
58             print("{1}: {2}\n{3}".format(*entry))
59         errors = True
60
61 print
62 print("* {0} error codes".format(len(errcode_map)))
63 print("* highest error code: " + max(all_errors))
64 print
65
66 if errors:
67     sys.exit(1)