bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / store / cascadingassignop.cxx
bloba1098ee0642c482ad4393f8b42ff0f960a1f53db
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * Based on LLVM/Clang.
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
28 // be smelly
29 static const int stmtlimit = 20;
31 namespace loplugin
34 struct WalkCounter
36 int stmtcount;
37 bool cascading;
38 bool conditionals;
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)
59 ++c.stmtcount;
60 const BinaryOperator* binop = dyn_cast< BinaryOperator >( *it );
61 if ( binop )
63 if ( (binop->isAssignmentOp() || binop->isCompoundAssignmentOp()))
64 c.cascading = true;
65 if ( dyn_cast< AbstractConditionalOperator >( binop ) || binop->isLogicalOp())
66 c.conditionals = true;
68 Walk(*it, c);
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 };
78 Walk(binop, c);
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());
86 return true;
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: */