bump product version to 5.0.4.1
[LibreOffice.git] / compilerplugins / clang / store / cascadingcondop.cxx
blob722b9819b60b75779c01bf0ede15efb74580fa4c
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 "cascadingcondop.hxx"
15 This is a compile check.
17 It checks for complex statements with conditional operators in conditional
18 operators, which are errorprone, 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 havent 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 = 50;
31 namespace loplugin
34 struct WalkCounter
36 int stmtcount;
37 bool cascading;
40 // Ctor, nothing special, pass the argument(s).
41 CascadingCondOp::CascadingCondOp( const InstantiationData& data )
42 : Plugin( 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)
58 ++c.stmtcount;
59 if ( dyn_cast< ConditionalOperator >( *it ))
60 c.cascading = true;
61 Walk(*it, c);
65 bool CascadingCondOp::VisitStmt( const Stmt* stmt )
67 if ( const ConditionalOperator* condop = dyn_cast< ConditionalOperator >( stmt ))
69 WalkCounter c = { 0, false };
70 Walk(condop, c);
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());
78 return true;
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: */