Tamas Zolnai | de7a30c | 2019-05-12 12:23:56 +0000 | [diff] [blame] | 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 { |
| 16 | namespace tidy { |
| 17 | namespace bugprone { |
| 18 | |
Tamas Zolnai | dab31924 | 2019-05-23 20:29:04 +0000 | [diff] [blame] | 19 | UnhandledSelfAssignmentCheck::UnhandledSelfAssignmentCheck( |
| 20 | StringRef Name, ClangTidyContext *Context) |
| 21 | : ClangTidyCheck(Name, Context), |
| 22 | WarnOnlyIfThisHasSuspiciousField( |
| 23 | Options.get("WarnOnlyIfThisHasSuspiciousField", true)) {} |
| 24 | |
| 25 | void UnhandledSelfAssignmentCheck::storeOptions( |
| 26 | ClangTidyOptions::OptionMap &Opts) { |
| 27 | Options.store(Opts, "WarnOnlyIfThisHasSuspiciousField", |
| 28 | WarnOnlyIfThisHasSuspiciousField); |
| 29 | } |
| 30 | |
Tamas Zolnai | de7a30c | 2019-05-12 12:23:56 +0000 | [diff] [blame] | 31 | void UnhandledSelfAssignmentCheck::registerMatchers(MatchFinder *Finder) { |
Tamas Zolnai | de7a30c | 2019-05-12 12:23:56 +0000 | [diff] [blame] | 32 | // We don't care about deleted, default or implicit operator implementations. |
| 33 | const auto IsUserDefined = cxxMethodDecl( |
| 34 | isDefinition(), unless(anyOf(isDeleted(), isImplicit(), isDefaulted()))); |
| 35 | |
| 36 | // We don't need to worry when a copy assignment operator gets the other |
| 37 | // object by value. |
| 38 | const auto HasReferenceParam = |
| 39 | cxxMethodDecl(hasParameter(0, parmVarDecl(hasType(referenceType())))); |
| 40 | |
| 41 | // Self-check: Code compares something with 'this' pointer. We don't check |
| 42 | // whether it is actually the parameter what we compare. |
Tamás Zolnai | 0f9e1e3 | 2020-04-04 17:17:52 +0200 | [diff] [blame^] | 43 | const auto HasNoSelfCheck = cxxMethodDecl(unless(anyOf( |
Nathan James | 97572fa | 2020-03-10 00:42:21 +0000 | [diff] [blame] | 44 | hasDescendant(binaryOperator(hasAnyOperatorName("==", "!="), |
Tamás Zolnai | 0f9e1e3 | 2020-04-04 17:17:52 +0200 | [diff] [blame^] | 45 | has(ignoringParenCasts(cxxThisExpr())))), |
| 46 | hasDescendant(cxxOperatorCallExpr( |
| 47 | hasAnyOverloadedOperatorName("==", "!="), argumentCountIs(2), |
| 48 | has(ignoringParenCasts(cxxThisExpr()))))))); |
Tamas Zolnai | de7a30c | 2019-05-12 12:23:56 +0000 | [diff] [blame] | 49 | |
| 50 | // Both copy-and-swap and copy-and-move method creates a copy first and |
| 51 | // assign it to 'this' with swap or move. |
| 52 | // In the non-template case, we can search for the copy constructor call. |
| 53 | const auto HasNonTemplateSelfCopy = cxxMethodDecl( |
| 54 | ofClass(cxxRecordDecl(unless(hasAncestor(classTemplateDecl())))), |
| 55 | hasDescendant(cxxConstructExpr(hasDeclaration(cxxConstructorDecl( |
| 56 | isCopyConstructor(), ofClass(equalsBoundNode("class"))))))); |
| 57 | |
| 58 | // In the template case, we need to handle two separate cases: 1) a local |
| 59 | // variable is created with the copy, 2) copy is created only as a temporary |
| 60 | // object. |
| 61 | const auto HasTemplateSelfCopy = cxxMethodDecl( |
| 62 | ofClass(cxxRecordDecl(hasAncestor(classTemplateDecl()))), |
| 63 | anyOf(hasDescendant( |
| 64 | varDecl(hasType(cxxRecordDecl(equalsBoundNode("class"))), |
| 65 | hasDescendant(parenListExpr()))), |
| 66 | hasDescendant(cxxUnresolvedConstructExpr(hasDescendant(declRefExpr( |
| 67 | hasType(cxxRecordDecl(equalsBoundNode("class"))))))))); |
| 68 | |
| 69 | // If inside the copy assignment operator another assignment operator is |
| 70 | // called on 'this' we assume that self-check might be handled inside |
| 71 | // this nested operator. |
| 72 | const auto HasNoNestedSelfAssign = |
| 73 | cxxMethodDecl(unless(hasDescendant(cxxMemberCallExpr(callee(cxxMethodDecl( |
| 74 | hasName("operator="), ofClass(equalsBoundNode("class")))))))); |
| 75 | |
Tamas Zolnai | dab31924 | 2019-05-23 20:29:04 +0000 | [diff] [blame] | 76 | DeclarationMatcher AdditionalMatcher = cxxMethodDecl(); |
| 77 | if (WarnOnlyIfThisHasSuspiciousField) { |
| 78 | // Matcher for standard smart pointers. |
| 79 | const auto SmartPointerType = qualType(hasUnqualifiedDesugaredType( |
| 80 | recordType(hasDeclaration(classTemplateSpecializationDecl( |
| 81 | hasAnyName("::std::shared_ptr", "::std::unique_ptr", |
| 82 | "::std::weak_ptr", "::std::auto_ptr"), |
| 83 | templateArgumentCountIs(1)))))); |
Tamas Zolnai | de7a30c | 2019-05-12 12:23:56 +0000 | [diff] [blame] | 84 | |
Tamas Zolnai | dab31924 | 2019-05-23 20:29:04 +0000 | [diff] [blame] | 85 | // We will warn only if the class has a pointer or a C array field which |
| 86 | // probably causes a problem during self-assignment (e.g. first resetting |
| 87 | // the pointer member, then trying to access the object pointed by the |
| 88 | // pointer, or memcpy overlapping arrays). |
| 89 | AdditionalMatcher = cxxMethodDecl(ofClass(cxxRecordDecl( |
| 90 | has(fieldDecl(anyOf(hasType(pointerType()), hasType(SmartPointerType), |
| 91 | hasType(arrayType()))))))); |
| 92 | } |
Tamas Zolnai | de7a30c | 2019-05-12 12:23:56 +0000 | [diff] [blame] | 93 | |
Tamas Zolnai | dab31924 | 2019-05-23 20:29:04 +0000 | [diff] [blame] | 94 | Finder->addMatcher(cxxMethodDecl(ofClass(cxxRecordDecl().bind("class")), |
| 95 | isCopyAssignmentOperator(), IsUserDefined, |
| 96 | HasReferenceParam, HasNoSelfCheck, |
| 97 | unless(HasNonTemplateSelfCopy), |
| 98 | unless(HasTemplateSelfCopy), |
| 99 | HasNoNestedSelfAssign, AdditionalMatcher) |
| 100 | .bind("copyAssignmentOperator"), |
| 101 | this); |
Tamas Zolnai | de7a30c | 2019-05-12 12:23:56 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void UnhandledSelfAssignmentCheck::check( |
| 105 | const MatchFinder::MatchResult &Result) { |
| 106 | const auto *MatchedDecl = |
| 107 | Result.Nodes.getNodeAs<CXXMethodDecl>("copyAssignmentOperator"); |
| 108 | diag(MatchedDecl->getLocation(), |
| 109 | "operator=() does not handle self-assignment properly"); |
| 110 | } |
| 111 | |
| 112 | } // namespace bugprone |
| 113 | } // namespace tidy |
| 114 | } // namespace clang |