summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/staticconstfield.cxx
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2018-09-13 15:07:13 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2018-09-16 12:01:59 +0200
commit7168fe32c9e79cf9cbbcf1e0b6a35d85781b1332 (patch)
treeca7573fadfa32bbcfb541b7d5352b00fd2391620 /compilerplugins/clang/staticconstfield.cxx
parentexplictly *not* translatable I mean (diff)
downloadcore-7168fe32c9e79cf9cbbcf1e0b6a35d85781b1332.tar.gz
core-7168fe32c9e79cf9cbbcf1e0b6a35d85781b1332.zip
rename conststringfield loplugin to staticconstfield
in preparation for making it more general Change-Id: I2fc8d0f99140dc7ef72341f8cbf28d6536ebd61f Reviewed-on: https://gerrit.libreoffice.org/60525 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins/clang/staticconstfield.cxx')
-rw-r--r--compilerplugins/clang/staticconstfield.cxx57
1 files changed, 57 insertions, 0 deletions
diff --git a/compilerplugins/clang/staticconstfield.cxx b/compilerplugins/clang/staticconstfield.cxx
new file mode 100644
index 000000000000..7276d7e05c49
--- /dev/null
+++ b/compilerplugins/clang/staticconstfield.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 StaticConstField : public loplugin::FilteringPlugin<StaticConstField>
+{
+public:
+ explicit StaticConstField(loplugin::InstantiationData const& data)
+ : loplugin::FilteringPlugin<StaticConstField>(data)
+ {
+ }
+
+ void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
+
+ bool TraverseConstructorInitializer(CXXCtorInitializer* init);
+};
+
+bool StaticConstField::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<StaticConstField> X("staticconstfield", true);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */