# Cppcheck report - LibreOffice: compilerplugins/clang/cstylecast.cxx

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140

    /* -*- 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/.
     */

    #include <cassert>
    #include <string>
    #include "plugin.hxx"
    #include "compat.hxx"

    //
    // We don't like using C-style casts in C++ code
    //

    namespace {

    bool hasCLanguageLinkageType(FunctionDecl const * decl) {
        return decl->isExternC() || compat::isInExternCContext(*decl);
    }

    QualType resolvePointers(QualType type) {
        while (type->isPointerType()) {
            type = type->getAs<PointerType>()->getPointeeType();
        }
        return type;
    }

    class CStyleCast:
        public RecursiveASTVisitor<CStyleCast>, public loplugin::Plugin
    {
    public:
        explicit CStyleCast(InstantiationData const & data):
            Plugin(data), externCFunction(false)
        {}

        virtual void run() override {
            if (compiler.getLangOpts().CPlusPlus) {
                TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
            }
        }

        bool TraverseFunctionDecl(FunctionDecl * decl);

        bool VisitCStyleCastExpr(const CStyleCastExpr * expr);

    private:
        bool externCFunction;
    };

    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 "???";
        }
    }

    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;
    }

    bool CStyleCast::VisitCStyleCastExpr(const CStyleCastExpr * expr) {<--- The function 'VisitCStyleCastExpr' is never used.
        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;
        }
        std::string incompFrom;
        std::string incompTo;
        if( expr->getCastKind() == CK_BitCast ) {
            QualType t = resolvePointers(expr->getSubExprAsWritten()->getType());
            // Ignore "safe" casts for now that involve casting from void* (and can
            // thus not be interpreted as either a static_cast or a
            // reinterpret_cast, with potentially different results):
            if (t->isVoidType()) {
                return true;
            }
            if (t->isIncompleteType()) {
                incompFrom = "incomplete ";
            }
            if (resolvePointers(expr->getType())->isIncompleteType()) {
                incompTo = "incomplete ";
            }
        }
        // ignore stuff from inside templates for now
        if( expr->getCastKind() == CK_Dependent ) {
            return true;
        }
        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;
            }
        }
        report(
            DiagnosticsEngine::Warning,
            "c-style cast, type=%0, from=%1%2, to=%3%4, recommendedFix=%5",
            expr->getSourceRange().getBegin())
          << expr->getCastKind()
          << incompFrom << expr->getSubExprAsWritten()->getType()
          << incompTo << expr->getType()
          << recommendedFix(expr->getCastKind())
          << expr->getSourceRange();
        return true;
    }

    loplugin::Plugin::Registration< CStyleCast > X("cstylecast");

    }

    /* vim:set shiftwidth=4 softtabstop=4 expandtab: */