Find out if a function is called within a C++ project?

后端 未结 8 544
失恋的感觉
失恋的感觉 2021-02-01 19:15

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 ha

相关标签:
8条回答
  • 2021-02-01 19:56

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

    0 讨论(0)
  • 2021-02-01 19:56

    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.

    0 讨论(0)
  • 2021-02-01 20:03

    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

    0 讨论(0)
  • 2021-02-01 20:06

    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!

    0 讨论(0)
  • 2021-02-01 20:07

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

    0 讨论(0)
  • 2021-02-01 20:13

    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.

    0 讨论(0)
提交回复
热议问题