summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2020-06-26 14:53:24 +0200
committerStephan Bergmann <sbergman@redhat.com>2020-06-26 17:31:53 +0200
commit311fe58df8cbdc6550f9b6ed89bd7f14d61c50a9 (patch)
tree416162927f59e9b6040f2f494b91908f3d219ec5
parent0960a8c5ce48adb1bc7bd434ed5242b51ce72149 (diff)
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@redhat.com>
-rw-r--r--compilerplugins/clang/elidestringvar.cxx18
-rw-r--r--compilerplugins/clang/test/elidestringvar.cxx39
-rw-r--r--solenv/CompilerTest_compilerplugins_clang.mk1
3 files changed, 57 insertions, 1 deletions
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@+1 {{literal OUString variable defined here [loplugin:elidestringvar]}}
+ OUString s2('a');
+ // expected-note@+1 {{literal OUString variable defined here [loplugin:elidestringvar]}}
+ OUString s3(u'a');
+ // expected-note@+1 {{literal OUString variable defined here [loplugin:elidestringvar]}}
+ OUString s4 = OUStringLiteral("a");
+ switch (n)
+ {
+ case 1:
+ return s1;
+ case 2:
+ // expected-error@+1 {{replace single use of literal OUString variable with a literal [loplugin:elidestringvar]}}
+ return s2;
+ case 3:
+ // expected-error@+1 {{replace single use of literal OUString variable with a literal [loplugin:elidestringvar]}}
+ return s3;
+ default:
+ // expected-error@+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 \