开发者

How to remove non-used variables and functions

I started to work with a C++ code from a previous programmer. I know that there is a lot of functions that are not called and variables which are not used and/or initializ开发者_如何学编程ed. I wonder if there are some tools that can automatically "clean" code and remove all this non-used code. I am talking about source code and not about final build output. I would prefer open source solutions.


Many compilers/linkers prune your code for you to a certain extent, like Visual C++'s /OPT:REF switch.


You might be able to get something useful out of running CPPCheck which performs static analysis of your code. Treating all errors as warnings will also help identify which variables are unused as your code will not compile until they are resolved.

The other thing you could do is create a set of unit tests to cover the expected functionality and run it through a coverage tool (such as gcov if you're using GCC), this will show which lines of code are executed and how many times.


This is the compiler/linker job to remove dead code from your binary. Compile your source code with gcc flags -fdata-sections and -ffunction-sections then link your binary with the flag –gc-sections.

I think you can also produce warnings at compilation time for static unused variables.

Finally you can use gcov or equivalent tool for code coverage

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜