blob: 3502a6fdef4f5616be7c4089381b4d052fafdc06 [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 Kornienko1c657f72017-03-22 12:50:05 +0000265 if (Style.Case && !Matchers[static_cast<size_t>(*Style.Case)].match(Name))
Alexander Kornienko76c28802015-08-19 11:15:36 +0000266 Matches = false;
267
268 return Matches;
269}
270
271static std::string fixupWithCase(StringRef Name,
272 IdentifierNamingCheck::CaseType Case) {
273 static llvm::Regex Splitter(
274 "([a-z0-9A-Z]*)(_+)|([A-Z]?[a-z0-9]+)([A-Z]|$)|([A-Z]+)([A-Z]|$)");
275
276 SmallVector<StringRef, 8> Substrs;
277 Name.split(Substrs, "_", -1, false);
278
279 SmallVector<StringRef, 8> Words;
280 for (auto Substr : Substrs) {
281 while (!Substr.empty()) {
282 SmallVector<StringRef, 8> Groups;
283 if (!Splitter.match(Substr, &Groups))
284 break;
285
286 if (Groups[2].size() > 0) {
287 Words.push_back(Groups[1]);
288 Substr = Substr.substr(Groups[0].size());
289 } else if (Groups[3].size() > 0) {
290 Words.push_back(Groups[3]);
291 Substr = Substr.substr(Groups[0].size() - Groups[4].size());
292 } else if (Groups[5].size() > 0) {
293 Words.push_back(Groups[5]);
294 Substr = Substr.substr(Groups[0].size() - Groups[6].size());
295 }
296 }
297 }
298
299 if (Words.empty())
300 return Name;
301
302 std::string Fixup;
303 switch (Case) {
304 case IdentifierNamingCheck::CT_AnyCase:
305 Fixup += Name;
306 break;
307
308 case IdentifierNamingCheck::CT_LowerCase:
309 for (auto const &Word : Words) {
310 if (&Word != &Words.front())
311 Fixup += "_";
312 Fixup += Word.lower();
313 }
314 break;
315
316 case IdentifierNamingCheck::CT_UpperCase:
317 for (auto const &Word : Words) {
318 if (&Word != &Words.front())
319 Fixup += "_";
320 Fixup += Word.upper();
321 }
322 break;
323
324 case IdentifierNamingCheck::CT_CamelCase:
325 for (auto const &Word : Words) {
326 Fixup += Word.substr(0, 1).upper();
327 Fixup += Word.substr(1).lower();
328 }
329 break;
330
331 case IdentifierNamingCheck::CT_CamelBack:
332 for (auto const &Word : Words) {
333 if (&Word == &Words.front()) {
334 Fixup += Word.lower();
335 } else {
336 Fixup += Word.substr(0, 1).upper();
337 Fixup += Word.substr(1).lower();
338 }
339 }
340 break;
Kirill Bobyrev5d8f0712016-07-20 12:28:38 +0000341
342 case IdentifierNamingCheck::CT_CamelSnakeCase:
343 for (auto const &Word : Words) {
344 if (&Word != &Words.front())
345 Fixup += "_";
346 Fixup += Word.substr(0, 1).upper();
347 Fixup += Word.substr(1).lower();
348 }
349 break;
350
351 case IdentifierNamingCheck::CT_CamelSnakeBack:
352 for (auto const &Word : Words) {
353 if (&Word != &Words.front()) {
354 Fixup += "_";
355 Fixup += Word.substr(0, 1).upper();
356 } else {
357 Fixup += Word.substr(0, 1).lower();
358 }
359 Fixup += Word.substr(1).lower();
360 }
361 break;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000362 }
363
364 return Fixup;
365}
366
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000367static std::string
368fixupWithStyle(StringRef Name,
369 const IdentifierNamingCheck::NamingStyle &Style) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000370 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
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000453 if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000454 return SK_PrivateMember;
455
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000456 if (Decl->getAccess() == AS_protected && NamingStyles[SK_ProtectedMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000457 return SK_ProtectedMember;
458
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000459 if (Decl->getAccess() == AS_public && NamingStyles[SK_PublicMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000460 return SK_PublicMember;
461
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000462 if (NamingStyles[SK_Member])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000463 return SK_Member;
464
465 return SK_Invalid;
466 }
467
468 if (const auto *Decl = dyn_cast<ParmVarDecl>(D)) {
469 QualType Type = Decl->getType();
470
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000471 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000472 return SK_ConstexprVariable;
473
474 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000475 NamingStyles[SK_ConstantParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000476 return SK_ConstantParameter;
477
478 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000479 NamingStyles[SK_Constant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000480 return SK_Constant;
481
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000482 if (Decl->isParameterPack() && NamingStyles[SK_ParameterPack])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000483 return SK_ParameterPack;
484
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000485 if (NamingStyles[SK_Parameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000486 return SK_Parameter;
487
488 return SK_Invalid;
489 }
490
491 if (const auto *Decl = dyn_cast<VarDecl>(D)) {
492 QualType Type = Decl->getType();
493
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000494 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000495 return SK_ConstexprVariable;
496
497 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000498 Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000499 return SK_ClassConstant;
500
501 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000502 Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000503 return SK_GlobalConstant;
504
505 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000506 Decl->isStaticLocal() && NamingStyles[SK_StaticConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000507 return SK_StaticConstant;
508
509 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000510 Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000511 return SK_LocalConstant;
512
513 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000514 Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalConstant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000515 return SK_LocalConstant;
516
517 if (!Type.isNull() && Type.isLocalConstQualified() &&
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000518 NamingStyles[SK_Constant])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000519 return SK_Constant;
520
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000521 if (Decl->isStaticDataMember() && NamingStyles[SK_ClassMember])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000522 return SK_ClassMember;
523
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000524 if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000525 return SK_GlobalVariable;
526
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000527 if (Decl->isStaticLocal() && NamingStyles[SK_StaticVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000528 return SK_StaticVariable;
529
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000530 if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000531 return SK_LocalVariable;
532
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000533 if (Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalVariable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000534 return SK_LocalVariable;
535
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000536 if (NamingStyles[SK_Variable])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000537 return SK_Variable;
538
539 return SK_Invalid;
540 }
541
542 if (const auto *Decl = dyn_cast<CXXMethodDecl>(D)) {
543 if (Decl->isMain() || !Decl->isUserProvided() ||
544 Decl->isUsualDeallocationFunction() ||
545 Decl->isCopyAssignmentOperator() || Decl->isMoveAssignmentOperator() ||
546 Decl->size_overridden_methods() > 0)
547 return SK_Invalid;
548
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000549 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000550 return SK_ConstexprMethod;
551
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000552 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprFunction])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000553 return SK_ConstexprFunction;
554
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000555 if (Decl->isStatic() && NamingStyles[SK_ClassMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000556 return SK_ClassMethod;
557
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000558 if (Decl->isVirtual() && NamingStyles[SK_VirtualMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000559 return SK_VirtualMethod;
560
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000561 if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000562 return SK_PrivateMethod;
563
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000564 if (Decl->getAccess() == AS_protected && NamingStyles[SK_ProtectedMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000565 return SK_ProtectedMethod;
566
Alexander Kornienko1c657f72017-03-22 12:50:05 +0000567 if (Decl->getAccess() == AS_public && NamingStyles[SK_PublicMethod])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000568 return SK_PublicMethod;
569
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000570 if (NamingStyles[SK_Method])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000571 return SK_Method;
572
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000573 if (NamingStyles[SK_Function])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000574 return SK_Function;
575
576 return SK_Invalid;
577 }
578
579 if (const auto *Decl = dyn_cast<FunctionDecl>(D)) {
580 if (Decl->isMain())
581 return SK_Invalid;
582
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000583 if (Decl->isConstexpr() && NamingStyles[SK_ConstexprFunction])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000584 return SK_ConstexprFunction;
585
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000586 if (Decl->isGlobal() && NamingStyles[SK_GlobalFunction])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000587 return SK_GlobalFunction;
588
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000589 if (NamingStyles[SK_Function])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000590 return SK_Function;
591 }
592
593 if (isa<TemplateTypeParmDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000594 if (NamingStyles[SK_TypeTemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000595 return SK_TypeTemplateParameter;
596
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000597 if (NamingStyles[SK_TemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000598 return SK_TemplateParameter;
599
600 return SK_Invalid;
601 }
602
603 if (isa<NonTypeTemplateParmDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000604 if (NamingStyles[SK_ValueTemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000605 return SK_ValueTemplateParameter;
606
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000607 if (NamingStyles[SK_TemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000608 return SK_TemplateParameter;
609
610 return SK_Invalid;
611 }
612
613 if (isa<TemplateTemplateParmDecl>(D)) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000614 if (NamingStyles[SK_TemplateTemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000615 return SK_TemplateTemplateParameter;
616
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000617 if (NamingStyles[SK_TemplateParameter])
Alexander Kornienko76c28802015-08-19 11:15:36 +0000618 return SK_TemplateParameter;
619
620 return SK_Invalid;
621 }
622
623 return SK_Invalid;
624}
625
Alexander Kornienko3d777682015-09-28 08:59:12 +0000626static void addUsage(IdentifierNamingCheck::NamingCheckFailureMap &Failures,
Alexander Kornienko21503902016-06-17 09:25:24 +0000627 const IdentifierNamingCheck::NamingCheckId &Decl,
Jason Henline481e6aa2016-10-24 17:20:32 +0000628 SourceRange Range, SourceManager *SourceMgr = nullptr) {
Jonathan Coe2c8592a2016-01-26 18:55:55 +0000629 // Do nothing if the provided range is invalid.
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000630 if (Range.getBegin().isInvalid() || Range.getEnd().isInvalid())
631 return;
632
Jason Henline481e6aa2016-10-24 17:20:32 +0000633 // If we have a source manager, use it to convert to the spelling location for
634 // performing the fix. This is necessary because macros can map the same
635 // spelling location to different source locations, and we only want to fix
636 // the token once, before it is expanded by the macro.
637 SourceLocation FixLocation = Range.getBegin();
638 if (SourceMgr)
639 FixLocation = SourceMgr->getSpellingLoc(FixLocation);
640 if (FixLocation.isInvalid())
641 return;
642
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000643 // Try to insert the identifier location in the Usages map, and bail out if it
644 // is already in there
Alexander Kornienko3d777682015-09-28 08:59:12 +0000645 auto &Failure = Failures[Decl];
Jason Henline481e6aa2016-10-24 17:20:32 +0000646 if (!Failure.RawUsageLocs.insert(FixLocation.getRawEncoding()).second)
Alexander Kornienko3d777682015-09-28 08:59:12 +0000647 return;
648
Jason Henline481e6aa2016-10-24 17:20:32 +0000649 if (!Failure.ShouldFix)
650 return;
651
652 // Check if the range is entirely contained within a macro argument.
653 SourceLocation MacroArgExpansionStartForRangeBegin;
654 SourceLocation MacroArgExpansionStartForRangeEnd;
655 bool RangeIsEntirelyWithinMacroArgument =
656 SourceMgr &&
657 SourceMgr->isMacroArgExpansion(Range.getBegin(),
658 &MacroArgExpansionStartForRangeBegin) &&
659 SourceMgr->isMacroArgExpansion(Range.getEnd(),
660 &MacroArgExpansionStartForRangeEnd) &&
661 MacroArgExpansionStartForRangeBegin == MacroArgExpansionStartForRangeEnd;
662
663 // Check if the range contains any locations from a macro expansion.
664 bool RangeContainsMacroExpansion = RangeIsEntirelyWithinMacroArgument ||
665 Range.getBegin().isMacroID() ||
666 Range.getEnd().isMacroID();
667
668 bool RangeCanBeFixed =
669 RangeIsEntirelyWithinMacroArgument || !RangeContainsMacroExpansion;
670 Failure.ShouldFix = RangeCanBeFixed;
Alexander Kornienko3d777682015-09-28 08:59:12 +0000671}
672
Alexander Kornienko21503902016-06-17 09:25:24 +0000673/// Convenience method when the usage to be added is a NamedDecl
674static void addUsage(IdentifierNamingCheck::NamingCheckFailureMap &Failures,
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000675 const NamedDecl *Decl, SourceRange Range,
676 SourceManager *SourceMgr = nullptr) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000677 return addUsage(Failures, IdentifierNamingCheck::NamingCheckId(
678 Decl->getLocation(), Decl->getNameAsString()),
Jason Henline481e6aa2016-10-24 17:20:32 +0000679 Range, SourceMgr);
Alexander Kornienko21503902016-06-17 09:25:24 +0000680}
681
Alexander Kornienko76c28802015-08-19 11:15:36 +0000682void IdentifierNamingCheck::check(const MatchFinder::MatchResult &Result) {
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000683 if (const auto *Decl =
684 Result.Nodes.getNodeAs<CXXConstructorDecl>("classRef")) {
685 if (Decl->isImplicit())
686 return;
687
688 addUsage(NamingCheckFailures, Decl->getParent(),
Alexander Kornienko21503902016-06-17 09:25:24 +0000689 Decl->getNameInfo().getSourceRange());
Eric Fiselier732a3e02016-11-16 21:15:58 +0000690
691 for (const auto *Init : Decl->inits()) {
692 if (!Init->isWritten() || Init->isInClassMemberInitializer())
693 continue;
694 if (const auto *FD = Init->getAnyMember())
695 addUsage(NamingCheckFailures, FD, SourceRange(Init->getMemberLocation()));
696 // Note: delegating constructors and base class initializers are handled
697 // via the "typeLoc" matcher.
698 }
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000699 return;
700 }
701
702 if (const auto *Decl =
703 Result.Nodes.getNodeAs<CXXDestructorDecl>("classRef")) {
704 if (Decl->isImplicit())
705 return;
706
707 SourceRange Range = Decl->getNameInfo().getSourceRange();
708 if (Range.getBegin().isInvalid())
709 return;
710 // The first token that will be found is the ~ (or the equivalent trigraph),
711 // we want instead to replace the next token, that will be the identifier.
712 Range.setBegin(CharSourceRange::getTokenRange(Range).getEnd());
713
Alexander Kornienko21503902016-06-17 09:25:24 +0000714 addUsage(NamingCheckFailures, Decl->getParent(), Range);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000715 return;
716 }
717
718 if (const auto *Loc = Result.Nodes.getNodeAs<TypeLoc>("typeLoc")) {
719 NamedDecl *Decl = nullptr;
720 if (const auto &Ref = Loc->getAs<TagTypeLoc>()) {
721 Decl = Ref.getDecl();
722 } else if (const auto &Ref = Loc->getAs<InjectedClassNameTypeLoc>()) {
723 Decl = Ref.getDecl();
724 } else if (const auto &Ref = Loc->getAs<UnresolvedUsingTypeLoc>()) {
725 Decl = Ref.getDecl();
726 } else if (const auto &Ref = Loc->getAs<TemplateTypeParmTypeLoc>()) {
727 Decl = Ref.getDecl();
728 }
729
730 if (Decl) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000731 addUsage(NamingCheckFailures, Decl, Loc->getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000732 return;
733 }
734
735 if (const auto &Ref = Loc->getAs<TemplateSpecializationTypeLoc>()) {
736 const auto *Decl =
737 Ref.getTypePtr()->getTemplateName().getAsTemplateDecl();
738
739 SourceRange Range(Ref.getTemplateNameLoc(), Ref.getTemplateNameLoc());
740 if (const auto *ClassDecl = dyn_cast<TemplateDecl>(Decl)) {
Matthias Gehrea9e812b2016-07-09 20:09:28 +0000741 if (const auto *TemplDecl = ClassDecl->getTemplatedDecl())
742 addUsage(NamingCheckFailures, TemplDecl, Range);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000743 return;
744 }
745 }
746
747 if (const auto &Ref =
748 Loc->getAs<DependentTemplateSpecializationTypeLoc>()) {
Matthias Gehrea9e812b2016-07-09 20:09:28 +0000749 if (const auto *Decl = Ref.getTypePtr()->getAsTagDecl())
750 addUsage(NamingCheckFailures, Decl, Loc->getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000751 return;
752 }
753 }
754
755 if (const auto *Loc =
756 Result.Nodes.getNodeAs<NestedNameSpecifierLoc>("nestedNameLoc")) {
757 if (NestedNameSpecifier *Spec = Loc->getNestedNameSpecifier()) {
758 if (NamespaceDecl *Decl = Spec->getAsNamespace()) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000759 addUsage(NamingCheckFailures, Decl, Loc->getLocalSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000760 return;
761 }
762 }
763 }
764
765 if (const auto *Decl = Result.Nodes.getNodeAs<UsingDecl>("using")) {
766 for (const auto &Shadow : Decl->shadows()) {
767 addUsage(NamingCheckFailures, Shadow->getTargetDecl(),
Alexander Kornienko21503902016-06-17 09:25:24 +0000768 Decl->getNameInfo().getSourceRange());
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000769 }
770 return;
771 }
772
773 if (const auto *DeclRef = Result.Nodes.getNodeAs<DeclRefExpr>("declRef")) {
Alexander Kornienko76c28802015-08-19 11:15:36 +0000774 SourceRange Range = DeclRef->getNameInfo().getSourceRange();
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000775 addUsage(NamingCheckFailures, DeclRef->getDecl(), Range,
776 Result.SourceManager);
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000777 return;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000778 }
779
780 if (const auto *Decl = Result.Nodes.getNodeAs<NamedDecl>("decl")) {
781 if (!Decl->getIdentifier() || Decl->getName().empty() || Decl->isImplicit())
782 return;
783
Alexander Kornienko21503902016-06-17 09:25:24 +0000784 // Fix type aliases in value declarations
785 if (const auto *Value = Result.Nodes.getNodeAs<ValueDecl>("decl")) {
786 if (const auto *Typedef =
787 Value->getType().getTypePtr()->getAs<TypedefType>()) {
788 addUsage(NamingCheckFailures, Typedef->getDecl(),
789 Value->getSourceRange());
790 }
791 }
792
793 // Fix type aliases in function declarations
794 if (const auto *Value = Result.Nodes.getNodeAs<FunctionDecl>("decl")) {
795 if (const auto *Typedef =
796 Value->getReturnType().getTypePtr()->getAs<TypedefType>()) {
797 addUsage(NamingCheckFailures, Typedef->getDecl(),
798 Value->getSourceRange());
799 }
800 for (unsigned i = 0; i < Value->getNumParams(); ++i) {
801 if (const auto *Typedef = Value->parameters()[i]
802 ->getType()
803 .getTypePtr()
804 ->getAs<TypedefType>()) {
805 addUsage(NamingCheckFailures, Typedef->getDecl(),
806 Value->getSourceRange());
807 }
808 }
809 }
810
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000811 // Ignore ClassTemplateSpecializationDecl which are creating duplicate
812 // replacements with CXXRecordDecl
813 if (isa<ClassTemplateSpecializationDecl>(Decl))
814 return;
815
Alexander Kornienko76c28802015-08-19 11:15:36 +0000816 StyleKind SK = findStyleKind(Decl, NamingStyles);
817 if (SK == SK_Invalid)
818 return;
819
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000820 if (!NamingStyles[SK])
821 return;
822
823 const NamingStyle &Style = *NamingStyles[SK];
Alexander Kornienko76c28802015-08-19 11:15:36 +0000824 StringRef Name = Decl->getName();
825 if (matchesStyle(Name, Style))
826 return;
827
828 std::string KindName = fixupWithCase(StyleNames[SK], CT_LowerCase);
829 std::replace(KindName.begin(), KindName.end(), '_', ' ');
830
831 std::string Fixup = fixupWithStyle(Name, Style);
832 if (StringRef(Fixup).equals(Name)) {
833 if (!IgnoreFailedSplit) {
Hans Wennborg53bd9f32016-02-29 21:17:39 +0000834 DEBUG(llvm::dbgs()
835 << Decl->getLocStart().printToString(*Result.SourceManager)
836 << llvm::format(": unable to split words for %s '%s'\n",
Mehdi Amini9ff8e872016-10-07 08:25:42 +0000837 KindName.c_str(), Name.str().c_str()));
Alexander Kornienko76c28802015-08-19 11:15:36 +0000838 }
839 } else {
Alexander Kornienko21503902016-06-17 09:25:24 +0000840 NamingCheckFailure &Failure = NamingCheckFailures[NamingCheckId(
841 Decl->getLocation(), Decl->getNameAsString())];
Alexander Kornienko76c28802015-08-19 11:15:36 +0000842 SourceRange Range =
843 DeclarationNameInfo(Decl->getDeclName(), Decl->getLocation())
844 .getSourceRange();
845
846 Failure.Fixup = std::move(Fixup);
847 Failure.KindName = std::move(KindName);
Alexander Kornienko21503902016-06-17 09:25:24 +0000848 addUsage(NamingCheckFailures, Decl, Range);
Alexander Kornienko76c28802015-08-19 11:15:36 +0000849 }
850 }
851}
852
Alexander Kornienko21503902016-06-17 09:25:24 +0000853void IdentifierNamingCheck::checkMacro(SourceManager &SourceMgr,
854 const Token &MacroNameTok,
855 const MacroInfo *MI) {
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000856 if (!NamingStyles[SK_MacroDefinition])
857 return;
858
Alexander Kornienko21503902016-06-17 09:25:24 +0000859 StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
Alexander Kornienkoee9247e2017-03-22 12:49:58 +0000860 const NamingStyle &Style = *NamingStyles[SK_MacroDefinition];
Alexander Kornienko21503902016-06-17 09:25:24 +0000861 if (matchesStyle(Name, Style))
862 return;
863
864 std::string KindName =
865 fixupWithCase(StyleNames[SK_MacroDefinition], CT_LowerCase);
866 std::replace(KindName.begin(), KindName.end(), '_', ' ');
867
868 std::string Fixup = fixupWithStyle(Name, Style);
869 if (StringRef(Fixup).equals(Name)) {
870 if (!IgnoreFailedSplit) {
871 DEBUG(
872 llvm::dbgs() << MacroNameTok.getLocation().printToString(SourceMgr)
873 << llvm::format(": unable to split words for %s '%s'\n",
Mehdi Amini9ff8e872016-10-07 08:25:42 +0000874 KindName.c_str(), Name.str().c_str()));
Alexander Kornienko21503902016-06-17 09:25:24 +0000875 }
876 } else {
877 NamingCheckId ID(MI->getDefinitionLoc(), Name);
878 NamingCheckFailure &Failure = NamingCheckFailures[ID];
879 SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
880
881 Failure.Fixup = std::move(Fixup);
882 Failure.KindName = std::move(KindName);
883 addUsage(NamingCheckFailures, ID, Range);
884 }
885}
886
887void IdentifierNamingCheck::expandMacro(const Token &MacroNameTok,
888 const MacroInfo *MI) {
889 StringRef Name = MacroNameTok.getIdentifierInfo()->getName();
890 NamingCheckId ID(MI->getDefinitionLoc(), Name);
891
892 auto Failure = NamingCheckFailures.find(ID);
893 if (Failure == NamingCheckFailures.end())
894 return;
895
896 SourceRange Range(MacroNameTok.getLocation(), MacroNameTok.getEndLoc());
897 addUsage(NamingCheckFailures, ID, Range);
898}
899
Alexander Kornienko76c28802015-08-19 11:15:36 +0000900void IdentifierNamingCheck::onEndOfTranslationUnit() {
901 for (const auto &Pair : NamingCheckFailures) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000902 const NamingCheckId &Decl = Pair.first;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000903 const NamingCheckFailure &Failure = Pair.second;
904
Alexander Kornienko3d777682015-09-28 08:59:12 +0000905 if (Failure.KindName.empty())
906 continue;
907
Alexander Kornienko76c28802015-08-19 11:15:36 +0000908 if (Failure.ShouldFix) {
Alexander Kornienko21503902016-06-17 09:25:24 +0000909 auto Diag = diag(Decl.first, "invalid case style for %0 '%1'")
910 << Failure.KindName << Decl.second;
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000911
Alexander Kornienko3d777682015-09-28 08:59:12 +0000912 for (const auto &Loc : Failure.RawUsageLocs) {
913 // We assume that the identifier name is made of one token only. This is
914 // always the case as we ignore usages in macros that could build
915 // identifier names by combining multiple tokens.
Alexander Kornienko30c423b2015-10-01 09:19:40 +0000916 //
917 // For destructors, we alread take care of it by remembering the
918 // location of the start of the identifier and not the start of the
919 // tilde.
920 //
921 // Other multi-token identifiers, such as operators are not checked at
922 // all.
Alexander Kornienko76c28802015-08-19 11:15:36 +0000923 Diag << FixItHint::CreateReplacement(
Alexander Kornienko3d777682015-09-28 08:59:12 +0000924 SourceRange(SourceLocation::getFromRawEncoding(Loc)),
925 Failure.Fixup);
Alexander Kornienko76c28802015-08-19 11:15:36 +0000926 }
927 }
928 }
929}
930
931} // namespace readability
932} // namespace tidy
933} // namespace clang