summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/ptrvector.cxx
diff options
context:
space:
mode:
authorNoel Grandin <noel@peralex.com>2015-11-12 16:14:04 +0200
committerNoel Grandin <noel@peralex.com>2015-11-13 15:18:06 +0200
commitf73284fb864699716b3a52faf2ad39bc8e48c3cc (patch)
tree4b7376fceed882c9b069a3708292c5ac658a512f /compilerplugins/clang/ptrvector.cxx
parent5493402fb37a1def960c93f7c31aff36a5ab5f9e (diff)
new loplugin ptrvector - find bad comparison of container<unique_ptr>
Change-Id: Idb18ae1ca2f7c644680703dc3b7dd3cd6000e040
Diffstat (limited to 'compilerplugins/clang/ptrvector.cxx')
-rw-r--r--compilerplugins/clang/ptrvector.cxx81
1 files changed, 81 insertions, 0 deletions
diff --git a/compilerplugins/clang/ptrvector.cxx b/compilerplugins/clang/ptrvector.cxx
new file mode 100644
index 000000000000..513d3509cf13
--- /dev/null
+++ b/compilerplugins/clang/ptrvector.cxx
@@ -0,0 +1,81 @@
+/* -*- 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 <fstream>
+#include <set>
+#include "plugin.hxx"
+#include "compat.hxx"
+
+/**
+ Check for calls to operator== on a std::container< std::unique_ptr >, which is not useful,
+ because std::container will compare the pointers so it is never true
+*/
+
+namespace {
+
+class PtrVector:
+ public RecursiveASTVisitor<PtrVector>, public loplugin::Plugin
+{
+public:
+ explicit PtrVector(InstantiationData const & data): Plugin(data) {}
+
+ virtual void run() override
+ {
+ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
+ }
+
+ bool shouldVisitTemplateInstantiations () const { return true; }
+
+ bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr* );
+};
+
+bool PtrVector::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr* expr)
+{
+ if (ignoreLocation(expr)) {
+ return true;
+ }
+ if (expr->getOperator() != clang::OverloadedOperatorKind::OO_EqualEqual
+ && expr->getOperator() != clang::OverloadedOperatorKind::OO_ExclaimEqual)
+ {
+ return true;
+ }
+ if (isa<CXXNullPtrLiteralExpr>(expr->getArg(1))) {
+ return true;
+ }
+ const Expr* argExpr = expr->getArg(0);
+ std::string s = argExpr->getType().getDesugaredType(compiler.getASTContext()).getAsString();
+ if (s.find("iterator") != std::string::npos) {
+ return true;
+ }
+ if (s.find("array") == std::string::npos && s.find("deque") == std::string::npos
+ && s.find("list") == std::string::npos && s.find("vector") == std::string::npos
+ && s.find("set") == std::string::npos && s.find("map") == std::string::npos
+ && s.find("stack") == std::string::npos && s.find("queue") == std::string::npos)
+ {
+ return true;
+ }
+ if (s.find("unique_ptr") != std::string::npos) {
+ expr->getArg(1)->dump();
+ report(
+ DiagnosticsEngine::Warning,
+ "do not call operator== on a std container containing a unique_ptr " + s,
+ expr->getExprLoc());
+ }
+ return true;
+}
+
+
+loplugin::Plugin::Registration< PtrVector > X("ptrvector");
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */