我正在与 https://people.eecs.berkeley.edu/~necula/cil/api/Cil.html
,并希望匹配指针并在特定表达式中提取变量名称
示例(假设这是正确的)
int ** p0,* p1,** p2,*** p3;
我的表情:** p0 + * p1 + ** p2 + ** p3
我想匹配整个取消引用的指针,例如:**p0, **p2, **p3
,而不是部分匹配:*p0, *p2, *p3
(这就是我的代码的作用,因此根据需要,它仅匹配*p1
)< / p>
class lvalVisitor ctx = object (*f d as params*)
inherit nopCilVisitor
method vlval lval =
match lval with
(*this will match only *pointer and not **pointer or ***pointer *)
| Mem (Lval (Var v, _)), _ -> printf "derefed. var %s" v.vname
SkipChildren
| Mem e, _ -> DoChildren
| _ -> DoChildren
end
TL; DR 我想知道一个指针是否被完全取消引用(计算星号不是一个好选择)
答案 0 :(得分:0)
我不熟悉CIL Api,但是递归方法不能胜任吗?
例如:
class lvalVisitor ctx = object (self)
inherit nopCilVisitor
method vlval lval =
match lval with
(*this will match only *pointer and not **pointer or ***pointer *)
| Mem (Lval (Var v, _)), _ ->
Format.printf "derefed. var %s" v.vname;
SkipChildren
| Mem (Lval e), _ -> self#vlval e
| _ -> DoChildren
end;;
答案 1 :(得分:0)
如果我们可以假设我们只有解引用和变量(在您的示例中就是这种情况),基本上,您只需要在 first 遇到SkipChildren
之后Mem
:< / p>
class lvalVisitor ctx = object(self) (*f d as params*)
inherit nopCilVisitor
method vlval lval =
match lval with
| Mem _, _ -> do_something lval; SkipChildren
| _ -> DoChildren
end
这应该(未经测试)仅在所有最上面的解除引用上调用do_something
,即从您在**p0
,*p1
,**p2
和{{1} }。在更复杂的情况下,例如*(* p + o),**p3
将看到整个lval,而只有它。您尚未指定在这种情况下应采取的措施。如果您还想检查do_something
,那么*p
实际上应该是另一种方法,您应该调用
do_something
当您想重新启动递归以进行更深入的探索时。