Is there any tool / way to detect/remove all unused variables,macros,headers(includes) and functions from c++ code?

StackOverflow https://stackoverflow.com/questions/16538479

  •  29-05-2022
  •  | 
  •  

Question

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??

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top