-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[libc++] Clang-tidy operator& hijacker. #128366
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3f89222
[libc++] Clang-tidy operator& hijacker.
mordante bc68ee1
Addresses review comments.
mordante 710398f
Merge branch 'main' into users/mordante/operator_hijacking_clang-tidy
mordante 8251c5d
Merge branch 'main' into users/mordante/operator_hijacking_clang-tidy
mordante b8432da
Try to fix CI and address review comment.
mordante 6681cc6
Another attempt at the CI.
mordante f938d3a
Another attempt at the CI.
mordante 66c72c5
Another attempt at the CI.
mordante 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
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
56 changes: 56 additions & 0 deletions
56
libcxx/test/tools/clang_tidy_checks/robust_against_operator_ampersand.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,56 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 "clang-tidy/ClangTidyCheck.h" | ||
| #include "clang-tidy/ClangTidyModuleRegistry.h" | ||
| #include "clang/ASTMatchers/ASTMatchers.h" | ||
| #include "clang/Tooling/FixIt.h" | ||
|
|
||
| #include "robust_against_operator_ampersand.hpp" | ||
|
|
||
| // This clang-tidy check ensures that we don't use operator& on dependant | ||
| // types. If the type is user supplied it may call the type's operator&. | ||
| // Instead use std::addressof. | ||
| // | ||
| // This is part of libc++'s policy | ||
| // https://libcxx.llvm.org/CodingGuidelines.html#don-t-use-argument-dependent-lookup-unless-required-by-the-standard | ||
|
|
||
| // TODO(LLVM-21) Remove dependentScopeDeclRefExpr | ||
| // dependentScopeDeclRefExpr requires Clang 20, this uses the same definition as Clang | ||
| #if defined(__clang_major__) && __clang_major__ < 20 | ||
| namespace clang::ast_matchers { | ||
| const internal::VariadicDynCastAllOfMatcher<Stmt, DependentScopeDeclRefExpr> dependentScopeDeclRefExpr; | ||
| } // namespace clang::ast_matchers | ||
| #endif | ||
|
|
||
| namespace libcpp { | ||
| robust_against_operator_ampersand::robust_against_operator_ampersand( | ||
| llvm::StringRef name, clang::tidy::ClangTidyContext* context) | ||
| : clang::tidy::ClangTidyCheck(name, context) {} | ||
|
|
||
| void robust_against_operator_ampersand::registerMatchers(clang::ast_matchers::MatchFinder* finder) { | ||
| using namespace clang::ast_matchers; | ||
| finder->addMatcher( | ||
| cxxOperatorCallExpr(allOf(hasOperatorName("&"), argumentCountIs(1), isTypeDependent()), | ||
| unless(hasUnaryOperand(dependentScopeDeclRefExpr()))) | ||
| .bind("match"), | ||
| this); | ||
| } | ||
|
|
||
| void robust_against_operator_ampersand::check(const clang::ast_matchers::MatchFinder::MatchResult& result) { | ||
| if (const auto* call = result.Nodes.getNodeAs< clang::CXXOperatorCallExpr >("match"); call != nullptr) { | ||
| diag(call->getBeginLoc(), "Guard against user provided operator& for dependent types.") | ||
| << clang::FixItHint::CreateReplacement( | ||
| call->getSourceRange(), | ||
| (llvm::Twine( | ||
| "std::addressof(" + clang::tooling::fixit::getText(*call->getArg(0), *result.Context) + ")")) | ||
| .str()); | ||
| } | ||
| } | ||
|
|
||
| } // namespace libcpp | ||
18 changes: 18 additions & 0 deletions
18
libcxx/test/tools/clang_tidy_checks/robust_against_operator_ampersand.hpp
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,18 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 "clang-tidy/ClangTidyCheck.h" | ||
|
|
||
| namespace libcpp { | ||
| class robust_against_operator_ampersand : public clang::tidy::ClangTidyCheck { | ||
| public: | ||
| robust_against_operator_ampersand(llvm::StringRef, clang::tidy::ClangTidyContext*); | ||
| void registerMatchers(clang::ast_matchers::MatchFinder*) override; | ||
| void check(const clang::ast_matchers::MatchFinder::MatchResult&) override; | ||
| }; | ||
| } // namespace libcpp |
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.
This should go into the coding guidelines instead.
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.
I disagree. The code should documented what the intention is.
As for the coding guidelines; it already has this information documented.
(Note testing for
operator,in the same part of the documentation is on my todo list.)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.
I agree with @mordante here, this is documented explicitly in the coding guidelines already:
IMO this comment doesn't provide absolutely necessary information, but it also doesn't hurt. I'd keep it.
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.
The guidelines should still document that this check exists. IMO this comment isn't useful, since it will get out of sync with the guidelines. I'd be much happier with a reference to the guidelines than documenting the same thing in different places.
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.
I would suggest the following, then:
I am mostly neutral on this issue. It's true that we're duplicating a bit of information, but we're really not duplicating a lot of it. I'd let @mordante decide here to avoid blocking this whole patch on such a minor point.
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.
I don't think it's useful still, but I wouldn't block it on this. However, I would block on not mentioning the check in the documentation as we do with all other checks.