Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / ptrvector.cxx
blobdc3a69d0a9b7c9a633594fea57822030ebe4638d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <memory>
11 #include <cassert>
12 #include <string>
13 #include <iostream>
14 #include <fstream>
15 #include <set>
16 #include "plugin.hxx"
18 /**
19 Check for calls to operator== on a std::container< std::unique_ptr >, which is not useful,
20 because std::container will compare the pointers so it is never true
23 namespace {
25 class PtrVector:
26 public RecursiveASTVisitor<PtrVector>, public loplugin::Plugin
28 public:
29 explicit PtrVector(loplugin::InstantiationData const & data): Plugin(data)
32 virtual void run() override
34 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
37 bool shouldVisitTemplateInstantiations () const { return true; }
39 bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr* );
42 bool PtrVector::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr* expr)
44 if (ignoreLocation(expr)) {
45 return true;
47 if (expr->getOperator() != clang::OverloadedOperatorKind::OO_EqualEqual
48 && expr->getOperator() != clang::OverloadedOperatorKind::OO_ExclaimEqual)
50 return true;
52 if (isa<CXXNullPtrLiteralExpr>(expr->getArg(1))) {
53 return true;
55 const Expr* argExpr = expr->getArg(0);
56 std::string s = argExpr->getType().getDesugaredType(compiler.getASTContext()).getAsString();
57 if (s.find("iterator") != std::string::npos) {
58 return true;
60 if (s.find("array") == std::string::npos && s.find("deque") == std::string::npos
61 && s.find("list") == std::string::npos && s.find("vector") == std::string::npos
62 && s.find("set") == std::string::npos && s.find("map") == std::string::npos
63 && s.find("stack") == std::string::npos && s.find("queue") == std::string::npos)
65 return true;
67 if (s.find("unique_ptr") != std::string::npos) {
68 expr->getArg(1)->dump();
69 report(
70 DiagnosticsEngine::Warning,
71 "do not call operator== on a std container containing a unique_ptr " + s,
72 expr->getExprLoc());
74 return true;
78 loplugin::Plugin::Registration< PtrVector > X("ptrvector");
82 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */