I'm refactoring some code in C++, and I want to deprecate some old methods. My current method for finding all of the methods looks like this:
This totally sucks. I've also tried grepping source for the name of the function calls, but I sometimes run into problems with functions of the same name with different arguments, so my compilation makes the C++ compiler resolve the names for me. I've found this question for C#, but my code base is entirely implemented in C++.
Is there a better way to find all of the callers of a class method or function in C++? I'm using GCC on Unix systems, but cross-platform solutions would be superlative.
GCC allows you to decorate variables, functions, and methods with __attribute__((deprecated))
, which will cause a warning on all callsites (unless -Wno-deprecated-declarations
is given).
class A {
public:
A() __attribute__((deprecated)) {}
};
int main() {
A a;
}
$ g++ test.c test.cc: In function ‘int main()’: test.cc:6: warning: ‘A::A()’ is deprecated (declared at test.cc:3)
Eclipse can do this without any plugins. It can be a useful tool for stuff like this even if you don't want to use it for your day-to-day editor.
You can also use Eclipse's built-in refactoring support to rename overloaded functions so that they're no longer overloaded. Eclipse is also fully cross-platform; you can use features like its indexer, search references, and refactoring even for projects that are maintained and built in other IDEs.
One option is to run your entire project through a program like Cscope . Which essentially parses the entire source code of your project and builds a database that allows for easy searching of things like all of a function's callers, all references, etc.
I know it works fairly well for C, and they claim it works decently for C++. KScope is a KDE GUI front end for it.
I think what you want is to look at a call graph. See this question for some good suggestions on how to do that.
For anyone wanting to do this in Visual Studio under windows....
Visual Assist is really good. It helps a lot with many C++ refactorings, Call graphs, renaming, automatically creating header definitions or generation methods from definitions, etc etc. It's not perfect, nothing like the quality of Java/C# refactoring tools, but really good for C++!!
这个Eclipse插件可能是正确的工具: CallGraph View
That's pretty much how I do it. If your code is all in once place a "grep" (or find | grep combo) might do the trick too.
One caviat: This method will very often miss instances that are not conditionally compiled into your current configuration. If your code makes use of #ifdefs to support multiple configurations, a thorough person will try to compile with every configuration to catch all instances.
If you let the Eclipse CDT index your source base, then you should be able to view the calling hierarchy for any function and even perform some refactoring.
You can download the Eclipse IDE for C/C++ Developers (hopefully you can find a port for your particular *NIX) and follow their instructions to get going.
In visual studio, right click on the function name and click "Call Browser" > Show Callers graph
Then in the display window there should be a folder called "Calls to function" that contains all the places that call that function, each of which you can double click on to go to them in the code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.