clang-tools  9.0.0
MoveConstructorInitCheck.cpp
Go to the documentation of this file.
1 //===--- MoveConstructorInitCheck.cpp - clang-tidy-------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "../utils/Matchers.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Frontend/CompilerInstance.h"
14 #include "clang/Lex/Lexer.h"
15 #include "clang/Lex/Preprocessor.h"
16 
17 using namespace clang::ast_matchers;
18 
19 namespace clang {
20 namespace tidy {
21 namespace performance {
22 
23 MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef Name,
24  ClangTidyContext *Context)
25  : ClangTidyCheck(Name, Context),
26  IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
27  Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {}
28 
29 void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) {
30  // Only register the matchers for C++11; the functionality currently does not
31  // provide any benefit to other languages, despite being benign.
32  if (!getLangOpts().CPlusPlus11)
33  return;
34 
35  Finder->addMatcher(
36  cxxConstructorDecl(
37  unless(isImplicit()), isMoveConstructor(),
38  hasAnyConstructorInitializer(
39  cxxCtorInitializer(
40  withInitializer(cxxConstructExpr(hasDeclaration(
41  cxxConstructorDecl(isCopyConstructor()).bind("ctor")))))
42  .bind("move-init"))),
43  this);
44 }
45 
46 void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) {
47  const auto *CopyCtor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
48  const auto *Initializer =
49  Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init");
50 
51  // Do not diagnose if the expression used to perform the initialization is a
52  // trivially-copyable type.
53  QualType QT = Initializer->getInit()->getType();
54  if (QT.isTriviallyCopyableType(*Result.Context))
55  return;
56 
57  if (QT.isConstQualified())
58  return;
59 
60  const auto *RD = QT->getAsCXXRecordDecl();
61  if (RD && RD->isTriviallyCopyable())
62  return;
63 
64  // Diagnose when the class type has a move constructor available, but the
65  // ctor-initializer uses the copy constructor instead.
66  const CXXConstructorDecl *Candidate = nullptr;
67  for (const auto *Ctor : CopyCtor->getParent()->ctors()) {
68  if (Ctor->isMoveConstructor() && Ctor->getAccess() <= AS_protected &&
69  !Ctor->isDeleted()) {
70  // The type has a move constructor that is at least accessible to the
71  // initializer.
72  //
73  // FIXME: Determine whether the move constructor is a viable candidate
74  // for the ctor-initializer, perhaps provide a fixit that suggests
75  // using std::move().
76  Candidate = Ctor;
77  break;
78  }
79  }
80 
81  if (Candidate) {
82  // There's a move constructor candidate that the caller probably intended
83  // to call instead.
84  diag(Initializer->getSourceLocation(),
85  "move constructor initializes %0 by calling a copy constructor")
86  << (Initializer->isBaseInitializer() ? "base class" : "class member");
87  diag(CopyCtor->getLocation(), "copy constructor being called",
88  DiagnosticIDs::Note);
89  diag(Candidate->getLocation(), "candidate move constructor here",
90  DiagnosticIDs::Note);
91  }
92 }
93 
95  const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
96  Inserter = llvm::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
97  IncludeStyle);
98  PP->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
ExpectedMatch Candidate
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.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: Rename.cpp:36
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.