Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 251
Sreekar Avatar asked May 14 '13 08:05

Sreekar


People also ask

How do I get rid of the unused variable warning?

Solution: If variable <variable_name> or function <function_name> is not used, it can be removed. If it is only used sometimes, you can use __attribute__((unused)) . This attribute suppresses these warnings.

Does compiler remove unused variables?

If either the compiler or the linker can see that there are no references to C functions or C variables, they can (and usually do) remove those unused things.

How do I remove unused variables in Visual Studio 2019?

In the first preview build of Visual Studio 2019 version 16.10, Microsoft adds a Remove Unused References command. It can be invoked by right-clicking on the project name or the dependencies node in Solution Explorer and select the " Remove Unused References... " command from the menu.

Do unused variables take up memory?

Does an unused member variable take up memory? No (if it is "really" unused).


1 Answers

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.

like image 196
Alex Bitek Avatar answered Sep 23 '22 22:09

Alex Bitek