[Libreoffice-commits] core.git: 2 commits - compilerplugins/clang connectivity/source

Noel Grandin noel.grandin at collabora.co.uk
Thu May 3 06:26:11 UTC 2018


 compilerplugins/clang/test/useuniqueptr.cxx    |   10 ++++++++++
 compilerplugins/clang/useuniqueptr.cxx         |   13 +++++++++++--
 connectivity/source/drivers/dbase/DIndex.cxx   |    6 +-----
 connectivity/source/drivers/dbase/DIndexes.cxx |    4 ++--
 connectivity/source/drivers/dbase/DTable.cxx   |    6 ++----
 connectivity/source/drivers/file/FTable.cxx    |   10 ++++------
 connectivity/source/inc/dbase/DIndex.hxx       |    2 +-
 connectivity/source/inc/dbase/DTable.hxx       |    2 +-
 connectivity/source/inc/file/FTable.hxx        |    4 ++--
 9 files changed, 34 insertions(+), 23 deletions(-)

New commits:
commit b66ba0f0f297f7190f8d969486e07ede52987188
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date:   Wed May 2 15:42:39 2018 +0200

    make createStream_simpleError return std::unique_ptr
    
    Change-Id: Ifb10f9806eeb3a54f8ed11ec0e65f9465cac3c5d
    Reviewed-on: https://gerrit.libreoffice.org/53765
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/connectivity/source/drivers/dbase/DIndex.cxx b/connectivity/source/drivers/dbase/DIndex.cxx
index e07402af2198..9468c3d3ad25 100644
--- a/connectivity/source/drivers/dbase/DIndex.cxx
+++ b/connectivity/source/drivers/dbase/DIndex.cxx
@@ -310,11 +310,7 @@ void ODbaseIndex::Release(bool bSave)
 
 void ODbaseIndex::closeImpl()
 {
-    if(m_pFileStream)
-    {
-        delete m_pFileStream;
-        m_pFileStream = nullptr;
-    }
+    m_pFileStream.reset();
 }
 
 ONDXPage* ODbaseIndex::CreatePage(sal_uInt32 nPagePos, ONDXPage* pParent, bool bLoad)
diff --git a/connectivity/source/drivers/dbase/DIndexes.cxx b/connectivity/source/drivers/dbase/DIndexes.cxx
index ee6a12f1906d..643c67eeee40 100644
--- a/connectivity/source/drivers/dbase/DIndexes.cxx
+++ b/connectivity/source/drivers/dbase/DIndexes.cxx
@@ -52,7 +52,7 @@ sdbcx::ObjectType ODbaseIndexes::createObject(const OUString& _rName)
     }
 
     sdbcx::ObjectType xRet;
-    SvStream* pFileStream = ::connectivity::file::OFileTable::createStream_simpleError(sFile, StreamMode::READ | StreamMode::NOCREATE | StreamMode::SHARE_DENYWRITE);
+    std::unique_ptr<SvStream> pFileStream = ::connectivity::file::OFileTable::createStream_simpleError(sFile, StreamMode::READ | StreamMode::NOCREATE | StreamMode::SHARE_DENYWRITE);
     if(pFileStream)
     {
         pFileStream->SetEndian(SvStreamEndian::LITTLE);
@@ -61,7 +61,7 @@ sdbcx::ObjectType ODbaseIndexes::createObject(const OUString& _rName)
 
         pFileStream->Seek(0);
         ReadHeader(*pFileStream, aHeader);
-        delete pFileStream;
+        pFileStream.reset();
 
         ODbaseIndex* pIndex = new ODbaseIndex(m_pTable,aHeader,_rName);
         xRet = pIndex;
diff --git a/connectivity/source/drivers/dbase/DTable.cxx b/connectivity/source/drivers/dbase/DTable.cxx
index 9bcec1816b39..87be422f133c 100644
--- a/connectivity/source/drivers/dbase/DTable.cxx
+++ b/connectivity/source/drivers/dbase/DTable.cxx
@@ -978,8 +978,7 @@ void ODbaseTable::FileClose()
     if (m_pMemoStream && m_pMemoStream->IsWritable())
         m_pMemoStream->Flush();
 
-    delete m_pMemoStream;
-    m_pMemoStream = nullptr;
+    m_pMemoStream.reset();
 
     ODbaseTable_BASE::FileClose();
 }
@@ -1383,8 +1382,7 @@ bool ODbaseTable::CreateMemoFile(const INetURLObject& aFile)
     (*m_pMemoStream).WriteUInt32( 1 );                  // pointer to the first free block
 
     m_pMemoStream->Flush();
-    delete m_pMemoStream;
-    m_pMemoStream = nullptr;
+    m_pMemoStream.reset();
     return true;
 }
 
diff --git a/connectivity/source/drivers/file/FTable.cxx b/connectivity/source/drivers/file/FTable.cxx
index 9ddadb19b86e..6358874f93f7 100644
--- a/connectivity/source/drivers/file/FTable.cxx
+++ b/connectivity/source/drivers/file/FTable.cxx
@@ -155,8 +155,7 @@ void OFileTable::FileClose()
     if (m_pFileStream && m_pFileStream->IsWritable())
         m_pFileStream->Flush();
 
-    delete m_pFileStream;
-    m_pFileStream = nullptr;
+    m_pFileStream.reset();
 
     if (m_pBuffer)
     {
@@ -191,13 +190,12 @@ void OFileTable::dropColumn(sal_Int32 /*_nPos*/)
 }
 
 
-SvStream* OFileTable::createStream_simpleError( const OUString& _rFileName, StreamMode _eOpenMode)
+std::unique_ptr<SvStream> OFileTable::createStream_simpleError( const OUString& _rFileName, StreamMode _eOpenMode)
 {
-    SvStream* pReturn = ::utl::UcbStreamHelper::CreateStream( _rFileName, _eOpenMode, bool(_eOpenMode & StreamMode::NOCREATE));
+    std::unique_ptr<SvStream> pReturn(::utl::UcbStreamHelper::CreateStream( _rFileName, _eOpenMode, bool(_eOpenMode & StreamMode::NOCREATE)));
     if (pReturn && (ERRCODE_NONE != pReturn->GetErrorCode()))
     {
-        delete pReturn;
-        pReturn = nullptr;
+        pReturn.reset();
     }
     return pReturn;
 }
diff --git a/connectivity/source/inc/dbase/DIndex.hxx b/connectivity/source/inc/dbase/DIndex.hxx
index 6e4356c4c3ee..bcb273aec506 100644
--- a/connectivity/source/inc/dbase/DIndex.hxx
+++ b/connectivity/source/inc/dbase/DIndex.hxx
@@ -68,7 +68,7 @@ namespace connectivity
             };
 
         private:
-            SvStream*       m_pFileStream;                  // Stream to read/write the index
+            std::unique_ptr<SvStream> m_pFileStream;        // Stream to read/write the index
             NDXHeader       m_aHeader;
             std::vector<ONDXPage*>
                             m_aCollector;                   // Pool of obsolete pages
diff --git a/connectivity/source/inc/dbase/DTable.hxx b/connectivity/source/inc/dbase/DTable.hxx
index 962a66a48f63..1a1e45c64e99 100644
--- a/connectivity/source/inc/dbase/DTable.hxx
+++ b/connectivity/source/inc/dbase/DTable.hxx
@@ -107,7 +107,7 @@ namespace connectivity
             std::vector<sal_Int32> m_aRealFieldLengths;
             DBFHeader       m_aHeader;
             DBFMemoHeader   m_aMemoHeader;
-            SvStream*       m_pMemoStream;
+            std::unique_ptr<SvStream> m_pMemoStream;
             rtl_TextEncoding m_eEncoding;
 
             void alterColumn(sal_Int32 index,
diff --git a/connectivity/source/inc/file/FTable.hxx b/connectivity/source/inc/file/FTable.hxx
index a13437a052fd..3495c79c24c9 100644
--- a/connectivity/source/inc/file/FTable.hxx
+++ b/connectivity/source/inc/file/FTable.hxx
@@ -39,7 +39,7 @@ namespace connectivity
         {
         protected:
             OConnection*                                        m_pConnection;
-            SvStream*                                           m_pFileStream;
+            std::unique_ptr<SvStream>                           m_pFileStream;
             ::rtl::Reference<OSQLColumns>                           m_aColumns;
             sal_Int32                                           m_nFilePos;                 // current IResultSetHelper::Movement
             sal_uInt8*                                          m_pBuffer;
@@ -100,7 +100,7 @@ namespace connectivity
 
             // creates a stream using ::utl::UcbStreamHelper::CreateStream, but the error is simplified
             // (NULL or non-NULL is returned)
-            static SvStream* createStream_simpleError( const OUString& _rFileName, StreamMode _eOpenMode);
+            static std::unique_ptr<SvStream> createStream_simpleError( const OUString& _rFileName, StreamMode _eOpenMode);
         };
     }
 }
commit ae680f7c7d45b7f6cff4dc458d5ad37c5f777948
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date:   Wed May 2 14:46:26 2018 +0200

    make useuniqueptr loplugin check child compound statements
    
    where the child compound statement is unconditional
    
    Change-Id: I755e7ee9134bde81811a694d42a996d3eaae3fc2
    Reviewed-on: https://gerrit.libreoffice.org/53763
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/compilerplugins/clang/test/useuniqueptr.cxx b/compilerplugins/clang/test/useuniqueptr.cxx
index a68cea3201d0..8ef71175bb99 100644
--- a/compilerplugins/clang/test/useuniqueptr.cxx
+++ b/compilerplugins/clang/test/useuniqueptr.cxx
@@ -162,4 +162,14 @@ class Foo13 {
         DELETEZ(m_pbar2); // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
     }
 };
+// check for unconditional inner compound statements
+class Foo14 {
+    int * m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
+    ~Foo14()
+    {
+        {
+            delete m_pbar1; // 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 0aab2a644bfb..ce09b5873cbb 100644
--- a/compilerplugins/clang/useuniqueptr.cxx
+++ b/compilerplugins/clang/useuniqueptr.cxx
@@ -52,6 +52,7 @@ public:
     bool VisitCXXMethodDecl(const CXXMethodDecl* );
     bool VisitCompoundStmt(const CompoundStmt* );
 private:
+    void CheckCompoundStmt(const CXXMethodDecl*, const CompoundStmt* );
     void CheckForUnconditionalDelete(const CXXMethodDecl*, const CompoundStmt* );
     void CheckForSimpleDelete(const CXXMethodDecl*, const CompoundStmt* );
     void CheckRangedLoopDelete(const CXXMethodDecl*, const CXXForRangeStmt* );
@@ -74,6 +75,13 @@ bool UseUniquePtr::VisitCXXMethodDecl(const CXXMethodDecl* methodDecl)
     if (!compoundStmt || compoundStmt->size() == 0)
         return true;
 
+    CheckCompoundStmt(methodDecl, compoundStmt);
+
+    return true;
+}
+
+void UseUniquePtr::CheckCompoundStmt(const CXXMethodDecl* methodDecl, const CompoundStmt* compoundStmt)
+{
     CheckForSimpleDelete(methodDecl, compoundStmt);
 
     for (auto i = compoundStmt->body_begin(); i != compoundStmt->body_end(); ++i)
@@ -84,9 +92,10 @@ bool UseUniquePtr::VisitCXXMethodDecl(const CXXMethodDecl* methodDecl)
             CheckLoopDelete(methodDecl, forStmt->getBody());
         else if (auto whileStmt = dyn_cast<WhileStmt>(*i))
             CheckLoopDelete(methodDecl, whileStmt->getBody());
+        // check for unconditional inner compound statements
+        else if (auto innerCompoundStmt = dyn_cast<CompoundStmt>(*i))
+            CheckCompoundStmt(methodDecl, innerCompoundStmt);
     }
-
-    return true;
 }
 
 /**


More information about the Libreoffice-commits mailing list