| 1 | //===--- MoveConstructorInitCheck.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 "MoveConstructorInitCheck.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::performance { |
| 16 | |
| 17 | MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef Name, |
| 18 | ClangTidyContext *Context) |
| 19 | : ClangTidyCheck(Name, Context) {} |
| 20 | |
| 21 | void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) { |
| 22 | Finder->addMatcher( |
| 23 | NodeMatch: traverse(TK: TK_AsIs, |
| 24 | InnerMatcher: cxxConstructorDecl( |
| 25 | unless(isImplicit()), isMoveConstructor(), |
| 26 | hasAnyConstructorInitializer( |
| 27 | InnerMatcher: cxxCtorInitializer( |
| 28 | withInitializer(InnerMatcher: cxxConstructExpr(hasDeclaration( |
| 29 | InnerMatcher: cxxConstructorDecl(isCopyConstructor()) |
| 30 | .bind(ID: "ctor" ))))) |
| 31 | .bind(ID: "move-init" )))), |
| 32 | Action: this); |
| 33 | } |
| 34 | |
| 35 | void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) { |
| 36 | const auto *CopyCtor = Result.Nodes.getNodeAs<CXXConstructorDecl>(ID: "ctor" ); |
| 37 | const auto *Initializer = |
| 38 | Result.Nodes.getNodeAs<CXXCtorInitializer>(ID: "move-init" ); |
| 39 | |
| 40 | // Do not diagnose if the expression used to perform the initialization is a |
| 41 | // trivially-copyable type. |
| 42 | QualType QT = Initializer->getInit()->getType(); |
| 43 | if (QT.isTriviallyCopyableType(Context: *Result.Context)) |
| 44 | return; |
| 45 | |
| 46 | if (QT.isConstQualified()) |
| 47 | return; |
| 48 | |
| 49 | const auto *RD = QT->getAsCXXRecordDecl(); |
| 50 | if (RD && RD->isTriviallyCopyable()) |
| 51 | return; |
| 52 | |
| 53 | // Diagnose when the class type has a move constructor available, but the |
| 54 | // ctor-initializer uses the copy constructor instead. |
| 55 | const CXXConstructorDecl *Candidate = nullptr; |
| 56 | for (const auto *Ctor : CopyCtor->getParent()->ctors()) { |
| 57 | if (Ctor->isMoveConstructor() && Ctor->getAccess() <= AS_protected && |
| 58 | !Ctor->isDeleted()) { |
| 59 | // The type has a move constructor that is at least accessible to the |
| 60 | // initializer. |
| 61 | // |
| 62 | // FIXME: Determine whether the move constructor is a viable candidate |
| 63 | // for the ctor-initializer, perhaps provide a fix-it that suggests |
| 64 | // using std::move(). |
| 65 | Candidate = Ctor; |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (Candidate) { |
| 71 | // There's a move constructor candidate that the caller probably intended |
| 72 | // to call instead. |
| 73 | diag(Loc: Initializer->getSourceLocation(), |
| 74 | Description: "move constructor initializes %select{class member|base class}0 by " |
| 75 | "calling a copy constructor" ) |
| 76 | << Initializer->isBaseInitializer(); |
| 77 | diag(CopyCtor->getLocation(), "copy constructor being called" , |
| 78 | DiagnosticIDs::Note); |
| 79 | diag(Candidate->getLocation(), "candidate move constructor here" , |
| 80 | DiagnosticIDs::Note); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | } // namespace clang::tidy::performance |
| 85 | |