如何从锵Expr的对象获取语句类对象 [英] How to get Stmt class object from Expr object in Clang

查看:187
本文介绍了如何从锵Expr的对象获取语句类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个铛插件在C code插入断言。我实现了一个类访问每个一元运算符,并检查它是否是一个指针引用。如果是这样,我想插入一个空指针断言检查它。但我坚持,因为我无法弄清楚如何获得包含锵的​​Expr的对象的语句对象。

这是我的code这仪器的说法,但在一个完全错误的位置(即刚刚指针引用后,我想只是包含解引用语句之前做到这一点。

 布尔MyRecursiveASTVisitor :: VisitUnaryOperator(UnaryOperator * E){
    如果(E-GT&; getOp code()== UO_Deref){
        EXPR * E1 = E-GT&; getSubExpr();
        SourceLocation SL = E-> getLocEnd();
        Rewrite.InsertText(SL断言(真实,真实);
        Rewrite.InsertText(SL,Rewrite.ConvertToString(E1),真,真);
        Rewrite.InsertText(SL!= NULL);,真,真);
    }
    返回true;
}


解决方案

如何 ASTContext :: getParents

您也可以建立部分家长的地图与 ParentMap 类。

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

解决方案

How about ASTContext::getParents?

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

这篇关于如何从锵Expr的对象获取语句类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆