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