我解决了这个问题,但我不确定这是否是正确的方法。我没有调用函数 - “viewGraph()”,而是使用“llvm::WriteGraph”。
这是代码:
string outputPath = "./";
outputPath.append("CallGraph");
outputPath.append(".dot");
// Write .dot
std::error_code EC;
raw_fd_ostream O(outputPath, EC, sys::fs::F_RW);
if (EC) {
llvm::errs() << "Error: " << EC.message() << "\n";
return;
}
llvm::WriteGraph(O, &mCG);
同时,我更改了 LLVM 源代码文件——GraphWriter.h
void writeNode(NodeRef Node) {
std::string NodeAttributes = DTraits.getNodeAttributes(Node, G);
O << "\tNode" << static_cast<const void*>(Node) << " [shape=record,";
if (!NodeAttributes.empty()) O << NodeAttributes << ",";
O << "label=\"{";
if (!DTraits.renderGraphFromBottomUp()) {
// I add here: for show the node's label value (otherwise the label will be empty)
std::string nodeLable ;
if(Node == G->getRoot()){
nodeLable = "root";
}
else{
const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl());
nodeLable = ND->getNameAsString();
}
// O << DOT::EscapeString(DTraits.getNodeLabel(Node, G));
O << nodeLable;
...
无论如何,它现在适用于我的代码。不知道有没有其他好的方法。