3

在过去的 5 年里,我一直在使用 Gnu Compiler Collection (gcc/g++),所以我是 clang 的新手,我想为 C/Objective C/C++ 源代码生成一个控制流图。谢天谢地,我在这里这里阅读并发现我可以使用 CFG 类函数获得源级语句的基本块表示:CFG::buildCFG() CFG::dump()

这很棒:),但我仍然不清楚如何在将函数体传递给 buildCFG 之前以 AST 形式和 ASTContext 获取函数体?

有人可以在这里给出一个简短的片段,演示如何使用clang导出给定源代码的CFG吗?

谢谢你们

4

2 回答 2

3

我能够使用与您上面提到的不同的方法生成 CFG。它可能会满足您的需求。注释掉以下行(临时):

sys::fs::remove(Filename);

ExecGraphViewer()Support/GraphWriter.cpp 中的函数并重建 clang。

然后,运行 clang 静态分析工具:

clang -cc1 -analyze -analyze-checker=debug.ViewCFG src_code.cpp

通常,此命令将打开一个图形查看器以查看您的 CFG,然后当您完成后,它将删除它为查看而创建的所有 .dot 文件。但是,如果您将此行注释掉,即使您关闭查看器,您也可以将这些文件复制到其他地方。

一定要重新添加它,否则它可能会变得非常大......

还想补充一点,我得到了亚当的帮助

于 2017-07-11T07:37:42.083 回答
0

此源代码片段将把给定函数(在本例中为 main 函数)的 CFG 打印到终端。

#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Analysis/CFG.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"

using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::tooling;

using namespace llvm;

DeclarationMatcher FunctionMatcher =
    functionDecl(isMain()).bind("mainFunction");

class CFGPrinter : public MatchFinder::MatchCallback {
public:
  virtual void run(const MatchFinder::MatchResult &Result) {
    if (const FunctionDecl *funcDecl =
            Result.Nodes.getNodeAs<clang::FunctionDecl>("mainFunction")) {
      ASTContext *context = Result.Context;
      Stmt *funcBody = funcDecl->getBody();
      static std::unique_ptr<CFG> sourceCFG = CFG::buildCFG(
          funcDecl, funcBody, context, clang::CFG::BuildOptions());
      auto langOpt = context->getLangOpts();
      sourceCFG->dump(langOpt, true);
    }
  }
};

// Apply a custom category to all command-line options so that they are the
// only ones displayed.
static llvm::cl::OptionCategory MyToolCategory("my-tool options");

// CommonOptionsParser declares HelpMessage with a description of the common
// command-line options related to the compilation database and input files.
// It's nice to have this help message in all tools.
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);

// A help message for this specific tool can be added afterwards.
static cl::extrahelp MoreHelp("\nMore help text...\n");

int main(int argc, const char **argv) {
  CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
  ClangTool Tool(OptionsParser.getCompilations(),
                 OptionsParser.getSourcePathList());

  CFGPrinter Printer;
  MatchFinder Finder;
  Finder.addMatcher(FunctionMatcher, &Printer);

  return Tool.run(newFrontendActionFactory(&Finder).get());
}
于 2019-10-16T13:35:50.443 回答