[Libreoffice-commits] core.git: Branch 'feature/vclref' - compilerplugins/clang include/vcl vcl/source

Noel Grandin noel at peralex.com
Fri Jan 9 02:45:20 PST 2015


 compilerplugins/clang/vclwidgets.cxx |   53 +++++++++++++----------------------
 include/vcl/dialog.hxx               |    4 +-
 include/vcl/edit.hxx                 |    2 -
 vcl/source/control/edit.cxx          |    4 +-
 vcl/source/window/dialog.cxx         |    8 ++---
 5 files changed, 30 insertions(+), 41 deletions(-)

New commits:
commit a55658c2100aed2cac4389a3c5ebd8185b7d4c23
Author: Noel Grandin <noel at peralex.com>
Date:   Fri Jan 9 11:33:52 2015 +0200

    vcl:compilerplugin: new rule, no passing of vcl::Window by VclPtr
    
    to prevent problems with accidentally deleting an object by doing this:
        Button *pButton = new Button(NULL);
    	...
    	pButton->callAMethodThatTakesARef(pButton);
    Since we take a ref as we construct a temporary VclReference<> - but
    this will dispose & delete the pButton as we return to the frame doing
    the callAMethod
    
    Change-Id: I60fc211b27fe7ff463aa58f1da106f430fc65529

diff --git a/compilerplugins/clang/vclwidgets.cxx b/compilerplugins/clang/vclwidgets.cxx
index 896cd3f..1f8e05b 100644
--- a/compilerplugins/clang/vclwidgets.cxx
+++ b/compilerplugins/clang/vclwidgets.cxx
@@ -37,8 +37,6 @@ public:
 
     bool VisitParmVarDecl(ParmVarDecl const * decl);
 
-    bool VisitVarDecl( const VarDecl* var );
-
     bool VisitFunctionDecl( const FunctionDecl* var );
 };
 
@@ -90,7 +88,7 @@ bool VCLWidgets::VisitCXXRecordDecl(const CXXRecordDecl * recordDecl) {
     }
     bool foundVclPtr = false;
     for(auto fieldDecl : recordDecl->fields()) {
-        if (fieldDecl->getType().getAsString().find("VclPtr")==0) {
+        if (fieldDecl->getType().getAsString().find("VclPtr") != std::string::npos) {
            foundVclPtr = true;
            break;
         }
@@ -153,38 +151,24 @@ bool VCLWidgets::VisitFieldDecl(const FieldDecl * fieldDecl) {
     return true;
 }
 
-bool VCLWidgets::VisitParmVarDecl(ParmVarDecl const * pvDecl) {
+bool VCLWidgets::VisitParmVarDecl(ParmVarDecl const * pvDecl)
+{
     if (ignoreLocation(pvDecl)) {
         return true;
     }
-    // check if this parameter is derived from Window
-    if (isPointerToWindowSubclass(pvDecl->getType())) {
-        report(
-            DiagnosticsEngine::Remark,
-            "vcl::Window subclass passed as a pointer parameter, should be wrapped in VclPtr.",
-            pvDecl->getLocation())
-          << pvDecl->getSourceRange();
-    }
-    return true;
-}
-
-bool VCLWidgets::VisitVarDecl( const VarDecl* varDecl )
-{
-    if (ignoreLocation(varDecl)) {
+    // ignore the stuff in the VclPtr template class
+    const CXXMethodDecl *pMethodDecl = dyn_cast<CXXMethodDecl>(pvDecl->getDeclContext());
+    if (pMethodDecl
+        && pMethodDecl->getParent()->getQualifiedNameAsString().find("VclPtr") != std::string::npos) {
         return true;
     }
-    if (!varDecl->isLocalVarDecl())
-        return true;
-
-    // check if this variables type is derived from Window
-    if (isPointerToWindowSubclass(varDecl->getType())) {
+    if (pvDecl->getType().getAsString().find("VclPtr") != std::string::npos) {
         report(
-            DiagnosticsEngine::Remark,
-            "vcl::Window subclass declared as a pointer var, should be wrapped in VclPtr.",
-            varDecl->getLocation())
-          << varDecl->getSourceRange();
+            DiagnosticsEngine::Warning,
+            "vcl::Window subclass passed as a VclPtr parameter, should be passed as a raw pointer.",
+            pvDecl->getLocation())
+          << pvDecl->getSourceRange();
     }
-
     return true;
 }
 
@@ -193,12 +177,17 @@ bool VCLWidgets::VisitFunctionDecl( const FunctionDecl* functionDecl )
     if (ignoreLocation(functionDecl)) {
         return true;
     }
+    // ignore the stuff in the VclPtr template class
+    const CXXMethodDecl *pMethodDecl = dyn_cast<CXXMethodDecl>(functionDecl);
+    if (pMethodDecl
+        && pMethodDecl->getParent()->getQualifiedNameAsString().find("VclPtr") != std::string::npos) {
+        return true;
+    }
     QualType t1 { compat::getReturnType(*functionDecl) };
-    // check if this variables type is derived from Window
-    if (isPointerToWindowSubclass(t1)) {
+    if (t1.getAsString().find("VclPtr") != std::string::npos) {
         report(
-            DiagnosticsEngine::Remark,
-            "vcl::Window subclass declared as a return type from a method/function, should be wrapped in VclPtr.",
+            DiagnosticsEngine::Warning,
+            "VclPtr declared as a return type from a method/function, should be passed as a raw pointer.",
             functionDecl->getLocation())
           << functionDecl->getSourceRange();
     }
diff --git a/include/vcl/dialog.hxx b/include/vcl/dialog.hxx
index cf1c6ff..722fc1a 100644
--- a/include/vcl/dialog.hxx
+++ b/include/vcl/dialog.hxx
@@ -77,8 +77,8 @@ protected:
 
 protected:
     friend class VclBuilder;
-    void set_action_area(const VclPtr<VclButtonBox> &xBox);
-    void set_content_area(const VclPtr<VclBox> &xBox);
+    void set_action_area(VclButtonBox* pBox);
+    void set_content_area(VclBox* pBox);
 
 public:
     explicit        Dialog( vcl::Window* pParent, WinBits nStyle = WB_STDDIALOG );
diff --git a/include/vcl/edit.hxx b/include/vcl/edit.hxx
index 59d1ce0..78c7fd2 100644
--- a/include/vcl/edit.hxx
+++ b/include/vcl/edit.hxx
@@ -236,7 +236,7 @@ public:
     virtual const Link& GetModifyHdl() const { return maModifyHdl; }
     virtual void        SetUpdateDataHdl( const Link& rLink ) { maUpdateDataHdl = rLink; }
 
-    void                SetSubEdit( const VclPtr<Edit>& pEdit );
+    void                SetSubEdit( Edit* pEdit );
     Edit*               GetSubEdit() const { return mpSubEdit; }
 
     boost::signals2::signal< void ( Edit* ) > autocompleteSignal;
diff --git a/vcl/source/control/edit.cxx b/vcl/source/control/edit.cxx
index 3f17870..0ba3df2 100644
--- a/vcl/source/control/edit.cxx
+++ b/vcl/source/control/edit.cxx
@@ -2706,10 +2706,10 @@ void Edit::ClearModifyFlag()
         mbModified = false;
 }
 
-void Edit::SetSubEdit( const VclPtr<Edit>& pEdit )
+void Edit::SetSubEdit( Edit* pEdit )
 {
     mpSubEdit.disposeAndClear();
-    mpSubEdit = pEdit;
+    mpSubEdit.set( pEdit );
     if ( mpSubEdit )
     {
         SetPointer( POINTER_ARROW );    // Nur das SubEdit hat den BEAM...
diff --git a/vcl/source/window/dialog.cxx b/vcl/source/window/dialog.cxx
index 4526f8a..50c982e 100644
--- a/vcl/source/window/dialog.cxx
+++ b/vcl/source/window/dialog.cxx
@@ -513,14 +513,14 @@ Dialog::Dialog(vcl::Window* pParent, WinBits nStyle)
     ImplInit( pParent, nStyle );
 }
 
-void Dialog::set_action_area(const VclPtr<VclButtonBox> &xBox)
+void Dialog::set_action_area(VclButtonBox* pBox)
 {
-    mpActionArea = xBox;
+    mpActionArea.set(pBox);
 }
 
-void Dialog::set_content_area(const VclPtr<VclBox> &xBox)
+void Dialog::set_content_area(VclBox* pBox)
 {
-    mpContentArea = xBox;
+    mpContentArea.set(pBox);
 }
 
 void Dialog::settingOptimalLayoutSize(VclBox *pBox)


More information about the Libreoffice-commits mailing list