clang-tools  12.0.0git
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(Options.getLocalOrGlobal("IncludeStyle",
27  utils::IncludeSorter::IS_LLVM)) {}
28 
29 void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) {
30  Finder->addMatcher(
31  traverse(ast_type_traits::TK_AsIs,
32  cxxConstructorDecl(
33  unless(isImplicit()), isMoveConstructor(),
34  hasAnyConstructorInitializer(
35  cxxCtorInitializer(
36  withInitializer(cxxConstructExpr(hasDeclaration(
37  cxxConstructorDecl(isCopyConstructor())
38  .bind("ctor")))))
39  .bind("move-init")))),
40  this);
41 }
42 
43 void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) {
44  const auto *CopyCtor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
45  const auto *Initializer =
46  Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init");
47 
48  // Do not diagnose if the expression used to perform the initialization is a
49  // trivially-copyable type.
50  QualType QT = Initializer->getInit()->getType();
51  if (QT.isTriviallyCopyableType(*Result.Context))
52  return;
53 
54  if (QT.isConstQualified())
55  return;
56 
57  const auto *RD = QT->getAsCXXRecordDecl();
58  if (RD && RD->isTriviallyCopyable())
59  return;
60 
61  // Diagnose when the class type has a move constructor available, but the
62  // ctor-initializer uses the copy constructor instead.
63  const CXXConstructorDecl *Candidate = nullptr;
64  for (const auto *Ctor : CopyCtor->getParent()->ctors()) {
65  if (Ctor->isMoveConstructor() && Ctor->getAccess() <= AS_protected &&
66  !Ctor->isDeleted()) {
67  // The type has a move constructor that is at least accessible to the
68  // initializer.
69  //
70  // FIXME: Determine whether the move constructor is a viable candidate
71  // for the ctor-initializer, perhaps provide a fixit that suggests
72  // using std::move().
73  Candidate = Ctor;
74  break;
75  }
76  }
77 
78  if (Candidate) {
79  // There's a move constructor candidate that the caller probably intended
80  // to call instead.
81  diag(Initializer->getSourceLocation(),
82  "move constructor initializes %0 by calling a copy constructor")
83  << (Initializer->isBaseInitializer() ? "base class" : "class member");
84  diag(CopyCtor->getLocation(), "copy constructor being called",
85  DiagnosticIDs::Note);
86  diag(Candidate->getLocation(), "candidate move constructor here",
87  DiagnosticIDs::Note);
88  }
89 }
90 
92  const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
93  Inserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
94  IncludeStyle);
95  PP->addPPCallbacks(Inserter->CreatePPCallbacks());
96 }
97 
99  Options.store(Opts, "IncludeStyle", IncludeStyle);
100 }
101 
102 } // namespace performance
103 } // namespace tidy
104 } // namespace clang
ExpectedMatch Candidate
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
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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.
std::map< std::string, ClangTidyValue > OptionMap
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.