How to scan C files to find the required include files ?

How to scan C files to find the required include files ?

Post by Gaut » Wed, 04 Jun 2003 10:02:47



Hi,

Legacy code has a lot of cross linking. Many source files include
unnecessary include files. I would like to clean this up to improve
maintainability.

Is someone aware of a tool which would identify the required include
files for c source files. Stepping through the code and identifying
the required files is not an option.

Thanks in advance.
Gautam

 
 
 

How to scan C files to find the required include files ?

Post by M?ns Rullg? » Wed, 04 Jun 2003 10:04:34



> Legacy code has a lot of cross linking. Many source files include
> unnecessary include files. I would like to clean this up to improve
> maintainability.

> Is someone aware of a tool which would identify the required include
> files for c source files. Stepping through the code and identifying
> the required files is not an option.

gcc -H

--
M?ns Rullg?rd


 
 
 

How to scan C files to find the required include files ?

Post by Richard Heathfiel » Wed, 04 Jun 2003 10:08:43



> Hi,

> Legacy code has a lot of cross linking. Many source files include
> unnecessary include files. I would like to clean this up to improve
> maintainability.

> Is someone aware of a tool which would identify the required include
> files for c source files. Stepping through the code and identifying
> the required files is not an option.

Your C compiler should do this easily.

Remove all headers.

Compile.

Fix by inserting headers for the functions, types, macros etc that it
complains it can't find.

Make sure you have full backups, Just In Case.

Read this list of instructions all the way through before starting to put it
into action.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

 
 
 

How to scan C files to find the required include files ?

Post by Marc Rochkin » Wed, 04 Jun 2003 10:33:35



Quote:> Hi,

> Legacy code has a lot of cross linking. Many source files include
> unnecessary include files. I would like to clean this up to improve
> maintainability.

> Is someone aware of a tool which would identify the required include
> files for c source files. Stepping through the code and identifying
> the required files is not an option.

Not possible in general, unless the tool can execute the compiled program
and verify that it is performing correctly. This is impractical, and (I
think--my computer science is rusty) even theoretically impossible.

Often the order of headers and which are included affects which of several
alternative implementations is chosen, and that in turn affects the run-time
behavior of the program. This is intertwined with linking, where the
selection of libraries is also involved.

So, I would never mess with includes in a mechanical way. Just because the
program compiles and links doesn't mean it's right.

--Marc

 
 
 

How to scan C files to find the required include files ?

Post by E. Robert Tisdal » Wed, 04 Jun 2003 10:17:02



> Legacy code has a lot of cross linking.
> Many source files include unnecessary include files.
> I would like to clean this up to improve maintainability.
> Is someone aware of a tool
> which would identify the required include files for c source files.
> Stepping through the code
> and identifying the required files is not an option.

I use my GNU C compiler

        > cat main.c
         #include<main.h>

         int main(int argc, char* argv[]) {
           return 0;
           }

         > gcc -I. -MM main.c
         main.o: main.c main.h

 
 
 

How to scan C files to find the required include files ?

Post by Jack Klei » Wed, 04 Jun 2003 11:32:31



wrote in comp.lang.c:

Quote:> Hi,

> Legacy code has a lot of cross linking. Many source files include
> unnecessary include files. I would like to clean this up to improve
> maintainability.

> Is someone aware of a tool which would identify the required include
> files for c source files. Stepping through the code and identifying
> the required files is not an option.

> Thanks in advance.
> Gautam

PCLint claims to be able to do this, and while I have not tested this
specific feature it generally lives up to its claims.  It is neither
free nor open source, and I don't think it's available for any
platform other than MS-DOS or Win32.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

 
 
 

How to scan C files to find the required include files ?

Post by Floyd Davidso » Wed, 04 Jun 2003 12:23:05


[your "followups to" is ignored because this topic is
appropriate in all listed groups and is not unique to clc.]



>> Hi,

>> Legacy code has a lot of cross linking. Many source files include
>> unnecessary include files. I would like to clean this up to improve
>> maintainability.

>> Is someone aware of a tool which would identify the required include
>> files for c source files. Stepping through the code and identifying
>> the required files is not an option.

>Your C compiler should do this easily.

>Remove all headers.

I would recommend doing this slightly in a different way.

In a good editor (vi or emacs are examples) where one can invoke
the compiler without exiting, etc. etc.

Remove each #include directive by commenting it out or using
conditionals, _one_ _at_ _a_ _time_.  

  Remove a header, compile.
  Remove a header, compile: errors; replace header.  
  Remove a header, compile.

And on and on until finished.

For those of us less instantly clueful about which header
a given macro/declaration/ or whatever is in, the uncertainty
of which header to add back in is greatly reduced when only
one possibility exists.

Richard probably _can_ remove a dozen includes, compile and
realize from one glance which six need to be there and which
six don't.  Most of us probably can get 3/4s of them right
the first time...

But if it is done one header at a time, it's 100% every time.
Hence most programmers can instantly spot the need for several
headers (stdlib.h, stdio.h, string.h, etc. etc.), so actually
a modified approach half way between one at a time and all at
at time is probably most productive.  Whack out all the common
ones and see if the errors apply.  Then do the odd ones one
at a time.

Quote:>Compile.

>Fix by inserting headers for the functions, types, macros etc that it
>complains it can't find.

>Make sure you have full backups, Just In Case.

>Read this list of instructions all the way through before starting to put it
>into action.

--
Floyd L. Davidson           <http://web.newsguy.com/floyd_davidson>

 
 
 

How to scan C files to find the required include files ?

Post by Thaddeus L Olczy » Wed, 04 Jun 2003 14:21:07




Quote:>Hi,

>Legacy code has a lot of cross linking. Many source files include
>unnecessary include files. I would like to clean this up to improve
>maintainability.

>Is someone aware of a tool which would identify the required include
>files for c source files. Stepping through the code and identifying
>the required files is not an option.

>Thanks in advance.
>Gautam

Hmm. I'm spouting an algorithm on the spot so I might be off a bit.
Tweak to fix problems.

Define a dependency as A depends on B if A includes B.
Make a list of all files in your source.
Scan each file for includes ( or use gcc -H ). Make an associative
array with key a source files ( here I am including both headers and
source )  and value a list of internal includes. By internal I mean
one which is part of the project source that you previously scanned.
Make a similar associative array for external includes.

Now extract all the source files which have no descendents ( easy
enough, count the number of elements in the value of the internal
associative array ). Call these roots.

Do a topological sort of the source files. Define below this way:
the top it set of the roots.Now mark  all external header includes (
at the end of the include ) with the comment string "//ext:" or
something similar to distinguish it. Next comment out any internal
header include that
doesn't sit in the file directly above with "//int:".

Now, in theory if no file included an absolutely  useless file, you
should be done ( for internal includes ). Of course you can count on
some files including things they don't need, so you need to do more.
-------------------------
Build the thing and save the commands needed to build in a file.
Comment out all headers that are internal in the botom source.
Compile the  files at the bottom of the internal dependency graph.
It should take you a couple of hours to write a Unix ( bash,awk,sed,
etc )/Perl/Python/Ruby script to do this.

(3)
Comment in internal includes until you get the files to compile.
( You can even write a script to do this. Compile get an error.
(2) Uncomment an include. (1) Recompile. If you get new errors, leave
the include uncommented and go to (2). If not then comment the
include go to the next include, go to (1). )
).
(4)
Then start the whole process over again until the process from (3)
to (4) results in no changes in which include files are commented
out. ( You had to comment in all the files you commented in (3) ).

Then do the level of headers that are one level above the previous
level of includes, until you are at the top.

Then you have cleaned up the internal headers.

Next do the external headers.

------------------------------

Repeat until done.

(5)
Next. Comment out all the external headers.
Compile the top level, commenting in headers
until they compile. Then do 5 for all the headers
in the next lower level until You reach the bottom.

----------------
Like I said you can write a script to do the whole thing,
which could take a while. Or you can write a script which
does parts ( in a short while ), and then so the rest by hand.

--------------------------------------------------
Thaddeus L. Olczyk, PhD
Think twice, code once.

 
 
 

How to scan C files to find the required include files ?

Post by CBFalcone » Wed, 04 Jun 2003 14:30:28




> > Legacy code has a lot of cross linking. Many source files
> > include unnecessary include files. I would like to clean this
> > up to improve maintainability.

> > Is someone aware of a tool which would identify the required
> > include files for c source files. Stepping through the code
> > and identifying the required files is not an option.

> Not possible in general, unless the tool can execute the compiled
> program and verify that it is performing correctly. This is
> impractical, and (I think--my computer science is rusty) even
> theoretically impossible.

... snip ...

> So, I would never mess with includes in a mechanical way. Just
> because the program compiles and links doesn't mean it's right.

It is extremely easy to compile the original source, save the
object file, and then repeat the process after stripping the
unneeded headers.  If the two object files compare equal with a
binary file compare, all is well.  You may need to set some
compile options to avoid embedding line numbers, dates, times,
etc.  You can put the whole verification process into your make
file.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!

 
 
 

How to scan C files to find the required include files ?

Post by CBFalcone » Wed, 04 Jun 2003 14:30:30




> wrote in comp.lang.c:

> > Hi,

> > Legacy code has a lot of cross linking. Many source files include
> > unnecessary include files. I would like to clean this up to improve
> > maintainability.

> > Is someone aware of a tool which would identify the required include
> > files for c source files. Stepping through the code and identifying
> > the required files is not an option.

> > Thanks in advance.
> > Gautam

> PCLint claims to be able to do this, and while I have not tested this
> specific feature it generally lives up to its claims.  It is neither
> free nor open source, and I don't think it's available for any
> platform other than MS-DOS or Win32.

I believe they have a shrouded source version available for Unix
etc.  Expensive.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!

 
 
 

How to scan C files to find the required include files ?

Post by Richard Heathfiel » Wed, 04 Jun 2003 16:14:07



> [your "followups to" is ignored because this topic is
> appropriate in all listed groups and is not unique to clc.]

Sorry about that. My newsreader did it sneakily while I wasn't looking. This
time, I've kicked it hard.



<snip>

Quote:

>>Remove all headers.

> I would recommend doing this slightly in a different way.

> In a good editor (vi or emacs are examples) where one can invoke
> the compiler without exiting, etc. etc.

> Remove each #include directive by commenting it out or using
> conditionals, _one_ _at_ _a_ _time_.

>   Remove a header, compile.
>   Remove a header, compile: errors; replace header.
>   Remove a header, compile.

> And on and on until finished.

Well, I think that's an excellent process improvement.

Quote:

> For those of us less instantly clueful about which header
> a given macro/declaration/ or whatever is in, the uncertainty
> of which header to add back in is greatly reduced when only
> one possibility exists.

> Richard probably _can_ remove a dozen includes, compile and
> realize from one glance which six need to be there and which
> six don't.

Sadly, this is probably truer than it ought to be.

Quote:> Most of us probably can get 3/4s of them right
> the first time...

> But if it is done one header at a time, it's 100% every time.
> Hence most programmers can instantly spot the need for several
> headers (stdlib.h, stdio.h, string.h, etc. etc.), so actually
> a modified approach half way between one at a time and all at
> at time is probably most productive.  Whack out all the common
> ones and see if the errors apply.  Then do the odd ones one
> at a time.

If you had to do this on a regular basis, I'd also recommend setting up some
kind of dictionary of dependencies between headers and the symbols they
define, to take the donkey-work out of it.

<snip>

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

 
 
 

How to scan C files to find the required include files ?

Post by Bill Godfr » Wed, 04 Jun 2003 17:54:31



> Fix by inserting headers for the functions, types, macros etc that it
> complains it can't find.

Make sure your compiler is configured to generate these warnings first.

Bill, Wall

 
 
 

How to scan C files to find the required include files ?

Post by Floyd Davidso » Wed, 04 Jun 2003 19:06:27




>> [your "followups to" is ignored because this topic is
>> appropriate in all listed groups and is not unique to clc.]

>Sorry about that. My newsreader did it sneakily while I wasn't looking. This
>time, I've kicked it hard.

Good.  

And everything else you said was good too, as always.

...

>--

>"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.

Damned, are you _still_ picking on that poor guy for just one
time only posting an off topic article to clc.  What a bunch of
pedants...

--
Floyd L. Davidson           <http://web.newsguy.com/floyd_davidson>

 
 
 

How to scan C files to find the required include files ?

Post by stephan bea » Wed, 04 Jun 2003 19:55:34



> Legacy code has a lot of cross linking. Many source files include
> unnecessary include files. I would like to clean this up to improve
> maintainability.

> Is someone aware of a tool which would identify the required include
> files for c source files. Stepping through the code and identifying
> the required files is not an option.

Use the dependency generation feature of your compiler (gcc -M, for
example).

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

 
 
 

How to scan C files to find the required include files ?

Post by CBFalcone » Wed, 04 Jun 2003 20:31:48



... snip ...

> If you had to do this on a regular basis, I'd also recommend
> setting up some kind of dictionary of dependencies between
> headers and the symbols they define, to take the donkey-work
> out of it.

Er, "grep -n %1 *.h" or equivalent should do in a pinch.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!

 
 
 

1. Include files can't find sys/*.h files.

I just installed linux slackware 3.0 off
the CD and I tried to compile a few programs
that came with the CD.  They inclue files
which try to include files from sys/*.h (e.g.
sys/errno.h) which the compiler says it can't
find.
How can I remedy this?

Thanks in advance,
Mike

2. Limiting Access in Openwindows

3. UNIX include files = linux include files?

4. Processor Speed?

5. - Two questions: find files with specific permission, find files that belong to..

6. Connecting to MS Mail Postoffice

7. GCC /G++ Problems finding include files

8. LPD and Solaris 2.4

9. g++ doesn't find include files

10. How to find a word in all files, including subdir?

11. xlC can't find include files.

12. cannot find include file: <singal.h>

13. Can't Find X11R6 include files.