1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
9 #ifndef LO_CLANG_SHARED_PLUGINS
15 // For std::array or std::vector x, replace &x[0] with x.data().
19 class Data final
: public loplugin::FilteringPlugin
<Data
>
22 explicit Data(loplugin::InstantiationData
const& data
)
23 : FilteringPlugin(data
)
27 bool VisitUnaryOperator(UnaryOperator
const* expr
)
29 if (ignoreLocation(expr
))
33 if (expr
->getOpcode() != UO_AddrOf
)
37 auto const e1
= dyn_cast
<CXXOperatorCallExpr
>(expr
->getSubExpr()->IgnoreParenImpCasts());
42 if (e1
->getOperator() != OO_Subscript
)
46 auto const t
= e1
->getArg(0)->getType();
47 auto const chk
= loplugin::TypeCheck(t
);
48 if (!(chk
.Class("array").StdNamespace() || chk
.Class("vector").StdNamespace()))
52 auto const e2
= e1
->getArg(1);
53 if (e2
->isValueDependent())
58 if (!compat::EvaluateAsInt(e2
, v
, compiler
.getASTContext()))
66 report(DiagnosticsEngine::Warning
,
67 "use 'data' member function to access first element of %0", expr
->getExprLoc())
68 << t
<< expr
->getSourceRange();
72 bool preRun() override
{ return compiler
.getLangOpts().CPlusPlus
; }
78 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
83 static loplugin::Plugin::Registration
<Data
> data("data");
87 #endif // LO_CLANG_SHARED_PLUGINS
89 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */