| 12
 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
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 
 | /* -*- 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/.
 */
#ifndef LO_CLANG_SHARED_PLUGINS
#include "plugin.hxx"
#include "check.hxx"
#include "compat.hxx"
#include <iostream>
/**
  Check for
   (*) calls to CPPUNIT_ASSERT when it should be using CPPUNIT_ASSERT_EQUALS
   (*) calls to CPPUNIT_ASSERT_EQUALS where the constant is the second param
*/
namespace {
class CppunitAssertEquals:
    public loplugin::FilteringPlugin<CppunitAssertEquals>
{
public:
    explicit CppunitAssertEquals(loplugin::InstantiationData const & data):
        FilteringPlugin(data) {}
    virtual bool preRun() override
    {
        return compiler.getLangOpts().CPlusPlus;
    }
    virtual void run() override
    {
        if (preRun()) {
            TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
        }
    }
    bool VisitCallExpr(const CallExpr*);
private:
    void checkExpr(
        SourceRange range, StringRef name, Expr const * expr, bool negated);
    void reportEquals(
        SourceRange range, StringRef name, bool negative, Expr const * lhs, Expr const * rhs);
    bool isCompileTimeConstant(Expr const * expr);
};
bool CppunitAssertEquals::VisitCallExpr(const CallExpr* callExpr)
{
    auto const decl = callExpr->getDirectCallee();
    if (!decl)
        return true;
    /*
       calls to CPPUNIT_ASSERT when it should be using CPPUNIT_ASSERT_EQUALS
    */
    if (loplugin::DeclCheck(decl).Function("failIf").Struct("Asserter")
             .Namespace("CppUnit").GlobalNamespace())
    {
        // Don't use callExpr->getLocStart() or callExpr->getExprLoc(), as those
        // fall into a nested use of the CPPUNIT_NS macro; CallExpr::getRParenLoc
        // happens to be readily available and cause good results:
        auto loc = callExpr->getRParenLoc();
        while (compiler.getSourceManager().isMacroArgExpansion(loc)) {
            loc = compiler.getSourceManager().getImmediateMacroCallerLoc(loc);
        }
        if (!compiler.getSourceManager().isMacroBodyExpansion(loc)
            || ignoreLocation(
                compiler.getSourceManager().getImmediateMacroCallerLoc(loc)))
        {
            return true;
        }
        auto name = Lexer::getImmediateMacroName(
            loc, compiler.getSourceManager(), compiler.getLangOpts());
        if (name != "CPPUNIT_ASSERT" && name != "CPPUNIT_ASSERT_MESSAGE") {
            return true;
        }
        if (decl->getNumParams() != 3) {
            report(
                DiagnosticsEngine::Warning,
                ("TODO: suspicious CppUnit::Asserter::failIf call with %0"
                 " parameters"),
                callExpr->getExprLoc())
                << decl->getNumParams() << callExpr->getSourceRange();
            return true;
        }
        auto const e1 = callExpr->getArg(0)->IgnoreParenImpCasts();
        Expr const * e2 = nullptr;
        if (auto const e3 = dyn_cast<UnaryOperator>(e1)) {
            if (e3->getOpcode() == UO_LNot) {
                e2 = e3->getSubExpr();
            }
        } else if (auto const e4 = dyn_cast<CXXOperatorCallExpr>(e1)) {
            if (e4->getOperator() == OO_Exclaim) {
                e2 = e4->getArg(0);
            }
        }
        if (e2 == nullptr) {
            report(
                DiagnosticsEngine::Warning,
                ("TODO: suspicious CppUnit::Asserter::failIf call not wrapping"
                 " !(...)"),
                callExpr->getExprLoc())
                << callExpr->getSourceRange();
            return true;
        }
        auto range = compat::getImmediateExpansionRange(compiler.getSourceManager(), loc);
        checkExpr(
            SourceRange(range.first, range.second), name,
            e2->IgnoreParenImpCasts(), false);
    }
    /**
      Check for calls to CPPUNIT_ASSERT_EQUALS where the constant is the second param
    */
    if (loplugin::DeclCheck(decl).Function("assertEquals").
             Namespace("CppUnit").GlobalNamespace())
    {
        // can happen in template test code that both params are compile time constants
        if (isCompileTimeConstant(callExpr->getArg(0)))
            return true;
        if (isCompileTimeConstant(callExpr->getArg(1)))
            report(
                DiagnosticsEngine::Warning,
                "CPPUNIT_ASSERT_EQUALS parameters look switched, expected value should be first param",
                callExpr->getExprLoc())
                << callExpr->getSourceRange();
    }
    if (loplugin::DeclCheck(decl).Function("assertDoubleEquals").
             Namespace("CppUnit").GlobalNamespace())
    {
        // can happen in template test code that both params are compile time constants
        if (isCompileTimeConstant(callExpr->getArg(0)))
            return true;
        if (isCompileTimeConstant(callExpr->getArg(1)))
            report(
                DiagnosticsEngine::Warning,
                "CPPUNIT_ASSERT_DOUBLES_EQUALS parameters look switched, expected value should be first param",
                callExpr->getExprLoc())
                << callExpr->getSourceRange();
    }
    if (loplugin::DeclCheck(decl).Function("assertLess").
             Namespace("CppUnit").GlobalNamespace())
    {
        // can happen in template test code that both params are compile time constants
        if (isCompileTimeConstant(callExpr->getArg(0)))
            return true;
        if (isCompileTimeConstant(callExpr->getArg(1)))
            report(
                DiagnosticsEngine::Warning,
                "CPPUNIT_ASSERT_LESS parameters look switched, expected value should be first param",
                callExpr->getExprLoc())
                << callExpr->getSourceRange();
    }
    if (loplugin::DeclCheck(decl).Function("assertLessEqual").
             Namespace("CppUnit").GlobalNamespace())
    {
        // can happen in template test code that both params are compile time constants
        if (isCompileTimeConstant(callExpr->getArg(0)))
            return true;
        if (isCompileTimeConstant(callExpr->getArg(1)))
            report(
                DiagnosticsEngine::Warning,
                "CPPUNIT_ASSERT_LESSEQUAL parameters look switched, expected value should be first param",
                callExpr->getExprLoc())
                << callExpr->getSourceRange();
    }
    if (loplugin::DeclCheck(decl).Function("assertGreater").
             Namespace("CppUnit").GlobalNamespace())
    {
        // can happen in template test code that both params are compile time constants
        if (isCompileTimeConstant(callExpr->getArg(0)))
            return true;
        if (isCompileTimeConstant(callExpr->getArg(1)))
            report(
                DiagnosticsEngine::Warning,
                "CPPUNIT_ASSERT_GREATER parameters look switched, expected value should be first param",
                callExpr->getExprLoc())
                << callExpr->getSourceRange();
    }
    if (loplugin::DeclCheck(decl).Function("assertGreaterEqual").
             Namespace("CppUnit").GlobalNamespace())
    {
        // can happen in template test code that both params are compile time constants
        if (isCompileTimeConstant(callExpr->getArg(0)))
            return true;
        if (isCompileTimeConstant(callExpr->getArg(1)))
            report(
                DiagnosticsEngine::Warning,
                "CPPUNIT_ASSERT_GREATEREQUAL parameters look switched, expected value should be first param",
                callExpr->getExprLoc())
                << callExpr->getSourceRange();
    }
    return true;
}
// copied from stringconcat.cxx
Expr const * stripConstructor(Expr const * expr) {
    auto e0 = expr;
    auto const e1 = dyn_cast<CXXFunctionalCastExpr>(e0);
    if (e1 != nullptr) {
        e0 = e1->getSubExpr()->IgnoreParenImpCasts();
    }
    auto const e2 = dyn_cast<CXXBindTemporaryExpr>(e0);
    if (e2 == nullptr) {
        return expr;
    }
    auto const e3 = dyn_cast<CXXConstructExpr>(
        e2->getSubExpr()->IgnoreParenImpCasts());
    if (e3 == nullptr) {
        return expr;
    }
    auto qt = loplugin::DeclCheck(e3->getConstructor());
    if (!((qt.MemberFunction().Class("OString").Namespace("rtl")
           .GlobalNamespace())
          || (qt.MemberFunction().Class("OUString").Namespace("rtl")
              .GlobalNamespace())))
    {
        return expr;
    }
    if (e3->getNumArgs() != 2) {
        return expr;
    }
    return e3->getArg(0)->IgnoreParenImpCasts();
}
bool CppunitAssertEquals::isCompileTimeConstant(Expr const * expr)
{
    if (expr->isCXX11ConstantExpr(compiler.getASTContext()))
        return true;
    // is string literal ?
    expr = expr->IgnoreParenImpCasts();
    expr = stripConstructor(expr);
    return isa<clang::StringLiteral>(expr);
}
void CppunitAssertEquals::checkExpr(
    SourceRange range, StringRef name, Expr const * expr, bool negated)
{
    if (auto const e = dyn_cast<UnaryOperator>(expr)) {
        if (e->getOpcode() == UO_LNot) {
            checkExpr(
                range, name, e->getSubExpr()->IgnoreParenImpCasts(), !negated);
        }
        return;
    }
    if (auto const e = dyn_cast<BinaryOperator>(expr)) {
        auto const op = e->getOpcode();
        if ((!negated && op == BO_EQ) || (negated && op == BO_NE)) {
            reportEquals(range, name, op == BO_NE, e->getLHS(), e->getRHS());
            return;
        }
        if ((!negated && op == BO_LAnd) || (negated && op == BO_LOr)) {
            report(
                DiagnosticsEngine::Warning,
                "rather split into two %0", e->getExprLoc())
                << name << range;
            return;
        }
        return;
    }
    if (auto const e = dyn_cast<CXXOperatorCallExpr>(expr)) {
        auto const op = e->getOperator();
        if ((!negated && op == OO_EqualEqual)
            || (negated && op == OO_ExclaimEqual))
        {
            reportEquals(range, name, op == OO_ExclaimEqual, e->getArg(0), e->getArg(1));
            return;
        }
        return;
    }
}
void CppunitAssertEquals::reportEquals(
    SourceRange range, StringRef name, bool negative, Expr const * lhs, Expr const * rhs)
{
    if (lhs->IgnoreImpCasts()->getType()->isNullPtrType()
        != rhs->IgnoreImpCasts()->getType()->isNullPtrType())
    {
        return;
    }
    report(
        DiagnosticsEngine::Warning,
        ("rather call"
         " %select{CPPUNIT_ASSERT_EQUAL|CPPUNIT_ASSERT_EQUAL_MESSAGE}0 when comparing %1 and %2 (or"
         " rewrite as an explicit operator %select{==|!=}3 call when the"
         " operator itself is the topic)"),
        range.getBegin())
        << (name == "CPPUNIT_ASSERT_MESSAGE") << lhs->IgnoreImpCasts()->getType()
        << rhs->IgnoreImpCasts()->getType() << negative << range;
}
loplugin::Plugin::Registration< CppunitAssertEquals > cppunitassertequals("cppunitassertequals");
} // namespace
#endif // LO_CLANG_SHARED_PLUGINS
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 |