blob: 175feea0faaec9c3400f055b6aba08f0d77e96a3 [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) \
Alexander Kornienko76c28802015-08-19 11:15:36 +0000112
113enum StyleKind {
114#define ENUMERATE(v) SK_ ## v,
115 NAMING_KEYS(ENUMERATE)
116#undef ENUMERATE
117 SK_Count,
118 SK_Invalid
119};
120
121static StringRef const StyleNames[] = {
122#define STRINGIZE(v) #v,
123 NAMING_KEYS(STRINGIZE)
124#undef STRINGIZE
125};
126
127#undef NAMING_KEYS
Alexander Kornienko3d777682015-09-28 08:59:12 +0000128// clang-format on
Alexander Kornienko76c28802015-08-19 11:15:36 +0000129
Alexander Kornienko21503902016-06-17 09:25:24 +0000130namespace {
131/// Callback supplies macros to IdentifierNamingCheck::checkMacro
132class IdentifierNamingCheckPPCallbacks : public PPCallbacks {
133public:
134 IdentifierNamingCheckPPCallbacks(Preprocessor *PP,
135 IdentifierNamingCheck *Check)
136 : PP(PP), Check(Check) {}
137
138 /// MacroDefined calls checkMacro for macros in the main file
139 void MacroDefined(const Token &MacroNameTok,
140 const MacroDirective *MD) override {
141 Check->checkMacro(PP->getSourceManager(), MacroNameTok, MD->getMacroInfo());
142 }
143
144 /// MacroExpands calls expandMacro for macros in the main file
145 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
146 SourceRange /*Range*/,
147 const MacroArgs * /*Args*/) override {
148 Check->expandMacro(MacroNameTok, MD.getMacroInfo());
149 }
150
151private:
152 Preprocessor *PP;
153 IdentifierNamingCheck *Check;
154};
155} // namespace
156
Alexander Kornienko76c28802015-08-19 11:15:36 +0000157IdentifierNamingCheck::IdentifierNamingCheck(StringRef Name,
158 ClangTidyContext *Context)
159 : ClangTidyCheck(Name, Context) {
160 auto const fromString = [](StringRef Str) {
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000161 return llvm::StringSwitch<llvm::Optional<CaseType>>(Str)
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000162 .Case("aNy_CasE", CT_AnyCase)
Alexander Kornienko76c28802015-08-19 11:15:36 +0000163 .Case("lower_case", CT_LowerCase)
164 .Case("UPPER_CASE", CT_UpperCase)
165 .Case("camelBack", CT_CamelBack)
166 .Case("CamelCase", CT_CamelCase)
Kirill Bobyrev5d8f0712016-07-20 12:28:38 +0000167 .Case("Camel_Snake_Case", CT_CamelSnakeCase)
168 .Case("camel_Snake_Back", CT_CamelSnakeBack)
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000169 .Default(llvm::None);
Alexander Kornienko76c28802015-08-19 11:15:36 +0000170 };
171
172 for (auto const &Name : StyleNames) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000173 auto const caseOptional =
174 fromString(Options.get((Name + "Case").str(), ""));
175 auto prefix = Options.get((Name + "Prefix").str(), "");
176 auto postfix = Options.get((Name + "Suffix").str(), "");
177
178 if (caseOptional || !prefix.empty() || !postfix.empty()) {
179 NamingStyles.push_back(NamingStyle(caseOptional, prefix, postfix));
180 } else {
181 NamingStyles.push_back(llvm::None);
182 }
Alexander Kornienko76c28802015-08-19 11:15:36 +0000183 }
184
185 IgnoreFailedSplit = Options.get("IgnoreFailedSplit", 0);
186}
187
188void IdentifierNamingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
189 auto const toString = [](CaseType Type) {
190 switch (Type) {
191 case CT_AnyCase:
192 return "aNy_CasE";
193 case CT_LowerCase:
194 return "lower_case";
195 case CT_CamelBack:
196 return "camelBack";
197 case CT_UpperCase:
198 return "UPPER_CASE";
199 case CT_CamelCase:
200 return "CamelCase";
Kirill Bobyrev5d8f0712016-07-20 12:28:38 +0000201 case CT_CamelSnakeCase:
202 return "Camel_Snake_Case";
203 case CT_CamelSnakeBack:
204 return "camel_Snake_Back";
Alexander Kornienko76c28802015-08-19 11:15:36 +0000205 }
206
207 llvm_unreachable("Unknown Case Type");
208 };
209
210 for (size_t i = 0; i < SK_Count; ++i) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000211 if (NamingStyles[i]) {
212 if (NamingStyles[i]->Case) {
213 Options.store(Opts, (StyleNames[i] + "Case").str(),
214 toString(*NamingStyles[i]->Case));
215 }
216 Options.store(Opts, (StyleNames[i] + "Prefix").str(),
217 NamingStyles[i]->Prefix);
218 Options.store(Opts, (StyleNames[i] + "Suffix").str(),
219 NamingStyles[i]->Suffix);
220 }
Alexander Kornienko76c28802015-08-19 11:15:36 +0000221 }
222
223 Options.store(Opts, "IgnoreFailedSplit", IgnoreFailedSplit);
224}
225
226void IdentifierNamingCheck::registerMatchers(MatchFinder *Finder) {
Alexander Kornienko76c28802015-08-19 11:15:36 +0000227 Finder->addMatcher(namedDecl().bind("decl"), this);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000228 Finder->addMatcher(usingDecl().bind("using"), this);
229 Finder->addMatcher(declRefExpr().bind("declRef"), this);
230 Finder->addMatcher(cxxConstructorDecl().bind("classRef"), this);
231 Finder->addMatcher(cxxDestructorDecl().bind("classRef"), this);
232 Finder->addMatcher(typeLoc().bind("typeLoc"), this);
233 Finder->addMatcher(nestedNameSpecifierLoc().bind("nestedNameLoc"), this);
Alexander Kornienko76c28802015-08-19 11:15:36 +0000234}
235
Alexander Kornienko21503902016-06-17 09:25:24 +0000236void IdentifierNamingCheck::registerPPCallbacks(CompilerInstance &Compiler) {
237 Compiler.getPreprocessor().addPPCallbacks(
238 llvm::make_unique<IdentifierNamingCheckPPCallbacks>(
239 &Compiler.getPreprocessor(), this));
240}
241
Alexander Kornienko76c28802015-08-19 11:15:36 +0000242static bool matchesStyle(StringRef Name,
243 IdentifierNamingCheck::NamingStyle Style) {
244 static llvm::Regex Matchers[] = {
245 llvm::Regex("^.*$"),
246 llvm::Regex("^[a-z][a-z0-9_]*$"),
247 llvm::Regex("^[a-z][a-zA-Z0-9]*$"),
248 llvm::Regex("^[A-Z][A-Z0-9_]*$"),
249 llvm::Regex("^[A-Z][a-zA-Z0-9]*$"),
Kirill Bobyrev5d8f0712016-07-20 12:28:38 +0000250 llvm::Regex("^[A-Z]([a-z0-9]*(_[A-Z])?)*"),
251 llvm::Regex("^[a-z]([a-z0-9]*(_[A-Z])?)*"),
Alexander Kornienko76c28802015-08-19 11:15:36 +0000252 };
253
254 bool Matches = true;
255 if (Name.startswith(Style.Prefix))
256 Name = Name.drop_front(Style.Prefix.size());
257 else
258 Matches = false;
259
260 if (Name.endswith(Style.Suffix))
261 Name = Name.drop_back(Style.Suffix.size());
262 else
263 Matches = false;
264
Alexander Kornienkoeec01ad2017-04-26 16:39:11 +0000265 // Ensure the name doesn't have any extra underscores beyond those specified
266 // in the prefix and suffix.
267 if (Name.startswith("_") || Name.endswith("_"))
268 Matches = false;
269
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000270 if (Style.Case && !Matchers[static_cast<size_t>(*Style.Case)].match(Name))
Alexander Kornienko76c28802015-08-19 11:15:36 +0000271 Matches = false;
272
273 return Matches;
274}
275
276static std::string fixupWithCase(StringRef Name,
277 IdentifierNamingCheck::CaseType Case) {
278 static llvm::Regex Splitter(
279 "([a-z0-9A-Z]*)(_+)|([A-Z]?[a-z0-9]+)([A-Z]|$)|([A-Z]+)([A-Z]|$)");
280
281 SmallVector<StringRef, 8> Substrs;
282 Name.split(Substrs, "_", -1, false);
283
284 SmallVector<StringRef, 8> Words;
285 for (auto Substr : Substrs) {
286 while (!Substr.empty()) {
287 SmallVector<StringRef, 8> Groups;
288 if (!Splitter.match(Substr, &Groups))
289 break;
290
291 if (Groups[2].size() > 0) {
292 Words.push_back(Groups[1]);
293 Substr = Substr.substr(Groups[0].size());
294 } else if (Groups[3].size() > 0) {
295 Words.push_back(Groups[3]);
296 Substr = Substr.substr(Groups[0].size() - Groups[4].size());
297 } else if (Groups[5].size() > 0) {
298 Words.push_back(Groups[5]);
299 Substr = Substr.substr(Groups[0].size() - Groups[6].size());
300 }
301 }
302 }
303
304 if (Words.empty())
305 return Name;
306
307 std::string Fixup;
308 switch (Case) {
309 case IdentifierNamingCheck::CT_AnyCase:
310 Fixup += Name;
311 break;
312
313 case IdentifierNamingCheck::CT_LowerCase:
314 for (auto const &Word : Words) {
315 if (&Word != &Words.front())
316 Fixup += "_";
317 Fixup += Word.lower();
318 }
319 break;
320
321 case IdentifierNamingCheck::CT_UpperCase:
322 for (auto const &Word : Words) {
323 if (&Word != &Words.front())
324 Fixup += "_";
325 Fixup += Word.upper();
326 }
327 break;
328
329 case IdentifierNamingCheck::CT_CamelCase:
330 for (auto const &Word : Words) {
331 Fixup += Word.substr(0, 1).upper();
332 Fixup += Word.substr(1).lower();
333 }
334 break;
335
336 case IdentifierNamingCheck::CT_CamelBack:
337 for (auto const &Word : Words) {
338 if (&Word == &Words.front()) {
339 Fixup += Word.lower();
340 } else {
341 Fixup += Word.substr(0, 1).upper();
342 Fixup += Word.substr(1).lower();
343 }
344 }
345 break;
Kirill Bobyrev5d8f0712016-07-20 12:28:38 +0000346
347 case IdentifierNamingCheck::CT_CamelSnakeCase:
348 for (auto const &Word : Words) {
349 if (&Word != &Words.front())
350 Fixup += "_";
351 Fixup += Word.substr(0, 1).upper();
352 Fixup += Word.substr(1).lower();
353 }
354 break;
355
356 case IdentifierNamingCheck::CT_CamelSnakeBack:
357 for (auto const &Word : Words) {
358 if (&Word != &Words.front()) {
359 Fixup += "_";
360 Fixup += Word.substr(0, 1).upper();
361 } else {
362 Fixup += Word.substr(0, 1).lower();
363 }
364 Fixup += Word.substr(1).lower();
365 }
366 break;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000367 }
368
369 return Fixup;
370}
371
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000372static std::string
373fixupWithStyle(StringRef Name,
374 const IdentifierNamingCheck::NamingStyle &Style) {
Alexander Kornienkoeec01ad2017-04-26 16:39:11 +0000375 const std::string Fixed = fixupWithCase(
376 Name, Style.Case.getValueOr(IdentifierNamingCheck::CaseType::CT_AnyCase));
377 StringRef Mid = StringRef(Fixed).trim("_");
378 if (Mid.empty())
379 Mid = "_";
380 return (Style.Prefix + Mid + Style.Suffix).str();
Alexander Kornienko76c28802015-08-19 11:15:36 +0000381}
382
383static StyleKind findStyleKind(
384 const NamedDecl *D,
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000385 const std::vector<llvm::Optional<IdentifierNamingCheck::NamingStyle>>
386 &NamingStyles) {
387 if (isa<TypedefDecl>(D) && NamingStyles[SK_Typedef])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000388 return SK_Typedef;
389
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000390 if (isa<TypeAliasDecl>(D) && NamingStyles[SK_TypeAlias])
Alexander Kornienko5ae76e02016-06-07 09:11:19 +0000391 return SK_TypeAlias;
392
Alexander Kornienko76c28802015-08-19 11:15:36 +0000393 if (const auto *Decl = dyn_cast<NamespaceDecl>(D)) {
394 if (Decl->isAnonymousNamespace())
395 return SK_Invalid;
396
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000397 if (Decl->isInline() && NamingStyles[SK_InlineNamespace])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000398 return SK_InlineNamespace;
399
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000400 if (NamingStyles[SK_Namespace])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000401 return SK_Namespace;
402 }
403
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000404 if (isa<EnumDecl>(D) && NamingStyles[SK_Enum])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000405 return SK_Enum;
406
407 if (isa<EnumConstantDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000408 if (NamingStyles[SK_EnumConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000409 return SK_EnumConstant;
410
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000411 if (NamingStyles[SK_Constant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000412 return SK_Constant;
413
414 return SK_Invalid;
415 }
416
417 if (const auto *Decl = dyn_cast<CXXRecordDecl>(D)) {
418 if (Decl->isAnonymousStructOrUnion())
419 return SK_Invalid;
420
Jonathan Coe89f12c02016-11-03 13:52:09 +0000421 if (!Decl->getCanonicalDecl()->isThisDeclarationADefinition())
422 return SK_Invalid;
423
Alexander Kornienko76c28802015-08-19 11:15:36 +0000424 if (Decl->hasDefinition() && Decl->isAbstract() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000425 NamingStyles[SK_AbstractClass])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000426 return SK_AbstractClass;
427
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000428 if (Decl->isStruct() && NamingStyles[SK_Struct])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000429 return SK_Struct;
430
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000431 if (Decl->isStruct() && NamingStyles[SK_Class])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000432 return SK_Class;
433
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000434 if (Decl->isClass() && NamingStyles[SK_Class])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000435 return SK_Class;
436
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000437 if (Decl->isClass() && NamingStyles[SK_Struct])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000438 return SK_Struct;
439
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000440 if (Decl->isUnion() && NamingStyles[SK_Union])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000441 return SK_Union;
442
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000443 if (Decl->isEnum() && NamingStyles[SK_Enum])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000444 return SK_Enum;
445
446 return SK_Invalid;
447 }
448
449 if (const auto *Decl = dyn_cast<FieldDecl>(D)) {
450 QualType Type = Decl->getType();
451
452 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000453 NamingStyles[SK_ConstantMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000454 return SK_ConstantMember;
455
456 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000457 NamingStyles[SK_Constant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000458 return SK_Constant;
459
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000460 if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000461 return SK_PrivateMember;
462
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000463 if (Decl->getAccess() == AS_protected && NamingStyles[SK_ProtectedMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000464 return SK_ProtectedMember;
465
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000466 if (Decl->getAccess() == AS_public && NamingStyles[SK_PublicMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000467 return SK_PublicMember;
468
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000469 if (NamingStyles[SK_Member])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000470 return SK_Member;
471
472 return SK_Invalid;
473 }
474
475 if (const auto *Decl = dyn_cast<ParmVarDecl>(D)) {
476 QualType Type = Decl->getType();
477
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000478 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000479 return SK_ConstexprVariable;
480
481 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000482 NamingStyles[SK_ConstantParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000483 return SK_ConstantParameter;
484
485 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000486 NamingStyles[SK_Constant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000487 return SK_Constant;
488
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000489 if (Decl->isParameterPack() && NamingStyles[SK_ParameterPack])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000490 return SK_ParameterPack;
491
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000492 if (NamingStyles[SK_Parameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000493 return SK_Parameter;
494
495 return SK_Invalid;
496 }
497
498 if (const auto *Decl = dyn_cast<VarDecl>(D)) {
499 QualType Type = Decl->getType();
500
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000501 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000502 return SK_ConstexprVariable;
503
504 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000505 Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000506 return SK_ClassConstant;
507
508 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000509 Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000510 return SK_GlobalConstant;
511
512 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000513 Decl->isStaticLocal() && NamingStyles[SK_StaticConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000514 return SK_StaticConstant;
515
516 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000517 Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000518 return SK_LocalConstant;
519
520 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000521 Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000522 return SK_LocalConstant;
523
524 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000525 NamingStyles[SK_Constant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000526 return SK_Constant;
527
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 Kornienko21503902016-06-17 09:25:24 +0000684 return addUsage(Failures, IdentifierNamingCheck::NamingCheckId(
685 Decl->getLocation(), Decl->getNameAsString()),
Jason Henline481e6aa2016-10-24 17:20:32 +0000686 Range, SourceMgr);
Alexander Kornienko21503902016-06-17 09:25:24 +0000687}
688
Alexander Kornienko76c28802015-08-19 11:15:36 +0000689void IdentifierNamingCheck::check(const MatchFinder::MatchResult &Result) {
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000690 if (const auto *Decl =
691 Result.Nodes.getNodeAs<CXXConstructorDecl>("classRef")) {
692 if (Decl->isImplicit())
693 return;
694
695 addUsage(NamingCheckFailures, Decl->getParent(),
Alexander Kornienko21503902016-06-17 09:25:24 +0000696 Decl->getNameInfo().getSourceRange());
Eric Fiselier732a3e02016-11-16 21:15:58 +0000697
698 for (const auto *Init : Decl->inits()) {
699 if (!Init->isWritten() || Init->isInClassMemberInitializer())
700 continue;
701 if (const auto *FD = Init->getAnyMember())
702 addUsage(NamingCheckFailures, FD, SourceRange(Init->getMemberLocation()));
703 // Note: delegating constructors and base class initializers are handled
704 // via the "typeLoc" matcher.
705 }
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000706 return;
707 }
708
709 if (const auto *Decl =
710 Result.Nodes.getNodeAs<CXXDestructorDecl>("classRef")) {
711 if (Decl->isImplicit())
712 return;
713
714 SourceRange Range = Decl->getNameInfo().getSourceRange();
715 if (Range.getBegin().isInvalid())
716 return;
717 // The first token that will be found is the ~ (or the equivalent trigraph),
718 // we want instead to replace the next token, that will be the identifier.
719 Range.setBegin(CharSourceRange::getTokenRange(Range).getEnd());
720
Alexander Kornienko21503902016-06-17 09:25:24 +0000721 addUsage(NamingCheckFailures, Decl->getParent(), Range);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000722 return;
723 }
724
725 if (const auto *Loc = Result.Nodes.getNodeAs<TypeLoc>("typeLoc")) {
726 NamedDecl *Decl = nullptr;
727 if (const auto &Ref = Loc->getAs<TagTypeLoc>()) {
728 Decl = Ref.getDecl();
729 } else if (const auto &Ref = Loc->getAs<InjectedClassNameTypeLoc>()) {
730 Decl = Ref.getDecl();
731 } else if (const auto &Ref = Loc->getAs<UnresolvedUsingTypeLoc>()) {
732 Decl = Ref.getDecl();
733 } else if (const auto &Ref = Loc->getAs<TemplateTypeParmTypeLoc>()) {
734 Decl = Ref.getDecl();
735 }
736
737 if (Decl) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000738 addUsage(NamingCheckFailures, Decl, Loc->getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000739 return;
740 }
741
742 if (const auto &Ref = Loc->getAs<TemplateSpecializationTypeLoc>()) {
743 const auto *Decl =
744 Ref.getTypePtr()->getTemplateName().getAsTemplateDecl();
745
746 SourceRange Range(Ref.getTemplateNameLoc(), Ref.getTemplateNameLoc());
747 if (const auto *ClassDecl = dyn_cast<TemplateDecl>(Decl)) {
Matthias Gehrea9e812b2016-07-09 20:09:28 +0000748 if (const auto *TemplDecl = ClassDecl->getTemplatedDecl())
749 addUsage(NamingCheckFailures, TemplDecl, Range);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000750 return;
751 }
752 }
753
754 if (const auto &Ref =
755 Loc->getAs<DependentTemplateSpecializationTypeLoc>()) {
Matthias Gehrea9e812b2016-07-09 20:09:28 +0000756 if (const auto *Decl = Ref.getTypePtr()->getAsTagDecl())
757 addUsage(NamingCheckFailures, Decl, Loc->getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000758 return;
759 }
760 }
761
762 if (const auto *Loc =
763 Result.Nodes.getNodeAs<NestedNameSpecifierLoc>("nestedNameLoc")) {
764 if (NestedNameSpecifier *Spec = Loc->getNestedNameSpecifier()) {
765 if (NamespaceDecl *Decl = Spec->getAsNamespace()) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000766 addUsage(NamingCheckFailures, Decl, Loc->getLocalSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000767 return;
768 }
769 }
770 }
771
772 if (const auto *Decl = Result.Nodes.getNodeAs<UsingDecl>("using")) {
773 for (const auto &Shadow : Decl->shadows()) {
774 addUsage(NamingCheckFailures, Shadow->getTargetDecl(),
Alexander Kornienko21503902016-06-17 09:25:24 +0000775 Decl->getNameInfo().getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000776 }
777 return;
778 }
779
780 if (const auto *DeclRef = Result.Nodes.getNodeAs<DeclRefExpr>("declRef")) {
Alexander Kornienko76c28802015-08-19 11:15:36 +0000781 SourceRange Range = DeclRef->getNameInfo().getSourceRange();
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000782 addUsage(NamingCheckFailures, DeclRef->getDecl(), Range,
783 Result.SourceManager);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000784 return;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000785 }
786
787 if (const auto *Decl = Result.Nodes.getNodeAs<NamedDecl>("decl")) {
788 if (!Decl->getIdentifier() || Decl->getName().empty() || Decl->isImplicit())
789 return;
790
Alexander Kornienko21503902016-06-17 09:25:24 +0000791 // Fix type aliases in value declarations
792 if (const auto *Value = Result.Nodes.getNodeAs<ValueDecl>("decl")) {
793 if (const auto *Typedef =
794 Value->getType().getTypePtr()->getAs<TypedefType>()) {
795 addUsage(NamingCheckFailures, Typedef->getDecl(),
796 Value->getSourceRange());
797 }
798 }
799
800 // Fix type aliases in function declarations
801 if (const auto *Value = Result.Nodes.getNodeAs<FunctionDecl>("decl")) {
802 if (const auto *Typedef =
803 Value->getReturnType().getTypePtr()->getAs<TypedefType>()) {
804 addUsage(NamingCheckFailures, Typedef->getDecl(),
805 Value->getSourceRange());
806 }
807 for (unsigned i = 0; i < Value->getNumParams(); ++i) {
808 if (const auto *Typedef = Value->parameters()[i]
809 ->getType()
810 .getTypePtr()
811 ->getAs<TypedefType>()) {
812 addUsage(NamingCheckFailures, Typedef->getDecl(),
813 Value->getSourceRange());
814 }
815 }
816 }
817
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000818 // Ignore ClassTemplateSpecializationDecl which are creating duplicate
819 // replacements with CXXRecordDecl
820 if (isa<ClassTemplateSpecializationDecl>(Decl))
821 return;
822
Alexander Kornienko76c28802015-08-19 11:15:36 +0000823 StyleKind SK = findStyleKind(Decl, NamingStyles);
824 if (SK == SK_Invalid)
825 return;
826
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000827 if (!NamingStyles[SK])
828 return;
829
830 const NamingStyle &Style = *NamingStyles[SK];
Alexander Kornienko76c28802015-08-19 11:15:36 +0000831 StringRef Name = Decl->getName();
832 if (matchesStyle(Name, Style))
833 return;
834
835 std::string KindName = fixupWithCase(StyleNames[SK], CT_LowerCase);
836 std::replace(KindName.begin(), KindName.end(), '_', ' ');
837
838 std::string Fixup = fixupWithStyle(Name, Style);
839 if (StringRef(Fixup).equals(Name)) {
840 if (!IgnoreFailedSplit) {
Hans Wennborg53bd9f32016-02-29 21:17:39 +0000841 DEBUG(llvm::dbgs()
842 << Decl->getLocStart().printToString(*Result.SourceManager)
843 << llvm::format(": unable to split words for %s '%s'\n",
Mehdi Amini9ff8e872016-10-07 08:25:42 +0000844 KindName.c_str(), Name.str().c_str()));
Alexander Kornienko76c28802015-08-19 11:15:36 +0000845 }
846 } else {
Alexander Kornienko21503902016-06-17 09:25:24 +0000847 NamingCheckFailure &Failure = NamingCheckFailures[NamingCheckId(
848 Decl->getLocation(), Decl->getNameAsString())];
Alexander Kornienko76c28802015-08-19 11:15:36 +0000849 SourceRange Range =
850 DeclarationNameInfo(Decl->getDeclName(), Decl->getLocation())
851 .getSourceRange();
852
853 Failure.Fixup = std::move(Fixup);
854 Failure.KindName = std::move(KindName);
Alexander Kornienko21503902016-06-17 09:25:24 +0000855 addUsage(NamingCheckFailures, Decl, Range);
Alexander Kornienko76c28802015-08-19 11:15:36 +0000856 }
857 }
858}
859
Alexander Kornienko21503902016-06-17 09:25:24 +0000860void IdentifierNamingCheck::checkMacro(SourceManager &SourceMgr,
861 const Token &MacroNameTok,
862 const MacroInfo *MI) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000863 if (!NamingStyles[SK_MacroDefinition])
864 return;
865
Alexander Kornienko21503902016-06-17 09:25:24 +0000866 StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000867 const NamingStyle &Style = *NamingStyles[SK_MacroDefinition];
Alexander Kornienko21503902016-06-17 09:25:24 +0000868 if (matchesStyle(Name, Style))
869 return;
870
871 std::string KindName =
872 fixupWithCase(StyleNames[SK_MacroDefinition], CT_LowerCase);
873 std::replace(KindName.begin(), KindName.end(), '_', ' ');
874
875 std::string Fixup = fixupWithStyle(Name, Style);
876 if (StringRef(Fixup).equals(Name)) {
877 if (!IgnoreFailedSplit) {
878 DEBUG(
879 llvm::dbgs() << MacroNameTok.getLocation().printToString(SourceMgr)
880 << llvm::format(": unable to split words for %s '%s'\n",
Mehdi Amini9ff8e872016-10-07 08:25:42 +0000881 KindName.c_str(), Name.str().c_str()));
Alexander Kornienko21503902016-06-17 09:25:24 +0000882 }
883 } else {
884 NamingCheckId ID(MI->getDefinitionLoc(), Name);
885 NamingCheckFailure &Failure = NamingCheckFailures[ID];
886 SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
887
888 Failure.Fixup = std::move(Fixup);
889 Failure.KindName = std::move(KindName);
890 addUsage(NamingCheckFailures, ID, Range);
891 }
892}
893
894void IdentifierNamingCheck::expandMacro(const Token &MacroNameTok,
895 const MacroInfo *MI) {
896 StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
897 NamingCheckId ID(MI->getDefinitionLoc(), Name);
898
899 auto Failure = NamingCheckFailures.find(ID);
900 if (Failure == NamingCheckFailures.end())
901 return;
902
903 SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
904 addUsage(NamingCheckFailures, ID, Range);
905}
906
Alexander Kornienko76c28802015-08-19 11:15:36 +0000907void IdentifierNamingCheck::onEndOfTranslationUnit() {
908 for (const auto &Pair : NamingCheckFailures) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000909 const NamingCheckId &Decl = Pair.first;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000910 const NamingCheckFailure &Failure = Pair.second;
911
Alexander Kornienko3d777682015-09-28 08:59:12 +0000912 if (Failure.KindName.empty())
913 continue;
914
Alexander Kornienko76c28802015-08-19 11:15:36 +0000915 if (Failure.ShouldFix) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000916 auto Diag = diag(Decl.first, "invalid case style for %0 '%1'")
917 << Failure.KindName << Decl.second;
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000918
Alexander Kornienko3d777682015-09-28 08:59:12 +0000919 for (const auto &Loc : Failure.RawUsageLocs) {
920 // We assume that the identifier name is made of one token only. This is
921 // always the case as we ignore usages in macros that could build
922 // identifier names by combining multiple tokens.
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000923 //
924 // For destructors, we alread take care of it by remembering the
925 // location of the start of the identifier and not the start of the
926 // tilde.
927 //
928 // Other multi-token identifiers, such as operators are not checked at
929 // all.
Alexander Kornienko76c28802015-08-19 11:15:36 +0000930 Diag << FixItHint::CreateReplacement(
Alexander Kornienko3d777682015-09-28 08:59:12 +0000931 SourceRange(SourceLocation::getFromRawEncoding(Loc)),
932 Failure.Fixup);
Alexander Kornienko76c28802015-08-19 11:15:36 +0000933 }
934 }
935 }
936}
937
938} // namespace readability
939} // namespace tidy
940} // namespace clang