8
8
9
9
#include " IdentifierNamingCheck.h"
10
10
11
+ #include " ../utils/ASTUtils.h"
12
+ #include " clang/ASTMatchers/ASTMatchFinder.h"
11
13
#include " clang/AST/CXXInheritance.h"
14
+ #include " clang/Frontend/CompilerInstance.h"
12
15
#include " clang/Lex/PPCallbacks.h"
13
16
#include " clang/Lex/Preprocessor.h"
14
17
#include " llvm/ADT/DenseMapInfo.h"
15
18
#include " llvm/Support/Debug.h"
16
19
#include " llvm/Support/Format.h"
17
- #include " llvm/Support/Regex.h"
18
20
19
21
#define DEBUG_TYPE " clang-tidy"
20
22
@@ -124,9 +126,7 @@ class IdentifierNamingCheckPPCallbacks : public PPCallbacks {
124
126
125
127
IdentifierNamingCheck::IdentifierNamingCheck (StringRef Name,
126
128
ClangTidyContext *Context)
127
- : RenamerClangTidyCheck(Name, Context),
128
- IgnoreFailedSplit (Options.get(" IgnoreFailedSplit" , 0 )),
129
- IgnoreMainLikeFunctions(Options.get(" IgnoreMainLikeFunctions" , 0 )) {
129
+ : RenamerClangTidyCheck(Name, Context) {
130
130
auto const fromString = [](StringRef Str) {
131
131
return llvm::StringSwitch<llvm::Optional<CaseType>>(Str)
132
132
.Case (" aNy_CasE" , CT_AnyCase)
@@ -151,6 +151,8 @@ IdentifierNamingCheck::IdentifierNamingCheck(StringRef Name,
151
151
NamingStyles.push_back (llvm::None);
152
152
}
153
153
}
154
+
155
+ IgnoreFailedSplit = Options.get (" IgnoreFailedSplit" , 0 );
154
156
}
155
157
156
158
IdentifierNamingCheck::~IdentifierNamingCheck () = default ;
@@ -191,7 +193,6 @@ void IdentifierNamingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
191
193
}
192
194
193
195
Options.store (Opts, " IgnoreFailedSplit" , IgnoreFailedSplit);
194
- Options.store (Opts, " IgnoreMainLikeFunctions" , IgnoreMainLikeFunctions);
195
196
}
196
197
197
198
static bool matchesStyle (StringRef Name,
@@ -323,67 +324,6 @@ static std::string fixupWithCase(StringRef Name,
323
324
return Fixup;
324
325
}
325
326
326
- static bool isParamInMainLikeFunction (const ParmVarDecl &ParmDecl,
327
- bool IncludeMainLike) {
328
- const auto *FDecl =
329
- dyn_cast_or_null<FunctionDecl>(ParmDecl.getParentFunctionOrMethod ());
330
- if (!FDecl)
331
- return false ;
332
- if (FDecl->isMain ())
333
- return true ;
334
- if (!IncludeMainLike)
335
- return false ;
336
- if (FDecl->getAccess () != AS_public && FDecl->getAccess () != AS_none)
337
- return false ;
338
- enum MainType { None, Main, WMain };
339
- auto IsCharPtrPtr = [](QualType QType) -> MainType {
340
- if (QType.isNull ())
341
- return None;
342
- if (QType = QType->getPointeeType (), QType.isNull ())
343
- return None;
344
- if (QType = QType->getPointeeType (), QType.isNull ())
345
- return None;
346
- if (QType->isCharType ())
347
- return Main;
348
- if (QType->isWideCharType ())
349
- return WMain;
350
- return None;
351
- };
352
- auto IsIntType = [](QualType QType) {
353
- if (QType.isNull ())
354
- return false ;
355
- if (const auto *Builtin =
356
- dyn_cast<BuiltinType>(QType->getUnqualifiedDesugaredType ())) {
357
- return Builtin->getKind () == BuiltinType::Int;
358
- }
359
- return false ;
360
- };
361
- if (!IsIntType (FDecl->getReturnType ()))
362
- return false ;
363
- if (FDecl->getNumParams () < 2 || FDecl->getNumParams () > 3 )
364
- return false ;
365
- if (!IsIntType (FDecl->parameters ()[0 ]->getType ()))
366
- return false ;
367
- MainType Type = IsCharPtrPtr (FDecl->parameters ()[1 ]->getType ());
368
- if (Type == None)
369
- return false ;
370
- if (FDecl->getNumParams () == 3 &&
371
- IsCharPtrPtr (FDecl->parameters ()[2 ]->getType ()) != Type)
372
- return false ;
373
-
374
- if (Type == Main) {
375
- static llvm::Regex Matcher (
376
- " (^[Mm]ain([_A-Z]|$))|([a-z0-9_]Main([_A-Z]|$))|(_main(_|$))" );
377
- assert (Matcher.isValid () && " Invalid Matcher for main like functions." );
378
- return Matcher.match (FDecl->getName ());
379
- } else {
380
- static llvm::Regex Matcher (" (^((W[Mm])|(wm))ain([_A-Z]|$))|([a-z0-9_]W[Mm]"
381
- " ain([_A-Z]|$))|(_wmain(_|$))" );
382
- assert (Matcher.isValid () && " Invalid Matcher for wmain like functions." );
383
- return Matcher.match (FDecl->getName ());
384
- }
385
- }
386
-
387
327
static std::string
388
328
fixupWithStyle (StringRef Name,
389
329
const IdentifierNamingCheck::NamingStyle &Style) {
@@ -398,8 +338,7 @@ fixupWithStyle(StringRef Name,
398
338
static StyleKind findStyleKind (
399
339
const NamedDecl *D,
400
340
const std::vector<llvm::Optional<IdentifierNamingCheck::NamingStyle>>
401
- &NamingStyles,
402
- bool IgnoreMainLikeFunctions) {
341
+ &NamingStyles) {
403
342
assert (D && D->getIdentifier () && !D->getName ().empty () && !D->isImplicit () &&
404
343
" Decl must be an explicit identifier with a name." );
405
344
@@ -495,8 +434,6 @@ static StyleKind findStyleKind(
495
434
}
496
435
497
436
if (const auto *Decl = dyn_cast<ParmVarDecl>(D)) {
498
- if (isParamInMainLikeFunction (*Decl, IgnoreMainLikeFunctions))
499
- return SK_Invalid;
500
437
QualType Type = Decl->getType ();
501
438
502
439
if (Decl->isConstexpr () && NamingStyles[SK_ConstexprVariable])
@@ -678,7 +615,7 @@ static StyleKind findStyleKind(
678
615
llvm::Optional<RenamerClangTidyCheck::FailureInfo>
679
616
IdentifierNamingCheck::GetDeclFailureInfo (const NamedDecl *Decl,
680
617
const SourceManager &SM) const {
681
- StyleKind SK = findStyleKind (Decl, NamingStyles, IgnoreMainLikeFunctions );
618
+ StyleKind SK = findStyleKind (Decl, NamingStyles);
682
619
if (SK == SK_Invalid)
683
620
return None;
684
621
0 commit comments