2 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * This file is part of the LibreOffice project.
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 #include "clang/AST/CXXInheritance.h"
17 // Check that we're not unnecessarily copying variables in a range based for loop
18 // e.g. "for (OUString a: aList)" results in a copy of each string being made,
19 // whereas "for (const OUString& a: aList)" does not.
25 public loplugin::FilteringPlugin
<RangedForCopy
>
28 explicit RangedForCopy(loplugin::InstantiationData
const & data
):
29 FilteringPlugin(data
) {}
31 virtual void run() override
{
32 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
35 bool VisitCXXForRangeStmt( const CXXForRangeStmt
* stmt
);
38 bool RangedForCopy::VisitCXXForRangeStmt( const CXXForRangeStmt
* stmt
)
40 if (ignoreLocation( stmt
))
43 const VarDecl
* varDecl
= stmt
->getLoopVariable();
47 const QualType type
= varDecl
->getType();
48 if (type
->isRecordType() && !type
->isReferenceType() && !type
->isPointerType())
50 std::string name
= type
.getAsString();
52 DiagnosticsEngine::Warning
,
53 "Loop variable passed by value, pass by reference instead, e.g. 'const %0&'",
54 compat::getBeginLoc(varDecl
))
55 << name
<< varDecl
->getSourceRange();
62 loplugin::Plugin::Registration
< RangedForCopy
> X("rangedforcopy");
66 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */