1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
//===--- MoveConstructorInitCheck.cpp - clang-tidy-------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "MoveConstructorInitCheck.h"
#include "../utils/Matchers.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang::tidy::performance {
MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
traverse(TK_AsIs,
cxxConstructorDecl(
unless(isImplicit()), isMoveConstructor(),
hasAnyConstructorInitializer(
cxxCtorInitializer(
withInitializer(cxxConstructExpr(hasDeclaration(
cxxConstructorDecl(isCopyConstructor())
.bind("ctor")))))
.bind("move-init")))),
this);
}
void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) {
const auto *CopyCtor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
const auto *Initializer =
Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init");
// Do not diagnose if the expression used to perform the initialization is a
// trivially-copyable type.
QualType QT = Initializer->getInit()->getType();
if (QT.isTriviallyCopyableType(*Result.Context))
return;
if (QT.isConstQualified())
return;
const auto *RD = QT->getAsCXXRecordDecl();
if (RD && RD->isTriviallyCopyable())
return;
// Diagnose when the class type has a move constructor available, but the
// ctor-initializer uses the copy constructor instead.
const CXXConstructorDecl *Candidate = nullptr;
for (const auto *Ctor : CopyCtor->getParent()->ctors()) {
if (Ctor->isMoveConstructor() && Ctor->getAccess() <= AS_protected &&
!Ctor->isDeleted()) {
// The type has a move constructor that is at least accessible to the
// initializer.
//
// FIXME: Determine whether the move constructor is a viable candidate
// for the ctor-initializer, perhaps provide a fix-it that suggests
// using std::move().
Candidate = Ctor;
break;
}
}
if (Candidate) {
// There's a move constructor candidate that the caller probably intended
// to call instead.
diag(Initializer->getSourceLocation(),
"move constructor initializes %select{class member|base class}0 by "
"calling a copy constructor")
<< Initializer->isBaseInitializer();
diag(CopyCtor->getLocation(), "copy constructor being called",
DiagnosticIDs::Note);
diag(Candidate->getLocation(), "candidate move constructor here",
DiagnosticIDs::Note);
}
}
} // namespace clang::tidy::performance
|