bool SimplifyDynamicCast::TraverseIfStmt(IfStmt* ifStmt)
{
CXXDynamicCastExpr const* dynamicCastExpr = nullptr; if (Expr const* condExpr = ifStmt->getCond())
{
condExpr = condExpr->IgnoreParenImpCasts();
dynamicCastExpr = dyn_cast<CXXDynamicCastExpr>(condExpr); if (!dynamicCastExpr)
{ if (auto binaryOp = dyn_cast<BinaryOperator>(condExpr))
{ if (binaryOp->getOpcode() == BO_NE)
dynamicCastExpr
= dyn_cast<CXXDynamicCastExpr>(binaryOp->getLHS()->IgnoreParenImpCasts());
}
}
}
Decl const* subExprDecl = nullptr; if (dynamicCastExpr)
{ auto subExprDeclRefExpr
= dyn_cast<DeclRefExpr>(dynamicCastExpr->getSubExpr()->IgnoreParenImpCasts()); if (!subExprDeclRefExpr)
dynamicCastExpr = nullptr; else
subExprDecl = subExprDeclRefExpr->getDecl();
} if (dynamicCastExpr)
{ auto qt = dynamicCastExpr->getTypeAsWritten();
dynamicCastVec.push_back(qt);
dynamicCastSubExprVec.push_back(subExprDecl);
ifVec.push_back(ifStmt);
} bool ret = RecursiveASTVisitor::TraverseIfStmt(ifStmt); if (dynamicCastExpr)
{
dynamicCastVec.pop_back();
dynamicCastSubExprVec.pop_back();
ifVec.pop_back();
} return ret;
}
bool SimplifyDynamicCast::VisitCXXStaticCastExpr(CXXStaticCastExpr const* staticCastExpr)
{ if (ignoreLocation(staticCastExpr)) returntrue; if (dynamicCastVec.empty()) returntrue;
auto qt = staticCastExpr->getTypeAsWritten(); auto it = std::find(dynamicCastVec.begin(), dynamicCastVec.end(), qt); if (it == dynamicCastVec.end()) returntrue; int idx = it - dynamicCastVec.begin(); auto subExprDecl = dyn_cast<DeclRefExpr>(staticCastExpr->getSubExpr()->IgnoreParenImpCasts()); if (!subExprDecl) returntrue; if (dynamicCastSubExprVec[idx] != subExprDecl->getDecl()) returntrue;
report(DiagnosticsEngine::Warning, "simplify, use var in if", staticCastExpr->getBeginLoc())
<< staticCastExpr->getSourceRange(); auto ifStmt = ifVec[idx];
report(DiagnosticsEngine::Note, "if here", ifStmt->getBeginLoc()) << ifStmt->getSourceRange(); returntrue;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.