detect indirect header dependencies
PATCH 1/1 detect indirect header dependencies
The kernel generally follows the rule that header files are included from every other source file using its declarations and not just through intermediate headers. We already have scripts to check this for version.h and config.h, but not for anything else.
I have used the algorithm from scripts/checkversion.pl to make the checks far more general. With scripts/checkheaders.pl, it is possible to check source files against the input from a ctags file, similar to checkversion.
On top of that, checkheaders.sh can be used in place of sparse to do automated checks during a kernel build.
The required coding policy is:
- For every C or preprocessor symbol, include the header declaring it directly from the file using it.
- If the declaration is in include/asm-generic/foo.h, include the file as <asm/foo.h>.
- If the declaration is in include/asm-*/foo.h and include/linux/foo.h exists, include the file as <linux/foo.h>.
- Some exceptions to these rules are hardcoded into the scripts.
There are tons of warnings from this, including a small number of false positives, so I doubt it makes much sense to attempt to fix them all. Also, some parts of the kernel might have conflicting policies.
Still, this might turn out as a helpful tool for janitors trying to clean up the include hierarchy.
checkheaders.sh new file +58 -0
@@ -0,0 +1,58 @@ +#!/bin/sh +# NAME +# checkheaders.sh - check a source file for correct use of header files +# +# SYNOPSIS +# scripts/checkheaders.sh [cpp-options] file +# +# DESCRIPTION +# This is meant as a simple frontend for scripts/checkheader.pl +# that automatically determines the headers to check. +# +# It will print a warning for every directly included header +# that does not contain any referenced identifier as well as +# every indirectly included header that is referenced. +# +# When both include/asm/foo.h and include/linux/foo.h exist, +# assume that we need to include <linux/foo.h>, even if we +# only use identifiers from <asm/foo.h>. +# +# EXAMPLES: +# scripts/checkheaders.sh -Iinclude -Iobj/include2 -D__KERNEL__ foo/bar.c +# checks only the file foo/bar.c +# +# make C=1 CHECK=$PWD/scripts/checkheaders.sh +# builds the kernel while using this script to check every +# single C source file. +# +# AUTHOR +# Arnd Bergmann <[email protected]> +# + +srctree="${srctree:-.}" +CPP="${CPP:-gcc -E}" + +# argument processing: assume the last non-option argument +# is the source file +for i in "$@" ; do + if [ "$i" = "${i#-}" ] ; then + file="$i" + fi +done + +if [ ! $file ] ; then + echo $0: no input file + exit 1 +fi + +# use the preprocessor to find all included files, then +# get the found definitions from ctags and feed them to +# the checkheaders.pl backend. +${CPP} "$@" | + grep '^#.*\".*include' | + cut -f 2 -d'"' | + sort | + uniq | + ctags --filter --c-kinds=cdefgpstuvx | + perl "${srctree}"/scripts/checkheaders.pl "${file}" | + sort -k2 -t: -n 1>&2
checkheaders.pl new file +193 -0
193 lines not on this page · raw message
_______________________________________________
Kernel-janitors mailing list
[email protected]
http://lists.osdl.org/mailman/listinfo/kernel-janitors
[Just noticed I had sent a -p0 patch accidentally, this is the -p1 patch]
The kernel generally follows the rule that header files are included from every other source file using its declarations and not just through intermediate headers. We already have scripts to check this for version.h and config.h, but not for anything else.
I have used the algorithm from scripts/checkversion.pl to make the checks far more general. With scripts/checkheaders.pl, it is possible to check source files against the input from a ctags file, similar to checkversion.
On top of that, checkheaders.sh can be used in place of sparse to do automated checks during a kernel build.
The required coding policy is:
- For every C or preprocessor symbol, include the header declaring it directly from the file using it.
- If the declaration is in include/asm-generic/foo.h, include the file as <asm/foo.h>.
- If the declaration is in include/asm-*/foo.h and include/linux/foo.h exists, include the file as <linux/foo.h>.
- Some exceptions to these rules are hardcoded into the scripts.
There are tons of warnings from this, including a small number of false positives, so I doubt it makes much sense to attempt to fix them all. Also, some parts of the kernel might have conflicting policies.
Still, this might turn out as a helpful tool for janitors trying to clean up the include hierarchy.
---
scripts/checkheaders.sh new file +58 -0
@@ -0,0 +1,58 @@ +#!/bin/sh +# NAME +# checkheaders.sh - check a source file for correct use of header files +# +# SYNOPSIS +# scripts/checkheaders.sh [cpp-options] file +# +# DESCRIPTION +# This is meant as a simple frontend for scripts/checkheader.pl +# that automatically determines the headers to check. +# +# It will print a warning for every directly included header +# that does not contain any referenced identifier as well as +# every indirectly included header that is referenced. +# +# When both include/asm/foo.h and include/linux/foo.h exist, +# assume that we need to include <linux/foo.h>, even if we +# only use identifiers from <asm/foo.h>. +# +# EXAMPLES: +# scripts/checkheaders.sh -Iinclude -Iobj/include2 -D__KERNEL__ foo/bar.c +# checks only the file foo/bar.c +# +# make C=1 CHECK=$PWD/scripts/checkheaders.sh +# builds the kernel while using this script to check every +# single C source file. +# +# AUTHOR +# Arnd Bergmann <[email protected]> +# + +srctree="${srctree:-.}" +CPP="${CPP:-gcc -E}" + +# argument processing: assume the last non-option argument +# is the source file +for i in "$@" ; do + if [ "$i" = "${i#-}" ] ; then + file="$i" + fi +done + +if [ ! $file ] ; then + echo $0: no input file + exit 1 +fi + +# use the preprocessor to find all included files, then +# get the found definitions from ctags and feed them to +# the checkheaders.pl backend. +${CPP} "$@" | + grep '^#.*\".*include' | + cut -f 2 -d'"' | + sort | + uniq | + ctags --filter --c-kinds=cdefgpstuvx | + perl "${srctree}"/scripts/checkheaders.pl "${file}" | + sort -k2 -t: -n 1>&2
scripts/checkheaders.pl new file +193 -0
193 lines not on this page · raw message
_______________________________________________
Kernel-janitors mailing list
[email protected]
http://lists.osdl.org/mailman/listinfo/kernel-janitors