diff options
-rw-r--r-- | basctl/source/basicide/scriptdocument.cxx | 2 | ||||
-rw-r--r-- | compilerplugins/clang/dodgyswitch.cxx | 77 | ||||
-rw-r--r-- | compilerplugins/clang/test/dodgyswitch.cxx | 30 | ||||
-rw-r--r-- | solenv/CompilerTest_compilerplugins_clang.mk | 1 |
4 files changed, 109 insertions, 1 deletions
diff --git a/basctl/source/basicide/scriptdocument.cxx b/basctl/source/basicide/scriptdocument.cxx index 08522afcc985..733cebdbb679 100644 --- a/basctl/source/basicide/scriptdocument.cxx +++ b/basctl/source/basicide/scriptdocument.cxx @@ -1475,6 +1475,7 @@ namespace basctl case LibraryType::All: aTitle = IDEResId(RID_STR_USERMACROSDIALOGS); break; default: break; + } } break; case LIBRARY_LOCATION_SHARE: @@ -1494,7 +1495,6 @@ namespace basctl break; default: break; - } } return aTitle; 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: */ diff --git a/solenv/CompilerTest_compilerplugins_clang.mk b/solenv/CompilerTest_compilerplugins_clang.mk index 97a870d9c296..4d9772fc4665 100644 --- a/solenv/CompilerTest_compilerplugins_clang.mk +++ b/solenv/CompilerTest_compilerplugins_clang.mk @@ -18,6 +18,7 @@ $(eval $(call gb_CompilerTest_add_exception_objects,compilerplugins_clang, \ $(if $(filter-out INTEL,$(CPU)),compilerplugins/clang/test/convertuintptr) \ compilerplugins/clang/test/cppunitassertequals \ compilerplugins/clang/test/datamembershadow \ + compilerplugins/clang/test/dodgyswitch \ compilerplugins/clang/test/droplong \ compilerplugins/clang/test/externvar \ compilerplugins/clang/test/expressionalwayszero \ |