clang-tools 10.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
9 #include "MoveConstructorInitCheck.h"
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
94 void MoveConstructorInitCheck::registerPPCallbacks(
95 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
96 Inserter = std::make_unique<utils::IncludeInserter>(SM, getLangOpts(),
97 IncludeStyle);
98 PP->addPPCallbacks(Inserter->CreatePPCallbacks());
99 }
100
101 void MoveConstructorInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
102 Options.store(Opts, "IncludeStyle",
103 utils::IncludeSorter::toString(IncludeStyle));
104 }
105
106 } // namespace performance
107 } // namespace tidy
108 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
Candidate
ExpectedMatch Candidate
Definition: FuzzyMatchTests.cpp:47
clang::tidy::utils::IncludeSorter::toString
static StringRef toString(IncludeStyle Style)
Converts IncludeStyle to string representation.
Definition: IncludeSorter.cpp:283
clang::tidy::performance::MoveConstructorInitCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: MoveConstructorInitCheck.cpp:46
clang::tidy::performance::MoveConstructorInitCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: MoveConstructorInitCheck.cpp:94
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:188
clang::tidy::performance::MoveConstructorInitCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: MoveConstructorInitCheck.cpp:101
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:184
clang::tidy::ClangTidyCheck::OptionsView::store
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: ClangTidyCheck.cpp:58
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:28
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
clang::tidy::performance::MoveConstructorInitCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: MoveConstructorInitCheck.cpp:29
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21
MoveConstructorInitCheck.h