1 # Common utils for the several housekeeping scripts.
8 log.basicConfig(level=log.INFO, format='%(levelname)s: %(message)s')
10 Lint = collections.namedtuple('Lint', 'name level doc sourcefile group')
11 Config = collections.namedtuple('Config', 'name ty doc default')
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})[\s\S])*\n}''', re.MULTILINE)
16 confvar_re = re.compile(
17 r'''/// Lint: ([\w,\s]+)\. (.*)\n\s*\(([^:]+):\s*([^\s=]+)\s*=\s*([^\.\)]+).*\),''', re.MULTILINE)
18 comment_re = re.compile(r'''\s*/// ?(.*)''')
21 "correctness": 'Deny',
26 "restriction": 'Allow',
33 def parse_lints(lints, filepath):
39 with open(filepath) as fp:
41 if clippy or deprecated:
42 m = lintname_re.search(line)
44 name = m.group(1).lower()
52 g = group_re.search(line)
54 group = g.group(1).lower()
55 level = lint_levels.get(group, None)
62 log.info("found %s with level %s in %s",
63 name, level, filepath)
64 lints.append(Lint(name, level, comment, filepath, group))
71 m = comment_re.search(line)
73 comment.append(m.group(1))
74 elif line.startswith("declare_clippy_lint!"):
77 elif line.startswith("declare_deprecated_lint!"):
80 elif line.startswith("declare_lint!"):
83 "don't use `declare_lint!` in Clippy, "
84 "use `declare_clippy_lint!` instead"
89 def parse_configs(path):
91 with open(os.path.join(path, 'utils/conf.rs')) as fp:
94 match = re.search(conf_re, contents)
95 confvars = re.findall(confvar_re, match.group(0))
97 for (lints, doc, name, ty, default) in confvars:
98 for lint in lints.split(','):
99 configs[lint.strip().lower()] = Config(name.replace("_", "-"), ty, doc, default)
103 def parse_all(path="clippy_lints/src"):
105 for root, dirs, files in os.walk(path):
107 if fn.endswith('.rs'):
108 parse_lints(lints, os.path.join(root, fn))
110 log.info("got %s lints", len(lints))
112 configs = parse_configs(path)
113 log.info("got %d configs", len(configs))
115 return lints, configs