2016-06-22 34 views
0

我需要使用clang的API轉儲源代碼塊的活性。我嘗試過打印塊的活性,但沒有成功。下面是我試過的代碼轉儲塊使用Clang的源代碼的活力

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

有人請指出我哪裏可能是錯的? 在此先感謝。

回答

0

我有同樣的問題,經過一些調試clang -cc1 -analyze -analyzer-checker=debug.DumpLiveVars我終於找到答案!

問題是LiveVariables分析不會自行探索子表達式(例如DeclRefExpr)。它只依賴於CFG枚舉。默認情況下,CFG只列舉頂級語句。

AnalysisDeclContext得到任何分析之前,您必須致電adc->getCFGBuildOptions().setAllAlwaysAdd()。這將爲控制流圖的CFGBlocks中的所有子表達式創建元素。

+0

非常感謝@Quentin它的工作。 :) –

+0

但是,這似乎不適用於鐺3.1。在叮噹時工作3.8。 –