bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / data.cxx
blob7e2f019e47a185f848fe1adb4d27bce8738ef6c7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 */
10 #include "check.hxx"
11 #include "compat.hxx"
12 #include "plugin.hxx"
14 // For std::array or std::vector x, replace &x[0] with x.data().
16 namespace
18 class Data final : public loplugin::FilteringPlugin<Data>
20 public:
21 explicit Data(loplugin::InstantiationData const& data)
22 : FilteringPlugin(data)
26 bool VisitUnaryOperator(UnaryOperator const* expr)
28 if (ignoreLocation(expr))
30 return true;
32 if (expr->getOpcode() != UO_AddrOf)
34 return true;
36 auto const e1 = dyn_cast<CXXOperatorCallExpr>(expr->getSubExpr()->IgnoreParenImpCasts());
37 if (e1 == nullptr)
39 return true;
41 if (e1->getOperator() != OO_Subscript)
43 return true;
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()))
49 return true;
51 auto const e2 = e1->getArg(1);
52 if (e2->isValueDependent())
54 return true;
56 APSInt v;
57 if (!compat::EvaluateAsInt(e2, v, compiler.getASTContext()))
59 return true;
61 if (v != 0)
63 return true;
65 report(DiagnosticsEngine::Warning,
66 "use 'data' member function to access first element of %0", expr->getExprLoc())
67 << t << expr->getSourceRange();
68 return true;
71 private:
72 void run() override
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: */