office-gobmx/compilerplugins/clang/sequenceloop.cxx
Noel Grandin fed87058ac Revert "loplugin:sequenceloop improve rvalue detection"
This reverts commit 3aca35f150.

Reason for revert: see commentary at
   https://gerrit.libreoffice.org/c/core/+/89184
   I misunderstood - even though this is a local copy, we will still call non-const begin()/end() and cause further allocations internal to the Sequence

Change-Id: Ia9ecacc6312afc2c9c80ca72afe6182d9b70241b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/89137
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
2020-02-23 11:10:18 +01:00

80 lines
2.3 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/.
*/
#ifndef LO_CLANG_SHARED_PLUGINS
#include <cassert>
#include <string>
#include <iostream>
#include <fstream>
#include <set>
#include <clang/AST/CXXInheritance.h>
#include "plugin.hxx"
#include "check.hxx"
/**
When used in "for" loops, css::uno::Sequence objects tend to end up calling the non-const begin()/end(),
which is considerably more expensive than the const variants because it forces a local copy
of the internal ref-counted impl object.
*/
namespace
{
class SequenceLoop : public loplugin::FilteringPlugin<SequenceLoop>
{
public:
explicit SequenceLoop(loplugin::InstantiationData const& data)
: FilteringPlugin(data)
{
}
virtual void run() override
{
if (preRun())
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
}
bool VisitCXXForRangeStmt(CXXForRangeStmt const*);
};
bool SequenceLoop::VisitCXXForRangeStmt(CXXForRangeStmt const* forStmt)
{
if (ignoreLocation(forStmt))
return true;
auto tc = loplugin::TypeCheck(forStmt->getRangeInit()->getType());
if (tc.Const())
return true;
if (!tc.Class("Sequence")
.Namespace("uno")
.Namespace("star")
.Namespace("sun")
.Namespace("com")
.GlobalNamespace())
return true;
const VarDecl* varDecl = forStmt->getLoopVariable();
auto tc2 = loplugin::TypeCheck(varDecl->getType());
if (!tc2.LvalueReference().Const())
return true;
report(DiagnosticsEngine::Warning,
("use std::as_const, or otherwise make the for-range-initializer expression const, to"
" avoid creating a copy of the Sequence"),
compat::getBeginLoc(forStmt->getRangeInit()))
<< forStmt->getSourceRange();
return true;
}
loplugin::Plugin::Registration<SequenceLoop> sequenceloop("sequenceloop");
} // namespace
#endif // LO_CLANG_SHARED_PLUGINS
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */