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