summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2017-10-24 12:22:20 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2017-10-25 08:32:16 +0200
commit80a30219c4c553ff22979b73dd97a8869d87e488 (patch)
treebd02d3d74650427602ec50b0ec654a7413ad8b6f
parent3fa0fc687d578afebedf13b7e83db9d1cf1ed4e0 (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>
-rw-r--r--basctl/source/basicide/scriptdocument.cxx2
-rw-r--r--compilerplugins/clang/dodgyswitch.cxx77
-rw-r--r--compilerplugins/clang/test/dodgyswitch.cxx30
-rw-r--r--solenv/CompilerTest_compilerplugins_clang.mk1
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 \