scripts/clean-includes: added duplicate #include check

Enhance the clean-includes script to optionally check for duplicate #include
entries.

Script might output false positive entries as well. Such entries should
not be removed. So if it finds any duplicate entries script will
terminate with an exit status 1. Then each and every file should be
checked manually and corrected if necessary.

In order to enable the check use --check-dup-head option with
scripts/clean-includes.

Reviewed-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Anand J <anand.indukala@gmail.com>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
This commit is contained in:
Anand J 2016-10-21 12:27:06 +05:30 committed by Michael Tokarev
parent bdbcb547cf
commit d66253e46a

View File

@ -14,15 +14,18 @@
# the top-level directory. # the top-level directory.
# Usage: # Usage:
# clean-includes [--git subjectprefix] file ... # clean-includes [--git subjectprefix] [--check-dup-head] file ...
# or # or
# clean-includes [--git subjectprefix] --all # clean-includes [--git subjectprefix] [--check-dup-head] --all
# #
# If the --git subjectprefix option is given, then after making # If the --git subjectprefix option is given, then after making
# the changes to the files this script will create a git commit # the changes to the files this script will create a git commit
# with the subject line "subjectprefix: Clean up includes" # with the subject line "subjectprefix: Clean up includes"
# and a boilerplate commit message. # and a boilerplate commit message.
# #
# If --check-dup-head is specified, additionally check for duplicate
# header includes.
#
# Using --all will cause clean-includes to run on the whole source # Using --all will cause clean-includes to run on the whole source
# tree (excluding certain directories which are known not to need # tree (excluding certain directories which are known not to need
# handling). # handling).
@ -45,23 +48,40 @@
GIT=no GIT=no
DUPHEAD=no
# Extended regular expression defining files to ignore when using --all # Extended regular expression defining files to ignore when using --all
XDIRREGEX='^(tests/tcg|tests/multiboot|pc-bios|disas/libvixl)' XDIRREGEX='^(tests/tcg|tests/multiboot|pc-bios|disas/libvixl)'
if [ $# -ne 0 ] && [ "$1" = "--git" ]; then while true
if [ $# -eq 1 ]; then do
echo "--git option requires an argument" case $1 in
exit 1 "--git")
fi if [ $# -eq 1 ]; then
GITSUBJ="$2" echo "--git option requires an argument"
GIT=yes exit 1
shift fi
shift GITSUBJ="$2"
fi GIT=yes
shift
shift
;;
"--check-dup-head")
DUPHEAD=yes
shift
;;
"--")
shift
break
;;
*)
break
;;
esac
done
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
echo "Usage: clean-includes [--git subjectprefix] [--all | foo.c ...]" echo "Usage: clean-includes [--git subjectprefix] [--check-dup-head] [--all | foo.c ...]"
echo "(modifies the files in place)" echo "(modifies the files in place)"
exit 1 exit 1
fi fi
@ -91,7 +111,6 @@ cat >"$COCCIFILE" <<EOT
) )
EOT EOT
for f in "$@"; do for f in "$@"; do
case "$f" in case "$f" in
*.inc.c) *.inc.c)
@ -154,6 +173,15 @@ for f in "$@"; do
done done
if [ "$DUPHEAD" = "yes" ]; then
egrep "^[[:space:]]*#[[:space:]]*include" "$@" | tr -d '[:blank:]' \
| sort | uniq -c | awk '{if ($1 > 1) print $0}'
if [ $? -eq 0 ]; then
echo "Found duplicate header file includes. Please check the above files manually."
exit 1
fi
fi
if [ "$GIT" = "yes" ]; then if [ "$GIT" = "yes" ]; then
git add -- "$@" git add -- "$@"
git commit --signoff -F - <<EOF git commit --signoff -F - <<EOF