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/.
14 // For std::array or std::vector x, replace &x[0] with x.data().
18 class Data final
: public loplugin::FilteringPlugin
<Data
>
21 explicit Data(loplugin::InstantiationData
const& data
)
22 : FilteringPlugin(data
)
26 bool VisitUnaryOperator(UnaryOperator
const* expr
)
28 if (ignoreLocation(expr
))
32 if (expr
->getOpcode() != UO_AddrOf
)
36 auto const e1
= dyn_cast
<CXXOperatorCallExpr
>(expr
->getSubExpr()->IgnoreParenImpCasts());
41 if (e1
->getOperator() != OO_Subscript
)
45 auto const t
= e1
->getArg(0)->getType();
46 auto const chk
= loplugin::TypeCheck(t
);
47 if (!(chk
.Class("array").StdNamespace() || chk
.Class("vector").StdNamespace()))
51 auto const e2
= e1
->getArg(1);
52 if (e2
->isValueDependent())
57 if (!compat::EvaluateAsInt(e2
, v
, compiler
.getASTContext()))
65 report(DiagnosticsEngine::Warning
,
66 "use 'data' member function to access first element of %0", expr
->getExprLoc())
67 << t
<< expr
->getSourceRange();
74 if (compiler
.getLangOpts().CPlusPlus
)
76 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
81 static loplugin::Plugin::Registration
<Data
> reg("data");
84 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */