1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
7 * This file is distributed under the University of Illinois Open Source
8 * License. See LICENSE.TXT for details.
12 #include "cascadingcondop.hxx"
15 This is a compile check.
17 It checks for complex statements with conditional operators in conditional
18 operators, which are error prone, e.g.
19 Thing foo = IsBar() ? ( IsBaz() ? b1 : b2 ) : b3;
21 However, it finds 556 cases in sw/source alone, thus likely needs some more
22 restricting, e.g. by checking for multiline conditional operator statements or
23 a certain length in characters (but that needs the Context/SourceManager, which
24 I haven't played with yet).
27 // the value is rather arbitrary, but code above this number of stmts begins to
29 static const int stmtlimit
= 50;
40 // Ctor, nothing special, pass the argument(s).
41 CascadingCondOp::CascadingCondOp( const InstantiationData
& data
)
42 : FilteringPlugin( data
)
46 // Perform the actual action.
47 void CascadingCondOp::run()
49 // Traverse the whole AST of the translation unit (i.e. examine the whole source file).
50 // The Clang AST helper class will call VisitReturnStmt for every return statement.
51 TraverseDecl( compiler
.getASTContext().getTranslationUnitDecl());
54 void CascadingCondOp::Walk( const Stmt
* stmt
, WalkCounter
& c
)
56 for(Stmt::const_child_iterator it
= stmt
->child_begin(); it
!= stmt
->child_end(); ++it
)
59 if ( dyn_cast
< ConditionalOperator
>( *it
))
65 bool CascadingCondOp::VisitStmt( const Stmt
* stmt
)
67 if ( const ConditionalOperator
* condop
= dyn_cast
< ConditionalOperator
>( stmt
))
69 WalkCounter c
= { 0, false };
71 if(c
.cascading
&& c
.stmtcount
>= stmtlimit
)
73 std::string
msg("cascading conditional operator, complexity: ");
74 msg
.append(std::to_string(c
.stmtcount
));
75 report( DiagnosticsEngine::Warning
, msg
, condop
->getLocStart());
81 // Register the plugin action with the LO plugin handling.
82 static Plugin::Registration
< CascadingCondOp
> X( "cascadingcondop" );
84 } // namespace loplugin
86 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */