blob: 254ef960e898caf0d2e29a2b06fa7886b9938eb0 [file] [log] [blame]
Alexander Kornienko11d4d642015-09-10 10:07:11 +00001//===--- InconsistentDeclarationParameterNameCheck.cpp - clang-tidy-------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "InconsistentDeclarationParameterNameCheck.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13
14#include <algorithm>
15#include <functional>
16#include <sstream>
17
Etienne Bergeron456177b2016-05-02 18:00:29 +000018using namespace clang::ast_matchers;
19
Alexander Kornienko11d4d642015-09-10 10:07:11 +000020namespace clang {
21namespace tidy {
22namespace readability {
23
Alexander Kornienko11d4d642015-09-10 10:07:11 +000024namespace {
25
26AST_MATCHER(FunctionDecl, hasOtherDeclarations) {
27 auto It = Node.redecls_begin();
28 auto EndIt = Node.redecls_end();
29
30 if (It == EndIt)
31 return false;
32
33 ++It;
34 return It != EndIt;
35}
36
37struct DifferingParamInfo {
38 DifferingParamInfo(StringRef SourceName, StringRef OtherName,
39 SourceRange OtherNameRange, bool GenerateFixItHint)
40 : SourceName(SourceName), OtherName(OtherName),
41 OtherNameRange(OtherNameRange), GenerateFixItHint(GenerateFixItHint) {}
42
43 StringRef SourceName;
44 StringRef OtherName;
45 SourceRange OtherNameRange;
46 bool GenerateFixItHint;
47};
48
49using DifferingParamsContainer = llvm::SmallVector<DifferingParamInfo, 10>;
50
51struct InconsistentDeclarationInfo {
52 InconsistentDeclarationInfo(SourceLocation DeclarationLocation,
53 DifferingParamsContainer &&DifferingParams)
54 : DeclarationLocation(DeclarationLocation),
55 DifferingParams(std::move(DifferingParams)) {}
56
57 SourceLocation DeclarationLocation;
58 DifferingParamsContainer DifferingParams;
59};
60
61using InconsistentDeclarationsContainer =
62 llvm::SmallVector<InconsistentDeclarationInfo, 2>;
63
64bool checkIfFixItHintIsApplicable(
65 const FunctionDecl *ParameterSourceDeclaration,
66 const ParmVarDecl *SourceParam, const FunctionDecl *OriginalDeclaration) {
67 // Assumptions with regard to function declarations/definition:
68 // * If both function declaration and definition are seen, assume that
Piotr Dziwinski0a295572015-10-24 20:11:47 +000069 // definition is most up-to-date, and use it to generate replacements.
Alexander Kornienko11d4d642015-09-10 10:07:11 +000070 // * If only function declarations are seen, there is no easy way to tell
Piotr Dziwinski0a295572015-10-24 20:11:47 +000071 // which is up-to-date and which is not, so don't do anything.
Alexander Kornienko11d4d642015-09-10 10:07:11 +000072 // TODO: This may be changed later, but for now it seems the reasonable
73 // solution.
74 if (!ParameterSourceDeclaration->isThisDeclarationADefinition())
75 return false;
76
77 // Assumption: if parameter is not referenced in function defintion body, it
78 // may indicate that it's outdated, so don't touch it.
79 if (!SourceParam->isReferenced())
80 return false;
81
82 // In case there is the primary template definition and (possibly several)
83 // template specializations (and each with possibly several redeclarations),
84 // it is not at all clear what to change.
85 if (OriginalDeclaration->getTemplatedKind() ==
86 FunctionDecl::TK_FunctionTemplateSpecialization)
87 return false;
88
89 // Other cases seem OK to allow replacements.
90 return true;
91}
92
93DifferingParamsContainer
94findDifferingParamsInDeclaration(const FunctionDecl *ParameterSourceDeclaration,
95 const FunctionDecl *OtherDeclaration,
96 const FunctionDecl *OriginalDeclaration) {
97 DifferingParamsContainer DifferingParams;
98
99 auto SourceParamIt = ParameterSourceDeclaration->param_begin();
100 auto OtherParamIt = OtherDeclaration->param_begin();
101
102 while (SourceParamIt != ParameterSourceDeclaration->param_end() &&
103 OtherParamIt != OtherDeclaration->param_end()) {
104 auto SourceParamName = (*SourceParamIt)->getName();
105 auto OtherParamName = (*OtherParamIt)->getName();
106
107 // FIXME: Provide a way to extract commented out parameter name from comment
108 // next to it.
109 if (!SourceParamName.empty() && !OtherParamName.empty() &&
110 SourceParamName != OtherParamName) {
111 SourceRange OtherParamNameRange =
112 DeclarationNameInfo((*OtherParamIt)->getDeclName(),
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000113 (*OtherParamIt)->getLocation())
114 .getSourceRange();
Alexander Kornienko11d4d642015-09-10 10:07:11 +0000115
116 bool GenerateFixItHint = checkIfFixItHintIsApplicable(
117 ParameterSourceDeclaration, *SourceParamIt, OriginalDeclaration);
118
119 DifferingParams.emplace_back(SourceParamName, OtherParamName,
120 OtherParamNameRange, GenerateFixItHint);
121 }
122
123 ++SourceParamIt;
124 ++OtherParamIt;
125 }
126
127 return DifferingParams;
128}
129
130InconsistentDeclarationsContainer
131findInconsitentDeclarations(const FunctionDecl *OriginalDeclaration,
132 const FunctionDecl *ParameterSourceDeclaration,
133 SourceManager &SM) {
134 InconsistentDeclarationsContainer InconsistentDeclarations;
135 SourceLocation ParameterSourceLocation =
136 ParameterSourceDeclaration->getLocation();
137
138 for (const FunctionDecl *OtherDeclaration : OriginalDeclaration->redecls()) {
139 SourceLocation OtherLocation = OtherDeclaration->getLocation();
140 if (OtherLocation != ParameterSourceLocation) { // Skip self.
141 DifferingParamsContainer DifferingParams =
142 findDifferingParamsInDeclaration(ParameterSourceDeclaration,
143 OtherDeclaration,
144 OriginalDeclaration);
145 if (!DifferingParams.empty()) {
146 InconsistentDeclarations.emplace_back(OtherDeclaration->getLocation(),
147 std::move(DifferingParams));
148 }
149 }
150 }
151
152 // Sort in order of appearance in translation unit to generate clear
153 // diagnostics.
154 std::sort(InconsistentDeclarations.begin(), InconsistentDeclarations.end(),
155 [&SM](const InconsistentDeclarationInfo &Info1,
156 const InconsistentDeclarationInfo &Info2) {
157 return SM.isBeforeInTranslationUnit(Info1.DeclarationLocation,
158 Info2.DeclarationLocation);
159 });
160 return InconsistentDeclarations;
161}
162
163const FunctionDecl *
164getParameterSourceDeclaration(const FunctionDecl *OriginalDeclaration) {
165 const FunctionTemplateDecl *PrimaryTemplate =
166 OriginalDeclaration->getPrimaryTemplate();
167 if (PrimaryTemplate != nullptr) {
168 // In case of template specializations, use primary template declaration as
169 // the source of parameter names.
170 return PrimaryTemplate->getTemplatedDecl();
171 }
172
173 // In other cases, try to change to function definition, if available.
174
175 if (OriginalDeclaration->isThisDeclarationADefinition())
176 return OriginalDeclaration;
177
178 for (const FunctionDecl *OtherDeclaration : OriginalDeclaration->redecls()) {
179 if (OtherDeclaration->isThisDeclarationADefinition()) {
180 return OtherDeclaration;
181 }
182 }
183
184 // No definition found, so return original declaration.
185 return OriginalDeclaration;
186}
187
188std::string joinParameterNames(
189 const DifferingParamsContainer &DifferingParams,
Benjamin Kramer51a9cc92016-06-15 15:46:10 +0000190 llvm::function_ref<StringRef(const DifferingParamInfo &)> ChooseParamName) {
Alexander Kornienko11d4d642015-09-10 10:07:11 +0000191 llvm::SmallVector<char, 40> Buffer;
192 llvm::raw_svector_ostream Str(Buffer);
193 bool First = true;
194 for (const DifferingParamInfo &ParamInfo : DifferingParams) {
195 if (First)
196 First = false;
197 else
198 Str << ", ";
199
200 Str << "'" << ChooseParamName(ParamInfo).str() << "'";
201 }
202 return Str.str().str();
203}
204
205void formatDifferingParamsDiagnostic(
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000206 InconsistentDeclarationParameterNameCheck *Check, SourceLocation Location,
207 StringRef OtherDeclarationDescription,
Alexander Kornienko11d4d642015-09-10 10:07:11 +0000208 const DifferingParamsContainer &DifferingParams) {
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000209 auto ChooseOtherName = [](const DifferingParamInfo &ParamInfo) {
210 return ParamInfo.OtherName;
211 };
212 auto ChooseSourceName = [](const DifferingParamInfo &ParamInfo) {
213 return ParamInfo.SourceName;
214 };
Alexander Kornienko11d4d642015-09-10 10:07:11 +0000215
216 auto ParamDiag =
217 Check->diag(Location,
218 "differing parameters are named here: (%0), in %1: (%2)",
219 DiagnosticIDs::Level::Note)
220 << joinParameterNames(DifferingParams, ChooseOtherName)
221 << OtherDeclarationDescription
222 << joinParameterNames(DifferingParams, ChooseSourceName);
223
224 for (const DifferingParamInfo &ParamInfo : DifferingParams) {
225 if (ParamInfo.GenerateFixItHint) {
226 ParamDiag << FixItHint::CreateReplacement(
227 CharSourceRange::getTokenRange(ParamInfo.OtherNameRange),
228 ParamInfo.SourceName);
229 }
230 }
231}
232
233void formatDiagnosticsForDeclarations(
234 InconsistentDeclarationParameterNameCheck *Check,
235 const FunctionDecl *ParameterSourceDeclaration,
236 const FunctionDecl *OriginalDeclaration,
237 const InconsistentDeclarationsContainer &InconsistentDeclarations) {
238 Check->diag(
239 OriginalDeclaration->getLocation(),
240 "function %q0 has %1 other declaration%s1 with different parameter names")
241 << OriginalDeclaration
242 << static_cast<int>(InconsistentDeclarations.size());
243 int Count = 1;
244 for (const InconsistentDeclarationInfo &InconsistentDeclaration :
245 InconsistentDeclarations) {
246 Check->diag(InconsistentDeclaration.DeclarationLocation,
247 "the %ordinal0 inconsistent declaration seen here",
248 DiagnosticIDs::Level::Note)
249 << Count;
250
251 formatDifferingParamsDiagnostic(
252 Check, InconsistentDeclaration.DeclarationLocation,
253 "the other declaration", InconsistentDeclaration.DifferingParams);
254
255 ++Count;
256 }
257}
258
259void formatDiagnostics(
260 InconsistentDeclarationParameterNameCheck *Check,
261 const FunctionDecl *ParameterSourceDeclaration,
262 const FunctionDecl *OriginalDeclaration,
263 const InconsistentDeclarationsContainer &InconsistentDeclarations,
264 StringRef FunctionDescription, StringRef ParameterSourceDescription) {
265 for (const InconsistentDeclarationInfo &InconsistentDeclaration :
266 InconsistentDeclarations) {
267 Check->diag(InconsistentDeclaration.DeclarationLocation,
268 "%0 %q1 has a %2 with different parameter names")
269 << FunctionDescription << OriginalDeclaration
270 << ParameterSourceDescription;
271
272 Check->diag(ParameterSourceDeclaration->getLocation(), "the %0 seen here",
273 DiagnosticIDs::Level::Note)
274 << ParameterSourceDescription;
275
276 formatDifferingParamsDiagnostic(
277 Check, InconsistentDeclaration.DeclarationLocation,
278 ParameterSourceDescription, InconsistentDeclaration.DifferingParams);
279 }
280}
281
282} // anonymous namespace
283
Miklos Vajna063e6cc2018-01-05 23:22:10 +0000284void InconsistentDeclarationParameterNameCheck::storeOptions(
285 ClangTidyOptions::OptionMap &Opts) {
286 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
287}
288
Alexander Kornienko11d4d642015-09-10 10:07:11 +0000289void InconsistentDeclarationParameterNameCheck::registerMatchers(
290 MatchFinder *Finder) {
291 Finder->addMatcher(functionDecl(unless(isImplicit()), hasOtherDeclarations())
292 .bind("functionDecl"),
293 this);
294}
295
296void InconsistentDeclarationParameterNameCheck::check(
297 const MatchFinder::MatchResult &Result) {
298 const auto *OriginalDeclaration =
299 Result.Nodes.getNodeAs<FunctionDecl>("functionDecl");
300
301 if (VisitedDeclarations.count(OriginalDeclaration) > 0)
302 return; // Avoid multiple warnings.
303
304 const FunctionDecl *ParameterSourceDeclaration =
305 getParameterSourceDeclaration(OriginalDeclaration);
306
307 InconsistentDeclarationsContainer InconsistentDeclarations =
308 findInconsitentDeclarations(OriginalDeclaration,
309 ParameterSourceDeclaration,
310 *Result.SourceManager);
311 if (InconsistentDeclarations.empty()) {
312 // Avoid unnecessary further visits.
313 markRedeclarationsAsVisited(OriginalDeclaration);
314 return;
315 }
316
Miklos Vajna063e6cc2018-01-05 23:22:10 +0000317 SourceLocation StartLoc = OriginalDeclaration->getLocStart();
318 if (StartLoc.isMacroID() && IgnoreMacros) {
319 markRedeclarationsAsVisited(OriginalDeclaration);
320 return;
321 }
322
Alexander Kornienko11d4d642015-09-10 10:07:11 +0000323 if (OriginalDeclaration->getTemplatedKind() ==
324 FunctionDecl::TK_FunctionTemplateSpecialization) {
325 formatDiagnostics(this, ParameterSourceDeclaration, OriginalDeclaration,
326 InconsistentDeclarations,
327 "function template specialization",
328 "primary template declaration");
329 } else if (ParameterSourceDeclaration->isThisDeclarationADefinition()) {
330 formatDiagnostics(this, ParameterSourceDeclaration, OriginalDeclaration,
331 InconsistentDeclarations, "function", "definition");
332 } else {
333 formatDiagnosticsForDeclarations(this, ParameterSourceDeclaration,
334 OriginalDeclaration,
335 InconsistentDeclarations);
336 }
337
338 markRedeclarationsAsVisited(OriginalDeclaration);
339}
340
341void InconsistentDeclarationParameterNameCheck::markRedeclarationsAsVisited(
342 const FunctionDecl *OriginalDeclaration) {
343 for (const FunctionDecl *Redecl : OriginalDeclaration->redecls()) {
344 VisitedDeclarations.insert(Redecl);
345 }
346}
347
348} // namespace readability
349} // namespace tidy
350} // namespace clang