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 "cascadingassignop.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
= 20;
41 // Ctor, nothing special, pass the argument(s).
42 CascadingAssignOp::CascadingAssignOp( const InstantiationData
& data
)
43 : FilteringPlugin( data
)
47 // Perform the actual action.
48 void CascadingAssignOp::run()
50 // Traverse the whole AST of the translation unit (i.e. examine the whole source file).
51 // The Clang AST helper class will call VisitReturnStmt for every return statement.
52 TraverseDecl( compiler
.getASTContext().getTranslationUnitDecl());
55 void CascadingAssignOp::Walk( const Stmt
* stmt
, WalkCounter
& c
)
57 for(Stmt::const_child_iterator it
= stmt
->child_begin(); it
!= stmt
->child_end(); ++it
)
60 const BinaryOperator
* binop
= dyn_cast
< BinaryOperator
>( *it
);
63 if ( (binop
->isAssignmentOp() || binop
->isCompoundAssignmentOp()))
65 if ( dyn_cast
< AbstractConditionalOperator
>( binop
) || binop
->isLogicalOp())
66 c
.conditionals
= true;
72 bool CascadingAssignOp::VisitStmt( const Stmt
* stmt
)
74 const BinaryOperator
* binop
= dyn_cast
< BinaryOperator
>( stmt
);
75 if ( binop
&& (binop
->isAssignmentOp() || binop
->isCompoundAssignmentOp()))
77 WalkCounter c
= { 0, false, false };
79 if(c
.cascading
&& c
.conditionals
&& c
.stmtcount
>= stmtlimit
)
81 std::string
msg("cascading assign operator mixing in conditionals, complexity: ");
82 msg
.append(std::to_string(c
.stmtcount
));
83 report( DiagnosticsEngine::Warning
, msg
, binop
->getLocStart());
89 // Register the plugin action with the LO plugin handling.
90 static Plugin::Registration
< CascadingAssignOp
> X( "cascadingassignop" );
92 } // namespace loplugin
94 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */