C++ LLVM—如何逐个函数获取函数';真实姓名

C++ LLVM—如何逐个函数获取函数';真实姓名,c++,clang,llvm,C++,Clang,Llvm,最近,我使用llvm在llvm-IR中插入call指令。问题是,如果我有一个名为add的函数,我无法使用GetFunction(string)找到它,因为IR中的add()可能\u Z3addv\u。我知道IR中的所有功能都有一个新名称,但我不知道新名称到底是什么 Module *m = f->getParent(); IRBuilder<> builder(m->getContext()); Function *call = m->getFunctio

最近,我使用llvm在llvm-IR中插入call指令。问题是,如果我有一个名为
add
的函数,我无法使用GetFunction(string)找到它,因为IR中的add()可能
\u Z3addv\u
。我知道IR中的所有功能都有一个新名称,但我不知道新名称到底是什么

  Module *m = f->getParent();
  IRBuilder<> builder(m->getContext());
  Function *call = m->getFunction("add");
  // call is NULL.
  std::vector<Value *> args;
  ......



Module *m = f->getParent();
IRBuilder<> builder(m->getContext());
Function *call = m->getFunction("_Z3addv");
// call is not NULL.
std::vector<Value *> args;
......
Module*m=f->getParent();
IRBuilder(m->getContext());
函数*call=m->getFunction(“添加”);
//调用为空。
std::向量args;
......
模块*m=f->getParent();
IRBuilder(m->getContext());
函数*call=m->getFunction(“Z3addv”);
//调用不为空。
std::向量args;
......

如何使用其原始名称查找函数?

您可以从
LLVMCore
重新使用
Mangler

下面是一个用法示例:

std::string mangledName;
raw_string_ostream mangledNameStream(mangledName);
Mangler::getNameWithPrefix(mangledNameStream, "add", m->getDataLayout());
// now mangledName contains, well, mangled name :)

libstdc++有一个很好的Demanling库,只需包含
cxxabi.h
然后您可以更改
Function*call=m->getFunction(“Z3addv”)

int状态;

Function*call=m->getFunction(abi::ucxa_demangle(“Z3addv”)、nullptr、nullptr和status)

C++名字的缩写是用CLAN来完成的,所以看看CLAN的API。这就是我想要的!!!注意:应该在3行之后添加
mangledNameStream.flush()
,以刷新缓冲区,否则mangledname可能为空。我刚刚尝试了这个方法,但似乎不起作用
mangledName
包含未混合的名称。与@executor21相同