tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / compilerplugins / clang / store / cascadingcondop.cxx
blob4671f41b436a65f9e3279560debac0249bac89d3
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 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 = 50;
31 namespace loplugin
33 struct WalkCounter
35 int stmtcount;
36 bool cascading;
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)
57 ++c.stmtcount;
58 if (dyn_cast<ConditionalOperator>(*it))
59 c.cascading = true;
60 Walk(*it, c);
64 bool CascadingCondOp::VisitStmt(const Stmt* stmt)
66 if (const ConditionalOperator* condop = dyn_cast<ConditionalOperator>(stmt))
68 WalkCounter c = { 0, false };
69 Walk(condop, c);
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());
77 return true;
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: */