public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* include-police.sh
@ 2009-03-29  4:01 Daniel Mack
  0 siblings, 0 replies; only message in thread
From: Daniel Mack @ 2009-03-29  4:01 UTC (permalink / raw)
  To: kernel list

Hi,

I hacked a small and dumb shell script awhile ago that can help finding
superfluous include files. Even though it's far from perfect (see the 
inline comments), its output had helped occasionally, so I wanted to
share this, also in relationship to the 'reverse Xmas tree' thread.

Comments welcome :)

Daniel


#!/bin/sh
#
# Find possibly unneeded include files in C/C++ files
# Daniel Mack <daniel@caiaq.de>, GPLv2
#
# The script searches for includes in the given file and removes them, one
# after the other. After each cycle, 'make' is called to build the object
# file. Includes found safe to omit (i.e., the removal did not break the
# built) are echoed, and the file is reset to its original state
# again.
#
# At the end of the script, the file will be restored to what it was in
# the beginning.
#
# Limitations/bugs:
#  * The output is only to be taken as _hint_ for developers
#  * The script is not aware of conditional includes
#  * Include files that do forward declarations of locally implemented
#    symbols are erroneously reported superfluous
#  * Depends on 'make' as build tool and the object file is expected
#    to be at the same location as the source file
#  * Doesn't report anything when the built fails anyway (for other reasons)
#

file=$1

if [ -z "$file" ]; then echo "Usage: $0 <file> [build params]"; exit 1; fi
if [ ! -f "$file" ]; then echo "$0: unable to open file $file"; exit 2; fi

tmp=/tmp/$(basename $file)-$$
orig=/tmp/$(basename $file).orig-$$
obj=${file%%.c}.o
shift

cp $file $orig

for include in $(grep ^#include $file | cut -f2 -d' '); do
	echo checking $include ...
	cp $orig $tmp
	grep -v '^#include '$include $tmp > $file
	make $* $obj >/dev/null 2>&1
	[ $? -eq 0 ] && echo --- $include can be omitted
done

cp $orig $file
rm -f $tmp $orig


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2009-03-29  4:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-29  4:01 include-police.sh Daniel Mack

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox