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

Stephan Bergmann (via logerrit) logerrit at kemper.freedesktop.org
Fri Jun 26 15:32:32 UTC 2020


 compilerplugins/clang/elidestringvar.cxx      |   18 +++++++++++-
 compilerplugins/clang/test/elidestringvar.cxx |   39 ++++++++++++++++++++++++++
 solenv/CompilerTest_compilerplugins_clang.mk  |    1 
 3 files changed, 57 insertions(+), 1 deletion(-)

New commits:
commit 311fe58df8cbdc6550f9b6ed89bd7f14d61c50a9
Author:     Stephan Bergmann <sbergman at redhat.com>
AuthorDate: Fri Jun 26 14:53:24 2020 +0200
Commit:     Stephan Bergmann <sbergman at redhat.com>
CommitDate: Fri Jun 26 17:31:53 2020 +0200

    Improve loplugin:elidestringvar
    
    ...by addressing the follow-up TODO mentioned in the commit message of
    7a3736f908c0ae207567603c61ce0f617339bac0 "New loplugin:elidestringvar"
    (extending it not only to uses with a constant sal_Unicode, but also to uses
    with OUStringLiteral).
    
    (All necessary changes have been made in preceding "Upcoming improved
    loplugin:elidestringvar" commits.)
    
    Change-Id: Ib0000ef9c4a1dad52124dfd039dd936cf7e3ba3f
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/97226
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sbergman at redhat.com>

diff --git a/compilerplugins/clang/elidestringvar.cxx b/compilerplugins/clang/elidestringvar.cxx
index 4ca28d0d881d..d091f8175783 100644
--- a/compilerplugins/clang/elidestringvar.cxx
+++ b/compilerplugins/clang/elidestringvar.cxx
@@ -67,7 +67,7 @@ public:
                 continue;
             }
             report(DiagnosticsEngine::Warning,
-                   "replace single use of literal OUString variable with the literal",
+                   "replace single use of literal OUString variable with a literal",
                    (*var.second.singleUse)->getExprLoc())
                 << (*var.second.singleUse)->getSourceRange();
             report(DiagnosticsEngine::Note, "literal OUString variable defined here",
@@ -121,6 +121,22 @@ public:
         {
             case 0:
                 break;
+            case 1:
+            {
+                auto const e2 = e1->getArg(0);
+                if (loplugin::TypeCheck(e2->getType())
+                        .Struct("OUStringLiteral")
+                        .Namespace("rtl")
+                        .GlobalNamespace())
+                {
+                    break;
+                }
+                if (e2->isIntegerConstantExpr(compiler.getASTContext()))
+                {
+                    break;
+                }
+                return true;
+            }
             case 2:
             {
                 auto const e2 = e1->getArg(1);
diff --git a/compilerplugins/clang/test/elidestringvar.cxx b/compilerplugins/clang/test/elidestringvar.cxx
new file mode 100644
index 000000000000..e7a5f40a8eae
--- /dev/null
+++ b/compilerplugins/clang/test/elidestringvar.cxx
@@ -0,0 +1,39 @@
+/* -*- 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 "rtl/ustring.hxx"
+
+OUString f(sal_Unicode c, int n)
+{
+    OUString s1(c);
+    // expected-note at +1 {{literal OUString variable defined here [loplugin:elidestringvar]}}
+    OUString s2('a');
+    // expected-note at +1 {{literal OUString variable defined here [loplugin:elidestringvar]}}
+    OUString s3(u'a');
+    // expected-note at +1 {{literal OUString variable defined here [loplugin:elidestringvar]}}
+    OUString s4 = OUStringLiteral("a");
+    switch (n)
+    {
+        case 1:
+            return s1;
+        case 2:
+            // expected-error at +1 {{replace single use of literal OUString variable with a literal [loplugin:elidestringvar]}}
+            return s2;
+        case 3:
+            // expected-error at +1 {{replace single use of literal OUString variable with a literal [loplugin:elidestringvar]}}
+            return s3;
+        default:
+            // expected-error at +1 {{replace single use of literal OUString variable with a literal [loplugin:elidestringvar]}}
+            return s4;
+    }
+}
+
+/* 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 59a00d31038d..3d457af768df 100644
--- a/solenv/CompilerTest_compilerplugins_clang.mk
+++ b/solenv/CompilerTest_compilerplugins_clang.mk
@@ -31,6 +31,7 @@ $(eval $(call gb_CompilerTest_add_exception_objects,compilerplugins_clang, \
     compilerplugins/clang/test/dbgunhandledexception \
     compilerplugins/clang/test/dodgyswitch \
     compilerplugins/clang/test/doubleconvert \
+    compilerplugins/clang/test/elidestringvar \
     compilerplugins/clang/test/emptyif \
     compilerplugins/clang/test/expressionalwayszero \
     compilerplugins/clang/test/external \


More information about the Libreoffice-commits mailing list