virtual void run()

in legacy/objc/src/XPFlagRefactoring/XPFlagRefactoring.cpp [372:439]


  virtual void run(const MatchFinder::MatchResult &Result) {
    const BinaryOperator *s = Result.Nodes.getNodeAs<BinaryOperator>("binOp");
    const ObjCMessageExpr *msgExpr =
        Result.Nodes.getNodeAs<ObjCMessageExpr>("theSelector");
    const StringLiteral *strLiteral =
        Result.Nodes.getNodeAs<StringLiteral>("theFlag");
    ASTContext *context = Result.Context;

    // avoid double processing of the same selector.
    if (msgExpr != NULL && processedExprs.find(msgExpr) != processedExprs.end())
      return;

    // ensure that the string literal, if it exists, matches the flagName.
    if (strLiteral != NULL && strLiteral->getString().compare(flagName)) {
      return;
    }

    Value v = IS_UNKNOWN;
    if (s != NULL) {
      v = XPUtils::evalExpr(s, Result, msgExpr, flagType);
    }

    string replacementText = "";
    Value l = XPUtils::evalExpr(s->getLHS(), Result, msgExpr, flagType);
    Value r = XPUtils::evalExpr(s->getRHS(), Result, msgExpr, flagType);

    const Expr *nodeToReplace = s;
    char delimiter = '\0';

    if (v == IS_UNKNOWN) {
      if (s->getOpcode() == BO_LAnd) {
        if (l == IS_TRUE) {
          replacementText = XPUtils::getExprSourceText(s->getRHS(), context);
        } else if (r == IS_TRUE) {
          replacementText = XPUtils::getExprSourceText(s->getLHS(), context);
        }
      } else if (s->getOpcode() == BO_LOr) {
        if (l == IS_FALSE) {
          replacementText = XPUtils::getExprSourceText(s->getRHS(), context);
        } else if (r == IS_FALSE) {
          replacementText = XPUtils::getExprSourceText(s->getLHS(), context);
        }
      } else {
        if (s->getOpcode() == BO_Assign) {
          nodeToReplace = s->getRHS();
          delimiter = ';';
          if (r == IS_TRUE) {
            replacementText = "true;";
          } else if (r == IS_FALSE) {
            replacementText = "false;";
          }
        }
      }
    } else if (v == IS_TRUE) {
      replacementText = "true";
    } else if (v == IS_FALSE) {
      replacementText = "false";
    }

    if (replacementText.compare("")) {
      processedExprs.insert(make_pair(msgExpr, true));
      SourceRange sr =
          XPUtils::getRequiredSourceRange(nodeToReplace, context, delimiter);
      llvm::errs() << "Rewriting in BinOpHandler \n";
      FixItRewriterOptions::rewriteCode(
          context, rewriter, sr, nodeToReplace->getBeginLoc(), replacementText);
    }
  }