2019-09-27 21:41:42 +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/.
|
|
|
|
*/
|
|
|
|
#ifndef LO_CLANG_SHARED_PLUGINS
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
#include "plugin.hxx"
|
|
|
|
#include "check.hxx"
|
|
|
|
#include "clang/AST/CXXInheritance.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
Look for repeated addition to OUString/OString.
|
|
|
|
|
|
|
|
Eg.
|
|
|
|
OUString x = "xxx";
|
|
|
|
x += b;
|
|
|
|
|
|
|
|
which can be simplified to
|
|
|
|
x = "xxx" + b
|
|
|
|
|
|
|
|
which is more efficient, because of the OUStringConcat magic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class StringAdd : public loplugin::FilteringPlugin<StringAdd>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit StringAdd(loplugin::InstantiationData const& data)
|
|
|
|
: FilteringPlugin(data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool preRun() override
|
|
|
|
{
|
|
|
|
std::string fn(handler.getMainFileName());
|
|
|
|
loplugin::normalizeDotDotInFilePath(fn);
|
2019-10-15 20:24:10 +02:00
|
|
|
if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sal/qa/rtl/oustring/"))
|
|
|
|
return false;
|
|
|
|
if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sal/qa/rtl/oustringbuffer/"))
|
|
|
|
return false;
|
|
|
|
if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sal/qa/rtl/strings/"))
|
|
|
|
return false;
|
|
|
|
if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sal/qa/OStringBuffer/"))
|
2019-09-27 21:41:42 +02:00
|
|
|
return false;
|
|
|
|
// there is an ifdef here, but my check is not working, not sure why
|
|
|
|
if (fn == SRCDIR "/pyuno/source/module/pyuno_runtime.cxx")
|
|
|
|
return false;
|
|
|
|
// TODO the += depends on the result of the preceding assign, so can't merge
|
|
|
|
if (fn == SRCDIR "/editeng/source/misc/svxacorr.cxx")
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void run() override
|
|
|
|
{
|
|
|
|
if (!preRun())
|
|
|
|
return;
|
|
|
|
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitCompoundStmt(CompoundStmt const*);
|
2019-10-13 08:47:47 +02:00
|
|
|
bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr const*);
|
2019-09-27 21:41:42 +02:00
|
|
|
|
|
|
|
private:
|
2019-10-30 20:27:26 +01:00
|
|
|
enum class Summands
|
|
|
|
{
|
|
|
|
OnlyCompileTimeConstants,
|
|
|
|
OnlySideEffectFree,
|
|
|
|
SideEffect
|
|
|
|
};
|
|
|
|
|
|
|
|
struct VarDeclAndSummands
|
2019-10-28 10:35:49 +02:00
|
|
|
{
|
|
|
|
const VarDecl* varDecl;
|
2019-10-30 20:27:26 +01:00
|
|
|
Summands summands;
|
2019-10-28 10:35:49 +02:00
|
|
|
};
|
|
|
|
|
2019-10-30 20:27:26 +01:00
|
|
|
VarDeclAndSummands findAssignOrAdd(Stmt const*);
|
|
|
|
bool checkForCompoundAssign(Stmt const* stmt1, Stmt const* stmt2, VarDeclAndSummands& varDecl);
|
2019-10-13 08:47:47 +02:00
|
|
|
|
|
|
|
Expr const* ignore(Expr const*);
|
2019-09-27 21:41:42 +02:00
|
|
|
bool isSideEffectFree(Expr const*);
|
2019-10-28 10:35:49 +02:00
|
|
|
bool isCompileTimeConstant(Expr const*);
|
2019-09-27 21:41:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
bool StringAdd::VisitCompoundStmt(CompoundStmt const* compoundStmt)
|
|
|
|
{
|
|
|
|
if (ignoreLocation(compoundStmt))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto it = compoundStmt->body_begin();
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (it == compoundStmt->body_end())
|
|
|
|
break;
|
2019-10-30 20:27:26 +01:00
|
|
|
VarDeclAndSummands foundVar = findAssignOrAdd(*it);
|
2019-09-27 21:41:42 +02:00
|
|
|
// reference types have slightly weird behaviour
|
2019-10-28 10:35:49 +02:00
|
|
|
if (foundVar.varDecl && !foundVar.varDecl->getType()->isReferenceType())
|
2019-09-27 21:41:42 +02:00
|
|
|
{
|
|
|
|
auto stmt1 = *it;
|
|
|
|
++it;
|
2019-10-30 20:27:26 +01:00
|
|
|
while (it != compoundStmt->body_end())
|
|
|
|
{
|
|
|
|
if (!checkForCompoundAssign(stmt1, *it, foundVar))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stmt1 = *it;
|
|
|
|
++it;
|
|
|
|
}
|
2019-09-27 21:41:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-30 20:27:26 +01:00
|
|
|
StringAdd::VarDeclAndSummands StringAdd::findAssignOrAdd(Stmt const* stmt)
|
2019-09-27 21:41:42 +02:00
|
|
|
{
|
|
|
|
if (auto exprCleanup = dyn_cast<ExprWithCleanups>(stmt))
|
|
|
|
stmt = exprCleanup->getSubExpr();
|
|
|
|
if (auto switchCase = dyn_cast<SwitchCase>(stmt))
|
|
|
|
stmt = switchCase->getSubStmt();
|
|
|
|
|
|
|
|
if (auto declStmt = dyn_cast<DeclStmt>(stmt))
|
|
|
|
if (declStmt->isSingleDecl())
|
|
|
|
if (auto varDeclLHS = dyn_cast_or_null<VarDecl>(declStmt->getSingleDecl()))
|
|
|
|
{
|
|
|
|
auto tc = loplugin::TypeCheck(varDeclLHS->getType());
|
|
|
|
if (!tc.Class("OUString").Namespace("rtl").GlobalNamespace()
|
|
|
|
&& !tc.Class("OString").Namespace("rtl").GlobalNamespace())
|
2019-10-28 10:35:49 +02:00
|
|
|
return {};
|
2019-09-27 21:41:42 +02:00
|
|
|
if (varDeclLHS->getStorageDuration() == SD_Static)
|
2019-10-28 10:35:49 +02:00
|
|
|
return {};
|
2019-09-27 21:41:42 +02:00
|
|
|
if (!varDeclLHS->hasInit())
|
2019-10-28 10:35:49 +02:00
|
|
|
return {};
|
2019-10-30 20:27:26 +01:00
|
|
|
return { varDeclLHS, (isCompileTimeConstant(varDeclLHS->getInit())
|
|
|
|
? Summands::OnlyCompileTimeConstants
|
|
|
|
: (isSideEffectFree(varDeclLHS->getInit())
|
|
|
|
? Summands::OnlySideEffectFree
|
|
|
|
: Summands::SideEffect)) };
|
2019-09-27 21:41:42 +02:00
|
|
|
}
|
|
|
|
if (auto operatorCall = dyn_cast<CXXOperatorCallExpr>(stmt))
|
|
|
|
if (operatorCall->getOperator() == OO_Equal || operatorCall->getOperator() == OO_PlusEqual)
|
|
|
|
if (auto declRefExprLHS = dyn_cast<DeclRefExpr>(ignore(operatorCall->getArg(0))))
|
|
|
|
if (auto varDeclLHS = dyn_cast<VarDecl>(declRefExprLHS->getDecl()))
|
|
|
|
{
|
|
|
|
auto tc = loplugin::TypeCheck(varDeclLHS->getType());
|
|
|
|
if (!tc.Class("OUString").Namespace("rtl").GlobalNamespace()
|
|
|
|
&& !tc.Class("OString").Namespace("rtl").GlobalNamespace())
|
2019-10-28 10:35:49 +02:00
|
|
|
return {};
|
2019-09-27 21:41:42 +02:00
|
|
|
auto rhs = operatorCall->getArg(1);
|
2019-10-30 20:27:26 +01:00
|
|
|
return { varDeclLHS,
|
|
|
|
(isCompileTimeConstant(rhs)
|
|
|
|
? Summands::OnlyCompileTimeConstants
|
|
|
|
: (isSideEffectFree(rhs) ? Summands::OnlySideEffectFree
|
|
|
|
: Summands::SideEffect)) };
|
2019-09-27 21:41:42 +02:00
|
|
|
}
|
2019-10-28 10:35:49 +02:00
|
|
|
return {};
|
2019-09-27 21:41:42 +02:00
|
|
|
}
|
|
|
|
|
2019-10-30 20:27:26 +01:00
|
|
|
bool StringAdd::checkForCompoundAssign(Stmt const* stmt1, Stmt const* stmt2,
|
|
|
|
VarDeclAndSummands& varDecl)
|
2019-09-27 21:41:42 +02:00
|
|
|
{
|
|
|
|
// OString additions are frequently wrapped in these
|
|
|
|
if (auto exprCleanup = dyn_cast<ExprWithCleanups>(stmt2))
|
|
|
|
stmt2 = exprCleanup->getSubExpr();
|
|
|
|
if (auto switchCase = dyn_cast<SwitchCase>(stmt2))
|
|
|
|
stmt2 = switchCase->getSubStmt();
|
|
|
|
auto operatorCall = dyn_cast<CXXOperatorCallExpr>(stmt2);
|
|
|
|
if (!operatorCall)
|
2019-10-30 20:27:26 +01:00
|
|
|
return false;
|
2019-09-27 21:41:42 +02:00
|
|
|
if (operatorCall->getOperator() != OO_PlusEqual)
|
2019-10-30 20:27:26 +01:00
|
|
|
return false;
|
2019-09-27 21:41:42 +02:00
|
|
|
auto declRefExprLHS = dyn_cast<DeclRefExpr>(ignore(operatorCall->getArg(0)));
|
|
|
|
if (!declRefExprLHS)
|
2019-10-30 20:27:26 +01:00
|
|
|
return false;
|
2019-10-28 10:35:49 +02:00
|
|
|
if (declRefExprLHS->getDecl() != varDecl.varDecl)
|
2019-10-30 20:27:26 +01:00
|
|
|
return false;
|
2019-10-28 10:35:49 +02:00
|
|
|
// if either side is a compile-time-constant, then we don't care about
|
|
|
|
// side-effects
|
2019-09-27 21:41:42 +02:00
|
|
|
auto rhs = operatorCall->getArg(1);
|
2019-10-30 20:27:26 +01:00
|
|
|
auto const ctcRhs = isCompileTimeConstant(rhs);
|
|
|
|
if (!ctcRhs)
|
|
|
|
{
|
|
|
|
auto const sefRhs = isSideEffectFree(rhs);
|
|
|
|
auto const oldSummands = varDecl.summands;
|
|
|
|
varDecl.summands = sefRhs ? Summands::OnlySideEffectFree : Summands::SideEffect;
|
|
|
|
if (oldSummands != Summands::OnlyCompileTimeConstants
|
|
|
|
&& (oldSummands == Summands::SideEffect || !sefRhs))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-09-27 21:41:42 +02:00
|
|
|
// if we cross a #ifdef boundary
|
2019-10-23 17:53:43 +02:00
|
|
|
if (containsPreprocessingConditionalInclusion(
|
|
|
|
SourceRange(stmt1->getSourceRange().getBegin(), stmt2->getSourceRange().getEnd())))
|
2019-10-30 20:27:26 +01:00
|
|
|
{
|
|
|
|
varDecl.summands
|
|
|
|
= ctcRhs ? Summands::OnlyCompileTimeConstants
|
|
|
|
: isSideEffectFree(rhs) ? Summands::OnlySideEffectFree : Summands::SideEffect;
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-27 21:41:42 +02:00
|
|
|
report(DiagnosticsEngine::Warning, "simplify by merging with the preceding assignment",
|
|
|
|
compat::getBeginLoc(stmt2))
|
|
|
|
<< stmt2->getSourceRange();
|
2019-10-30 20:27:26 +01:00
|
|
|
return true;
|
2019-09-27 21:41:42 +02:00
|
|
|
}
|
|
|
|
|
2019-10-13 08:47:47 +02:00
|
|
|
// Check for generating temporaries when adding strings
|
|
|
|
//
|
|
|
|
bool StringAdd::VisitCXXOperatorCallExpr(CXXOperatorCallExpr const* operatorCall)
|
|
|
|
{
|
|
|
|
if (ignoreLocation(operatorCall))
|
|
|
|
return true;
|
|
|
|
if (operatorCall->getOperator() != OO_Plus)
|
|
|
|
return true;
|
|
|
|
auto tc = loplugin::TypeCheck(operatorCall->getType()->getUnqualifiedDesugaredType());
|
|
|
|
if (!tc.Struct("OUStringConcat").Namespace("rtl").GlobalNamespace()
|
|
|
|
&& !tc.Struct("OStringConcat").Namespace("rtl").GlobalNamespace()
|
|
|
|
&& !tc.Class("OUString").Namespace("rtl").GlobalNamespace()
|
|
|
|
&& !tc.Class("OString").Namespace("rtl").GlobalNamespace())
|
|
|
|
return true;
|
|
|
|
|
2019-10-28 10:20:08 +01:00
|
|
|
auto check = [operatorCall, this](unsigned arg) {
|
2019-10-15 16:45:57 +02:00
|
|
|
auto const e
|
|
|
|
= dyn_cast<CXXFunctionalCastExpr>(operatorCall->getArg(arg)->IgnoreParenImpCasts());
|
2019-10-15 12:11:34 +02:00
|
|
|
if (e == nullptr)
|
|
|
|
return;
|
|
|
|
auto tc3 = loplugin::TypeCheck(e->getType());
|
2019-10-13 08:47:47 +02:00
|
|
|
if (!tc3.Class("OUString").Namespace("rtl").GlobalNamespace()
|
|
|
|
&& !tc3.Class("OString").Namespace("rtl").GlobalNamespace())
|
|
|
|
return;
|
2019-10-15 16:45:57 +02:00
|
|
|
report(DiagnosticsEngine::Warning,
|
|
|
|
("avoid constructing %0 from %1 on %select{L|R}2HS of + (where %select{R|L}2HS is of"
|
|
|
|
" type %3)"),
|
2019-10-15 12:11:34 +02:00
|
|
|
compat::getBeginLoc(e))
|
2019-10-15 16:45:57 +02:00
|
|
|
<< e->getType().getLocalUnqualifiedType() << e->getSubExprAsWritten()->getType() << arg
|
|
|
|
<< operatorCall->getArg(1 - arg)->IgnoreImpCasts()->getType() << e->getSourceRange();
|
2019-10-13 08:47:47 +02:00
|
|
|
};
|
|
|
|
|
2019-10-15 16:45:57 +02:00
|
|
|
check(0);
|
|
|
|
check(1);
|
2019-10-13 08:47:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-27 21:41:42 +02:00
|
|
|
Expr const* StringAdd::ignore(Expr const* expr)
|
|
|
|
{
|
|
|
|
return compat::IgnoreImplicit(compat::IgnoreImplicit(expr)->IgnoreParens());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StringAdd::isSideEffectFree(Expr const* expr)
|
|
|
|
{
|
|
|
|
expr = ignore(expr);
|
2019-10-13 08:47:47 +02:00
|
|
|
// I don't think the OUStringAppend functionality can handle this efficiently
|
|
|
|
if (isa<ConditionalOperator>(expr))
|
|
|
|
return false;
|
2019-09-27 21:41:42 +02:00
|
|
|
// Multiple statements have a well defined evaluation order (sequence points between them)
|
|
|
|
// but a single expression may be evaluated in arbitrary order;
|
|
|
|
// if there are side effects in one of the sub-expressions that have an effect on another subexpression,
|
|
|
|
// the result may be incorrect, and you don't necessarily notice in tests because the order is compiler-dependent.
|
|
|
|
// for example see commit afd743141f7a7dd05914d0872c9afe079f16fe0c where such a refactoring introduced such a bug.
|
|
|
|
// So only consider simple RHS expressions.
|
|
|
|
if (!expr->HasSideEffects(compiler.getASTContext()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// check for chained adds which are side-effect free
|
|
|
|
if (auto operatorCall = dyn_cast<CXXOperatorCallExpr>(expr))
|
|
|
|
{
|
|
|
|
auto op = operatorCall->getOperator();
|
|
|
|
if (op == OO_PlusEqual || op == OO_Plus)
|
|
|
|
if (isSideEffectFree(operatorCall->getArg(0))
|
|
|
|
&& isSideEffectFree(operatorCall->getArg(1)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto callExpr = dyn_cast<CallExpr>(expr))
|
|
|
|
{
|
2019-10-13 08:47:47 +02:00
|
|
|
// check for calls through OUString::number/OUString::unacquired
|
2019-09-27 21:41:42 +02:00
|
|
|
if (auto calleeMethodDecl = dyn_cast_or_null<CXXMethodDecl>(callExpr->getCalleeDecl()))
|
2020-12-11 17:44:34 +01:00
|
|
|
if (calleeMethodDecl)
|
2019-09-27 21:41:42 +02:00
|
|
|
{
|
2020-12-11 17:44:34 +01:00
|
|
|
if (calleeMethodDecl->getIdentifier())
|
2019-10-13 08:47:47 +02:00
|
|
|
{
|
2020-12-11 17:44:34 +01:00
|
|
|
auto name = calleeMethodDecl->getName();
|
|
|
|
if (callExpr->getNumArgs() > 0
|
|
|
|
&& (name == "number" || name == "unacquired" || name == "boolean"
|
|
|
|
|| name == "copy"))
|
2019-10-13 08:47:47 +02:00
|
|
|
{
|
2020-12-11 17:44:34 +01:00
|
|
|
auto tc = loplugin::TypeCheck(calleeMethodDecl->getParent());
|
|
|
|
if (tc.Class("OUString") || tc.Class("OString"))
|
|
|
|
{
|
|
|
|
if (isSideEffectFree(callExpr->getArg(0)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (auto const d = dyn_cast<CXXConversionDecl>(calleeMethodDecl))
|
|
|
|
{
|
|
|
|
if (loplugin::TypeCheck(d->getConversionType())
|
|
|
|
.ClassOrStruct("basic_string_view")
|
|
|
|
.StdNamespace())
|
|
|
|
{
|
|
|
|
auto const tc = loplugin::TypeCheck(calleeMethodDecl->getParent());
|
|
|
|
if (tc.Class("OUString").Namespace("rtl").GlobalNamespace()
|
|
|
|
|| tc.Class("OString").Namespace("rtl").GlobalNamespace())
|
|
|
|
{
|
|
|
|
if (isSideEffectFree(callExpr->getCallee()))
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-13 08:47:47 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-27 21:41:42 +02:00
|
|
|
}
|
|
|
|
if (auto calleeFunctionDecl = dyn_cast_or_null<FunctionDecl>(callExpr->getCalleeDecl()))
|
|
|
|
if (calleeFunctionDecl && calleeFunctionDecl->getIdentifier())
|
|
|
|
{
|
|
|
|
auto name = calleeFunctionDecl->getName();
|
|
|
|
// check for calls through OUStringToOString
|
|
|
|
if (name == "OUStringToOString" || name == "OStringToOUString")
|
|
|
|
if (isSideEffectFree(callExpr->getArg(0)))
|
|
|
|
return true;
|
2020-07-06 03:39:12 +02:00
|
|
|
// allowlist some known-safe methods
|
2019-10-13 08:47:47 +02:00
|
|
|
if (name.endswith("ResId") || name == "GetXMLToken")
|
2019-09-27 21:41:42 +02:00
|
|
|
if (isSideEffectFree(callExpr->getArg(0)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sometimes we have a constructor call on the RHS
|
|
|
|
if (auto constructExpr = dyn_cast<CXXConstructExpr>(expr))
|
|
|
|
{
|
|
|
|
auto dc = loplugin::DeclCheck(constructExpr->getConstructor());
|
|
|
|
if (dc.MemberFunction().Class("OUString") || dc.MemberFunction().Class("OString"))
|
|
|
|
if (constructExpr->getNumArgs() == 0 || isSideEffectFree(constructExpr->getArg(0)))
|
|
|
|
return true;
|
|
|
|
// Expr::HasSideEffects does not like stuff that passes through OUStringLiteral
|
|
|
|
auto dc2 = loplugin::DeclCheck(constructExpr->getConstructor()->getParent());
|
Turn OUStringLiteral into a consteval'ed, static-refcound rtl_uString
...from which an OUString can cheaply be instantiated. This is the OUString
equivalent of 4b9e440c51be3e40326bc90c33ae69885bfb51e4 "Turn OStringLiteral into
a consteval'ed, static-refcound rtl_String". Most remarks about that commit
apply here too (this commit is just substantially bigger and a bit more
complicated because there were so much more uses of OUStringLiteral than of
OStringLiteral):
The one downside is that OUStringLiteral now needs to be a template abstracting
over the string length. But any uses for which that is a problem (e.g., as the
element type of a container that would no longer be homogeneous, or in the
signature of a function that shall not be turned into a template for one reason
or another) can be replaced with std::u16string_view, without loss of efficiency
compared to the original OUStringLiteral, and without loss of expressivity.
The new OUStringLiteral ctor code would probably not be very efficient if it
were ever executed at runtime, but it is intended to be only executed at compile
time. Where available, C++20 "consteval" is used to statically ensure that.
The intended use of the new OUStringLiteral is in all cases where an
object that shall itself not be an OUString (e.g., because it shall be a
global static variable for which the OUString ctor/dtor would be detrimental at
library load/unload) must be converted to an OUString instance in at least one
place. Other string literal abstractions could use std::u16string_view (or just
plain char16_t const[N]), but interestingly OUStringLiteral might be more
efficient than constexpr std::u16string_view even for such cases, as it should
not need any relocations at library load time. For now, no existing uses of
OUStringLiteral have been changed to some other abstraction (unless technically
necessary as discussed above), and no additional places that would benefit from
OUStringLiteral have been changed to use it.
Global constexpr OUStringLiteral variables defined in an included file would be
somewhat suboptimal, as each translation unit that uses them would create its
own, unshared instance. The envisioned solution is to turn them into static
data members of some class (and there may be a loplugin coming to find and fix
affected places). Another approach that has been taken here in a few cases
where such variables were only used in one .cxx anyway is to move their
definitions from the .hxx into that one .cxx (in turn causing some files to
become empty and get removed completely)---which also silenced some GCC
-Werror=unused-variable if a variable from a .hxx was not used in some .cxx
including it.
To keep individual commits reasonably manageable, some consumers of
OUStringLiteral in rtl/ustrbuf.hxx and rtl/ustring.hxx are left in a somewhat
odd state for now, where they don't take advantage of OUStringLiteral's
equivalence to rtl_uString, but just keep extracting its contents and copy it
elsewhere. In follow-up commits, those consumers should be changed
appropriately, making them treat OUStringLiteral like an rtl_uString or
dropping the OUStringLiteral overload in favor of an existing (and cheap to use
now) OUString overload, etc.
In a similar vein, comparison operators between OUString and std::u16string_view
have been added to the existing plethora of comparison operator overloads. It
would be nice to eventually consolidate them, esp. with the overloads taking
OUStringLiteral and/or char16_t const[N] string literals, but that appears
tricky to get right without introducing new ambiguities. Also, a handful of
places across the code base use comparisons between OUString and OUStringNumber,
which are now ambiguous (converting the OUStringNumber to either OUString or
std::u16string_view). For simplicity, those few places have manually been fixed
for now by adding explicit conversion to std::u16string_view.
Also some compilerplugins code needed to be adapted, and some of the
compilerplugins/test cases have become irrelevant (and have been removed), as
the tested code would no longer compile in the first place.
sal/qa/rtl/strings/test_oustring_concat.cxx documents a workaround for GCC bug
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96878> "Failed class template
argument deduction in unevaluated, parenthesized context". That place, as well
as uses of OUStringLiteral in extensions/source/abpilot/fieldmappingimpl.cxx and
i18npool/source/localedata/localedata.cxx, which have been replaced with
OUString::Concat (and which is arguably a better choice, anyway), also caused
failures with at least Clang 5.0.2 (but would not have caused failures with at
least recent Clang 12 trunk, so appear to be bugs in Clang that have meanwhile
been fixed).
Change-Id: I34174462a28f2000cfeb2d219ffd533a767920b8
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/102222
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2020-09-08 09:48:17 +02:00
|
|
|
if (dc2.Class("OUStringLiteral").Namespace("rtl").GlobalNamespace())
|
2019-09-27 21:41:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// when adding literals, we sometimes get this
|
|
|
|
if (auto functionalCastExpr = dyn_cast<CXXFunctionalCastExpr>(expr))
|
|
|
|
{
|
|
|
|
auto tc = loplugin::TypeCheck(functionalCastExpr->getType());
|
Turn OUStringLiteral into a consteval'ed, static-refcound rtl_uString
...from which an OUString can cheaply be instantiated. This is the OUString
equivalent of 4b9e440c51be3e40326bc90c33ae69885bfb51e4 "Turn OStringLiteral into
a consteval'ed, static-refcound rtl_String". Most remarks about that commit
apply here too (this commit is just substantially bigger and a bit more
complicated because there were so much more uses of OUStringLiteral than of
OStringLiteral):
The one downside is that OUStringLiteral now needs to be a template abstracting
over the string length. But any uses for which that is a problem (e.g., as the
element type of a container that would no longer be homogeneous, or in the
signature of a function that shall not be turned into a template for one reason
or another) can be replaced with std::u16string_view, without loss of efficiency
compared to the original OUStringLiteral, and without loss of expressivity.
The new OUStringLiteral ctor code would probably not be very efficient if it
were ever executed at runtime, but it is intended to be only executed at compile
time. Where available, C++20 "consteval" is used to statically ensure that.
The intended use of the new OUStringLiteral is in all cases where an
object that shall itself not be an OUString (e.g., because it shall be a
global static variable for which the OUString ctor/dtor would be detrimental at
library load/unload) must be converted to an OUString instance in at least one
place. Other string literal abstractions could use std::u16string_view (or just
plain char16_t const[N]), but interestingly OUStringLiteral might be more
efficient than constexpr std::u16string_view even for such cases, as it should
not need any relocations at library load time. For now, no existing uses of
OUStringLiteral have been changed to some other abstraction (unless technically
necessary as discussed above), and no additional places that would benefit from
OUStringLiteral have been changed to use it.
Global constexpr OUStringLiteral variables defined in an included file would be
somewhat suboptimal, as each translation unit that uses them would create its
own, unshared instance. The envisioned solution is to turn them into static
data members of some class (and there may be a loplugin coming to find and fix
affected places). Another approach that has been taken here in a few cases
where such variables were only used in one .cxx anyway is to move their
definitions from the .hxx into that one .cxx (in turn causing some files to
become empty and get removed completely)---which also silenced some GCC
-Werror=unused-variable if a variable from a .hxx was not used in some .cxx
including it.
To keep individual commits reasonably manageable, some consumers of
OUStringLiteral in rtl/ustrbuf.hxx and rtl/ustring.hxx are left in a somewhat
odd state for now, where they don't take advantage of OUStringLiteral's
equivalence to rtl_uString, but just keep extracting its contents and copy it
elsewhere. In follow-up commits, those consumers should be changed
appropriately, making them treat OUStringLiteral like an rtl_uString or
dropping the OUStringLiteral overload in favor of an existing (and cheap to use
now) OUString overload, etc.
In a similar vein, comparison operators between OUString and std::u16string_view
have been added to the existing plethora of comparison operator overloads. It
would be nice to eventually consolidate them, esp. with the overloads taking
OUStringLiteral and/or char16_t const[N] string literals, but that appears
tricky to get right without introducing new ambiguities. Also, a handful of
places across the code base use comparisons between OUString and OUStringNumber,
which are now ambiguous (converting the OUStringNumber to either OUString or
std::u16string_view). For simplicity, those few places have manually been fixed
for now by adding explicit conversion to std::u16string_view.
Also some compilerplugins code needed to be adapted, and some of the
compilerplugins/test cases have become irrelevant (and have been removed), as
the tested code would no longer compile in the first place.
sal/qa/rtl/strings/test_oustring_concat.cxx documents a workaround for GCC bug
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96878> "Failed class template
argument deduction in unevaluated, parenthesized context". That place, as well
as uses of OUStringLiteral in extensions/source/abpilot/fieldmappingimpl.cxx and
i18npool/source/localedata/localedata.cxx, which have been replaced with
OUString::Concat (and which is arguably a better choice, anyway), also caused
failures with at least Clang 5.0.2 (but would not have caused failures with at
least recent Clang 12 trunk, so appear to be bugs in Clang that have meanwhile
been fixed).
Change-Id: I34174462a28f2000cfeb2d219ffd533a767920b8
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/102222
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2020-09-08 09:48:17 +02:00
|
|
|
if (tc.Class("OUStringLiteral").Namespace("rtl").GlobalNamespace())
|
2019-09-27 21:41:42 +02:00
|
|
|
return isSideEffectFree(functionalCastExpr->getSubExpr());
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-28 10:35:49 +02:00
|
|
|
bool StringAdd::isCompileTimeConstant(Expr const* expr)
|
|
|
|
{
|
|
|
|
expr = compat::IgnoreImplicit(expr);
|
|
|
|
if (auto cxxConstructExpr = dyn_cast<CXXConstructExpr>(expr))
|
2019-10-30 20:27:26 +01:00
|
|
|
if (cxxConstructExpr->getNumArgs() > 0)
|
|
|
|
expr = cxxConstructExpr->getArg(0);
|
|
|
|
return isa<clang::StringLiteral>(expr);
|
2019-10-28 10:35:49 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 21:41:42 +02:00
|
|
|
loplugin::Plugin::Registration<StringAdd> stringadd("stringadd");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // LO_CLANG_SHARED_PLUGINS
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|