1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 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 10 #ifndef LO_CLANG_SHARED_PLUGINS 11 12 #include "plugin.hxx" 13 14 namespace { 15 16 class DerefNullPtr: 17 public loplugin::FilteringPlugin<DerefNullPtr> 18 { 19 public: DerefNullPtr(loplugin::InstantiationData const & data)20 explicit DerefNullPtr(loplugin::InstantiationData const & data): 21 FilteringPlugin(data) {} 22 run()23 void run() override 24 { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } 25 26 bool VisitUnaryOperator(UnaryOperator const * op); 27 }; 28 VisitUnaryOperator(UnaryOperator const * op)29bool DerefNullPtr::VisitUnaryOperator(UnaryOperator const * op) { 30 if (op->getOpcode() != UO_Deref) { 31 return true; 32 } 33 if (!ignoreLocation(op) 34 && (op->getSubExpr()->IgnoreParenCasts()->isNullPointerConstant( 35 compiler.getASTContext(), Expr::NPC_ValueDependentIsNotNull/*TODO*/) 36 != Expr::NPCK_NotNull)) 37 { 38 report( 39 DiagnosticsEngine::Warning, "null pointer dereference", 40 compat::getBeginLoc(op)) 41 << op->getSourceRange(); 42 } 43 return true; 44 } 45 46 loplugin::Plugin::Registration<DerefNullPtr> derefnullptr("derefnullptr"); 47 48 } 49 50 #endif // LO_CLANG_SHARED_PLUGINS 51 52 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 53