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

Commit 61d75fd

Browse files
ulfalizernashif
authored andcommitted
check_compliance.py: Detect bad header comments and other nits
Add a generic kitchen-sink Nits test for various minor nits that aren't already covered by tools like checkpatch.pl and pylint. So far checks this: - Header comments in Kconfig files - Missing newlines at the end of various source files (probably a bad editor setting) - Leading/trailing blank lines in files Signed-off-by: Ulf Magnusson <[email protected]>
1 parent 0df7951 commit 61d75fd

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

scripts/check_compliance.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,76 @@ def run(self):
659659
"CODEOWNERS file to cover those files")
660660

661661

662+
class Nits(ComplianceTest):
663+
"""
664+
Checks various nits in added/modified files. Doesn't check stuff that's
665+
already covered by e.g. checkpatch.pl and pylint.
666+
"""
667+
_name = "Nits"
668+
_doc = "https://docs.zephyrproject.org/latest/contribute/#coding-style"
669+
670+
def run(self):
671+
self.prepare(GIT_TOP)
672+
673+
# Loop through added/modified files
674+
for fname in git("diff", "--name-only", "--diff-filter=d",
675+
self.commit_range).splitlines():
676+
is_kconfig = "Kconfig" in fname
677+
678+
if is_kconfig:
679+
self.check_kconfig_file(fname)
680+
681+
if fname.endswith((".c", ".cpp", ".h", ".ld", ".py", ".rst",
682+
".yaml", ".yml")) or is_kconfig:
683+
self.check_source_file(fname)
684+
685+
def check_kconfig_file(self, fname):
686+
# Nits related to Kconfig files
687+
688+
with open(os.path.join(GIT_TOP, fname), encoding="utf-8") as f:
689+
contents = f.read()
690+
691+
# 'Kconfig - yada yada' has a copy-pasted redundant filename at the
692+
# start. This probably means all of the header was copy-pasted.
693+
if re.match(r"\s*#\s*(K|k)config[\w.-]*\s*-", contents):
694+
self.add_failure("""
695+
Please use this format for the header in '{}' (see
696+
https://docs.zephyrproject.org/latest/guides/kconfig/index.html#header-comments-and-other-nits):
697+
698+
# <Overview of symbols defined in the file, preferably in plain English>
699+
(Blank line)
700+
# Copyright (c) 2019 ...
701+
# SPDX-License-Identifier: <License>
702+
(Blank line)
703+
(Kconfig definitions)
704+
705+
Skip the "Kconfig - " part of the first line, since it's clear that the comment
706+
is about Kconfig from context. The "# Kconfig - " is what triggers this
707+
failure.
708+
""".format(fname))
709+
710+
def check_source_file(self, fname):
711+
# Generic nits related to various source files
712+
713+
with open(os.path.join(GIT_TOP, fname), encoding="utf-8") as f:
714+
contents = f.read()
715+
716+
if not contents.endswith("\n"):
717+
self.add_failure("Missing newline at end of '{}'. Check your text "
718+
"editor settings.".format(fname))
719+
720+
if contents.startswith("\n"):
721+
self.add_failure("Please remove blank lines at start of '{}'"
722+
.format(fname))
723+
724+
if contents.endswith("\n\n"):
725+
self.add_failure("Please remove blank lines at end of '{}'"
726+
.format(fname))
727+
728+
662729
class Documentation(ComplianceTest):
663730
"""
664731
Checks if documentation build has generated any new warnings.
665-
666732
"""
667733
name = "Documentation"
668734
doc = "https://docs.zephyrproject.org/latest/guides/documentation/index.html"

0 commit comments

Comments
 (0)