blob: 2b13d8a5c0f6e139386e0c0b0e07e6aefa18e19c [file] [log] [blame]
Valentin Rothberg7c5227a2016-08-28 08:51:28 +02001#!/usr/bin/env python3
Valentin Rothberg24fe1f02014-09-27 16:30:45 +02002
Valentin Rothbergb1a3f242015-03-16 12:16:14 +01003"""Find Kconfig symbols that are referenced but not defined."""
Valentin Rothberg24fe1f02014-09-27 16:30:45 +02004
Valentin Rothbergf175ba12016-08-27 10:59:07 +02005# (c) 2014-2016 Valentin Rothberg <valentinrothberg@gmail.com>
Valentin Rothbergcc641d52014-11-08 20:56:35 +01006# (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
Valentin Rothberg1b2c8412015-11-26 14:17:15 +010011import difflib
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020012import os
13import re
Valentin Rothberge2042a82015-10-15 10:37:47 +020014import signal
Valentin Rothbergf175ba12016-08-27 10:59:07 +020015import subprocess
Valentin Rothbergb1a3f242015-03-16 12:16:14 +010016import sys
Valentin Rothberge2042a82015-10-15 10:37:47 +020017from multiprocessing import Pool, cpu_count
Valentin Rothbergb1a3f242015-03-16 12:16:14 +010018from optparse import OptionParser
Valentin Rothberge2042a82015-10-15 10:37:47 +020019from subprocess import Popen, PIPE, STDOUT
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020020
Valentin Rothbergcc641d52014-11-08 20:56:35 +010021
22# regex expressions
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020023OPERATORS = r"&|\(|\)|\||\!"
Valentin Rothbergcc641d52014-11-08 20:56:35 +010024FEATURE = r"(?:\w*[A-Z0-9]\w*){2,}"
25DEF = r"^\s*(?:menu){,1}config\s+(" + FEATURE + r")\s*"
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020026EXPR = r"(?:" + OPERATORS + r"|\s|" + FEATURE + r")+"
Valentin Rothberg0bd38ae2015-07-27 12:33:05 +020027DEFAULT = r"default\s+.*?(?:if\s.+){,1}"
28STMT = r"^\s*(?:if|select|depends\s+on|(?:" + DEFAULT + r"))\s+" + EXPR
Valentin Rothbergcc641d52014-11-08 20:56:35 +010029SOURCE_FEATURE = r"(?:\W|\b)+[D]{,1}CONFIG_(" + FEATURE + r")"
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020030
Valentin Rothbergcc641d52014-11-08 20:56:35 +010031# regex objects
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020032REGEX_FILE_KCONFIG = re.compile(r".*Kconfig[\.\w+\-]*$")
Valentin Rothberge2042a82015-10-15 10:37:47 +020033REGEX_FEATURE = re.compile(r'(?!\B)' + FEATURE + r'(?!\B)')
Valentin Rothbergcc641d52014-11-08 20:56:35 +010034REGEX_SOURCE_FEATURE = re.compile(SOURCE_FEATURE)
35REGEX_KCONFIG_DEF = re.compile(DEF)
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020036REGEX_KCONFIG_EXPR = re.compile(EXPR)
37REGEX_KCONFIG_STMT = re.compile(STMT)
38REGEX_KCONFIG_HELP = re.compile(r"^\s+(help|---help---)\s*$")
39REGEX_FILTER_FEATURES = re.compile(r"[A-Za-z0-9]$")
Valentin Rothberg0bd38ae2015-07-27 12:33:05 +020040REGEX_NUMERIC = re.compile(r"0[xX][0-9a-fA-F]+|[0-9]+")
Valentin Rothberge2042a82015-10-15 10:37:47 +020041REGEX_QUOTES = re.compile("(\"(.*?)\")")
Valentin Rothberg24fe1f02014-09-27 16:30:45 +020042
43
Valentin Rothbergb1a3f242015-03-16 12:16:14 +010044def parse_options():
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."
54
55 parser = OptionParser(usage=usage)
56
57 parser.add_option('-c', '--commit', dest='commit', action='store',
58 default="",
59 help="Check if the specified commit (hash) introduces "
60 "undefined Kconfig symbols.")
61
62 parser.add_option('-d', '--diff', dest='diff', action='store',
63 default="",
64 help="Diff undefined symbols between two commits. The "
65 "input format bases on Git log's "
66 "\'commmit1..commit2\'.")
67
Valentin Rothberga42fa922015-06-01 16:00:19 +020068 parser.add_option('-f', '--find', dest='find', action='store_true',
69 default=False,
70 help="Find and show commits that may cause symbols to be "
71 "missing. Required to run with --diff.")
72
Valentin Rothbergcf132e42015-04-29 16:58:27 +020073 parser.add_option('-i', '--ignore', dest='ignore', action='store',
74 default="",
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'.")
78
Valentin Rothberg1b2c8412015-11-26 14:17:15 +010079 parser.add_option('-s', '--sim', dest='sim', action='store', default="",
80 help="Print a list of maximum 10 string-similar symbols.")
81
Valentin Rothbergb1a3f242015-03-16 12:16:14 +010082 parser.add_option('', '--force', dest='force', action='store_true',
83 default=False,
84 help="Reset current Git tree even when it's dirty.")
85
Andrew Donnellan4c73c082016-07-05 17:47:37 +100086 parser.add_option('', '--no-color', dest='color', action='store_false',
87 default=True,
88 help="Don't print colored output. Default when not "
89 "outputting to a terminal.")
90
Valentin Rothbergb1a3f242015-03-16 12:16:14 +010091 (opts, _) = parser.parse_args()
92
93 if opts.commit and opts.diff:
94 sys.exit("Please specify only one option at once.")
95
96 if opts.diff and not re.match(r"^[\w\-\.]+\.\.[\w\-\.]+$", opts.diff):
97 sys.exit("Please specify valid input in the following format: "
Andreas Ziegler38cbfe42016-03-31 09:24:29 +020098 "\'commit1..commit2\'")
Valentin Rothbergb1a3f242015-03-16 12:16:14 +010099
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 "
107 "continue.")
108
Valentin Rothberga42fa922015-06-01 16:00:19 +0200109 if opts.commit:
110 opts.find = False
111
Valentin Rothbergcf132e42015-04-29 16:58:27 +0200112 if opts.ignore:
113 try:
114 re.match(opts.ignore, "this/is/just/a/test.c")
115 except:
116 sys.exit("Please specify a valid Python regex.")
117
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100118 return opts
119
120
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200121def main():
122 """Main function of this module."""
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100123 opts = parse_options()
124
Andrew Donnellan4c73c082016-07-05 17:47:37 +1000125 global color
126 color = opts.color and sys.stdout.isatty()
127
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100128 if opts.sim and not opts.commit and not opts.diff:
129 sims = find_sims(opts.sim, opts.ignore)
130 if sims:
Valentin Rothberg7c5227a2016-08-28 08:51:28 +0200131 print("%s: %s" % (yel("Similar symbols"), ', '.join(sims)))
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100132 else:
Valentin Rothberg7c5227a2016-08-28 08:51:28 +0200133 print("%s: no similar symbols found" % yel("Similar symbols"))
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100134 sys.exit(0)
135
136 # dictionary of (un)defined symbols
137 defined = {}
138 undefined = {}
139
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100140 if opts.commit or opts.diff:
141 head = get_head()
142
143 # get commit range
144 commit_a = None
145 commit_b = None
146 if opts.commit:
147 commit_a = opts.commit + "~"
148 commit_b = opts.commit
149 elif opts.diff:
150 split = opts.diff.split("..")
151 commit_a = split[0]
152 commit_b = split[1]
153 undefined_a = {}
154 undefined_b = {}
155
156 # get undefined items before the commit
157 execute("git reset --hard %s" % commit_a)
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100158 undefined_a, _ = check_symbols(opts.ignore)
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100159
160 # get undefined items for the commit
161 execute("git reset --hard %s" % commit_b)
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100162 undefined_b, defined = check_symbols(opts.ignore)
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100163
164 # report cases that are present for the commit but not before
Valentin Rothberge9533ae2015-03-23 18:40:49 +0100165 for feature in sorted(undefined_b):
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100166 # feature has not been undefined before
167 if not feature in undefined_a:
Valentin Rothberge9533ae2015-03-23 18:40:49 +0100168 files = sorted(undefined_b.get(feature))
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100169 undefined[feature] = files
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100170 # check if there are new files that reference the undefined feature
171 else:
Valentin Rothberge9533ae2015-03-23 18:40:49 +0100172 files = sorted(undefined_b.get(feature) -
173 undefined_a.get(feature))
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100174 if files:
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100175 undefined[feature] = files
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100176
177 # reset to head
178 execute("git reset --hard %s" % head)
179
180 # default to check the entire tree
181 else:
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100182 undefined, defined = check_symbols(opts.ignore)
183
184 # now print the output
185 for feature in sorted(undefined):
Valentin Rothberg7c5227a2016-08-28 08:51:28 +0200186 print(red(feature))
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100187
188 files = sorted(undefined.get(feature))
Valentin Rothberg7c5227a2016-08-28 08:51:28 +0200189 print("%s: %s" % (yel("Referencing files"), ", ".join(files)))
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100190
191 sims = find_sims(feature, opts.ignore, defined)
192 sims_out = yel("Similar symbols")
193 if sims:
Valentin Rothberg7c5227a2016-08-28 08:51:28 +0200194 print("%s: %s" % (sims_out, ', '.join(sims)))
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100195 else:
Valentin Rothberg7c5227a2016-08-28 08:51:28 +0200196 print("%s: %s" % (sims_out, "no similar symbols found"))
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100197
198 if opts.find:
Valentin Rothberg7c5227a2016-08-28 08:51:28 +0200199 print("%s:" % yel("Commits changing symbol"))
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100200 commits = find_commits(feature, opts.diff)
201 if commits:
202 for commit in commits:
203 commit = commit.split(" ", 1)
Valentin Rothberg7c5227a2016-08-28 08:51:28 +0200204 print("\t- %s (\"%s\")" % (yel(commit[0]), commit[1]))
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100205 else:
Valentin Rothberg7c5227a2016-08-28 08:51:28 +0200206 print("\t- no commit found")
207 print() # new line
Valentin Rothbergc7455662015-06-01 16:00:20 +0200208
209
210def yel(string):
211 """
212 Color %string yellow.
213 """
Andrew Donnellan4c73c082016-07-05 17:47:37 +1000214 return "\033[33m%s\033[0m" % string if color else string
Valentin Rothbergc7455662015-06-01 16:00:20 +0200215
216
217def red(string):
218 """
219 Color %string red.
220 """
Andrew Donnellan4c73c082016-07-05 17:47:37 +1000221 return "\033[31m%s\033[0m" % string if color else string
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100222
223
224def execute(cmd):
225 """Execute %cmd and return stdout. Exit in case of error."""
Valentin Rothbergf175ba12016-08-27 10:59:07 +0200226 try:
227 cmdlist = cmd.split(" ")
Valentin Rothberg7c5227a2016-08-28 08:51:28 +0200228 stdout = subprocess.check_output(cmdlist, stderr=subprocess.STDOUT, shell=False)
229 stdout = stdout.decode(errors='replace')
Valentin Rothbergf175ba12016-08-27 10:59:07 +0200230 except subprocess.CalledProcessError as fail:
231 exit("Failed to execute %s\n%s" % (cmd, fail))
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100232 return stdout
233
234
Valentin Rothberga42fa922015-06-01 16:00:19 +0200235def 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"
238 % (symbol, diff))
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100239 return [x for x in commits.split("\n") if x]
Valentin Rothberga42fa922015-06-01 16:00:19 +0200240
241
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100242def tree_is_dirty():
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")
246 for line in stdout:
247 if re.findall(r"[URMADC]{1}", line[:2]):
248 return True
249 return False
250
251
252def get_head():
253 """Return commit hash of current HEAD."""
254 stdout = execute("git rev-parse HEAD")
255 return stdout.strip('\n')
256
257
Valentin Rothberge2042a82015-10-15 10:37:47 +0200258def partition(lst, size):
259 """Partition list @lst into eveni-sized lists of size @size."""
Valentin Rothberg7c5227a2016-08-28 08:51:28 +0200260 return [lst[i::size] for i in range(size)]
Valentin Rothberge2042a82015-10-15 10:37:47 +0200261
262
263def init_worker():
264 """Set signal handler to ignore SIGINT."""
265 signal.signal(signal.SIGINT, signal.SIG_IGN)
266
267
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100268def find_sims(symbol, ignore, defined = []):
269 """Return a list of max. ten Kconfig symbols that are string-similar to
270 @symbol."""
271 if defined:
272 return sorted(difflib.get_close_matches(symbol, set(defined), 10))
273
274 pool = Pool(cpu_count(), init_worker)
275 kfiles = []
276 for gitfile in get_files():
277 if REGEX_FILE_KCONFIG.match(gitfile):
278 kfiles.append(gitfile)
279
280 arglist = []
281 for part in partition(kfiles, cpu_count()):
282 arglist.append((part, ignore))
283
284 for res in pool.map(parse_kconfig_files, arglist):
285 defined.extend(res[0])
286
287 return sorted(difflib.get_close_matches(symbol, set(defined), 10))
288
289
290def get_files():
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":
295 stdout = stdout[:-1]
296
297 files = []
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/"):
302 continue
303 files.append(gitfile)
304 return files
305
306
Valentin Rothbergcf132e42015-04-29 16:58:27 +0200307def check_symbols(ignore):
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100308 """Find undefined Kconfig symbols and return a dict with the symbol as key
Valentin Rothbergcf132e42015-04-29 16:58:27 +0200309 and a list of referencing files as value. Files matching %ignore are not
310 checked for undefined symbols."""
Valentin Rothberge2042a82015-10-15 10:37:47 +0200311 pool = Pool(cpu_count(), init_worker)
312 try:
313 return check_symbols_helper(pool, ignore)
314 except KeyboardInterrupt:
315 pool.terminate()
316 pool.join()
317 sys.exit(1)
318
319
320def 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."""
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200323 source_files = []
324 kconfig_files = []
Valentin Rothberge2042a82015-10-15 10:37:47 +0200325 defined_features = []
326 referenced_features = dict() # {file: [features]}
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200327
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100328 for gitfile in get_files():
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200329 if REGEX_FILE_KCONFIG.match(gitfile):
330 kconfig_files.append(gitfile)
331 else:
Valentin Rothberge2042a82015-10-15 10:37:47 +0200332 if ignore and not re.match(ignore, gitfile):
333 continue
334 # add source files that do not match the ignore pattern
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200335 source_files.append(gitfile)
336
Valentin Rothberge2042a82015-10-15 10:37:47 +0200337 # parse source files
338 arglist = partition(source_files, cpu_count())
339 for res in pool.map(parse_source_files, arglist):
340 referenced_features.update(res)
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200341
Valentin Rothberge2042a82015-10-15 10:37:47 +0200342
343 # parse kconfig files
344 arglist = []
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)
351
352 # inverse mapping of referenced_features to dict(feature: [files])
353 inv_map = dict()
Valentin Rothberg7c5227a2016-08-28 08:51:28 +0200354 for _file, features in referenced_features.items():
Valentin Rothberge2042a82015-10-15 10:37:47 +0200355 for feature in features:
356 inv_map[feature] = inv_map.get(feature, set())
357 inv_map[feature].add(_file)
358 referenced_features = inv_map
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200359
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100360 undefined = {} # {feature: [files]}
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200361 for feature in sorted(referenced_features):
Valentin Rothbergcc641d52014-11-08 20:56:35 +0100362 # filter some false positives
363 if feature == "FOO" or feature == "BAR" or \
364 feature == "FOO_BAR" or feature == "XXX":
365 continue
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200366 if feature not in defined_features:
367 if feature.endswith("_MODULE"):
Valentin Rothbergcc641d52014-11-08 20:56:35 +0100368 # avoid false positives for kernel modules
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200369 if feature[:-len("_MODULE")] in defined_features:
370 continue
Valentin Rothbergb1a3f242015-03-16 12:16:14 +0100371 undefined[feature] = referenced_features.get(feature)
Valentin Rothberg1b2c8412015-11-26 14:17:15 +0100372 return undefined, defined_features
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200373
374
Valentin Rothberge2042a82015-10-15 10:37:47 +0200375def 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
382
383
384def parse_source_file(sfile):
385 """Parse @sfile and return a list of referenced Kconfig features."""
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200386 lines = []
Valentin Rothberge2042a82015-10-15 10:37:47 +0200387 references = []
388
389 if not os.path.exists(sfile):
390 return references
391
Valentin Rothberg7c5227a2016-08-28 08:51:28 +0200392 with open(sfile, "r", encoding='utf-8', errors='replace') as stream:
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200393 lines = stream.readlines()
394
395 for line in lines:
396 if not "CONFIG_" in line:
397 continue
398 features = REGEX_SOURCE_FEATURE.findall(line)
399 for feature in features:
400 if not REGEX_FILTER_FEATURES.search(feature):
401 continue
Valentin Rothberge2042a82015-10-15 10:37:47 +0200402 references.append(feature)
403
404 return references
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200405
406
407def get_features_in_line(line):
408 """Return mentioned Kconfig features in @line."""
409 return REGEX_FEATURE.findall(line)
410
411
Valentin Rothberge2042a82015-10-15 10:37:47 +0200412def 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
415 pattern."""
416 kconfig_files = args[0]
417 ignore = args[1]
418 defined_features = []
419 referenced_features = dict()
420
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
426 continue
427 referenced_features[kfile] = references
428 return (defined_features, referenced_features)
429
430
431def parse_kconfig_file(kfile):
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200432 """Parse @kfile and update feature definitions and references."""
433 lines = []
Valentin Rothberge2042a82015-10-15 10:37:47 +0200434 defined = []
435 references = []
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200436 skip = False
437
Valentin Rothberge2042a82015-10-15 10:37:47 +0200438 if not os.path.exists(kfile):
439 return defined, references
440
Valentin Rothberg7c5227a2016-08-28 08:51:28 +0200441 with open(kfile, "r", encoding='utf-8', errors='replace') as stream:
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200442 lines = stream.readlines()
443
444 for i in range(len(lines)):
445 line = lines[i]
446 line = line.strip('\n')
Valentin Rothbergcc641d52014-11-08 20:56:35 +0100447 line = line.split("#")[0] # ignore comments
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200448
449 if REGEX_KCONFIG_DEF.match(line):
450 feature_def = REGEX_KCONFIG_DEF.findall(line)
Valentin Rothberge2042a82015-10-15 10:37:47 +0200451 defined.append(feature_def[0])
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200452 skip = False
453 elif REGEX_KCONFIG_HELP.match(line):
454 skip = True
455 elif skip:
Valentin Rothbergcc641d52014-11-08 20:56:35 +0100456 # ignore content of help messages
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200457 pass
458 elif REGEX_KCONFIG_STMT.match(line):
Valentin Rothberge2042a82015-10-15 10:37:47 +0200459 line = REGEX_QUOTES.sub("", line)
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200460 features = get_features_in_line(line)
Valentin Rothbergcc641d52014-11-08 20:56:35 +0100461 # multi-line statements
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200462 while line.endswith("\\"):
463 i += 1
464 line = lines[i]
465 line = line.strip('\n')
466 features.extend(get_features_in_line(line))
467 for feature in set(features):
Valentin Rothberg0bd38ae2015-07-27 12:33:05 +0200468 if REGEX_NUMERIC.match(feature):
469 # ignore numeric values
470 continue
Valentin Rothberge2042a82015-10-15 10:37:47 +0200471 references.append(feature)
472
473 return defined, references
Valentin Rothberg24fe1f02014-09-27 16:30:45 +0200474
475
476if __name__ == "__main__":
477 main()