1 | //===--- UnhandledSelfAssignmentCheck.cpp - clang-tidy --------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "UnhandledSelfAssignmentCheck.h" |
10 | #include "clang/AST/ASTContext.h" |
11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
12 | |
13 | using namespace clang::ast_matchers; |
14 | |
15 | namespace clang::tidy::bugprone { |
16 | |
17 | UnhandledSelfAssignmentCheck::UnhandledSelfAssignmentCheck( |
18 | StringRef Name, ClangTidyContext *Context) |
19 | : ClangTidyCheck(Name, Context), |
20 | WarnOnlyIfThisHasSuspiciousField( |
21 | Options.get(LocalName: "WarnOnlyIfThisHasSuspiciousField" , Default: true)) {} |
22 | |
23 | void UnhandledSelfAssignmentCheck::storeOptions( |
24 | ClangTidyOptions::OptionMap &Opts) { |
25 | Options.store(Options&: Opts, LocalName: "WarnOnlyIfThisHasSuspiciousField" , |
26 | Value: WarnOnlyIfThisHasSuspiciousField); |
27 | } |
28 | |
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()))); |
33 | |
34 | // We don't need to worry when a copy assignment operator gets the other |
35 | // object by value. |
36 | const auto HasReferenceParam = |
37 | cxxMethodDecl(hasParameter(N: 0, InnerMatcher: parmVarDecl(hasType(InnerMatcher: referenceType())))); |
38 | |
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(InnerMatcher: ignoringParenCasts(InnerMatcher: cxxThisExpr())))))); |
44 | |
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(InnerMatcher: cxxRecordDecl(unless(hasAncestor(classTemplateDecl())))), |
50 | traverse(TK: TK_AsIs, |
51 | InnerMatcher: hasDescendant(cxxConstructExpr(hasDeclaration(InnerMatcher: cxxConstructorDecl( |
52 | isCopyConstructor(), ofClass(InnerMatcher: equalsBoundNode(ID: "class" )))))))); |
53 | |
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 |
56 | // object. |
57 | const auto HasTemplateSelfCopy = cxxMethodDecl( |
58 | ofClass(InnerMatcher: cxxRecordDecl(hasAncestor(classTemplateDecl()))), |
59 | anyOf(hasDescendant( |
60 | varDecl(hasType(InnerMatcher: cxxRecordDecl(equalsBoundNode(ID: "class" ))), |
61 | hasDescendant(parenListExpr()))), |
62 | hasDescendant(cxxUnresolvedConstructExpr(hasDescendant(declRefExpr( |
63 | hasType(InnerMatcher: cxxRecordDecl(equalsBoundNode(ID: "class" ))))))))); |
64 | |
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(InnerMatcher: cxxMethodDecl( |
70 | hasName(Name: "operator=" ), ofClass(InnerMatcher: equalsBoundNode(ID: "class" )))))))); |
71 | |
72 | DeclarationMatcher AdditionalMatcher = cxxMethodDecl(); |
73 | if (WarnOnlyIfThisHasSuspiciousField) { |
74 | // Matcher for standard smart pointers. |
75 | const auto SmartPointerType = qualType(hasUnqualifiedDesugaredType( |
76 | InnerMatcher: recordType(hasDeclaration(InnerMatcher: classTemplateSpecializationDecl( |
77 | hasAnyName("::std::shared_ptr" , "::std::unique_ptr" , |
78 | "::std::weak_ptr" , "::std::auto_ptr" ), |
79 | templateArgumentCountIs(N: 1)))))); |
80 | |
81 | // We will warn only if the class has a pointer or a C array field which |
82 | // probably causes a problem during self-assignment (e.g. first resetting |
83 | // the pointer member, then trying to access the object pointed by the |
84 | // pointer, or memcpy overlapping arrays). |
85 | AdditionalMatcher = cxxMethodDecl(ofClass(InnerMatcher: cxxRecordDecl( |
86 | has(fieldDecl(anyOf(hasType(InnerMatcher: pointerType()), hasType(InnerMatcher: SmartPointerType), |
87 | hasType(InnerMatcher: arrayType()))))))); |
88 | } |
89 | |
90 | Finder->addMatcher(NodeMatch: cxxMethodDecl(ofClass(InnerMatcher: cxxRecordDecl().bind(ID: "class" )), |
91 | isCopyAssignmentOperator(), IsUserDefined, |
92 | HasReferenceParam, HasNoSelfCheck, |
93 | unless(HasNonTemplateSelfCopy), |
94 | unless(HasTemplateSelfCopy), |
95 | HasNoNestedSelfAssign, AdditionalMatcher) |
96 | .bind(ID: "copyAssignmentOperator" ), |
97 | Action: this); |
98 | } |
99 | |
100 | void UnhandledSelfAssignmentCheck::check( |
101 | const MatchFinder::MatchResult &Result) { |
102 | const auto *MatchedDecl = |
103 | Result.Nodes.getNodeAs<CXXMethodDecl>(ID: "copyAssignmentOperator" ); |
104 | diag(MatchedDecl->getLocation(), |
105 | "operator=() does not handle self-assignment properly" ); |
106 | } |
107 | |
108 | } // namespace clang::tidy::bugprone |
109 | |