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条回答
Summer. ? 凉城
2楼-- · 2020-05-20 06:43

If you want to know, dynamically, which functions are being used you could get the (vc++) compiler to insert callcap hooks and then use those to dump out usage information.

This could be a useful compliment to static analysis based approaches, since it will see every piece of code that is entered during execution (regardless of how execution arrives there).

See http://msdn.microsoft.com/en-us/library/ms254291(VS.80).aspx for info on call profile hooks in visual studio.

查看更多
Luminary・发光体
3楼-- · 2020-05-20 06:46

The excellent (and free) Source Monitor static analysis tool, from http://www.campwoodsw.com/ can give you counts of the number of calls to a method, which I think is what you want.

Edit: Seems to be my evening for screwing up. The calls metric does not in fact do what I thought it did. Still, SM is an excellent tool so I hope that bringing it to people's attention has done some good!

查看更多
登录 后发表回答