clang-tools  5.0.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 namespace misc {
23 
24 MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef Name,
26  : ClangTidyCheck(Name, Context),
27  IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
28  Options.get("IncludeStyle", "llvm"))) {}
29 
31  // Only register the matchers for C++11; the functionality currently does not
32  // provide any benefit to other languages, despite being benign.
33  if (!getLangOpts().CPlusPlus11)
34  return;
35 
36  Finder->addMatcher(
37  cxxConstructorDecl(
38  unless(isImplicit()),
39  allOf(isMoveConstructor(),
40  hasAnyConstructorInitializer(
41  cxxCtorInitializer(
42  withInitializer(cxxConstructExpr(hasDeclaration(
43  cxxConstructorDecl(isCopyConstructor())
44  .bind("ctor")))))
45  .bind("move-init")))),
46  this);
47 }
48 
49 void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) {
50  const auto *CopyCtor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
51  const auto *Initializer =
52  Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init");
53 
54  // Do not diagnose if the expression used to perform the initialization is a
55  // trivially-copyable type.
56  QualType QT = Initializer->getInit()->getType();
57  if (QT.isTriviallyCopyableType(*Result.Context))
58  return;
59 
60  if (QT.isConstQualified())
61  return;
62 
63  const auto *RD = QT->getAsCXXRecordDecl();
64  if (RD && RD->isTriviallyCopyable())
65  return;
66 
67  // Diagnose when the class type has a move constructor available, but the
68  // ctor-initializer uses the copy constructor instead.
69  const CXXConstructorDecl *Candidate = nullptr;
70  for (const auto *Ctor : CopyCtor->getParent()->ctors()) {
71  if (Ctor->isMoveConstructor() && Ctor->getAccess() <= AS_protected &&
72  !Ctor->isDeleted()) {
73  // The type has a move constructor that is at least accessible to the
74  // initializer.
75  //
76  // FIXME: Determine whether the move constructor is a viable candidate
77  // for the ctor-initializer, perhaps provide a fixit that suggests
78  // using std::move().
79  Candidate = Ctor;
80  break;
81  }
82  }
83 
84  if (Candidate) {
85  // There's a move constructor candidate that the caller probably intended
86  // to call instead.
87  diag(Initializer->getSourceLocation(),
88  "move constructor initializes %0 by calling a copy constructor")
89  << (Initializer->isBaseInitializer() ? "base class" : "class member");
90  diag(CopyCtor->getLocation(), "copy constructor being called",
91  DiagnosticIDs::Note);
92  diag(Candidate->getLocation(), "candidate move constructor here",
93  DiagnosticIDs::Note);
94  }
95 }
96 
97 void MoveConstructorInitCheck::registerPPCallbacks(CompilerInstance &Compiler) {
98  Inserter.reset(new utils::IncludeInserter(
99  Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
100  Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
101 }
102 
104  Options.store(Opts, "IncludeStyle",
105  utils::IncludeSorter::toString(IncludeStyle));
106 }
107 
108 } // namespace misc
109 } // namespace tidy
110 } // namespace clang
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:187
StringHandle Name
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:275
static StringRef toString(IncludeStyle Style)
Converts IncludeStyle to string representation.
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
void registerPPCallbacks(clang::CompilerInstance &Compiler) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
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:449
std::map< std::string, std::string > OptionMap
Produces fixes to insert specified includes to source files, if not yet present.
ClangTidyContext & Context
Definition: ClangTidy.cpp:87
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidy.cpp:416