summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2017-03-25 10:47:34 +0100
committerStephan Bergmann <sbergman@redhat.com>2017-03-25 16:21:04 +0000
commit5d8e6901ec14edd9da10d9dee63b2ef2ab058692 (patch)
tree4f4ba52e1a1e14a03ffa1164366121fe82b87f8c /compilerplugins
parent7668d4e616dd37d3b335c43db897b64a7349047b (diff)
Add loplugin:redundantinline
...after it had recently been run with 6cb9e6dad798ec59f055aebe84a9c4a21e4be40d "Remove redundant 'inline' keyword" Change-Id: I7f3ee2ff1c32988dcff7245c64b50fe20b0a5e79 Reviewed-on: https://gerrit.libreoffice.org/35681 Reviewed-by: Stephan Bergmann <sbergman@redhat.com> Tested-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/redundantinline.cxx127
-rw-r--r--compilerplugins/clang/test/redundantinline.cxx14
-rw-r--r--compilerplugins/clang/test/redundantinline.hxx79
3 files changed, 220 insertions, 0 deletions
diff --git a/compilerplugins/clang/redundantinline.cxx b/compilerplugins/clang/redundantinline.cxx
new file mode 100644
index 000000000000..88c870d75465
--- /dev/null
+++ b/compilerplugins/clang/redundantinline.cxx
@@ -0,0 +1,127 @@
+/* -*- 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 <cassert>
+
+#include "plugin.hxx"
+
+namespace {
+
+class Visitor:
+ public RecursiveASTVisitor<Visitor>, public loplugin::RewritePlugin
+{
+public:
+ explicit Visitor(InstantiationData const & data): RewritePlugin(data) {}
+
+ void run() override {
+ if (compiler.getLangOpts().CPlusPlus) {
+ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
+ }
+ }
+
+ bool VisitFunctionDecl(FunctionDecl const * decl) {
+ if (ignoreLocation(decl) || !decl->isInlineSpecified()
+ || !(decl->doesThisDeclarationHaveABody()
+ || decl->isExplicitlyDefaulted())
+ || !(decl->getLexicalDeclContext()->isRecord()
+ || decl->isConstexpr()))
+ {
+ return true;
+ }
+ auto l1 = unwindToQObject(decl->getLocStart());
+ if (l1.isValid() && l1 == unwindToQObject(decl->getLocEnd())) {
+ return true;
+ }
+ SourceLocation inlineLoc;
+ unsigned n;
+ auto end = Lexer::getLocForEndOfToken(
+ compiler.getSourceManager().getExpansionLoc(decl->getLocEnd()), 0,
+ compiler.getSourceManager(), compiler.getLangOpts());
+ assert(end.isValid());
+ for (auto loc = compiler.getSourceManager().getExpansionLoc(
+ decl->getLocStart());
+ loc != end; loc = loc.getLocWithOffset(std::max<unsigned>(n, 1)))
+ {
+ n = Lexer::MeasureTokenLength(
+ loc, compiler.getSourceManager(), compiler.getLangOpts());
+ StringRef s(compiler.getSourceManager().getCharacterData(loc), n);
+ //TODO: see compilerplugins/clang/override.cxx:
+ if (s.startswith("\\\n")) {
+ s = s.drop_front(2);
+ }
+ if (s == "inline") {
+ if (!compiler.getSourceManager().isMacroArgExpansion(loc)) {
+ inlineLoc = loc;
+ }
+ break;
+ } else if (s == "#") {
+ // Hard to pick the right 'inline' in code like
+ //
+ // #if 1
+ // static
+ // #else
+ // inline
+ // #endif
+ // inline void f() {}
+ //
+ // so just give up once a preprocessing directive is seen:
+ break;
+ }
+ }
+ if (rewriter != nullptr && inlineLoc.isValid()) {
+ for (auto loc = inlineLoc.getLocWithOffset(
+ std::max<unsigned>(n, 1));;)
+ {
+ assert(loc != end);
+ unsigned n2 = Lexer::MeasureTokenLength(
+ loc, compiler.getSourceManager(), compiler.getLangOpts());
+ StringRef s(
+ compiler.getSourceManager().getCharacterData(loc), n2);
+ //TODO: see compilerplugins/clang/override.cxx:
+ if (s.startswith("\\\n")) {
+ s = s.drop_front(2);
+ }
+ if (!s.empty()) {
+ break;
+ }
+ n2 = std::max<unsigned>(n2, 1);
+ n += n2;
+ loc = loc.getLocWithOffset(n2);
+ }
+ if (removeText(inlineLoc, n, RewriteOptions(RemoveLineIfEmpty))) {
+ return true;
+ }
+ }
+ report(
+ DiagnosticsEngine::Warning,
+ "function definition redundantly declared 'inline'",
+ inlineLoc.isValid() ? inlineLoc : decl->getLocStart())
+ << decl->getSourceRange();
+ return true;
+ }
+
+private:
+ SourceLocation unwindToQObject(SourceLocation const & loc) {
+ if (!loc.isMacroID()) {
+ return SourceLocation();
+ }
+ auto l = compiler.getSourceManager().getImmediateMacroCallerLoc(loc);
+ return
+ (Lexer::getImmediateMacroName(
+ loc, compiler.getSourceManager(), compiler.getLangOpts())
+ == "Q_OBJECT")
+ ? l : unwindToQObject(l);
+ }
+};
+
+loplugin::Plugin::Registration<Visitor> reg("redundantinline", true);
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/compilerplugins/clang/test/redundantinline.cxx b/compilerplugins/clang/test/redundantinline.cxx
new file mode 100644
index 000000000000..038a1497d98e
--- /dev/null
+++ b/compilerplugins/clang/test/redundantinline.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/.
+ */
+
+#include "redundantinline.hxx"
+
+S1::~S1() = default;
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/compilerplugins/clang/test/redundantinline.hxx b/compilerplugins/clang/test/redundantinline.hxx
new file mode 100644
index 000000000000..a0c91ceb67be
--- /dev/null
+++ b/compilerplugins/clang/test/redundantinline.hxx
@@ -0,0 +1,79 @@
+/* -*- 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/.
+ */
+
+#ifndef INCLUDED_COMPILERPLUGINS_CLANG_TEST_REDUNDANTINLINE_HXX
+#define INCLUDED_COMPILERPLUGINS_CLANG_TEST_REDUNDANTINLINE_HXX
+
+struct S1 {
+ inline S1();
+ inline ~S1();
+};
+
+S1::S1() = default;
+
+struct S2 {
+ inline S2() = default; // expected-error {{[loplugin:redundantinline]}}
+ inline ~S2() = default; // expected-error {{[loplugin:redundantinline]}} expected-error {{[loplugin:unnecessaryoverride]}}
+};
+
+struct S3 {
+ inline S3();
+ inline ~S3();
+
+ inline void f1();
+
+ static inline void f2();
+
+ inline void operator +();
+
+ inline operator int();
+
+ friend inline void f3();
+};
+
+S3::S3() {}
+
+S3::~S3() { f1(); }
+
+void S3::f1() { (void)this; }
+
+void S3::f2() {}
+
+void S3::operator +() {}
+
+void f3() {}
+
+S3::operator int() { return 0; }
+
+struct S4 {
+ inline S4() {} // expected-error {{[loplugin:redundantinline]}}
+ inline ~S4() { f1(); } // expected-error {{[loplugin:redundantinline]}}
+
+ inline void f1() { (void)this; } // expected-error {{[loplugin:redundantinline]}}
+
+ static inline void f2() {} // expected-error {{[loplugin:redundantinline]}}
+
+ inline void operator +() {} // expected-error {{[loplugin:redundantinline]}}
+
+ inline operator int() { return 0; } // expected-error {{[loplugin:redundantinline]}}
+
+ friend inline void f4() {} // expected-error {{[loplugin:redundantinline]}}
+
+ static constexpr int f5() { return 0; }
+
+ static constexpr inline int f6() { return 0; } // expected-error {{[loplugin:redundantinline]}}
+};
+
+constexpr int f5() { return 0; }
+
+constexpr inline int f6() { return 0; } // expected-error {{[loplugin:redundantinline]}}
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */