0

I'm looking to do something similar to this how to get integer variable name and its value from Expr* in clang using the RecursiveASTVisitor

The goal is to first retrieve all assignment operations then perform my own checks on them, to do taint analysis.

I've overridden the VisitBinaryOperator as such

bool VisitBinaryOperator (BinaryOperator *bOp) {
  if ( !bOP->isAssignmentOp() ) {
    return true;
  }

  Expr *LHSexpr = bOp->getLHS();
  Expr *RHSexpr = bOp->getRHS();

  LHSexpr->dump();
  RHSexpr->dump();
}

This RecursiveASTVisitor is being run on Objective C codes, so I do not know what the LHS or RHS type will evaluate to (could even be a function on the RHS?)

Would it be possible to get the text representation of what is on the LHS/RHS out from clang in order to perform regex expression on them??

Community
  • 1
  • 1
Jeremy Kuah
  • 519
  • 1
  • 6
  • 18

1 Answers1

0

Sorry, I found something similar that works for this particular case.

Solution:

bool VisitBinaryOperator (BinaryOperator *bOp) {
  if ( !bOP->isAssignmentOp() ) {
    return true;
  }

  Expr *LHSexpr = bOp->getLHS();
  Expr *RHSexpr = bOp->getRHS();

  std::string LHS_string = convertExpressionToString(LHSexpr);
  std::string RHS_string = convertExpressionToString(RHSexpr);

  return true;
}

std::string convertExpressionToString(Expr *E) {
  SourceManager &SM = Context->getSourceManager();
  clang::LangOptions lopt;

  SourceLocation startLoc = E->getLocStart();
  SourceLocation _endLoc = E->getLocEnd();
  SourceLocation endLoc = clang::Lexer::getLocForEndOfToken(_endLoc, 0, SM, lopt);

  return std::string(SM.getCharacterData(startLoc), SM.getCharacterData(endLoc) - SM.getCharacterData(startLoc));
}

Only thing I'm not very sure about is why _endLoc is required to compute endLoc and how is the Lexer actually working.

EDIT: Link to the post I found help Getting the source behind clang's AST

Community
  • 1
  • 1
Jeremy Kuah
  • 519
  • 1
  • 6
  • 18