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

Bjoern Michaelsen bjoern.michaelsen at canonical.com
Thu Nov 20 10:16:33 PST 2014


 compilerplugins/clang/store/cascadingcondop.cxx |   34 +++++++++++++++++++-----
 compilerplugins/clang/store/cascadingcondop.hxx |    4 ++
 sw/source/core/doc/doccorr.cxx                  |    9 ++++++
 3 files changed, 39 insertions(+), 8 deletions(-)

New commits:
commit 395d6a96aaee78abc5c4316e010df1e8c05ceca7
Author: Bjoern Michaelsen <bjoern.michaelsen at canonical.com>
Date:   Wed Nov 19 17:46:01 2014 +0100

    cascading conditional operators, give a hint on complexity
    
    Change-Id: Ie9d0b07a32cc17705db735ea18f70f28d57badd4
    Reviewed-on: https://gerrit.libreoffice.org/12990
    Reviewed-by: Björn Michaelsen <bjoern.michaelsen at canonical.com>
    Tested-by: Björn Michaelsen <bjoern.michaelsen at canonical.com>

diff --git a/compilerplugins/clang/store/cascadingcondop.cxx b/compilerplugins/clang/store/cascadingcondop.cxx
index f18a93b..722b981 100644
--- a/compilerplugins/clang/store/cascadingcondop.cxx
+++ b/compilerplugins/clang/store/cascadingcondop.cxx
@@ -14,8 +14,8 @@
 /*
 This is a compile check.
 
-It checks for conditional operators in conditional operators, which are
-errorprone, e.g.
+It checks for complex statements with conditional operators in conditional
+operators, which are errorprone, e.g.
  Thing foo = IsBar() ? ( IsBaz() ? b1 : b2 ) : b3;
 
 However, it finds 556 cases in sw/source alone, thus likely needs some more
@@ -24,9 +24,19 @@ a certain length in characters (but that needs the Context/SourceManager, which
 I havent played with yet).
 */
 
+// the value is rather arbitrary, but code above this number of stmts begins to
+// be smelly
+static const int stmtlimit = 50;
+
 namespace loplugin
 {
 
+struct WalkCounter
+{
+    int  stmtcount;
+    bool cascading;
+};
+
 // Ctor, nothing special, pass the argument(s).
 CascadingCondOp::CascadingCondOp( const InstantiationData& data )
     : Plugin( data )
@@ -41,20 +51,30 @@ void CascadingCondOp::run()
     TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
 }
 
-void CascadingCondOp::Walk( const Stmt* stmt )
+void CascadingCondOp::Walk( const Stmt* stmt, WalkCounter& c )
 {
     for(Stmt::const_child_iterator it = stmt->child_begin(); it != stmt->child_end(); ++it)
     {
-        if ( const ConditionalOperator* condop = dyn_cast< ConditionalOperator >( *it ))
-            report( DiagnosticsEngine::Warning, "cascading conditional operator", condop->getLocStart());
-        Walk(*it);
+        ++c.stmtcount;
+        if ( dyn_cast< ConditionalOperator >( *it ))
+            c.cascading = true;
+        Walk(*it, c);
     }
 }
 
 bool CascadingCondOp::VisitStmt( const Stmt* stmt )
 {
     if ( const ConditionalOperator* condop = dyn_cast< ConditionalOperator >( stmt ))
-        Walk(condop);
+    {
+        WalkCounter c = { 0, false };
+        Walk(condop, c);
+        if(c.cascading && c.stmtcount >= stmtlimit)
+        {
+            std::string msg("cascading conditional operator, complexity: ");
+            msg.append(std::to_string(c.stmtcount));
+            report( DiagnosticsEngine::Warning, msg, condop->getLocStart());
+        }
+    }
     return true;
 }
 
diff --git a/compilerplugins/clang/store/cascadingcondop.hxx b/compilerplugins/clang/store/cascadingcondop.hxx
index e648bdf..8d41177 100644
--- a/compilerplugins/clang/store/cascadingcondop.hxx
+++ b/compilerplugins/clang/store/cascadingcondop.hxx
@@ -17,6 +17,8 @@
 namespace loplugin
 {
 
+struct WalkCounter;
+
 // The class implementing the plugin action.
 class CascadingCondOp
     // Inherits from the Clang class that will allow examing the Clang AST tree (i.e. syntax tree).
@@ -27,7 +29,7 @@ class CascadingCondOp
     public:
         CascadingCondOp( const InstantiationData& data );
         virtual void run() override;
-        void Walk( const Stmt* stmt );
+        void Walk( const Stmt* stmt, WalkCounter& c );
         bool VisitStmt( const Stmt* stmt );
     };
 
diff --git a/sw/source/core/doc/doccorr.cxx b/sw/source/core/doc/doccorr.cxx
index 3cc38fa..981b7da 100644
--- a/sw/source/core/doc/doccorr.cxx
+++ b/sw/source/core/doc/doccorr.cxx
@@ -358,4 +358,13 @@ SwEditShell* SwDoc::GetEditShell()
     return GetEditShell();
 }
 
+//bool foo()
+//{
+//    bool b1 = true ? true : false;
+//    bool b2 = (true ? true : false) ? true : false;
+//    bool b3 = true ? (true ? true : false) : false;
+//    bool b4 = true ? true : (true ? true : false);
+//    return false;
+//}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list