clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

MoveConstructorInitCheck.cpp
Go to the documentation of this file.
1 //===--- 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"
11 #include "../utils/Matchers.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "clang/Frontend/CompilerInstance.h"
15 #include "clang/Lex/Lexer.h"
16 #include "clang/Lex/Preprocessor.h"
17
18 using namespace clang::ast_matchers;
19
20 namespace clang {
21 namespace tidy {
22
23 namespace {
24
25 unsigned int
26 parmVarDeclRefExprOccurences(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
41 MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef Name,
42 ClangTidyContext *Context)
43 : ClangTidyCheck(Name, Context),
44 IncludeStyle(IncludeSorter::parseIncludeStyle(
45 Options.get("IncludeStyle", "llvm"))),
46 UseCERTSemantics(Context->isCheckEnabled("cert-oop11-cpp")) {}
47
48 void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) {
49 // Only register the matchers for C++11; the functionality currently does not
50 // provide any benefit to other languages, despite being benign.
51 if (!getLangOpts().CPlusPlus11)
52 return;
53
54 Finder->addMatcher(
55 cxxConstructorDecl(
56 unless(isImplicit()),
57 allOf(isMoveConstructor(),
58 hasAnyConstructorInitializer(
59 cxxCtorInitializer(
60 withInitializer(cxxConstructExpr(hasDeclaration(
61 cxxConstructorDecl(isCopyConstructor())
62 .bind("ctor")))))
63 .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()));
71
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);
91 }
92
93 void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) {
94 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
100 void 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
123 void MoveConstructorInitCheck::handleMoveConstructor(
124 const MatchFinder::MatchResult &Result) {
125 const auto *CopyCtor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
126 const auto *Initializer = Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init");
127
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
168 void MoveConstructorInitCheck::registerPPCallbacks(CompilerInstance &Compiler) {
169 Inserter.reset(new IncludeInserter(Compiler.getSourceManager(),
170 Compiler.getLangOpts(), IncludeStyle));
171 Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
172 }
173
174 void MoveConstructorInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
175 Options.store(Opts, "IncludeStyle", IncludeSorter::toString(IncludeStyle));
176 }
177
178 } // namespace tidy
179 } // namespace clang
clang::tidy::ClangTidyCheck::getLangOpts
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:162
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
clang::tidy::IncludeSorter::toString
static StringRef toString(IncludeStyle Style)
Definition: IncludeSorter.cpp:290
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:102
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidy.h:158
clang::tidy::MoveConstructorInitCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: MoveConstructorInitCheck.cpp:174
clang::tidy::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidy.cpp:344
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:82
clang::tidy::MoveConstructorInitCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: MoveConstructorInitCheck.cpp:93
clang::tidy::type_traits::isExpensiveToCopy
llvm::Optional< bool > isExpensiveToCopy(QualType Type, ASTContext &Context)
Definition: TypeTraits.cpp:27
clang::tidy::IncludeSorter
Definition: IncludeSorter.h:23
Context
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:124
clang::tidy::IncludeInserter
Definition: IncludeInserter.h:47
clang::tidy::MoveConstructorInitCheck::registerPPCallbacks
void registerPPCallbacks(clang::CompilerInstance &Compiler) override
Definition: MoveConstructorInitCheck.cpp:168
clang::tidy::MoveConstructorInitCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register ASTMatchers with Finder.
Definition: MoveConstructorInitCheck.cpp:48
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidy.cpp:323
MoveConstructorInitCheck.h
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121