IdentifierNamingCheck.cpp
22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//===--- IdentifierNamingCheck.cpp - clang-tidy ---------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "IdentifierNamingCheck.h"
#include "../GlobList.h"
#include "clang/AST/CXXInheritance.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Regex.h"
#define DEBUG_TYPE "clang-tidy"
// FixItHint
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
llvm::ArrayRef<
std::pair<readability::IdentifierNamingCheck::CaseType, StringRef>>
OptionEnumMapping<
readability::IdentifierNamingCheck::CaseType>::getEnumMapping() {
static constexpr std::pair<readability::IdentifierNamingCheck::CaseType,
StringRef>
Mapping[] = {
{readability::IdentifierNamingCheck::CT_AnyCase, "aNy_CasE"},
{readability::IdentifierNamingCheck::CT_LowerCase, "lower_case"},
{readability::IdentifierNamingCheck::CT_UpperCase, "UPPER_CASE"},
{readability::IdentifierNamingCheck::CT_CamelBack, "camelBack"},
{readability::IdentifierNamingCheck::CT_CamelCase, "CamelCase"},
{readability::IdentifierNamingCheck::CT_CamelSnakeCase,
"Camel_Snake_Case"},
{readability::IdentifierNamingCheck::CT_CamelSnakeBack,
"camel_Snake_Back"}};
return llvm::makeArrayRef(Mapping);
}
namespace readability {
// clang-format off
#define NAMING_KEYS(m) \
m(Namespace) \
m(InlineNamespace) \
m(EnumConstant) \
m(ConstexprVariable) \
m(ConstantMember) \
m(PrivateMember) \
m(ProtectedMember) \
m(PublicMember) \
m(Member) \
m(ClassConstant) \
m(ClassMember) \
m(GlobalConstant) \
m(GlobalConstantPointer) \
m(GlobalPointer) \
m(GlobalVariable) \
m(LocalConstant) \
m(LocalConstantPointer) \
m(LocalPointer) \
m(LocalVariable) \
m(StaticConstant) \
m(StaticVariable) \
m(Constant) \
m(Variable) \
m(ConstantParameter) \
m(ParameterPack) \
m(Parameter) \
m(PointerParameter) \
m(ConstantPointerParameter) \
m(AbstractClass) \
m(Struct) \
m(Class) \
m(Union) \
m(Enum) \
m(GlobalFunction) \
m(ConstexprFunction) \
m(Function) \
m(ConstexprMethod) \
m(VirtualMethod) \
m(ClassMethod) \
m(PrivateMethod) \
m(ProtectedMethod) \
m(PublicMethod) \
m(Method) \
m(Typedef) \
m(TypeTemplateParameter) \
m(ValueTemplateParameter) \
m(TemplateTemplateParameter) \
m(TemplateParameter) \
m(TypeAlias) \
m(MacroDefinition) \
m(ObjcIvar) \
enum StyleKind {
#define ENUMERATE(v) SK_ ## v,
NAMING_KEYS(ENUMERATE)
#undef ENUMERATE
SK_Count,
SK_Invalid
};
static StringRef const StyleNames[] = {
#define STRINGIZE(v) #v,
NAMING_KEYS(STRINGIZE)
#undef STRINGIZE
};
#undef NAMING_KEYS
// clang-format on
static std::vector<llvm::Optional<IdentifierNamingCheck::NamingStyle>>
getNamingStyles(const ClangTidyCheck::OptionsView &Options) {
std::vector<llvm::Optional<IdentifierNamingCheck::NamingStyle>> Styles;
Styles.reserve(array_lengthof(StyleNames));
for (auto const &StyleName : StyleNames) {
auto CaseOptional = Options.getOptional<IdentifierNamingCheck::CaseType>(
(StyleName + "Case").str());
auto Prefix = Options.get((StyleName + "Prefix").str(), "");
auto Postfix = Options.get((StyleName + "Suffix").str(), "");
if (CaseOptional || !Prefix.empty() || !Postfix.empty())
Styles.emplace_back(IdentifierNamingCheck::NamingStyle{
std::move(CaseOptional), std::move(Prefix), std::move(Postfix)});
else
Styles.emplace_back(llvm::None);
}
return Styles;
}
IdentifierNamingCheck::IdentifierNamingCheck(StringRef Name,
ClangTidyContext *Context)
: RenamerClangTidyCheck(Name, Context), Context(Context), CheckName(Name),
GetConfigPerFile(Options.get("GetConfigPerFile", true)),
IgnoreFailedSplit(Options.get("IgnoreFailedSplit", false)),
IgnoreMainLikeFunctions(Options.get("IgnoreMainLikeFunctions", false)) {
auto IterAndInserted = NamingStylesCache.try_emplace(
llvm::sys::path::parent_path(Context->getCurrentFile()),
getNamingStyles(Options));
assert(IterAndInserted.second && "Couldn't insert Style");
// Holding a reference to the data in the vector is safe as it should never
// move.
MainFileStyle = IterAndInserted.first->getValue();
}
IdentifierNamingCheck::~IdentifierNamingCheck() = default;
void IdentifierNamingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
RenamerClangTidyCheck::storeOptions(Opts);
ArrayRef<llvm::Optional<NamingStyle>> NamingStyles =
getStyleForFile(Context->getCurrentFile());
for (size_t I = 0; I < SK_Count; ++I) {
if (!NamingStyles[I])
continue;
if (NamingStyles[I]->Case)
Options.store(Opts, (StyleNames[I] + "Case").str(),
*NamingStyles[I]->Case);
Options.store(Opts, (StyleNames[I] + "Prefix").str(),
NamingStyles[I]->Prefix);
Options.store(Opts, (StyleNames[I] + "Suffix").str(),
NamingStyles[I]->Suffix);
}
Options.store(Opts, "GetConfigPerFile", GetConfigPerFile);
Options.store(Opts, "IgnoreFailedSplit", IgnoreFailedSplit);
Options.store(Opts, "IgnoreMainLikeFunctions", IgnoreMainLikeFunctions);
}
static bool matchesStyle(StringRef Name,
IdentifierNamingCheck::NamingStyle Style) {
static llvm::Regex Matchers[] = {
llvm::Regex("^.*$"),
llvm::Regex("^[a-z][a-z0-9_]*$"),
llvm::Regex("^[a-z][a-zA-Z0-9]*$"),
llvm::Regex("^[A-Z][A-Z0-9_]*$"),
llvm::Regex("^[A-Z][a-zA-Z0-9]*$"),
llvm::Regex("^[A-Z]([a-z0-9]*(_[A-Z])?)*"),
llvm::Regex("^[a-z]([a-z0-9]*(_[A-Z])?)*"),
};
if (!Name.consume_front(Style.Prefix))
return false;
if (!Name.consume_back(Style.Suffix))
return false;
// Ensure the name doesn't have any extra underscores beyond those specified
// in the prefix and suffix.
if (Name.startswith("_") || Name.endswith("_"))
return false;
if (Style.Case && !Matchers[static_cast<size_t>(*Style.Case)].match(Name))
return false;
return true;
}
static std::string fixupWithCase(StringRef Name,
IdentifierNamingCheck::CaseType Case) {
static llvm::Regex Splitter(
"([a-z0-9A-Z]*)(_+)|([A-Z]?[a-z0-9]+)([A-Z]|$)|([A-Z]+)([A-Z]|$)");
SmallVector<StringRef, 8> Substrs;
Name.split(Substrs, "_", -1, false);
SmallVector<StringRef, 8> Words;
SmallVector<StringRef, 8> Groups;
for (auto Substr : Substrs) {
while (!Substr.empty()) {
Groups.clear();
if (!Splitter.match(Substr, &Groups))
break;
if (Groups[2].size() > 0) {
Words.push_back(Groups[1]);
Substr = Substr.substr(Groups[0].size());
} else if (Groups[3].size() > 0) {
Words.push_back(Groups[3]);
Substr = Substr.substr(Groups[0].size() - Groups[4].size());
} else if (Groups[5].size() > 0) {
Words.push_back(Groups[5]);
Substr = Substr.substr(Groups[0].size() - Groups[6].size());
}
}
}
if (Words.empty())
return Name.str();
SmallString<128> Fixup;
switch (Case) {
case IdentifierNamingCheck::CT_AnyCase:
return Name.str();
break;
case IdentifierNamingCheck::CT_LowerCase:
for (auto const &Word : Words) {
if (&Word != &Words.front())
Fixup += "_";
Fixup += Word.lower();
}
break;
case IdentifierNamingCheck::CT_UpperCase:
for (auto const &Word : Words) {
if (&Word != &Words.front())
Fixup += "_";
Fixup += Word.upper();
}
break;
case IdentifierNamingCheck::CT_CamelCase:
for (auto const &Word : Words) {
Fixup += toupper(Word.front());
Fixup += Word.substr(1).lower();
}
break;
case IdentifierNamingCheck::CT_CamelBack:
for (auto const &Word : Words) {
if (&Word == &Words.front()) {
Fixup += Word.lower();
} else {
Fixup += toupper(Word.front());
Fixup += Word.substr(1).lower();
}
}
break;
case IdentifierNamingCheck::CT_CamelSnakeCase:
for (auto const &Word : Words) {
if (&Word != &Words.front())
Fixup += "_";
Fixup += toupper(Word.front());
Fixup += Word.substr(1).lower();
}
break;
case IdentifierNamingCheck::CT_CamelSnakeBack:
for (auto const &Word : Words) {
if (&Word != &Words.front()) {
Fixup += "_";
Fixup += toupper(Word.front());
} else {
Fixup += tolower(Word.front());
}
Fixup += Word.substr(1).lower();
}
break;
}
return Fixup.str().str();
}
static bool isParamInMainLikeFunction(const ParmVarDecl &ParmDecl,
bool IncludeMainLike) {
const auto *FDecl =
dyn_cast_or_null<FunctionDecl>(ParmDecl.getParentFunctionOrMethod());
if (!FDecl)
return false;
if (FDecl->isMain())
return true;
if (!IncludeMainLike)
return false;
if (FDecl->getAccess() != AS_public && FDecl->getAccess() != AS_none)
return false;
enum MainType { None, Main, WMain };
auto IsCharPtrPtr = [](QualType QType) -> MainType {
if (QType.isNull())
return None;
if (QType = QType->getPointeeType(), QType.isNull())
return None;
if (QType = QType->getPointeeType(), QType.isNull())
return None;
if (QType->isCharType())
return Main;
if (QType->isWideCharType())
return WMain;
return None;
};
auto IsIntType = [](QualType QType) {
if (QType.isNull())
return false;
if (const auto *Builtin =
dyn_cast<BuiltinType>(QType->getUnqualifiedDesugaredType())) {
return Builtin->getKind() == BuiltinType::Int;
}
return false;
};
if (!IsIntType(FDecl->getReturnType()))
return false;
if (FDecl->getNumParams() < 2 || FDecl->getNumParams() > 3)
return false;
if (!IsIntType(FDecl->parameters()[0]->getType()))
return false;
MainType Type = IsCharPtrPtr(FDecl->parameters()[1]->getType());
if (Type == None)
return false;
if (FDecl->getNumParams() == 3 &&
IsCharPtrPtr(FDecl->parameters()[2]->getType()) != Type)
return false;
if (Type == Main) {
static llvm::Regex Matcher(
"(^[Mm]ain([_A-Z]|$))|([a-z0-9_]Main([_A-Z]|$))|(_main(_|$))");
assert(Matcher.isValid() && "Invalid Matcher for main like functions.");
return Matcher.match(FDecl->getName());
} else {
static llvm::Regex Matcher("(^((W[Mm])|(wm))ain([_A-Z]|$))|([a-z0-9_]W[Mm]"
"ain([_A-Z]|$))|(_wmain(_|$))");
assert(Matcher.isValid() && "Invalid Matcher for wmain like functions.");
return Matcher.match(FDecl->getName());
}
}
static std::string
fixupWithStyle(StringRef Name,
const IdentifierNamingCheck::NamingStyle &Style) {
const std::string Fixed = fixupWithCase(
Name, Style.Case.getValueOr(IdentifierNamingCheck::CaseType::CT_AnyCase));
StringRef Mid = StringRef(Fixed).trim("_");
if (Mid.empty())
Mid = "_";
return (Style.Prefix + Mid + Style.Suffix).str();
}
static StyleKind findStyleKind(
const NamedDecl *D,
ArrayRef<llvm::Optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
bool IgnoreMainLikeFunctions) {
assert(D && D->getIdentifier() && !D->getName().empty() && !D->isImplicit() &&
"Decl must be an explicit identifier with a name.");
if (isa<ObjCIvarDecl>(D) && NamingStyles[SK_ObjcIvar])
return SK_ObjcIvar;
if (isa<TypedefDecl>(D) && NamingStyles[SK_Typedef])
return SK_Typedef;
if (isa<TypeAliasDecl>(D) && NamingStyles[SK_TypeAlias])
return SK_TypeAlias;
if (const auto *Decl = dyn_cast<NamespaceDecl>(D)) {
if (Decl->isAnonymousNamespace())
return SK_Invalid;
if (Decl->isInline() && NamingStyles[SK_InlineNamespace])
return SK_InlineNamespace;
if (NamingStyles[SK_Namespace])
return SK_Namespace;
}
if (isa<EnumDecl>(D) && NamingStyles[SK_Enum])
return SK_Enum;
if (isa<EnumConstantDecl>(D)) {
if (NamingStyles[SK_EnumConstant])
return SK_EnumConstant;
if (NamingStyles[SK_Constant])
return SK_Constant;
return SK_Invalid;
}
if (const auto *Decl = dyn_cast<CXXRecordDecl>(D)) {
if (Decl->isAnonymousStructOrUnion())
return SK_Invalid;
if (!Decl->getCanonicalDecl()->isThisDeclarationADefinition())
return SK_Invalid;
if (Decl->hasDefinition() && Decl->isAbstract() &&
NamingStyles[SK_AbstractClass])
return SK_AbstractClass;
if (Decl->isStruct() && NamingStyles[SK_Struct])
return SK_Struct;
if (Decl->isStruct() && NamingStyles[SK_Class])
return SK_Class;
if (Decl->isClass() && NamingStyles[SK_Class])
return SK_Class;
if (Decl->isClass() && NamingStyles[SK_Struct])
return SK_Struct;
if (Decl->isUnion() && NamingStyles[SK_Union])
return SK_Union;
if (Decl->isEnum() && NamingStyles[SK_Enum])
return SK_Enum;
return SK_Invalid;
}
if (const auto *Decl = dyn_cast<FieldDecl>(D)) {
QualType Type = Decl->getType();
if (!Type.isNull() && Type.isConstQualified()) {
if (NamingStyles[SK_ConstantMember])
return SK_ConstantMember;
if (NamingStyles[SK_Constant])
return SK_Constant;
}
if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMember])
return SK_PrivateMember;
if (Decl->getAccess() == AS_protected && NamingStyles[SK_ProtectedMember])
return SK_ProtectedMember;
if (Decl->getAccess() == AS_public && NamingStyles[SK_PublicMember])
return SK_PublicMember;
if (NamingStyles[SK_Member])
return SK_Member;
return SK_Invalid;
}
if (const auto *Decl = dyn_cast<ParmVarDecl>(D)) {
if (isParamInMainLikeFunction(*Decl, IgnoreMainLikeFunctions))
return SK_Invalid;
QualType Type = Decl->getType();
if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
return SK_ConstexprVariable;
if (!Type.isNull() && Type.isConstQualified()) {
if (Type.getTypePtr()->isAnyPointerType() && NamingStyles[SK_ConstantPointerParameter])
return SK_ConstantPointerParameter;
if (NamingStyles[SK_ConstantParameter])
return SK_ConstantParameter;
if (NamingStyles[SK_Constant])
return SK_Constant;
}
if (Decl->isParameterPack() && NamingStyles[SK_ParameterPack])
return SK_ParameterPack;
if (!Type.isNull() && Type.getTypePtr()->isAnyPointerType() && NamingStyles[SK_PointerParameter])
return SK_PointerParameter;
if (NamingStyles[SK_Parameter])
return SK_Parameter;
return SK_Invalid;
}
if (const auto *Decl = dyn_cast<VarDecl>(D)) {
QualType Type = Decl->getType();
if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
return SK_ConstexprVariable;
if (!Type.isNull() && Type.isConstQualified()) {
if (Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant])
return SK_ClassConstant;
if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() && NamingStyles[SK_GlobalConstantPointer])
return SK_GlobalConstantPointer;
if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant])
return SK_GlobalConstant;
if (Decl->isStaticLocal() && NamingStyles[SK_StaticConstant])
return SK_StaticConstant;
if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() && NamingStyles[SK_LocalConstantPointer])
return SK_LocalConstantPointer;
if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant])
return SK_LocalConstant;
if (Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalConstant])
return SK_LocalConstant;
if (NamingStyles[SK_Constant])
return SK_Constant;
}
if (Decl->isStaticDataMember() && NamingStyles[SK_ClassMember])
return SK_ClassMember;
if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() && NamingStyles[SK_GlobalPointer])
return SK_GlobalPointer;
if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalVariable])
return SK_GlobalVariable;
if (Decl->isStaticLocal() && NamingStyles[SK_StaticVariable])
return SK_StaticVariable;
if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() && NamingStyles[SK_LocalPointer])
return SK_LocalPointer;
if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalVariable])
return SK_LocalVariable;
if (Decl->isFunctionOrMethodVarDecl() && NamingStyles[SK_LocalVariable])
return SK_LocalVariable;
if (NamingStyles[SK_Variable])
return SK_Variable;
return SK_Invalid;
}
if (const auto *Decl = dyn_cast<CXXMethodDecl>(D)) {
if (Decl->isMain() || !Decl->isUserProvided() ||
Decl->size_overridden_methods() > 0)
return SK_Invalid;
// If this method has the same name as any base method, this is likely
// necessary even if it's not an override. e.g. CRTP.
auto FindHidden = [&](const CXXBaseSpecifier *S, clang::CXXBasePath &P) {
return CXXRecordDecl::FindOrdinaryMember(S, P, Decl->getDeclName());
};
CXXBasePaths UnusedPaths;
if (Decl->getParent()->lookupInBases(FindHidden, UnusedPaths))
return SK_Invalid;
if (Decl->isConstexpr() && NamingStyles[SK_ConstexprMethod])
return SK_ConstexprMethod;
if (Decl->isConstexpr() && NamingStyles[SK_ConstexprFunction])
return SK_ConstexprFunction;
if (Decl->isStatic() && NamingStyles[SK_ClassMethod])
return SK_ClassMethod;
if (Decl->isVirtual() && NamingStyles[SK_VirtualMethod])
return SK_VirtualMethod;
if (Decl->getAccess() == AS_private && NamingStyles[SK_PrivateMethod])
return SK_PrivateMethod;
if (Decl->getAccess() == AS_protected && NamingStyles[SK_ProtectedMethod])
return SK_ProtectedMethod;
if (Decl->getAccess() == AS_public && NamingStyles[SK_PublicMethod])
return SK_PublicMethod;
if (NamingStyles[SK_Method])
return SK_Method;
if (NamingStyles[SK_Function])
return SK_Function;
return SK_Invalid;
}
if (const auto *Decl = dyn_cast<FunctionDecl>(D)) {
if (Decl->isMain())
return SK_Invalid;
if (Decl->isConstexpr() && NamingStyles[SK_ConstexprFunction])
return SK_ConstexprFunction;
if (Decl->isGlobal() && NamingStyles[SK_GlobalFunction])
return SK_GlobalFunction;
if (NamingStyles[SK_Function])
return SK_Function;
}
if (isa<TemplateTypeParmDecl>(D)) {
if (NamingStyles[SK_TypeTemplateParameter])
return SK_TypeTemplateParameter;
if (NamingStyles[SK_TemplateParameter])
return SK_TemplateParameter;
return SK_Invalid;
}
if (isa<NonTypeTemplateParmDecl>(D)) {
if (NamingStyles[SK_ValueTemplateParameter])
return SK_ValueTemplateParameter;
if (NamingStyles[SK_TemplateParameter])
return SK_TemplateParameter;
return SK_Invalid;
}
if (isa<TemplateTemplateParmDecl>(D)) {
if (NamingStyles[SK_TemplateTemplateParameter])
return SK_TemplateTemplateParameter;
if (NamingStyles[SK_TemplateParameter])
return SK_TemplateParameter;
return SK_Invalid;
}
return SK_Invalid;
}
static llvm::Optional<RenamerClangTidyCheck::FailureInfo> getFailureInfo(
StringRef Name, SourceLocation Location,
ArrayRef<llvm::Optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
StyleKind SK, const SourceManager &SM, bool IgnoreFailedSplit) {
if (SK == SK_Invalid || !NamingStyles[SK])
return None;
const IdentifierNamingCheck::NamingStyle &Style = *NamingStyles[SK];
if (matchesStyle(Name, Style))
return None;
std::string KindName =
fixupWithCase(StyleNames[SK], IdentifierNamingCheck::CT_LowerCase);
std::replace(KindName.begin(), KindName.end(), '_', ' ');
std::string Fixup = fixupWithStyle(Name, Style);
if (StringRef(Fixup).equals(Name)) {
if (!IgnoreFailedSplit) {
LLVM_DEBUG(Location.print(llvm::dbgs(), SM);
llvm::dbgs()
<< llvm::formatv(": unable to split words for {0} '{1}'\n",
KindName, Name));
}
return None;
}
return RenamerClangTidyCheck::FailureInfo{std::move(KindName),
std::move(Fixup)};
}
llvm::Optional<RenamerClangTidyCheck::FailureInfo>
IdentifierNamingCheck::GetDeclFailureInfo(const NamedDecl *Decl,
const SourceManager &SM) const {
SourceLocation Loc = Decl->getLocation();
ArrayRef<llvm::Optional<NamingStyle>> NamingStyles =
getStyleForFile(SM.getFilename(Loc));
return getFailureInfo(
Decl->getName(), Loc, NamingStyles,
findStyleKind(Decl, NamingStyles, IgnoreMainLikeFunctions), SM,
IgnoreFailedSplit);
}
llvm::Optional<RenamerClangTidyCheck::FailureInfo>
IdentifierNamingCheck::GetMacroFailureInfo(const Token &MacroNameTok,
const SourceManager &SM) const {
SourceLocation Loc = MacroNameTok.getLocation();
return getFailureInfo(MacroNameTok.getIdentifierInfo()->getName(), Loc,
getStyleForFile(SM.getFilename(Loc)),
SK_MacroDefinition, SM, IgnoreFailedSplit);
}
RenamerClangTidyCheck::DiagInfo
IdentifierNamingCheck::GetDiagInfo(const NamingCheckId &ID,
const NamingCheckFailure &Failure) const {
return DiagInfo{"invalid case style for %0 '%1'",
[&](DiagnosticBuilder &Diag) {
Diag << Failure.Info.KindName << ID.second;
}};
}
ArrayRef<llvm::Optional<IdentifierNamingCheck::NamingStyle>>
IdentifierNamingCheck::getStyleForFile(StringRef FileName) const {
if (!GetConfigPerFile)
return MainFileStyle;
auto &Styles = NamingStylesCache[llvm::sys::path::parent_path(FileName)];
if (Styles.empty()) {
ClangTidyOptions Options = Context->getOptionsForFile(FileName);
if (Options.Checks && GlobList(*Options.Checks).contains(CheckName))
Styles = getNamingStyles({CheckName, Options.CheckOptions});
else
Styles.resize(SK_Count, None);
}
return Styles;
}
} // namespace readability
} // namespace tidy
} // namespace clang