blob: f2de9fbde2a6ab72cbb24dae156350a42dbd41aa [file] [log] [blame]
Tamas Zolnaide7a30c2019-05-12 12:23:56 +00001//===--- 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
13using namespace clang::ast_matchers;
14
15namespace clang {
16namespace tidy {
17namespace bugprone {
18
Tamas Zolnaidab319242019-05-23 20:29:04 +000019UnhandledSelfAssignmentCheck::UnhandledSelfAssignmentCheck(
20 StringRef Name, ClangTidyContext *Context)
21 : ClangTidyCheck(Name, Context),
22 WarnOnlyIfThisHasSuspiciousField(
23 Options.get("WarnOnlyIfThisHasSuspiciousField", true)) {}
24
25void UnhandledSelfAssignmentCheck::storeOptions(
26 ClangTidyOptions::OptionMap &Opts) {
27 Options.store(Opts, "WarnOnlyIfThisHasSuspiciousField",
28 WarnOnlyIfThisHasSuspiciousField);
29}
30
Tamas Zolnaide7a30c2019-05-12 12:23:56 +000031void UnhandledSelfAssignmentCheck::registerMatchers(MatchFinder *Finder) {
Tamas Zolnaide7a30c2019-05-12 12:23:56 +000032 // 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.
Nathan James97572fa2020-03-10 00:42:21 +000043 const auto HasNoSelfCheck = cxxMethodDecl(unless(
44 hasDescendant(binaryOperator(hasAnyOperatorName("==", "!="),
45 has(ignoringParenCasts(cxxThisExpr()))))));
Tamas Zolnaide7a30c2019-05-12 12:23:56 +000046
47 // Both copy-and-swap and copy-and-move method creates a copy first and
48 // assign it to 'this' with swap or move.
49 // In the non-template case, we can search for the copy constructor call.
50 const auto HasNonTemplateSelfCopy = cxxMethodDecl(
51 ofClass(cxxRecordDecl(unless(hasAncestor(classTemplateDecl())))),
52 hasDescendant(cxxConstructExpr(hasDeclaration(cxxConstructorDecl(
53 isCopyConstructor(), ofClass(equalsBoundNode("class")))))));
54
55 // In the template case, we need to handle two separate cases: 1) a local
56 // variable is created with the copy, 2) copy is created only as a temporary
57 // object.
58 const auto HasTemplateSelfCopy = cxxMethodDecl(
59 ofClass(cxxRecordDecl(hasAncestor(classTemplateDecl()))),
60 anyOf(hasDescendant(
61 varDecl(hasType(cxxRecordDecl(equalsBoundNode("class"))),
62 hasDescendant(parenListExpr()))),
63 hasDescendant(cxxUnresolvedConstructExpr(hasDescendant(declRefExpr(
64 hasType(cxxRecordDecl(equalsBoundNode("class")))))))));
65
66 // If inside the copy assignment operator another assignment operator is
67 // called on 'this' we assume that self-check might be handled inside
68 // this nested operator.
69 const auto HasNoNestedSelfAssign =
70 cxxMethodDecl(unless(hasDescendant(cxxMemberCallExpr(callee(cxxMethodDecl(
71 hasName("operator="), ofClass(equalsBoundNode("class"))))))));
72
Tamas Zolnaidab319242019-05-23 20:29:04 +000073 DeclarationMatcher AdditionalMatcher = cxxMethodDecl();
74 if (WarnOnlyIfThisHasSuspiciousField) {
75 // Matcher for standard smart pointers.
76 const auto SmartPointerType = qualType(hasUnqualifiedDesugaredType(
77 recordType(hasDeclaration(classTemplateSpecializationDecl(
78 hasAnyName("::std::shared_ptr", "::std::unique_ptr",
79 "::std::weak_ptr", "::std::auto_ptr"),
80 templateArgumentCountIs(1))))));
Tamas Zolnaide7a30c2019-05-12 12:23:56 +000081
Tamas Zolnaidab319242019-05-23 20:29:04 +000082 // We will warn only if the class has a pointer or a C array field which
83 // probably causes a problem during self-assignment (e.g. first resetting
84 // the pointer member, then trying to access the object pointed by the
85 // pointer, or memcpy overlapping arrays).
86 AdditionalMatcher = cxxMethodDecl(ofClass(cxxRecordDecl(
87 has(fieldDecl(anyOf(hasType(pointerType()), hasType(SmartPointerType),
88 hasType(arrayType())))))));
89 }
Tamas Zolnaide7a30c2019-05-12 12:23:56 +000090
Tamas Zolnaidab319242019-05-23 20:29:04 +000091 Finder->addMatcher(cxxMethodDecl(ofClass(cxxRecordDecl().bind("class")),
92 isCopyAssignmentOperator(), IsUserDefined,
93 HasReferenceParam, HasNoSelfCheck,
94 unless(HasNonTemplateSelfCopy),
95 unless(HasTemplateSelfCopy),
96 HasNoNestedSelfAssign, AdditionalMatcher)
97 .bind("copyAssignmentOperator"),
98 this);
Tamas Zolnaide7a30c2019-05-12 12:23:56 +000099}
100
101void UnhandledSelfAssignmentCheck::check(
102 const MatchFinder::MatchResult &Result) {
103 const auto *MatchedDecl =
104 Result.Nodes.getNodeAs<CXXMethodDecl>("copyAssignmentOperator");
105 diag(MatchedDecl->getLocation(),
106 "operator=() does not handle self-assignment properly");
107}
108
109} // namespace bugprone
110} // namespace tidy
111} // namespace clang