0

我是 clang libtooling 的初学者。我正在尝试使用 clang::CallGraph viewGraph 来生成我的调用图的 .dot 文件。这是代码:

clang::CallGraph mCG;

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

mCG.viewGraph();

有趣的是,生成的调用图文件 (.dot) 没有节点的标签,尽管我可以正确打印带有所有节点名称的调用图。

这是生成的图片: 在此处输入图像描述

我很好奇为什么会这样显示。我的代码中哪一部分有问题?

提前致谢!

4

1 回答 1

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;
...

无论如何,它现在适用于我的代码。不知道有没有其他好的方法。

于 2017-07-15T20:59:46.410 回答