InconsistentDeclarationParameterNameCheck.cpp
13.1 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//===--- InconsistentDeclarationParameterNameCheck.cpp - clang-tidy-------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "InconsistentDeclarationParameterNameCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include <algorithm>
#include <functional>
#include <sstream>
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace readability {
namespace {
AST_MATCHER(FunctionDecl, hasOtherDeclarations) {
auto It = Node.redecls_begin();
auto EndIt = Node.redecls_end();
if (It == EndIt)
return false;
++It;
return It != EndIt;
}
struct DifferingParamInfo {
DifferingParamInfo(StringRef SourceName, StringRef OtherName,
SourceRange OtherNameRange, bool GenerateFixItHint)
: SourceName(SourceName), OtherName(OtherName),
OtherNameRange(OtherNameRange), GenerateFixItHint(GenerateFixItHint) {}
StringRef SourceName;
StringRef OtherName;
SourceRange OtherNameRange;
bool GenerateFixItHint;
};
using DifferingParamsContainer = llvm::SmallVector<DifferingParamInfo, 10>;
struct InconsistentDeclarationInfo {
InconsistentDeclarationInfo(SourceLocation DeclarationLocation,
DifferingParamsContainer &&DifferingParams)
: DeclarationLocation(DeclarationLocation),
DifferingParams(std::move(DifferingParams)) {}
SourceLocation DeclarationLocation;
DifferingParamsContainer DifferingParams;
};
using InconsistentDeclarationsContainer =
llvm::SmallVector<InconsistentDeclarationInfo, 2>;
bool checkIfFixItHintIsApplicable(
const FunctionDecl *ParameterSourceDeclaration,
const ParmVarDecl *SourceParam, const FunctionDecl *OriginalDeclaration) {
// Assumptions with regard to function declarations/definition:
// * If both function declaration and definition are seen, assume that
// definition is most up-to-date, and use it to generate replacements.
// * If only function declarations are seen, there is no easy way to tell
// which is up-to-date and which is not, so don't do anything.
// TODO: This may be changed later, but for now it seems the reasonable
// solution.
if (!ParameterSourceDeclaration->isThisDeclarationADefinition())
return false;
// Assumption: if parameter is not referenced in function definition body, it
// may indicate that it's outdated, so don't touch it.
if (!SourceParam->isReferenced())
return false;
// In case there is the primary template definition and (possibly several)
// template specializations (and each with possibly several redeclarations),
// it is not at all clear what to change.
if (OriginalDeclaration->getTemplatedKind() ==
FunctionDecl::TK_FunctionTemplateSpecialization)
return false;
// Other cases seem OK to allow replacements.
return true;
}
bool nameMatch(StringRef L, StringRef R, bool Strict) {
if (Strict)
return L.empty() || R.empty() || L == R;
// We allow two names if one is a prefix/suffix of the other, ignoring case.
// Important special case: this is true if either parameter has no name!
return L.startswith_lower(R) || R.startswith_lower(L) ||
L.endswith_lower(R) || R.endswith_lower(L);
}
DifferingParamsContainer
findDifferingParamsInDeclaration(const FunctionDecl *ParameterSourceDeclaration,
const FunctionDecl *OtherDeclaration,
const FunctionDecl *OriginalDeclaration,
bool Strict) {
DifferingParamsContainer DifferingParams;
auto SourceParamIt = ParameterSourceDeclaration->param_begin();
auto OtherParamIt = OtherDeclaration->param_begin();
while (SourceParamIt != ParameterSourceDeclaration->param_end() &&
OtherParamIt != OtherDeclaration->param_end()) {
auto SourceParamName = (*SourceParamIt)->getName();
auto OtherParamName = (*OtherParamIt)->getName();
// FIXME: Provide a way to extract commented out parameter name from comment
// next to it.
if (!nameMatch(SourceParamName, OtherParamName, Strict)) {
SourceRange OtherParamNameRange =
DeclarationNameInfo((*OtherParamIt)->getDeclName(),
(*OtherParamIt)->getLocation())
.getSourceRange();
bool GenerateFixItHint = checkIfFixItHintIsApplicable(
ParameterSourceDeclaration, *SourceParamIt, OriginalDeclaration);
DifferingParams.emplace_back(SourceParamName, OtherParamName,
OtherParamNameRange, GenerateFixItHint);
}
++SourceParamIt;
++OtherParamIt;
}
return DifferingParams;
}
InconsistentDeclarationsContainer
findInconsistentDeclarations(const FunctionDecl *OriginalDeclaration,
const FunctionDecl *ParameterSourceDeclaration,
SourceManager &SM, bool Strict) {
InconsistentDeclarationsContainer InconsistentDeclarations;
SourceLocation ParameterSourceLocation =
ParameterSourceDeclaration->getLocation();
for (const FunctionDecl *OtherDeclaration : OriginalDeclaration->redecls()) {
SourceLocation OtherLocation = OtherDeclaration->getLocation();
if (OtherLocation != ParameterSourceLocation) { // Skip self.
DifferingParamsContainer DifferingParams =
findDifferingParamsInDeclaration(ParameterSourceDeclaration,
OtherDeclaration,
OriginalDeclaration, Strict);
if (!DifferingParams.empty()) {
InconsistentDeclarations.emplace_back(OtherDeclaration->getLocation(),
std::move(DifferingParams));
}
}
}
// Sort in order of appearance in translation unit to generate clear
// diagnostics.
std::sort(InconsistentDeclarations.begin(), InconsistentDeclarations.end(),
[&SM](const InconsistentDeclarationInfo &Info1,
const InconsistentDeclarationInfo &Info2) {
return SM.isBeforeInTranslationUnit(Info1.DeclarationLocation,
Info2.DeclarationLocation);
});
return InconsistentDeclarations;
}
const FunctionDecl *
getParameterSourceDeclaration(const FunctionDecl *OriginalDeclaration) {
const FunctionTemplateDecl *PrimaryTemplate =
OriginalDeclaration->getPrimaryTemplate();
if (PrimaryTemplate != nullptr) {
// In case of template specializations, use primary template declaration as
// the source of parameter names.
return PrimaryTemplate->getTemplatedDecl();
}
// In other cases, try to change to function definition, if available.
if (OriginalDeclaration->isThisDeclarationADefinition())
return OriginalDeclaration;
for (const FunctionDecl *OtherDeclaration : OriginalDeclaration->redecls()) {
if (OtherDeclaration->isThisDeclarationADefinition()) {
return OtherDeclaration;
}
}
// No definition found, so return original declaration.
return OriginalDeclaration;
}
std::string joinParameterNames(
const DifferingParamsContainer &DifferingParams,
llvm::function_ref<StringRef(const DifferingParamInfo &)> ChooseParamName) {
llvm::SmallVector<char, 40> Buffer;
llvm::raw_svector_ostream Str(Buffer);
bool First = true;
for (const DifferingParamInfo &ParamInfo : DifferingParams) {
if (First)
First = false;
else
Str << ", ";
Str << "'" << ChooseParamName(ParamInfo).str() << "'";
}
return Str.str().str();
}
void formatDifferingParamsDiagnostic(
InconsistentDeclarationParameterNameCheck *Check, SourceLocation Location,
StringRef OtherDeclarationDescription,
const DifferingParamsContainer &DifferingParams) {
auto ChooseOtherName = [](const DifferingParamInfo &ParamInfo) {
return ParamInfo.OtherName;
};
auto ChooseSourceName = [](const DifferingParamInfo &ParamInfo) {
return ParamInfo.SourceName;
};
auto ParamDiag =
Check->diag(Location,
"differing parameters are named here: (%0), in %1: (%2)",
DiagnosticIDs::Level::Note)
<< joinParameterNames(DifferingParams, ChooseOtherName)
<< OtherDeclarationDescription
<< joinParameterNames(DifferingParams, ChooseSourceName);
for (const DifferingParamInfo &ParamInfo : DifferingParams) {
if (ParamInfo.GenerateFixItHint) {
ParamDiag << FixItHint::CreateReplacement(
CharSourceRange::getTokenRange(ParamInfo.OtherNameRange),
ParamInfo.SourceName);
}
}
}
void formatDiagnosticsForDeclarations(
InconsistentDeclarationParameterNameCheck *Check,
const FunctionDecl *ParameterSourceDeclaration,
const FunctionDecl *OriginalDeclaration,
const InconsistentDeclarationsContainer &InconsistentDeclarations) {
Check->diag(
OriginalDeclaration->getLocation(),
"function %q0 has %1 other declaration%s1 with different parameter names")
<< OriginalDeclaration
<< static_cast<int>(InconsistentDeclarations.size());
int Count = 1;
for (const InconsistentDeclarationInfo &InconsistentDeclaration :
InconsistentDeclarations) {
Check->diag(InconsistentDeclaration.DeclarationLocation,
"the %ordinal0 inconsistent declaration seen here",
DiagnosticIDs::Level::Note)
<< Count;
formatDifferingParamsDiagnostic(
Check, InconsistentDeclaration.DeclarationLocation,
"the other declaration", InconsistentDeclaration.DifferingParams);
++Count;
}
}
void formatDiagnostics(
InconsistentDeclarationParameterNameCheck *Check,
const FunctionDecl *ParameterSourceDeclaration,
const FunctionDecl *OriginalDeclaration,
const InconsistentDeclarationsContainer &InconsistentDeclarations,
StringRef FunctionDescription, StringRef ParameterSourceDescription) {
for (const InconsistentDeclarationInfo &InconsistentDeclaration :
InconsistentDeclarations) {
Check->diag(InconsistentDeclaration.DeclarationLocation,
"%0 %q1 has a %2 with different parameter names")
<< FunctionDescription << OriginalDeclaration
<< ParameterSourceDescription;
Check->diag(ParameterSourceDeclaration->getLocation(), "the %0 seen here",
DiagnosticIDs::Level::Note)
<< ParameterSourceDescription;
formatDifferingParamsDiagnostic(
Check, InconsistentDeclaration.DeclarationLocation,
ParameterSourceDescription, InconsistentDeclaration.DifferingParams);
}
}
} // anonymous namespace
void InconsistentDeclarationParameterNameCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "IgnoreMacros", IgnoreMacros);
Options.store(Opts, "Strict", Strict);
}
void InconsistentDeclarationParameterNameCheck::registerMatchers(
MatchFinder *Finder) {
Finder->addMatcher(functionDecl(unless(isImplicit()), hasOtherDeclarations())
.bind("functionDecl"),
this);
}
void InconsistentDeclarationParameterNameCheck::check(
const MatchFinder::MatchResult &Result) {
const auto *OriginalDeclaration =
Result.Nodes.getNodeAs<FunctionDecl>("functionDecl");
if (VisitedDeclarations.count(OriginalDeclaration) > 0)
return; // Avoid multiple warnings.
const FunctionDecl *ParameterSourceDeclaration =
getParameterSourceDeclaration(OriginalDeclaration);
InconsistentDeclarationsContainer InconsistentDeclarations =
findInconsistentDeclarations(OriginalDeclaration,
ParameterSourceDeclaration,
*Result.SourceManager, Strict);
if (InconsistentDeclarations.empty()) {
// Avoid unnecessary further visits.
markRedeclarationsAsVisited(OriginalDeclaration);
return;
}
SourceLocation StartLoc = OriginalDeclaration->getBeginLoc();
if (StartLoc.isMacroID() && IgnoreMacros) {
markRedeclarationsAsVisited(OriginalDeclaration);
return;
}
if (OriginalDeclaration->getTemplatedKind() ==
FunctionDecl::TK_FunctionTemplateSpecialization) {
formatDiagnostics(this, ParameterSourceDeclaration, OriginalDeclaration,
InconsistentDeclarations,
"function template specialization",
"primary template declaration");
} else if (ParameterSourceDeclaration->isThisDeclarationADefinition()) {
formatDiagnostics(this, ParameterSourceDeclaration, OriginalDeclaration,
InconsistentDeclarations, "function", "definition");
} else {
formatDiagnosticsForDeclarations(this, ParameterSourceDeclaration,
OriginalDeclaration,
InconsistentDeclarations);
}
markRedeclarationsAsVisited(OriginalDeclaration);
}
void InconsistentDeclarationParameterNameCheck::markRedeclarationsAsVisited(
const FunctionDecl *OriginalDeclaration) {
for (const FunctionDecl *Redecl : OriginalDeclaration->redecls()) {
VisitedDeclarations.insert(Redecl);
}
}
} // namespace readability
} // namespace tidy
} // namespace clang