bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / dodgyswitch.cxx
blobd0674b3632b062a98687bd16b8f051c955a18f2c
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 * 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/.
8 */
10 #include <cassert>
11 #include <string>
12 #include <iostream>
13 #include <fstream>
14 #include <set>
15 #include "plugin.hxx"
17 namespace {
19 class DodgySwitch:
20 public loplugin::FilteringPlugin<DodgySwitch>
22 public:
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 * );
33 private:
34 bool IsParentSwitch(Stmt const * );
37 bool DodgySwitch::VisitDefaultStmt(DefaultStmt const * defaultStmt)
39 if (ignoreLocation(defaultStmt))
40 return true;
41 if (!IsParentSwitch(defaultStmt))
42 report(
43 DiagnosticsEngine::Warning, "default statement not directly under switch",
44 compat::getBeginLoc(defaultStmt))
45 << defaultStmt->getSourceRange();
46 return true;
49 bool DodgySwitch::VisitCaseStmt(CaseStmt const * caseStmt)
51 if (ignoreLocation(caseStmt))
52 return true;
53 if (!IsParentSwitch(caseStmt))
55 //parentStmt(parentStmt(caseStmt))->dump();
56 report(
57 DiagnosticsEngine::Warning, "case statement not directly under switch",
58 compat::getBeginLoc(caseStmt))
59 << caseStmt->getSourceRange();
61 return true;
64 bool DodgySwitch::IsParentSwitch(Stmt const * stmt)
66 auto parent = getParentStmt(stmt);
67 if (isa<CaseStmt>(parent) || isa<DefaultStmt>(parent)) // daisy chain
68 return true;
69 auto compoundStmt = dyn_cast<CompoundStmt>(parent);
70 if (!compoundStmt)
71 return false;
72 return isa<SwitchStmt>(getParentStmt(compoundStmt));
75 loplugin::Plugin::Registration< DodgySwitch > X("dodgyswitch", false);
78 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */