summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2019-08-29 10:35:23 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2019-08-30 08:12:07 +0200
commit6e4a2540d4d8ad9e141b87ac3d2123d1c96366ef (patch)
treec37720e7c9cb051f80aa1294b17b53e23b243187 /compilerplugins
parent0285492a45e9cc19c26e14dcdf297bcc491da4d2 (diff)
new loplugin:noexceptmove
idea from mike kaganski look for places where we can mark move operators as noexcept, which makes some STL operations more efficient Change-Id: Id732b89d1fcadd5ceb0ea2b9d159fed06136330f Reviewed-on: https://gerrit.libreoffice.org/78251 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/noexceptmove.cxx319
-rw-r--r--compilerplugins/clang/test/noexceptmove.cxx102
2 files changed, 421 insertions, 0 deletions
diff --git a/compilerplugins/clang/noexceptmove.cxx b/compilerplugins/clang/noexceptmove.cxx
new file mode 100644
index 000000000000..483ccaed44cf
--- /dev/null
+++ b/compilerplugins/clang/noexceptmove.cxx
@@ -0,0 +1,319 @@
+/* -*- 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/.
+ */
+// versions before 9.0 didn't have getExceptionSpecType
+
+#include "plugin.hxx"
+
+// clang before V9 does not have API to report exception spec type
+#if CLANG_VERSION >= 90000
+
+#include "check.hxx"
+
+#include <string>
+#include <set>
+
+/**
+ Look for move constructors that can be noexcept.
+*/
+
+namespace
+{
+/// Look for the stuff that can be marked noexcept, but only if we also mark some of the callees noexcept.
+/// Off by default so as not too annoy people.
+constexpr bool bLookForStuffWeCanFix = false;
+
+class NoExceptMove : public loplugin::FilteringPlugin<NoExceptMove>
+{
+public:
+ explicit NoExceptMove(loplugin::InstantiationData const& data)
+ : FilteringPlugin(data)
+ {
+ }
+
+ virtual void run() override
+ {
+ StringRef fn(handler.getMainFileName());
+ // ONDXPagePtr::operator= calls ONDXPage::ReleaseRef which cannot be noexcept
+ if (loplugin::isSamePathname(fn,
+ SRCDIR "/connectivity/source/drivers/dbase/dindexnode.cxx"))
+ return;
+ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
+ }
+
+ bool shouldVisitImplicitCode() const { return true; }
+
+ bool TraverseCXXConstructorDecl(CXXConstructorDecl*);
+ bool TraverseCXXMethodDecl(CXXMethodDecl*);
+ bool VisitCallExpr(const CallExpr*);
+ bool VisitCXXConstructExpr(const CXXConstructExpr*);
+ bool VisitVarDecl(const VarDecl*);
+
+private:
+ llvm::Optional<bool> IsCallThrows(const CallExpr* callExpr);
+ std::vector<bool> m_ConstructorThrows;
+ std::vector<std::vector<const Decl*>> m_Exclusions;
+ std::vector<bool> m_CannotFix;
+};
+
+bool NoExceptMove::TraverseCXXConstructorDecl(CXXConstructorDecl* constructorDecl)
+{
+ const bool isMove = constructorDecl->isMoveConstructor()
+ && constructorDecl->getExceptionSpecType() == EST_None
+ && !constructorDecl->isDefaulted() && !constructorDecl->isDeleted()
+ && !ignoreLocation(constructorDecl)
+ && constructorDecl->isThisDeclarationADefinition();
+ if (isMove)
+ {
+ m_ConstructorThrows.push_back(false);
+ m_Exclusions.emplace_back();
+ m_CannotFix.push_back(false);
+ }
+ bool rv = RecursiveASTVisitor::TraverseCXXConstructorDecl(constructorDecl);
+ if (isMove)
+ {
+ if (!m_ConstructorThrows.back())
+ {
+ report(DiagnosticsEngine::Warning, "move constructor can be noexcept",
+ constructorDecl->getSourceRange().getBegin())
+ << constructorDecl->getSourceRange();
+ auto canonicalDecl = constructorDecl->getCanonicalDecl();
+ if (canonicalDecl != constructorDecl)
+ report(DiagnosticsEngine::Note, "declaration here",
+ canonicalDecl->getSourceRange().getBegin())
+ << canonicalDecl->getSourceRange();
+ }
+ else if (bLookForStuffWeCanFix && !m_CannotFix.back())
+ {
+ report(DiagnosticsEngine::Warning, "move constructor can be noexcept",
+ constructorDecl->getSourceRange().getBegin())
+ << constructorDecl->getSourceRange();
+ auto canonicalDecl = constructorDecl->getCanonicalDecl();
+ if (canonicalDecl != constructorDecl)
+ report(DiagnosticsEngine::Note, "declaration here",
+ canonicalDecl->getSourceRange().getBegin())
+ << canonicalDecl->getSourceRange();
+ for (const Decl* callDecl : m_Exclusions.back())
+ report(DiagnosticsEngine::Warning, "but need to fix this to be noexcept",
+ callDecl->getSourceRange().getBegin())
+ << callDecl->getSourceRange();
+ }
+ m_ConstructorThrows.pop_back();
+ m_Exclusions.pop_back();
+ m_CannotFix.pop_back();
+ }
+ return rv;
+}
+
+bool NoExceptMove::TraverseCXXMethodDecl(CXXMethodDecl* methodDecl)
+{
+ bool isMove = methodDecl->isMoveAssignmentOperator()
+ && methodDecl->getExceptionSpecType() == EST_None && !methodDecl->isDefaulted()
+ && !methodDecl->isDeleted() && !ignoreLocation(methodDecl)
+ && methodDecl->isThisDeclarationADefinition();
+ if (isMove)
+ {
+ StringRef fn = getFileNameOfSpellingLoc(
+ compiler.getSourceManager().getSpellingLoc(compat::getBeginLoc(methodDecl)));
+ // SfxObjectShellLock::operator= calls SotObject::OwnerLock whichs in turn calls stuff which cannot be noexcept
+ if (loplugin::isSamePathname(fn, SRCDIR "/include/sfx2/objsh.hxx"))
+ isMove = false;
+ }
+ if (isMove)
+ {
+ m_ConstructorThrows.push_back(false);
+ m_Exclusions.emplace_back();
+ m_CannotFix.push_back(false);
+ }
+ bool rv = RecursiveASTVisitor::TraverseCXXMethodDecl(methodDecl);
+ if (isMove)
+ {
+ if (!m_ConstructorThrows.back())
+ {
+ report(DiagnosticsEngine::Warning, "move operator= can be noexcept",
+ methodDecl->getSourceRange().getBegin())
+ << methodDecl->getSourceRange();
+ auto canonicalDecl = methodDecl->getCanonicalDecl();
+ if (canonicalDecl != methodDecl)
+ report(DiagnosticsEngine::Note, "declaration here",
+ canonicalDecl->getSourceRange().getBegin())
+ << canonicalDecl->getSourceRange();
+ }
+ else if (bLookForStuffWeCanFix && !m_CannotFix.back())
+ {
+ report(DiagnosticsEngine::Warning, "move operator= can be noexcept",
+ methodDecl->getSourceRange().getBegin())
+ << methodDecl->getSourceRange();
+ auto canonicalDecl = methodDecl->getCanonicalDecl();
+ if (canonicalDecl != methodDecl)
+ report(DiagnosticsEngine::Note, "declaration here",
+ canonicalDecl->getSourceRange().getBegin())
+ << canonicalDecl->getSourceRange();
+ for (const Decl* callDecl : m_Exclusions.back())
+ report(DiagnosticsEngine::Warning, "but need to fix this to be noexcept",
+ callDecl->getSourceRange().getBegin())
+ << callDecl->getSourceRange();
+ }
+ m_ConstructorThrows.pop_back();
+ m_Exclusions.pop_back();
+ m_CannotFix.pop_back();
+ }
+ return rv;
+}
+
+bool NoExceptMove::VisitCallExpr(const CallExpr* callExpr)
+{
+ if (ignoreLocation(callExpr))
+ return true;
+ if (m_ConstructorThrows.empty())
+ return true;
+ llvm::Optional<bool> bCallThrows = IsCallThrows(callExpr);
+ if (!bCallThrows)
+ {
+ callExpr->dump();
+ if (callExpr->getCalleeDecl())
+ callExpr->getCalleeDecl()->dump();
+ report(DiagnosticsEngine::Warning, "whats up doc?", callExpr->getSourceRange().getBegin())
+ << callExpr->getSourceRange();
+ m_ConstructorThrows.back() = true;
+ return true;
+ }
+ if (*bCallThrows)
+ m_ConstructorThrows.back() = true;
+ return true;
+}
+
+static bool IsCallThrowsSpec(clang::ExceptionSpecificationType est)
+{
+ return !(est == EST_DynamicNone || est == EST_NoThrow || est == EST_BasicNoexcept
+ || est == EST_NoexceptTrue);
+}
+
+bool NoExceptMove::VisitCXXConstructExpr(const CXXConstructExpr* constructExpr)
+{
+ if (ignoreLocation(constructExpr))
+ return true;
+ if (m_ConstructorThrows.empty())
+ return true;
+ auto constructorDecl = constructExpr->getConstructor();
+ auto est = constructorDecl->getExceptionSpecType();
+ if (constructorDecl->isDefaulted() && est == EST_None)
+ ; // ok, non-throwing
+ else if (IsCallThrowsSpec(est))
+ {
+ m_ConstructorThrows.back() = true;
+ }
+ return true;
+}
+
+bool NoExceptMove::VisitVarDecl(const VarDecl* varDecl)
+{
+ if (varDecl->getLocation().isValid() && ignoreLocation(varDecl))
+ return true;
+ if (m_ConstructorThrows.empty())
+ return true;
+ // The clang AST does not show me implicit calls to destructors at the end of a block,
+ // so assume any local var decls of class type will call their destructor.
+ if (!varDecl->getType()->isRecordType())
+ return true;
+ auto cxxRecordDecl = varDecl->getType()->getAsCXXRecordDecl();
+ if (!cxxRecordDecl)
+ return true;
+ auto destructorDecl = cxxRecordDecl->getDestructor();
+ if (!destructorDecl)
+ return true;
+ auto est = destructorDecl->getExceptionSpecType();
+ if (destructorDecl->isDefaulted() && est == EST_None)
+ ; // ok, non-throwing
+ else if (IsCallThrowsSpec(est))
+ {
+ m_ConstructorThrows.back() = true;
+ }
+ return true;
+}
+
+llvm::Optional<bool> NoExceptMove::IsCallThrows(const CallExpr* callExpr)
+{
+ const FunctionDecl* calleeFunctionDecl = callExpr->getDirectCallee();
+ if (calleeFunctionDecl)
+ {
+ auto est = calleeFunctionDecl->getExceptionSpecType();
+ if (bLookForStuffWeCanFix)
+ {
+ if (est == EST_None && !ignoreLocation(calleeFunctionDecl))
+ m_Exclusions.back().push_back(calleeFunctionDecl);
+ else
+ m_CannotFix.back() = true;
+ }
+ // Whitelist of functions that could be noexcept, but we can't change them because of backwards-compatibility reasons
+ // css::uno::XInterface::acquire
+ // css::uno::XInterface::release
+ if (calleeFunctionDecl->getIdentifier())
+ {
+ auto name = calleeFunctionDecl->getName();
+ if (auto cxxMethodDecl = dyn_cast<CXXMethodDecl>(calleeFunctionDecl))
+ if (loplugin::ContextCheck(cxxMethodDecl->getParent()->getDeclContext())
+ .Namespace("uno")
+ .Namespace("star")
+ .Namespace("sun")
+ .Namespace("com")
+ .GlobalNamespace()
+ && (name == "acquire" || name == "release"))
+ return false;
+ if (name == "osl_releasePipe" || name == "osl_destroySocketAddr")
+ return false;
+ }
+ return IsCallThrowsSpec(est);
+ }
+
+ auto calleeExpr = callExpr->getCallee();
+ if (isa<CXXDependentScopeMemberExpr>(calleeExpr) || isa<UnresolvedLookupExpr>(calleeExpr))
+ {
+ m_CannotFix.back() = true;
+ return true;
+ }
+
+ // check for call via function-pointer
+ clang::QualType calleeType;
+ if (auto fieldDecl = dyn_cast_or_null<FieldDecl>(callExpr->getCalleeDecl()))
+ calleeType = fieldDecl->getType();
+ else if (auto varDecl = dyn_cast_or_null<VarDecl>(callExpr->getCalleeDecl()))
+ calleeType = varDecl->getType();
+ else
+ {
+ m_CannotFix.back() = true;
+ return llvm::Optional<bool>();
+ }
+
+ // whitelist of functions that could be noexcept, but we can't change them because of backwards-compatibility reasons
+ if (auto typedefType = calleeType->getAs<TypedefType>())
+ if (typedefType->getDecl()->getName() == "uno_ReleaseMappingFunc")
+ return false;
+
+ if (calleeType->isPointerType())
+ calleeType = calleeType->getPointeeType();
+ auto funcProto = calleeType->getAs<FunctionProtoType>();
+ if (!funcProto)
+ {
+ m_CannotFix.back() = true;
+ return llvm::Optional<bool>();
+ }
+
+ auto est = funcProto->getExceptionSpecType();
+ if (bLookForStuffWeCanFix)
+ {
+ m_CannotFix.back() = true; // TODO, could improve
+ }
+ return IsCallThrowsSpec(est);
+}
+
+loplugin::Plugin::Registration<NoExceptMove> noexceptmove("noexceptmove");
+}
+
+#endif // CLANG_VERSION
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/compilerplugins/clang/test/noexceptmove.cxx b/compilerplugins/clang/test/noexceptmove.cxx
new file mode 100644
index 000000000000..fda58deae403
--- /dev/null
+++ b/compilerplugins/clang/test/noexceptmove.cxx
@@ -0,0 +1,102 @@
+/* -*- 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"
+
+// clang before V9 does not have API to report exception spec type
+#if CLANG_VERSION >= 90000
+
+namespace test1
+{
+class Mapping
+{
+ char* m_pMapping;
+
+ // expected-error@+1 {{move constructor can be noexcept [loplugin:noexceptmove]}}
+ Mapping(Mapping&& other)
+ : m_pMapping(other.m_pMapping)
+ {
+ other.m_pMapping = nullptr;
+ }
+
+ // expected-error@+1 {{move operator= can be noexcept [loplugin:noexceptmove]}}
+ Mapping& operator=(Mapping&& other)
+ {
+ m_pMapping = other.m_pMapping;
+ other.m_pMapping = nullptr;
+ return *this;
+ }
+};
+};
+
+// No warning expected, because calling throwing function.
+namespace test2
+{
+void foo() noexcept(false);
+
+class Bar
+{
+ Bar(Bar&&) { foo(); }
+};
+};
+
+// no warning expected, because calling throwing constructor
+namespace test3
+{
+struct Foo
+{
+ Foo() noexcept(false);
+};
+class Bar
+{
+ Bar(Bar&&) { Foo aFoo; }
+};
+
+class Bar2
+{
+ Foo m_foo;
+
+ Bar2(Bar2&&) {}
+};
+};
+
+// No warning expected, because calling throwing destructor.
+namespace test4
+{
+struct Foo
+{
+ ~Foo() noexcept(false);
+};
+
+class Bar
+{
+ Bar(Bar&&) { Foo aFoo; }
+};
+};
+
+// Check for calls to defaulted constructors.
+namespace test5
+{
+struct Foo
+{
+ Foo() = default; // non-throwing
+};
+class Bar
+{
+ Bar(Bar&&) // expected-error {{move constructor can be noexcept [loplugin:noexceptmove]}}
+ {
+ Foo aFoo;
+ (void)aFoo;
+ }
+};
+};
+
+#else
+// expected-no-diagnostics
+#endif // CLANG_VERSION
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */