How about ASTContext::getParents
?
You can also build partial parent maps with the ParentMap
class.
题
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.