public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Daniel Mack <daniel@caiaq.de>
To: kernel list <linux-kernel@vger.kernel.org>
Subject: include-police.sh
Date: Sun, 29 Mar 2009 06:01:48 +0200	[thread overview]
Message-ID: <20090329040148.GA11678@buzzloop.caiaq.de> (raw)

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


                 reply	other threads:[~2009-03-29  4:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090329040148.GA11678@buzzloop.caiaq.de \
    --to=daniel@caiaq.de \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox