From b6778e921f72ca0ef49b4e18f3f990a397b9c491 Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Fri, 19 May 2017 17:20:05 +0200 Subject: loplugin:stringcopy Change-Id: Ib04ef019996888166c25ad140b7c718a173a782a --- compilerplugins/clang/stringcopy.cxx | 53 +++++++++++++++++++++++++++++++ compilerplugins/clang/test/stringcopy.cxx | 23 ++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 compilerplugins/clang/stringcopy.cxx create mode 100644 compilerplugins/clang/test/stringcopy.cxx (limited to 'compilerplugins') 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, 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 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: */ -- cgit