blob: 992bdc0063be96ffca76a7d484a0d1516d5a60b3 [file] [log] [blame]
Alexander Kornienko76c28802015-08-19 11:15:36 +00001//===--- IdentifierNamingCheck.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 "IdentifierNamingCheck.h"
11
Alexander Kornienko76c28802015-08-19 11:15:36 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
Alexander Kornienko21503902016-06-17 09:25:24 +000013#include "clang/Frontend/CompilerInstance.h"
14#include "clang/Lex/PPCallbacks.h"
15#include "clang/Lex/Preprocessor.h"
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000016#include "llvm/ADT/DenseMapInfo.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/Format.h"
Alexander Kornienko76c28802015-08-19 11:15:36 +000019
20#define DEBUG_TYPE "clang-tidy"
21
22using namespace clang::ast_matchers;
23
Alexander Kornienko21503902016-06-17 09:25:24 +000024namespace llvm {
25/// Specialisation of DenseMapInfo to allow NamingCheckId objects in DenseMaps
26template <>
27struct DenseMapInfo<
28 clang::tidy::readability::IdentifierNamingCheck::NamingCheckId> {
29 using NamingCheckId =
30 clang::tidy::readability::IdentifierNamingCheck::NamingCheckId;
31
32 static inline NamingCheckId getEmptyKey() {
33 return NamingCheckId(
34 clang::SourceLocation::getFromRawEncoding(static_cast<unsigned>(-1)),
35 "EMPTY");
36 }
37
38 static inline NamingCheckId getTombstoneKey() {
39 return NamingCheckId(
40 clang::SourceLocation::getFromRawEncoding(static_cast<unsigned>(-2)),
41 "TOMBSTONE");
42 }
43
44 static unsigned getHashValue(NamingCheckId Val) {
45 assert(Val != getEmptyKey() && "Cannot hash the empty key!");
46 assert(Val != getTombstoneKey() && "Cannot hash the tombstone key!");
47
48 std::hash<NamingCheckId::second_type> SecondHash;
49 return Val.first.getRawEncoding() + SecondHash(Val.second);
50 }
51
Benjamin Kramera079cdb2017-03-21 21:34:58 +000052 static bool isEqual(const NamingCheckId &LHS, const NamingCheckId &RHS) {
Alexander Kornienko21503902016-06-17 09:25:24 +000053 if (RHS == getEmptyKey())
54 return LHS == getEmptyKey();
55 if (RHS == getTombstoneKey())
56 return LHS == getTombstoneKey();
57 return LHS == RHS;
58 }
59};
60} // namespace llvm
61
Alexander Kornienko76c28802015-08-19 11:15:36 +000062namespace clang {
63namespace tidy {
64namespace readability {
65
Alexander Kornienko3d777682015-09-28 08:59:12 +000066// clang-format off
Alexander Kornienko76c28802015-08-19 11:15:36 +000067#define NAMING_KEYS(m) \
68 m(Namespace) \
69 m(InlineNamespace) \
70 m(EnumConstant) \
71 m(ConstexprVariable) \
72 m(ConstantMember) \
73 m(PrivateMember) \
74 m(ProtectedMember) \
75 m(PublicMember) \
76 m(Member) \
77 m(ClassConstant) \
78 m(ClassMember) \
79 m(GlobalConstant) \
80 m(GlobalVariable) \
81 m(LocalConstant) \
82 m(LocalVariable) \
83 m(StaticConstant) \
84 m(StaticVariable) \
85 m(Constant) \
86 m(Variable) \
87 m(ConstantParameter) \
88 m(ParameterPack) \
89 m(Parameter) \
90 m(AbstractClass) \
91 m(Struct) \
92 m(Class) \
93 m(Union) \
94 m(Enum) \
95 m(GlobalFunction) \
96 m(ConstexprFunction) \
97 m(Function) \
98 m(ConstexprMethod) \
99 m(VirtualMethod) \
100 m(ClassMethod) \
101 m(PrivateMethod) \
102 m(ProtectedMethod) \
103 m(PublicMethod) \
104 m(Method) \
105 m(Typedef) \
106 m(TypeTemplateParameter) \
107 m(ValueTemplateParameter) \
108 m(TemplateTemplateParameter) \
109 m(TemplateParameter) \
Alexander Kornienko5ae76e02016-06-07 09:11:19 +0000110 m(TypeAlias) \
Alexander Kornienko21503902016-06-17 09:25:24 +0000111 m(MacroDefinition) \
Yan Zhang004b6c62018-04-23 00:15:15 +0000112 m(ObjcIvar) \
Alexander Kornienko76c28802015-08-19 11:15:36 +0000113
114enum StyleKind {
115#define ENUMERATE(v) SK_ ## v,
116 NAMING_KEYS(ENUMERATE)
117#undef ENUMERATE
118 SK_Count,
119 SK_Invalid
120};
121
122static StringRef const StyleNames[] = {
123#define STRINGIZE(v) #v,
124 NAMING_KEYS(STRINGIZE)
125#undef STRINGIZE
126};
127
128#undef NAMING_KEYS
Alexander Kornienko3d777682015-09-28 08:59:12 +0000129// clang-format on
Alexander Kornienko76c28802015-08-19 11:15:36 +0000130
Alexander Kornienko21503902016-06-17 09:25:24 +0000131namespace {
132/// Callback supplies macros to IdentifierNamingCheck::checkMacro
133class IdentifierNamingCheckPPCallbacks : public PPCallbacks {
134public:
135 IdentifierNamingCheckPPCallbacks(Preprocessor *PP,
136 IdentifierNamingCheck *Check)
137 : PP(PP), Check(Check) {}
138
139 /// MacroDefined calls checkMacro for macros in the main file
140 void MacroDefined(const Token &MacroNameTok,
141 const MacroDirective *MD) override {
142 Check->checkMacro(PP->getSourceManager(), MacroNameTok, MD->getMacroInfo());
143 }
144
145 /// MacroExpands calls expandMacro for macros in the main file
146 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
147 SourceRange /*Range*/,
148 const MacroArgs * /*Args*/) override {
149 Check->expandMacro(MacroNameTok, MD.getMacroInfo());
150 }
151
152private:
153 Preprocessor *PP;
154 IdentifierNamingCheck *Check;
155};
156} // namespace
157
Alexander Kornienko76c28802015-08-19 11:15:36 +0000158IdentifierNamingCheck::IdentifierNamingCheck(StringRef Name,
159 ClangTidyContext *Context)
160 : ClangTidyCheck(Name, Context) {
161 auto const fromString = [](StringRef Str) {
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000162 return llvm::StringSwitch<llvm::Optional<CaseType>>(Str)
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000163 .Case("aNy_CasE", CT_AnyCase)
Alexander Kornienko76c28802015-08-19 11:15:36 +0000164 .Case("lower_case", CT_LowerCase)
165 .Case("UPPER_CASE", CT_UpperCase)
166 .Case("camelBack", CT_CamelBack)
167 .Case("CamelCase", CT_CamelCase)
Kirill Bobyrev5d8f0712016-07-20 12:28:38 +0000168 .Case("Camel_Snake_Case", CT_CamelSnakeCase)
169 .Case("camel_Snake_Back", CT_CamelSnakeBack)
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000170 .Default(llvm::None);
Alexander Kornienko76c28802015-08-19 11:15:36 +0000171 };
172
173 for (auto const &Name : StyleNames) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000174 auto const caseOptional =
175 fromString(Options.get((Name + "Case").str(), ""));
176 auto prefix = Options.get((Name + "Prefix").str(), "");
177 auto postfix = Options.get((Name + "Suffix").str(), "");
178
179 if (caseOptional || !prefix.empty() || !postfix.empty()) {
180 NamingStyles.push_back(NamingStyle(caseOptional, prefix, postfix));
181 } else {
182 NamingStyles.push_back(llvm::None);
183 }
Alexander Kornienko76c28802015-08-19 11:15:36 +0000184 }
185
186 IgnoreFailedSplit = Options.get("IgnoreFailedSplit", 0);
187}
188
189void IdentifierNamingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
190 auto const toString = [](CaseType Type) {
191 switch (Type) {
192 case CT_AnyCase:
193 return "aNy_CasE";
194 case CT_LowerCase:
195 return "lower_case";
196 case CT_CamelBack:
197 return "camelBack";
198 case CT_UpperCase:
199 return "UPPER_CASE";
200 case CT_CamelCase:
201 return "CamelCase";
Kirill Bobyrev5d8f0712016-07-20 12:28:38 +0000202 case CT_CamelSnakeCase:
203 return "Camel_Snake_Case";
204 case CT_CamelSnakeBack:
205 return "camel_Snake_Back";
Alexander Kornienko76c28802015-08-19 11:15:36 +0000206 }
207
208 llvm_unreachable("Unknown Case Type");
209 };
210
211 for (size_t i = 0; i < SK_Count; ++i) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000212 if (NamingStyles[i]) {
213 if (NamingStyles[i]->Case) {
214 Options.store(Opts, (StyleNames[i] + "Case").str(),
215 toString(*NamingStyles[i]->Case));
216 }
217 Options.store(Opts, (StyleNames[i] + "Prefix").str(),
218 NamingStyles[i]->Prefix);
219 Options.store(Opts, (StyleNames[i] + "Suffix").str(),
220 NamingStyles[i]->Suffix);
221 }
Alexander Kornienko76c28802015-08-19 11:15:36 +0000222 }
223
224 Options.store(Opts, "IgnoreFailedSplit", IgnoreFailedSplit);
225}
226
227void IdentifierNamingCheck::registerMatchers(MatchFinder *Finder) {
Alexander Kornienko76c28802015-08-19 11:15:36 +0000228 Finder->addMatcher(namedDecl().bind("decl"), this);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000229 Finder->addMatcher(usingDecl().bind("using"), this);
230 Finder->addMatcher(declRefExpr().bind("declRef"), this);
231 Finder->addMatcher(cxxConstructorDecl().bind("classRef"), this);
232 Finder->addMatcher(cxxDestructorDecl().bind("classRef"), this);
233 Finder->addMatcher(typeLoc().bind("typeLoc"), this);
234 Finder->addMatcher(nestedNameSpecifierLoc().bind("nestedNameLoc"), this);
Alexander Kornienko76c28802015-08-19 11:15:36 +0000235}
236
Alexander Kornienko21503902016-06-17 09:25:24 +0000237void IdentifierNamingCheck::registerPPCallbacks(CompilerInstance &Compiler) {
238 Compiler.getPreprocessor().addPPCallbacks(
239 llvm::make_unique<IdentifierNamingCheckPPCallbacks>(
240 &Compiler.getPreprocessor(), this));
241}
242
Alexander Kornienko76c28802015-08-19 11:15:36 +0000243static bool matchesStyle(StringRef Name,
244 IdentifierNamingCheck::NamingStyle Style) {
245 static llvm::Regex Matchers[] = {
246 llvm::Regex("^.*$"),
247 llvm::Regex("^[a-z][a-z0-9_]*$"),
248 llvm::Regex("^[a-z][a-zA-Z0-9]*$"),
249 llvm::Regex("^[A-Z][A-Z0-9_]*$"),
250 llvm::Regex("^[A-Z][a-zA-Z0-9]*$"),
Kirill Bobyrev5d8f0712016-07-20 12:28:38 +0000251 llvm::Regex("^[A-Z]([a-z0-9]*(_[A-Z])?)*"),
252 llvm::Regex("^[a-z]([a-z0-9]*(_[A-Z])?)*"),
Alexander Kornienko76c28802015-08-19 11:15:36 +0000253 };
254
255 bool Matches = true;
256 if (Name.startswith(Style.Prefix))
257 Name = Name.drop_front(Style.Prefix.size());
258 else
259 Matches = false;
260
261 if (Name.endswith(Style.Suffix))
262 Name = Name.drop_back(Style.Suffix.size());
263 else
264 Matches = false;
265
Alexander Kornienkoeec01ad2017-04-26 16:39:11 +0000266 // Ensure the name doesn't have any extra underscores beyond those specified
267 // in the prefix and suffix.
268 if (Name.startswith("_") || Name.endswith("_"))
269 Matches = false;
270
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000271 if (Style.Case && !Matchers[static_cast<size_t>(*Style.Case)].match(Name))
Alexander Kornienko76c28802015-08-19 11:15:36 +0000272 Matches = false;
273
274 return Matches;
275}
276
277static std::string fixupWithCase(StringRef Name,
278 IdentifierNamingCheck::CaseType Case) {
279 static llvm::Regex Splitter(
280 "([a-z0-9A-Z]*)(_+)|([A-Z]?[a-z0-9]+)([A-Z]|$)|([A-Z]+)([A-Z]|$)");
281
282 SmallVector<StringRef, 8> Substrs;
283 Name.split(Substrs, "_", -1, false);
284
285 SmallVector<StringRef, 8> Words;
286 for (auto Substr : Substrs) {
287 while (!Substr.empty()) {
288 SmallVector<StringRef, 8> Groups;
289 if (!Splitter.match(Substr, &Groups))
290 break;
291
292 if (Groups[2].size() > 0) {
293 Words.push_back(Groups[1]);
294 Substr = Substr.substr(Groups[0].size());
295 } else if (Groups[3].size() > 0) {
296 Words.push_back(Groups[3]);
297 Substr = Substr.substr(Groups[0].size() - Groups[4].size());
298 } else if (Groups[5].size() > 0) {
299 Words.push_back(Groups[5]);
300 Substr = Substr.substr(Groups[0].size() - Groups[6].size());
301 }
302 }
303 }
304
305 if (Words.empty())
306 return Name;
307
308 std::string Fixup;
309 switch (Case) {
310 case IdentifierNamingCheck::CT_AnyCase:
311 Fixup += Name;
312 break;
313
314 case IdentifierNamingCheck::CT_LowerCase:
315 for (auto const &Word : Words) {
316 if (&Word != &Words.front())
317 Fixup += "_";
318 Fixup += Word.lower();
319 }
320 break;
321
322 case IdentifierNamingCheck::CT_UpperCase:
323 for (auto const &Word : Words) {
324 if (&Word != &Words.front())
325 Fixup += "_";
326 Fixup += Word.upper();
327 }
328 break;
329
330 case IdentifierNamingCheck::CT_CamelCase:
331 for (auto const &Word : Words) {
332 Fixup += Word.substr(0, 1).upper();
333 Fixup += Word.substr(1).lower();
334 }
335 break;
336
337 case IdentifierNamingCheck::CT_CamelBack:
338 for (auto const &Word : Words) {
339 if (&Word == &Words.front()) {
340 Fixup += Word.lower();
341 } else {
342 Fixup += Word.substr(0, 1).upper();
343 Fixup += Word.substr(1).lower();
344 }
345 }
346 break;
Kirill Bobyrev5d8f0712016-07-20 12:28:38 +0000347
348 case IdentifierNamingCheck::CT_CamelSnakeCase:
349 for (auto const &Word : Words) {
350 if (&Word != &Words.front())
351 Fixup += "_";
352 Fixup += Word.substr(0, 1).upper();
353 Fixup += Word.substr(1).lower();
354 }
355 break;
356
357 case IdentifierNamingCheck::CT_CamelSnakeBack:
358 for (auto const &Word : Words) {
359 if (&Word != &Words.front()) {
360 Fixup += "_";
361 Fixup += Word.substr(0, 1).upper();
362 } else {
363 Fixup += Word.substr(0, 1).lower();
364 }
365 Fixup += Word.substr(1).lower();
366 }
367 break;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000368 }
369
370 return Fixup;
371}
372
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000373static std::string
374fixupWithStyle(StringRef Name,
375 const IdentifierNamingCheck::NamingStyle &Style) {
Alexander Kornienkoeec01ad2017-04-26 16:39:11 +0000376 const std::string Fixed = fixupWithCase(
377 Name, Style.Case.getValueOr(IdentifierNamingCheck::CaseType::CT_AnyCase));
378 StringRef Mid = StringRef(Fixed).trim("_");
379 if (Mid.empty())
380 Mid = "_";
381 return (Style.Prefix + Mid + Style.Suffix).str();
Alexander Kornienko76c28802015-08-19 11:15:36 +0000382}
383
384static StyleKind findStyleKind(
385 const NamedDecl *D,
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000386 const std::vector<llvm::Optional<IdentifierNamingCheck::NamingStyle>>
387 &NamingStyles) {
Yan Zhang004b6c62018-04-23 00:15:15 +0000388 if (isa<ObjCIvarDecl>(D) && NamingStyles[SK_ObjcIvar])
389 return SK_ObjcIvar;
390
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000391 if (isa<TypedefDecl>(D) && NamingStyles[SK_Typedef])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000392 return SK_Typedef;
393
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000394 if (isa<TypeAliasDecl>(D) && NamingStyles[SK_TypeAlias])
Alexander Kornienko5ae76e02016-06-07 09:11:19 +0000395 return SK_TypeAlias;
396
Alexander Kornienko76c28802015-08-19 11:15:36 +0000397 if (const auto *Decl = dyn_cast<NamespaceDecl>(D)) {
398 if (Decl->isAnonymousNamespace())
399 return SK_Invalid;
400
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000401 if (Decl->isInline() && NamingStyles[SK_InlineNamespace])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000402 return SK_InlineNamespace;
403
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000404 if (NamingStyles[SK_Namespace])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000405 return SK_Namespace;
406 }
407
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000408 if (isa<EnumDecl>(D) && NamingStyles[SK_Enum])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000409 return SK_Enum;
410
411 if (isa<EnumConstantDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000412 if (NamingStyles[SK_EnumConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000413 return SK_EnumConstant;
414
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000415 if (NamingStyles[SK_Constant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000416 return SK_Constant;
417
418 return SK_Invalid;
419 }
420
421 if (const auto *Decl = dyn_cast<CXXRecordDecl>(D)) {
422 if (Decl->isAnonymousStructOrUnion())
423 return SK_Invalid;
424
Jonathan Coe89f12c02016-11-03 13:52:09 +0000425 if (!Decl->getCanonicalDecl()->isThisDeclarationADefinition())
426 return SK_Invalid;
427
Alexander Kornienko76c28802015-08-19 11:15:36 +0000428 if (Decl->hasDefinition() && Decl->isAbstract() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000429 NamingStyles[SK_AbstractClass])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000430 return SK_AbstractClass;
431
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000432 if (Decl->isStruct() && NamingStyles[SK_Struct])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000433 return SK_Struct;
434
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000435 if (Decl->isStruct() && NamingStyles[SK_Class])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000436 return SK_Class;
437
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000438 if (Decl->isClass() && NamingStyles[SK_Class])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000439 return SK_Class;
440
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000441 if (Decl->isClass() && NamingStyles[SK_Struct])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000442 return SK_Struct;
443
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000444 if (Decl->isUnion() && NamingStyles[SK_Union])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000445 return SK_Union;
446
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000447 if (Decl->isEnum() && NamingStyles[SK_Enum])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000448 return SK_Enum;
449
450 return SK_Invalid;
451 }
452
453 if (const auto *Decl = dyn_cast<FieldDecl>(D)) {
454 QualType Type = Decl->getType();
455
Alexander Kornienkoba874922017-12-11 19:02:26 +0000456 if (!Type.isNull() && Type.isConstQualified()) {
457 if (NamingStyles[SK_ConstantMember])
458 return SK_ConstantMember;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000459
Alexander Kornienkoba874922017-12-11 19:02:26 +0000460 if (NamingStyles[SK_Constant])
461 return SK_Constant;
462 }
Alexander Kornienko76c28802015-08-19 11:15:36 +0000463
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000464 if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000465 return SK_PrivateMember;
466
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000467 if (Decl->getAccess() == AS_protected && NamingStyles[SK_ProtectedMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000468 return SK_ProtectedMember;
469
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000470 if (Decl->getAccess() == AS_public && NamingStyles[SK_PublicMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000471 return SK_PublicMember;
472
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000473 if (NamingStyles[SK_Member])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000474 return SK_Member;
475
476 return SK_Invalid;
477 }
478
479 if (const auto *Decl = dyn_cast<ParmVarDecl>(D)) {
480 QualType Type = Decl->getType();
481
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000482 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000483 return SK_ConstexprVariable;
484
Alexander Kornienkoba874922017-12-11 19:02:26 +0000485 if (!Type.isNull() && Type.isConstQualified()) {
486 if (NamingStyles[SK_ConstantParameter])
487 return SK_ConstantParameter;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000488
Alexander Kornienkoba874922017-12-11 19:02:26 +0000489 if (NamingStyles[SK_Constant])
490 return SK_Constant;
491 }
Alexander Kornienko76c28802015-08-19 11:15:36 +0000492
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000493 if (Decl->isParameterPack() && NamingStyles[SK_ParameterPack])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000494 return SK_ParameterPack;
495
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000496 if (NamingStyles[SK_Parameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000497 return SK_Parameter;
498
499 return SK_Invalid;
500 }
501
502 if (const auto *Decl = dyn_cast<VarDecl>(D)) {
503 QualType Type = Decl->getType();
504
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000505 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000506 return SK_ConstexprVariable;
507
Alexander Kornienkoba874922017-12-11 19:02:26 +0000508 if (!Type.isNull() && Type.isConstQualified()) {
509 if (Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant])
510 return SK_ClassConstant;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000511
Alexander Kornienkoba874922017-12-11 19:02:26 +0000512 if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant])
513 return SK_GlobalConstant;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000514
Alexander Kornienkoba874922017-12-11 19:02:26 +0000515 if (Decl->isStaticLocal() && NamingStyles[SK_StaticConstant])
516 return SK_StaticConstant;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000517
Alexander Kornienkoba874922017-12-11 19:02:26 +0000518 if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant])
519 return SK_LocalConstant;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000520
Alexander Kornienkoba874922017-12-11 19:02:26 +0000521 if (Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalConstant])
522 return SK_LocalConstant;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000523
Alexander Kornienkoba874922017-12-11 19:02:26 +0000524 if (NamingStyles[SK_Constant])
525 return SK_Constant;
526 }
Alexander Kornienko76c28802015-08-19 11:15:36 +0000527
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000528 if (Decl->isStaticDataMember() && NamingStyles[SK_ClassMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000529 return SK_ClassMember;
530
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000531 if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000532 return SK_GlobalVariable;
533
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000534 if (Decl->isStaticLocal() && NamingStyles[SK_StaticVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000535 return SK_StaticVariable;
536
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000537 if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000538 return SK_LocalVariable;
539
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000540 if (Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000541 return SK_LocalVariable;
542
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000543 if (NamingStyles[SK_Variable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000544 return SK_Variable;
545
546 return SK_Invalid;
547 }
548
549 if (const auto *Decl = dyn_cast<CXXMethodDecl>(D)) {
550 if (Decl->isMain() || !Decl->isUserProvided() ||
551 Decl->isUsualDeallocationFunction() ||
552 Decl->isCopyAssignmentOperator() || Decl->isMoveAssignmentOperator() ||
553 Decl->size_overridden_methods() > 0)
554 return SK_Invalid;
555
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000556 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000557 return SK_ConstexprMethod;
558
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000559 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprFunction])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000560 return SK_ConstexprFunction;
561
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000562 if (Decl->isStatic() && NamingStyles[SK_ClassMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000563 return SK_ClassMethod;
564
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000565 if (Decl->isVirtual() && NamingStyles[SK_VirtualMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000566 return SK_VirtualMethod;
567
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000568 if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000569 return SK_PrivateMethod;
570
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000571 if (Decl->getAccess() == AS_protected && NamingStyles[SK_ProtectedMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000572 return SK_ProtectedMethod;
573
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000574 if (Decl->getAccess() == AS_public && NamingStyles[SK_PublicMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000575 return SK_PublicMethod;
576
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000577 if (NamingStyles[SK_Method])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000578 return SK_Method;
579
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000580 if (NamingStyles[SK_Function])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000581 return SK_Function;
582
583 return SK_Invalid;
584 }
585
586 if (const auto *Decl = dyn_cast<FunctionDecl>(D)) {
587 if (Decl->isMain())
588 return SK_Invalid;
589
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000590 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprFunction])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000591 return SK_ConstexprFunction;
592
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000593 if (Decl->isGlobal() && NamingStyles[SK_GlobalFunction])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000594 return SK_GlobalFunction;
595
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000596 if (NamingStyles[SK_Function])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000597 return SK_Function;
598 }
599
600 if (isa<TemplateTypeParmDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000601 if (NamingStyles[SK_TypeTemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000602 return SK_TypeTemplateParameter;
603
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000604 if (NamingStyles[SK_TemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000605 return SK_TemplateParameter;
606
607 return SK_Invalid;
608 }
609
610 if (isa<NonTypeTemplateParmDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000611 if (NamingStyles[SK_ValueTemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000612 return SK_ValueTemplateParameter;
613
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000614 if (NamingStyles[SK_TemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000615 return SK_TemplateParameter;
616
617 return SK_Invalid;
618 }
619
620 if (isa<TemplateTemplateParmDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000621 if (NamingStyles[SK_TemplateTemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000622 return SK_TemplateTemplateParameter;
623
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000624 if (NamingStyles[SK_TemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000625 return SK_TemplateParameter;
626
627 return SK_Invalid;
628 }
629
630 return SK_Invalid;
631}
632
Alexander Kornienko3d777682015-09-28 08:59:12 +0000633static void addUsage(IdentifierNamingCheck::NamingCheckFailureMap &Failures,
Alexander Kornienko21503902016-06-17 09:25:24 +0000634 const IdentifierNamingCheck::NamingCheckId &Decl,
Jason Henline481e6aa2016-10-24 17:20:32 +0000635 SourceRange Range, SourceManager *SourceMgr = nullptr) {
Jonathan Coe2c8592a2016-01-26 18:55:55 +0000636 // Do nothing if the provided range is invalid.
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000637 if (Range.getBegin().isInvalid() || Range.getEnd().isInvalid())
638 return;
639
Jason Henline481e6aa2016-10-24 17:20:32 +0000640 // If we have a source manager, use it to convert to the spelling location for
641 // performing the fix. This is necessary because macros can map the same
642 // spelling location to different source locations, and we only want to fix
643 // the token once, before it is expanded by the macro.
644 SourceLocation FixLocation = Range.getBegin();
645 if (SourceMgr)
646 FixLocation = SourceMgr->getSpellingLoc(FixLocation);
647 if (FixLocation.isInvalid())
648 return;
649
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000650 // Try to insert the identifier location in the Usages map, and bail out if it
651 // is already in there
Alexander Kornienko3d777682015-09-28 08:59:12 +0000652 auto &Failure = Failures[Decl];
Jason Henline481e6aa2016-10-24 17:20:32 +0000653 if (!Failure.RawUsageLocs.insert(FixLocation.getRawEncoding()).second)
Alexander Kornienko3d777682015-09-28 08:59:12 +0000654 return;
655
Jason Henline481e6aa2016-10-24 17:20:32 +0000656 if (!Failure.ShouldFix)
657 return;
658
659 // Check if the range is entirely contained within a macro argument.
660 SourceLocation MacroArgExpansionStartForRangeBegin;
661 SourceLocation MacroArgExpansionStartForRangeEnd;
662 bool RangeIsEntirelyWithinMacroArgument =
663 SourceMgr &&
664 SourceMgr->isMacroArgExpansion(Range.getBegin(),
665 &MacroArgExpansionStartForRangeBegin) &&
666 SourceMgr->isMacroArgExpansion(Range.getEnd(),
667 &MacroArgExpansionStartForRangeEnd) &&
668 MacroArgExpansionStartForRangeBegin == MacroArgExpansionStartForRangeEnd;
669
670 // Check if the range contains any locations from a macro expansion.
671 bool RangeContainsMacroExpansion = RangeIsEntirelyWithinMacroArgument ||
672 Range.getBegin().isMacroID() ||
673 Range.getEnd().isMacroID();
674
675 bool RangeCanBeFixed =
676 RangeIsEntirelyWithinMacroArgument || !RangeContainsMacroExpansion;
677 Failure.ShouldFix = RangeCanBeFixed;
Alexander Kornienko3d777682015-09-28 08:59:12 +0000678}
679
Alexander Kornienko21503902016-06-17 09:25:24 +0000680/// Convenience method when the usage to be added is a NamedDecl
681static void addUsage(IdentifierNamingCheck::NamingCheckFailureMap &Failures,
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000682 const NamedDecl *Decl, SourceRange Range,
683 SourceManager *SourceMgr = nullptr) {
Alexander Kornienkoba874922017-12-11 19:02:26 +0000684 return addUsage(Failures,
685 IdentifierNamingCheck::NamingCheckId(Decl->getLocation(),
686 Decl->getNameAsString()),
Jason Henline481e6aa2016-10-24 17:20:32 +0000687 Range, SourceMgr);
Alexander Kornienko21503902016-06-17 09:25:24 +0000688}
689
Alexander Kornienko76c28802015-08-19 11:15:36 +0000690void IdentifierNamingCheck::check(const MatchFinder::MatchResult &Result) {
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000691 if (const auto *Decl =
692 Result.Nodes.getNodeAs<CXXConstructorDecl>("classRef")) {
693 if (Decl->isImplicit())
694 return;
695
696 addUsage(NamingCheckFailures, Decl->getParent(),
Alexander Kornienko21503902016-06-17 09:25:24 +0000697 Decl->getNameInfo().getSourceRange());
Eric Fiselier732a3e02016-11-16 21:15:58 +0000698
699 for (const auto *Init : Decl->inits()) {
700 if (!Init->isWritten() || Init->isInClassMemberInitializer())
701 continue;
702 if (const auto *FD = Init->getAnyMember())
Alexander Kornienkoba874922017-12-11 19:02:26 +0000703 addUsage(NamingCheckFailures, FD,
704 SourceRange(Init->getMemberLocation()));
Eric Fiselier732a3e02016-11-16 21:15:58 +0000705 // Note: delegating constructors and base class initializers are handled
706 // via the "typeLoc" matcher.
707 }
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000708 return;
709 }
710
711 if (const auto *Decl =
712 Result.Nodes.getNodeAs<CXXDestructorDecl>("classRef")) {
713 if (Decl->isImplicit())
714 return;
715
716 SourceRange Range = Decl->getNameInfo().getSourceRange();
717 if (Range.getBegin().isInvalid())
718 return;
719 // The first token that will be found is the ~ (or the equivalent trigraph),
720 // we want instead to replace the next token, that will be the identifier.
721 Range.setBegin(CharSourceRange::getTokenRange(Range).getEnd());
722
Alexander Kornienko21503902016-06-17 09:25:24 +0000723 addUsage(NamingCheckFailures, Decl->getParent(), Range);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000724 return;
725 }
726
727 if (const auto *Loc = Result.Nodes.getNodeAs<TypeLoc>("typeLoc")) {
728 NamedDecl *Decl = nullptr;
729 if (const auto &Ref = Loc->getAs<TagTypeLoc>()) {
730 Decl = Ref.getDecl();
731 } else if (const auto &Ref = Loc->getAs<InjectedClassNameTypeLoc>()) {
732 Decl = Ref.getDecl();
733 } else if (const auto &Ref = Loc->getAs<UnresolvedUsingTypeLoc>()) {
734 Decl = Ref.getDecl();
735 } else if (const auto &Ref = Loc->getAs<TemplateTypeParmTypeLoc>()) {
736 Decl = Ref.getDecl();
737 }
738
739 if (Decl) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000740 addUsage(NamingCheckFailures, Decl, Loc->getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000741 return;
742 }
743
744 if (const auto &Ref = Loc->getAs<TemplateSpecializationTypeLoc>()) {
745 const auto *Decl =
746 Ref.getTypePtr()->getTemplateName().getAsTemplateDecl();
747
748 SourceRange Range(Ref.getTemplateNameLoc(), Ref.getTemplateNameLoc());
749 if (const auto *ClassDecl = dyn_cast<TemplateDecl>(Decl)) {
Matthias Gehrea9e812b2016-07-09 20:09:28 +0000750 if (const auto *TemplDecl = ClassDecl->getTemplatedDecl())
751 addUsage(NamingCheckFailures, TemplDecl, Range);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000752 return;
753 }
754 }
755
756 if (const auto &Ref =
757 Loc->getAs<DependentTemplateSpecializationTypeLoc>()) {
Matthias Gehrea9e812b2016-07-09 20:09:28 +0000758 if (const auto *Decl = Ref.getTypePtr()->getAsTagDecl())
759 addUsage(NamingCheckFailures, Decl, Loc->getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000760 return;
761 }
762 }
763
764 if (const auto *Loc =
765 Result.Nodes.getNodeAs<NestedNameSpecifierLoc>("nestedNameLoc")) {
766 if (NestedNameSpecifier *Spec = Loc->getNestedNameSpecifier()) {
767 if (NamespaceDecl *Decl = Spec->getAsNamespace()) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000768 addUsage(NamingCheckFailures, Decl, Loc->getLocalSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000769 return;
770 }
771 }
772 }
773
774 if (const auto *Decl = Result.Nodes.getNodeAs<UsingDecl>("using")) {
775 for (const auto &Shadow : Decl->shadows()) {
776 addUsage(NamingCheckFailures, Shadow->getTargetDecl(),
Alexander Kornienko21503902016-06-17 09:25:24 +0000777 Decl->getNameInfo().getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000778 }
779 return;
780 }
781
782 if (const auto *DeclRef = Result.Nodes.getNodeAs<DeclRefExpr>("declRef")) {
Alexander Kornienko76c28802015-08-19 11:15:36 +0000783 SourceRange Range = DeclRef->getNameInfo().getSourceRange();
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000784 addUsage(NamingCheckFailures, DeclRef->getDecl(), Range,
785 Result.SourceManager);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000786 return;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000787 }
788
789 if (const auto *Decl = Result.Nodes.getNodeAs<NamedDecl>("decl")) {
790 if (!Decl->getIdentifier() || Decl->getName().empty() || Decl->isImplicit())
791 return;
792
Alexander Kornienko21503902016-06-17 09:25:24 +0000793 // Fix type aliases in value declarations
794 if (const auto *Value = Result.Nodes.getNodeAs<ValueDecl>("decl")) {
795 if (const auto *Typedef =
796 Value->getType().getTypePtr()->getAs<TypedefType>()) {
797 addUsage(NamingCheckFailures, Typedef->getDecl(),
798 Value->getSourceRange());
799 }
800 }
801
802 // Fix type aliases in function declarations
803 if (const auto *Value = Result.Nodes.getNodeAs<FunctionDecl>("decl")) {
804 if (const auto *Typedef =
805 Value->getReturnType().getTypePtr()->getAs<TypedefType>()) {
806 addUsage(NamingCheckFailures, Typedef->getDecl(),
807 Value->getSourceRange());
808 }
809 for (unsigned i = 0; i < Value->getNumParams(); ++i) {
810 if (const auto *Typedef = Value->parameters()[i]
811 ->getType()
812 .getTypePtr()
813 ->getAs<TypedefType>()) {
814 addUsage(NamingCheckFailures, Typedef->getDecl(),
815 Value->getSourceRange());
816 }
817 }
818 }
819
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000820 // Ignore ClassTemplateSpecializationDecl which are creating duplicate
821 // replacements with CXXRecordDecl
822 if (isa<ClassTemplateSpecializationDecl>(Decl))
823 return;
824
Alexander Kornienko76c28802015-08-19 11:15:36 +0000825 StyleKind SK = findStyleKind(Decl, NamingStyles);
826 if (SK == SK_Invalid)
827 return;
828
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000829 if (!NamingStyles[SK])
830 return;
831
832 const NamingStyle &Style = *NamingStyles[SK];
Alexander Kornienko76c28802015-08-19 11:15:36 +0000833 StringRef Name = Decl->getName();
834 if (matchesStyle(Name, Style))
835 return;
836
837 std::string KindName = fixupWithCase(StyleNames[SK], CT_LowerCase);
838 std::replace(KindName.begin(), KindName.end(), '_', ' ');
839
840 std::string Fixup = fixupWithStyle(Name, Style);
841 if (StringRef(Fixup).equals(Name)) {
842 if (!IgnoreFailedSplit) {
Hans Wennborg53bd9f32016-02-29 21:17:39 +0000843 DEBUG(llvm::dbgs()
844 << Decl->getLocStart().printToString(*Result.SourceManager)
845 << llvm::format(": unable to split words for %s '%s'\n",
Mehdi Amini9ff8e872016-10-07 08:25:42 +0000846 KindName.c_str(), Name.str().c_str()));
Alexander Kornienko76c28802015-08-19 11:15:36 +0000847 }
848 } else {
Alexander Kornienko21503902016-06-17 09:25:24 +0000849 NamingCheckFailure &Failure = NamingCheckFailures[NamingCheckId(
850 Decl->getLocation(), Decl->getNameAsString())];
Alexander Kornienko76c28802015-08-19 11:15:36 +0000851 SourceRange Range =
852 DeclarationNameInfo(Decl->getDeclName(), Decl->getLocation())
853 .getSourceRange();
854
855 Failure.Fixup = std::move(Fixup);
856 Failure.KindName = std::move(KindName);
Alexander Kornienko21503902016-06-17 09:25:24 +0000857 addUsage(NamingCheckFailures, Decl, Range);
Alexander Kornienko76c28802015-08-19 11:15:36 +0000858 }
859 }
860}
861
Alexander Kornienko21503902016-06-17 09:25:24 +0000862void IdentifierNamingCheck::checkMacro(SourceManager &SourceMgr,
863 const Token &MacroNameTok,
864 const MacroInfo *MI) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000865 if (!NamingStyles[SK_MacroDefinition])
866 return;
867
Alexander Kornienko21503902016-06-17 09:25:24 +0000868 StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000869 const NamingStyle &Style = *NamingStyles[SK_MacroDefinition];
Alexander Kornienko21503902016-06-17 09:25:24 +0000870 if (matchesStyle(Name, Style))
871 return;
872
873 std::string KindName =
874 fixupWithCase(StyleNames[SK_MacroDefinition], CT_LowerCase);
875 std::replace(KindName.begin(), KindName.end(), '_', ' ');
876
877 std::string Fixup = fixupWithStyle(Name, Style);
878 if (StringRef(Fixup).equals(Name)) {
879 if (!IgnoreFailedSplit) {
880 DEBUG(
881 llvm::dbgs() << MacroNameTok.getLocation().printToString(SourceMgr)
882 << llvm::format(": unable to split words for %s '%s'\n",
Mehdi Amini9ff8e872016-10-07 08:25:42 +0000883 KindName.c_str(), Name.str().c_str()));
Alexander Kornienko21503902016-06-17 09:25:24 +0000884 }
885 } else {
886 NamingCheckId ID(MI->getDefinitionLoc(), Name);
887 NamingCheckFailure &Failure = NamingCheckFailures[ID];
888 SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
889
890 Failure.Fixup = std::move(Fixup);
891 Failure.KindName = std::move(KindName);
892 addUsage(NamingCheckFailures, ID, Range);
893 }
894}
895
896void IdentifierNamingCheck::expandMacro(const Token &MacroNameTok,
897 const MacroInfo *MI) {
898 StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
899 NamingCheckId ID(MI->getDefinitionLoc(), Name);
900
901 auto Failure = NamingCheckFailures.find(ID);
902 if (Failure == NamingCheckFailures.end())
903 return;
904
905 SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
906 addUsage(NamingCheckFailures, ID, Range);
907}
908
Alexander Kornienko76c28802015-08-19 11:15:36 +0000909void IdentifierNamingCheck::onEndOfTranslationUnit() {
910 for (const auto &Pair : NamingCheckFailures) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000911 const NamingCheckId &Decl = Pair.first;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000912 const NamingCheckFailure &Failure = Pair.second;
913
Alexander Kornienko3d777682015-09-28 08:59:12 +0000914 if (Failure.KindName.empty())
915 continue;
916
Alexander Kornienko76c28802015-08-19 11:15:36 +0000917 if (Failure.ShouldFix) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000918 auto Diag = diag(Decl.first, "invalid case style for %0 '%1'")
919 << Failure.KindName << Decl.second;
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000920
Alexander Kornienko3d777682015-09-28 08:59:12 +0000921 for (const auto &Loc : Failure.RawUsageLocs) {
922 // We assume that the identifier name is made of one token only. This is
923 // always the case as we ignore usages in macros that could build
924 // identifier names by combining multiple tokens.
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000925 //
926 // For destructors, we alread take care of it by remembering the
927 // location of the start of the identifier and not the start of the
928 // tilde.
929 //
930 // Other multi-token identifiers, such as operators are not checked at
931 // all.
Alexander Kornienko76c28802015-08-19 11:15:36 +0000932 Diag << FixItHint::CreateReplacement(
Alexander Kornienko3d777682015-09-28 08:59:12 +0000933 SourceRange(SourceLocation::getFromRawEncoding(Loc)),
934 Failure.Fixup);
Alexander Kornienko76c28802015-08-19 11:15:36 +0000935 }
936 }
937 }
938}
939
940} // namespace readability
941} // namespace tidy
942} // namespace clang