2017-07-14 73 views
0

我是clang libtooling的初學者。 我正在嘗試使用clang :: CallGraph viewGraph生成我的調用圖的.dot文件。 這裏是代碼:鐺libtooling生成的調用圖(.dot)沒有節點標籤

clang::CallGraph mCG; 

for (unsigned i = 0 ; i < DeclsSize ; ++i) { 
    clang::FunctionDecl *FnDecl = (clang::FunctionDecl *) (Decls[i]); 
    mCG.addToCallGraph(FnDecl); 
} 

mCG.viewGraph(); 

有趣的是,生成調用圖文件(.DOT)沒有節點的標籤,但我可以正確打印我的所有節點的名稱調用圖。

這裏是生成的PIC: enter image description here

我很好奇爲什麼會出現這樣的。我的代碼中哪部分是錯誤的?

在此先感謝!

回答

0

我解決了這個問題,但我不確定它是否是一個正確的方法。 而不是調用函數 - 「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; 
... 

不管怎麼說,現在工作爲我的代碼。不確定是否還有其他好的方法。