2014-08-28 08:58:48 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2015-01-08 10:59:24 +01:00
|
|
|
#include <cassert>
|
2014-08-28 08:58:48 +02:00
|
|
|
#include <string>
|
|
|
|
#include "plugin.hxx"
|
|
|
|
#include "compat.hxx"
|
|
|
|
|
|
|
|
//
|
|
|
|
// We don't like using C-style casts in C++ code
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-01-08 10:59:24 +01:00
|
|
|
bool hasCLanguageLinkageType(FunctionDecl const * decl) {
|
|
|
|
return decl->isExternC() || compat::isInExternCContext(*decl);
|
|
|
|
}
|
|
|
|
|
2014-12-07 15:17:32 +01:00
|
|
|
QualType resolvePointers(QualType type) {
|
|
|
|
while (type->isPointerType()) {
|
|
|
|
type = type->getAs<PointerType>()->getPointeeType();
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2014-08-28 08:58:48 +02:00
|
|
|
class CStyleCast:
|
|
|
|
public RecursiveASTVisitor<CStyleCast>, public loplugin::Plugin
|
|
|
|
{
|
|
|
|
public:
|
2015-01-08 10:59:24 +01:00
|
|
|
explicit CStyleCast(InstantiationData const & data):
|
|
|
|
Plugin(data), externCFunction(false)
|
|
|
|
{}
|
2014-08-28 08:58:48 +02:00
|
|
|
|
2014-12-04 08:22:09 +01:00
|
|
|
virtual void run() override {
|
|
|
|
if (compiler.getLangOpts().CPlusPlus) {
|
|
|
|
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 08:58:48 +02:00
|
|
|
|
2015-01-08 10:59:24 +01:00
|
|
|
bool TraverseFunctionDecl(FunctionDecl * decl);
|
|
|
|
|
2014-08-28 08:58:48 +02:00
|
|
|
bool VisitCStyleCastExpr(const CStyleCastExpr * expr);
|
2015-01-08 10:59:24 +01:00
|
|
|
|
2015-01-09 16:42:09 +01:00
|
|
|
bool VisitImplicitCastExpr(ImplicitCastExpr const * expr);
|
|
|
|
|
2015-01-08 10:59:24 +01:00
|
|
|
private:
|
|
|
|
bool externCFunction;
|
2014-08-28 08:58:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char * recommendedFix(clang::CastKind ck) {
|
|
|
|
switch(ck) {
|
|
|
|
case CK_IntegralToPointer: return "reinterpret_cast";
|
|
|
|
case CK_PointerToIntegral: return "reinterpret_cast";
|
|
|
|
case CK_BaseToDerived: return "static_cast";
|
|
|
|
default: return "???";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 10:59:24 +01:00
|
|
|
bool CStyleCast::TraverseFunctionDecl(FunctionDecl * decl) {
|
|
|
|
bool ext = hasCLanguageLinkageType(decl)
|
|
|
|
&& decl->isThisDeclarationADefinition();
|
|
|
|
if (ext) {
|
|
|
|
assert(!externCFunction);
|
|
|
|
externCFunction = true;
|
|
|
|
}
|
|
|
|
bool ret = RecursiveASTVisitor::TraverseFunctionDecl(decl);
|
|
|
|
if (ext) {
|
|
|
|
externCFunction = false;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-28 08:58:48 +02:00
|
|
|
bool CStyleCast::VisitCStyleCastExpr(const CStyleCastExpr * expr) {
|
|
|
|
if (ignoreLocation(expr)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// casting to void is typically used when a parameter or field is only used in
|
|
|
|
// debug mode, and we want to eliminate an "unused" warning
|
|
|
|
if( expr->getCastKind() == CK_ToVoid ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// ignore integral-type conversions for now, there is unsufficient agreement about
|
|
|
|
// the merits of C++ style casting in this case
|
|
|
|
if( expr->getCastKind() == CK_IntegralCast ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if( expr->getCastKind() == CK_NoOp ) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-07 15:17:32 +01:00
|
|
|
std::string incompFrom;
|
|
|
|
std::string incompTo;
|
2014-08-28 08:58:48 +02:00
|
|
|
if( expr->getCastKind() == CK_BitCast ) {
|
2014-12-07 15:17:32 +01:00
|
|
|
QualType t1 = resolvePointers(expr->getSubExprAsWritten()->getType());
|
|
|
|
QualType t2 = resolvePointers(expr->getType());
|
|
|
|
// Ignore "safe" casts for now that do not involve incomplete types (and
|
|
|
|
// can thus not be interpreted as either a static_cast or a
|
|
|
|
// reinterpret_cast, with potentially different results):
|
|
|
|
if (t1->isVoidType() || t2->isVoidType()
|
|
|
|
|| !(t1->isIncompleteType() || t2->isIncompleteType()))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (t1->isIncompleteType()) {
|
|
|
|
incompFrom = "incomplete ";
|
|
|
|
}
|
|
|
|
if (t2->isIncompleteType()) {
|
|
|
|
incompTo = "incomplete ";
|
|
|
|
}
|
2014-08-28 08:58:48 +02:00
|
|
|
}
|
2014-09-19 15:52:05 +02:00
|
|
|
// ignore stuff from inside templates for now
|
|
|
|
if( expr->getCastKind() == CK_Dependent ) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-08 10:59:24 +01:00
|
|
|
if (externCFunction || expr->getLocStart().isMacroID()) {
|
|
|
|
SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(
|
|
|
|
expr->getLocStart());
|
|
|
|
StringRef filename = compiler.getSourceManager().getFilename(spellingLocation);
|
|
|
|
// ignore C code
|
|
|
|
if ( filename.endswith(".h") ) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-28 08:58:48 +02:00
|
|
|
}
|
|
|
|
report(
|
|
|
|
DiagnosticsEngine::Warning,
|
2014-12-07 15:17:32 +01:00
|
|
|
"c-style cast, type=%0, from=%1%2, to=%3%4, recommendedFix=%5",
|
2014-08-28 08:58:48 +02:00
|
|
|
expr->getSourceRange().getBegin())
|
|
|
|
<< expr->getCastKind()
|
2014-12-07 15:17:32 +01:00
|
|
|
<< incompFrom << expr->getSubExprAsWritten()->getType()
|
|
|
|
<< incompTo << expr->getType()
|
2014-08-28 08:58:48 +02:00
|
|
|
<< recommendedFix(expr->getCastKind())
|
|
|
|
<< expr->getSourceRange();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-09 16:42:09 +01:00
|
|
|
bool CStyleCast::VisitImplicitCastExpr(const ImplicitCastExpr * expr) {
|
|
|
|
if (ignoreLocation(expr) || expr->getCastKind() != CK_BitCast) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
QualType t = expr->getType();
|
|
|
|
if (!(t->isPointerType()
|
|
|
|
&& t->getAs<PointerType>()->getPointeeType()->isVoidType()
|
|
|
|
&& expr->getSubExpr()->getType()->isPointerType()))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Expr const * e = expr->getSubExpr()->IgnoreParenImpCasts();
|
|
|
|
while (isa<CXXConstCastExpr>(e)) {
|
|
|
|
e = dyn_cast<CXXConstCastExpr>(e)->getSubExpr()->IgnoreParenImpCasts();
|
|
|
|
}
|
|
|
|
if (isa<CXXReinterpretCastExpr>(e)) {
|
|
|
|
report(
|
|
|
|
DiagnosticsEngine::Warning,
|
|
|
|
("redundant reinterpret_cast, result is implicitly cast to void"
|
|
|
|
" pointer"),
|
|
|
|
e->getExprLoc())
|
|
|
|
<< e->getSourceRange();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-28 08:58:48 +02:00
|
|
|
loplugin::Plugin::Registration< CStyleCast > X("cstylecast");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|