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. 4 * 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 10 11 #include "check.hxx" 12 #include "compat.hxx" 13 #include "plugin.hxx" 14 15 // For std::array or std::vector x, replace &x[0] with x.data(). 16 17 namespace 18 { 19 class Data final : public loplugin::FilteringPlugin<Data> 20 { 21 public: Data(loplugin::InstantiationData const & data)22 explicit Data(loplugin::InstantiationData const& data) 23 : FilteringPlugin(data) 24 { 25 } 26 VisitUnaryOperator(UnaryOperator const * expr)27 bool VisitUnaryOperator(UnaryOperator const* expr) 28 { 29 if (ignoreLocation(expr)) 30 { 31 return true; 32 } 33 if (expr->getOpcode() != UO_AddrOf) 34 { 35 return true; 36 } 37 auto const e1 = dyn_cast<CXXOperatorCallExpr>(expr->getSubExpr()->IgnoreParenImpCasts()); 38 if (e1 == nullptr) 39 { 40 return true; 41 } 42 if (e1->getOperator() != OO_Subscript) 43 { 44 return true; 45 } 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())) 49 { 50 return true; 51 } 52 auto const e2 = e1->getArg(1); 53 if (e2->isValueDependent()) 54 { 55 return true; 56 } 57 APSInt v; 58 if (!compat::EvaluateAsInt(e2, v, compiler.getASTContext())) 59 { 60 return true; 61 } 62 if (v != 0) 63 { 64 return true; 65 } 66 report(DiagnosticsEngine::Warning, 67 "use 'data' member function to access first element of %0", expr->getExprLoc()) 68 << t << expr->getSourceRange(); 69 return true; 70 } 71 preRun()72 bool preRun() override { return compiler.getLangOpts().CPlusPlus; } 73 run()74 void run() override 75 { 76 if (preRun()) 77 { 78 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); 79 } 80 } 81 }; 82 83 static loplugin::Plugin::Registration<Data> data("data"); 84 85 } // namespace 86 87 #endif // LO_CLANG_SHARED_PLUGINS 88 89 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ 90