blob: b27918cde0a30fbdd2c043d0635abe2ec13f5cf4 [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 {
Etienne Bergeron456177b2016-05-02 18:00:29 +000022namespace misc {
Aaron Ballman9392ced2015-08-20 15:52:52 +000023
Aaron Ballmanfc4e0422015-10-06 16:27:03 +000024namespace {
25
26unsigned int
27parmVarDeclRefExprOccurences(const ParmVarDecl &MovableParam,
28 const CXXConstructorDecl &ConstructorDecl,
29 ASTContext &Context) {
30 unsigned int Occurrences = 0;
31 auto AllDeclRefs =
32 findAll(declRefExpr(to(parmVarDecl(equalsNode(&MovableParam)))));
33 Occurrences += match(AllDeclRefs, *ConstructorDecl.getBody(), Context).size();
34 for (const auto *Initializer : ConstructorDecl.inits()) {
35 Occurrences += match(AllDeclRefs, *Initializer->getInit(), Context).size();
36 }
37 return Occurrences;
38}
39
40} // namespace
41
42MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef Name,
43 ClangTidyContext *Context)
44 : ClangTidyCheck(Name, Context),
Etienne Bergeron2a4c00f2016-05-03 02:54:05 +000045 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
Aaron Ballmanc00ad6c2016-01-08 15:50:51 +000046 Options.get("IncludeStyle", "llvm"))),
Alexander Kornienko5d08bb72016-05-20 13:42:40 +000047 UseCERTSemantics(Options.get("UseCERTSemantics", 0) != 0) {}
Aaron Ballmanfc4e0422015-10-06 16:27:03 +000048
Aaron Ballman9392ced2015-08-20 15:52:52 +000049void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballman327e97b2015-08-28 19:27:19 +000050 // Only register the matchers for C++11; the functionality currently does not
51 // provide any benefit to other languages, despite being benign.
Aaron Ballmanbf891092015-08-31 15:28:57 +000052 if (!getLangOpts().CPlusPlus11)
53 return;
54
55 Finder->addMatcher(
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000056 cxxConstructorDecl(
57 unless(isImplicit()),
58 allOf(isMoveConstructor(),
59 hasAnyConstructorInitializer(
60 cxxCtorInitializer(
61 withInitializer(cxxConstructExpr(hasDeclaration(
62 cxxConstructorDecl(isCopyConstructor())
63 .bind("ctor")))))
Aaron Ballmanfc4e0422015-10-06 16:27:03 +000064 .bind("move-init")))),
65 this);
66
67 auto NonConstValueMovableAndExpensiveToCopy =
68 qualType(allOf(unless(pointerType()), unless(isConstQualified()),
69 hasDeclaration(cxxRecordDecl(hasMethod(cxxConstructorDecl(
70 isMoveConstructor(), unless(isDeleted()))))),
71 matchers::isExpensiveToCopy()));
Aaron Ballmanc00ad6c2016-01-08 15:50:51 +000072
73 // This checker is also used to implement cert-oop11-cpp, but when using that
74 // form of the checker, we do not want to diagnose movable parameters.
Alexander Kornienko5d08bb72016-05-20 13:42:40 +000075 if (!UseCERTSemantics) {
Aaron Ballmanc00ad6c2016-01-08 15:50:51 +000076 Finder->addMatcher(
77 cxxConstructorDecl(
78 allOf(
79 unless(isMoveConstructor()),
80 hasAnyConstructorInitializer(withInitializer(cxxConstructExpr(
81 hasDeclaration(cxxConstructorDecl(isCopyConstructor())),
82 hasArgument(
83 0,
84 declRefExpr(
85 to(parmVarDecl(
86 hasType(
87 NonConstValueMovableAndExpensiveToCopy))
88 .bind("movable-param")))
89 .bind("init-arg")))))))
90 .bind("ctor-decl"),
91 this);
Alexander Kornienko5d08bb72016-05-20 13:42:40 +000092 }
Aaron Ballman9392ced2015-08-20 15:52:52 +000093}
94
95void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) {
Aaron Ballmanfc4e0422015-10-06 16:27:03 +000096 if (Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init") != nullptr)
97 handleMoveConstructor(Result);
98 if (Result.Nodes.getNodeAs<ParmVarDecl>("movable-param") != nullptr)
99 handleParamNotMoved(Result);
100}
101
102void MoveConstructorInitCheck::handleParamNotMoved(
103 const MatchFinder::MatchResult &Result) {
104 const auto *MovableParam =
105 Result.Nodes.getNodeAs<ParmVarDecl>("movable-param");
106 const auto *ConstructorDecl =
107 Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor-decl");
108 const auto *InitArg = Result.Nodes.getNodeAs<DeclRefExpr>("init-arg");
109 // If the parameter is referenced more than once it is not safe to move it.
110 if (parmVarDeclRefExprOccurences(*MovableParam, *ConstructorDecl,
111 *Result.Context) > 1)
112 return;
Felix Berger99a0ddd2016-05-03 23:07:44 +0000113 auto DiagOut = diag(InitArg->getLocStart(),
114 "value argument %0 can be moved to avoid copy")
115 << MovableParam;
Aaron Ballmanfc4e0422015-10-06 16:27:03 +0000116 DiagOut << FixItHint::CreateReplacement(
117 InitArg->getSourceRange(),
118 (Twine("std::move(") + MovableParam->getName() + ")").str());
119 if (auto IncludeFixit = Inserter->CreateIncludeInsertion(
120 Result.SourceManager->getFileID(InitArg->getLocStart()), "utility",
121 /*IsAngled=*/true)) {
122 DiagOut << *IncludeFixit;
123 }
124}
125
126void MoveConstructorInitCheck::handleMoveConstructor(
127 const MatchFinder::MatchResult &Result) {
Aaron Ballman9392ced2015-08-20 15:52:52 +0000128 const auto *CopyCtor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000129 const auto *Initializer =
130 Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init");
Aaron Ballman9392ced2015-08-20 15:52:52 +0000131
132 // Do not diagnose if the expression used to perform the initialization is a
133 // trivially-copyable type.
134 QualType QT = Initializer->getInit()->getType();
135 if (QT.isTriviallyCopyableType(*Result.Context))
136 return;
Kirill Bobyrev11cea452016-08-01 12:06:18 +0000137
Aaron Ballman9392ced2015-08-20 15:52:52 +0000138 const auto *RD = QT->getAsCXXRecordDecl();
139 if (RD && RD->isTriviallyCopyable())
140 return;
141
142 // Diagnose when the class type has a move constructor available, but the
143 // ctor-initializer uses the copy constructor instead.
144 const CXXConstructorDecl *Candidate = nullptr;
145 for (const auto *Ctor : CopyCtor->getParent()->ctors()) {
146 if (Ctor->isMoveConstructor() && Ctor->getAccess() <= AS_protected &&
147 !Ctor->isDeleted()) {
148 // The type has a move constructor that is at least accessible to the
149 // initializer.
150 //
151 // FIXME: Determine whether the move constructor is a viable candidate
152 // for the ctor-initializer, perhaps provide a fixit that suggests
153 // using std::move().
154 Candidate = Ctor;
155 break;
156 }
157 }
158
159 if (Candidate) {
160 // There's a move constructor candidate that the caller probably intended
161 // to call instead.
162 diag(Initializer->getSourceLocation(),
163 "move constructor initializes %0 by calling a copy constructor")
164 << (Initializer->isBaseInitializer() ? "base class" : "class member");
165 diag(CopyCtor->getLocation(), "copy constructor being called",
166 DiagnosticIDs::Note);
167 diag(Candidate->getLocation(), "candidate move constructor here",
168 DiagnosticIDs::Note);
169 }
170}
171
Aaron Ballmanfc4e0422015-10-06 16:27:03 +0000172void MoveConstructorInitCheck::registerPPCallbacks(CompilerInstance &Compiler) {
Etienne Bergeron2a4c00f2016-05-03 02:54:05 +0000173 Inserter.reset(new utils::IncludeInserter(
174 Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
Aaron Ballmanfc4e0422015-10-06 16:27:03 +0000175 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
176}
177
178void MoveConstructorInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Etienne Bergeron2a4c00f2016-05-03 02:54:05 +0000179 Options.store(Opts, "IncludeStyle",
180 utils::IncludeSorter::toString(IncludeStyle));
Alexander Kornienko5d08bb72016-05-20 13:42:40 +0000181 Options.store(Opts, "UseCERTSemantics", UseCERTSemantics ? 1 : 0);
Aaron Ballmanfc4e0422015-10-06 16:27:03 +0000182}
183
Etienne Bergeron456177b2016-05-02 18:00:29 +0000184} // namespace misc
Aaron Ballman9392ced2015-08-20 15:52:52 +0000185} // namespace tidy
186} // namespace clang