diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-04-24 14:04:53 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-04-29 17:03:18 +0200 |
commit | a369e6bbd539caf59ccbb10d374c8aeab72054db (patch) | |
tree | 7fc2ef9ec6b90c93b7f642bb8c393f0dde3c4eef /compilerplugins | |
parent | 8f2788e6fcf0ebbaedef02478362dbda4e8c14c4 (diff) |
loplugin:useuniqueptr look for DELETEZ expressions
can't believe I've been missing these
Change-Id: If39827e1583cbcedfd5061a5059d6df53be0f9c8
Reviewed-on: https://gerrit.libreoffice.org/53598
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/test/useuniqueptr.cxx | 11 | ||||
-rw-r--r-- | compilerplugins/clang/useuniqueptr.cxx | 31 |
2 files changed, 42 insertions, 0 deletions
diff --git a/compilerplugins/clang/test/useuniqueptr.cxx b/compilerplugins/clang/test/useuniqueptr.cxx index ef0dde3cf538..a68cea3201d0 100644 --- a/compilerplugins/clang/test/useuniqueptr.cxx +++ b/compilerplugins/clang/test/useuniqueptr.cxx @@ -151,4 +151,15 @@ class Foo12 { delete m_pbar[i++]; // expected-error {{rather manage with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}} } }; +#define DELETEZ( p ) ( delete p,p = NULL ) +class Foo13 { + int * m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}} + int * m_pbar2; // expected-note {{member is here [loplugin:useuniqueptr]}} + ~Foo13() + { + if (m_pbar1) + DELETEZ(m_pbar1); // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}} + DELETEZ(m_pbar2); // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}} + } +}; /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/compilerplugins/clang/useuniqueptr.cxx b/compilerplugins/clang/useuniqueptr.cxx index 0b5fb2c79aa7..0aab2a644bfb 100644 --- a/compilerplugins/clang/useuniqueptr.cxx +++ b/compilerplugins/clang/useuniqueptr.cxx @@ -58,6 +58,7 @@ private: void CheckLoopDelete(const CXXMethodDecl*, const Stmt* ); void CheckLoopDelete(const CXXMethodDecl*, const CXXDeleteExpr* ); void CheckDeleteExpr(const CXXMethodDecl*, const CXXDeleteExpr*); + void CheckParenExpr(const CXXMethodDecl*, const ParenExpr*); void CheckDeleteExpr(const CXXMethodDecl*, const CXXDeleteExpr*, const MemberExpr*, StringRef message); }; @@ -101,6 +102,12 @@ void UseUniquePtr::CheckForSimpleDelete(const CXXMethodDecl* methodDecl, const C CheckDeleteExpr(methodDecl, deleteExpr); continue; } + auto parenExpr = dyn_cast<ParenExpr>(*i); + if (parenExpr) + { + CheckParenExpr(methodDecl, parenExpr); + continue; + } // Check for conditional deletes like: // if (m_pField != nullptr) delete m_pField; auto ifStmt = dyn_cast<IfStmt>(*i); @@ -132,6 +139,13 @@ void UseUniquePtr::CheckForSimpleDelete(const CXXMethodDecl* methodDecl, const C continue; } + parenExpr = dyn_cast<ParenExpr>(ifStmt->getThen()); + if (parenExpr) + { + CheckParenExpr(methodDecl, parenExpr); + continue; + } + auto ifThenCompoundStmt = dyn_cast<CompoundStmt>(ifStmt->getThen()); if (!ifThenCompoundStmt) continue; @@ -140,6 +154,9 @@ void UseUniquePtr::CheckForSimpleDelete(const CXXMethodDecl* methodDecl, const C auto ifDeleteExpr = dyn_cast<CXXDeleteExpr>(*j); if (ifDeleteExpr) CheckDeleteExpr(methodDecl, ifDeleteExpr); + ParenExpr const * parenExpr = dyn_cast<ParenExpr>(*i); + if (parenExpr) + CheckParenExpr(methodDecl, parenExpr); } } } @@ -160,6 +177,20 @@ void UseUniquePtr::CheckDeleteExpr(const CXXMethodDecl* methodDecl, const CXXDel } /** + * Look for DELETEZ expressions. + */ +void UseUniquePtr::CheckParenExpr(const CXXMethodDecl* methodDecl, const ParenExpr* parenExpr) +{ + auto binaryOp = dyn_cast<BinaryOperator>(parenExpr->getSubExpr()); + if (!binaryOp || binaryOp->getOpcode() != BO_Comma) + return; + auto deleteExpr = dyn_cast<CXXDeleteExpr>(binaryOp->getLHS()); + if (!deleteExpr) + return; + CheckDeleteExpr(methodDecl, deleteExpr); +} + +/** * Check the delete expression in a destructor. */ void UseUniquePtr::CheckDeleteExpr(const CXXMethodDecl* methodDecl, const CXXDeleteExpr* deleteExpr, |