clang-tools  3.8.0
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 
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 
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()))))),
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 
175  Options.store(Opts, "IncludeStyle", IncludeSorter::toString(IncludeStyle));
176 }
177 
178 } // namespace tidy
179 } // namespace clang
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:162
StringHandle Name
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
static StringRef toString(IncludeStyle Style)
Base class for all clang-tidy checks.
Definition: ClangTidy.h:102
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
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
std::map< std::string, std::string > OptionMap
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
llvm::Optional< bool > isExpensiveToCopy(QualType Type, ASTContext &Context)
Definition: TypeTraits.cpp:27
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerPPCallbacks(clang::CompilerInstance &Compiler) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register ASTMatchers with Finder.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidy.cpp:323
const NamedDecl * Result
Definition: USRFinder.cpp:121