3 """Find Kconfig identifieres that are referenced but not defined."""
5 # Copyright (C) 2014 Valentin Rothberg <valentinrothberg@gmail.com>
6 # Copyright (C) 2014 Stefan Hengelein <stefan.hengelein@fau.de>
8 # This program is free software; you can redistribute it and/or modify it
9 # under the terms and conditions of the GNU General Public License,
10 # version 2, as published by the Free Software Foundation.
12 # This program is distributed in the hope it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 from subprocess import Popen, PIPE, STDOUT
23 OPERATORS = r"&|\(|\)|\||\!"
24 FEATURE = r"\w*[A-Z]{1}\w*"
25 CONFIG_DEF = r"^\s*(?:menu){,1}config\s+(" + FEATURE + r")\s*"
26 EXPR = r"(?:" + OPERATORS + r"|\s|" + FEATURE + r")+"
27 STMT = r"^\s*(?:if|select|depends\s+on)\s+" + EXPR
30 REGEX_FILE_KCONFIG = re.compile(r".*Kconfig[\.\w+\-]*$")
31 REGEX_FEATURE = re.compile(r"(" + FEATURE + r")")
32 REGEX_SOURCE_FEATURE = re.compile(r"(?:D|\W|\b)+CONFIG_(" + FEATURE + r")")
33 REGEX_KCONFIG_DEF = re.compile(CONFIG_DEF)
34 REGEX_KCONFIG_EXPR = re.compile(EXPR)
35 REGEX_KCONFIG_STMT = re.compile(STMT)
36 REGEX_KCONFIG_HELP = re.compile(r"^\s+(help|---help---)\s*$")
37 REGEX_FILTER_FEATURES = re.compile(r"[A-Za-z0-9]$")
41 """Main function of this module."""
44 defined_features = set()
45 referenced_features = dict()
47 # use 'git ls-files' to get the worklist
48 pop = Popen("git ls-files", stdout=PIPE, stderr=STDOUT, shell=True)
49 (stdout, _) = pop.communicate() # wait until finished
50 if len(stdout) > 0 and stdout[-1] == "\n":
53 for gitfile in stdout.rsplit("\n"):
54 if ".git" in gitfile or "ChangeLog" in gitfile or \
55 os.path.isdir(gitfile):
57 if REGEX_FILE_KCONFIG.match(gitfile):
58 kconfig_files.append(gitfile)
60 # All non-Kconfig files are checked for consistency
61 source_files.append(gitfile)
63 for sfile in source_files:
64 parse_source_file(sfile, referenced_features)
66 for kfile in kconfig_files:
67 parse_kconfig_file(kfile, defined_features, referenced_features)
69 print "Undefined symbol used\tFile list"
70 for feature in sorted(referenced_features):
71 if feature not in defined_features:
72 if feature.endswith("_MODULE"):
73 # Avoid false positives for kernel modules
74 if feature[:-len("_MODULE")] in defined_features:
76 if "FOO" in feature or "BAR" in feature:
78 files = referenced_features.get(feature)
79 print "%s:\t%s" % (feature, ", ".join(files))
82 def parse_source_file(sfile, referenced_features):
83 """Parse @sfile for referenced Kconfig features."""
85 with open(sfile, "r") as stream:
86 lines = stream.readlines()
89 if not "CONFIG_" in line:
91 features = REGEX_SOURCE_FEATURE.findall(line)
92 for feature in features:
93 if not REGEX_FILTER_FEATURES.search(feature):
95 paths = referenced_features.get(feature, set())
97 referenced_features[feature] = paths
100 def get_features_in_line(line):
101 """Return mentioned Kconfig features in @line."""
102 return REGEX_FEATURE.findall(line)
105 def parse_kconfig_file(kfile, defined_features, referenced_features):
106 """Parse @kfile and update feature definitions and references."""
110 with open(kfile, "r") as stream:
111 lines = stream.readlines()
113 for i in range(len(lines)):
115 line = line.strip('\n')
116 line = line.split("#")[0] # Ignore Kconfig comments
118 if REGEX_KCONFIG_DEF.match(line):
119 feature_def = REGEX_KCONFIG_DEF.findall(line)
120 defined_features.add(feature_def[0])
122 elif REGEX_KCONFIG_HELP.match(line):
125 # Ignore content of help messages
127 elif REGEX_KCONFIG_STMT.match(line):
128 features = get_features_in_line(line)
129 # Multi-line statements
130 while line.endswith("\\"):
133 line = line.strip('\n')
134 features.extend(get_features_in_line(line))
135 for feature in set(features):
136 paths = referenced_features.get(feature, set())
138 referenced_features[feature] = paths
141 if __name__ == "__main__":