微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Clang-检索ParmVarDecl的初始化值

如何解决Clang-检索ParmVarDecl的初始化值

我有一个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 举报,一经查实,本站将立刻删除。