summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/store
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2017-08-16 11:19:02 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2017-08-16 13:17:07 +0200
commit9fb742489b2101c5e7cd8015d053695ce3066291 (patch)
treeba4a889f07110ca17ffb3e52e85b18b9d1e086ba /compilerplugins/clang/store
parentca83aad3703a2c3b28eb7cca43f3b6d2e9351aa4 (diff)
move deadclass plugin to store
<sberg> noelgrandin, and is loplugin:deadclass even sound? struct B { B(B const &) {} }; struct D: B { D(): B(*this) {} }; Change-Id: Idadd379b925aa6f9de6c625bffa8560ec4192ac7
Diffstat (limited to 'compilerplugins/clang/store')
-rw-r--r--compilerplugins/clang/store/deadclass.cxx69
-rw-r--r--compilerplugins/clang/store/test/deadclass.cxx14
2 files changed, 83 insertions, 0 deletions
diff --git a/compilerplugins/clang/store/deadclass.cxx b/compilerplugins/clang/store/deadclass.cxx
new file mode 100644
index 000000000000..1aaac2a8d09b
--- /dev/null
+++ b/compilerplugins/clang/store/deadclass.cxx
@@ -0,0 +1,69 @@
+/* -*- 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 "plugin.hxx"
+
+namespace {
+
+class DeadClass:
+ public RecursiveASTVisitor<DeadClass>, public loplugin::Plugin
+{
+public:
+ explicit DeadClass(InstantiationData const & data): Plugin(data) {}
+
+ void run() override;
+
+ bool VisitCXXRecordDecl(CXXRecordDecl const *);
+};
+
+void DeadClass::run() {
+ if (compiler.getLangOpts().CPlusPlus) {
+ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
+ }
+}
+
+bool DeadClass::VisitCXXRecordDecl(CXXRecordDecl const * decl) {
+ if (ignoreLocation(decl) || !decl->isThisDeclarationADefinition())
+ return true;
+ if (decl->needsImplicitDefaultConstructor())
+ return true;
+ if (decl->getDescribedClassTemplate())
+ return true;
+ if (isa<ClassTemplateSpecializationDecl>(decl))
+ return true;
+ int otherCnt = 0;
+ int copyMoveCnt = 0;
+ for (auto i = decl->ctor_begin(); i != decl->ctor_end(); ++i) {
+ if (!i->isUserProvided())
+ continue;
+ if (i->isCopyOrMoveConstructor())
+ copyMoveCnt++;
+ else
+ otherCnt++;
+ }
+ if (otherCnt == 0 && copyMoveCnt > 0)
+ {
+ report(
+ DiagnosticsEngine::Warning,
+ "class has only copy/move constructors, must be dead",
+ decl->getLocStart())
+ << decl->getSourceRange();
+ for (auto i = decl->ctor_begin(); i != decl->ctor_end(); ++i) {
+ if (i->isDeleted())
+ continue;
+ }
+ }
+ return true;
+}
+
+loplugin::Plugin::Registration<DeadClass> X("deadclass");
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/compilerplugins/clang/store/test/deadclass.cxx b/compilerplugins/clang/store/test/deadclass.cxx
new file mode 100644
index 000000000000..d8b9cdb60889
--- /dev/null
+++ b/compilerplugins/clang/store/test/deadclass.cxx
@@ -0,0 +1,14 @@
+/* -*- 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/.
+ */
+
+struct Foo { // expected-error {{class has only copy/move constructors, must be dead [loplugin:deadclass]}}
+ Foo(Foo&);
+};
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */