bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / ptrvector.cxx
blob758e3e90f34cd45e947387033c3d577518aa01cc
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 */
9 #ifndef LO_CLANG_SHARED_PLUGINS
11 #include <memory>
12 #include <cassert>
13 #include <string>
14 #include <iostream>
15 #include <fstream>
16 #include <set>
18 #include "check.hxx"
19 #include "plugin.hxx"
21 /**
22 Check for calls to operator== on a std::container< std::unique_ptr >, which is not useful,
23 because std::container will compare the pointers so it is never true
26 namespace {
28 class PtrVector:
29 public loplugin::FilteringPlugin<PtrVector>
31 public:
32 explicit PtrVector(loplugin::InstantiationData const & data): FilteringPlugin(data)
35 virtual void run() override
37 if (preRun())
38 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
41 bool shouldVisitTemplateInstantiations () const { return true; }
43 bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr* );
46 bool PtrVector::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr* expr)
48 if (ignoreLocation(expr)) {
49 return true;
51 if (expr->getOperator() != clang::OverloadedOperatorKind::OO_EqualEqual
52 && expr->getOperator() != clang::OverloadedOperatorKind::OO_ExclaimEqual)
54 return true;
56 if (isa<CXXNullPtrLiteralExpr>(expr->getArg(1))) {
57 return true;
59 const Expr* argExpr = expr->getArg(0);
60 std::string s = argExpr->getType().getDesugaredType(compiler.getASTContext()).getAsString();
61 if (s.find("iterator") != std::string::npos
62 || (loplugin::TypeCheck(argExpr->getType()).Class("__wrap_iter").Namespace("__1")
63 .StdNamespace()))
65 return true;
67 if (s.find("array") == std::string::npos && s.find("deque") == std::string::npos
68 && s.find("list") == std::string::npos && s.find("vector") == std::string::npos
69 && s.find("set") == std::string::npos && s.find("map") == std::string::npos
70 && s.find("stack") == std::string::npos && s.find("queue") == std::string::npos)
72 return true;
74 if (s.find("unique_ptr") != std::string::npos) {
75 expr->getArg(1)->dump();
76 report(
77 DiagnosticsEngine::Warning,
78 "do not call operator== on a std container containing a unique_ptr " + s,
79 expr->getExprLoc());
81 return true;
85 loplugin::Plugin::Registration< PtrVector > ptrvector("ptrvector");
87 } // namespace
89 #endif // LO_CLANG_SHARED_PLUGINS
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */