2017-06-27 7 views
0

私はソースでいくつかのclangを書き直しています。ステートメントの前に1行挿入したい。私は次のようにif文の内側からの呼び出しがある場合のように:、Clang Rewrite:呼び出し式の先頭を見つけよう

//there are call inside 
if (cmp(a,b) > 0 && cmp(c,d) < 0){ 
//do something 
} 

を私は今までに何ができるか:

if (cmp(a,b) > 0 && cmp(c,d) < 0){ 
//do something 
} 

を私は文の場合は、以下のように、その前にコメントを書き換えたいです私はVisitCallExpr()で呼び出しを検出できました。しかし、コメントを書くためにif文の前にどのように場所を見つけることができますか? N.B.

for (int a = 0; a < range(s); a++){ 
//do something 
} 

誰も私がこれを行う方法を見つけるのを助けることができる:ここでのif文は次のように、あまりにものためである可能性があります。

答えて

0

私はこれを行う方法を見つけましたが、一般化された方法ではありません。私はその問題に関する別の質問を開いた。しかし、我々はここだけで、指定の状況を考慮した場合のように、次の

// add something here 
if (cmp(a,b)){ 
//do something 
} 
我々は、単純なASTマッチャで行うことができ

Matcher.addMatcher(ifStmt(hasCondition(callExpr())) 
          .bind("call_from_if"),&handleIFCALL); 

とハンドラのようにする必要があります定義:

class IFCALL : public MatchFinder::MatchCallback { 
public: 
    IFCALL(Rewriter &Rewrite) : Rewrite(Rewrite) {} 

    virtual void run(const MatchFinder::MatchResult &Result) { 
    const IfStmt *statement = 
     Result.Nodes.getNodeAs<clang::IfStmt>("call_from_if"); 

     SourceLocation ST = statement->getLocStart(); 
     std::stringstream SSBefore; 
     SSBefore << "\n \\\\ if statement have call inside \n"; 
     Rewrite.InsertText(ST, SSBefore.str(), true, true); 
    } 
    } 

private: 
    Rewriter &Rewrite; 
}; 

このリンクは私にこのソリューションを見つけるのに役立ちます: https://jonasdevlieghere.com/understanding-the-clang-ast/

新しい問題へのリンクは次のとおりです: Clang IfStmt with shortcut binary operator in condition

関連する問題