1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
9 #ifndef LO_CLANG_SHARED_PLUGINS
17 #include <clang/AST/CXXInheritance.h>
22 When used in "for" loops, css::uno::Sequence objects tend to end up calling the non-const begin()/end(),
23 which is considerably more expensive than the const variants because it forces a local copy
24 of the internal ref-counted impl object.
29 class SequenceLoop
: public loplugin::FilteringPlugin
<SequenceLoop
>
32 explicit SequenceLoop(loplugin::InstantiationData
const& data
)
33 : FilteringPlugin(data
)
37 virtual void run() override
40 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
43 bool VisitCXXForRangeStmt(CXXForRangeStmt
const*);
46 bool SequenceLoop::VisitCXXForRangeStmt(CXXForRangeStmt
const* forStmt
)
48 if (ignoreLocation(forStmt
))
51 auto tc
= loplugin::TypeCheck(forStmt
->getRangeInit()->getType());
54 if (!tc
.Class("Sequence")
61 const VarDecl
* varDecl
= forStmt
->getLoopVariable();
62 auto tc2
= loplugin::TypeCheck(varDecl
->getType());
63 if (!tc2
.LvalueReference().Const())
66 report(DiagnosticsEngine::Warning
,
67 ("use std::as_const, or otherwise make the for-range-initializer expression const, to"
68 " avoid creating a copy of the Sequence"),
69 compat::getBeginLoc(forStmt
->getRangeInit()))
70 << forStmt
->getSourceRange();
74 loplugin::Plugin::Registration
<SequenceLoop
> sequenceloop("sequenceloop");
78 #endif // LO_CLANG_SHARED_PLUGINS
80 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */