Skip to content

Commit 34dc72a

Browse files
committed
refactoring
1 parent 89029b7 commit 34dc72a

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

clang-tools-extra/clang-tidy/misc/ShadowedNamespaceFunctionCheck.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ void ShadowedNamespaceFunctionCheck::check(
4545

4646
// Skip templates, static functions, main, etc.
4747
if (Func->isTemplated() || Func->isStatic() ||
48-
Func->getName() == "main" || Func->isImplicit())
48+
Func->isMain() || Func->isImplicit())
4949
return;
5050

51-
std::string FuncName = Func->getNameAsString();
51+
const std::string FuncName = Func->getNameAsString();
5252
if (FuncName.empty())
5353
return;
5454

55-
ASTContext *Context = Result.Context;
55+
const ASTContext *Context = Result.Context;
5656

5757
// Look for functions with the same name in namespaces
5858
const FunctionDecl *ShadowedFunc = nullptr;
@@ -61,7 +61,7 @@ void ShadowedNamespaceFunctionCheck::check(
6161
// Traverse all declarations in the translation unit
6262
for (const auto *Decl : Context->getTranslationUnitDecl()->decls()) {
6363
if (const auto *NS = dyn_cast<NamespaceDecl>(Decl)) {
64-
findShadowedInNamespace(NS, Func, FuncName, ShadowedFunc, ShadowedNamespace);
64+
std::tie(ShadowedFunc, ShadowedNamespace) = findShadowedInNamespace(NS, Func, FuncName);
6565
if (ShadowedFunc) break;
6666
}
6767
}
@@ -73,17 +73,17 @@ void ShadowedNamespaceFunctionCheck::check(
7373
return;
7474

7575
// Generate warning message
76-
std::string NamespaceName = ShadowedNamespace->getQualifiedNameAsString();
76+
const std::string NamespaceName = ShadowedNamespace->getQualifiedNameAsString();
7777
auto Diag = diag(Func->getLocation(),
7878
"free function %0 shadows '%1::%2'")
7979
<< Func->getDeclName()
8080
<< NamespaceName
8181
<< ShadowedFunc->getDeclName().getAsString();
8282

8383
// Generate fixit hint to add namespace qualification
84-
SourceLocation NameLoc = Func->getLocation();
84+
const SourceLocation NameLoc = Func->getLocation();
8585
if (NameLoc.isValid() && !Func->getPreviousDecl()) {
86-
std::string Fix = NamespaceName + "::";
86+
const std::string Fix = NamespaceName + "::";
8787
Diag << FixItHint::CreateInsertion(NameLoc, Fix);
8888
}
8989

@@ -93,23 +93,21 @@ void ShadowedNamespaceFunctionCheck::check(
9393
<< ShadowedFunc->getDeclName();
9494
}
9595

96-
void ShadowedNamespaceFunctionCheck::findShadowedInNamespace(
96+
std::pair<const FunctionDecl *, const NamespaceDecl *> ShadowedNamespaceFunctionCheck::findShadowedInNamespace(
9797
const NamespaceDecl *NS,
9898
const FunctionDecl *GlobalFunc,
99-
const std::string &GlobalFuncName,
100-
const FunctionDecl *&ShadowedFunc,
101-
const NamespaceDecl *&ShadowedNamespace) {
99+
const std::string &GlobalFuncName) {
102100

103101
// Skip anonymous namespaces
104102
if (NS->isAnonymousNamespace())
105-
return;
103+
return {nullptr, nullptr};
106104

107105
for (const auto *Decl : NS->decls()) {
108106
// Check nested namespaces
109107
if (const auto *NestedNS = dyn_cast<NamespaceDecl>(Decl)) {
110-
findShadowedInNamespace(NestedNS, GlobalFunc, GlobalFuncName,
111-
ShadowedFunc, ShadowedNamespace);
112-
if (ShadowedFunc) return;
108+
auto [ShadowedFunc, ShadowedNamespace] = findShadowedInNamespace(NestedNS, GlobalFunc, GlobalFuncName);
109+
if (ShadowedFunc)
110+
return {ShadowedFunc, ShadowedNamespace};
113111
}
114112

115113
// Check functions
@@ -120,12 +118,11 @@ void ShadowedNamespaceFunctionCheck::findShadowedInNamespace(
120118
continue;
121119

122120
if (Func->getNameAsString() == GlobalFuncName) {
123-
ShadowedFunc = Func;
124-
ShadowedNamespace = NS;
125-
return;
121+
return {Func, NS};
126122
}
127123
}
128124
}
125+
return {nullptr, nullptr};
129126
}
130127

131128
} // namespace misc

clang-tools-extra/clang-tidy/misc/ShadowedNamespaceFunctionCheck.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_SHADOWEDNAMESPACEFUNCTIONCHECK_H
1111

1212
#include "../ClangTidyCheck.h"
13+
#include <tuple>
1314

1415
namespace clang::tidy::misc {
1516

@@ -27,11 +28,10 @@ class ShadowedNamespaceFunctionCheck : public ClangTidyCheck {
2728
return LangOpts.CPlusPlus;
2829
}
2930
private:
30-
void findShadowedInNamespace(const NamespaceDecl *NS,
31-
const FunctionDecl *GlobalFunc,
32-
const std::string &GlobalFuncName,
33-
const FunctionDecl *&ShadowedFunc,
34-
const NamespaceDecl *&ShadowedNamespace);
31+
std::pair<const FunctionDecl *, const NamespaceDecl *>
32+
findShadowedInNamespace(const NamespaceDecl *NS,
33+
const FunctionDecl *GlobalFunc,
34+
const std::string &GlobalFuncName);
3535
};
3636

3737
} // namespace clang::tidy::misc

0 commit comments

Comments
 (0)