20

I had to customize some projects which have been written for some other purpose but some core functionality is same for my project and works as it is. But There are lots of variables, macros,functions etc.. which are not useful for my current context and they are just making the code very uneasy to read and unnecessarily big.

So I started removing the variables macros functions etc.. by using "Find References" and "Show Call Graph" in Netbeans. I'm using netbeans remote development tools for c/c++. But its cumbersome. So Is there any tool to do this clean up??

Sreekar
  • 995
  • 1
  • 10
  • 22
  • 1
    Maybe the [clang static analyzer](http://clang-analyzer.llvm.org/) for semantic analysis? –  May 14 '13 at 08:38
  • Actually, clang warns on unused variables and functions even during normal compilation. – ltjax May 14 '13 at 09:27
  • 1
    This is harder than it seems. How do you know if an #include is unused? I mean you can have something like a #define that maps `malloc()` onto `malloc_dbg()` in there and then it compiles both with and without including that file, just the behavior changes. – sharptooth May 14 '13 at 09:27

3 Answers3

13

From what I know there is currently no tool that does all the things you have mentioned, however there is one that helps in cleaning up the unused include headers: include-what-you-use

"Include what you use" means this: for every symbol (type, function variable, or macro) that you use in foo.cc, either foo.cc or foo.h should #include a .h file that exports the declaration of that symbol. The include-what-you-use tool is a program that can be built with the clang libraries in order to analyze #includes of source files to find include-what-you-use violations, and suggest fixes for them.

The main goal of include-what-you-use is to remove superfluous #includes. It does this both by figuring out what #includes are not actually needed for this file (for both .cc and .h files), and replacing #includes with forward-declares when possible.

One might expect that the Clang static analyzer would do this, but from what I see the availalbe checks do not offer such things.

This might be a good time for someone to suggest a feature request to the analyzer or create a separate tool using LibTooling on a similar par with the tools described at Clang Tools

In the meantime, I'd suggest you enable -Wall and -Wextra compiler flags, which will trigger the following warnings (among others)(see the GCC docs below):

  • -Wunused-function
  • -Wunused-label
  • -Wunused-value
  • -Wunused-variable
  • -Wunused-parameter
  • -Wunused-but-set-parameter

If for some reason you don't want to do that, you could just add -Wunused which will enable only the above -Wunused options combined, without the other flags that -Wall or -Wextra adds.

But in order to get a warning about an unused function parameter, you must either specify -Wextra -Wunused (note that -Wall implies -Wunused), or separately specify -Wunused-parameter.

Of course, this means that you have to do the cleanup manually

If you want to be extra pedantic you might as well convert all the warnings into errors by adding the -pedantic-errors flag

For more details read the GCC Warnings Options documentation.

Alex Bitek
  • 6,529
  • 5
  • 47
  • 77
3

I've sometimes used the method of marking a big block of code as "not used" by adding

#if 0
  ... lots of code 
#endif

You can then compile the code and see what goes wrong. Analyze the "undeclared varibale X" errors you get and reinstate the bits necessary for that. You can do this by either "cutting up" the #if 0 block (adding an #endif, then a new #if 0 a bit further down), or by moving the pieces you need out of the current block.

For example, if you have a block of global variables or macros, just put #if 0 around all of them, and see which ones are actually used. [Although macros can be a little more tricky if they are used in #ifdef and such].

I'd be surprised if there isn't a tool out there, but at the same time, you still have to do the cutting job, and once you have a large chunk of code in #if 0 ... #endif, it's pretty easy to cut it out.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • I can do it manually and I'm doing it currently. But I was asking about the tool to automatically analyze source and show me the things to remove. Eg: In eclipse or netbeans for Java, IDE shows unused variable or unused import etc.. – Sreekar May 14 '13 at 08:50
  • the `#if 0` is not very safe due to name lookup -- e.g. if a header were erased using `#if 0`, the compiler may simply use a function (which is called) where that function is also declared in an outer scope. since most people tend not to qualify scopes explicitly, the approach has potential to introduce bugs related to name lookup without warning. – justin May 14 '13 at 09:42
0

Many static code analysis tools provide the information you want. Wikipedia has a list. We have successfully used such a tool (with some custom changes) to remove includes and speed up compile time.

Sarien
  • 6,647
  • 6
  • 35
  • 55