1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/* -*- 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 RedundantInline:
public RecursiveASTVisitor<RedundantInline>, public loplugin::RewritePlugin
{
public:
explicit RedundantInline(loplugin::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(compat::getBeginLoc(decl));
if (l1.isValid() && l1 == unwindToQObject(compat::getEndLoc(decl))) {
return true;
}
SourceLocation inlineLoc;
unsigned n;
auto end = Lexer::getLocForEndOfToken(
compiler.getSourceManager().getExpansionLoc(compat::getEndLoc(decl)), 0,
compiler.getSourceManager(), compiler.getLangOpts());
assert(end.isValid());
for (auto loc = compiler.getSourceManager().getExpansionLoc(
compat::getBeginLoc(decl));
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 : compat::getBeginLoc(decl))
<< 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<RedundantInline> reg("redundantinline", true);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
|