3 """Find Kconfig symbols that are referenced but not defined."""
5 # (c) 2014-2016 Valentin Rothberg <valentinrothberg@gmail.com>
6 # (c) 2014 Stefan Hengelein <stefan.hengelein@fau.de>
8 # Licensed under the terms of the GNU GPL License version 2
17 from multiprocessing import Pool, cpu_count
18 from optparse import OptionParser
19 from subprocess import Popen, PIPE, STDOUT
23 OPERATORS = r"&|\(|\)|\||\!"
24 FEATURE = r"(?:\w*[A-Z0-9]\w*){2,}"
25 DEF = r"^\s*(?:menu){,1}config\s+(" + FEATURE + r")\s*"
26 EXPR = r"(?:" + OPERATORS + r"|\s|" + FEATURE + r")+"
27 DEFAULT = r"default\s+.*?(?:if\s.+){,1}"
28 STMT = r"^\s*(?:if|select|depends\s+on|(?:" + DEFAULT + r"))\s+" + EXPR
29 SOURCE_FEATURE = r"(?:\W|\b)+[D]{,1}CONFIG_(" + FEATURE + r")"
32 REGEX_FILE_KCONFIG = re.compile(r".*Kconfig[\.\w+\-]*$")
33 REGEX_FEATURE = re.compile(r'(?!\B)' + FEATURE + r'(?!\B)')
34 REGEX_SOURCE_FEATURE = re.compile(SOURCE_FEATURE)
35 REGEX_KCONFIG_DEF = re.compile(DEF)
36 REGEX_KCONFIG_EXPR = re.compile(EXPR)
37 REGEX_KCONFIG_STMT = re.compile(STMT)
38 REGEX_KCONFIG_HELP = re.compile(r"^\s+(help|---help---)\s*$")
39 REGEX_FILTER_FEATURES = re.compile(r"[A-Za-z0-9]$")
40 REGEX_NUMERIC = re.compile(r"0[xX][0-9a-fA-F]+|[0-9]+")
41 REGEX_QUOTES = re.compile("(\"(.*?)\")")
45 """The user interface of this module."""
46 usage = "%prog [options]\n\n" \
47 "Run this tool to detect Kconfig symbols that are referenced but " \
48 "not defined in\nKconfig. The output of this tool has the " \
49 "format \'Undefined symbol\\tFile list\'\n\n" \
50 "If no option is specified, %prog will default to check your\n" \
51 "current tree. Please note that specifying commits will " \
52 "\'git reset --hard\'\nyour current tree! You may save " \
53 "uncommitted changes to avoid losing data."
55 parser = OptionParser(usage=usage)
57 parser.add_option('-c', '--commit', dest='commit', action='store',
59 help="Check if the specified commit (hash) introduces "
60 "undefined Kconfig symbols.")
62 parser.add_option('-d', '--diff', dest='diff', action='store',
64 help="Diff undefined symbols between two commits. The "
65 "input format bases on Git log's "
66 "\'commmit1..commit2\'.")
68 parser.add_option('-f', '--find', dest='find', action='store_true',
70 help="Find and show commits that may cause symbols to be "
71 "missing. Required to run with --diff.")
73 parser.add_option('-i', '--ignore', dest='ignore', action='store',
75 help="Ignore files matching this pattern. Note that "
76 "the pattern needs to be a Python regex. To "
77 "ignore defconfigs, specify -i '.*defconfig'.")
79 parser.add_option('-s', '--sim', dest='sim', action='store', default="",
80 help="Print a list of maximum 10 string-similar symbols.")
82 parser.add_option('', '--force', dest='force', action='store_true',
84 help="Reset current Git tree even when it's dirty.")
86 parser.add_option('', '--no-color', dest='color', action='store_false',
88 help="Don't print colored output. Default when not "
89 "outputting to a terminal.")
91 (opts, _) = parser.parse_args()
93 if opts.commit and opts.diff:
94 sys.exit("Please specify only one option at once.")
96 if opts.diff and not re.match(r"^[\w\-\.]+\.\.[\w\-\.]+$", opts.diff):
97 sys.exit("Please specify valid input in the following format: "
98 "\'commit1..commit2\'")
100 if opts.commit or opts.diff:
101 if not opts.force and tree_is_dirty():
102 sys.exit("The current Git tree is dirty (see 'git status'). "
103 "Running this script may\ndelete important data since it "
104 "calls 'git reset --hard' for some performance\nreasons. "
105 " Please run this script in a clean Git tree or pass "
106 "'--force' if you\nwant to ignore this warning and "
114 re.match(opts.ignore, "this/is/just/a/test.c")
116 sys.exit("Please specify a valid Python regex.")
122 """Main function of this module."""
123 opts = parse_options()
126 color = opts.color and sys.stdout.isatty()
128 if opts.sim and not opts.commit and not opts.diff:
129 sims = find_sims(opts.sim, opts.ignore)
131 print("%s: %s" % (yel("Similar symbols"), ', '.join(sims)))
133 print("%s: no similar symbols found" % yel("Similar symbols"))
136 # dictionary of (un)defined symbols
140 if opts.commit or opts.diff:
147 commit_a = opts.commit + "~"
148 commit_b = opts.commit
150 split = opts.diff.split("..")
156 # get undefined items before the commit
157 execute("git reset --hard %s" % commit_a)
158 undefined_a, _ = check_symbols(opts.ignore)
160 # get undefined items for the commit
161 execute("git reset --hard %s" % commit_b)
162 undefined_b, defined = check_symbols(opts.ignore)
164 # report cases that are present for the commit but not before
165 for feature in sorted(undefined_b):
166 # feature has not been undefined before
167 if not feature in undefined_a:
168 files = sorted(undefined_b.get(feature))
169 undefined[feature] = files
170 # check if there are new files that reference the undefined feature
172 files = sorted(undefined_b.get(feature) -
173 undefined_a.get(feature))
175 undefined[feature] = files
178 execute("git reset --hard %s" % head)
180 # default to check the entire tree
182 undefined, defined = check_symbols(opts.ignore)
184 # now print the output
185 for feature in sorted(undefined):
188 files = sorted(undefined.get(feature))
189 print("%s: %s" % (yel("Referencing files"), ", ".join(files)))
191 sims = find_sims(feature, opts.ignore, defined)
192 sims_out = yel("Similar symbols")
194 print("%s: %s" % (sims_out, ', '.join(sims)))
196 print("%s: %s" % (sims_out, "no similar symbols found"))
199 print("%s:" % yel("Commits changing symbol"))
200 commits = find_commits(feature, opts.diff)
202 for commit in commits:
203 commit = commit.split(" ", 1)
204 print("\t- %s (\"%s\")" % (yel(commit[0]), commit[1]))
206 print("\t- no commit found")
212 Color %string yellow.
214 return "\033[33m%s\033[0m" % string if color else string
221 return "\033[31m%s\033[0m" % string if color else string
225 """Execute %cmd and return stdout. Exit in case of error."""
227 cmdlist = cmd.split(" ")
228 stdout = subprocess.check_output(cmdlist, stderr=subprocess.STDOUT, shell=False)
229 stdout = stdout.decode(errors='replace')
230 except subprocess.CalledProcessError as fail:
231 exit("Failed to execute %s\n%s" % (cmd, fail))
235 def find_commits(symbol, diff):
236 """Find commits changing %symbol in the given range of %diff."""
237 commits = execute("git log --pretty=oneline --abbrev-commit -G %s %s"
239 return [x for x in commits.split("\n") if x]
243 """Return true if the current working tree is dirty (i.e., if any file has
244 been added, deleted, modified, renamed or copied but not committed)."""
245 stdout = execute("git status --porcelain")
247 if re.findall(r"[URMADC]{1}", line[:2]):
253 """Return commit hash of current HEAD."""
254 stdout = execute("git rev-parse HEAD")
255 return stdout.strip('\n')
258 def partition(lst, size):
259 """Partition list @lst into eveni-sized lists of size @size."""
260 return [lst[i::size] for i in range(size)]
264 """Set signal handler to ignore SIGINT."""
265 signal.signal(signal.SIGINT, signal.SIG_IGN)
268 def find_sims(symbol, ignore, defined = []):
269 """Return a list of max. ten Kconfig symbols that are string-similar to
272 return sorted(difflib.get_close_matches(symbol, set(defined), 10))
274 pool = Pool(cpu_count(), init_worker)
276 for gitfile in get_files():
277 if REGEX_FILE_KCONFIG.match(gitfile):
278 kfiles.append(gitfile)
281 for part in partition(kfiles, cpu_count()):
282 arglist.append((part, ignore))
284 for res in pool.map(parse_kconfig_files, arglist):
285 defined.extend(res[0])
287 return sorted(difflib.get_close_matches(symbol, set(defined), 10))
291 """Return a list of all files in the current git directory."""
292 # use 'git ls-files' to get the worklist
293 stdout = execute("git ls-files")
294 if len(stdout) > 0 and stdout[-1] == "\n":
298 for gitfile in stdout.rsplit("\n"):
299 if ".git" in gitfile or "ChangeLog" in gitfile or \
300 ".log" in gitfile or os.path.isdir(gitfile) or \
301 gitfile.startswith("tools/"):
303 files.append(gitfile)
307 def check_symbols(ignore):
308 """Find undefined Kconfig symbols and return a dict with the symbol as key
309 and a list of referencing files as value. Files matching %ignore are not
310 checked for undefined symbols."""
311 pool = Pool(cpu_count(), init_worker)
313 return check_symbols_helper(pool, ignore)
314 except KeyboardInterrupt:
320 def check_symbols_helper(pool, ignore):
321 """Helper method for check_symbols(). Used to catch keyboard interrupts in
322 check_symbols() in order to properly terminate running worker processes."""
325 defined_features = []
326 referenced_features = dict() # {file: [features]}
328 for gitfile in get_files():
329 if REGEX_FILE_KCONFIG.match(gitfile):
330 kconfig_files.append(gitfile)
332 if ignore and not re.match(ignore, gitfile):
334 # add source files that do not match the ignore pattern
335 source_files.append(gitfile)
338 arglist = partition(source_files, cpu_count())
339 for res in pool.map(parse_source_files, arglist):
340 referenced_features.update(res)
343 # parse kconfig files
345 for part in partition(kconfig_files, cpu_count()):
346 arglist.append((part, ignore))
347 for res in pool.map(parse_kconfig_files, arglist):
348 defined_features.extend(res[0])
349 referenced_features.update(res[1])
350 defined_features = set(defined_features)
352 # inverse mapping of referenced_features to dict(feature: [files])
354 for _file, features in referenced_features.items():
355 for feature in features:
356 inv_map[feature] = inv_map.get(feature, set())
357 inv_map[feature].add(_file)
358 referenced_features = inv_map
360 undefined = {} # {feature: [files]}
361 for feature in sorted(referenced_features):
362 # filter some false positives
363 if feature == "FOO" or feature == "BAR" or \
364 feature == "FOO_BAR" or feature == "XXX":
366 if feature not in defined_features:
367 if feature.endswith("_MODULE"):
368 # avoid false positives for kernel modules
369 if feature[:-len("_MODULE")] in defined_features:
371 undefined[feature] = referenced_features.get(feature)
372 return undefined, defined_features
375 def parse_source_files(source_files):
376 """Parse each source file in @source_files and return dictionary with source
377 files as keys and lists of references Kconfig symbols as values."""
378 referenced_features = dict()
379 for sfile in source_files:
380 referenced_features[sfile] = parse_source_file(sfile)
381 return referenced_features
384 def parse_source_file(sfile):
385 """Parse @sfile and return a list of referenced Kconfig features."""
389 if not os.path.exists(sfile):
392 with open(sfile, "r", encoding='utf-8', errors='replace') as stream:
393 lines = stream.readlines()
396 if not "CONFIG_" in line:
398 features = REGEX_SOURCE_FEATURE.findall(line)
399 for feature in features:
400 if not REGEX_FILTER_FEATURES.search(feature):
402 references.append(feature)
407 def get_features_in_line(line):
408 """Return mentioned Kconfig features in @line."""
409 return REGEX_FEATURE.findall(line)
412 def parse_kconfig_files(args):
413 """Parse kconfig files and return tuple of defined and references Kconfig
414 symbols. Note, @args is a tuple of a list of files and the @ignore
416 kconfig_files = args[0]
418 defined_features = []
419 referenced_features = dict()
421 for kfile in kconfig_files:
422 defined, references = parse_kconfig_file(kfile)
423 defined_features.extend(defined)
424 if ignore and re.match(ignore, kfile):
425 # do not collect references for files that match the ignore pattern
427 referenced_features[kfile] = references
428 return (defined_features, referenced_features)
431 def parse_kconfig_file(kfile):
432 """Parse @kfile and update feature definitions and references."""
438 if not os.path.exists(kfile):
439 return defined, references
441 with open(kfile, "r", encoding='utf-8', errors='replace') as stream:
442 lines = stream.readlines()
444 for i in range(len(lines)):
446 line = line.strip('\n')
447 line = line.split("#")[0] # ignore comments
449 if REGEX_KCONFIG_DEF.match(line):
450 feature_def = REGEX_KCONFIG_DEF.findall(line)
451 defined.append(feature_def[0])
453 elif REGEX_KCONFIG_HELP.match(line):
456 # ignore content of help messages
458 elif REGEX_KCONFIG_STMT.match(line):
459 line = REGEX_QUOTES.sub("", line)
460 features = get_features_in_line(line)
461 # multi-line statements
462 while line.endswith("\\"):
465 line = line.strip('\n')
466 features.extend(get_features_in_line(line))
467 for feature in set(features):
468 if REGEX_NUMERIC.match(feature):
469 # ignore numeric values
471 references.append(feature)
473 return defined, references
476 if __name__ == "__main__":