-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy] Fix false positive in cppcoreguidelines-avoid-const-or-ref-data-members when detecting templated classes with inheritance #115180
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
HerrCai0907
merged 5 commits into
llvm:main
from
HerrCai0907:111985-cppcoreguidelines-avoid-const-or-ref-data-members-gives-false-positive-when-a-templated-class-inherits
Nov 24, 2024
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
428283c
[clang-tidy] fix false positive when detecting templated classes with…
HerrCai0907 c2e0f7c
fix review
HerrCai0907 bb42e54
Merge branch 'main' of https://github.com/llvm/llvm-project into 1119…
HerrCai0907 deb6e54
fix review
HerrCai0907 993db43
fix review
HerrCai0907 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,79 +13,88 @@ | |
| using namespace clang::ast_matchers; | ||
|
|
||
| namespace clang::tidy::cppcoreguidelines { | ||
| namespace { | ||
|
|
||
| AST_MATCHER(FieldDecl, isMemberOfLambda) { | ||
| return Node.getParent()->isLambda(); | ||
| static bool hasCopyConstructor(CXXRecordDecl const &Node) { | ||
| if (Node.needsOverloadResolutionForCopyConstructor() && | ||
| Node.needsImplicitCopyConstructor()) { | ||
| // unresolved | ||
| for (CXXBaseSpecifier const &BS : Node.bases()) { | ||
| CXXRecordDecl const *BRD = BS.getType()->getAsCXXRecordDecl(); | ||
| if (BRD != nullptr && !hasCopyConstructor(*BRD)) | ||
| return false; | ||
| } | ||
| } | ||
| if (Node.hasSimpleCopyConstructor()) | ||
| return true; | ||
| for (CXXConstructorDecl const *Ctor : Node.ctors()) | ||
| if (Ctor->isCopyConstructor()) | ||
| return !Ctor->isDeleted(); | ||
| return false; | ||
| } | ||
|
|
||
| struct MemberFunctionInfo { | ||
| bool Declared{}; | ||
| bool Deleted{}; | ||
| }; | ||
|
|
||
| struct MemberFunctionPairInfo { | ||
| MemberFunctionInfo Copy{}; | ||
| MemberFunctionInfo Move{}; | ||
| }; | ||
|
|
||
| MemberFunctionPairInfo getConstructorsInfo(CXXRecordDecl const &Node) { | ||
| MemberFunctionPairInfo Constructors{}; | ||
|
|
||
| for (CXXConstructorDecl const *Ctor : Node.ctors()) { | ||
| if (Ctor->isCopyConstructor()) { | ||
| Constructors.Copy.Declared = true; | ||
| if (Ctor->isDeleted()) | ||
| Constructors.Copy.Deleted = true; | ||
| } | ||
| if (Ctor->isMoveConstructor()) { | ||
| Constructors.Move.Declared = true; | ||
| if (Ctor->isDeleted()) | ||
| Constructors.Move.Deleted = true; | ||
| static bool hasMoveConstructor(CXXRecordDecl const &Node) { | ||
| if (Node.needsOverloadResolutionForMoveConstructor() && | ||
| Node.needsImplicitMoveConstructor()) { | ||
| // unresolved | ||
| for (CXXBaseSpecifier const &BS : Node.bases()) { | ||
| CXXRecordDecl const *BRD = BS.getType()->getAsCXXRecordDecl(); | ||
| if (BRD != nullptr && !hasMoveConstructor(*BRD)) | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return Constructors; | ||
| if (Node.hasSimpleMoveConstructor()) | ||
| return true; | ||
| for (CXXConstructorDecl const *Ctor : Node.ctors()) | ||
| if (Ctor->isMoveConstructor()) | ||
| return !Ctor->isDeleted(); | ||
| return false; | ||
| } | ||
|
|
||
| MemberFunctionPairInfo getAssignmentsInfo(CXXRecordDecl const &Node) { | ||
| MemberFunctionPairInfo Assignments{}; | ||
|
|
||
| for (CXXMethodDecl const *Method : Node.methods()) { | ||
| if (Method->isCopyAssignmentOperator()) { | ||
| Assignments.Copy.Declared = true; | ||
| if (Method->isDeleted()) | ||
| Assignments.Copy.Deleted = true; | ||
| static bool hasCopyAssignment(CXXRecordDecl const &Node) { | ||
5chmidti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (Node.needsOverloadResolutionForCopyAssignment() && | ||
| Node.needsImplicitCopyAssignment()) { | ||
| // unresolved | ||
| for (CXXBaseSpecifier const &BS : Node.bases()) { | ||
| CXXRecordDecl const *BRD = BS.getType()->getAsCXXRecordDecl(); | ||
| if (BRD != nullptr && !hasCopyAssignment(*BRD)) | ||
| return false; | ||
| } | ||
| } | ||
| if (Node.hasSimpleCopyAssignment()) | ||
| return true; | ||
| for (CXXMethodDecl const *Method : Node.methods()) | ||
| if (Method->isCopyAssignmentOperator()) | ||
| return !Method->isDeleted(); | ||
| return false; | ||
| } | ||
|
|
||
| if (Method->isMoveAssignmentOperator()) { | ||
| Assignments.Move.Declared = true; | ||
| if (Method->isDeleted()) | ||
| Assignments.Move.Deleted = true; | ||
| static bool hasMoveAssignment(CXXRecordDecl const &Node) { | ||
| if (Node.needsOverloadResolutionForMoveAssignment() && | ||
| Node.needsImplicitMoveAssignment()) { | ||
| // unresolved | ||
| for (CXXBaseSpecifier const &BS : Node.bases()) { | ||
| CXXRecordDecl const *BRD = BS.getType()->getAsCXXRecordDecl(); | ||
| if (BRD != nullptr && !hasMoveAssignment(*BRD)) | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return Assignments; | ||
| if (Node.hasSimpleMoveAssignment()) | ||
| return true; | ||
| for (CXXMethodDecl const *Method : Node.methods()) | ||
| if (Method->isMoveAssignmentOperator()) | ||
| return !Method->isDeleted(); | ||
| return false; | ||
| } | ||
|
|
||
| AST_MATCHER(CXXRecordDecl, isCopyableOrMovable) { | ||
| MemberFunctionPairInfo Constructors = getConstructorsInfo(Node); | ||
| MemberFunctionPairInfo Assignments = getAssignmentsInfo(Node); | ||
| namespace { | ||
|
|
||
| if (Node.hasSimpleCopyConstructor() || | ||
| (Constructors.Copy.Declared && !Constructors.Copy.Deleted)) | ||
| return true; | ||
| if (Node.hasSimpleMoveConstructor() || | ||
| (Constructors.Move.Declared && !Constructors.Move.Deleted)) | ||
| return true; | ||
| if (Node.hasSimpleCopyAssignment() || | ||
| (Assignments.Copy.Declared && !Assignments.Copy.Deleted)) | ||
| return true; | ||
| if (Node.hasSimpleMoveAssignment() || | ||
| (Assignments.Move.Declared && !Assignments.Move.Deleted)) | ||
| return true; | ||
| AST_MATCHER(FieldDecl, isMemberOfLambda) { | ||
| return Node.getParent()->isLambda(); | ||
| } | ||
|
|
||
| return false; | ||
| AST_MATCHER(CXXRecordDecl, isCopyableOrMovable) { | ||
| return hasCopyConstructor(Node) || hasMoveConstructor(Node) || | ||
|
||
| hasCopyAssignment(Node) || hasMoveAssignment(Node); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.