1 //===--- UnhandledSelfAssignmentCheck.cpp - clang-tidy --------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "UnhandledSelfAssignmentCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 using namespace clang::ast_matchers
;
15 namespace clang::tidy::bugprone
{
17 UnhandledSelfAssignmentCheck::UnhandledSelfAssignmentCheck(
18 StringRef Name
, ClangTidyContext
*Context
)
19 : ClangTidyCheck(Name
, Context
),
20 WarnOnlyIfThisHasSuspiciousField(
21 Options
.get("WarnOnlyIfThisHasSuspiciousField", true)) {}
23 void UnhandledSelfAssignmentCheck::storeOptions(
24 ClangTidyOptions::OptionMap
&Opts
) {
25 Options
.store(Opts
, "WarnOnlyIfThisHasSuspiciousField",
26 WarnOnlyIfThisHasSuspiciousField
);
29 void UnhandledSelfAssignmentCheck::registerMatchers(MatchFinder
*Finder
) {
30 // We don't care about deleted, default or implicit operator implementations.
31 const auto IsUserDefined
= cxxMethodDecl(
32 isDefinition(), unless(anyOf(isDeleted(), isImplicit(), isDefaulted())));
34 // We don't need to worry when a copy assignment operator gets the other
36 const auto HasReferenceParam
=
37 cxxMethodDecl(hasParameter(0, parmVarDecl(hasType(referenceType()))));
39 // Self-check: Code compares something with 'this' pointer. We don't check
40 // whether it is actually the parameter what we compare.
41 const auto HasNoSelfCheck
= cxxMethodDecl(unless(hasDescendant(
42 binaryOperation(hasAnyOperatorName("==", "!="),
43 hasEitherOperand(ignoringParenCasts(cxxThisExpr()))))));
45 // Both copy-and-swap and copy-and-move method creates a copy first and
46 // assign it to 'this' with swap or move.
47 // In the non-template case, we can search for the copy constructor call.
48 const auto HasNonTemplateSelfCopy
= cxxMethodDecl(
49 ofClass(cxxRecordDecl(unless(hasAncestor(classTemplateDecl())))),
51 hasDescendant(cxxConstructExpr(hasDeclaration(cxxConstructorDecl(
52 isCopyConstructor(), ofClass(equalsBoundNode("class"))))))));
54 // In the template case, we need to handle two separate cases: 1) a local
55 // variable is created with the copy, 2) copy is created only as a temporary
57 const auto HasTemplateSelfCopy
= cxxMethodDecl(
58 ofClass(cxxRecordDecl(hasAncestor(classTemplateDecl()))),
60 varDecl(hasType(cxxRecordDecl(equalsBoundNode("class"))),
61 hasDescendant(parenListExpr()))),
62 hasDescendant(cxxUnresolvedConstructExpr(hasDescendant(declRefExpr(
63 hasType(cxxRecordDecl(equalsBoundNode("class")))))))));
65 // If inside the copy assignment operator another assignment operator is
66 // called on 'this' we assume that self-check might be handled inside
67 // this nested operator.
68 const auto HasNoNestedSelfAssign
=
69 cxxMethodDecl(unless(hasDescendant(cxxMemberCallExpr(callee(cxxMethodDecl(
70 hasName("operator="), ofClass(equalsBoundNode("class"))))))));
72 DeclarationMatcher AdditionalMatcher
= cxxMethodDecl();
73 if (WarnOnlyIfThisHasSuspiciousField
) {
74 // Matcher for standard smart pointers.
75 const auto SmartPointerType
= qualType(hasUnqualifiedDesugaredType(
76 recordType(hasDeclaration(classTemplateSpecializationDecl(
77 anyOf(allOf(hasAnyName("::std::shared_ptr", "::std::weak_ptr",
79 templateArgumentCountIs(1)),
80 allOf(hasName("::std::unique_ptr"),
81 templateArgumentCountIs(2))))))));
83 // We will warn only if the class has a pointer or a C array field which
84 // probably causes a problem during self-assignment (e.g. first resetting
85 // the pointer member, then trying to access the object pointed by the
86 // pointer, or memcpy overlapping arrays).
87 AdditionalMatcher
= cxxMethodDecl(ofClass(cxxRecordDecl(
88 has(fieldDecl(anyOf(hasType(pointerType()), hasType(SmartPointerType
),
89 hasType(arrayType())))))));
92 Finder
->addMatcher(cxxMethodDecl(ofClass(cxxRecordDecl().bind("class")),
93 isCopyAssignmentOperator(), IsUserDefined
,
94 HasReferenceParam
, HasNoSelfCheck
,
95 unless(HasNonTemplateSelfCopy
),
96 unless(HasTemplateSelfCopy
),
97 HasNoNestedSelfAssign
, AdditionalMatcher
)
98 .bind("copyAssignmentOperator"),
102 void UnhandledSelfAssignmentCheck::check(
103 const MatchFinder::MatchResult
&Result
) {
104 const auto *MatchedDecl
=
105 Result
.Nodes
.getNodeAs
<CXXMethodDecl
>("copyAssignmentOperator");
106 diag(MatchedDecl
->getLocation(),
107 "operator=() does not handle self-assignment properly");
110 } // namespace clang::tidy::bugprone