Skip to content

Commit c72a6ac

Browse files
committed
Revert "[clang-tidy] readability-identifier-naming disregards parameters restrictions on main like functions"
This reverts commit 27e3671. This was an accidental push, and the author requested a revert on IRC as their local branch is in a bad state.
1 parent 242fed9 commit c72a6ac

File tree

7 files changed

+24
-312
lines changed

7 files changed

+24
-312
lines changed

clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
#include "IdentifierNamingCheck.h"
1010

11+
#include "../utils/ASTUtils.h"
12+
#include "clang/ASTMatchers/ASTMatchFinder.h"
1113
#include "clang/AST/CXXInheritance.h"
14+
#include "clang/Frontend/CompilerInstance.h"
1215
#include "clang/Lex/PPCallbacks.h"
1316
#include "clang/Lex/Preprocessor.h"
1417
#include "llvm/ADT/DenseMapInfo.h"
1518
#include "llvm/Support/Debug.h"
1619
#include "llvm/Support/Format.h"
17-
#include "llvm/Support/Regex.h"
1820

1921
#define DEBUG_TYPE "clang-tidy"
2022

@@ -124,9 +126,7 @@ class IdentifierNamingCheckPPCallbacks : public PPCallbacks {
124126

125127
IdentifierNamingCheck::IdentifierNamingCheck(StringRef Name,
126128
ClangTidyContext *Context)
127-
: RenamerClangTidyCheck(Name, Context),
128-
IgnoreFailedSplit(Options.get("IgnoreFailedSplit", 0)),
129-
IgnoreMainLikeFunctions(Options.get("IgnoreMainLikeFunctions", 0)) {
129+
: RenamerClangTidyCheck(Name, Context) {
130130
auto const fromString = [](StringRef Str) {
131131
return llvm::StringSwitch<llvm::Optional<CaseType>>(Str)
132132
.Case("aNy_CasE", CT_AnyCase)
@@ -151,6 +151,8 @@ IdentifierNamingCheck::IdentifierNamingCheck(StringRef Name,
151151
NamingStyles.push_back(llvm::None);
152152
}
153153
}
154+
155+
IgnoreFailedSplit = Options.get("IgnoreFailedSplit", 0);
154156
}
155157

156158
IdentifierNamingCheck::~IdentifierNamingCheck() = default;
@@ -191,7 +193,6 @@ void IdentifierNamingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
191193
}
192194

193195
Options.store(Opts, "IgnoreFailedSplit", IgnoreFailedSplit);
194-
Options.store(Opts, "IgnoreMainLikeFunctions", IgnoreMainLikeFunctions);
195196
}
196197

197198
static bool matchesStyle(StringRef Name,
@@ -323,67 +324,6 @@ static std::string fixupWithCase(StringRef Name,
323324
return Fixup;
324325
}
325326

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-
387327
static std::string
388328
fixupWithStyle(StringRef Name,
389329
const IdentifierNamingCheck::NamingStyle &Style) {
@@ -398,8 +338,7 @@ fixupWithStyle(StringRef Name,
398338
static StyleKind findStyleKind(
399339
const NamedDecl *D,
400340
const std::vector<llvm::Optional<IdentifierNamingCheck::NamingStyle>>
401-
&NamingStyles,
402-
bool IgnoreMainLikeFunctions) {
341+
&NamingStyles) {
403342
assert(D && D->getIdentifier() && !D->getName().empty() && !D->isImplicit() &&
404343
"Decl must be an explicit identifier with a name.");
405344

@@ -495,8 +434,6 @@ static StyleKind findStyleKind(
495434
}
496435

497436
if (const auto *Decl = dyn_cast<ParmVarDecl>(D)) {
498-
if (isParamInMainLikeFunction(*Decl, IgnoreMainLikeFunctions))
499-
return SK_Invalid;
500437
QualType Type = Decl->getType();
501438

502439
if (Decl->isConstexpr() && NamingStyles[SK_ConstexprVariable])
@@ -678,7 +615,7 @@ static StyleKind findStyleKind(
678615
llvm::Optional<RenamerClangTidyCheck::FailureInfo>
679616
IdentifierNamingCheck::GetDeclFailureInfo(const NamedDecl *Decl,
680617
const SourceManager &SM) const {
681-
StyleKind SK = findStyleKind(Decl, NamingStyles, IgnoreMainLikeFunctions);
618+
StyleKind SK = findStyleKind(Decl, NamingStyles);
682619
if (SK == SK_Invalid)
683620
return None;
684621

clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ class IdentifierNamingCheck final : public RenamerClangTidyCheck {
7070
const NamingCheckFailure &Failure) const override;
7171

7272
std::vector<llvm::Optional<NamingStyle>> NamingStyles;
73-
const bool IgnoreFailedSplit;
74-
const bool IgnoreMainLikeFunctions;
73+
bool IgnoreFailedSplit;
7574
};
7675

7776
} // namespace readability

clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "clang/Lex/PPCallbacks.h"
1515
#include "clang/Lex/Preprocessor.h"
1616
#include "llvm/ADT/DenseMapInfo.h"
17+
#include "llvm/Support/Debug.h"
18+
#include "llvm/Support/Format.h"
1719

1820
#define DEBUG_TYPE "clang-tidy"
1921

@@ -104,22 +106,20 @@ void RenamerClangTidyCheck::registerMatchers(MatchFinder *Finder) {
104106
this);
105107
Finder->addMatcher(typeLoc().bind("typeLoc"), this);
106108
Finder->addMatcher(nestedNameSpecifierLoc().bind("nestedNameLoc"), this);
107-
auto MemberOrDependent =
108-
expr(eachOf(memberExpr().bind("memberExpr"),
109-
cxxDependentScopeMemberExpr().bind("depMemberExpr")));
110109
Finder->addMatcher(
111110
functionDecl(unless(cxxMethodDecl(isImplicit())),
112-
hasBody(forEachDescendant(MemberOrDependent))),
111+
hasBody(forEachDescendant(memberExpr().bind("memberExpr")))),
113112
this);
114113
Finder->addMatcher(
115-
cxxConstructorDecl(unless(isImplicit()),
116-
forEachConstructorInitializer(allOf(
117-
isWritten(), withInitializer(forEachDescendant(
118-
MemberOrDependent))))),
119-
this);
120-
Finder->addMatcher(
121-
fieldDecl(hasInClassInitializer(forEachDescendant(MemberOrDependent))),
114+
cxxConstructorDecl(
115+
unless(isImplicit()),
116+
forEachConstructorInitializer(
117+
allOf(isWritten(), withInitializer(forEachDescendant(
118+
memberExpr().bind("memberExpr")))))),
122119
this);
120+
Finder->addMatcher(fieldDecl(hasInClassInitializer(
121+
forEachDescendant(memberExpr().bind("memberExpr")))),
122+
this);
123123
}
124124

125125
void RenamerClangTidyCheck::registerPPCallbacks(
@@ -271,39 +271,6 @@ void RenamerClangTidyCheck::check(const MatchFinder::MatchResult &Result) {
271271
return;
272272
}
273273

274-
if (const auto *DepMemberRef =
275-
Result.Nodes.getNodeAs<CXXDependentScopeMemberExpr>(
276-
"depMemberExpr")) {
277-
QualType BaseType = DepMemberRef->isArrow()
278-
? DepMemberRef->getBaseType()->getPointeeType()
279-
: DepMemberRef->getBaseType();
280-
if (BaseType.isNull())
281-
return;
282-
const CXXRecordDecl *Base = BaseType.getTypePtr()->getAsCXXRecordDecl();
283-
if (!Base)
284-
return;
285-
DeclarationName DeclName = DepMemberRef->getMemberNameInfo().getName();
286-
if (!DeclName.isIdentifier())
287-
return;
288-
StringRef DependentName = DeclName.getAsIdentifierInfo()->getName();
289-
for (const FieldDecl *Field : Base->fields()) {
290-
if (Field->getParent() == Base && Field->getDeclName().isIdentifier() &&
291-
Field->getName().equals(DependentName)) {
292-
SourceRange Range = DepMemberRef->getMemberNameInfo().getSourceRange();
293-
addUsage(NamingCheckFailures, Field, Range, Result.SourceManager);
294-
return;
295-
}
296-
}
297-
for (const CXXMethodDecl *Method : Base->methods()) {
298-
if (Method->getParent() == Base && Method->getDeclName().isIdentifier() &&
299-
Method->getName().equals(DependentName)) {
300-
SourceRange Range = DepMemberRef->getMemberNameInfo().getSourceRange();
301-
addUsage(NamingCheckFailures, Method, Range, Result.SourceManager);
302-
return;
303-
}
304-
}
305-
}
306-
307274
if (const auto *Decl = Result.Nodes.getNodeAs<NamedDecl>("decl")) {
308275
if (!Decl->getIdentifier() || Decl->getName().empty() || Decl->isImplicit())
309276
return;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ New aliases
104104
Changes in existing checks
105105
^^^^^^^^^^^^^^^^^^^^^^^^^^
106106

107-
- Improved :doc:'readability-identifier-naming
108-
<clang-tidy/checks/readability-identifier-naming>` check.
109-
110-
Now able to rename member references in class template definitions with
111-
explicit access.
112107

113108
Renamed checks
114109
^^^^^^^^^^^^^^

clang-tools-extra/docs/clang-tidy/checks/readability-identifier-naming.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ The following options are describe below:
5555
- :option:`GlobalFunctionCase`, :option:`GlobalFunctionPrefix`, :option:`GlobalFunctionSuffix`
5656
- :option:`GlobalPointerCase`, :option:`GlobalPointerPrefix`, :option:`GlobalPointerSuffix`
5757
- :option:`GlobalVariableCase`, :option:`GlobalVariablePrefix`, :option:`GlobalVariableSuffix`
58-
- :option:`IgnoreMainLikeFunctions`
5958
- :option:`InlineNamespaceCase`, :option:`InlineNamespacePrefix`, :option:`InlineNamespaceSuffix`
6059
- :option:`LocalConstantCase`, :option:`LocalConstantPrefix`, :option:`LocalConstantSuffix`
6160
- :option:`LocalConstantPointerCase`, :option:`LocalConstantPointerPrefix`, :option:`LocalConstantPointerSuffix`
@@ -828,12 +827,6 @@ After:
828827

829828
int pre_global3_post;
830829

831-
.. option:: IgnoreMainLikeFunctions
832-
833-
When set to `1` functions that have a similar signature to ``main`` or
834-
``wmain`` won't enforce checks on the names of their parameters.
835-
Default value is `0`.
836-
837830
.. option:: InlineNamespaceCase
838831

839832
When defined, the check will ensure inline namespaces names conform to the

clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-main-like.cpp

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)