2
votes


i have an object callInst.How i can take the real name of function and not the name which have in IR code ? if i run this code in my pass(which Useless post in another question)

StringRef get_function_name(CallInst *call)
{
Function *fun = call->getCalledFunction();
if (fun) 
    return call->getName(); 
else
    return StringRef("indirect call");
}    

this give me the name of IR code(for example call,call1,call2).I would like to have the real name of callInst (printf,foo,main).

Any idea?

Thanks a lot

3

3 Answers

3
votes

What you get it the mangled name for the function. You have to demangle it to get the "real" name back. I assume you are working on linux and generate your IR with clang (since you have the clang tag on your question). On linux you can use

#include <iostream>
#include <memory>
#include <string>
#include <cxxabi.h>
using namespace std;

inline std::string demangle(const char* name) 
{
        int status = -1; 

        std::unique_ptr<char, void(*)(void*)> res { abi::__cxa_demangle(name, NULL, NULL, &status), std::free };
        return (status == 0) ? res.get() : std::string(name);
}

int main() {
    cout << demangle("mangled name here");
    return 0;
}

to demangle the name of the function.

1
votes

A complement to @Michael Haidl's answer, boost provides a better representation for the demangled names by constructing boost::typeindex::type_id_with_cvr.

type_index ti = type_id_with_cvr<int&>();
std::cout << ti.pretty_name();  // Outputs 'int&' 

See tutorial for details.

But keep in mind that this is only used for debugging purpose since LLVM IR names are not guaranteed to be the same as the font-end source code, and some internal function names are even allowed to be stripped.

1
votes

it was more simple than the logic of demangled etc.I was just print the name of callinst and not the value of real calledfunction.

StringRef get_function_name(CallInst *call)
{
Function *fun = call->getCalledFunction();
if (fun) 
    return fun->getName(); //here i would take fun and not call! 
else
    return StringRef("indirect call");
}    

and all its ok now!with

errs()<<fun->getName().str()<<"\n";

i will take the real names! thnx for respond...i hope i help others with the same problem...