diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2020-12-23 13:16:31 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2020-12-26 14:51:15 +0100 |
commit | a3d0091d185f39eddabf4d372ebe0ac3061dbb89 (patch) | |
tree | 4d70cedc274972ccc21b6b8e7105f042090cf5d6 /compilerplugins | |
parent | af791fb775e35c11ad01c42a7085dd121ab9c7a6 (diff) |
New loplugin:stringliteralvar
See the comment at the top of compilerplugins/clang/stringliteralvar.cxx for
details.
(Turned some affected variables in included files into inline variables, to
avoid GCC warnings about unused variables.)
Change-Id: Ie77219e6adfdaaceaa8b4e590b08971f2f04c83a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/108239
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/stringliteralvar.cxx | 203 | ||||
-rw-r--r-- | compilerplugins/clang/test/stringliteralvar.cxx | 81 |
2 files changed, 284 insertions, 0 deletions
diff --git a/compilerplugins/clang/stringliteralvar.cxx b/compilerplugins/clang/stringliteralvar.cxx new file mode 100644 index 000000000000..348193421a61 --- /dev/null +++ b/compilerplugins/clang/stringliteralvar.cxx @@ -0,0 +1,203 @@ +/* -*- 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/. + */ + +// Find constant character array variables that are passed into O[U]String constructors and should +// thus be turned into O[U]StringLiteral variables. +// +// Such a variable may have been used in multiple places, not all of which would be compatible with +// changing the variable's type to O[U]StringLiteral. However, this plugin is aggressive and +// ignores all but the first use of such a variable. In all cases of incompatible uses so far, it +// was possible to change to surrounding code (for the better) to make the changes work. +// +// The plugin also flags O[U]StringLiteral variables of automatic storage duration, and uses of such +// variables with sizeof---two likely errors that can occur once a variable has been changed from a +// character array to O[U]StringLiteral. +// +//TODO: In theory, we should not only look for variables, but also for non-static data members. In +// practice, those should be rare, though, as they should arguably have been static data members to +// begin with. + +#include <cassert> + +#include "check.hxx" +#include "plugin.hxx" + +namespace +{ +bool isAutomaticVariable(VarDecl const* decl) +{ + switch (cast<VarDecl>(decl)->getStorageDuration()) + { + case SD_Automatic: + return true; + case SD_Thread: + case SD_Static: + return false; + case SD_FullExpression: + case SD_Dynamic: + assert(false); + default: + llvm_unreachable("unknown StorageDuration"); + } +} + +class StringLiteralVar final : public loplugin::FilteringPlugin<StringLiteralVar> +{ +public: + explicit StringLiteralVar(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + bool TraverseInitListExpr(InitListExpr* expr, DataRecursionQueue* queue = nullptr) + { + return WalkUpFromInitListExpr(expr) + && TraverseSynOrSemInitListExpr( + expr->isSemanticForm() ? expr : expr->getSemanticForm(), queue); + } + + bool VisitCXXConstructExpr(CXXConstructExpr const* expr) + { + if (ignoreLocation(expr)) + { + return true; + } + loplugin::TypeCheck const tc(expr->getType()); + if (!(tc.Class("OString").Namespace("rtl").GlobalNamespace() + || tc.Class("OUString").Namespace("rtl").GlobalNamespace())) + { + return true; + } + switch (expr->getConstructor()->getNumParams()) + { + case 1: + { + auto const e = dyn_cast<DeclRefExpr>(expr->getArg(0)->IgnoreParenImpCasts()); + if (e == nullptr) + { + return true; + } + auto const tc = loplugin::TypeCheck(e->getType()); + if (!(tc.Class("OStringLiteral").Namespace("rtl").GlobalNamespace() + || tc.Class("OUStringLiteral").Namespace("rtl").GlobalNamespace())) + { + return true; + } + auto const d = e->getDecl(); + if (!isAutomaticVariable(cast<VarDecl>(d))) + { + return true; + } + if (!reportedAutomatic_.insert(d).second) + { + return true; + } + report(DiagnosticsEngine::Warning, + "variable %0 of type %1 with automatic storage duration most likely needs " + "to be static", + d->getLocation()) + << d << d->getType() << d->getSourceRange(); + report(DiagnosticsEngine::Note, "first converted to %0 here", expr->getLocation()) + << expr->getType() << expr->getSourceRange(); + } + break; + case 2: + { + auto const e1 = dyn_cast<DeclRefExpr>(expr->getArg(0)->IgnoreParenImpCasts()); + if (e1 == nullptr) + { + return true; + } + auto const t = e1->getType(); + if (!(t.isConstQualified() && t->isConstantArrayType())) + { + return true; + } + auto const e2 = expr->getArg(1); + if (!(isa<CXXDefaultArgExpr>(e2) + && loplugin::TypeCheck(e2->getType()) + .Struct("Dummy") + .Namespace("libreoffice_internal") + .Namespace("rtl") + .GlobalNamespace())) + { + return true; + } + auto const d = e1->getDecl(); + if (!reportedArray_.insert(d).second) + { + return true; + } + report(DiagnosticsEngine::Warning, + "change type of variable %0 from constant character array (%1) to " + "%select{OStringLiteral|OUStringLiteral}2%select{|, and make it static}3", + d->getLocation()) + << d << d->getType() + << (tc.Class("OString").Namespace("rtl").GlobalNamespace() ? 0 : 1) + << isAutomaticVariable(cast<VarDecl>(d)) << d->getSourceRange(); + report(DiagnosticsEngine::Note, "first passed into a %0 constructor here", + expr->getLocation()) + << expr->getType().getUnqualifiedType() << expr->getSourceRange(); + } + break; + } + return true; + } + + bool VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr const* expr) + { + if (ignoreLocation(expr)) + { + return true; + } + if (expr->getKind() != UETT_SizeOf) + { + return true; + } + if (expr->isArgumentType()) + { + return true; + } + auto const e = dyn_cast<DeclRefExpr>(expr->getArgumentExpr()->IgnoreParenImpCasts()); + if (e == nullptr) + { + return true; + } + auto const tc = loplugin::TypeCheck(e->getType()); + if (!(tc.Class("OStringLiteral").Namespace("rtl").GlobalNamespace() + || tc.Class("OUStringLiteral").Namespace("rtl").GlobalNamespace())) + { + return true; + } + auto const d = e->getDecl(); + report(DiagnosticsEngine::Warning, + "variable %0 of type %1 suspiciously used in a sizeof expression", e->getLocation()) + << d << d->getType() << expr->getSourceRange(); + return true; + } + + bool preRun() override { return compiler.getLangOpts().CPlusPlus; } + +private: + void run() override + { + if (preRun()) + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + } + + std::set<Decl const*> reportedAutomatic_; + std::set<Decl const*> reportedArray_; +}; + +static loplugin::Plugin::Registration<StringLiteralVar> reg("stringliteralvar"); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/compilerplugins/clang/test/stringliteralvar.cxx b/compilerplugins/clang/test/stringliteralvar.cxx new file mode 100644 index 000000000000..25a85f90a5a3 --- /dev/null +++ b/compilerplugins/clang/test/stringliteralvar.cxx @@ -0,0 +1,81 @@ +/* -*- 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 <vector> + +#include <rtl/ustring.hxx> + +// expected-error@+1 {{change type of variable 'literal1' from constant character array ('const char [4]') to OStringLiteral [loplugin:stringliteralvar]}} +char const literal1[] = "foo"; +OString f1() +{ + // expected-note@+1 {{first passed into a 'rtl::OString' constructor here [loplugin:stringliteralvar]}} + return literal1; +} + +void f(OUString const&); +void f2() +{ + // expected-error@+1 {{change type of variable 'literal' from constant character array ('const char [4]') to OUStringLiteral, and make it static [loplugin:stringliteralvar]}} + char const literal[] = "foo"; + // expected-note@+1 {{first passed into a 'rtl::OUString' constructor here [loplugin:stringliteralvar]}} + f(literal); +} + +struct S3 +{ + // expected-error@+1 {{change type of variable 'literal' from constant character array ('const char16_t [4]') to OUStringLiteral [loplugin:stringliteralvar]}} + static constexpr char16_t literal[] = u"foo"; +}; +void f3() +{ + // expected-note@+1 {{first passed into a 'rtl::OUString' constructor here [loplugin:stringliteralvar]}} + f(S3::literal); +} + +std::vector<OUString> f4() +{ + // expected-error@+1 {{change type of variable 'literal' from constant character array ('const char16_t [4]') to OUStringLiteral [loplugin:stringliteralvar]}} + static constexpr char16_t literal[] = u"foo"; + // expected-note@+1 {{first passed into a 'rtl::OUString' constructor here [loplugin:stringliteralvar]}} + return { literal }; +} + +void f5() +{ + // expected-error@+1 {{variable 'literal' of type 'const rtl::OUStringLiteral<4>' with automatic storage duration most likely needs to be static [loplugin:stringliteralvar]}} + OUStringLiteral const literal = u"foo"; + // expected-note@+1 {{first converted to 'rtl::OUString' here [loplugin:stringliteralvar]}} + f(literal); +} + +void f6() +{ + // expected-error@+1 {{variable 'literal' of type 'const rtl::OUStringLiteral<4>' with automatic storage duration most likely needs to be static [loplugin:stringliteralvar]}} + constexpr OUStringLiteral literal = u"foo"; + // expected-note@+1 {{first converted to 'rtl::OUString' here [loplugin:stringliteralvar]}} + f(literal); +} + +void f7() +{ + static constexpr OUStringLiteral const literal = u"foo"; + f(literal); +} + +void f8() +{ + static constexpr OUStringLiteral const literal = u"foo"; + // expected-error@+1 {{variable 'literal' of type 'const rtl::OUStringLiteral<4>' suspiciously used in a sizeof expression [loplugin:stringliteralvar]}} + (void)sizeof literal; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |