Search code examples
compiler-constructionclangabstract-syntax-treesemanticsvisitor-pattern

Matching a <= BinaryOperator using Clang's ASTvisitor


I have the following AST and I'd like to visit <= using BinaryOperator visitor:

| |   `-ForStmt 0xfcb1fd8 <line:7:2, line:14:2>
| |     |-DeclStmt 0xfcb1680 <line:7:7, col:20>
| |     | `-VarDecl 0xfcb15e8 <col:7, col:19> col:17 used i 'uint16_t':'unsigned short' cinit
| |     |   `-ImplicitCastExpr 0xfcb1668 <col:19> 'uint16_t':'unsigned short' <IntegralCast>
| |     |     `-IntegerLiteral 0xfcb1648 <col:19> 'int' 0
| |     |-BinaryOperator 0xfcb1a28 <col:22, col:55> 'bool' '<='
| |     | |-ImplicitCastExpr 0xfcb19f8 <col:22> 'int' <IntegralCast>

BinaryOperator is inherited from Expr::BinaryOperator, I can't quite figure out how to traverse down from an Expr *E to a BinaryOperator *BO:

static bool vectorLoopConditionVisitor(Sema &S, BinaryOperator *BO){

    if (!BO){
    // error
      }
   if (!BO->isAssignmentOp() && //need a condition to match with "<=" ){  
      // error
    }


  return false;
}

 static bool vectorLoopVisitor(Sema &S, Stmt *St, uint32_t Depth) {
    ForStmt *Fst = dyn_cast<ForStmt>(St);

    // ...
    vectorLoopConditionVisitor(S, dyn_cast<BinaryOperator>(Fst->getCond())); 
    // this is most certainly wrong as it returns a null pointer and segfault
    //...
    vectorLoopBodyVisitor(S, Fst->getBody(), Depth);

    return false;
  }

Solution

  • Could you please expand the code a bit more? How do you construct the visitor and how do you traverse the code?

    The following visitor should do the job for you:

    class BinaryVisitor : public clang::RecursiveASTVisitor<BinaryVisitor> {
    public:
      bool VisitBinaryOperator(clang::BinaryOperator *binaryOperator) {
        if (binaryOperator->getOpcode() != clang::BinaryOperator::Opcode::BO_LE) {
          return true;
        }
    
        // Handle the '<=' matching here
    
        return true;
      }
    };
    
    /// ...
    BinaryVisitor visitor;
    visitor.TraverseDecl(someDecl);
    

    This visitor will check every BinaryOperaror and will skip anything that is not "less than" (clang::BinaryOperator::Opcode::BO_LE).