How to get Stmt class object from Expr object in Clang

社会主义新天地 提交于 2019-12-11 04:56:15

问题


I am writing a clang plugin for inserting assertions in a C code. I have implemented a class for visiting each unary operator and check if it is a pointer dereference. If it is, I would like to insert an NULL pointer assertion check for it. But I am stuck as I cannot figure out how to get the Stmt object containing the Expr object in Clang.

This is my code which instruments the assertion but at a completely wrong location (ie just after the pointer dereference. I would like to do it just before the statement containing the dereference.

bool MyRecursiveASTVisitor::VisitUnaryOperator(UnaryOperator *E){
    if (E->getOpcode() == UO_Deref ){
        Expr *e1 = E->getSubExpr();
        SourceLocation SL = E->getLocEnd();
        Rewrite.InsertText(SL, "assert(", true, true);
        Rewrite.InsertText(SL, Rewrite.ConvertToString(e1), true, true);
        Rewrite.InsertText(SL, " != NULL);", true, true);
    }
    return true;
}

回答1:


How about ASTContext::getParents?

You can also build partial parent maps with the ParentMap class.



来源:https://stackoverflow.com/questions/18868994/how-to-get-stmt-class-object-from-expr-object-in-clang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!