-
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
base: main
Are you sure you want to change the base?
Conversation
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
| - Does not warn about template functions | ||
| - Does not warn about variadic functions |
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.
Missing punctuation at the end?
|
|
||
| - Does not warn about template functions | ||
| - Does not warn about variadic functions | ||
|
|
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.
Excessive newline.
| } | ||
|
|
||
| void ShadowedNamespaceFunctionCheck::registerMatchers(MatchFinder *Finder) { | ||
| // Simple matcher for all function definitions |
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.
Remove unrelated (obvious) comments
| if (!Func || !Result.SourceManager) | ||
| return; |
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.
Just assert, we expect the matcher to work.
Also, it's strange not to have Source Manager, we never check for it
| std::pair<const FunctionDecl *, const NamespaceDecl *> | ||
| ShadowedNamespaceFunctionCheck::findShadowedInNamespace( | ||
| const NamespaceDecl *NS, const FunctionDecl *GlobalFunc, | ||
| const std::string &GlobalFuncName) { |
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.
Does it need to be a class member and not a regular static function?
| if (Func->isTemplated() || Func->isStatic() || Func->isMain() || | ||
| Func->isImplicit() || Func->isVariadic()) | ||
| return; |
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.
We should check it in matchers
| // Skip if not in global namespace | ||
| const DeclContext *DC = Func->getDeclContext(); | ||
| if (!DC->isTranslationUnit()) | ||
| return; |
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.
Should be checked in matchers, decl(hasDeclContext(translationUnitDecl()))
| << Func->getDeclName() << NamespaceName | ||
| << ShadowedFunc->getDeclName().getAsString(); | ||
|
|
||
| // Generate fixit hint to add namespace qualification |
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.
ditto comments
| Diag << FixItHint::CreateInsertion(NameLoc, Fix); | ||
| } | ||
|
|
||
| // Note: Also show where the shadowed function is declared |
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.
ditto comments
| Detects free functions in the global namespace that shadow functions declared | ||
| in other namespaces. This check helps prevent accidental shadowing of namespace |
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.
| Detects free functions in the global namespace that shadow functions declared | |
| in other namespaces. This check helps prevent accidental shadowing of namespace | |
| Detects free functions in the global namespace that shadow functions declared | |
| in other namespaces. | |
| This check helps prevent accidental shadowing of namespace |
| - New :doc:`misc-shadowed-namespace-function | ||
| <clang-tidy/checks/misc/shadowed-namespace-function>` check. | ||
|
|
||
| Detects free functions in global namespace that shadow functions from other namespaces. |
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.
80 char limit
...ools-extra/test/clang-tidy/checkers/misc/shadowed-namespace-function-anonymous-namespace.cpp
Outdated
Show resolved
Hide resolved
🐧 Linux x64 Test Results
|
Implementation of this check was mostly generated by DeepSeek.
I've tested it on llvm codebase with
-DLLVM_ENABLE_PROJECTS="bolt;clang;clang-tools-extra;compiler-rt;cross-project-tests;libc;libclc;lld;lldb;mlir;openmp;polly"It provided 6 warnings with fixit hints.
See 11.log based on trunk 070f331
Closes #6739