blob: 70b03b8223494bf09960e7ff59c9967094f80a1c [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 Kornienkoee9247e2017-03-22 12:49:58 +0000161 return llvm::StringSwitch<llvm::Optional<CaseType> >(Str)
162 .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 Kornienkoee9247e2017-03-22 12:49:58 +0000265 if (Style.Case &&
266 !Matchers[static_cast<size_t>(*Style.Case)].match(Name))
Alexander Kornienko76c28802015-08-19 11:15:36 +0000267 Matches = false;
268
269 return Matches;
270}
271
272static std::string fixupWithCase(StringRef Name,
273 IdentifierNamingCheck::CaseType Case) {
274 static llvm::Regex Splitter(
275 "([a-z0-9A-Z]*)(_+)|([A-Z]?[a-z0-9]+)([A-Z]|$)|([A-Z]+)([A-Z]|$)");
276
277 SmallVector<StringRef, 8> Substrs;
278 Name.split(Substrs, "_", -1, false);
279
280 SmallVector<StringRef, 8> Words;
281 for (auto Substr : Substrs) {
282 while (!Substr.empty()) {
283 SmallVector<StringRef, 8> Groups;
284 if (!Splitter.match(Substr, &Groups))
285 break;
286
287 if (Groups[2].size() > 0) {
288 Words.push_back(Groups[1]);
289 Substr = Substr.substr(Groups[0].size());
290 } else if (Groups[3].size() > 0) {
291 Words.push_back(Groups[3]);
292 Substr = Substr.substr(Groups[0].size() - Groups[4].size());
293 } else if (Groups[5].size() > 0) {
294 Words.push_back(Groups[5]);
295 Substr = Substr.substr(Groups[0].size() - Groups[6].size());
296 }
297 }
298 }
299
300 if (Words.empty())
301 return Name;
302
303 std::string Fixup;
304 switch (Case) {
305 case IdentifierNamingCheck::CT_AnyCase:
306 Fixup += Name;
307 break;
308
309 case IdentifierNamingCheck::CT_LowerCase:
310 for (auto const &Word : Words) {
311 if (&Word != &Words.front())
312 Fixup += "_";
313 Fixup += Word.lower();
314 }
315 break;
316
317 case IdentifierNamingCheck::CT_UpperCase:
318 for (auto const &Word : Words) {
319 if (&Word != &Words.front())
320 Fixup += "_";
321 Fixup += Word.upper();
322 }
323 break;
324
325 case IdentifierNamingCheck::CT_CamelCase:
326 for (auto const &Word : Words) {
327 Fixup += Word.substr(0, 1).upper();
328 Fixup += Word.substr(1).lower();
329 }
330 break;
331
332 case IdentifierNamingCheck::CT_CamelBack:
333 for (auto const &Word : Words) {
334 if (&Word == &Words.front()) {
335 Fixup += Word.lower();
336 } else {
337 Fixup += Word.substr(0, 1).upper();
338 Fixup += Word.substr(1).lower();
339 }
340 }
341 break;
Kirill Bobyrev5d8f0712016-07-20 12:28:38 +0000342
343 case IdentifierNamingCheck::CT_CamelSnakeCase:
344 for (auto const &Word : Words) {
345 if (&Word != &Words.front())
346 Fixup += "_";
347 Fixup += Word.substr(0, 1).upper();
348 Fixup += Word.substr(1).lower();
349 }
350 break;
351
352 case IdentifierNamingCheck::CT_CamelSnakeBack:
353 for (auto const &Word : Words) {
354 if (&Word != &Words.front()) {
355 Fixup += "_";
356 Fixup += Word.substr(0, 1).upper();
357 } else {
358 Fixup += Word.substr(0, 1).lower();
359 }
360 Fixup += Word.substr(1).lower();
361 }
362 break;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000363 }
364
365 return Fixup;
366}
367
368static std::string fixupWithStyle(StringRef Name,
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000369 const IdentifierNamingCheck::NamingStyle &Style) {
370 return Style.Prefix +
371 fixupWithCase(Name, Style.Case.getValueOr(
372 IdentifierNamingCheck::CaseType::CT_AnyCase)) +
373 Style.Suffix;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000374}
375
376static StyleKind findStyleKind(
377 const NamedDecl *D,
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000378 const std::vector<llvm::Optional<IdentifierNamingCheck::NamingStyle>>
379 &NamingStyles) {
380 if (isa<TypedefDecl>(D) && NamingStyles[SK_Typedef])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000381 return SK_Typedef;
382
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000383 if (isa<TypeAliasDecl>(D) && NamingStyles[SK_TypeAlias])
Alexander Kornienko5ae76e02016-06-07 09:11:19 +0000384 return SK_TypeAlias;
385
Alexander Kornienko76c28802015-08-19 11:15:36 +0000386 if (const auto *Decl = dyn_cast<NamespaceDecl>(D)) {
387 if (Decl->isAnonymousNamespace())
388 return SK_Invalid;
389
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000390 if (Decl->isInline() && NamingStyles[SK_InlineNamespace])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000391 return SK_InlineNamespace;
392
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000393 if (NamingStyles[SK_Namespace])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000394 return SK_Namespace;
395 }
396
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000397 if (isa<EnumDecl>(D) && NamingStyles[SK_Enum])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000398 return SK_Enum;
399
400 if (isa<EnumConstantDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000401 if (NamingStyles[SK_EnumConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000402 return SK_EnumConstant;
403
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000404 if (NamingStyles[SK_Constant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000405 return SK_Constant;
406
407 return SK_Invalid;
408 }
409
410 if (const auto *Decl = dyn_cast<CXXRecordDecl>(D)) {
411 if (Decl->isAnonymousStructOrUnion())
412 return SK_Invalid;
413
Jonathan Coe89f12c02016-11-03 13:52:09 +0000414 if (!Decl->getCanonicalDecl()->isThisDeclarationADefinition())
415 return SK_Invalid;
416
Alexander Kornienko76c28802015-08-19 11:15:36 +0000417 if (Decl->hasDefinition() && Decl->isAbstract() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000418 NamingStyles[SK_AbstractClass])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000419 return SK_AbstractClass;
420
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000421 if (Decl->isStruct() && NamingStyles[SK_Struct])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000422 return SK_Struct;
423
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000424 if (Decl->isStruct() && NamingStyles[SK_Class])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000425 return SK_Class;
426
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000427 if (Decl->isClass() && NamingStyles[SK_Class])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000428 return SK_Class;
429
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000430 if (Decl->isClass() && NamingStyles[SK_Struct])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000431 return SK_Struct;
432
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000433 if (Decl->isUnion() && NamingStyles[SK_Union])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000434 return SK_Union;
435
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000436 if (Decl->isEnum() && NamingStyles[SK_Enum])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000437 return SK_Enum;
438
439 return SK_Invalid;
440 }
441
442 if (const auto *Decl = dyn_cast<FieldDecl>(D)) {
443 QualType Type = Decl->getType();
444
445 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000446 NamingStyles[SK_ConstantMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000447 return SK_ConstantMember;
448
449 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000450 NamingStyles[SK_Constant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000451 return SK_Constant;
452
453 if (Decl->getAccess() == AS_private &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000454 NamingStyles[SK_PrivateMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000455 return SK_PrivateMember;
456
457 if (Decl->getAccess() == AS_protected &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000458 NamingStyles[SK_ProtectedMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000459 return SK_ProtectedMember;
460
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000461 if (Decl->getAccess() == AS_public &&
462 NamingStyles[SK_PublicMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000463 return SK_PublicMember;
464
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000465 if (NamingStyles[SK_Member])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000466 return SK_Member;
467
468 return SK_Invalid;
469 }
470
471 if (const auto *Decl = dyn_cast<ParmVarDecl>(D)) {
472 QualType Type = Decl->getType();
473
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000474 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000475 return SK_ConstexprVariable;
476
477 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000478 NamingStyles[SK_ConstantParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000479 return SK_ConstantParameter;
480
481 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000482 NamingStyles[SK_Constant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000483 return SK_Constant;
484
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000485 if (Decl->isParameterPack() && NamingStyles[SK_ParameterPack])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000486 return SK_ParameterPack;
487
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000488 if (NamingStyles[SK_Parameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000489 return SK_Parameter;
490
491 return SK_Invalid;
492 }
493
494 if (const auto *Decl = dyn_cast<VarDecl>(D)) {
495 QualType Type = Decl->getType();
496
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000497 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000498 return SK_ConstexprVariable;
499
500 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000501 Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000502 return SK_ClassConstant;
503
504 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000505 Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000506 return SK_GlobalConstant;
507
508 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000509 Decl->isStaticLocal() && NamingStyles[SK_StaticConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000510 return SK_StaticConstant;
511
512 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000513 Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000514 return SK_LocalConstant;
515
516 if (!Type.isNull() && Type.isLocalConstQualified() &&
517 Decl->isFunctionOrMethodVarDecl() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000518 NamingStyles[SK_LocalConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000519 return SK_LocalConstant;
520
521 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000522 NamingStyles[SK_Constant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000523 return SK_Constant;
524
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000525 if (Decl->isStaticDataMember() && NamingStyles[SK_ClassMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000526 return SK_ClassMember;
527
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000528 if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000529 return SK_GlobalVariable;
530
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000531 if (Decl->isStaticLocal() && NamingStyles[SK_StaticVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000532 return SK_StaticVariable;
533
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000534 if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000535 return SK_LocalVariable;
536
537 if (Decl->isFunctionOrMethodVarDecl() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000538 NamingStyles[SK_LocalVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000539 return SK_LocalVariable;
540
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000541 if (NamingStyles[SK_Variable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000542 return SK_Variable;
543
544 return SK_Invalid;
545 }
546
547 if (const auto *Decl = dyn_cast<CXXMethodDecl>(D)) {
548 if (Decl->isMain() || !Decl->isUserProvided() ||
549 Decl->isUsualDeallocationFunction() ||
550 Decl->isCopyAssignmentOperator() || Decl->isMoveAssignmentOperator() ||
551 Decl->size_overridden_methods() > 0)
552 return SK_Invalid;
553
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000554 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000555 return SK_ConstexprMethod;
556
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000557 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprFunction])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000558 return SK_ConstexprFunction;
559
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000560 if (Decl->isStatic() && NamingStyles[SK_ClassMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000561 return SK_ClassMethod;
562
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000563 if (Decl->isVirtual() && NamingStyles[SK_VirtualMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000564 return SK_VirtualMethod;
565
566 if (Decl->getAccess() == AS_private &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000567 NamingStyles[SK_PrivateMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000568 return SK_PrivateMethod;
569
570 if (Decl->getAccess() == AS_protected &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000571 NamingStyles[SK_ProtectedMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000572 return SK_ProtectedMethod;
573
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000574 if (Decl->getAccess() == AS_public &&
575 NamingStyles[SK_PublicMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000576 return SK_PublicMethod;
577
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000578 if (NamingStyles[SK_Method])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000579 return SK_Method;
580
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000581 if (NamingStyles[SK_Function])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000582 return SK_Function;
583
584 return SK_Invalid;
585 }
586
587 if (const auto *Decl = dyn_cast<FunctionDecl>(D)) {
588 if (Decl->isMain())
589 return SK_Invalid;
590
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000591 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprFunction])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000592 return SK_ConstexprFunction;
593
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000594 if (Decl->isGlobal() && NamingStyles[SK_GlobalFunction])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000595 return SK_GlobalFunction;
596
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000597 if (NamingStyles[SK_Function])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000598 return SK_Function;
599 }
600
601 if (isa<TemplateTypeParmDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000602 if (NamingStyles[SK_TypeTemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000603 return SK_TypeTemplateParameter;
604
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000605 if (NamingStyles[SK_TemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000606 return SK_TemplateParameter;
607
608 return SK_Invalid;
609 }
610
611 if (isa<NonTypeTemplateParmDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000612 if (NamingStyles[SK_ValueTemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000613 return SK_ValueTemplateParameter;
614
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000615 if (NamingStyles[SK_TemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000616 return SK_TemplateParameter;
617
618 return SK_Invalid;
619 }
620
621 if (isa<TemplateTemplateParmDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000622 if (NamingStyles[SK_TemplateTemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000623 return SK_TemplateTemplateParameter;
624
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000625 if (NamingStyles[SK_TemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000626 return SK_TemplateParameter;
627
628 return SK_Invalid;
629 }
630
631 return SK_Invalid;
632}
633
Alexander Kornienko3d777682015-09-28 08:59:12 +0000634static void addUsage(IdentifierNamingCheck::NamingCheckFailureMap &Failures,
Alexander Kornienko21503902016-06-17 09:25:24 +0000635 const IdentifierNamingCheck::NamingCheckId &Decl,
Jason Henline481e6aa2016-10-24 17:20:32 +0000636 SourceRange Range, SourceManager *SourceMgr = nullptr) {
Jonathan Coe2c8592a2016-01-26 18:55:55 +0000637 // Do nothing if the provided range is invalid.
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000638 if (Range.getBegin().isInvalid() || Range.getEnd().isInvalid())
639 return;
640
Jason Henline481e6aa2016-10-24 17:20:32 +0000641 // If we have a source manager, use it to convert to the spelling location for
642 // performing the fix. This is necessary because macros can map the same
643 // spelling location to different source locations, and we only want to fix
644 // the token once, before it is expanded by the macro.
645 SourceLocation FixLocation = Range.getBegin();
646 if (SourceMgr)
647 FixLocation = SourceMgr->getSpellingLoc(FixLocation);
648 if (FixLocation.isInvalid())
649 return;
650
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000651 // Try to insert the identifier location in the Usages map, and bail out if it
652 // is already in there
Alexander Kornienko3d777682015-09-28 08:59:12 +0000653 auto &Failure = Failures[Decl];
Jason Henline481e6aa2016-10-24 17:20:32 +0000654 if (!Failure.RawUsageLocs.insert(FixLocation.getRawEncoding()).second)
Alexander Kornienko3d777682015-09-28 08:59:12 +0000655 return;
656
Jason Henline481e6aa2016-10-24 17:20:32 +0000657 if (!Failure.ShouldFix)
658 return;
659
660 // Check if the range is entirely contained within a macro argument.
661 SourceLocation MacroArgExpansionStartForRangeBegin;
662 SourceLocation MacroArgExpansionStartForRangeEnd;
663 bool RangeIsEntirelyWithinMacroArgument =
664 SourceMgr &&
665 SourceMgr->isMacroArgExpansion(Range.getBegin(),
666 &MacroArgExpansionStartForRangeBegin) &&
667 SourceMgr->isMacroArgExpansion(Range.getEnd(),
668 &MacroArgExpansionStartForRangeEnd) &&
669 MacroArgExpansionStartForRangeBegin == MacroArgExpansionStartForRangeEnd;
670
671 // Check if the range contains any locations from a macro expansion.
672 bool RangeContainsMacroExpansion = RangeIsEntirelyWithinMacroArgument ||
673 Range.getBegin().isMacroID() ||
674 Range.getEnd().isMacroID();
675
676 bool RangeCanBeFixed =
677 RangeIsEntirelyWithinMacroArgument || !RangeContainsMacroExpansion;
678 Failure.ShouldFix = RangeCanBeFixed;
Alexander Kornienko3d777682015-09-28 08:59:12 +0000679}
680
Alexander Kornienko21503902016-06-17 09:25:24 +0000681/// Convenience method when the usage to be added is a NamedDecl
682static void addUsage(IdentifierNamingCheck::NamingCheckFailureMap &Failures,
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000683 const NamedDecl *Decl, SourceRange Range,
684 SourceManager *SourceMgr = nullptr) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000685 return addUsage(Failures, IdentifierNamingCheck::NamingCheckId(
686 Decl->getLocation(), 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())
703 addUsage(NamingCheckFailures, FD, SourceRange(Init->getMemberLocation()));
704 // Note: delegating constructors and base class initializers are handled
705 // via the "typeLoc" matcher.
706 }
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000707 return;
708 }
709
710 if (const auto *Decl =
711 Result.Nodes.getNodeAs<CXXDestructorDecl>("classRef")) {
712 if (Decl->isImplicit())
713 return;
714
715 SourceRange Range = Decl->getNameInfo().getSourceRange();
716 if (Range.getBegin().isInvalid())
717 return;
718 // The first token that will be found is the ~ (or the equivalent trigraph),
719 // we want instead to replace the next token, that will be the identifier.
720 Range.setBegin(CharSourceRange::getTokenRange(Range).getEnd());
721
Alexander Kornienko21503902016-06-17 09:25:24 +0000722 addUsage(NamingCheckFailures, Decl->getParent(), Range);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000723 return;
724 }
725
726 if (const auto *Loc = Result.Nodes.getNodeAs<TypeLoc>("typeLoc")) {
727 NamedDecl *Decl = nullptr;
728 if (const auto &Ref = Loc->getAs<TagTypeLoc>()) {
729 Decl = Ref.getDecl();
730 } else if (const auto &Ref = Loc->getAs<InjectedClassNameTypeLoc>()) {
731 Decl = Ref.getDecl();
732 } else if (const auto &Ref = Loc->getAs<UnresolvedUsingTypeLoc>()) {
733 Decl = Ref.getDecl();
734 } else if (const auto &Ref = Loc->getAs<TemplateTypeParmTypeLoc>()) {
735 Decl = Ref.getDecl();
736 }
737
738 if (Decl) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000739 addUsage(NamingCheckFailures, Decl, Loc->getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000740 return;
741 }
742
743 if (const auto &Ref = Loc->getAs<TemplateSpecializationTypeLoc>()) {
744 const auto *Decl =
745 Ref.getTypePtr()->getTemplateName().getAsTemplateDecl();
746
747 SourceRange Range(Ref.getTemplateNameLoc(), Ref.getTemplateNameLoc());
748 if (const auto *ClassDecl = dyn_cast<TemplateDecl>(Decl)) {
Matthias Gehrea9e812b2016-07-09 20:09:28 +0000749 if (const auto *TemplDecl = ClassDecl->getTemplatedDecl())
750 addUsage(NamingCheckFailures, TemplDecl, Range);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000751 return;
752 }
753 }
754
755 if (const auto &Ref =
756 Loc->getAs<DependentTemplateSpecializationTypeLoc>()) {
Matthias Gehrea9e812b2016-07-09 20:09:28 +0000757 if (const auto *Decl = Ref.getTypePtr()->getAsTagDecl())
758 addUsage(NamingCheckFailures, Decl, Loc->getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000759 return;
760 }
761 }
762
763 if (const auto *Loc =
764 Result.Nodes.getNodeAs<NestedNameSpecifierLoc>("nestedNameLoc")) {
765 if (NestedNameSpecifier *Spec = Loc->getNestedNameSpecifier()) {
766 if (NamespaceDecl *Decl = Spec->getAsNamespace()) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000767 addUsage(NamingCheckFailures, Decl, Loc->getLocalSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000768 return;
769 }
770 }
771 }
772
773 if (const auto *Decl = Result.Nodes.getNodeAs<UsingDecl>("using")) {
774 for (const auto &Shadow : Decl->shadows()) {
775 addUsage(NamingCheckFailures, Shadow->getTargetDecl(),
Alexander Kornienko21503902016-06-17 09:25:24 +0000776 Decl->getNameInfo().getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000777 }
778 return;
779 }
780
781 if (const auto *DeclRef = Result.Nodes.getNodeAs<DeclRefExpr>("declRef")) {
Alexander Kornienko76c28802015-08-19 11:15:36 +0000782 SourceRange Range = DeclRef->getNameInfo().getSourceRange();
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000783 addUsage(NamingCheckFailures, DeclRef->getDecl(), Range,
784 Result.SourceManager);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000785 return;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000786 }
787
788 if (const auto *Decl = Result.Nodes.getNodeAs<NamedDecl>("decl")) {
789 if (!Decl->getIdentifier() || Decl->getName().empty() || Decl->isImplicit())
790 return;
791
Alexander Kornienko21503902016-06-17 09:25:24 +0000792 // Fix type aliases in value declarations
793 if (const auto *Value = Result.Nodes.getNodeAs<ValueDecl>("decl")) {
794 if (const auto *Typedef =
795 Value->getType().getTypePtr()->getAs<TypedefType>()) {
796 addUsage(NamingCheckFailures, Typedef->getDecl(),
797 Value->getSourceRange());
798 }
799 }
800
801 // Fix type aliases in function declarations
802 if (const auto *Value = Result.Nodes.getNodeAs<FunctionDecl>("decl")) {
803 if (const auto *Typedef =
804 Value->getReturnType().getTypePtr()->getAs<TypedefType>()) {
805 addUsage(NamingCheckFailures, Typedef->getDecl(),
806 Value->getSourceRange());
807 }
808 for (unsigned i = 0; i < Value->getNumParams(); ++i) {
809 if (const auto *Typedef = Value->parameters()[i]
810 ->getType()
811 .getTypePtr()
812 ->getAs<TypedefType>()) {
813 addUsage(NamingCheckFailures, Typedef->getDecl(),
814 Value->getSourceRange());
815 }
816 }
817 }
818
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000819 // Ignore ClassTemplateSpecializationDecl which are creating duplicate
820 // replacements with CXXRecordDecl
821 if (isa<ClassTemplateSpecializationDecl>(Decl))
822 return;
823
Alexander Kornienko76c28802015-08-19 11:15:36 +0000824 StyleKind SK = findStyleKind(Decl, NamingStyles);
825 if (SK == SK_Invalid)
826 return;
827
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000828 if (!NamingStyles[SK])
829 return;
830
831 const NamingStyle &Style = *NamingStyles[SK];
Alexander Kornienko76c28802015-08-19 11:15:36 +0000832 StringRef Name = Decl->getName();
833 if (matchesStyle(Name, Style))
834 return;
835
836 std::string KindName = fixupWithCase(StyleNames[SK], CT_LowerCase);
837 std::replace(KindName.begin(), KindName.end(), '_', ' ');
838
839 std::string Fixup = fixupWithStyle(Name, Style);
840 if (StringRef(Fixup).equals(Name)) {
841 if (!IgnoreFailedSplit) {
Hans Wennborg53bd9f32016-02-29 21:17:39 +0000842 DEBUG(llvm::dbgs()
843 << Decl->getLocStart().printToString(*Result.SourceManager)
844 << llvm::format(": unable to split words for %s '%s'\n",
Mehdi Amini9ff8e872016-10-07 08:25:42 +0000845 KindName.c_str(), Name.str().c_str()));
Alexander Kornienko76c28802015-08-19 11:15:36 +0000846 }
847 } else {
Alexander Kornienko21503902016-06-17 09:25:24 +0000848 NamingCheckFailure &Failure = NamingCheckFailures[NamingCheckId(
849 Decl->getLocation(), Decl->getNameAsString())];
Alexander Kornienko76c28802015-08-19 11:15:36 +0000850 SourceRange Range =
851 DeclarationNameInfo(Decl->getDeclName(), Decl->getLocation())
852 .getSourceRange();
853
854 Failure.Fixup = std::move(Fixup);
855 Failure.KindName = std::move(KindName);
Alexander Kornienko21503902016-06-17 09:25:24 +0000856 addUsage(NamingCheckFailures, Decl, Range);
Alexander Kornienko76c28802015-08-19 11:15:36 +0000857 }
858 }
859}
860
Alexander Kornienko21503902016-06-17 09:25:24 +0000861void IdentifierNamingCheck::checkMacro(SourceManager &SourceMgr,
862 const Token &MacroNameTok,
863 const MacroInfo *MI) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000864 if (!NamingStyles[SK_MacroDefinition])
865 return;
866
Alexander Kornienko21503902016-06-17 09:25:24 +0000867 StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000868 const NamingStyle &Style = *NamingStyles[SK_MacroDefinition];
Alexander Kornienko21503902016-06-17 09:25:24 +0000869 if (matchesStyle(Name, Style))
870 return;
871
872 std::string KindName =
873 fixupWithCase(StyleNames[SK_MacroDefinition], CT_LowerCase);
874 std::replace(KindName.begin(), KindName.end(), '_', ' ');
875
876 std::string Fixup = fixupWithStyle(Name, Style);
877 if (StringRef(Fixup).equals(Name)) {
878 if (!IgnoreFailedSplit) {
879 DEBUG(
880 llvm::dbgs() << MacroNameTok.getLocation().printToString(SourceMgr)
881 << llvm::format(": unable to split words for %s '%s'\n",
Mehdi Amini9ff8e872016-10-07 08:25:42 +0000882 KindName.c_str(), Name.str().c_str()));
Alexander Kornienko21503902016-06-17 09:25:24 +0000883 }
884 } else {
885 NamingCheckId ID(MI->getDefinitionLoc(), Name);
886 NamingCheckFailure &Failure = NamingCheckFailures[ID];
887 SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
888
889 Failure.Fixup = std::move(Fixup);
890 Failure.KindName = std::move(KindName);
891 addUsage(NamingCheckFailures, ID, Range);
892 }
893}
894
895void IdentifierNamingCheck::expandMacro(const Token &MacroNameTok,
896 const MacroInfo *MI) {
897 StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
898 NamingCheckId ID(MI->getDefinitionLoc(), Name);
899
900 auto Failure = NamingCheckFailures.find(ID);
901 if (Failure == NamingCheckFailures.end())
902 return;
903
904 SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
905 addUsage(NamingCheckFailures, ID, Range);
906}
907
Alexander Kornienko76c28802015-08-19 11:15:36 +0000908void IdentifierNamingCheck::onEndOfTranslationUnit() {
909 for (const auto &Pair : NamingCheckFailures) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000910 const NamingCheckId &Decl = Pair.first;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000911 const NamingCheckFailure &Failure = Pair.second;
912
Alexander Kornienko3d777682015-09-28 08:59:12 +0000913 if (Failure.KindName.empty())
914 continue;
915
Alexander Kornienko76c28802015-08-19 11:15:36 +0000916 if (Failure.ShouldFix) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000917 auto Diag = diag(Decl.first, "invalid case style for %0 '%1'")
918 << Failure.KindName << Decl.second;
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000919
Alexander Kornienko3d777682015-09-28 08:59:12 +0000920 for (const auto &Loc : Failure.RawUsageLocs) {
921 // We assume that the identifier name is made of one token only. This is
922 // always the case as we ignore usages in macros that could build
923 // identifier names by combining multiple tokens.
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000924 //
925 // For destructors, we alread take care of it by remembering the
926 // location of the start of the identifier and not the start of the
927 // tilde.
928 //
929 // Other multi-token identifiers, such as operators are not checked at
930 // all.
Alexander Kornienko76c28802015-08-19 11:15:36 +0000931 Diag << FixItHint::CreateReplacement(
Alexander Kornienko3d777682015-09-28 08:59:12 +0000932 SourceRange(SourceLocation::getFromRawEncoding(Loc)),
933 Failure.Fixup);
Alexander Kornienko76c28802015-08-19 11:15:36 +0000934 }
935 }
936 }
937}
938
939} // namespace readability
940} // namespace tidy
941} // namespace clang