From 0bfc98e63b062bc847fd0f37308c050ac618c56f Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Tue, 12 May 2015 18:27:37 +0200 Subject: loplugin:redundantcast: reinterpret_cast to void* Change-Id: I947b49cfb15f0e7d6ddfaae386656c70e4bd48ba --- compilerplugins/clang/redundantcast.cxx | 74 ++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 30 deletions(-) (limited to 'compilerplugins') diff --git a/compilerplugins/clang/redundantcast.cxx b/compilerplugins/clang/redundantcast.cxx index 3abf66a853b3..474dafa11e85 100644 --- a/compilerplugins/clang/redundantcast.cxx +++ b/compilerplugins/clang/redundantcast.cxx @@ -207,45 +207,59 @@ bool RedundantCast::VisitImplicitCastExpr(const ImplicitCastExpr * expr) { bool RedundantCast::VisitCXXReinterpretCastExpr( CXXReinterpretCastExpr const * expr) { - if (ignoreLocation(expr) - || !expr->getSubExpr()->getType()->isVoidPointerType()) - { - return true; - } - auto t = expr->getType()->getAs(); - if (t == nullptr || !t->getPointeeType()->isObjectType()) { + if (ignoreLocation(expr)) { return true; } - if (rewriter != nullptr) { - auto loc = expr->getLocStart(); - while (compiler.getSourceManager().isMacroArgExpansion(loc)) { - loc = compiler.getSourceManager().getImmediateMacroCallerLoc(loc); + if (expr->getSubExpr()->getType()->isVoidPointerType()) { + auto t = expr->getType()->getAs(); + if (t == nullptr || !t->getPointeeType()->isObjectType()) { + return true; } - if (compat::isMacroBodyExpansion(compiler, loc)) { - auto loc2 = expr->getLocEnd(); - while (compiler.getSourceManager().isMacroArgExpansion(loc2)) { - loc2 = compiler.getSourceManager().getImmediateMacroCallerLoc( - loc2); + if (rewriter != nullptr) { + auto loc = expr->getLocStart(); + while (compiler.getSourceManager().isMacroArgExpansion(loc)) { + loc = compiler.getSourceManager().getImmediateMacroCallerLoc( + loc); } - if (compat::isMacroBodyExpansion(compiler, loc2)) { - //TODO: check loc, loc2 are in same macro body expansion - loc = compiler.getSourceManager().getSpellingLoc(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; } } - 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")) { + 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(); + 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; } -- cgit v1.2.3