-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy] Add misc-shadowed-namespace-function check #168406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
denzor200
wants to merge
25
commits into
llvm:main
Choose a base branch
from
denzor200:shadowed-namespace-function
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+514
−0
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
b7d665b
Implementation template from DeepSeek
denzor200 e9de03d
fix unit-test
denzor200 199ecec
Docs written by DeepSeek
denzor200 8018941
short description by DeepSeek
denzor200 320f6c7
fix list.rst
denzor200 5994fc0
more tests && fix the doc
denzor200 4112759
fix invalid fixit for correctly defined function
denzor200 89029b7
more unit-test
denzor200 34dc72a
refactoring
denzor200 a249084
Fix FP
denzor200 b1ffd92
more unit-tests
denzor200 1589ed5
lint
denzor200 3b36a43
format
denzor200 1f7748d
friend
denzor200 d6bdb39
fix doc
denzor200 51eac24
important TODOs for future versions of check
denzor200 532b761
refactor tests
denzor200 94ebafa
review
denzor200 cd94fa7
format
denzor200 b1bb0e3
review
denzor200 013340f
add inline namespace tests
denzor200 54872ef
fix ambiguous case
denzor200 263f99b
formtat
denzor200 3dc1ded
review
denzor200 9f12578
lint
denzor200 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
clang-tools-extra/clang-tidy/misc/ShadowedNamespaceFunctionCheck.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 "ShadowedNamespaceFunctionCheck.h" | ||
| #include "../utils/FixItHintUtils.h" | ||
| #include "clang/AST/ASTContext.h" | ||
| #include "clang/AST/Decl.h" | ||
| #include "clang/AST/DeclCXX.h" | ||
| #include "clang/ASTMatchers/ASTMatchers.h" | ||
| #include "llvm/ADT/STLExtras.h" | ||
| #include "llvm/ADT/SmallPtrSet.h" | ||
|
|
||
| using namespace clang; | ||
| using namespace clang::ast_matchers; | ||
| using namespace clang::tidy; | ||
|
|
||
| namespace clang::tidy::misc { | ||
|
|
||
| template <typename ContainerTy> | ||
| static auto makeCannonicalTypesRange(ContainerTy &&C) { | ||
| return llvm::map_range(C, [](const ParmVarDecl *Param) { | ||
| return Param->getType().getCanonicalType(); | ||
| }); | ||
| } | ||
|
|
||
| static bool hasSameSignature(const FunctionDecl *Func1, | ||
| const FunctionDecl *Func2) { | ||
| if (Func1->param_size() != Func2->param_size()) | ||
| return false; | ||
|
|
||
| if (Func1->getReturnType().getCanonicalType() != | ||
| Func2->getReturnType().getCanonicalType()) | ||
| return false; | ||
|
|
||
| return llvm::equal(makeCannonicalTypesRange(Func1->parameters()), | ||
| makeCannonicalTypesRange(Func2->parameters())); | ||
| } | ||
|
|
||
| static std::pair<const FunctionDecl *, const NamespaceDecl *> | ||
| findShadowedInNamespace(const NamespaceDecl *NS, const FunctionDecl *GlobalFunc, | ||
| StringRef GlobalFuncName, | ||
| llvm::SmallPtrSet<const FunctionDecl *, 16> &All) { | ||
|
|
||
| if (NS->isAnonymousNamespace()) | ||
| return {nullptr, nullptr}; | ||
|
|
||
| const FunctionDecl *ShadowedFunc = nullptr; | ||
| const NamespaceDecl *ShadowedNamespace = nullptr; | ||
|
|
||
| for (const auto *Decl : NS->decls()) { | ||
| // Check nested namespaces | ||
| if (const auto *NestedNS = dyn_cast<NamespaceDecl>(Decl)) { | ||
| auto [NestedShadowedFunc, NestedShadowedNamespace] = | ||
| findShadowedInNamespace(NestedNS, GlobalFunc, GlobalFuncName, All); | ||
| if (!ShadowedFunc) | ||
| std::tie(ShadowedFunc, ShadowedNamespace) = | ||
| std::tie(NestedShadowedFunc, NestedShadowedNamespace); | ||
| } | ||
|
|
||
| // Check functions | ||
| if (const auto *Func = dyn_cast<FunctionDecl>(Decl)) { | ||
| // TODO: syncronize this check with the matcher? | ||
| if (Func == GlobalFunc || Func->isTemplated() || | ||
| Func->isThisDeclarationADefinition()) | ||
| continue; | ||
|
|
||
| if (Func->getName() == GlobalFuncName && !Func->isVariadic() && | ||
| hasSameSignature(Func, GlobalFunc)) { | ||
| All.insert(Func); | ||
| if (!ShadowedFunc) | ||
| std::tie(ShadowedFunc, ShadowedNamespace) = std::tie(Func, NS); | ||
| } | ||
| } | ||
| } | ||
| return {ShadowedFunc, ShadowedNamespace}; | ||
| } | ||
|
|
||
| void ShadowedNamespaceFunctionCheck::registerMatchers(MatchFinder *Finder) { | ||
| Finder->addMatcher( | ||
| functionDecl(isDefinition(), decl(hasDeclContext(translationUnitDecl())), | ||
| unless(anyOf(isImplicit(), isVariadic(), isMain(), | ||
| isStaticStorageClass(), | ||
| ast_matchers::isTemplateInstantiation()))) | ||
| .bind("func"), | ||
| this); | ||
| } | ||
|
|
||
| void ShadowedNamespaceFunctionCheck::check( | ||
| const MatchFinder::MatchResult &Result) { | ||
| const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func"); | ||
|
|
||
| const StringRef FuncName = Func->getName(); | ||
| if (FuncName.empty()) | ||
| return; | ||
|
|
||
| const ASTContext *Context = Result.Context; | ||
|
|
||
| llvm::SmallPtrSet<const FunctionDecl *, 16> AllShadowedFuncs; | ||
| const FunctionDecl *ShadowedFunc = nullptr; | ||
| const NamespaceDecl *ShadowedNamespace = nullptr; | ||
|
|
||
| for (const auto *Decl : Context->getTranslationUnitDecl()->decls()) { | ||
| if (const auto *NS = dyn_cast<NamespaceDecl>(Decl)) { | ||
| auto [NestedShadowedFunc, NestedShadowedNamespace] = | ||
| findShadowedInNamespace(NS, Func, FuncName, AllShadowedFuncs); | ||
| if (!ShadowedFunc) | ||
| std::tie(ShadowedFunc, ShadowedNamespace) = | ||
| std::tie(NestedShadowedFunc, NestedShadowedNamespace); | ||
| } | ||
| } | ||
|
|
||
| if (!ShadowedFunc || !ShadowedNamespace) | ||
| return; | ||
|
|
||
| // TODO: should it be inside findShadowedInNamespace? | ||
| if (ShadowedFunc->getDefinition()) | ||
| return; | ||
|
|
||
| const bool Ambiguous = AllShadowedFuncs.size() > 1; | ||
| std::string NamespaceName = ShadowedNamespace->getQualifiedNameAsString(); | ||
| auto Diag = diag(Func->getLocation(), | ||
| "free function %0 shadows %select{|at least }1'%2::%3'") | ||
| << Func << Ambiguous << NamespaceName | ||
| << ShadowedFunc->getDeclName().getAsString(); | ||
|
|
||
| const SourceLocation NameLoc = Func->getLocation(); | ||
| if (NameLoc.isValid() && !Func->getPreviousDecl() && !Ambiguous) { | ||
| const std::string Fix = std::move(NamespaceName) + "::"; | ||
| Diag << FixItHint::CreateInsertion(NameLoc, Fix); | ||
| } | ||
|
|
||
| for (const FunctionDecl *NoteShadowedFunc : AllShadowedFuncs) | ||
| diag(NoteShadowedFunc->getLocation(), "function %0 declared here", | ||
| DiagnosticIDs::Note) | ||
| << NoteShadowedFunc->getDeclName(); | ||
| } | ||
|
|
||
| } // namespace clang::tidy::misc | ||
35 changes: 35 additions & 0 deletions
35
clang-tools-extra/clang-tidy/misc/ShadowedNamespaceFunctionCheck.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_SHADOWEDNAMESPACEFUNCTIONCHECK_H | ||
| #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_SHADOWEDNAMESPACEFUNCTIONCHECK_H | ||
|
|
||
| #include "../ClangTidyCheck.h" | ||
| #include <tuple> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be moved to |
||
|
|
||
| namespace clang::tidy::misc { | ||
|
|
||
| /// Detects free functions in global namespace that shadow functions from other | ||
| /// namespaces. | ||
| /// | ||
| /// For the user-facing documentation see: | ||
| /// https://clang.llvm.org/extra/clang-tidy/checks/misc/shadowed-namespace-function.html | ||
| class ShadowedNamespaceFunctionCheck : public ClangTidyCheck { | ||
| public: | ||
| ShadowedNamespaceFunctionCheck(StringRef Name, ClangTidyContext *Context) | ||
| : ClangTidyCheck(Name, Context) {} | ||
| void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
| void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
| bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { | ||
| return LangOpts.CPlusPlus; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace clang::tidy::misc | ||
|
|
||
| #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_SHADOWEDNAMESPACEFUNCTIONCHECK_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
clang-tools-extra/docs/clang-tidy/checks/misc/shadowed-namespace-function.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| .. title:: clang-tidy - misc-shadowed-namespace-function | ||
|
|
||
| misc-shadowed-namespace-function | ||
| ================================ | ||
|
|
||
| Detects free functions in the global namespace that shadow functions declared | ||
| in other namespaces. | ||
|
|
||
| This check helps prevent accidental shadowing of namespace functions, which can | ||
| lead to confusion about which function is being called and potential linking | ||
| errors. | ||
|
|
||
| Examples | ||
| -------- | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| namespace utils { | ||
| void process(); | ||
| void calculate(); | ||
| } | ||
|
|
||
| // Warning: free function shadows utils::process | ||
| void process() {} | ||
|
|
||
| // No warning - static function | ||
| static void calculate() {} | ||
|
|
||
| The check will suggest adding the appropriate namespace qualification: | ||
|
|
||
| .. code-block:: diff | ||
| - void process() {} | ||
| + void utils::process() {} | ||
| The check will not warn about: | ||
|
|
||
| - Static functions or member functions; | ||
| - Functions in anonymous namespaces; | ||
| - The ``main`` function. | ||
|
|
||
| Limitations | ||
| ----------- | ||
|
|
||
| - Does not warn about friend functions: | ||
|
|
||
| .. code-block:: c++ | ||
|
|
||
| namespace llvm::gsym { | ||
| struct MergedFunctionsInfo { | ||
| friend bool operator==(const MergedFunctionsInfo &LHS, | ||
| const MergedFunctionsInfo &RHS); | ||
| }; | ||
| } | ||
|
|
||
| using namespace llvm::gsym; | ||
|
|
||
| bool operator==(const MergedFunctionsInfo &LHS, // no warning in this version | ||
| const MergedFunctionsInfo &RHS) { | ||
| return LHS.MergedFunctions == RHS.MergedFunctions; | ||
| } | ||
|
|
||
| - Does not warn about template functions; | ||
| - Does not warn about variadic functions. |
29 changes: 29 additions & 0 deletions
29
clang-tools-extra/test/clang-tidy/checkers/misc/shadowed-namespace-function-cxx20.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // RUN: %check_clang_tidy -std=c++20 %s misc-shadowed-namespace-function %t | ||
|
|
||
| void f1_nested_inline_ns(); | ||
| namespace foo_nested_inline_ns::inline foo2::foo3 { | ||
| void f0_nested_inline_ns(); | ||
| void f1_nested_inline_ns(); | ||
| } | ||
| void f0_nested_inline_ns() {} | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: free function 'f0_nested_inline_ns' shadows 'foo_nested_inline_ns::foo3::f0_nested_inline_ns' [misc-shadowed-namespace-function] | ||
| // CHECK-FIXES: void foo_nested_inline_ns::foo3::f0_nested_inline_ns() {} | ||
| void f1_nested_inline_ns() {} | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: free function 'f1_nested_inline_ns' shadows 'foo_nested_inline_ns::foo3::f1_nested_inline_ns' [misc-shadowed-namespace-function] | ||
| // CHECK-MESSAGES-NOT: :[[@LINE-2]]:{{.*}}: note: FIX-IT applied suggested code changes | ||
|
|
||
| ////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| void f1_nested_inline_ns_2(); | ||
| namespace foo_nested_inline_ns_2::inline foo2 { | ||
| void f0_nested_inline_ns_2(); | ||
| void f1_nested_inline_ns_2(); | ||
| } | ||
| void f0_nested_inline_ns_2() {} | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: free function 'f0_nested_inline_ns_2' shadows 'foo_nested_inline_ns_2::foo2::f0_nested_inline_ns_2' [misc-shadowed-namespace-function] | ||
| // CHECK-FIXES: void foo_nested_inline_ns_2::foo2::f0_nested_inline_ns_2() {} | ||
| void f1_nested_inline_ns_2() {} | ||
| // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: free function 'f1_nested_inline_ns_2' shadows 'foo_nested_inline_ns_2::foo2::f1_nested_inline_ns_2' [misc-shadowed-namespace-function] | ||
| // CHECK-MESSAGES-NOT: :[[@LINE-2]]:{{.*}}: note: FIX-IT applied suggested code changes | ||
|
|
||
| ////////////////////////////////////////////////////////////////////////////////////////// |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary
ast_matchers::?