update credits
[LibreOffice.git] / compilerplugins / clang / data.cxx
blob9cff5426fc1d78c5eefba80c2e59fd3a961eb82c
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 */
9 #ifndef LO_CLANG_SHARED_PLUGINS
11 #include "check.hxx"
12 #include "compat.hxx"
13 #include "plugin.hxx"
15 // For std::array or std::vector x, replace &x[0] with x.data().
17 namespace
19 class Data final : public loplugin::FilteringPlugin<Data>
21 public:
22 explicit Data(loplugin::InstantiationData const& data)
23 : FilteringPlugin(data)
27 bool VisitUnaryOperator(UnaryOperator const* expr)
29 if (ignoreLocation(expr))
31 return true;
33 if (expr->getOpcode() != UO_AddrOf)
35 return true;
37 auto const e1 = dyn_cast<CXXOperatorCallExpr>(expr->getSubExpr()->IgnoreParenImpCasts());
38 if (e1 == nullptr)
40 return true;
42 if (e1->getOperator() != OO_Subscript)
44 return true;
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()))
50 return true;
52 auto const e2 = e1->getArg(1);
53 if (e2->isValueDependent())
55 return true;
57 APSInt v;
58 if (!compat::EvaluateAsInt(e2, v, compiler.getASTContext()))
60 return true;
62 if (v != 0)
64 return true;
66 report(DiagnosticsEngine::Warning,
67 "use 'data' member function to access first element of %0", expr->getExprLoc())
68 << t << expr->getSourceRange();
69 return true;
72 bool preRun() override { return compiler.getLangOpts().CPlusPlus; }
74 void run() override
76 if (preRun())
78 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
83 static loplugin::Plugin::Registration<Data> data("data");
85 } // namespace
87 #endif // LO_CLANG_SHARED_PLUGINS
89 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */