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.
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.
11 # Digs error codes out of files named 'diagnostics.rs' across
12 # the tree, and ensures thare are no duplicates.
19 print("usage: errorck.py <src-dir>")
24 error_re = re.compile("(E\d\d\d\d)")
26 # In the register_long_diagnostics! macro, entries look like this:
29 # <Long diagnostic message>
32 # These two variables are for detecting the beginning and end of diagnostic
33 # messages so that duplicate error codes are not reported when a code occurs
34 # inside a diagnostic message
35 long_diag_begin = "r##\""
36 long_diag_end = "\"##"
38 for (dirpath, dirnames, filenames) in os.walk(src_dir):
39 if "src/test" in dirpath or "src/llvm" in dirpath:
40 # Short circuit for fast
43 for filename in filenames:
44 if filename != "diagnostics.rs":
47 path = os.path.join(dirpath, filename)
49 with open(path, 'r') as f:
50 inside_long_diag = False
51 for line_num, line in enumerate(f, start=1):
53 # Skip duplicate error code checking for this line
54 if long_diag_end in line:
55 inside_long_diag = False
58 match = error_re.search(line)
60 errcode = match.group(1)
61 new_record = [(errcode, path, line_num, line)]
62 existing = errcode_map.get(errcode)
63 if existing is not None:
65 errcode_map[errcode] = existing + new_record
67 errcode_map[errcode] = new_record
69 if long_diag_begin in line:
70 inside_long_diag = True
75 for errcode, entries in errcode_map.items():
76 all_errors.append(entries[0][0])
78 print("error: duplicate error code " + errcode)
80 print("{1}: {2}\n{3}".format(*entry))
84 print("* {0} error codes".format(len(errcode_map)))
85 print("* highest error code: " + max(all_errors))