]> git.lizzy.rs Git - rust.git/blob - util/lintlib.py
c28177e10629c927618cea9bfbc226153e9de0da
[rust.git] / util / lintlib.py
1 # Common utils for the several housekeeping scripts.
2
3 import os
4 import re
5 import collections
6
7 import logging as log
8 log.basicConfig(level=log.INFO, format='%(levelname)s: %(message)s')
9
10 Lint = collections.namedtuple('Lint', 'name level doc sourcefile group')
11 Config = collections.namedtuple('Config', 'name ty doc default')
12
13 lintname_re = re.compile(r'''pub\s+([A-Z_][A-Z_0-9]*)''')
14 group_re = re.compile(r'''\s*([a-z_][a-z_0-9]+)''')
15 conf_re = re.compile(r'''define_Conf! {\n([^}]*)\n}''', re.MULTILINE)
16 confvar_re = re.compile(
17     r'''/// Lint: (\w+). (.*).*\n\s*\([^,]+,\s+"([^"]+)",\s+([^=\)]+)=>\s+(.*)\),''', re.MULTILINE)
18
19 lint_levels = {
20     "correctness": 'Deny',
21     "style": 'Warn',
22     "complexity": 'Warn',
23     "perf": 'Warn',
24     "restriction": 'Allow',
25     "pedantic": 'Allow',
26     "nursery": 'Allow',
27 }
28
29
30 def parse_lints(lints, filepath):
31     last_comment = []
32     comment = True
33     clippy = False
34     deprecated = False
35     name = ""
36
37     with open(filepath) as fp:
38         for line in fp:
39             if comment:
40                 if line.startswith("/// "):
41                     last_comment.append(line[4:])
42                 elif line.startswith("///"):
43                     last_comment.append(line[3:])
44                 elif line.startswith("declare_lint!"):
45                     import sys
46                     print "don't use `declare_lint!` in clippy, use `declare_clippy_lint!` instead"
47                     sys.exit(42)
48                 elif line.startswith("declare_clippy_lint!"):
49                     comment = False
50                     deprecated = False
51                     clippy = True
52                     name = ""
53                 elif line.startswith("declare_deprecated_lint!"):
54                     comment = False
55                     deprecated = True
56                     clippy = False
57                 else:
58                     last_comment = []
59             if not comment:
60                 m = lintname_re.search(line)
61
62                 if m:
63                     name = m.group(1).lower()
64                     line = next(fp)
65
66                     if deprecated:
67                         level = "Deprecated"
68                         group = "deprecated"
69                     else:
70                         while True:
71                             g = group_re.search(line)
72                             if g:
73                                 group = g.group(1).lower()
74                                 level = lint_levels[group]
75                                 break
76                             line = next(fp)
77
78                     log.info("found %s with level %s in %s",
79                              name, level, filepath)
80                     lints.append(Lint(name, level, last_comment, filepath, group))
81                     last_comment = []
82                     comment = True
83
84                     if "}" in line:
85                         log.warn("Warning: missing Lint-Name in %s", filepath)
86                         comment = True
87
88
89 def parse_configs(path):
90     configs = {}
91     with open(os.path.join(path, 'utils/conf.rs')) as fp:
92         contents = fp.read()
93
94     match = re.search(conf_re, contents)
95     confvars = re.findall(confvar_re, match.group(1))
96
97     for (lint, doc, name, default, ty) in confvars:
98         configs[lint.lower()] = Config(name.replace("_", "-"), ty, doc, default)
99
100     return configs
101
102
103 def parse_all(path="clippy_lints/src"):
104     lints = []
105     for filename in os.listdir(path):
106         if filename.endswith(".rs"):
107             parse_lints(lints, os.path.join(path, filename))
108     log.info("got %s lints", len(lints))
109
110     configs = parse_configs(path)
111     log.info("got %d configs", len(configs))
112
113     return lints, configs