bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / sequenceloop.cxx
blob7f14d6c4d951d28a5418e3c2325a5fdc1c62626d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
9 #ifndef LO_CLANG_SHARED_PLUGINS
11 #include <cassert>
12 #include <string>
13 #include <iostream>
14 #include <fstream>
15 #include <set>
17 #include <clang/AST/CXXInheritance.h>
18 #include "plugin.hxx"
19 #include "check.hxx"
21 /**
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.
27 namespace
29 class SequenceLoop : public loplugin::FilteringPlugin<SequenceLoop>
31 public:
32 explicit SequenceLoop(loplugin::InstantiationData const& data)
33 : FilteringPlugin(data)
37 virtual void run() override
39 if (preRun())
40 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
43 bool VisitCXXForRangeStmt(CXXForRangeStmt const*);
46 bool SequenceLoop::VisitCXXForRangeStmt(CXXForRangeStmt const* forStmt)
48 if (ignoreLocation(forStmt))
49 return true;
51 auto tc = loplugin::TypeCheck(forStmt->getRangeInit()->getType());
52 if (tc.Const())
53 return true;
54 if (!tc.Class("Sequence")
55 .Namespace("uno")
56 .Namespace("star")
57 .Namespace("sun")
58 .Namespace("com")
59 .GlobalNamespace())
60 return true;
61 const VarDecl* varDecl = forStmt->getLoopVariable();
62 auto tc2 = loplugin::TypeCheck(varDecl->getType());
63 if (!tc2.LvalueReference().Const())
64 return true;
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();
71 return true;
74 loplugin::Plugin::Registration<SequenceLoop> sequenceloop("sequenceloop");
76 } // namespace
78 #endif // LO_CLANG_SHARED_PLUGINS
80 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */