【问题标题】:Dump Block Liveness of source code using Clang使用 Clang 转储源代码的块活跃度
【发布时间】:2016-06-22 12:43:10
【问题描述】:

我需要使用 clang 的 API 转储源代码的块活跃度。我曾尝试打印块活性,但没有成功。以下是我尝试过的代码

bool MyASTVisitor::VisitFunctionDecl(FunctionDecl *f) {

    std::cout<<"Dump Liveness\n";
    clang::AnalysisDeclContextManager adcm;
    clang::AnalysisDeclContext *adc = adcm.getContext(llvm::cast<clang::Decl>(f));
    //clang::LiveVariables *lv = clang::LiveVariables::create(*adc);
    //clang::LiveVariables *lv = clang::LiveVariables::computeLiveness(*adc,false);
    clang::LiveVariables *lv = adc->getAnalysis<LiveVariables>();
    clang::LiveVariables::Observer *obs = new clang::LiveVariables::Observer();

    lv->runOnAllBlocks(*obs);

    lv->dumpBlockLiveness((f->getASTContext()).getSourceManager());

    return true;
}

我已覆盖访问者函数并尝试打印函数的活跃度。我尝试使用 create、computeLiveness 和 getAnalysis 方法来获取 LiveVariables 对象,但所有方法都失败了。但是除了区块号之外没有显示任何活跃度信息。

当我使用 clang 的命令行参数打印活性时,它会正确显示输出。

我使用以下源代码作为测试用例,取自Live Variable Analysis Wikipedia .

    int main(int argc, char *argv[])
{
  int a,b,c,d,x;

  a = 3;  
  b = 5;
  d = 4;
  x = 100;

  if(a>b){
    c = a+b;
    d = 2;
  }

  c = 4; 
  return b * d + c;
}

有人能指出我哪里错了吗? 提前致谢。

【问题讨论】:

    标签: c++ clang abstract-syntax-tree clang++


    【解决方案1】:

    我也遇到了同样的问题,经过clang -cc1 -analyze -analyzer-checker=debug.DumpLiveVars的一些调试终于找到了答案!

    问题在于LiveVariables 分析本身并没有探索子表达式(例如DeclRefExpr)。它仅依赖于 CFG 枚举。默认情况下,CFG 仅枚举顶级语句。

    您必须先致电adc-&gt;getCFGBuildOptions().setAllAlwaysAdd(),然后才能从您的AnalysisDeclContext 获得任何分析。这将为控制流图的CFGBlocks 中的所有子表达式创建元素。

    【讨论】:

    • 但是这似乎不适用于 clang 3.1。适用于 clang 3.8。
    猜你喜欢
    • 2014-02-22
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 2020-09-29
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多