stringconcat.cxx 5.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* -*- 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 "plugin.hxx"
11
#include "check.hxx"
12 13 14

namespace {

15
Expr const * stripCtor(Expr const * expr) {
16 17 18
    auto e1 = expr;
    if (auto const e = dyn_cast<CXXFunctionalCastExpr>(e1)) {
        e1 = e->getSubExpr()->IgnoreParenImpCasts();
19
    }
20 21 22 23
    if (auto const e = dyn_cast<CXXBindTemporaryExpr>(e1)) {
        e1 = e->getSubExpr()->IgnoreParenImpCasts();
    }
    auto const e2 = dyn_cast<CXXConstructExpr>(e1);
24 25 26
    if (e2 == nullptr) {
        return expr;
    }
27 28 29 30 31
    auto qt = loplugin::DeclCheck(e2->getConstructor());
    if (qt.MemberFunction().Struct("OStringLiteral").Namespace("rtl").GlobalNamespace()) {
        if (e2->getNumArgs() == 1) {
            return e2->getArg(0)->IgnoreParenImpCasts();
        }
32 33
        return expr;
    }
Stephan Bergmann's avatar
Stephan Bergmann committed
34 35 36
    if (!((qt.MemberFunction().Class("OString").Namespace("rtl")
           .GlobalNamespace())
          || (qt.MemberFunction().Class("OUString").Namespace("rtl")
37 38
              .GlobalNamespace())
          || qt.MemberFunction().Struct("OUStringLiteral").Namespace("rtl").GlobalNamespace()))
Stephan Bergmann's avatar
Stephan Bergmann committed
39
    {
40 41
        return expr;
    }
42
    if (e2->getNumArgs() != 2) {
43 44
        return expr;
    }
45
    return e2->getArg(0)->IgnoreParenImpCasts();
46 47
}

48
class StringConcat:
49
    public loplugin::FilteringPlugin<StringConcat>
50 51
{
public:
52
    explicit StringConcat(loplugin::InstantiationData const & data):
53
        FilteringPlugin(data) {}
54 55 56 57 58

    void run() override
    { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }

    bool VisitCallExpr(CallExpr const * expr);
59 60 61

private:
    bool isStringLiteral(Expr const * expr);
62 63 64 65 66 67 68 69 70 71 72 73 74 75
};

bool StringConcat::VisitCallExpr(CallExpr const * expr) {
    if (ignoreLocation(expr)) {
        return true;
    }
    FunctionDecl const * fdecl = expr->getDirectCallee();
    if (fdecl == nullptr) {
        return true;
    }
    OverloadedOperatorKind oo = fdecl->getOverloadedOperator();
    if ((oo != OverloadedOperatorKind::OO_Plus
         && oo != OverloadedOperatorKind::OO_LessLess)
        || fdecl->getNumParams() != 2 || expr->getNumArgs() != 2
76
        || !isStringLiteral(expr->getArg(1)->IgnoreParenImpCasts()))
77 78 79
    {
        return true;
    }
80 81
    SourceLocation leftLoc;
    auto const leftExpr = expr->getArg(0)->IgnoreParenImpCasts();
82
    if (isStringLiteral(leftExpr)) {
83
        leftLoc = compat::getBeginLoc(leftExpr);
84 85 86 87 88 89 90 91 92 93 94 95 96
    } else {
        CallExpr const * left = dyn_cast<CallExpr>(leftExpr);
        if (left == nullptr) {
            return true;
        }
        FunctionDecl const * ldecl = left->getDirectCallee();
        if (ldecl == nullptr) {
            return true;
        }
        OverloadedOperatorKind loo = ldecl->getOverloadedOperator();
        if ((loo != OverloadedOperatorKind::OO_Plus
             && loo != OverloadedOperatorKind::OO_LessLess)
            || ldecl->getNumParams() != 2 || left->getNumArgs() != 2
97
            || !isStringLiteral(left->getArg(1)->IgnoreParenImpCasts()))
98 99 100
        {
            return true;
        }
101
        leftLoc = compat::getBeginLoc(left->getArg(1));
102 103
    }
    StringRef name {
104
        getFileNameOfSpellingLoc(
105
            compiler.getSourceManager().getSpellingLoc(compat::getBeginLoc(expr))) };
106
    if (loplugin::isSamePathname(
107 108
            name, SRCDIR "/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx")
        || loplugin::isSamePathname(
109 110 111
            name, SRCDIR "/sal/qa/rtl/strings/test_ostring_concat.cxx")
        || loplugin::isSamePathname(
            name, SRCDIR "/sal/qa/rtl/strings/test_oustring_concat.cxx"))
112 113 114 115 116 117 118 119 120
    {
        return true;
    }
    CXXOperatorCallExpr const * op = dyn_cast<CXXOperatorCallExpr>(expr);
    report(
        DiagnosticsEngine::Warning,
        "replace '%0' between string literals with juxtaposition",
        op == nullptr ? expr->getExprLoc() : op->getOperatorLoc())
        << (oo == OverloadedOperatorKind::OO_Plus ? "+" : "<<")
121
        << SourceRange(leftLoc, compat::getEndLoc(expr->getArg(1)));
122 123 124
    return true;
}

125 126 127 128 129 130 131 132
bool StringConcat::isStringLiteral(Expr const * expr) {
    expr = stripCtor(expr);
    if (!isa<clang::StringLiteral>(expr)) {
        return false;
    }
    // OSL_THIS_FUNC may be defined as "" in include/osl/diagnose.h, so don't
    // warn about expressions like 'SAL_INFO(..., OSL_THIS_FUNC << ":")' or
    // 'OUString(OSL_THIS_FUNC) + ":"':
133
    auto loc = compat::getBeginLoc(expr);
134 135 136 137 138 139 140 141 142
    while (compiler.getSourceManager().isMacroArgExpansion(loc)) {
        loc = compiler.getSourceManager().getImmediateMacroCallerLoc(loc);
    }
    return !compiler.getSourceManager().isMacroBodyExpansion(loc)
        || (Lexer::getImmediateMacroName(
                loc, compiler.getSourceManager(), compiler.getLangOpts())
            != "OSL_THIS_FUNC");
}

143 144 145 146 147
loplugin::Plugin::Registration<StringConcat> X("stringconcat");

}

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