Search code examples
llvmllvm-ircall-graph

llvm : CallGraphNode giving incorrect function Name


I am trying to traverse the call graph in llvm. Once I get a node in the call graph, I am trying to print the function name corresponding to that call graph node as well as the number of references.

Results: 1)The function name is always an empty string. 2)The number of references is always a random number. 3)Also the called functions names are also an empty string.

Code: bool runOnModule(llvm::Module &M) override {

    CallGraph cg = CallGraph(M);
    cg.dump();// this is correct. It is printing the expected the call graph

    for ( CallGraph::const_iterator itr = cg.begin(), ie = cg.end() ; itr != ie; itr++)
    {
       if (itr->second != nullptr)
       {
            itr->second->dump();
            errs()<<"-----------CGN---------\n";
            CallGraphNode *cgn = itr->second.get();



            if(const Function* fptr = cgn->getFunction())
            {
                errs()<<"Number of references are"<<cgn->getNumReferences()<<"\n";

                errs()<<fptr->getName()<<"\n";


                if(cgn->operator[](0) != nullptr)
                {

                    if(cgn->operator[](0)->getFunction() != nullptr)
                    {
                        errs()<<cgn->operator[](0)->getFunction()->getName()<<"\n";
                    }

                 }
              }
         }
   }

}


Solution

  • try adding -DNDEBUG compilation flag. I had the same problem and it helped. If you are using cmake, you can add following line to CMakeLists file:

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG")