blob: e9cc689033fe8f0edb696003845cd00872fe0ff2 [file] [log] [blame]
Valentin Rothberg24fe1f02014-09-27 16:30:45 +02001#!/usr/bin/env python
2
Valentin Rothbergcc641d52014-11-08 20:56:35 +01003"""Find Kconfig identifiers that are referenced but not defined."""
Valentin Rothberg24fe1f02014-09-27 16:30:45 +02004
Valentin Rothbergcc641d52014-11-08 20:56:35 +01005# (c) 2014 Valentin Rothberg <valentinrothberg@gmail.com>
6# (c) 2014 Stefan Hengelein <stefan.hengelein@fau.de>
Valentin Rothberg24fe1f02014-09-27 16:30:45 +02007#
Valentin Rothbergcc641d52014-11-08 20:56:35 +01008# Licensed under the terms of the GNU GPL License version 2
Valentin Rothberg24fe1f02014-09-27 16:30:45 +02009
10
11import os
12import re
13from subprocess import Popen, PIPE, STDOUT
14
Valentin Rothbergcc641d52014-11-08 20:56:35 +010015
16# regex expressions
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020017OPERATORS = r"&|\(|\)|\||\!"
Valentin Rothbergcc641d52014-11-08 20:56:35 +010018FEATURE = r"(?:\w*[A-Z0-9]\w*){2,}"
19DEF = r"^\s*(?:menu){,1}config\s+(" + FEATURE + r")\s*"
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020020EXPR = r"(?:" + OPERATORS + r"|\s|" + FEATURE + r")+"
21STMT = r"^\s*(?:if|select|depends\s+on)\s+" + EXPR
Valentin Rothbergcc641d52014-11-08 20:56:35 +010022SOURCE_FEATURE = r"(?:\W|\b)+[D]{,1}CONFIG_(" + FEATURE + r")"
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020023
Valentin Rothbergcc641d52014-11-08 20:56:35 +010024# regex objects
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020025REGEX_FILE_KCONFIG = re.compile(r".*Kconfig[\.\w+\-]*$")
26REGEX_FEATURE = re.compile(r"(" + FEATURE + r")")
Valentin Rothbergcc641d52014-11-08 20:56:35 +010027REGEX_SOURCE_FEATURE = re.compile(SOURCE_FEATURE)
28REGEX_KCONFIG_DEF = re.compile(DEF)
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020029REGEX_KCONFIG_EXPR = re.compile(EXPR)
30REGEX_KCONFIG_STMT = re.compile(STMT)
31REGEX_KCONFIG_HELP = re.compile(r"^\s+(help|---help---)\s*$")
32REGEX_FILTER_FEATURES = re.compile(r"[A-Za-z0-9]$")
33
34
35def main():
36 """Main function of this module."""
37 source_files = []
38 kconfig_files = []
39 defined_features = set()
Valentin Rothbergcc641d52014-11-08 20:56:35 +010040 referenced_features = dict() # {feature: [files]}
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020041
42 # use 'git ls-files' to get the worklist
43 pop = Popen("git ls-files", stdout=PIPE, stderr=STDOUT, shell=True)
44 (stdout, _) = pop.communicate() # wait until finished
45 if len(stdout) > 0 and stdout[-1] == "\n":
46 stdout = stdout[:-1]
47
48 for gitfile in stdout.rsplit("\n"):
49 if ".git" in gitfile or "ChangeLog" in gitfile or \
Valentin Rothbergcc641d52014-11-08 20:56:35 +010050 ".log" in gitfile or os.path.isdir(gitfile):
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020051 continue
52 if REGEX_FILE_KCONFIG.match(gitfile):
53 kconfig_files.append(gitfile)
54 else:
Valentin Rothbergcc641d52014-11-08 20:56:35 +010055 # all non-Kconfig files are checked for consistency
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020056 source_files.append(gitfile)
57
58 for sfile in source_files:
59 parse_source_file(sfile, referenced_features)
60
61 for kfile in kconfig_files:
62 parse_kconfig_file(kfile, defined_features, referenced_features)
63
64 print "Undefined symbol used\tFile list"
65 for feature in sorted(referenced_features):
Valentin Rothbergcc641d52014-11-08 20:56:35 +010066 # filter some false positives
67 if feature == "FOO" or feature == "BAR" or \
68 feature == "FOO_BAR" or feature == "XXX":
69 continue
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020070 if feature not in defined_features:
71 if feature.endswith("_MODULE"):
Valentin Rothbergcc641d52014-11-08 20:56:35 +010072 # avoid false positives for kernel modules
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020073 if feature[:-len("_MODULE")] in defined_features:
74 continue
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020075 files = referenced_features.get(feature)
Valentin Rothbergcc641d52014-11-08 20:56:35 +010076 print "%s\t%s" % (feature, ", ".join(files))
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020077
78
79def parse_source_file(sfile, referenced_features):
80 """Parse @sfile for referenced Kconfig features."""
81 lines = []
82 with open(sfile, "r") as stream:
83 lines = stream.readlines()
84
85 for line in lines:
86 if not "CONFIG_" in line:
87 continue
88 features = REGEX_SOURCE_FEATURE.findall(line)
89 for feature in features:
90 if not REGEX_FILTER_FEATURES.search(feature):
91 continue
Valentin Rothbergcc641d52014-11-08 20:56:35 +010092 sfiles = referenced_features.get(feature, set())
93 sfiles.add(sfile)
94 referenced_features[feature] = sfiles
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020095
96
97def get_features_in_line(line):
98 """Return mentioned Kconfig features in @line."""
99 return REGEX_FEATURE.findall(line)
100
101
102def parse_kconfig_file(kfile, defined_features, referenced_features):
103 """Parse @kfile and update feature definitions and references."""
104 lines = []
105 skip = False
106
107 with open(kfile, "r") as stream:
108 lines = stream.readlines()
109
110 for i in range(len(lines)):
111 line = lines[i]
112 line = line.strip('\n')
Valentin Rothbergcc641d52014-11-08 20:56:35 +0100113 line = line.split("#")[0] # ignore comments
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200114
115 if REGEX_KCONFIG_DEF.match(line):
116 feature_def = REGEX_KCONFIG_DEF.findall(line)
117 defined_features.add(feature_def[0])
118 skip = False
119 elif REGEX_KCONFIG_HELP.match(line):
120 skip = True
121 elif skip:
Valentin Rothbergcc641d52014-11-08 20:56:35 +0100122 # ignore content of help messages
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200123 pass
124 elif REGEX_KCONFIG_STMT.match(line):
125 features = get_features_in_line(line)
Valentin Rothbergcc641d52014-11-08 20:56:35 +0100126 # multi-line statements
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200127 while line.endswith("\\"):
128 i += 1
129 line = lines[i]
130 line = line.strip('\n')
131 features.extend(get_features_in_line(line))
132 for feature in set(features):
133 paths = referenced_features.get(feature, set())
134 paths.add(kfile)
135 referenced_features[feature] = paths
136
137
138if __name__ == "__main__":
139 main()