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;
39 // Ctor, nothing special, pass the argument(s).
40 CascadingCondOp::CascadingCondOp(const InstantiationData
& data
)
41 : FilteringPlugin(data
)
45 // Perform the actual action.
46 void CascadingCondOp::run()
48 // Traverse the whole AST of the translation unit (i.e. examine the whole source file).
49 // The Clang AST helper class will call VisitReturnStmt for every return statement.
50 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
53 void CascadingCondOp::Walk(const Stmt
* stmt
, WalkCounter
& c
)
55 for (Stmt::const_child_iterator it
= stmt
->child_begin(); it
!= stmt
->child_end(); ++it
)
58 if (dyn_cast
<ConditionalOperator
>(*it
))
64 bool CascadingCondOp::VisitStmt(const Stmt
* stmt
)
66 if (const ConditionalOperator
* condop
= dyn_cast
<ConditionalOperator
>(stmt
))
68 WalkCounter c
= { 0, false };
70 if (c
.cascading
&& c
.stmtcount
>= stmtlimit
)
72 std::string
msg("cascading conditional operator, complexity: ");
73 msg
.append(std::to_string(c
.stmtcount
));
74 report(DiagnosticsEngine::Warning
, msg
, condop
->getLocStart());
80 // Register the plugin action with the LO plugin handling.
81 static Plugin::Registration
<CascadingCondOp
> X("cascadingcondop");
83 } // namespace loplugin
85 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */