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 { |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 22 | namespace misc { |
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 | namespace { |
| 25 | |
| 26 | unsigned int |
| 27 | parmVarDeclRefExprOccurences(const ParmVarDecl &MovableParam, |
| 28 | const CXXConstructorDecl &ConstructorDecl, |
| 29 | ASTContext &Context) { |
| 30 | unsigned int Occurrences = 0; |
| 31 | auto AllDeclRefs = |
| 32 | findAll(declRefExpr(to(parmVarDecl(equalsNode(&MovableParam))))); |
| 33 | Occurrences += match(AllDeclRefs, *ConstructorDecl.getBody(), Context).size(); |
| 34 | for (const auto *Initializer : ConstructorDecl.inits()) { |
| 35 | Occurrences += match(AllDeclRefs, *Initializer->getInit(), Context).size(); |
| 36 | } |
| 37 | return Occurrences; |
| 38 | } |
| 39 | |
| 40 | } // namespace |
| 41 | |
| 42 | MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef Name, |
| 43 | ClangTidyContext *Context) |
| 44 | : ClangTidyCheck(Name, Context), |
| 45 | IncludeStyle(IncludeSorter::parseIncludeStyle( |
Aaron Ballman | c00ad6c | 2016-01-08 15:50:51 +0000 | [diff] [blame] | 46 | Options.get("IncludeStyle", "llvm"))), |
| 47 | UseCERTSemantics(Context->isCheckEnabled("cert-oop11-cpp")) {} |
Aaron Ballman | fc4e042 | 2015-10-06 16:27:03 +0000 | [diff] [blame] | 48 | |
Aaron Ballman | 9392ced | 2015-08-20 15:52:52 +0000 | [diff] [blame] | 49 | void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) { |
Aaron Ballman | 327e97b | 2015-08-28 19:27:19 +0000 | [diff] [blame] | 50 | // Only register the matchers for C++11; the functionality currently does not |
| 51 | // provide any benefit to other languages, despite being benign. |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 52 | if (!getLangOpts().CPlusPlus11) |
| 53 | return; |
| 54 | |
| 55 | Finder->addMatcher( |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame] | 56 | cxxConstructorDecl( |
| 57 | unless(isImplicit()), |
| 58 | allOf(isMoveConstructor(), |
| 59 | hasAnyConstructorInitializer( |
| 60 | cxxCtorInitializer( |
| 61 | withInitializer(cxxConstructExpr(hasDeclaration( |
| 62 | cxxConstructorDecl(isCopyConstructor()) |
| 63 | .bind("ctor"))))) |
Aaron Ballman | fc4e042 | 2015-10-06 16:27:03 +0000 | [diff] [blame] | 64 | .bind("move-init")))), |
| 65 | this); |
| 66 | |
| 67 | auto NonConstValueMovableAndExpensiveToCopy = |
| 68 | qualType(allOf(unless(pointerType()), unless(isConstQualified()), |
| 69 | hasDeclaration(cxxRecordDecl(hasMethod(cxxConstructorDecl( |
| 70 | isMoveConstructor(), unless(isDeleted()))))), |
| 71 | matchers::isExpensiveToCopy())); |
Aaron Ballman | c00ad6c | 2016-01-08 15:50:51 +0000 | [diff] [blame] | 72 | |
| 73 | // This checker is also used to implement cert-oop11-cpp, but when using that |
| 74 | // form of the checker, we do not want to diagnose movable parameters. |
| 75 | if (!UseCERTSemantics) |
| 76 | Finder->addMatcher( |
| 77 | cxxConstructorDecl( |
| 78 | allOf( |
| 79 | unless(isMoveConstructor()), |
| 80 | hasAnyConstructorInitializer(withInitializer(cxxConstructExpr( |
| 81 | hasDeclaration(cxxConstructorDecl(isCopyConstructor())), |
| 82 | hasArgument( |
| 83 | 0, |
| 84 | declRefExpr( |
| 85 | to(parmVarDecl( |
| 86 | hasType( |
| 87 | NonConstValueMovableAndExpensiveToCopy)) |
| 88 | .bind("movable-param"))) |
| 89 | .bind("init-arg"))))))) |
| 90 | .bind("ctor-decl"), |
| 91 | this); |
Aaron Ballman | 9392ced | 2015-08-20 15:52:52 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) { |
Aaron Ballman | fc4e042 | 2015-10-06 16:27:03 +0000 | [diff] [blame] | 95 | if (Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init") != nullptr) |
| 96 | handleMoveConstructor(Result); |
| 97 | if (Result.Nodes.getNodeAs<ParmVarDecl>("movable-param") != nullptr) |
| 98 | handleParamNotMoved(Result); |
| 99 | } |
| 100 | |
| 101 | void MoveConstructorInitCheck::handleParamNotMoved( |
| 102 | const MatchFinder::MatchResult &Result) { |
| 103 | const auto *MovableParam = |
| 104 | Result.Nodes.getNodeAs<ParmVarDecl>("movable-param"); |
| 105 | const auto *ConstructorDecl = |
| 106 | Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor-decl"); |
| 107 | const auto *InitArg = Result.Nodes.getNodeAs<DeclRefExpr>("init-arg"); |
| 108 | // If the parameter is referenced more than once it is not safe to move it. |
| 109 | if (parmVarDeclRefExprOccurences(*MovableParam, *ConstructorDecl, |
| 110 | *Result.Context) > 1) |
| 111 | return; |
| 112 | auto DiagOut = |
| 113 | diag(InitArg->getLocStart(), "value argument can be moved to avoid copy"); |
| 114 | DiagOut << FixItHint::CreateReplacement( |
| 115 | InitArg->getSourceRange(), |
| 116 | (Twine("std::move(") + MovableParam->getName() + ")").str()); |
| 117 | if (auto IncludeFixit = Inserter->CreateIncludeInsertion( |
| 118 | Result.SourceManager->getFileID(InitArg->getLocStart()), "utility", |
| 119 | /*IsAngled=*/true)) { |
| 120 | DiagOut << *IncludeFixit; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void MoveConstructorInitCheck::handleMoveConstructor( |
| 125 | const MatchFinder::MatchResult &Result) { |
Aaron Ballman | 9392ced | 2015-08-20 15:52:52 +0000 | [diff] [blame] | 126 | const auto *CopyCtor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor"); |
Aaron Ballman | fc4e042 | 2015-10-06 16:27:03 +0000 | [diff] [blame] | 127 | const auto *Initializer = Result.Nodes.getNodeAs<CXXCtorInitializer>("move-init"); |
Aaron Ballman | 9392ced | 2015-08-20 15:52:52 +0000 | [diff] [blame] | 128 | |
| 129 | // Do not diagnose if the expression used to perform the initialization is a |
| 130 | // trivially-copyable type. |
| 131 | QualType QT = Initializer->getInit()->getType(); |
| 132 | if (QT.isTriviallyCopyableType(*Result.Context)) |
| 133 | return; |
| 134 | |
| 135 | const auto *RD = QT->getAsCXXRecordDecl(); |
| 136 | if (RD && RD->isTriviallyCopyable()) |
| 137 | return; |
| 138 | |
| 139 | // Diagnose when the class type has a move constructor available, but the |
| 140 | // ctor-initializer uses the copy constructor instead. |
| 141 | const CXXConstructorDecl *Candidate = nullptr; |
| 142 | for (const auto *Ctor : CopyCtor->getParent()->ctors()) { |
| 143 | if (Ctor->isMoveConstructor() && Ctor->getAccess() <= AS_protected && |
| 144 | !Ctor->isDeleted()) { |
| 145 | // The type has a move constructor that is at least accessible to the |
| 146 | // initializer. |
| 147 | // |
| 148 | // FIXME: Determine whether the move constructor is a viable candidate |
| 149 | // for the ctor-initializer, perhaps provide a fixit that suggests |
| 150 | // using std::move(). |
| 151 | Candidate = Ctor; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (Candidate) { |
| 157 | // There's a move constructor candidate that the caller probably intended |
| 158 | // to call instead. |
| 159 | diag(Initializer->getSourceLocation(), |
| 160 | "move constructor initializes %0 by calling a copy constructor") |
| 161 | << (Initializer->isBaseInitializer() ? "base class" : "class member"); |
| 162 | diag(CopyCtor->getLocation(), "copy constructor being called", |
| 163 | DiagnosticIDs::Note); |
| 164 | diag(Candidate->getLocation(), "candidate move constructor here", |
| 165 | DiagnosticIDs::Note); |
| 166 | } |
| 167 | } |
| 168 | |
Aaron Ballman | fc4e042 | 2015-10-06 16:27:03 +0000 | [diff] [blame] | 169 | void MoveConstructorInitCheck::registerPPCallbacks(CompilerInstance &Compiler) { |
| 170 | Inserter.reset(new IncludeInserter(Compiler.getSourceManager(), |
| 171 | Compiler.getLangOpts(), IncludeStyle)); |
| 172 | Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); |
| 173 | } |
| 174 | |
| 175 | void MoveConstructorInitCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 176 | Options.store(Opts, "IncludeStyle", IncludeSorter::toString(IncludeStyle)); |
| 177 | } |
| 178 | |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 179 | } // namespace misc |
Aaron Ballman | 9392ced | 2015-08-20 15:52:52 +0000 | [diff] [blame] | 180 | } // namespace tidy |
| 181 | } // namespace clang |