[Libreoffice-commits] core.git: compilerplugins/clang solenv/CompilerTest_compilerplugins_clang.mk

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Wed Feb 13 12:56:36 UTC 2019


 compilerplugins/clang/stringconcat.cxx       |   30 +++++++-----
 compilerplugins/clang/test/stringconcat.cxx  |   65 +++++++++++++++++++++++++++
 solenv/CompilerTest_compilerplugins_clang.mk |    1 
 3 files changed, 84 insertions(+), 12 deletions(-)

New commits:
commit bf1a511f9c7b3ca8131601ab3b267b8e24891f9a
Author:     Stephan Bergmann <sbergman at redhat.com>
AuthorDate: Wed Feb 13 11:17:10 2019 +0100
Commit:     Stephan Bergmann <sbergman at redhat.com>
CommitDate: Wed Feb 13 13:56:12 2019 +0100

    Make loplugin:stringconcat more robust
    
    Change-Id: Ib8adefd90141007c0422b4c15ba9c2cc5f707f3f
    Reviewed-on: https://gerrit.libreoffice.org/67762
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sbergman at redhat.com>

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 at -1 {{replace '<<' between string literals with juxtaposition}}
+    s1 << "foo" << FOO;
+    // expected-error at -1 {{replace '<<' between string literals with juxtaposition}}
+    s1 << "foo" << foo;
+    s1 << "foo" << OString("foo");
+    // expected-error at -1 {{replace '<<' between string literals with juxtaposition}}
+    s1 << "foo" << OString(FOO);
+    // expected-error at -1 {{replace '<<' between string literals with juxtaposition}}
+    s1 << "foo" << OString(foo);
+    s1 << "foo" << OUString("foo");
+    // expected-error at -1 {{replace '<<' between string literals with juxtaposition}}
+    s1 << "foo" << OUString(FOO);
+    // expected-error at -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 at -1 {{replace '+' between string literals with juxtaposition}}
+    s2 = "foo" + OString(FOO);
+    // expected-error at -1 {{replace '+' between string literals with juxtaposition}}
+    s2 = "foo" + OString(foo);
+    s2 = "foo" + OStringLiteral("foo");
+    // expected-error at -1 {{replace '+' between string literals with juxtaposition}}
+    s2 = "foo" + OStringLiteral(FOO);
+    // expected-error at -1 {{replace '+' between string literals with juxtaposition}}
+    s2 = "foo" + OStringLiteral(foo);
+    OUString s3;
+    s3 = "foo" + OUString("foo");
+    // expected-error at -1 {{replace '+' between string literals with juxtaposition}}
+    s3 = "foo" + OUString(FOO);
+    // expected-error at -1 {{replace '+' between string literals with juxtaposition}}
+    s3 = "foo" + OUString(foo);
+    s3 = "foo" + OUStringLiteral("foo");
+    // expected-error at -1 {{replace '+' between string literals with juxtaposition}}
+    s3 = "foo" + OUStringLiteral(FOO);
+    // expected-error at -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: */
diff --git a/solenv/CompilerTest_compilerplugins_clang.mk b/solenv/CompilerTest_compilerplugins_clang.mk
index 97c541885c75..b5ec80afed04 100644
--- a/solenv/CompilerTest_compilerplugins_clang.mk
+++ b/solenv/CompilerTest_compilerplugins_clang.mk
@@ -58,6 +58,7 @@ $(eval $(call gb_CompilerTest_add_exception_objects,compilerplugins_clang, \
     compilerplugins/clang/test/staticconstfield \
     compilerplugins/clang/test/staticvar \
     compilerplugins/clang/test/stringbuffer \
+    compilerplugins/clang/test/stringconcat \
     compilerplugins/clang/test/stringconstant \
     compilerplugins/clang/test/stringloop \
     compilerplugins/clang/test/unnecessarycatchthrow \


More information about the Libreoffice-commits mailing list