Find out if a function is called within a C++ proj

2020-05-20 05:47发布

I'm trying to remove functions that are not used from a C++ project. Over time it's become bloated and I'm looking to remove functions that aren't used at all.

I have all the projects in a solution file in Visual Studio, but I use cmake so I can generate project files for another IDE if necessary (which is why this isn't tagged with visual-studio).

Does something like this exist? Where it'll analyze the source and tell me which functions are not called. I saw PC-Lint mentioned in a few questions here, but that doesn't seem to do this.

What I really want to do is call "Find all references" on each function and remove the functions not called, but doing this manually would take much too long.

标签: c++
8条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-05-20 06:22

I'm pretty sure that mathematically, this can't be done in the general case. If you allow for recursion and function pointers (or first class functions) then you end up in a pretty simple reduction to the Halting Problem.

Granted, this be a case that you never have to deal with, but you should know abut it...

查看更多
The star\"
3楼-- · 2020-05-20 06:25

Visual Studio can generate call graphs, showing 'called-by' for each function. Doxygen will do the same if you don't want to use Visual Studio.

However both these methods will fail to detect a function called through a pointer, but that should normally be easy to check manually.

查看更多
狗以群分
4楼-- · 2020-05-20 06:26

If your code is simple enough static analysis might work. However C++ is very context-sensitive :/. So I personally would not even try to look for a tool in the area. At least not until CLANG is fully compliant with C++ :D

I hope you have unit-tests, I would get visual studio to compile code which generates a runtime profile and then farm the function names's (with a scripting language) from the generated profile. If you have covered all of the use-cases (either manually or with unit-tests) in your application you should be able to identify the least used (or never-used) functions. Then you can use the mark-one eyeball to trim down the source-base.

There is nothing like doing It manually though :D

查看更多
在下西门庆
5楼-- · 2020-05-20 06:36

Use __declspec(deprecated) in front of the function declaration you want to get rid of. That will throw up compile warnings if that function is actually used at compile time.

查看更多
戒情不戒烟
6楼-- · 2020-05-20 06:38

I suppose the easiest way is to remove a function (or class, variable, anything else you might think is unneeded) and then see if it compiles. If the function is used you will get a compile or link error at some point during the rebuild.

Generally you should remove the definition rather than the declaration, as otherwise with things like overloaded functions and specialised templates it may compile and link to one of the others, not causing an error but changing the programs behaviour. By removing the definition, the compiler still sees the declaration, but the linker will fail to link it.

Items that a declared but not defined wont cause an error if their not used, since the linker will never try to link to them.

查看更多
闹够了就滚
7楼-- · 2020-05-20 06:40

Sounds like you need a code coverage tool. There's a list of them in this wikipedia article.

查看更多
登录 后发表回答