如何在使用clang的另一条语句之前插入一条语句 [英] How to instrument a statement just before another statement using clang
问题描述
我必须通过在clang前面添加一条语句来对某些语句进行检测.我有一个指向Expr对象的指针,使用它需要在包含该对象的语句之前插入另一个语句.现在,我正在使用一种骇人听闻的方法,该方法只是将SourceLocation指针移回,直到看到一个;或}或{.但这并不适用于所有情况.例如,当我尝试插入for语句时,它将失败. clang中是否有任何类提供了一种更简洁的方法来实现此目的?
I have to instrument certain statements in clang by adding a statement just before it. I have a pointer to an Expr object using which I need to insert another statement just before the statement containing it. Right now I am using a hacky approach which just moves back the SourceLocation pointer till I see a ; or } or {. But this does not work for all cases. eg when I try to instrument a for statement, it fails. Is there any class in clang which provides a method to do this in a more cleaner way?
这是我的代码段.我需要在包含指针取消引用的语句之前插入一个断言.
Here is snippet of my code. I need to insert an assert just before the statement containing a pointer dereference.
bool MyRecursiveASTVisitor::VisitUnaryOperator(UnaryOperator *E){
if (E->getOpcode() == UO_Deref ){
Expr *e1 = E->getSubExpr();
SourceLocation SL = E->getLocStart();
}
return true;
}
推荐答案
您可以使用返回 SourceRange
SourceRange类的语句上的getSourceRange()
getBegin()
,它将返回 SourceLocation
.
You can use getSourceRange()
on the statement which returns SourceRange
SourceRange Class has getBegin()
which will return SourceLocation
.
您可以结帐 https://github.com/eschulte/clang-mutate 和在那里看到一个例子...
you can checkout https://github.com/eschulte/clang-mutate and see an example there...
编辑:
In order to extract Expr out of Stmt you can checkout this SO question How to get Stmt class object from Expr object in Clang
这篇关于如何在使用clang的另一条语句之前插入一条语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!