I am using Clang to parse some C++ code. I would like to print the name and mangled name for every FunctionDecl
that I encounter.
I can print the function name fairly easily by adding this to my RecursiveASTVisitor
:
bool VisitFunctionDecl(FunctionDecl* f) {
auto declName = f->getNameInfo().getName();
auto functionName = declName.getAsString();
std::cout << functionName << std::endl;
return true;
}
How can I also print the mangled name?
Working code I produced after following Sebastian's pointers:
const auto getMangledName = [&](FunctionDecl* decl) {
auto mangleContext = context.createMangleContext();
if (!mangleContext->shouldMangleDeclName(decl)) {
return decl->getNameInfo().getName().getAsString();
}
std::string mangledName;
llvm::raw_string_ostream ostream(mangledName);
mangleContext->mangleName(decl, ostream);
ostream.flush();
delete mangleContext;
return mangledName;
};