From d1e47b1428abf1732ab4d5e219b210760d4152e0 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Mon, 22 May 2017 13:25:58 +0200 Subject: enhance useuniqueptr loplugin teach it to look for the following sequence in a destructor: delete m_pfoo; m_pfoo = nullptr; Change-Id: Icd6271a63a024e32b53cc9e599f8f59952160380 Reviewed-on: https://gerrit.libreoffice.org/37900 Tested-by: Jenkins Reviewed-by: Noel Grandin --- compilerplugins/clang/useuniqueptr.cxx | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'compilerplugins/clang/useuniqueptr.cxx') diff --git a/compilerplugins/clang/useuniqueptr.cxx b/compilerplugins/clang/useuniqueptr.cxx index 59d1a3f3ec68..afae4c3c770a 100644 --- a/compilerplugins/clang/useuniqueptr.cxx +++ b/compilerplugins/clang/useuniqueptr.cxx @@ -44,30 +44,32 @@ bool UseUniquePtr::VisitCXXDestructorDecl(const CXXDestructorDecl* destructorDec if (isInUnoIncludeFile(destructorDecl)) return true; - if (destructorDecl->getBody() == nullptr) + const CompoundStmt* compoundStmt = dyn_cast_or_null< CompoundStmt >( destructorDecl->getBody() ); + if (!compoundStmt) return true; - const CompoundStmt* compoundStmt = dyn_cast< CompoundStmt >( destructorDecl->getBody() ); - if (compoundStmt == nullptr) { - return true; - } - const CXXDeleteExpr* deleteExpr; + const CXXDeleteExpr* deleteExpr = nullptr; if (compoundStmt->size() == 1) { deleteExpr = dyn_cast(compoundStmt->body_front()); } else if (compoundStmt->size() == 2) { // ignore SAL_INFO type stuff - // TODO should probably be a little more specific here - if (!isa(compoundStmt->body_front())) { - return true; + // @TODO should probably be a little more specific here + if (isa(compoundStmt->body_front())) { + deleteExpr = dyn_cast(compoundStmt->body_back()); + } + // look for the following pattern: + // delete m_pbar; + // m_pbar = nullptr; + else if (auto binaryOp = dyn_cast(compoundStmt->body_back())) { + if (binaryOp->getOpcode() == BO_Assign) + deleteExpr = dyn_cast(compoundStmt->body_front()); } - deleteExpr = dyn_cast(compoundStmt->body_back()); } else { return true; } - if (deleteExpr == nullptr) { + if (deleteExpr == nullptr) return true; - } const ImplicitCastExpr* pCastExpr = dyn_cast(deleteExpr->getArgument()); if (!pCastExpr) -- cgit