clang-tools 20.0.0git
MoveConstructorInitCheck.cpp
Go to the documentation of this file.
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
10#include "../utils/Matchers.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13
14using namespace clang::ast_matchers;
15
17
19 ClangTidyContext *Context)
20 : ClangTidyCheck(Name, Context) {}
21
23 Finder->addMatcher(
24 traverse(TK_AsIs,
25 cxxConstructorDecl(
26 unless(isImplicit()), isMoveConstructor(),
27 hasAnyConstructorInitializer(
28 cxxCtorInitializer(
29 withInitializer(cxxConstructExpr(hasDeclaration(
30 cxxConstructorDecl(isCopyConstructor())
31 .bind("ctor")))))
32 .bind("move-init")))),
33 this);
34}
35
36void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) {
37 const auto *CopyCtor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
38 const auto *Initializer =
39 Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init");
40
41 // Do not diagnose if the expression used to perform the initialization is a
42 // trivially-copyable type.
43 QualType QT = Initializer->getInit()->getType();
44 if (QT.isTriviallyCopyableType(*Result.Context))
45 return;
46
47 if (QT.isConstQualified())
48 return;
49
50 const auto *RD = QT->getAsCXXRecordDecl();
51 if (RD && RD->isTriviallyCopyable())
52 return;
53
54 // Diagnose when the class type has a move constructor available, but the
55 // ctor-initializer uses the copy constructor instead.
56 const CXXConstructorDecl *Candidate = nullptr;
57 for (const auto *Ctor : CopyCtor->getParent()->ctors()) {
58 if (Ctor->isMoveConstructor() && Ctor->getAccess() <= AS_protected &&
59 !Ctor->isDeleted()) {
60 // The type has a move constructor that is at least accessible to the
61 // initializer.
62 //
63 // FIXME: Determine whether the move constructor is a viable candidate
64 // for the ctor-initializer, perhaps provide a fix-it that suggests
65 // using std::move().
66 Candidate = Ctor;
67 break;
68 }
69 }
70
71 if (Candidate) {
72 // There's a move constructor candidate that the caller probably intended
73 // to call instead.
74 diag(Initializer->getSourceLocation(),
75 "move constructor initializes %select{class member|base class}0 by "
76 "calling a copy constructor")
77 << Initializer->isBaseInitializer();
78 diag(CopyCtor->getLocation(), "copy constructor being called",
79 DiagnosticIDs::Note);
80 diag(Candidate->getLocation(), "candidate move constructor here",
81 DiagnosticIDs::Note);
82 }
83}
84
85} // namespace clang::tidy::performance
llvm::SmallString< 256U > Name
ExpectedMatch Candidate
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
MoveConstructorInitCheck(StringRef Name, ClangTidyContext *Context)