diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2017-10-24 12:22:20 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2017-10-25 08:32:16 +0200 |
commit | 80a30219c4c553ff22979b73dd97a8869d87e488 (patch) | |
tree | bd02d3d74650427602ec50b0ec654a7413ad8b6f /compilerplugins | |
parent | 3fa0fc687d578afebedf13b7e83db9d1cf1ed4e0 (diff) |
new loplugin:dodgyswitch
and fix bug in ScriptDocument::getTitle
which has been there since
commit e304ba66f4aba5cc55612508b5738a1ed26a7904
Date: Thu Mar 15 14:59:30 2007 +0000
INTEGRATION: CWS basmgr02 (1.1.2); FILE ADDED
plugin is off by default since it uses expensive parentStmt() calls
Change-Id: Id0f16baec48e0381e0083594d7e59b58b023da2f
Reviewed-on: https://gerrit.libreoffice.org/43750
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/dodgyswitch.cxx | 77 | ||||
-rw-r--r-- | compilerplugins/clang/test/dodgyswitch.cxx | 30 |
2 files changed, 107 insertions, 0 deletions
diff --git a/compilerplugins/clang/dodgyswitch.cxx b/compilerplugins/clang/dodgyswitch.cxx new file mode 100644 index 000000000000..b731e30419f9 --- /dev/null +++ b/compilerplugins/clang/dodgyswitch.cxx @@ -0,0 +1,77 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <cassert> +#include <string> +#include <iostream> +#include <fstream> +#include <set> +#include "plugin.hxx" + +namespace { + +class DodgySwitch: + public RecursiveASTVisitor<DodgySwitch>, public loplugin::Plugin +{ +public: + explicit DodgySwitch(InstantiationData const & data): Plugin(data) {} + + virtual void run() override + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitDefaultStmt(DefaultStmt const * ); + bool VisitCaseStmt(CaseStmt const * ); +private: + bool IsParentSwitch(Stmt const * ); +}; + +bool DodgySwitch::VisitDefaultStmt(DefaultStmt const * defaultStmt) +{ + if (ignoreLocation(defaultStmt)) + return true; + if (!IsParentSwitch(defaultStmt)) + report( + DiagnosticsEngine::Warning, "default statement not directly under switch", + defaultStmt->getLocStart()) + << defaultStmt->getSourceRange(); + return true; +} + +bool DodgySwitch::VisitCaseStmt(CaseStmt const * caseStmt) +{ + if (ignoreLocation(caseStmt)) + return true; + if (!IsParentSwitch(caseStmt)) + { + //parentStmt(parentStmt(caseStmt))->dump(); + report( + DiagnosticsEngine::Warning, "case statement not directly under switch", + caseStmt->getLocStart()) + << caseStmt->getSourceRange(); + } + return true; +} + +bool DodgySwitch::IsParentSwitch(Stmt const * stmt) +{ + auto parent = parentStmt(stmt); + if (isa<CaseStmt>(parent) || isa<DefaultStmt>(parent)) // daisy chain + return true; + auto compoundStmt = dyn_cast<CompoundStmt>(parent); + if (!compoundStmt) + return false; + return isa<SwitchStmt>(parentStmt(compoundStmt)); +} + +loplugin::Plugin::Registration< DodgySwitch > X("dodgyswitch", false); + +} +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/test/dodgyswitch.cxx b/compilerplugins/clang/test/dodgyswitch.cxx new file mode 100644 index 000000000000..3a61ed388c2b --- /dev/null +++ b/compilerplugins/clang/test/dodgyswitch.cxx @@ -0,0 +1,30 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <sal/types.h> + +int foo(); + +int main() { + switch (foo()) + { + case 1: { break; } + case 2: { + SAL_FALLTHROUGH; + { + case 3: // expected-error {{case statement not directly under switch [loplugin:dodgyswitch]}} + break; + } + default: // expected-error {{default statement not directly under switch [loplugin:dodgyswitch]}} + break; + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |