diff options
author | Bjoern Michaelsen <bjoern.michaelsen@canonical.com> | 2014-11-19 17:46:01 +0100 |
---|---|---|
committer | Björn Michaelsen <bjoern.michaelsen@canonical.com> | 2014-11-20 18:16:02 +0000 |
commit | 395d6a96aaee78abc5c4316e010df1e8c05ceca7 (patch) | |
tree | be748e414da06eeaba15f1c3f18a9795ea9b08f1 /compilerplugins | |
parent | 2851ce5afd0f37764cbbc2c2a9a63c7adc844311 (diff) |
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@canonical.com>
Tested-by: Björn Michaelsen <bjoern.michaelsen@canonical.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/store/cascadingcondop.cxx | 34 | ||||
-rw-r--r-- | compilerplugins/clang/store/cascadingcondop.hxx | 4 |
2 files changed, 30 insertions, 8 deletions
diff --git a/compilerplugins/clang/store/cascadingcondop.cxx b/compilerplugins/clang/store/cascadingcondop.cxx index f18a93b28bb0..722b9819b60b 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 e648bdf15e50..8d41177ec051 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 ); }; |