I'm writing a program in LibTooling to print information of all functions, including methods in classes. But it seems VisitFunctionDecl() only detects functions outside a class. I also tried VisiCXXMethodDecl(),
bool VisiCXXMethodDecl(CXXMethodDecl *node) {
string return_type = node->getReturnType().getAsString();
string function_name = node->getNameInfo().getAsString();
cout << "function name: " << function_name << endl;
cout << "return type: " << return_type << endl;
cout << "has body: " << node->hasBody() << endl;
}
but still failed to detect them. Can anyone tell me what's the correct way to detect methods defined in classes?
Quick fix
In your question, you have a typo in the name of the visitor method, which should be
VisitCXXMethodDecl, notVisiCXXMethodDecl(missing thetinVisit). That may be all that needs fixing, but I can't tell without seeing the rest of the code.Note that the compiler will not warn about that sort of typo because
RecursiveASTVisitordoes not use virtual methods to customize its action (if it did, and you used theoverridespecifier, then the typo would result in an error). Instead, it uses template metaprogramming and static overriding, and in that context, a typo'd method name just looks like some unrelated method.Complete example
Here is a complete program that prints the names and locations of every C++ method declaration (including definitions) in a translation unit.
method-decls.cc:Makefile(based on the one at this answer of mine, but with a few further refinements):test.cc:Example invocation: