summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/store/cascadingcondop.cxx34
-rw-r--r--compilerplugins/clang/store/cascadingcondop.hxx4
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 );
};