我解決了這個問題,但我不確定它是否是一個正確的方法。 而不是調用函數 - 「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;
...
不管怎麼說,現在工作爲我的代碼。不確定是否還有其他好的方法。