bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / ptrvector.cxx
blobed0893e3558f8f7fc2bd47b84542172d8bd217b9
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>
17 #include "check.hxx"
18 #include "plugin.hxx"
20 /**
21 Check for calls to operator== on a std::container< std::unique_ptr >, which is not useful,
22 because std::container will compare the pointers so it is never true
25 namespace {
27 class PtrVector:
28 public loplugin::FilteringPlugin<PtrVector>
30 public:
31 explicit PtrVector(loplugin::InstantiationData const & data): FilteringPlugin(data)
34 virtual void run() override
36 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
39 bool shouldVisitTemplateInstantiations () const { return true; }
41 bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr* );
44 bool PtrVector::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr* expr)
46 if (ignoreLocation(expr)) {
47 return true;
49 if (expr->getOperator() != clang::OverloadedOperatorKind::OO_EqualEqual
50 && expr->getOperator() != clang::OverloadedOperatorKind::OO_ExclaimEqual)
52 return true;
54 if (isa<CXXNullPtrLiteralExpr>(expr->getArg(1))) {
55 return true;
57 const Expr* argExpr = expr->getArg(0);
58 std::string s = argExpr->getType().getDesugaredType(compiler.getASTContext()).getAsString();
59 if (s.find("iterator") != std::string::npos
60 || (loplugin::TypeCheck(argExpr->getType()).Class("__wrap_iter").Namespace("__1")
61 .StdNamespace()))
63 return true;
65 if (s.find("array") == std::string::npos && s.find("deque") == std::string::npos
66 && s.find("list") == std::string::npos && s.find("vector") == std::string::npos
67 && s.find("set") == std::string::npos && s.find("map") == std::string::npos
68 && s.find("stack") == std::string::npos && s.find("queue") == std::string::npos)
70 return true;
72 if (s.find("unique_ptr") != std::string::npos) {
73 expr->getArg(1)->dump();
74 report(
75 DiagnosticsEngine::Warning,
76 "do not call operator== on a std container containing a unique_ptr " + s,
77 expr->getExprLoc());
79 return true;
83 loplugin::Plugin::Registration< PtrVector > X("ptrvector");
87 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */