Skip to content
This repository was archived by the owner on Apr 6, 2022. It is now read-only.

Commit 77d600c

Browse files
ulfalizernashif
authored andcommitted
check_compliance.py: Detect refs to undef. symbols in samples and tests
Extend the check for references to undefined Kconfig symbols to also detect undefined symbols in samples and tests. Samples and tests were skipped due to using separate Kconfig trees, which hid the symbols defined in them. Work around it by grepping for Kconfig symbol definitions in them instead. Keep properly parsing the main Kconfig tree, as it's needed to see symbol names that are stitched together with the Kconfig preprocessor. Signed-off-by: Ulf Magnusson <[email protected]>
1 parent 921d4f3 commit 77d600c

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

scripts/check_compliance.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,14 @@ def check_no_undef_outside_kconfig(self, kconf):
366366
#
367367
# - *, meant for comments like '#endif /* CONFIG_FOO_* */
368368
#
369-
# We skip the samples/ and tests/ directories for now. They often
370-
# contain Kconfig files that are not part of the main Kconfig tree,
371-
# which will trigger false positives until we do something fancier.
372-
#
373-
# We also skip doc/releases, which often references removed symbols.
369+
# We skip doc/releases, which often references removed symbols.
374370

375371
# Warning: Needs to work with both --perl-regexp and the 're' module
376372
regex = r"\bCONFIG_[A-Z0-9_]+\b(?!\s*##|[$@{*])"
377373

378374
grep_cmd = ["git", "grep", "--line-number", "-I", "--null",
379375
"--perl-regexp", regex,
380-
"--", ":!samples", ":!tests", ":!doc/releases"]
376+
"--", ":!doc/releases"]
381377

382378
grep_process = subprocess.Popen(grep_cmd,
383379
stdout=subprocess.PIPE,
@@ -393,7 +389,7 @@ def check_no_undef_outside_kconfig(self, kconf):
393389
.format(" ".join(grep_cmd), grep_process.returncode,
394390
grep_stdout, grep_stderr))
395391

396-
defined_syms = set(sym.name for sym in kconf.unique_defined_syms)
392+
defined_syms = get_defined_syms(kconf)
397393
undef_to_locs = collections.defaultdict(list)
398394

399395
# splitlines() supports various line terminators
@@ -436,6 +432,41 @@ def check_no_undef_outside_kconfig(self, kconf):
436432
{}""".format(os.path.basename(__file__), undef_desc))
437433

438434

435+
def get_defined_syms(kconf):
436+
# Returns all defined Kconfig symbols. This is complicated by samples and
437+
# tests defining their own Kconfig trees. For those, just grep for
438+
# 'config FOO' to find definitions. Doing it "properly" with Kconfiglib is
439+
# still useful for the main tree, because some symbols are defined using
440+
# preprocessor macros.
441+
442+
# Warning: Needs to work with both --extended-regexp and the 're' module
443+
regex = "^\s*(menu)?config\s*([A-Z0-9_]+)\s*(#|$)"
444+
445+
grep_cmd = ["git", "grep", "-I", "-h", "--extended-regexp", regex,
446+
"--", ":samples", ":tests"]
447+
448+
grep_process = subprocess.Popen(grep_cmd,
449+
stdout=subprocess.PIPE,
450+
stderr=subprocess.PIPE,
451+
cwd=ZEPHYR_BASE)
452+
453+
grep_stdout, grep_stderr = grep_process.communicate()
454+
# Fail if there's anything on stderr too, so that it doesn't get missed
455+
if grep_process.returncode or grep_stderr:
456+
self.error("'{}' failed with exit code {} (while searching for "
457+
"Kconfig symbol definitions)\n\nstdout:\n{}\n\n"
458+
"stderr:\n{}"
459+
.format(" ".join(grep_cmd), grep_process.returncode,
460+
grep_stdout, grep_stderr))
461+
462+
res = set(sym.name for sym in kconf.unique_defined_syms)
463+
464+
for def_line in grep_stdout.decode("utf-8").splitlines():
465+
res.add(re.match(regex, def_line).group(2))
466+
467+
return res
468+
469+
439470
# Many of these are symbols used as examples. Note that the list is sorted
440471
# alphabetically, and skips the CONFIG_ prefix.
441472
UNDEF_KCONFIG_WHITELIST = {
@@ -444,6 +475,7 @@ def check_no_undef_outside_kconfig(self, kconf):
444475
"CDC_ACM_PORT_NAME_",
445476
"CLOCK_STM32_SYSCLK_SRC_",
446477
"CMU",
478+
"BT_6LOWPAN", # Defined in Linux, mentioned in docs
447479
"COUNTER_RTC_STM32_CLOCK_SRC",
448480
"CRC", # Used in TI CC13x2 / CC26x2 SDK comment
449481
"DEEP_SLEEP", # #defined by RV32M1 in ext/
@@ -469,13 +501,15 @@ def check_no_undef_outside_kconfig(self, kconf):
469501
"PEDO_THS_MIN",
470502
"REG1",
471503
"REG2",
504+
"SAMPLE_MODULE_LOG_LEVEL", # Used as an example in samples/subsys/logging
472505
"SEL",
473506
"SHIFT",
474507
"SOC_WATCH", # Issue 13749
475508
"SOME_BOOL",
476509
"SOME_INT",
477510
"SOME_OTHER_BOOL",
478511
"SOME_STRING",
512+
"SRAM2", # Referenced in a comment in samples/application_development
479513
"STACK_SIZE", # Used as an example in the Kconfig docs
480514
"STD_CPP", # Referenced in CMake comment
481515
"TEST1",

0 commit comments

Comments
 (0)