[en_US] Added 13 autocorrect words
[LibreOffice.git] / compilerplugins / clang / rangedforcopy.cxx
blob68004a1708ae1ef3685567dd943c866d682f44e1
2 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
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/.
9 */
10 #ifndef LO_CLANG_SHARED_PLUGINS
12 #include <string>
13 #include <iostream>
15 #include "plugin.hxx"
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.
22 namespace
25 class RangedForCopy:
26 public loplugin::FilteringPlugin<RangedForCopy>
28 public:
29 explicit RangedForCopy(loplugin::InstantiationData const & data):
30 FilteringPlugin(data) {}
32 virtual void run() override {
33 if (preRun())
34 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
37 bool VisitCXXForRangeStmt( const CXXForRangeStmt* stmt );
40 bool RangedForCopy::VisitCXXForRangeStmt( const CXXForRangeStmt* stmt )
42 if (ignoreLocation( stmt ))
43 return true;
45 const VarDecl* varDecl = stmt->getLoopVariable();
46 if (!varDecl)
47 return true;
49 const QualType type = varDecl->getType();
50 if (type->isRecordType() && !type->isReferenceType() && !type->isPointerType())
52 std::string name = type.getAsString();
53 report(
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();
60 return true;
64 loplugin::Plugin::Registration< RangedForCopy > rangedforcopy("rangedforcopy");
66 } // namespace
68 #endif // LO_CLANG_SHARED_PLUGINS
70 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */