|
21 | 21 |
|
22 | 22 | src_dir = sys.argv[1] |
23 | 23 | errcode_map = {} |
| 24 | +errcode_checked = [] |
| 25 | +errcode_not_found = [] |
24 | 26 | error_re = re.compile("(E\d\d\d\d)") |
25 | 27 |
|
| 28 | +def check_unused_error_codes(error_codes, check_error_codes, filenames, dirnames, dirpath): |
| 29 | + for filename in filenames: |
| 30 | + if filename == "diagnostics.rs" or not filename.endswith(".rs"): |
| 31 | + continue |
| 32 | + path = os.path.join(dirpath, filename) |
| 33 | + |
| 34 | + with open(path, 'r') as f: |
| 35 | + for line in f: |
| 36 | + match = error_re.search(line) |
| 37 | + if match: |
| 38 | + errcode = match.group(1) |
| 39 | + if errcode in error_codes: |
| 40 | + error_codes.remove(errcode) |
| 41 | + if errcode not in check_error_codes: |
| 42 | + check_error_codes.append(errcode) |
| 43 | + for dirname in dirnames: |
| 44 | + path = os.path.join(dirpath, dirname) |
| 45 | + for (dirpath, dnames, fnames) in os.walk(path): |
| 46 | + check_unused_error_codes(error_codes, check_error_codes, fnames, dnames, dirpath) |
| 47 | + |
| 48 | + |
26 | 49 | # In the register_long_diagnostics! macro, entries look like this: |
27 | 50 | # |
28 | 51 | # EXXXX: r##" |
|
35 | 58 | long_diag_begin = "r##\"" |
36 | 59 | long_diag_end = "\"##" |
37 | 60 |
|
| 61 | +errors = False |
| 62 | +all_errors = [] |
| 63 | + |
38 | 64 | for (dirpath, dirnames, filenames) in os.walk(src_dir): |
39 | 65 | if "src/test" in dirpath or "src/llvm" in dirpath: |
40 | 66 | # Short circuit for fast |
41 | 67 | continue |
42 | 68 |
|
| 69 | + errcode_to_check = [] |
43 | 70 | for filename in filenames: |
44 | 71 | if filename != "diagnostics.rs": |
45 | 72 | continue |
46 | | - |
47 | 73 | path = os.path.join(dirpath, filename) |
48 | 74 |
|
49 | 75 | with open(path, 'r') as f: |
50 | 76 | inside_long_diag = False |
| 77 | + errcode_to_check = [] |
51 | 78 | for line_num, line in enumerate(f, start=1): |
52 | 79 | if inside_long_diag: |
53 | 80 | # Skip duplicate error code checking for this line |
|
65 | 92 | errcode_map[errcode] = existing + new_record |
66 | 93 | else: |
67 | 94 | errcode_map[errcode] = new_record |
| 95 | + # we don't check if this is a long error explanation |
| 96 | + if (long_diag_begin not in line and not line.strip().startswith("//") |
| 97 | + and errcode not in errcode_to_check and errcode not in errcode_checked |
| 98 | + and errcode not in errcode_not_found): |
| 99 | + errcode_to_check.append(errcode) |
68 | 100 |
|
69 | 101 | if long_diag_begin in line: |
70 | 102 | inside_long_diag = True |
| 103 | + break |
| 104 | + check_unused_error_codes(errcode_to_check, errcode_checked, filenames, dirnames, dirpath) |
| 105 | + if len(errcode_to_check) > 0: |
| 106 | + for errcode in errcode_to_check: |
| 107 | + if errcode in errcode_checked: |
| 108 | + continue |
| 109 | + errcode_not_found.append(errcode) |
| 110 | + |
| 111 | +if len(errcode_not_found) > 0: |
| 112 | + errcode_not_found.sort() |
| 113 | + for errcode in errcode_not_found: |
| 114 | + if errcode in errcode_checked: |
| 115 | + continue |
| 116 | + all_errors.append(errcode) |
| 117 | + print("error: unused error code: " + errcode) |
| 118 | + errors = True |
71 | 119 |
|
72 | | -errors = False |
73 | | -all_errors = [] |
74 | 120 |
|
75 | 121 | for errcode, entries in errcode_map.items(): |
76 | 122 | all_errors.append(entries[0][0]) |
77 | 123 | if len(entries) > 1: |
| 124 | + entries.sort() |
78 | 125 | print("error: duplicate error code " + errcode) |
79 | 126 | for entry in entries: |
80 | 127 | print("{1}: {2}\n{3}".format(*entry)) |
|
0 commit comments