Aaron Ballman | 9392ced | 2015-08-20 15:52:52 +0000 | [diff] [blame] | 1 | //===--- MoveConstructorInitCheck.cpp - clang-tidy-------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "MoveConstructorInitCheck.h" |
Aaron Ballman | fc4e042 | 2015-10-06 16:27:03 +0000 | [diff] [blame] | 11 | #include "../utils/Matchers.h" |
Aaron Ballman | 9392ced | 2015-08-20 15:52:52 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
| 13 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
Aaron Ballman | fc4e042 | 2015-10-06 16:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CompilerInstance.h" |
| 15 | #include "clang/Lex/Lexer.h" |
| 16 | #include "clang/Lex/Preprocessor.h" |
Aaron Ballman | 9392ced | 2015-08-20 15:52:52 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang::ast_matchers; |
| 19 | |
| 20 | namespace clang { |
| 21 | namespace tidy { |
Alexander Kornienko | 6e39e68 | 2017-11-27 13:06:28 +0000 | [diff] [blame] | 22 | namespace performance { |
Aaron Ballman | 9392ced | 2015-08-20 15:52:52 +0000 | [diff] [blame] | 23 | |
Aaron Ballman | fc4e042 | 2015-10-06 16:27:03 +0000 | [diff] [blame] | 24 | MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef Name, |
| 25 | ClangTidyContext *Context) |
| 26 | : ClangTidyCheck(Name, Context), |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 27 | IncludeStyle(utils::IncludeSorter::parseIncludeStyle( |
Alexander Kornienko | b1c7432 | 2017-07-20 12:02:03 +0000 | [diff] [blame] | 28 | Options.getLocalOrGlobal("IncludeStyle", "llvm"))) {} |
Aaron Ballman | fc4e042 | 2015-10-06 16:27:03 +0000 | [diff] [blame] | 29 | |
Aaron Ballman | 9392ced | 2015-08-20 15:52:52 +0000 | [diff] [blame] | 30 | void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) { |
Aaron Ballman | 327e97b | 2015-08-28 19:27:19 +0000 | [diff] [blame] | 31 | // Only register the matchers for C++11; the functionality currently does not |
| 32 | // provide any benefit to other languages, despite being benign. |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 33 | if (!getLangOpts().CPlusPlus11) |
| 34 | return; |
| 35 | |
| 36 | Finder->addMatcher( |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame] | 37 | cxxConstructorDecl( |
| 38 | unless(isImplicit()), |
| 39 | allOf(isMoveConstructor(), |
| 40 | hasAnyConstructorInitializer( |
| 41 | cxxCtorInitializer( |
| 42 | withInitializer(cxxConstructExpr(hasDeclaration( |
| 43 | cxxConstructorDecl(isCopyConstructor()) |
| 44 | .bind("ctor"))))) |
Aaron Ballman | fc4e042 | 2015-10-06 16:27:03 +0000 | [diff] [blame] | 45 | .bind("move-init")))), |
| 46 | this); |
Aaron Ballman | 9392ced | 2015-08-20 15:52:52 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) { |
| 50 | const auto *CopyCtor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor"); |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 51 | const auto *Initializer = |
| 52 | Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init"); |
Aaron Ballman | 9392ced | 2015-08-20 15:52:52 +0000 | [diff] [blame] | 53 | |
| 54 | // Do not diagnose if the expression used to perform the initialization is a |
| 55 | // trivially-copyable type. |
| 56 | QualType QT = Initializer->getInit()->getType(); |
| 57 | if (QT.isTriviallyCopyableType(*Result.Context)) |
| 58 | return; |
Kirill Bobyrev | 11cea45 | 2016-08-01 12:06:18 +0000 | [diff] [blame] | 59 | |
Alexander Kornienko | bb64200 | 2017-02-08 14:56:16 +0000 | [diff] [blame] | 60 | if (QT.isConstQualified()) |
| 61 | return; |
| 62 | |
Aaron Ballman | 9392ced | 2015-08-20 15:52:52 +0000 | [diff] [blame] | 63 | const auto *RD = QT->getAsCXXRecordDecl(); |
| 64 | if (RD && RD->isTriviallyCopyable()) |
| 65 | return; |
| 66 | |
| 67 | // Diagnose when the class type has a move constructor available, but the |
| 68 | // ctor-initializer uses the copy constructor instead. |
| 69 | const CXXConstructorDecl *Candidate = nullptr; |
| 70 | for (const auto *Ctor : CopyCtor->getParent()->ctors()) { |
| 71 | if (Ctor->isMoveConstructor() && Ctor->getAccess() <= AS_protected && |
| 72 | !Ctor->isDeleted()) { |
| 73 | // The type has a move constructor that is at least accessible to the |
| 74 | // initializer. |
| 75 | // |
| 76 | // FIXME: Determine whether the move constructor is a viable candidate |
| 77 | // for the ctor-initializer, perhaps provide a fixit that suggests |
| 78 | // using std::move(). |
| 79 | Candidate = Ctor; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (Candidate) { |
| 85 | // There's a move constructor candidate that the caller probably intended |
| 86 | // to call instead. |
| 87 | diag(Initializer->getSourceLocation(), |
| 88 | "move constructor initializes %0 by calling a copy constructor") |
| 89 | << (Initializer->isBaseInitializer() ? "base class" : "class member"); |
| 90 | diag(CopyCtor->getLocation(), "copy constructor being called", |
| 91 | DiagnosticIDs::Note); |
| 92 | diag(Candidate->getLocation(), "candidate move constructor here", |
| 93 | DiagnosticIDs::Note); |
| 94 | } |
| 95 | } |
| 96 | |
Aaron Ballman | fc4e042 | 2015-10-06 16:27:03 +0000 | [diff] [blame] | 97 | void MoveConstructorInitCheck::registerPPCallbacks(CompilerInstance &Compiler) { |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 98 | Inserter.reset(new utils::IncludeInserter( |
| 99 | Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle)); |
Aaron Ballman | fc4e042 | 2015-10-06 16:27:03 +0000 | [diff] [blame] | 100 | Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); |
| 101 | } |
| 102 | |
| 103 | void MoveConstructorInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 104 | Options.store(Opts, "IncludeStyle", |
| 105 | utils::IncludeSorter::toString(IncludeStyle)); |
Aaron Ballman | fc4e042 | 2015-10-06 16:27:03 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Alexander Kornienko | 6e39e68 | 2017-11-27 13:06:28 +0000 | [diff] [blame] | 108 | } // namespace performance |
Aaron Ballman | 9392ced | 2015-08-20 15:52:52 +0000 | [diff] [blame] | 109 | } // namespace tidy |
| 110 | } // namespace clang |