2015-08-13 10:32:35 +02:00
|
|
|
#!/usr/bin/env python
|
2015-08-13 11:31:09 +02:00
|
|
|
# Generate a Markdown table of all lints, and put it in README.md.
|
|
|
|
# With -n option, only print the new table to stdout.
|
2016-01-29 22:24:08 +01:00
|
|
|
# With -c option, print a warning and set exit status to 1 if a file would be
|
|
|
|
# changed.
|
2015-08-13 11:31:09 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
|
|
|
declare_lint_re = re.compile(r'''
|
|
|
|
declare_lint! \s* [{(] \s*
|
2016-01-27 20:59:19 +01:00
|
|
|
pub \s+ (?P<name>[A-Z_][A-Z_0-9]*) \s*,\s*
|
2015-08-13 11:31:09 +02:00
|
|
|
(?P<level>Forbid|Deny|Warn|Allow) \s*,\s*
|
|
|
|
" (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
|
2016-03-24 19:25:59 +01:00
|
|
|
''', re.VERBOSE | re.DOTALL)
|
|
|
|
|
|
|
|
declare_deprecated_lint_re = re.compile(r'''
|
|
|
|
declare_deprecated_lint! \s* [{(] \s*
|
|
|
|
pub \s+ (?P<name>[A-Z_][A-Z_0-9]*) \s*,\s*
|
|
|
|
" (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
|
|
|
|
''', re.VERBOSE | re.DOTALL)
|
2015-08-13 11:31:09 +02:00
|
|
|
|
2016-04-30 23:54:10 +02:00
|
|
|
declare_restriction_lint_re = re.compile(r'''
|
|
|
|
declare_restriction_lint! \s* [{(] \s*
|
|
|
|
pub \s+ (?P<name>[A-Z_][A-Z_0-9]*) \s*,\s*
|
|
|
|
" (?P<desc>(?:[^"\\]+|\\.)*) " \s* [})]
|
|
|
|
''', re.VERBOSE | re.DOTALL)
|
|
|
|
|
2015-08-13 11:31:09 +02:00
|
|
|
nl_escape_re = re.compile(r'\\\n\s*')
|
|
|
|
|
2015-08-26 16:04:50 +02:00
|
|
|
wiki_link = 'https://github.com/Manishearth/rust-clippy/wiki'
|
2015-08-13 11:31:09 +02:00
|
|
|
|
2016-01-29 22:24:08 +01:00
|
|
|
|
2016-04-30 23:54:10 +02:00
|
|
|
def collect(lints, deprecated_lints, restriction_lints, fn):
|
2015-08-13 11:31:09 +02:00
|
|
|
"""Collect all lints from a file.
|
|
|
|
|
|
|
|
Adds entries to the lints list as `(module, name, level, desc)`.
|
|
|
|
"""
|
|
|
|
with open(fn) as fp:
|
|
|
|
code = fp.read()
|
|
|
|
for match in declare_lint_re.finditer(code):
|
|
|
|
# remove \-newline escapes from description string
|
|
|
|
desc = nl_escape_re.sub('', match.group('desc'))
|
|
|
|
lints.append((os.path.splitext(os.path.basename(fn))[0],
|
|
|
|
match.group('name').lower(),
|
|
|
|
match.group('level').lower(),
|
|
|
|
desc.replace('\\"', '"')))
|
|
|
|
|
2016-03-24 19:25:59 +01:00
|
|
|
for match in declare_deprecated_lint_re.finditer(code):
|
|
|
|
# remove \-newline escapes from description string
|
|
|
|
desc = nl_escape_re.sub('', match.group('desc'))
|
|
|
|
deprecated_lints.append((os.path.splitext(os.path.basename(fn))[0],
|
|
|
|
match.group('name').lower(),
|
|
|
|
desc.replace('\\"', '"')))
|
|
|
|
|
2016-04-30 23:54:10 +02:00
|
|
|
for match in declare_restriction_lint_re.finditer(code):
|
|
|
|
# remove \-newline escapes from description string
|
|
|
|
desc = nl_escape_re.sub('', match.group('desc'))
|
|
|
|
restriction_lints.append((os.path.splitext(os.path.basename(fn))[0],
|
|
|
|
match.group('name').lower(),
|
|
|
|
"allow",
|
|
|
|
desc.replace('\\"', '"')))
|
|
|
|
|
2015-08-13 11:31:09 +02:00
|
|
|
|
2015-08-26 16:04:50 +02:00
|
|
|
def gen_table(lints, link=None):
|
2015-08-13 11:31:09 +02:00
|
|
|
"""Write lint table in Markdown format."""
|
2015-08-26 16:04:50 +02:00
|
|
|
if link:
|
|
|
|
lints = [(p, '[%s](%s#%s)' % (l, link, l), lvl, d)
|
2015-09-01 17:53:56 +02:00
|
|
|
for (p, l, lvl, d) in lints]
|
2015-08-13 11:31:09 +02:00
|
|
|
# first and third column widths
|
|
|
|
w_name = max(len(l[1]) for l in lints)
|
|
|
|
w_desc = max(len(l[3]) for l in lints)
|
|
|
|
# header and underline
|
2015-08-13 10:32:35 +02:00
|
|
|
yield '%-*s | default | meaning\n' % (w_name, 'name')
|
|
|
|
yield '%s-|-%s-|-%s\n' % ('-' * w_name, '-' * 7, '-' * w_desc)
|
2015-08-13 11:31:09 +02:00
|
|
|
# one table row per lint
|
|
|
|
for (_, name, default, meaning) in sorted(lints, key=lambda l: l[1]):
|
2015-08-13 10:32:35 +02:00
|
|
|
yield '%-*s | %-7s | %s\n' % (w_name, name, default, meaning)
|
2015-08-13 11:31:09 +02:00
|
|
|
|
|
|
|
|
2015-09-01 17:53:56 +02:00
|
|
|
def gen_group(lints, levels=None):
|
2015-08-13 11:31:09 +02:00
|
|
|
"""Write lint group (list of all lints in the form module::NAME)."""
|
2015-09-01 17:53:56 +02:00
|
|
|
if levels:
|
|
|
|
lints = [tup for tup in lints if tup[2] in levels]
|
2015-08-13 11:31:09 +02:00
|
|
|
for (module, name, _, _) in sorted(lints):
|
2015-08-13 10:32:35 +02:00
|
|
|
yield ' %s::%s,\n' % (module, name.upper())
|
2015-08-13 11:31:09 +02:00
|
|
|
|
|
|
|
|
2016-02-20 17:00:36 +01:00
|
|
|
def gen_mods(lints):
|
|
|
|
"""Declare modules"""
|
|
|
|
|
|
|
|
for module in sorted(set(lint[0] for lint in lints)):
|
|
|
|
yield 'pub mod %s;\n' % module
|
|
|
|
|
|
|
|
|
2016-03-24 19:25:59 +01:00
|
|
|
def gen_deprecated(lints):
|
|
|
|
"""Declare deprecated lints"""
|
|
|
|
|
|
|
|
for lint in lints:
|
|
|
|
yield ' store.register_removed("%s", "%s");\n' % (lint[1], lint[2])
|
|
|
|
|
2015-08-13 11:31:09 +02:00
|
|
|
def replace_region(fn, region_start, region_end, callback,
|
2015-08-13 10:32:35 +02:00
|
|
|
replace_start=True, write_back=True):
|
2015-08-13 11:31:09 +02:00
|
|
|
"""Replace a region in a file delimited by two lines matching regexes.
|
|
|
|
|
|
|
|
A callback is called to write the new region. If `replace_start` is true,
|
|
|
|
the start delimiter line is replaced as well. The end delimiter line is
|
|
|
|
never replaced.
|
|
|
|
"""
|
|
|
|
# read current content
|
|
|
|
with open(fn) as fp:
|
|
|
|
lines = list(fp)
|
|
|
|
|
|
|
|
# replace old region with new region
|
2015-08-13 10:32:35 +02:00
|
|
|
new_lines = []
|
|
|
|
in_old_region = False
|
|
|
|
for line in lines:
|
|
|
|
if in_old_region:
|
|
|
|
if re.search(region_end, line):
|
|
|
|
in_old_region = False
|
|
|
|
new_lines.extend(callback())
|
|
|
|
new_lines.append(line)
|
|
|
|
elif re.search(region_start, line):
|
|
|
|
if not replace_start:
|
|
|
|
new_lines.append(line)
|
|
|
|
# old region starts here
|
|
|
|
in_old_region = True
|
|
|
|
else:
|
|
|
|
new_lines.append(line)
|
|
|
|
|
|
|
|
# write back to file
|
|
|
|
if write_back:
|
|
|
|
with open(fn, 'w') as fp:
|
|
|
|
fp.writelines(new_lines)
|
|
|
|
|
|
|
|
# if something changed, return true
|
|
|
|
return lines != new_lines
|
|
|
|
|
|
|
|
|
|
|
|
def main(print_only=False, check=False):
|
2015-08-13 11:31:09 +02:00
|
|
|
lints = []
|
2016-03-24 19:25:59 +01:00
|
|
|
deprecated_lints = []
|
2016-04-30 23:54:10 +02:00
|
|
|
restriction_lints = []
|
2015-08-13 11:31:09 +02:00
|
|
|
|
|
|
|
# check directory
|
|
|
|
if not os.path.isfile('src/lib.rs'):
|
|
|
|
print('Error: call this script from clippy checkout directory!')
|
|
|
|
return
|
|
|
|
|
|
|
|
# collect all lints from source files
|
2016-04-14 17:56:46 +02:00
|
|
|
for root, _, files in os.walk('src'):
|
2015-08-13 11:31:09 +02:00
|
|
|
for fn in files:
|
|
|
|
if fn.endswith('.rs'):
|
2016-04-30 23:54:10 +02:00
|
|
|
collect(lints, deprecated_lints, restriction_lints,
|
|
|
|
os.path.join(root, fn))
|
2015-08-13 11:31:09 +02:00
|
|
|
|
|
|
|
if print_only:
|
2016-04-30 23:54:10 +02:00
|
|
|
sys.stdout.writelines(gen_table(lints + restriction_lints))
|
2015-08-13 11:31:09 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
# replace table in README.md
|
2015-09-01 17:53:56 +02:00
|
|
|
changed = replace_region(
|
|
|
|
'README.md', r'^name +\|', '^$',
|
2016-04-30 23:54:10 +02:00
|
|
|
lambda: gen_table(lints + restriction_lints, link=wiki_link),
|
2015-09-01 17:53:56 +02:00
|
|
|
write_back=not check)
|
2015-08-13 11:31:09 +02:00
|
|
|
|
2015-09-01 17:53:56 +02:00
|
|
|
changed |= replace_region(
|
|
|
|
'README.md',
|
2015-08-24 16:23:05 +02:00
|
|
|
r'^There are \d+ lints included in this crate:', "",
|
2016-04-30 23:54:10 +02:00
|
|
|
lambda: ['There are %d lints included in this crate:\n' % (len(lints)
|
|
|
|
+ len(restriction_lints))],
|
2015-08-24 16:23:05 +02:00
|
|
|
write_back=not check)
|
|
|
|
|
2016-04-14 17:56:46 +02:00
|
|
|
# update the links in the CHANGELOG
|
|
|
|
changed |= replace_region(
|
|
|
|
'CHANGELOG.md',
|
|
|
|
"<!-- begin autogenerated links to wiki -->",
|
|
|
|
"<!-- end autogenerated links to wiki -->",
|
|
|
|
lambda: ["[`{0}`]: {1}#{0}\n".format(l[1], wiki_link) for l in
|
2016-04-30 23:54:10 +02:00
|
|
|
sorted(lints + restriction_lints + deprecated_lints,
|
|
|
|
key=lambda l: l[1])],
|
2016-04-14 17:56:46 +02:00
|
|
|
replace_start=False, write_back=not check)
|
|
|
|
|
2016-02-20 17:00:36 +01:00
|
|
|
# update the `pub mod` list
|
|
|
|
changed |= replace_region(
|
|
|
|
'src/lib.rs', r'begin lints modules', r'end lints modules',
|
2016-04-30 23:54:10 +02:00
|
|
|
lambda: gen_mods(lints + restriction_lints),
|
2016-02-20 17:00:36 +01:00
|
|
|
replace_start=False, write_back=not check)
|
|
|
|
|
2015-08-13 11:31:09 +02:00
|
|
|
# same for "clippy" lint collection
|
2015-09-01 17:53:56 +02:00
|
|
|
changed |= replace_region(
|
|
|
|
'src/lib.rs', r'reg.register_lint_group\("clippy"', r'\]\);',
|
|
|
|
lambda: gen_group(lints, levels=('warn', 'deny')),
|
|
|
|
replace_start=False, write_back=not check)
|
|
|
|
|
2016-03-24 19:25:59 +01:00
|
|
|
# same for "deprecated" lint collection
|
|
|
|
changed |= replace_region(
|
|
|
|
'src/lib.rs', r'let mut store', r'end deprecated lints',
|
|
|
|
lambda: gen_deprecated(deprecated_lints),
|
|
|
|
replace_start=False,
|
|
|
|
write_back=not check)
|
|
|
|
|
2015-09-01 17:53:56 +02:00
|
|
|
# same for "clippy_pedantic" lint collection
|
|
|
|
changed |= replace_region(
|
|
|
|
'src/lib.rs', r'reg.register_lint_group\("clippy_pedantic"', r'\]\);',
|
|
|
|
lambda: gen_group(lints, levels=('allow',)),
|
|
|
|
replace_start=False, write_back=not check)
|
2015-08-13 10:32:35 +02:00
|
|
|
|
2016-04-30 23:54:10 +02:00
|
|
|
# same for "clippy_restrictions" lint collection
|
|
|
|
changed |= replace_region(
|
|
|
|
'src/lib.rs', r'reg.register_lint_group\("clippy_restrictions"',
|
|
|
|
r'\]\);', lambda: gen_group(restriction_lints),
|
|
|
|
replace_start=False, write_back=not check)
|
|
|
|
|
2015-08-13 10:32:35 +02:00
|
|
|
if check and changed:
|
|
|
|
print('Please run util/update_lints.py to regenerate lints lists.')
|
|
|
|
return 1
|
2015-08-13 11:31:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-08-13 10:32:35 +02:00
|
|
|
sys.exit(main(print_only='-n' in sys.argv, check='-c' in sys.argv))
|