loplugin:redundantcast: reinterpret_cast to void*

Change-Id: I947b49cfb15f0e7d6ddfaae386656c70e4bd48ba
This commit is contained in:
Stephan Bergmann
2015-05-12 18:27:37 +02:00
parent 4e871a9b22
commit 0bfc98e63b
10 changed files with 59 additions and 46 deletions

View File

@@ -207,45 +207,59 @@ bool RedundantCast::VisitImplicitCastExpr(const ImplicitCastExpr * expr) {
bool RedundantCast::VisitCXXReinterpretCastExpr(
CXXReinterpretCastExpr const * expr)
{
if (ignoreLocation(expr)
|| !expr->getSubExpr()->getType()->isVoidPointerType())
{
if (ignoreLocation(expr)) {
return true;
}
auto t = expr->getType()->getAs<PointerType>();
if (t == nullptr || !t->getPointeeType()->isObjectType()) {
return true;
}
if (rewriter != nullptr) {
auto loc = expr->getLocStart();
while (compiler.getSourceManager().isMacroArgExpansion(loc)) {
loc = compiler.getSourceManager().getImmediateMacroCallerLoc(loc);
}
if (compat::isMacroBodyExpansion(compiler, loc)) {
auto loc2 = expr->getLocEnd();
while (compiler.getSourceManager().isMacroArgExpansion(loc2)) {
loc2 = compiler.getSourceManager().getImmediateMacroCallerLoc(
loc2);
}
if (compat::isMacroBodyExpansion(compiler, loc2)) {
//TODO: check loc, loc2 are in same macro body expansion
loc = compiler.getSourceManager().getSpellingLoc(loc);
}
}
auto s = compiler.getSourceManager().getCharacterData(loc);
auto n = Lexer::MeasureTokenLength(
loc, compiler.getSourceManager(), compiler.getLangOpts());
std::string tok(s, n);
if (tok == "reinterpret_cast" && replaceText(loc, n, "static_cast")) {
if (expr->getSubExpr()->getType()->isVoidPointerType()) {
auto t = expr->getType()->getAs<PointerType>();
if (t == nullptr || !t->getPointeeType()->isObjectType()) {
return true;
}
if (rewriter != nullptr) {
auto loc = expr->getLocStart();
while (compiler.getSourceManager().isMacroArgExpansion(loc)) {
loc = compiler.getSourceManager().getImmediateMacroCallerLoc(
loc);
}
if (compat::isMacroBodyExpansion(compiler, loc)) {
auto loc2 = expr->getLocEnd();
while (compiler.getSourceManager().isMacroArgExpansion(loc2)) {
loc2 = compiler.getSourceManager()
.getImmediateMacroCallerLoc(loc2);
}
if (compat::isMacroBodyExpansion(compiler, loc2)) {
//TODO: check loc, loc2 are in same macro body expansion
loc = compiler.getSourceManager().getSpellingLoc(loc);
}
}
auto s = compiler.getSourceManager().getCharacterData(loc);
auto n = Lexer::MeasureTokenLength(
loc, compiler.getSourceManager(), compiler.getLangOpts());
std::string tok(s, n);
if (tok == "reinterpret_cast" && replaceText(loc, n, "static_cast"))
{
return true;
}
}
report(
DiagnosticsEngine::Warning,
"reinterpret_cast from %0 to %1 can be simplified to static_cast",
expr->getExprLoc())
<< expr->getSubExprAsWritten()->getType() << expr->getType()
<< expr->getSourceRange();
} else if (expr->getType()->isVoidPointerType()) {
auto t = expr->getSubExpr()->getType()->getAs<PointerType>();
if (t == nullptr || !t->getPointeeType()->isObjectType()) {
return true;
}
report(
DiagnosticsEngine::Warning,
("reinterpret_cast from %0 to %1 can be simplified to static_cast"
" or an implicit conversion"),
expr->getExprLoc())
<< expr->getSubExprAsWritten()->getType() << expr->getType()
<< expr->getSourceRange();
}
report(
DiagnosticsEngine::Warning,
"reinterpret_cast from %0 to %1 can be simplified to static_cast",
expr->getExprLoc())
<< expr->getSubExprAsWritten()->getType() << expr->getType()
<< expr->getSourceRange();
return true;
}