summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2017-05-19 17:20:05 +0200
committerStephan Bergmann <sbergman@redhat.com>2017-05-19 17:20:05 +0200
commitb6778e921f72ca0ef49b4e18f3f990a397b9c491 (patch)
tree96f000f088ea67fcc66b69086dac1385478d4982 /compilerplugins
parent0e4a884b818f6fcb4b1f5809cfd5f4487021ea88 (diff)
loplugin:stringcopy
Change-Id: Ib04ef019996888166c25ad140b7c718a173a782a
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/stringcopy.cxx53
-rw-r--r--compilerplugins/clang/test/stringcopy.cxx23
2 files changed, 76 insertions, 0 deletions
diff --git a/compilerplugins/clang/stringcopy.cxx b/compilerplugins/clang/stringcopy.cxx
new file mode 100644
index 000000000000..64783f5a67d9
--- /dev/null
+++ b/compilerplugins/clang/stringcopy.cxx
@@ -0,0 +1,53 @@
+/* -*- 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 "check.hxx"
+#include "plugin.hxx"
+
+namespace {
+
+class Visitor final:
+ public RecursiveASTVisitor<Visitor>, public loplugin::Plugin
+{
+public:
+ explicit Visitor(InstantiationData const & data): Plugin(data) {}
+
+ bool VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr const * expr) {
+ if (ignoreLocation(expr)) {
+ return true;
+ }
+ auto const t1 = expr->getTypeAsWritten();
+ auto const t2 = expr->getSubExprAsWritten()->getType();
+ if ((t1.getCanonicalType().getTypePtr()
+ != t2.getCanonicalType().getTypePtr())
+ || !(loplugin::TypeCheck(t1).Class("OUString").Namespace("rtl")
+ .GlobalNamespace()))
+ {
+ return true;
+ }
+ report(
+ DiagnosticsEngine::Warning,
+ "redundant copy construction from %0 to %1", expr->getExprLoc())
+ << t2 << t1 << expr->getSourceRange();
+ return true;
+ }
+
+private:
+ void run() override {
+ if (compiler.getLangOpts().CPlusPlus) {
+ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
+ }
+ }
+};
+
+static loplugin::Plugin::Registration<Visitor> reg("stringcopy");
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/compilerplugins/clang/test/stringcopy.cxx b/compilerplugins/clang/test/stringcopy.cxx
new file mode 100644
index 000000000000..c801b7096f74
--- /dev/null
+++ b/compilerplugins/clang/test/stringcopy.cxx
@@ -0,0 +1,23 @@
+/* -*- 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"
+
+int main() {
+ OUString s;
+ (void) OUString(s); // expected-error {{redundant copy construction from 'rtl::OUString' to 'rtl::OUString' [loplugin:stringcopy]}}
+ using T1 = OUString;
+ (void) T1(s); // expected-error {{redundant copy construction from 'rtl::OUString' to 'T1' (aka 'rtl::OUString') [loplugin:stringcopy]}}
+ using T2 = OUString const;
+ (void) T2(s); // expected-error {{redundant copy construction from 'rtl::OUString' to 'T2' (aka 'const rtl::OUString') [loplugin:stringcopy]}}
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */