blob: 95a71f63986f14bff3dcf17339b7a29e35e39a2e [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
Alexander Kornienkoba874922017-12-11 19:02:26 +0000452 if (!Type.isNull() && Type.isConstQualified()) {
453 if (NamingStyles[SK_ConstantMember])
454 return SK_ConstantMember;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000455
Alexander Kornienkoba874922017-12-11 19:02:26 +0000456 if (NamingStyles[SK_Constant])
457 return SK_Constant;
458 }
Alexander Kornienko76c28802015-08-19 11:15:36 +0000459
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
Alexander Kornienkoba874922017-12-11 19:02:26 +0000481 if (!Type.isNull() && Type.isConstQualified()) {
482 if (NamingStyles[SK_ConstantParameter])
483 return SK_ConstantParameter;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000484
Alexander Kornienkoba874922017-12-11 19:02:26 +0000485 if (NamingStyles[SK_Constant])
486 return SK_Constant;
487 }
Alexander Kornienko76c28802015-08-19 11:15:36 +0000488
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
Alexander Kornienkoba874922017-12-11 19:02:26 +0000504 if (!Type.isNull() && Type.isConstQualified()) {
505 if (Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant])
506 return SK_ClassConstant;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000507
Alexander Kornienkoba874922017-12-11 19:02:26 +0000508 if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant])
509 return SK_GlobalConstant;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000510
Alexander Kornienkoba874922017-12-11 19:02:26 +0000511 if (Decl->isStaticLocal() && NamingStyles[SK_StaticConstant])
512 return SK_StaticConstant;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000513
Alexander Kornienkoba874922017-12-11 19:02:26 +0000514 if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant])
515 return SK_LocalConstant;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000516
Alexander Kornienkoba874922017-12-11 19:02:26 +0000517 if (Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalConstant])
518 return SK_LocalConstant;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000519
Alexander Kornienkoba874922017-12-11 19:02:26 +0000520 if (NamingStyles[SK_Constant])
521 return SK_Constant;
522 }
Alexander Kornienko76c28802015-08-19 11:15:36 +0000523
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000524 if (Decl->isStaticDataMember() && NamingStyles[SK_ClassMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000525 return SK_ClassMember;
526
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000527 if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000528 return SK_GlobalVariable;
529
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000530 if (Decl->isStaticLocal() && NamingStyles[SK_StaticVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000531 return SK_StaticVariable;
532
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000533 if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000534 return SK_LocalVariable;
535
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000536 if (Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000537 return SK_LocalVariable;
538
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000539 if (NamingStyles[SK_Variable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000540 return SK_Variable;
541
542 return SK_Invalid;
543 }
544
545 if (const auto *Decl = dyn_cast<CXXMethodDecl>(D)) {
546 if (Decl->isMain() || !Decl->isUserProvided() ||
547 Decl->isUsualDeallocationFunction() ||
548 Decl->isCopyAssignmentOperator() || Decl->isMoveAssignmentOperator() ||
549 Decl->size_overridden_methods() > 0)
550 return SK_Invalid;
551
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000552 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000553 return SK_ConstexprMethod;
554
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000555 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprFunction])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000556 return SK_ConstexprFunction;
557
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000558 if (Decl->isStatic() && NamingStyles[SK_ClassMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000559 return SK_ClassMethod;
560
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000561 if (Decl->isVirtual() && NamingStyles[SK_VirtualMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000562 return SK_VirtualMethod;
563
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000564 if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000565 return SK_PrivateMethod;
566
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000567 if (Decl->getAccess() == AS_protected && NamingStyles[SK_ProtectedMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000568 return SK_ProtectedMethod;
569
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000570 if (Decl->getAccess() == AS_public && NamingStyles[SK_PublicMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000571 return SK_PublicMethod;
572
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000573 if (NamingStyles[SK_Method])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000574 return SK_Method;
575
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000576 if (NamingStyles[SK_Function])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000577 return SK_Function;
578
579 return SK_Invalid;
580 }
581
582 if (const auto *Decl = dyn_cast<FunctionDecl>(D)) {
583 if (Decl->isMain())
584 return SK_Invalid;
585
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000586 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprFunction])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000587 return SK_ConstexprFunction;
588
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000589 if (Decl->isGlobal() && NamingStyles[SK_GlobalFunction])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000590 return SK_GlobalFunction;
591
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000592 if (NamingStyles[SK_Function])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000593 return SK_Function;
594 }
595
596 if (isa<TemplateTypeParmDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000597 if (NamingStyles[SK_TypeTemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000598 return SK_TypeTemplateParameter;
599
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000600 if (NamingStyles[SK_TemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000601 return SK_TemplateParameter;
602
603 return SK_Invalid;
604 }
605
606 if (isa<NonTypeTemplateParmDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000607 if (NamingStyles[SK_ValueTemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000608 return SK_ValueTemplateParameter;
609
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000610 if (NamingStyles[SK_TemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000611 return SK_TemplateParameter;
612
613 return SK_Invalid;
614 }
615
616 if (isa<TemplateTemplateParmDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000617 if (NamingStyles[SK_TemplateTemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000618 return SK_TemplateTemplateParameter;
619
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000620 if (NamingStyles[SK_TemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000621 return SK_TemplateParameter;
622
623 return SK_Invalid;
624 }
625
626 return SK_Invalid;
627}
628
Alexander Kornienko3d777682015-09-28 08:59:12 +0000629static void addUsage(IdentifierNamingCheck::NamingCheckFailureMap &Failures,
Alexander Kornienko21503902016-06-17 09:25:24 +0000630 const IdentifierNamingCheck::NamingCheckId &Decl,
Jason Henline481e6aa2016-10-24 17:20:32 +0000631 SourceRange Range, SourceManager *SourceMgr = nullptr) {
Jonathan Coe2c8592a2016-01-26 18:55:55 +0000632 // Do nothing if the provided range is invalid.
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000633 if (Range.getBegin().isInvalid() || Range.getEnd().isInvalid())
634 return;
635
Jason Henline481e6aa2016-10-24 17:20:32 +0000636 // If we have a source manager, use it to convert to the spelling location for
637 // performing the fix. This is necessary because macros can map the same
638 // spelling location to different source locations, and we only want to fix
639 // the token once, before it is expanded by the macro.
640 SourceLocation FixLocation = Range.getBegin();
641 if (SourceMgr)
642 FixLocation = SourceMgr->getSpellingLoc(FixLocation);
643 if (FixLocation.isInvalid())
644 return;
645
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000646 // Try to insert the identifier location in the Usages map, and bail out if it
647 // is already in there
Alexander Kornienko3d777682015-09-28 08:59:12 +0000648 auto &Failure = Failures[Decl];
Jason Henline481e6aa2016-10-24 17:20:32 +0000649 if (!Failure.RawUsageLocs.insert(FixLocation.getRawEncoding()).second)
Alexander Kornienko3d777682015-09-28 08:59:12 +0000650 return;
651
Jason Henline481e6aa2016-10-24 17:20:32 +0000652 if (!Failure.ShouldFix)
653 return;
654
655 // Check if the range is entirely contained within a macro argument.
656 SourceLocation MacroArgExpansionStartForRangeBegin;
657 SourceLocation MacroArgExpansionStartForRangeEnd;
658 bool RangeIsEntirelyWithinMacroArgument =
659 SourceMgr &&
660 SourceMgr->isMacroArgExpansion(Range.getBegin(),
661 &MacroArgExpansionStartForRangeBegin) &&
662 SourceMgr->isMacroArgExpansion(Range.getEnd(),
663 &MacroArgExpansionStartForRangeEnd) &&
664 MacroArgExpansionStartForRangeBegin == MacroArgExpansionStartForRangeEnd;
665
666 // Check if the range contains any locations from a macro expansion.
667 bool RangeContainsMacroExpansion = RangeIsEntirelyWithinMacroArgument ||
668 Range.getBegin().isMacroID() ||
669 Range.getEnd().isMacroID();
670
671 bool RangeCanBeFixed =
672 RangeIsEntirelyWithinMacroArgument || !RangeContainsMacroExpansion;
673 Failure.ShouldFix = RangeCanBeFixed;
Alexander Kornienko3d777682015-09-28 08:59:12 +0000674}
675
Alexander Kornienko21503902016-06-17 09:25:24 +0000676/// Convenience method when the usage to be added is a NamedDecl
677static void addUsage(IdentifierNamingCheck::NamingCheckFailureMap &Failures,
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000678 const NamedDecl *Decl, SourceRange Range,
679 SourceManager *SourceMgr = nullptr) {
Alexander Kornienkoba874922017-12-11 19:02:26 +0000680 return addUsage(Failures,
681 IdentifierNamingCheck::NamingCheckId(Decl->getLocation(),
682 Decl->getNameAsString()),
Jason Henline481e6aa2016-10-24 17:20:32 +0000683 Range, SourceMgr);
Alexander Kornienko21503902016-06-17 09:25:24 +0000684}
685
Alexander Kornienko76c28802015-08-19 11:15:36 +0000686void IdentifierNamingCheck::check(const MatchFinder::MatchResult &Result) {
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000687 if (const auto *Decl =
688 Result.Nodes.getNodeAs<CXXConstructorDecl>("classRef")) {
689 if (Decl->isImplicit())
690 return;
691
692 addUsage(NamingCheckFailures, Decl->getParent(),
Alexander Kornienko21503902016-06-17 09:25:24 +0000693 Decl->getNameInfo().getSourceRange());
Eric Fiselier732a3e02016-11-16 21:15:58 +0000694
695 for (const auto *Init : Decl->inits()) {
696 if (!Init->isWritten() || Init->isInClassMemberInitializer())
697 continue;
698 if (const auto *FD = Init->getAnyMember())
Alexander Kornienkoba874922017-12-11 19:02:26 +0000699 addUsage(NamingCheckFailures, FD,
700 SourceRange(Init->getMemberLocation()));
Eric Fiselier732a3e02016-11-16 21:15:58 +0000701 // Note: delegating constructors and base class initializers are handled
702 // via the "typeLoc" matcher.
703 }
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000704 return;
705 }
706
707 if (const auto *Decl =
708 Result.Nodes.getNodeAs<CXXDestructorDecl>("classRef")) {
709 if (Decl->isImplicit())
710 return;
711
712 SourceRange Range = Decl->getNameInfo().getSourceRange();
713 if (Range.getBegin().isInvalid())
714 return;
715 // The first token that will be found is the ~ (or the equivalent trigraph),
716 // we want instead to replace the next token, that will be the identifier.
717 Range.setBegin(CharSourceRange::getTokenRange(Range).getEnd());
718
Alexander Kornienko21503902016-06-17 09:25:24 +0000719 addUsage(NamingCheckFailures, Decl->getParent(), Range);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000720 return;
721 }
722
723 if (const auto *Loc = Result.Nodes.getNodeAs<TypeLoc>("typeLoc")) {
724 NamedDecl *Decl = nullptr;
725 if (const auto &Ref = Loc->getAs<TagTypeLoc>()) {
726 Decl = Ref.getDecl();
727 } else if (const auto &Ref = Loc->getAs<InjectedClassNameTypeLoc>()) {
728 Decl = Ref.getDecl();
729 } else if (const auto &Ref = Loc->getAs<UnresolvedUsingTypeLoc>()) {
730 Decl = Ref.getDecl();
731 } else if (const auto &Ref = Loc->getAs<TemplateTypeParmTypeLoc>()) {
732 Decl = Ref.getDecl();
733 }
734
735 if (Decl) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000736 addUsage(NamingCheckFailures, Decl, Loc->getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000737 return;
738 }
739
740 if (const auto &Ref = Loc->getAs<TemplateSpecializationTypeLoc>()) {
741 const auto *Decl =
742 Ref.getTypePtr()->getTemplateName().getAsTemplateDecl();
743
744 SourceRange Range(Ref.getTemplateNameLoc(), Ref.getTemplateNameLoc());
745 if (const auto *ClassDecl = dyn_cast<TemplateDecl>(Decl)) {
Matthias Gehrea9e812b2016-07-09 20:09:28 +0000746 if (const auto *TemplDecl = ClassDecl->getTemplatedDecl())
747 addUsage(NamingCheckFailures, TemplDecl, Range);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000748 return;
749 }
750 }
751
752 if (const auto &Ref =
753 Loc->getAs<DependentTemplateSpecializationTypeLoc>()) {
Matthias Gehrea9e812b2016-07-09 20:09:28 +0000754 if (const auto *Decl = Ref.getTypePtr()->getAsTagDecl())
755 addUsage(NamingCheckFailures, Decl, Loc->getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000756 return;
757 }
758 }
759
760 if (const auto *Loc =
761 Result.Nodes.getNodeAs<NestedNameSpecifierLoc>("nestedNameLoc")) {
762 if (NestedNameSpecifier *Spec = Loc->getNestedNameSpecifier()) {
763 if (NamespaceDecl *Decl = Spec->getAsNamespace()) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000764 addUsage(NamingCheckFailures, Decl, Loc->getLocalSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000765 return;
766 }
767 }
768 }
769
770 if (const auto *Decl = Result.Nodes.getNodeAs<UsingDecl>("using")) {
771 for (const auto &Shadow : Decl->shadows()) {
772 addUsage(NamingCheckFailures, Shadow->getTargetDecl(),
Alexander Kornienko21503902016-06-17 09:25:24 +0000773 Decl->getNameInfo().getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000774 }
775 return;
776 }
777
778 if (const auto *DeclRef = Result.Nodes.getNodeAs<DeclRefExpr>("declRef")) {
Alexander Kornienko76c28802015-08-19 11:15:36 +0000779 SourceRange Range = DeclRef->getNameInfo().getSourceRange();
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000780 addUsage(NamingCheckFailures, DeclRef->getDecl(), Range,
781 Result.SourceManager);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000782 return;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000783 }
784
785 if (const auto *Decl = Result.Nodes.getNodeAs<NamedDecl>("decl")) {
786 if (!Decl->getIdentifier() || Decl->getName().empty() || Decl->isImplicit())
787 return;
788
Alexander Kornienko21503902016-06-17 09:25:24 +0000789 // Fix type aliases in value declarations
790 if (const auto *Value = Result.Nodes.getNodeAs<ValueDecl>("decl")) {
791 if (const auto *Typedef =
792 Value->getType().getTypePtr()->getAs<TypedefType>()) {
793 addUsage(NamingCheckFailures, Typedef->getDecl(),
794 Value->getSourceRange());
795 }
796 }
797
798 // Fix type aliases in function declarations
799 if (const auto *Value = Result.Nodes.getNodeAs<FunctionDecl>("decl")) {
800 if (const auto *Typedef =
801 Value->getReturnType().getTypePtr()->getAs<TypedefType>()) {
802 addUsage(NamingCheckFailures, Typedef->getDecl(),
803 Value->getSourceRange());
804 }
805 for (unsigned i = 0; i < Value->getNumParams(); ++i) {
806 if (const auto *Typedef = Value->parameters()[i]
807 ->getType()
808 .getTypePtr()
809 ->getAs<TypedefType>()) {
810 addUsage(NamingCheckFailures, Typedef->getDecl(),
811 Value->getSourceRange());
812 }
813 }
814 }
815
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000816 // Ignore ClassTemplateSpecializationDecl which are creating duplicate
817 // replacements with CXXRecordDecl
818 if (isa<ClassTemplateSpecializationDecl>(Decl))
819 return;
820
Alexander Kornienko76c28802015-08-19 11:15:36 +0000821 StyleKind SK = findStyleKind(Decl, NamingStyles);
822 if (SK == SK_Invalid)
823 return;
824
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000825 if (!NamingStyles[SK])
826 return;
827
828 const NamingStyle &Style = *NamingStyles[SK];
Alexander Kornienko76c28802015-08-19 11:15:36 +0000829 StringRef Name = Decl->getName();
830 if (matchesStyle(Name, Style))
831 return;
832
833 std::string KindName = fixupWithCase(StyleNames[SK], CT_LowerCase);
834 std::replace(KindName.begin(), KindName.end(), '_', ' ');
835
836 std::string Fixup = fixupWithStyle(Name, Style);
837 if (StringRef(Fixup).equals(Name)) {
838 if (!IgnoreFailedSplit) {
Hans Wennborg53bd9f32016-02-29 21:17:39 +0000839 DEBUG(llvm::dbgs()
840 << Decl->getLocStart().printToString(*Result.SourceManager)
841 << llvm::format(": unable to split words for %s '%s'\n",
Mehdi Amini9ff8e872016-10-07 08:25:42 +0000842 KindName.c_str(), Name.str().c_str()));
Alexander Kornienko76c28802015-08-19 11:15:36 +0000843 }
844 } else {
Alexander Kornienko21503902016-06-17 09:25:24 +0000845 NamingCheckFailure &Failure = NamingCheckFailures[NamingCheckId(
846 Decl->getLocation(), Decl->getNameAsString())];
Alexander Kornienko76c28802015-08-19 11:15:36 +0000847 SourceRange Range =
848 DeclarationNameInfo(Decl->getDeclName(), Decl->getLocation())
849 .getSourceRange();
850
851 Failure.Fixup = std::move(Fixup);
852 Failure.KindName = std::move(KindName);
Alexander Kornienko21503902016-06-17 09:25:24 +0000853 addUsage(NamingCheckFailures, Decl, Range);
Alexander Kornienko76c28802015-08-19 11:15:36 +0000854 }
855 }
856}
857
Alexander Kornienko21503902016-06-17 09:25:24 +0000858void IdentifierNamingCheck::checkMacro(SourceManager &SourceMgr,
859 const Token &MacroNameTok,
860 const MacroInfo *MI) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000861 if (!NamingStyles[SK_MacroDefinition])
862 return;
863
Alexander Kornienko21503902016-06-17 09:25:24 +0000864 StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000865 const NamingStyle &Style = *NamingStyles[SK_MacroDefinition];
Alexander Kornienko21503902016-06-17 09:25:24 +0000866 if (matchesStyle(Name, Style))
867 return;
868
869 std::string KindName =
870 fixupWithCase(StyleNames[SK_MacroDefinition], CT_LowerCase);
871 std::replace(KindName.begin(), KindName.end(), '_', ' ');
872
873 std::string Fixup = fixupWithStyle(Name, Style);
874 if (StringRef(Fixup).equals(Name)) {
875 if (!IgnoreFailedSplit) {
876 DEBUG(
877 llvm::dbgs() << MacroNameTok.getLocation().printToString(SourceMgr)
878 << llvm::format(": unable to split words for %s '%s'\n",
Mehdi Amini9ff8e872016-10-07 08:25:42 +0000879 KindName.c_str(), Name.str().c_str()));
Alexander Kornienko21503902016-06-17 09:25:24 +0000880 }
881 } else {
882 NamingCheckId ID(MI->getDefinitionLoc(), Name);
883 NamingCheckFailure &Failure = NamingCheckFailures[ID];
884 SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
885
886 Failure.Fixup = std::move(Fixup);
887 Failure.KindName = std::move(KindName);
888 addUsage(NamingCheckFailures, ID, Range);
889 }
890}
891
892void IdentifierNamingCheck::expandMacro(const Token &MacroNameTok,
893 const MacroInfo *MI) {
894 StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
895 NamingCheckId ID(MI->getDefinitionLoc(), Name);
896
897 auto Failure = NamingCheckFailures.find(ID);
898 if (Failure == NamingCheckFailures.end())
899 return;
900
901 SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
902 addUsage(NamingCheckFailures, ID, Range);
903}
904
Alexander Kornienko76c28802015-08-19 11:15:36 +0000905void IdentifierNamingCheck::onEndOfTranslationUnit() {
906 for (const auto &Pair : NamingCheckFailures) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000907 const NamingCheckId &Decl = Pair.first;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000908 const NamingCheckFailure &Failure = Pair.second;
909
Alexander Kornienko3d777682015-09-28 08:59:12 +0000910 if (Failure.KindName.empty())
911 continue;
912
Alexander Kornienko76c28802015-08-19 11:15:36 +0000913 if (Failure.ShouldFix) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000914 auto Diag = diag(Decl.first, "invalid case style for %0 '%1'")
915 << Failure.KindName << Decl.second;
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000916
Alexander Kornienko3d777682015-09-28 08:59:12 +0000917 for (const auto &Loc : Failure.RawUsageLocs) {
918 // We assume that the identifier name is made of one token only. This is
919 // always the case as we ignore usages in macros that could build
920 // identifier names by combining multiple tokens.
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000921 //
922 // For destructors, we alread take care of it by remembering the
923 // location of the start of the identifier and not the start of the
924 // tilde.
925 //
926 // Other multi-token identifiers, such as operators are not checked at
927 // all.
Alexander Kornienko76c28802015-08-19 11:15:36 +0000928 Diag << FixItHint::CreateReplacement(
Alexander Kornienko3d777682015-09-28 08:59:12 +0000929 SourceRange(SourceLocation::getFromRawEncoding(Loc)),
930 Failure.Fixup);
Alexander Kornienko76c28802015-08-19 11:15:36 +0000931 }
932 }
933 }
934}
935
936} // namespace readability
937} // namespace tidy
938} // namespace clang