1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
20 public loplugin::FilteringPlugin
<DodgySwitch
>
23 explicit DodgySwitch(loplugin::InstantiationData
const & data
): FilteringPlugin(data
)
26 virtual void run() override
28 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
31 bool VisitDefaultStmt(DefaultStmt
const * );
32 bool VisitCaseStmt(CaseStmt
const * );
34 bool IsParentSwitch(Stmt
const * );
37 bool DodgySwitch::VisitDefaultStmt(DefaultStmt
const * defaultStmt
)
39 if (ignoreLocation(defaultStmt
))
41 if (!IsParentSwitch(defaultStmt
))
43 DiagnosticsEngine::Warning
, "default statement not directly under switch",
44 compat::getBeginLoc(defaultStmt
))
45 << defaultStmt
->getSourceRange();
49 bool DodgySwitch::VisitCaseStmt(CaseStmt
const * caseStmt
)
51 if (ignoreLocation(caseStmt
))
53 if (!IsParentSwitch(caseStmt
))
55 //parentStmt(parentStmt(caseStmt))->dump();
57 DiagnosticsEngine::Warning
, "case statement not directly under switch",
58 compat::getBeginLoc(caseStmt
))
59 << caseStmt
->getSourceRange();
64 bool DodgySwitch::IsParentSwitch(Stmt
const * stmt
)
66 auto parent
= getParentStmt(stmt
);
67 if (isa
<CaseStmt
>(parent
) || isa
<DefaultStmt
>(parent
)) // daisy chain
69 auto compoundStmt
= dyn_cast
<CompoundStmt
>(parent
);
72 return isa
<SwitchStmt
>(getParentStmt(compoundStmt
));
75 loplugin::Plugin::Registration
< DodgySwitch
> X("dodgyswitch", false);
78 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */