Files
libreoffice/compilerplugins/clang/ptrvector.cxx
Stephan Bergmann f260284c39 Adapt loplugin:ptrvector to libc++ (on macOS)
...where it produced false positive

> /Users/stephan/Software/lo2/core/UnoControls/source/controls/progressmonitor.cxx:245:21: error: do not call operator== on a std container containing a unique_ptr const class std::__1::__wrap_iter<class std::__1::unique_ptr<struct unocontrols::IMPL_TextlistItem, struct std::__1::default_delete<struct unocontrols::IMPL_TextlistItem> > *> [loplugin:ptrvector]
>             if (itr != maTextlist_Top.end())
>                     ^

Change-Id: I8a8b6f30145e6aff2caa03c03f0fd20af0ba42b5
Reviewed-on: https://gerrit.libreoffice.org/59181
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2018-08-16 20:22:17 +02:00

88 lines
2.6 KiB
C++

/* -*- 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 <memory>
#include <cassert>
#include <string>
#include <iostream>
#include <fstream>
#include <set>
#include "check.hxx"
#include "plugin.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 loplugin::FilteringPlugin<PtrVector>
{
public:
explicit PtrVector(loplugin::InstantiationData const & data): FilteringPlugin(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
|| (loplugin::TypeCheck(argExpr->getType()).Class("__wrap_iter").Namespace("__1")
.StdNamespace()))
{
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: */