Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Tool to determine which headers are needed/not_needed?

Reply
Thread Tools

Tool to determine which headers are needed/not_needed?

 
 
C_guy
Guest
Posts: n/a
 
      09-10-2008
Does anyone know of a (hopefully free) tool that can traverse a
project and determine which "#include"s are not needed or needed in
every .C file? This would be helpful in removing header inclusions
that are redundant and/or unnecessary.

Thanks!
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      09-11-2008
C_guy wrote:
>
> Does anyone know of a (hopefully free) tool that can traverse a
> project and determine which "#include"s are not needed or needed
> in every .C file? This would be helpful in removing header
> inclusions that are redundant and/or unnecessary.


Use your editor. Comment out a #include statement. Recompile. If
no errors appear, that statement can go. If errors appear, remove
the comment. Repeat.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      09-11-2008
On Wed, 10 Sep 2008 08:10:56 -0700 (PDT), C_guy <(E-Mail Removed)>
wrote:

>Does anyone know of a (hopefully free) tool that can traverse a
>project and determine which "#include"s are not needed or needed in
>every .C file? This would be helpful in removing header inclusions
>that are redundant and/or unnecessary.


If you start with none, the compiler will tell every time it
encounters something that would have been in a header. Add one at a
time till the diagnostics disappear.

Other than a pretty small time penalty, including an unnecessary
header or two usually doesn't cause problems.

--
Remove del for email
 
Reply With Quote
 
robertwessel2@yahoo.com
Guest
Posts: n/a
 
      09-11-2008
On Sep 10, 7:09*pm, CBFalconer <(E-Mail Removed)> wrote:
> C_guy wrote:
>
> > Does anyone know of a (hopefully free) tool that can traverse a
> > project and determine which "#include"s are not needed or needed
> > in every .C file? *This would be helpful in removing header
> > inclusions that are redundant and/or unnecessary.

>
> Use your editor. *Comment out a #include statement. *Recompile. *If
> no errors appear, that statement can go. *If errors appear, remove
> the comment. *Repeat.



Surely that's too simplistic - consider a header containing:

#undef FOO
#define FOO 17

Assuming a prior definition of FOO (to something other than 17), one
could easily see the compilation succeed, yet no longer be producing
the correct program).

Or a header that contains:

int a=1;

And everywhere else (possibly in the same compilation unit, if needed)
there's:

int a;

Many other potential problems exist

Which is not to say that sort of things is good programming practice
(and frankly most dependencies like that probably reflect poor
design), but we've all seen really bizarre stuff in headers, no?

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      09-11-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote, On 11/09/08 02:48:
> On Sep 10, 7:09 pm, CBFalconer <(E-Mail Removed)> wrote:
>> C_guy wrote:
>>
>>> Does anyone know of a (hopefully free) tool that can traverse a
>>> project and determine which "#include"s are not needed or needed
>>> in every .C file? This would be helpful in removing header
>>> inclusions that are redundant and/or unnecessary.

>> Use your editor. Comment out a #include statement. Recompile. If
>> no errors appear, that statement can go. If errors appear, remove
>> the comment. Repeat.

>
>
> Surely that's too simplistic - consider a header containing:
>
> #undef FOO
> #define FOO 17
>
> Assuming a prior definition of FOO (to something other than 17), one
> could easily see the compilation succeed, yet no longer be producing
> the correct program).
>
> Or a header that contains:
>
> int a=1;
>
> And everywhere else (possibly in the same compilation unit, if needed)
> there's:
>
> int a;
>
> Many other potential problems exist


A far more likely potential problem in my opinion is if the header contains:
double foo(...)

Then with the default options on a lot of compilers (or with C89
conformance added to the defaults) the compiler will *not* complain, it
will just silently compiler the code with what is now undefined
behaviour which may or may not work. If foo currently returns an int it
is even worse, because then (if the parameters are not a problem) it
*will* work until foo() the return type of foo is changed.

> Which is not to say that sort of things is good programming practice
> (and frankly most dependencies like that probably reflect poor
> design), but we've all seen really bizarre stuff in headers, no?


You don't need bizarre stuff in the header for commenting it out to
silently cause problems. Simple good practice can do it.

The OP would be well advised to ensure that the compiler is told to warn
about functions being called without a prototype in scope, and as much
else as possible. This will help catch problems where a header is *not*
included (directly or indirectly) but is needed. How to do this (and if
it is possible) is compiler dependant and so should be asked on a group
dedicated to the compiler.
--
Flash Gordon
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-11-2008
Jack Klein wrote:
> On Wed, 10 Sep 2008 08:10:56 -0700 (PDT), C_guy <(E-Mail Removed)>
> wrote in comp.lang.c:
>
>> Does anyone know of a (hopefully free) tool that can traverse a
>> project and determine which "#include"s are not needed or needed in
>> every .C file? This would be helpful in removing header inclusions
>> that are redundant and/or unnecessary.

>
> I see you've already got a few recommendations for the comment out and
> look for errors school of thought, which does work but can take an
> enormous amount of time for a large source code base.
>
> The only tool that I know of that does this, and really well, is not
> free. PC-Lint, http://www.gimpel.com. This feature is really a nice
> little bonus. Its code checking function alone is worth many times
> its price to anyone producing professional code.
>

I hadn't looked before, but Sun Studio lint (which is free) also reports
unnecessary headers.

--
Ian Collins.
 
Reply With Quote
 
Amandil
Guest
Posts: n/a
 
      09-11-2008
On Sep 10, 8:09*pm, CBFalconer <(E-Mail Removed)> wrote:
> C_guy wrote:
>
> > Does anyone know of a (hopefully free) tool that can traverse a
> > project and determine which "#include"s are not needed or needed
> > in every .C file? *This would be helpful in removing header
> > inclusions that are redundant and/or unnecessary.

>
> Use your editor. *Comment out a #include statement. *Recompile. *If
> no errors appear, that statement can go. *If errors appear, remove
> the comment. *Repeat.
>


That won't necessarily accomplish what the OP is trying to do. For
example:
/* Begin file: */
#include <stdarg.h>

int main()
{
puts("Hello, World");
return EXIT_SUCCESS;
}
/* End of file */

The above program will flag exactly 1 error, EXIT_SUCCESS not having
been
#define'd. #include'ing <stdlib.h> will fix that. The OP wants this
tool to
remind him to also #include <stdio.h>, and notify him that <stdarg.h>
is
unnecessary. Compiling with -Wall (or whatever it is on other
compilers) will
_warn_ me about the missing declaration for puts() (Which I may or may
not remember
is in <stdio.h>), but it won't make a peep about the extra header.

Mr. Klein helpfully mentioned a tool which may help you.

-- Marty Amandil
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-11-2008
Amandil wrote:
> CBFalconer <(E-Mail Removed)> wrote:
>> C_guy wrote:
>>
>>> Does anyone know of a (hopefully free) tool that can traverse a
>>> project and determine which "#include"s are not needed or needed
>>> in every .C file? This would be helpful in removing header
>>> inclusions that are redundant and/or unnecessary.

>>
>> Use your editor. Comment out a #include statement. Recompile.
>> If no errors appear, that statement can go. If errors appear,
>> remove the comment. Repeat.

>
> That won't necessarily accomplish what the OP is trying to do. For
> example:
> /* Begin file: */
> #include <stdarg.h>
>
> int main()
> {
> puts("Hello, World");
> return EXIT_SUCCESS;
> }
> /* End of file */
>
> The above program will flag exactly 1 error, EXIT_SUCCESS not
> having been #define'd. #include'ing <stdlib.h> will fix that. The
> OP wants this tool to remind him to also #include <stdio.h>, and
> notify him that <stdarg.h> is unnecessary. Compiling with -Wall
> (or whatever it is on other compilers) will _warn_ me about the
> missing declaration for puts() (Which I may or may not remember
> is in <stdio.h>), but it won't make a peep about the extra header.


No, C_guy wants to know what he can delete. Your suggestion above
will produce errors for the puts and the EXIT_SUCCESS usage.
Removing the include of stdarg.h will not affect that.

When something is undefined, most systems have an easy way to
access help on that item, which will specify the include file to
use. For example:

puts
====

Syntax
------
#include <stdio.h>

int puts(const char *string);

and you can always search the C standard for the same information.
The text version is very handy for this. Available (bzip2
compressed) at:

<http://cbfalconer.home.att.net/download/n869_txt.bz2>

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      09-11-2008
On Thu, 11 Sep 2008 17:53:37 -0400, CBFalconer wrote:
> Amandil wrote:
>> CBFalconer <(E-Mail Removed)> wrote:
>>> C_guy wrote:
>>>
>>>> Does anyone know of a (hopefully free) tool that can traverse a
>>>> project and determine which "#include"s are not needed or needed in
>>>> every .C file? This would be helpful in removing header inclusions
>>>> that are redundant and/or unnecessary.
>>>
>>> Use your editor. Comment out a #include statement. Recompile. If no
>>> errors appear, that statement can go. If errors appear, remove the
>>> comment. Repeat.

>>
>> That won't necessarily accomplish what the OP is trying to do. For
>> example:
>> /* Begin file: */
>> #include <stdarg.h>
>>
>> int main()
>> {
>> puts("Hello, World");
>> return EXIT_SUCCESS;
>> }
>> /* End of file */
>>
>> [snip]

>
> No, C_guy wants to know what he can delete. [snip]


Fine, then given

/* Begin file: */
#include <stdio.h>

int main()
{
puts("Hello, World");
return 0;
}
/* End of file */

are you saying C_guy wants the inclusion of <stdio.h> to go?
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-11-2008
CBFalconer <(E-Mail Removed)> writes:
[...]
> and you can always search the C standard for the same information.
> The text version is very handy for this. Available (bzip2
> compressed) at:
>
> <http://cbfalconer.home.att.net/download/n869_txt.bz2>


No, the C standard is not available as a text version.

n869_txt.bz2 is a pre-standard draft. n1256.pdf is the latest
post-standard draft, or the standard itself is available (but not
free) via ANSI or ISO.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with wsgiref.headers.Headers Phil Python 4 01-17-2010 04:47 PM
Server cannot clear headers after HTTP headers have been sent Ian ASP .Net Security 2 03-20-2007 09:00 AM
Reading 'received' headers: Email Headers Parsing dont bother Python 0 03-03-2004 08:18 PM
Looking For a tool to crawl ASP and compare to a folder of files to see which are used and which are not. Steve Mauldin ASP General 0 02-06-2004 04:15 PM
nuby: determine method passed and determine the receiver that received the method Pea, Botp Ruby 1 01-24-2004 07:51 PM



Advertisments