summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2018-10-16 10:40:16 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2018-10-17 08:25:17 +0200
commit6ee4375763854e43e549aee5a35520def2e215a2 (patch)
treee8f25d738779c2b8516e87cf3a1493d196135bd7 /compilerplugins
parent16690220ed6e68f2e9674a09b5008f38c5e6ed8d (diff)
new loplugin staticvar
looks for variables that can be declared const and static i.e. they can be stored in the read-only linker segment and shared between different processes Change-Id: I577fb2070604003e56fb44f8a02c9684070311cf Reviewed-on: https://gerrit.libreoffice.org/61817 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/staticvar.cxx204
-rw-r--r--compilerplugins/clang/test/staticvar.cxx80
2 files changed, 284 insertions, 0 deletions
diff --git a/compilerplugins/clang/staticvar.cxx b/compilerplugins/clang/staticvar.cxx
new file mode 100644
index 000000000000..a9db2f4dda04
--- /dev/null
+++ b/compilerplugins/clang/staticvar.cxx
@@ -0,0 +1,204 @@
+/* -*- 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 <cassert>
+#include <string>
+#include <iostream>
+#include <unordered_map>
+#include <unordered_set>
+
+#include "plugin.hxx"
+#include "check.hxx"
+#include "clang/AST/CXXInheritance.h"
+#include "clang/AST/StmtVisitor.h"
+
+// Look for variables that either
+// (a) could be statically initialised, without runtime code, and warn
+// (b) variables that are statically declared, but require runtime initialisation, and warn
+//
+// e.g.
+// static const OUString[] XXX { "xxx" };
+// requires runtime initialisation, so should rather be declared as OUStringLiteral
+// and
+// static int[] XXX { 1,2 };
+// can be declared const since it does not require runtime initialisation.
+
+namespace
+{
+class StaticVar : public loplugin::FilteringPlugin<StaticVar>
+{
+public:
+ explicit StaticVar(loplugin::InstantiationData const& data)
+ : FilteringPlugin(data)
+ {
+ }
+
+ virtual void run() override
+ {
+ std::string fn(handler.getMainFileName());
+ loplugin::normalizeDotDotInFilePath(fn);
+
+ if (
+ // uses icu::UnicodeString
+ fn == SRCDIR "/l10ntools/source/xmlparse.cxx"
+ // contains mutable state
+ || fn == SRCDIR "/sal/osl/unx/signal.cxx"
+ || fn == SRCDIR "/sal/qa/rtl/digest/rtl_digest.cxx"
+ || fn == SRCDIR "/sal/qa/rtl/strings/test_oustring_endswith.cxx"
+ || fn == SRCDIR "/sal/qa/rtl/strings/test_oustring_convert.cxx"
+ || fn == SRCDIR "/svl/qa/unit/items/test_itempool.cxx"
+ // contains mutable state
+ || fn == SRCDIR "/vcl/unx/generic/dtrans/X11_selection.cxx"
+ || fn == SRCDIR "/sax/qa/cppunit/xmlimport.cxx"
+ || fn == SRCDIR "/pyuno/source/module/pyuno.cxx"
+ || fn == SRCDIR "/pyuno/source/module/pyuno_module.cxx"
+ || fn == SRCDIR "/pyuno/source/module/pyuno_struct.cxx"
+ // TODO for this one we need a static OUString
+ || fn == SRCDIR "/xmloff/source/core/xmltoken.cxx"
+ // mutable
+ || fn == SRCDIR "/basic/source/runtime/stdobj.cxx"
+ // TODO this needs more extensive cleanup
+ || fn == SRCDIR "/connectivity/source/drivers/postgresql/pq_statics.cxx"
+ // mutable
+ || fn == SRCDIR "/hwpfilter/source/hwpreader.cxx"
+ // mutable
+ || fn == SRCDIR "/sw/source/filter/basflt/fltini.cxx"
+ // mutable
+ || fn == SRCDIR "/sw/source/uibase/docvw/srcedtw.cxx"
+ // mutable
+ || fn == SRCDIR "/forms/source/misc/limitedformats.cxx"
+ // aHTMLOptionTab is ordered by useful grouping, so let it sort at runtime
+ || fn == SRCDIR "/svtools/source/svhtml/htmlkywd.cxx"
+ // TODO sorting some of these tables will be a lot of work...
+ || fn == SRCDIR "/sw/source/filter/ww8/ww8par6.cxx")
+ return;
+ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
+ }
+
+ bool VisitVarDecl(VarDecl const*);
+};
+
+static bool containsNonLiteral(Expr const* expr)
+{
+ expr = expr->IgnoreImplicit();
+ if (auto initList = dyn_cast<InitListExpr>(expr))
+ {
+ for (unsigned i = 0; i < initList->getNumInits(); ++i)
+ if (containsNonLiteral(initList->getInit(i)))
+ return true;
+ }
+ else if (auto constructExpr = dyn_cast<CXXConstructExpr>(expr))
+ {
+ for (Expr const* arg : constructExpr->arguments())
+ if (containsNonLiteral(arg))
+ return true;
+ }
+ else if (isa<MemberExpr>(expr))
+ return true;
+ else if (auto declRefExpr = dyn_cast<DeclRefExpr>(expr))
+ {
+ auto varDecl = dyn_cast_or_null<VarDecl>(declRefExpr->getDecl());
+ return varDecl && varDecl->isLocalVarDeclOrParm();
+ }
+ else if (isa<CXXMemberCallExpr>(expr))
+ return true;
+ else if (auto castExpr = dyn_cast<CXXFunctionalCastExpr>(expr))
+ return containsNonLiteral(castExpr->getSubExpr());
+ else if (auto unaryOp = dyn_cast<UnaryOperator>(expr))
+ return containsNonLiteral(unaryOp->getSubExpr());
+
+ return false;
+}
+
+bool StaticVar::VisitVarDecl(VarDecl const* varDecl)
+{
+ if (ignoreLocation(varDecl))
+ return true;
+ if (!varDecl->hasInit())
+ return true;
+ auto initList = dyn_cast_or_null<InitListExpr>(varDecl->getInit());
+ if (!initList)
+ return true;
+ if (varDecl->isExceptionVariable() || isa<ParmVarDecl>(varDecl))
+ return true;
+ if (!varDecl->getType()->isArrayType())
+ return true;
+ auto elementType = varDecl->getType()->getBaseElementTypeUnsafe();
+ if (!elementType->isRecordType())
+ return true;
+ auto elementRecordDecl
+ = dyn_cast_or_null<CXXRecordDecl>(elementType->getAs<RecordType>()->getDecl());
+ if (!elementRecordDecl)
+ return true;
+ if (containsNonLiteral(initList))
+ return true;
+
+ if (elementRecordDecl->hasTrivialDestructor())
+ {
+ if (varDecl->isLocalVarDecl())
+ {
+ if (varDecl->getStorageDuration() == SD_Static && varDecl->getType().isConstQualified())
+ return true;
+ }
+ else
+ {
+ if (varDecl->getType().isConstQualified())
+ return true;
+ }
+
+ // TODO cannot figure out how to make the loplugin::TypeCheck stuff match this
+ // std::string typeName = varDecl->getType().getAsString();
+ // if (typeName == "std::va_list" || typeName == "va_list")
+ // return true;
+
+ auto const tcElement = loplugin::TypeCheck(elementRecordDecl);
+ if (tcElement.Struct("ContextID_Index_Pair").GlobalNamespace())
+ return true;
+ if (tcElement.Class("SfxSlot").GlobalNamespace())
+ return true;
+
+ if (varDecl->isLocalVarDecl())
+ report(DiagnosticsEngine::Warning, "var should be static const, or whitelisted",
+ varDecl->getLocation())
+ << varDecl->getSourceRange();
+ else
+ report(DiagnosticsEngine::Warning, "var should be const, or whitelisted",
+ varDecl->getLocation())
+ << varDecl->getSourceRange();
+ }
+ else
+ {
+ if (varDecl->isLocalVarDecl())
+ {
+ if (varDecl->getStorageDuration() != SD_Static
+ || !varDecl->getType().isConstQualified())
+ return true;
+ }
+ else
+ {
+ if (!varDecl->getType().isConstQualified())
+ return true;
+ }
+
+ if (varDecl->isLocalVarDecl())
+ report(DiagnosticsEngine::Warning, "static const var requires runtime initialization?",
+ varDecl->getLocation())
+ << varDecl->getSourceRange();
+ else
+ report(DiagnosticsEngine::Warning, "static var requires runtime initialization?",
+ varDecl->getLocation())
+ << varDecl->getSourceRange();
+ }
+ return true;
+}
+
+loplugin::Plugin::Registration<StaticVar> X("staticvar", false);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/compilerplugins/clang/test/staticvar.cxx b/compilerplugins/clang/test/staticvar.cxx
new file mode 100644
index 000000000000..9963e99db687
--- /dev/null
+++ b/compilerplugins/clang/test/staticvar.cxx
@@ -0,0 +1,80 @@
+/* -*- 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 <config_clang.h>
+#include <sal/config.h>
+#include <rtl/ustring.hxx>
+
+struct S1
+{
+ int x, y;
+};
+
+S1 const& f1(int a)
+{
+ static S1 s1[]{
+ // expected-error@-1 {{var should be static const, or whitelisted [loplugin:staticvar]}}
+ { 1, 1 }
+ };
+ // no warning expected
+ const S1 s2[]{ { a, 1 } };
+ (void)s2;
+ return s1[0];
+}
+
+struct S2
+{
+ OUString x;
+};
+
+#if CLANG_VERSION >= 50000 // probably something to do with how OUString initialisers work
+S2 const& f2()
+{
+ static S2 const s1[]{
+ // expected-error@-1 {{static const var requires runtime initialization? [loplugin:staticvar]}}
+ { "xxx" }
+ };
+ return s1[0];
+}
+#endif
+
+// no warning expected
+S2 const& f3()
+{
+ static S2 s1[]{ { "xxx" } };
+ return s1[0];
+}
+
+// no warning expected
+struct S4
+{
+ OUStringLiteral const cName;
+ bool const bCanBeVisible;
+};
+S4 const& f4()
+{
+ static const S4 s1[] = {
+ { OUStringLiteral("/DocColor"), false },
+ };
+ return s1[0];
+}
+
+struct S5
+{
+ bool const bCanBeVisible;
+};
+void f5(bool b)
+{
+ const S5 s1[] = {
+ { b },
+ };
+ (void)s1;
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */