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}''', re.MULTILINE)
16 confvar_re = re.compile(
17 r'''/// Lint: (\w+). (.*).*\n\s*\([^,]+,\s+"([^"]+)",\s+([^=\)]+)=>\s+(.*)\),''', re.MULTILINE)
20 "correctness": 'Deny',
24 "restriction": 'Allow',
31 def parse_lints(lints, filepath):
38 with open(filepath) as fp:
41 if line.startswith("/// "):
42 last_comment.append(line[4:])
43 elif line.startswith("///"):
44 last_comment.append(line[3:])
45 elif line.startswith("declare_lint!"):
47 print "don't use `declare_lint!` in Clippy, use `declare_clippy_lint!` instead"
49 elif line.startswith("declare_clippy_lint!"):
54 elif line.startswith("declare_deprecated_lint!"):
61 m = lintname_re.search(line)
64 name = m.group(1).lower()
72 g = group_re.search(line)
74 group = g.group(1).lower()
75 level = lint_levels[group]
79 log.info("found %s with level %s in %s",
80 name, level, filepath)
81 lints.append(Lint(name, level, last_comment, filepath, group))
86 log.warn("Warning: missing Lint-Name in %s", filepath)
90 def parse_configs(path):
92 with open(os.path.join(path, 'utils/conf.rs')) as fp:
95 match = re.search(conf_re, contents)
96 confvars = re.findall(confvar_re, match.group(1))
98 for (lint, doc, name, default, ty) in confvars:
99 configs[lint.lower()] = Config(name.replace("_", "-"), ty, doc, default)
104 def parse_all(path="clippy_lints/src"):
106 for filename in os.listdir(path):
107 if filename.endswith(".rs"):
108 parse_lints(lints, os.path.join(path, filename))
109 log.info("got %s lints", len(lints))
111 configs = parse_configs(path)
112 log.info("got %d configs", len(configs))
114 return lints, configs