473,882 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

automatically remove unused #includes from C source?

Hi All,

Does anyone know of a tool that can automatically analyze C source to
remove unused #includes?

Thanks,
Sean

Oct 20 '06
31 8639
On Fri, 20 Oct 2006 21:42:07 +0000 (UTC), Walter Roberson wrote:
>Conformance tests can take 3 days per build, and if you
are checking whether a project with 1500 #includes (distributed
over the source) can survive deleting one particular include
out of one particular module, then you need up to pow(2,1500)
complete builds and conformance tests. Even if each *complete*
application conformance test took only 1 second, it'd take
10^444 CPU years to complete the testing.
That calculaton is quite contrived. I wonder how you would do changes
in you code base besides removing an #include, not to speak of
refactoring.
>*Much* faster to break
it into chunks (e.g., by source file) and check to see whether
each chunk still produces the same code after removal of a
particular include:
.... and if it compiles but produces different object code then you
have found an include order dependency bug ;-)

Best regards,
Roland Pibinger
Oct 21 '06 #11
On Fri, 20 Oct 2006 21:48:43 -0700, Walter Bright
<wa****@digital mars-nospamm.comwrot e:
>Otherwise, a customized C compiler could absolutely tell if there were
any dependencies on a particular #include file.
BTW, there is a huge demand for static code analysis tools in C and
C++ (also in a commercial sense). For most of those code analysis
tasks you need to have a fully-fledged (customized) compiler. So, if I
had that compiler ...

Best regards,
Roland Pibinger
Oct 21 '06 #12
Roland Pibinger wrote:
BTW, there is a huge demand for static code analysis tools in C and
C++ (also in a commercial sense). For most of those code analysis
tasks you need to have a fully-fledged (customized) compiler. So, if I
had that compiler ...
True, I've seen some amazingly high prices quoted for static code
analysis. There's nothing stopping someone from approaching Digital Mars
or other compiler vendors and offering to purchase a license for the
compiler to get into that business.
Oct 21 '06 #13
On Sat, 21 Oct 2006 01:11:22 -0700, Walter Bright
<wa****@digital mars-nospamm.comwrot e:
>True, I've seen some amazingly high prices quoted for static code
analysis. There's nothing stopping someone from approaching Digital Mars
or other compiler vendors and offering to purchase a license for the
compiler to get into that business.
What is stopping you?
Oct 21 '06 #14
On Sat, 21 Oct 2006 06:09:05 GMT, Neil <Ne*******@worl dnet.att.net>
wrote:
>sm*********@gm ail.com wrote:
>Hi All,

Does anyone know of a tool that can automatically analyze C source to
remove unused #includes?

Thanks,
Sean

doesn't PC-LINT give you a list of unused includes?
Yes.

--
jay
Oct 21 '06 #15
Roland Pibinger wrote:
On Fri, 20 Oct 2006 21:48:43 -0700, Walter Bright
<wa****@digital mars-nospamm.comwrot e:
>>Otherwise, a customized C compiler could absolutely tell if there were
any dependencies on a particular #include file.


BTW, there is a huge demand for static code analysis tools in C and
C++ (also in a commercial sense). For most of those code analysis
tasks you need to have a fully-fledged (customized) compiler. So, if I
had that compiler ...
Due to extensions, such an tool can only realy be part of the compiler
suite.

--
Ian Collins.
Oct 21 '06 #16
Roland Pibinger said:
On Sat, 21 Oct 2006 01:11:22 -0700, Walter Bright
<wa****@digital mars-nospamm.comwrot e:
>>True, I've seen some amazingly high prices quoted for static code
analysis. There's nothing stopping someone from approaching Digital Mars
or other compiler vendors and offering to purchase a license for the
compiler to get into that business.

What is stopping you?
I don't think Walter Bright needs to approach /anyone/ to purchase a licence
for the Digital Mars compiler. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 21 '06 #17
Walter Bright wrote:
Walter Roberson wrote:
>Conformance tests can take 3 days per build, and if you
are checking whether a project with 1500 #includes (distributed
over the source) can survive deleting one particular include
out of one particular module, then you need up to pow(2,1500)
complete builds and conformance tests. Even if each *complete*
application conformance test took only 1 second, it'd take
10^444 CPU years to complete the testing. *Much* faster to break
it into chunks (e.g., by source file) and check to see whether
each chunk still produces the same code after removal of a
particular include: the timing then becomes proportional to
the sum of pow(2,includes_ in_this_chunk) instead of the product
of those as would be the case with what you propose.

That is a good idea: selectively removing #include statements, and
then simply seeing if the resulting object code file changes.

Otherwise, a customized C compiler could absolutely tell if there
were any dependencies on a particular #include file.
Such an operation would need C99 specs, otherwise the use of
implied int would foul the results. It might be enough to tell the
compiler to insist on prototypes.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Oct 21 '06 #18
In article <45************ *@news.utanet.a t>,
Roland Pibinger <rp*****@yahoo. comwrote:
>On Fri, 20 Oct 2006 21:42:07 +0000 (UTC), Walter Roberson wrote:
>>Conformance tests can take 3 days per build, and if you
are checking whether a project with 1500 #includes (distributed
over the source) can survive deleting one particular include
out of one particular module, then you need up to pow(2,1500)
complete builds and conformance tests. Even if each *complete*
application conformance test took only 1 second, it'd take
10^444 CPU years to complete the testing.
>That calculaton is quite contrived.
Contrived? Well, yes, in the sense that any large project is likely
to have much *more* than 1500 #include statements. For example, I just
ran a count against the trn4 source (which is less than 1 megabit
when gzip'd), and it has 1659 #include statements. openssl 0.9.7e
has 4679 #include statements (it's about 3 megabytes gzip'd).
>I wonder how you would do changes
in you code base besides removing an #include, not to speak of
refactoring.
You seem to have forgotten that you yourself proposed,
"Automate that and you have the requested tool!" in response to my
saying, "It might be easier just to start commenting out #include's".
When I indicated that it is more complex than that and that
comparing object code is necessary (not just looking to see if
looking for compile errors), you said,
"You have to test your application after code changes anyway."

Taken in context, your remark about testing after code changes
must be considered to apply to the *automated* tool you proposed.
And the difficulty with automated tools along these lines is that they
are necessarily dumb: if removing #include file1.h gives you a
compile error, then the tool cannot assume that file1.h is a -necessary-
dependancy (i.e., a tool that could test in linear time): the tool
would have to assume the possibility that removing file1.h
only gave an error because of something in file2.h --- and yes,
there can be backwards dependancies, in which file1.h is needed to
complete something included -before- that point. Thus, in this kind
of automated tool that doesn't know how to parse the C code itself,
full dependancy checking can only be done by trying every -possible-
combination of #include files, which is a 2^N process.

Do you feel that 1 second to "test your application after code changes"
is significantly longer than is realistic? It probably takes longer
than that just to compile and link the source each time.

>I wonder how you would do changes
in you code base besides removing an #include, not to speak of
refactoring.
I don't mechanically automate the code change and test process.

>>*Much* faster to break
it into chunks (e.g., by source file) and check to see whether
each chunk still produces the same code after removal of a
particular include:
>... and if it compiles but produces different object code then you
have found an include order dependency bug ;-)
Include order dependencies are not bugs unless the prevailing
development paradigm for the project has declared them to be so.

Once you get beyond standard C into POSIX or system dependancies,
it is *common* for #include files to be documented as being order
dependant upon something else. Better system developers hide
that by #include'ing the dependancies and ensuring that, as far as
is reasonable, that each system include file has guards against
multiple inclusion, but that's a matter of Quality of Implementation,
not part of the standards.
Still, it is true that in the case of multiple source files that
together have 1500 #include, that you would not need to do pow(2,1500)
application tests, if you are using a compiler that supports
independant compilation and later linking. If you do have independant
compilation, then within each source file it is a 2^N process
to find all the #include combinations that will compile, but most of
the combinations will not. Only the versions that will compile need
to go into the pool for experimental linkage; linkage experiments
would be the product of the number of eligable compilations for each
source. Only the linkages that survived would need to go on for testing.
The number of cases that wil make it to testing is not possible to
estimate without statistical information about the probability that any
given #include might turn out to be unneeded.

--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Oct 21 '06 #19
Roland Pibinger wrote:
On Sat, 21 Oct 2006 01:11:22 -0700, Walter Bright
<wa****@digital mars-nospamm.comwrot e:
>True, I've seen some amazingly high prices quoted for static code
analysis. There's nothing stopping someone from approaching Digital Mars
or other compiler vendors and offering to purchase a license for the
compiler to get into that business.

What is stopping you?
I'm pretty overloaded already.
Oct 21 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2302
by: Sam Smith | last post by:
Hi, I'm thinking about whether or not to build a general library on its own and later link that lib with the main executable, or to build the main executable with a subset of the relevant source files from that library. If I choose to build the executable directly with only relevant source files, I know I do not bloat the executable with symbols and functions that are never used.
35
1254
by: Geronimo W. Christ Esq | last post by:
Are there any scripts or tools out there that could look recursively through a group of C/C++ source files, and allow unreferenced function calls or values to be easily identified ? LXR is handy for indexing source code, and for a given function or global variable it can show you all the places where it is referenced. It would be really nice to have a tool that would simply list all of the referenced functions, so that you could go...
8
2245
by: netsurfer | last post by:
Hi: Have a question on making the date automatically filled in by what the user enters in by the date at the top. The date entered at the top would most likely be on a Wednesday then I need to have all the prior dates pop in. Example: User enters 2/9/05 in the date field at the top being a Wednesday...I need the dates at the bottom to be filled in automatically, respectively as 2/3/05, 2/4/05, 2/5/05, 2/6/05, 2/7/05, 2/8/05, 2/9/05...
6
7002
by: Jozef | last post by:
Hello, Is there any way to automatically create an ODBC data source if it doesn't already exist? I'd like to deploy this program on workstations that I may not have access to, and don't want to rely on the user for any technical expertise. Is there any way to do that? Thanks!
3
3073
by: Apollo440 | last post by:
How to remove unused using directive? eg) using System; using System.Windows.Forms; <- i want to remove this unused using directive namespace foo { :
4
7138
by: Maxim | last post by:
Greetings, In my C# project, I'm using a third-party Opensource library... in source code form. I mean no assemblies, just pure source code. It allows me to add new application-specific functionality to that library, finetune it, and see how things work. Well, the problem is that the library is quite huge, so the application's size gets not really acceptable - I'm developping for an
2
4017
by: AussieRules | last post by:
Hi, There use to be some 3rd party tools that would remove unused subs and function and unused code from vb projects. Is there are a way to do this in vb.net 03
3
7200
by: Tom McL. | last post by:
Is there a way I identify and remove unused variable within vb2005 standard edition
100
5169
by: jacob navia | last post by:
Recently, a heated debate started because of poor mr heathfield was unable to compile a program with // comments. Here is a utility for him, so that he can (at last) compile my programs :-) More seriously, this code takes 560 bytes. Amazing isn't it? C is very ompact, you can do great things in a few bytes. Obviously I have avoided here, in consideration for his pedantic
0
10725
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10830
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10403
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7956
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7113
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5978
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.