bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / rangedforcopy.cxx
bloba8a6e7d6cfbb553937af03e74e34ea47f5c6006b
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 */
11 #include <string>
12 #include <iostream>
14 #include "plugin.hxx"
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.
21 namespace
24 class RangedForCopy:
25 public loplugin::FilteringPlugin<RangedForCopy>
27 public:
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 ))
41 return true;
43 const VarDecl* varDecl = stmt->getLoopVariable();
44 if (!varDecl)
45 return true;
47 const QualType type = varDecl->getType();
48 if (type->isRecordType() && !type->isReferenceType() && !type->isPointerType())
50 std::string name = type.getAsString();
51 report(
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();
58 return true;
62 loplugin::Plugin::Registration< RangedForCopy > X("rangedforcopy");
66 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */