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