diff options
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/conststringfield.cxx | 57 | ||||
-rw-r--r-- | compilerplugins/clang/test/conststringfield.cxx | 43 |
2 files changed, 100 insertions, 0 deletions
diff --git a/compilerplugins/clang/conststringfield.cxx b/compilerplugins/clang/conststringfield.cxx new file mode 100644 index 000000000000..23a7db9ebea2 --- /dev/null +++ b/compilerplugins/clang/conststringfield.cxx @@ -0,0 +1,57 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * 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 "plugin.hxx" +#include "check.hxx" +#include "compat.hxx" +#include <iostream> + +namespace +{ +class ConstStringField : public loplugin::FilteringPlugin<ConstStringField> +{ +public: + explicit ConstStringField(loplugin::InstantiationData const& data) + : loplugin::FilteringPlugin<ConstStringField>(data) + { + } + + void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool TraverseConstructorInitializer(CXXCtorInitializer* init); +}; + +bool ConstStringField::TraverseConstructorInitializer(CXXCtorInitializer* init) +{ + if (!init->getSourceLocation().isValid() || ignoreLocation(init->getSourceLocation())) + return true; + if (!init->getMember()) + return true; + auto tc = loplugin::TypeCheck(init->getMember()->getType()); + if (!tc.Const().Class("OUString").Namespace("rtl").GlobalNamespace() + && !tc.Const().Class("OString").Namespace("rtl").GlobalNamespace()) + return true; + if (auto constructExpr = dyn_cast<CXXConstructExpr>(init->getInit())) + { + if (constructExpr->getNumArgs() >= 1 && isa<clang::StringLiteral>(constructExpr->getArg(0))) + { + report(DiagnosticsEngine::Warning, "string field can be static const", + init->getSourceLocation()) + << init->getSourceRange(); + report(DiagnosticsEngine::Note, "field here", init->getMember()->getLocation()) + << init->getMember()->getSourceRange(); + } + } + return RecursiveASTVisitor::TraverseConstructorInitializer(init); +} + +loplugin::Plugin::Registration<ConstStringField> X("conststringfield", true); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/test/conststringfield.cxx b/compilerplugins/clang/test/conststringfield.cxx new file mode 100644 index 000000000000..fa8785713521 --- /dev/null +++ b/compilerplugins/clang/test/conststringfield.cxx @@ -0,0 +1,43 @@ +/* -*- 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 <rtl/ustring.hxx> +#include <rtl/string.hxx> + +class Class1 +{ + OUString const m_field1; // expected-note {{field here [loplugin:conststringfield]}} + Class1() + : m_field1("xxxx") + // expected-error@-1 {{string field can be static const [loplugin:conststringfield]}} + { + } +}; + +class Class2 +{ + OString const m_field1; // expected-note {{field here [loplugin:conststringfield]}} + Class2() + : m_field1("xxxx") + // expected-error@-1 {{string field can be static const [loplugin:conststringfield]}} + { + } +}; + +// no warning expected +class Class4 +{ + OUString m_field1; + Class4() + : m_field1("xxxx") + { + } +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |