diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2019-02-13 11:17:10 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2019-02-13 13:56:12 +0100 |
commit | bf1a511f9c7b3ca8131601ab3b267b8e24891f9a (patch) | |
tree | 7b01110efd8c2c83418f901b175b0c0e0088bead /compilerplugins | |
parent | e69843768707146dcd9c294b2cabe721f37e90ac (diff) |
Make loplugin:stringconcat more robust
Change-Id: Ib8adefd90141007c0422b4c15ba9c2cc5f707f3f
Reviewed-on: https://gerrit.libreoffice.org/67762
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/stringconcat.cxx | 30 | ||||
-rw-r--r-- | compilerplugins/clang/test/stringconcat.cxx | 65 |
2 files changed, 83 insertions, 12 deletions
diff --git a/compilerplugins/clang/stringconcat.cxx b/compilerplugins/clang/stringconcat.cxx index caa8e61ddedb..9cef9866db61 100644 --- a/compilerplugins/clang/stringconcat.cxx +++ b/compilerplugins/clang/stringconcat.cxx @@ -13,32 +13,36 @@ namespace { Expr const * stripCtor(Expr const * expr) { - auto e0 = expr; - auto const e1 = dyn_cast<CXXFunctionalCastExpr>(e0); - if (e1 != nullptr) { - e0 = e1->getSubExpr()->IgnoreParenImpCasts(); + auto e1 = expr; + if (auto const e = dyn_cast<CXXFunctionalCastExpr>(e1)) { + e1 = e->getSubExpr()->IgnoreParenImpCasts(); } - auto const e2 = dyn_cast<CXXBindTemporaryExpr>(e0); + 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 const e3 = dyn_cast<CXXConstructExpr>( - e2->getSubExpr()->IgnoreParenImpCasts()); - if (e3 == nullptr) { + auto qt = loplugin::DeclCheck(e2->getConstructor()); + if (qt.MemberFunction().Struct("OStringLiteral").Namespace("rtl").GlobalNamespace()) { + if (e2->getNumArgs() == 1) { + return e2->getArg(0)->IgnoreParenImpCasts(); + } return expr; } - auto qt = loplugin::DeclCheck(e3->getConstructor()); if (!((qt.MemberFunction().Class("OString").Namespace("rtl") .GlobalNamespace()) || (qt.MemberFunction().Class("OUString").Namespace("rtl") - .GlobalNamespace()))) + .GlobalNamespace()) + || qt.MemberFunction().Struct("OUStringLiteral").Namespace("rtl").GlobalNamespace())) { return expr; } - if (e3->getNumArgs() != 2) { + if (e2->getNumArgs() != 2) { return expr; } - return e3->getArg(0)->IgnoreParenImpCasts(); + return e2->getArg(0)->IgnoreParenImpCasts(); } class StringConcat: @@ -100,6 +104,8 @@ bool StringConcat::VisitCallExpr(CallExpr const * expr) { getFileNameOfSpellingLoc( compiler.getSourceManager().getSpellingLoc(compat::getBeginLoc(expr))) }; 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")) diff --git a/compilerplugins/clang/test/stringconcat.cxx b/compilerplugins/clang/test/stringconcat.cxx new file mode 100644 index 000000000000..e2d5a8ce20e3 --- /dev/null +++ b/compilerplugins/clang/test/stringconcat.cxx @@ -0,0 +1,65 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * 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 <sal/config.h> + +#include <ostream> + +#include <rtl/string.hxx> +#include <rtl/ustring.hxx> + +#define FOO "foo" + +void f(std::ostream& s1) +{ + static char const foo[] = "foo"; + s1 << "foo" + << "foo"; + // expected-error@-1 {{replace '<<' between string literals with juxtaposition}} + s1 << "foo" << FOO; + // expected-error@-1 {{replace '<<' between string literals with juxtaposition}} + s1 << "foo" << foo; + s1 << "foo" << OString("foo"); + // expected-error@-1 {{replace '<<' between string literals with juxtaposition}} + s1 << "foo" << OString(FOO); + // expected-error@-1 {{replace '<<' between string literals with juxtaposition}} + s1 << "foo" << OString(foo); + s1 << "foo" << OUString("foo"); + // expected-error@-1 {{replace '<<' between string literals with juxtaposition}} + s1 << "foo" << OUString(FOO); + // expected-error@-1 {{replace '<<' between string literals with juxtaposition}} + s1 << "foo" << OUString(foo); + s1 << "foo" << OUStringLiteral("foo"); //TODO: warn too, OUStringLiteral wrapped in OUString + s1 << "foo" << OUStringLiteral(FOO); //TODO: warn too, OUStringLiteral wrapped in OUString + s1 << "foo" << OUStringLiteral(foo); + OString s2; + s2 = "foo" + OString("foo"); + // expected-error@-1 {{replace '+' between string literals with juxtaposition}} + s2 = "foo" + OString(FOO); + // expected-error@-1 {{replace '+' between string literals with juxtaposition}} + s2 = "foo" + OString(foo); + s2 = "foo" + OStringLiteral("foo"); + // expected-error@-1 {{replace '+' between string literals with juxtaposition}} + s2 = "foo" + OStringLiteral(FOO); + // expected-error@-1 {{replace '+' between string literals with juxtaposition}} + s2 = "foo" + OStringLiteral(foo); + OUString s3; + s3 = "foo" + OUString("foo"); + // expected-error@-1 {{replace '+' between string literals with juxtaposition}} + s3 = "foo" + OUString(FOO); + // expected-error@-1 {{replace '+' between string literals with juxtaposition}} + s3 = "foo" + OUString(foo); + s3 = "foo" + OUStringLiteral("foo"); + // expected-error@-1 {{replace '+' between string literals with juxtaposition}} + s3 = "foo" + OUStringLiteral(FOO); + // expected-error@-1 {{replace '+' between string literals with juxtaposition}} + s3 = "foo" + OUStringLiteral(foo); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |