office-gobmx/compilerplugins/clang/stringconcatliterals.cxx
Stephan Bergmann 91ba9654ba Move tools/diagnose_ex.h to comphelper/diagnose_ex.hxx
...so that its TOOLS_WARN_EXCEPTION can be used in
comphelper/source/misc/logging.cxx in a follow-up commit.  (And while at it,
rename from diangose_ex.h to the more appropriate diagnose_ex.hxx.  The
comphelper module is sufficiently low-level for this immediate use case, so use
that at least for now; o3tl might be even more suitable but doesn't have a
Library until now.  Also, for the immediate use case it would have sufficed to
only break DbgGetCaughtException, exceptionToString, TOOLS_WARN_EXCEPTION,
TOOLS_WARN_EXCEPTION_IF, and TOOLS_INFO_EXCEPTION out of
include/tools/diagnose_ex.h into an additional new
include/comphelper/diagnose_ex.hxx, but its probably easier overall to just move
the complete include file as is.)

Change-Id: I9f3222d4ccf1a9ac29d7eb9ba1530d53e2affaee
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138451
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2022-08-18 17:10:19 +02:00

156 lines
5.2 KiB
C++

/* -*- 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 "config_clang.h"
#include "plugin.hxx"
#include "check.hxx"
namespace {
Expr const * stripCtor(Expr const * expr) {
auto e1 = expr;
if (auto const e = dyn_cast<CXXFunctionalCastExpr>(e1)) {
e1 = e->getSubExpr()->IgnoreParenImpCasts();
}
if (auto const e = dyn_cast<CXXBindTemporaryExpr>(e1)) {
e1 = e->getSubExpr()->IgnoreParenImpCasts();
}
auto const e2 = dyn_cast<CXXConstructExpr>(e1);
if (e2 == nullptr) {
return expr;
}
auto qt = loplugin::DeclCheck(e2->getConstructor());
if (qt.MemberFunction().Class("OStringLiteral").Namespace("rtl").GlobalNamespace()
|| qt.MemberFunction().Class("OUStringLiteral").Namespace("rtl").GlobalNamespace())
{
if (e2->getNumArgs() == 1) {
return e2->getArg(0)->IgnoreParenImpCasts();
}
return expr;
}
if (!((qt.MemberFunction().Class("OString").Namespace("rtl")
.GlobalNamespace())
|| (qt.MemberFunction().Class("OUString").Namespace("rtl")
.GlobalNamespace())))
{
return expr;
}
if (e2->getNumArgs() != 2) {
return expr;
}
return e2->getArg(0)->IgnoreParenImpCasts();
}
class StringConcatLiterals:
public loplugin::FilteringPlugin<StringConcatLiterals>
{
public:
explicit StringConcatLiterals(loplugin::InstantiationData const & data):
FilteringPlugin(data) {}
void run() override
{ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
bool VisitCallExpr(CallExpr const * expr);
private:
bool isStringLiteral(Expr const * expr);
};
bool StringConcatLiterals::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
|| !isStringLiteral(expr->getArg(1)->IgnoreParenImpCasts()))
{
return true;
}
SourceLocation leftLoc;
auto const leftExpr = expr->getArg(0)->IgnoreParenImpCasts();
if (isStringLiteral(leftExpr)) {
leftLoc = leftExpr->getBeginLoc();
} 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
|| !isStringLiteral(left->getArg(1)->IgnoreParenImpCasts()))
{
return true;
}
leftLoc = left->getArg(1)->getBeginLoc();
}
// We add an extra " " in the TOOLS_WARN_EXCEPTION macro, which triggers this plugin
if (loplugin::isSamePathname(
getFilenameOfLocation(
compiler.getSourceManager().getSpellingLoc(
compiler.getSourceManager().getImmediateMacroCallerLoc(
compiler.getSourceManager().getImmediateMacroCallerLoc(
compiler.getSourceManager().getImmediateMacroCallerLoc(
expr->getBeginLoc()))))),
SRCDIR "/include/comphelper/diagnose_ex.hxx"))
return true;
StringRef name {
getFilenameOfLocation(
compiler.getSourceManager().getSpellingLoc(expr->getBeginLoc())) };
if (loplugin::isSamePathname(
name, SRCDIR "/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx")
|| loplugin::isSamePathname(
name, SRCDIR "/sal/qa/rtl/strings/test_ostring_concat.cxx")
|| loplugin::isSamePathname(
name, SRCDIR "/sal/qa/rtl/strings/test_oustring_concat.cxx"))
{
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 ? "+" : "<<")
<< SourceRange(leftLoc, expr->getArg(1)->getEndLoc());
return true;
}
bool StringConcatLiterals::isStringLiteral(Expr const * expr) {
expr = stripCtor(expr);
if (!isa<clang::StringLiteral>(expr)) {
return false;
}
return true;
}
loplugin::Plugin::Registration<StringConcatLiterals> stringconcatliterals("stringconcatliterals");
} // namespace
#endif // LO_CLANG_SHARED_PLUGINS
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */