blob: ba182f3962ebe84a0cc0da948d365d0ca36ac5c2 [file] [log] [blame]
Aaron Ballman9392ced2015-08-20 15:52:52 +00001//===--- MoveConstructorInitCheck.cpp - clang-tidy-------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "MoveConstructorInitCheck.h"
Aaron Ballmanfc4e0422015-10-06 16:27:03 +000011#include "../utils/Matchers.h"
Aaron Ballman9392ced2015-08-20 15:52:52 +000012#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
Aaron Ballmanfc4e0422015-10-06 16:27:03 +000014#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Lex/Lexer.h"
16#include "clang/Lex/Preprocessor.h"
Aaron Ballman9392ced2015-08-20 15:52:52 +000017
18using namespace clang::ast_matchers;
19
20namespace clang {
21namespace tidy {
22
Aaron Ballmanfc4e0422015-10-06 16:27:03 +000023namespace {
24
25unsigned int
26parmVarDeclRefExprOccurences(const ParmVarDecl &MovableParam,
27 const CXXConstructorDecl &ConstructorDecl,
28 ASTContext &Context) {
29 unsigned int Occurrences = 0;
30 auto AllDeclRefs =
31 findAll(declRefExpr(to(parmVarDecl(equalsNode(&MovableParam)))));
32 Occurrences += match(AllDeclRefs, *ConstructorDecl.getBody(), Context).size();
33 for (const auto *Initializer : ConstructorDecl.inits()) {
34 Occurrences += match(AllDeclRefs, *Initializer->getInit(), Context).size();
35 }
36 return Occurrences;
37}
38
39} // namespace
40
41MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef Name,
42 ClangTidyContext *Context)
43 : ClangTidyCheck(Name, Context),
44 IncludeStyle(IncludeSorter::parseIncludeStyle(
45 Options.get("IncludeStyle", "llvm"))) {}
46
Aaron Ballman9392ced2015-08-20 15:52:52 +000047void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballman327e97b2015-08-28 19:27:19 +000048 // Only register the matchers for C++11; the functionality currently does not
49 // provide any benefit to other languages, despite being benign.
Aaron Ballmanbf891092015-08-31 15:28:57 +000050 if (!getLangOpts().CPlusPlus11)
51 return;
52
53 Finder->addMatcher(
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000054 cxxConstructorDecl(
55 unless(isImplicit()),
56 allOf(isMoveConstructor(),
57 hasAnyConstructorInitializer(
58 cxxCtorInitializer(
59 withInitializer(cxxConstructExpr(hasDeclaration(
60 cxxConstructorDecl(isCopyConstructor())
61 .bind("ctor")))))
Aaron Ballmanfc4e0422015-10-06 16:27:03 +000062 .bind("move-init")))),
63 this);
64
65 auto NonConstValueMovableAndExpensiveToCopy =
66 qualType(allOf(unless(pointerType()), unless(isConstQualified()),
67 hasDeclaration(cxxRecordDecl(hasMethod(cxxConstructorDecl(
68 isMoveConstructor(), unless(isDeleted()))))),
69 matchers::isExpensiveToCopy()));
70 Finder->addMatcher(
71 cxxConstructorDecl(
72 allOf(
73 unless(isMoveConstructor()),
74 hasAnyConstructorInitializer(withInitializer(cxxConstructExpr(
75 hasDeclaration(cxxConstructorDecl(isCopyConstructor())),
76 hasArgument(
77 0, declRefExpr(
78 to(parmVarDecl(
79 hasType(
80 NonConstValueMovableAndExpensiveToCopy))
81 .bind("movable-param")))
82 .bind("init-arg")))))))
83 .bind("ctor-decl"),
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000084 this);
Aaron Ballman9392ced2015-08-20 15:52:52 +000085}
86
87void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) {
Aaron Ballmanfc4e0422015-10-06 16:27:03 +000088 if (Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init") != nullptr)
89 handleMoveConstructor(Result);
90 if (Result.Nodes.getNodeAs<ParmVarDecl>("movable-param") != nullptr)
91 handleParamNotMoved(Result);
92}
93
94void MoveConstructorInitCheck::handleParamNotMoved(
95 const MatchFinder::MatchResult &Result) {
96 const auto *MovableParam =
97 Result.Nodes.getNodeAs<ParmVarDecl>("movable-param");
98 const auto *ConstructorDecl =
99 Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor-decl");
100 const auto *InitArg = Result.Nodes.getNodeAs<DeclRefExpr>("init-arg");
101 // If the parameter is referenced more than once it is not safe to move it.
102 if (parmVarDeclRefExprOccurences(*MovableParam, *ConstructorDecl,
103 *Result.Context) > 1)
104 return;
105 auto DiagOut =
106 diag(InitArg->getLocStart(), "value argument can be moved to avoid copy");
107 DiagOut << FixItHint::CreateReplacement(
108 InitArg->getSourceRange(),
109 (Twine("std::move(") + MovableParam->getName() + ")").str());
110 if (auto IncludeFixit = Inserter->CreateIncludeInsertion(
111 Result.SourceManager->getFileID(InitArg->getLocStart()), "utility",
112 /*IsAngled=*/true)) {
113 DiagOut << *IncludeFixit;
114 }
115}
116
117void MoveConstructorInitCheck::handleMoveConstructor(
118 const MatchFinder::MatchResult &Result) {
Aaron Ballman9392ced2015-08-20 15:52:52 +0000119 const auto *CopyCtor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
Aaron Ballmanfc4e0422015-10-06 16:27:03 +0000120 const auto *Initializer = Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init");
Aaron Ballman9392ced2015-08-20 15:52:52 +0000121
122 // Do not diagnose if the expression used to perform the initialization is a
123 // trivially-copyable type.
124 QualType QT = Initializer->getInit()->getType();
125 if (QT.isTriviallyCopyableType(*Result.Context))
126 return;
127
128 const auto *RD = QT->getAsCXXRecordDecl();
129 if (RD && RD->isTriviallyCopyable())
130 return;
131
132 // Diagnose when the class type has a move constructor available, but the
133 // ctor-initializer uses the copy constructor instead.
134 const CXXConstructorDecl *Candidate = nullptr;
135 for (const auto *Ctor : CopyCtor->getParent()->ctors()) {
136 if (Ctor->isMoveConstructor() && Ctor->getAccess() <= AS_protected &&
137 !Ctor->isDeleted()) {
138 // The type has a move constructor that is at least accessible to the
139 // initializer.
140 //
141 // FIXME: Determine whether the move constructor is a viable candidate
142 // for the ctor-initializer, perhaps provide a fixit that suggests
143 // using std::move().
144 Candidate = Ctor;
145 break;
146 }
147 }
148
149 if (Candidate) {
150 // There's a move constructor candidate that the caller probably intended
151 // to call instead.
152 diag(Initializer->getSourceLocation(),
153 "move constructor initializes %0 by calling a copy constructor")
154 << (Initializer->isBaseInitializer() ? "base class" : "class member");
155 diag(CopyCtor->getLocation(), "copy constructor being called",
156 DiagnosticIDs::Note);
157 diag(Candidate->getLocation(), "candidate move constructor here",
158 DiagnosticIDs::Note);
159 }
160}
161
Aaron Ballmanfc4e0422015-10-06 16:27:03 +0000162void MoveConstructorInitCheck::registerPPCallbacks(CompilerInstance &Compiler) {
163 Inserter.reset(new IncludeInserter(Compiler.getSourceManager(),
164 Compiler.getLangOpts(), IncludeStyle));
165 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
166}
167
168void MoveConstructorInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
169 Options.store(Opts, "IncludeStyle", IncludeSorter::toString(IncludeStyle));
170}
171
Aaron Ballman9392ced2015-08-20 15:52:52 +0000172} // namespace tidy
173} // namespace clang