问题描述
我有一个ForStmt
,我想为其检索GetBoundFunc()
返回的循环绑定,如下所示:
void testASTVistor (int N) {
N = 123;
for (int i = 0; i <= GetBoundFunc(N); i++ ){
//do Sth;
}
}
AST
对于N
看起来像这样:
-FunctionDecl 0x55d60e61cd48 <line:3:1,line:8:1> line:3:6 testASTVistor 'void (int)'
|-ParmVarDecl 0x55d60e61cc88 <col:21,col:25> col:25 used N 'int'
`-CompoundStmt 0x55d60e61d0e8 <col:28,line:8:1>
¦ |-BinaryOperator 0x55d60e61ce28 <line:4:3,col:7> 'int' lvalue '='
¦ | |-DeclRefExpr 0x55d60e61cde8 <col:3> 'int' lvalue ParmVar 0x55d60e61cc88 'N' 'int'
¦ | `-IntegerLiteral 0x55d60e61ce08 <col:7> 'int' 123
但是,当我的ASTRecurvieVisitor代码应返回N的初始化值时,它会不断出现段错误:
class BinaryVisitor : public clang::RecursiveASTVisitor<BinaryVisitor> {
public:
bool VisitBinaryOperator(clang::BinaryOperator *BO) {
if (const CallExpr *RHS = dyn_cast<CallExpr>(BO->getRHS()->IgnoreParenImpCasts())) {
if (const DeclRefExpr *DRE = dyn_cast_or_null<DeclRefExpr>(RHS->getArg(0))) {
if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
if (const IntegerLiteral *Bound = dyn_cast<IntegerLiteral>(PVD->getinit()->ignoreImpCasts())) {
return false;
}
}
}
};
/// ...
BinaryVisitor visitor;
visitor.TraverseDecl(someDecl);
evaluateValue()
和getEvaluatedValue()
也尝试过,似乎没有人绕过这个问题。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)