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 
MoveConstructorInitCheck(StringRef Name,ClangTidyContext * Context)23 MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef Name,
24                                                    ClangTidyContext *Context)
25     : ClangTidyCheck(Name, Context),
26       Inserter(Options.getLocalOrGlobal("IncludeStyle",
27                                         utils::IncludeSorter::IS_LLVM)) {}
28 
registerMatchers(MatchFinder * Finder)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 
check(const MatchFinder::MatchResult & Result)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 
registerPPCallbacks(const SourceManager & SM,Preprocessor * PP,Preprocessor * ModuleExpanderPP)91 void MoveConstructorInitCheck::registerPPCallbacks(
92     const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
93   Inserter.registerPreprocessor(PP);
94 }
95 
storeOptions(ClangTidyOptions::OptionMap & Opts)96 void MoveConstructorInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
97   Options.store(Opts, "IncludeStyle", Inserter.getStyle());
98 }
99 
100 } // namespace performance
101 } // namespace tidy
102 } // namespace clang
103