blob: eeebcf692bc8550b562802959fd99ee1b1c52c2d [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(
Aaron Ballmanc00ad6c2016-01-08 15:50:51 +000045 Options.get("IncludeStyle", "llvm"))),
46 UseCERTSemantics(Context->isCheckEnabled("cert-oop11-cpp")) {}
Aaron Ballmanfc4e0422015-10-06 16:27:03 +000047
Aaron Ballman9392ced2015-08-20 15:52:52 +000048void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballman327e97b2015-08-28 19:27:19 +000049 // Only register the matchers for C++11; the functionality currently does not
50 // provide any benefit to other languages, despite being benign.
Aaron Ballmanbf891092015-08-31 15:28:57 +000051 if (!getLangOpts().CPlusPlus11)
52 return;
53
54 Finder->addMatcher(
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000055 cxxConstructorDecl(
56 unless(isImplicit()),
57 allOf(isMoveConstructor(),
58 hasAnyConstructorInitializer(
59 cxxCtorInitializer(
60 withInitializer(cxxConstructExpr(hasDeclaration(
61 cxxConstructorDecl(isCopyConstructor())
62 .bind("ctor")))))
Aaron Ballmanfc4e0422015-10-06 16:27:03 +000063 .bind("move-init")))),
64 this);
65
66 auto NonConstValueMovableAndExpensiveToCopy =
67 qualType(allOf(unless(pointerType()), unless(isConstQualified()),
68 hasDeclaration(cxxRecordDecl(hasMethod(cxxConstructorDecl(
69 isMoveConstructor(), unless(isDeleted()))))),
70 matchers::isExpensiveToCopy()));
Aaron Ballmanc00ad6c2016-01-08 15:50:51 +000071
72 // This checker is also used to implement cert-oop11-cpp, but when using that
73 // form of the checker, we do not want to diagnose movable parameters.
74 if (!UseCERTSemantics)
75 Finder->addMatcher(
76 cxxConstructorDecl(
77 allOf(
78 unless(isMoveConstructor()),
79 hasAnyConstructorInitializer(withInitializer(cxxConstructExpr(
80 hasDeclaration(cxxConstructorDecl(isCopyConstructor())),
81 hasArgument(
82 0,
83 declRefExpr(
84 to(parmVarDecl(
85 hasType(
86 NonConstValueMovableAndExpensiveToCopy))
87 .bind("movable-param")))
88 .bind("init-arg")))))))
89 .bind("ctor-decl"),
90 this);
Aaron Ballman9392ced2015-08-20 15:52:52 +000091}
92
93void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) {
Aaron Ballmanfc4e0422015-10-06 16:27:03 +000094 if (Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init") != nullptr)
95 handleMoveConstructor(Result);
96 if (Result.Nodes.getNodeAs<ParmVarDecl>("movable-param") != nullptr)
97 handleParamNotMoved(Result);
98}
99
100void MoveConstructorInitCheck::handleParamNotMoved(
101 const MatchFinder::MatchResult &Result) {
102 const auto *MovableParam =
103 Result.Nodes.getNodeAs<ParmVarDecl>("movable-param");
104 const auto *ConstructorDecl =
105 Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor-decl");
106 const auto *InitArg = Result.Nodes.getNodeAs<DeclRefExpr>("init-arg");
107 // If the parameter is referenced more than once it is not safe to move it.
108 if (parmVarDeclRefExprOccurences(*MovableParam, *ConstructorDecl,
109 *Result.Context) > 1)
110 return;
111 auto DiagOut =
112 diag(InitArg->getLocStart(), "value argument can be moved to avoid copy");
113 DiagOut << FixItHint::CreateReplacement(
114 InitArg->getSourceRange(),
115 (Twine("std::move(") + MovableParam->getName() + ")").str());
116 if (auto IncludeFixit = Inserter->CreateIncludeInsertion(
117 Result.SourceManager->getFileID(InitArg->getLocStart()), "utility",
118 /*IsAngled=*/true)) {
119 DiagOut << *IncludeFixit;
120 }
121}
122
123void MoveConstructorInitCheck::handleMoveConstructor(
124 const MatchFinder::MatchResult &Result) {
Aaron Ballman9392ced2015-08-20 15:52:52 +0000125 const auto *CopyCtor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
Aaron Ballmanfc4e0422015-10-06 16:27:03 +0000126 const auto *Initializer = Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init");
Aaron Ballman9392ced2015-08-20 15:52:52 +0000127
128 // Do not diagnose if the expression used to perform the initialization is a
129 // trivially-copyable type.
130 QualType QT = Initializer->getInit()->getType();
131 if (QT.isTriviallyCopyableType(*Result.Context))
132 return;
133
134 const auto *RD = QT->getAsCXXRecordDecl();
135 if (RD && RD->isTriviallyCopyable())
136 return;
137
138 // Diagnose when the class type has a move constructor available, but the
139 // ctor-initializer uses the copy constructor instead.
140 const CXXConstructorDecl *Candidate = nullptr;
141 for (const auto *Ctor : CopyCtor->getParent()->ctors()) {
142 if (Ctor->isMoveConstructor() && Ctor->getAccess() <= AS_protected &&
143 !Ctor->isDeleted()) {
144 // The type has a move constructor that is at least accessible to the
145 // initializer.
146 //
147 // FIXME: Determine whether the move constructor is a viable candidate
148 // for the ctor-initializer, perhaps provide a fixit that suggests
149 // using std::move().
150 Candidate = Ctor;
151 break;
152 }
153 }
154
155 if (Candidate) {
156 // There's a move constructor candidate that the caller probably intended
157 // to call instead.
158 diag(Initializer->getSourceLocation(),
159 "move constructor initializes %0 by calling a copy constructor")
160 << (Initializer->isBaseInitializer() ? "base class" : "class member");
161 diag(CopyCtor->getLocation(), "copy constructor being called",
162 DiagnosticIDs::Note);
163 diag(Candidate->getLocation(), "candidate move constructor here",
164 DiagnosticIDs::Note);
165 }
166}
167
Aaron Ballmanfc4e0422015-10-06 16:27:03 +0000168void MoveConstructorInitCheck::registerPPCallbacks(CompilerInstance &Compiler) {
169 Inserter.reset(new IncludeInserter(Compiler.getSourceManager(),
170 Compiler.getLangOpts(), IncludeStyle));
171 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
172}
173
174void MoveConstructorInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
175 Options.store(Opts, "IncludeStyle", IncludeSorter::toString(IncludeStyle));
176}
177
Aaron Ballman9392ced2015-08-20 15:52:52 +0000178} // namespace tidy
179} // namespace clang