2016-08-06 03:40:35 -05:00
|
|
|
# Common utils for the several housekeeping scripts.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import collections
|
|
|
|
|
|
|
|
import logging as log
|
|
|
|
log.basicConfig(level=log.INFO, format='%(levelname)s: %(message)s')
|
|
|
|
|
2018-03-31 10:53:24 -05:00
|
|
|
Lint = collections.namedtuple('Lint', 'name level doc sourcefile group')
|
2016-08-06 03:40:35 -05:00
|
|
|
Config = collections.namedtuple('Config', 'name ty doc default')
|
|
|
|
|
|
|
|
lintname_re = re.compile(r'''pub\s+([A-Z_][A-Z_0-9]*)''')
|
2018-03-31 10:53:24 -05:00
|
|
|
group_re = re.compile(r'''\s*([a-z_][a-z_0-9]+)''')
|
2021-06-20 15:32:32 -05:00
|
|
|
conf_re = re.compile(r'''define_Conf! {\n((?!\n})[\s\S])*\n}''', re.MULTILINE)
|
2016-08-06 03:40:35 -05:00
|
|
|
confvar_re = re.compile(
|
2021-05-02 17:50:22 -05:00
|
|
|
r'''/// Lint: ([\w,\s]+)\. (.*)\n\s*\(([^:]+):\s*([^\s=]+)\s*=\s*([^\.\)]+).*\),''', re.MULTILINE)
|
2019-03-05 10:50:33 -06:00
|
|
|
comment_re = re.compile(r'''\s*/// ?(.*)''')
|
2016-08-06 03:40:35 -05:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
lint_levels = {
|
|
|
|
"correctness": 'Deny',
|
2021-06-14 10:01:43 -05:00
|
|
|
"suspicious": 'Warn',
|
2018-03-28 08:24:26 -05:00
|
|
|
"style": 'Warn',
|
|
|
|
"complexity": 'Warn',
|
|
|
|
"perf": 'Warn',
|
|
|
|
"restriction": 'Allow',
|
|
|
|
"pedantic": 'Allow',
|
|
|
|
"nursery": 'Allow',
|
2018-04-29 18:20:39 -05:00
|
|
|
"cargo": 'Allow',
|
2018-03-28 08:24:26 -05:00
|
|
|
}
|
2016-08-06 03:40:35 -05:00
|
|
|
|
2018-03-31 10:53:24 -05:00
|
|
|
|
2016-08-06 03:40:35 -05:00
|
|
|
def parse_lints(lints, filepath):
|
2019-03-05 10:50:33 -06:00
|
|
|
comment = []
|
2018-03-28 08:24:26 -05:00
|
|
|
clippy = False
|
|
|
|
deprecated = False
|
|
|
|
name = ""
|
2016-08-06 03:40:35 -05:00
|
|
|
|
|
|
|
with open(filepath) as fp:
|
|
|
|
for line in fp:
|
2019-03-05 10:50:33 -06:00
|
|
|
if clippy or deprecated:
|
2018-03-31 10:53:24 -05:00
|
|
|
m = lintname_re.search(line)
|
|
|
|
if m:
|
|
|
|
name = m.group(1).lower()
|
|
|
|
line = next(fp)
|
|
|
|
|
|
|
|
if deprecated:
|
|
|
|
level = "Deprecated"
|
|
|
|
group = "deprecated"
|
|
|
|
else:
|
|
|
|
while True:
|
|
|
|
g = group_re.search(line)
|
|
|
|
if g:
|
|
|
|
group = g.group(1).lower()
|
2018-10-05 01:02:44 -05:00
|
|
|
level = lint_levels.get(group, None)
|
2018-03-31 10:53:24 -05:00
|
|
|
break
|
|
|
|
line = next(fp)
|
|
|
|
|
2018-10-05 01:02:44 -05:00
|
|
|
if level is None:
|
|
|
|
continue
|
|
|
|
|
2018-03-31 10:53:24 -05:00
|
|
|
log.info("found %s with level %s in %s",
|
|
|
|
name, level, filepath)
|
2019-03-05 10:50:33 -06:00
|
|
|
lints.append(Lint(name, level, comment, filepath, group))
|
|
|
|
comment = []
|
2018-03-31 10:53:24 -05:00
|
|
|
|
2019-03-05 10:50:33 -06:00
|
|
|
clippy = False
|
|
|
|
deprecated = False
|
|
|
|
name = ""
|
|
|
|
else:
|
|
|
|
m = comment_re.search(line)
|
|
|
|
if m:
|
|
|
|
comment.append(m.group(1))
|
|
|
|
elif line.startswith("declare_clippy_lint!"):
|
|
|
|
clippy = True
|
|
|
|
deprecated = False
|
|
|
|
elif line.startswith("declare_deprecated_lint!"):
|
|
|
|
clippy = False
|
|
|
|
deprecated = True
|
|
|
|
elif line.startswith("declare_lint!"):
|
|
|
|
import sys
|
|
|
|
print(
|
|
|
|
"don't use `declare_lint!` in Clippy, "
|
|
|
|
"use `declare_clippy_lint!` instead"
|
|
|
|
)
|
|
|
|
sys.exit(42)
|
2016-08-06 03:40:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
def parse_configs(path):
|
|
|
|
configs = {}
|
|
|
|
with open(os.path.join(path, 'utils/conf.rs')) as fp:
|
|
|
|
contents = fp.read()
|
|
|
|
|
|
|
|
match = re.search(conf_re, contents)
|
2021-06-20 15:32:32 -05:00
|
|
|
confvars = re.findall(confvar_re, match.group(0))
|
2016-08-06 03:40:35 -05:00
|
|
|
|
2020-03-23 16:07:46 -05:00
|
|
|
for (lints, doc, name, ty, default) in confvars:
|
|
|
|
for lint in lints.split(','):
|
|
|
|
configs[lint.strip().lower()] = Config(name.replace("_", "-"), ty, doc, default)
|
2016-08-06 03:40:35 -05:00
|
|
|
return configs
|
|
|
|
|
|
|
|
|
|
|
|
def parse_all(path="clippy_lints/src"):
|
|
|
|
lints = []
|
2018-10-05 01:02:44 -05:00
|
|
|
for root, dirs, files in os.walk(path):
|
|
|
|
for fn in files:
|
|
|
|
if fn.endswith('.rs'):
|
|
|
|
parse_lints(lints, os.path.join(root, fn))
|
|
|
|
|
2016-08-06 03:40:35 -05:00
|
|
|
log.info("got %s lints", len(lints))
|
|
|
|
|
|
|
|
configs = parse_configs(path)
|
|
|
|
log.info("got %d configs", len(configs))
|
|
|
|
|
|
|
|
return lints, configs
|