@@ -20,7 +20,8 @@ using namespace clang::tidy;
2020
2121namespace clang ::tidy::misc {
2222
23- static bool hasSameParameters (const FunctionDecl *Func1, const FunctionDecl *Func2) {
23+ static bool hasSameParameters (const FunctionDecl *Func1,
24+ const FunctionDecl *Func2) {
2425 if (Func1->param_size () != Func2->param_size ())
2526 return false ;
2627
@@ -34,18 +35,13 @@ static bool hasSameParameters(const FunctionDecl *Func1, const FunctionDecl *Fun
3435
3536void ShadowedNamespaceFunctionCheck::registerMatchers (MatchFinder *Finder) {
3637 // Simple matcher for all function definitions
37- Finder->addMatcher (
38- functionDecl (
39- isDefinition ()
40- ).bind (" func" ),
41- this
42- );
38+ Finder->addMatcher (functionDecl (isDefinition ()).bind (" func" ), this );
4339}
4440
4541void ShadowedNamespaceFunctionCheck::check (
4642 const MatchFinder::MatchResult &Result) {
4743 const auto *Func = Result.Nodes .getNodeAs <FunctionDecl>(" func" );
48-
44+
4945 if (!Func || !Result.SourceManager )
5046 return ;
5147
@@ -55,8 +51,8 @@ void ShadowedNamespaceFunctionCheck::check(
5551 return ;
5652
5753 // Skip templates, static functions, main, etc.
58- if (Func->isTemplated () || Func->isStatic () ||
59- Func->isMain () || Func-> isImplicit () || Func->isVariadic ())
54+ if (Func->isTemplated () || Func->isStatic () || Func-> isMain () ||
55+ Func->isImplicit () || Func->isVariadic ())
6056 return ;
6157
6258 const std::string FuncName = Func->getNameAsString ();
@@ -72,8 +68,10 @@ void ShadowedNamespaceFunctionCheck::check(
7268 // Traverse all declarations in the translation unit
7369 for (const auto *Decl : Context->getTranslationUnitDecl ()->decls ()) {
7470 if (const auto *NS = dyn_cast<NamespaceDecl>(Decl)) {
75- std::tie (ShadowedFunc, ShadowedNamespace) = findShadowedInNamespace (NS, Func, FuncName);
76- if (ShadowedFunc) break ;
71+ std::tie (ShadowedFunc, ShadowedNamespace) =
72+ findShadowedInNamespace (NS, Func, FuncName);
73+ if (ShadowedFunc)
74+ break ;
7775 }
7876 }
7977
@@ -84,11 +82,10 @@ void ShadowedNamespaceFunctionCheck::check(
8482 return ;
8583
8684 // Generate warning message
87- const std::string NamespaceName = ShadowedNamespace->getQualifiedNameAsString ();
88- auto Diag = diag (Func->getLocation (),
89- " free function %0 shadows '%1::%2'" )
90- << Func->getDeclName ()
91- << NamespaceName
85+ const std::string NamespaceName =
86+ ShadowedNamespace->getQualifiedNameAsString ();
87+ auto Diag = diag (Func->getLocation (), " free function %0 shadows '%1::%2'" )
88+ << Func->getDeclName () << NamespaceName
9289 << ShadowedFunc->getDeclName ().getAsString ();
9390
9491 // Generate fixit hint to add namespace qualification
@@ -99,36 +96,40 @@ void ShadowedNamespaceFunctionCheck::check(
9996 }
10097
10198 // Note: Also show where the shadowed function is declared
102- diag (ShadowedFunc->getLocation (),
103- " function %0 declared here " , DiagnosticIDs::Note)
99+ diag (ShadowedFunc->getLocation (), " function %0 declared here " ,
100+ DiagnosticIDs::Note)
104101 << ShadowedFunc->getDeclName ();
105102}
106103
107- std::pair<const FunctionDecl *, const NamespaceDecl *> ShadowedNamespaceFunctionCheck::findShadowedInNamespace (
108- const NamespaceDecl *NS,
109- const FunctionDecl *GlobalFunc,
104+ std::pair<const FunctionDecl *, const NamespaceDecl *>
105+ ShadowedNamespaceFunctionCheck::findShadowedInNamespace (
106+ const NamespaceDecl *NS, const FunctionDecl *GlobalFunc,
110107 const std::string &GlobalFuncName) {
111-
108+
112109 // Skip anonymous namespaces
113110 if (NS->isAnonymousNamespace ())
114111 return {nullptr , nullptr };
115112
116113 for (const auto *Decl : NS->decls ()) {
117114 // Check nested namespaces
118115 if (const auto *NestedNS = dyn_cast<NamespaceDecl>(Decl)) {
119- auto [ShadowedFunc, ShadowedNamespace] = findShadowedInNamespace (NestedNS, GlobalFunc, GlobalFuncName);
116+ auto [ShadowedFunc, ShadowedNamespace] =
117+ findShadowedInNamespace (NestedNS, GlobalFunc, GlobalFuncName);
120118 if (ShadowedFunc)
121119 return {ShadowedFunc, ShadowedNamespace};
122120 }
123121
124122 // Check functions
125123 if (const auto *Func = dyn_cast<FunctionDecl>(Decl)) {
126124 // Skip if it's the same function, templates, or definitions
127- if (Func == GlobalFunc || Func->isTemplated () ||
125+ if (Func == GlobalFunc || Func->isTemplated () ||
128126 Func->isThisDeclarationADefinition ())
129127 continue ;
130128
131- if (Func->getNameAsString () == GlobalFuncName && !Func->isVariadic () && hasSameParameters (Func, GlobalFunc) && Func->getReturnType ().getCanonicalType () == GlobalFunc->getReturnType ().getCanonicalType ()) {
129+ if (Func->getNameAsString () == GlobalFuncName && !Func->isVariadic () &&
130+ hasSameParameters (Func, GlobalFunc) &&
131+ Func->getReturnType ().getCanonicalType () ==
132+ GlobalFunc->getReturnType ().getCanonicalType ()) {
132133 return {Func, NS};
133134 }
134135 }
@@ -137,4 +138,3 @@ std::pair<const FunctionDecl *, const NamespaceDecl *> ShadowedNamespaceFunction
137138}
138139
139140} // namespace clang::tidy::misc
140-
0 commit comments