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/.
10 #ifndef LO_CLANG_SHARED_PLUGINS
16 #include "clang/AST/CXXInheritance.h"
18 // Check that we're not unnecessarily copying variables in a range based for loop
19 // e.g. "for (OUString a: aList)" results in a copy of each string being made,
20 // whereas "for (const OUString& a: aList)" does not.
26 public loplugin::FilteringPlugin
<RangedForCopy
>
29 explicit RangedForCopy(loplugin::InstantiationData
const & data
):
30 FilteringPlugin(data
) {}
32 virtual void run() override
{
34 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
37 bool VisitCXXForRangeStmt( const CXXForRangeStmt
* stmt
);
40 bool RangedForCopy::VisitCXXForRangeStmt( const CXXForRangeStmt
* stmt
)
42 if (ignoreLocation( stmt
))
45 const VarDecl
* varDecl
= stmt
->getLoopVariable();
49 const QualType type
= varDecl
->getType();
50 if (type
->isRecordType() && !type
->isReferenceType() && !type
->isPointerType())
52 std::string name
= type
.getAsString();
54 DiagnosticsEngine::Warning
,
55 "Loop variable passed by value, pass by reference instead, e.g. 'const %0&'",
56 compat::getBeginLoc(varDecl
))
57 << name
<< varDecl
->getSourceRange();
64 loplugin::Plugin::Registration
< RangedForCopy
> rangedforcopy("rangedforcopy");
68 #endif // LO_CLANG_SHARED_PLUGINS
70 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */