diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2017-05-22 13:25:58 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2017-05-23 09:31:20 +0200 |
commit | d1e47b1428abf1732ab4d5e219b210760d4152e0 (patch) | |
tree | 8eac1def834ba548c45a8a1a18e8e39d45eedc1d /compilerplugins/clang/useuniqueptr.cxx | |
parent | 919a4ef592b6026a7533a93682f39118fef29ce8 (diff) |
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 <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins/clang/useuniqueptr.cxx')
-rw-r--r-- | compilerplugins/clang/useuniqueptr.cxx | 26 |
1 files changed, 14 insertions, 12 deletions
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<CXXDeleteExpr>(compoundStmt->body_front()); } else if (compoundStmt->size() == 2) { // ignore SAL_INFO type stuff - // TODO should probably be a little more specific here - if (!isa<DoStmt>(compoundStmt->body_front())) { - return true; + // @TODO should probably be a little more specific here + if (isa<DoStmt>(compoundStmt->body_front())) { + deleteExpr = dyn_cast<CXXDeleteExpr>(compoundStmt->body_back()); + } + // look for the following pattern: + // delete m_pbar; + // m_pbar = nullptr; + else if (auto binaryOp = dyn_cast<BinaryOperator>(compoundStmt->body_back())) { + if (binaryOp->getOpcode() == BO_Assign) + deleteExpr = dyn_cast<CXXDeleteExpr>(compoundStmt->body_front()); } - deleteExpr = dyn_cast<CXXDeleteExpr>(compoundStmt->body_back()); } else { return true; } - if (deleteExpr == nullptr) { + if (deleteExpr == nullptr) return true; - } const ImplicitCastExpr* pCastExpr = dyn_cast<ImplicitCastExpr>(deleteExpr->getArgument()); if (!pCastExpr) |