Alexander Kornienko | 7ed89bc | 2015-05-27 14:24:11 +0000 | [diff] [blame] | 1 | //===--- NoexceptMoveConstructorCheck.cpp - clang-tidy---------------------===// |
Alexander Kornienko | 3396a8b | 2015-05-22 10:31:17 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | 7ed89bc | 2015-05-27 14:24:11 +0000 | [diff] [blame] | 10 | #include "NoexceptMoveConstructorCheck.h" |
Alexander Kornienko | 3396a8b | 2015-05-22 10:31:17 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | |
| 14 | using namespace clang::ast_matchers; |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace tidy { |
| 18 | |
Alexander Kornienko | 7ed89bc | 2015-05-27 14:24:11 +0000 | [diff] [blame] | 19 | void NoexceptMoveConstructorCheck::registerMatchers(MatchFinder *Finder) { |
Aaron Ballman | 327e97b | 2015-08-28 19:27:19 +0000 | [diff] [blame] | 20 | // Only register the matchers for C++11; the functionality currently does not |
| 21 | // provide any benefit to other languages, despite being benign. |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 22 | if (!getLangOpts().CPlusPlus11) |
| 23 | return; |
| 24 | |
| 25 | Finder->addMatcher( |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame^] | 26 | cxxMethodDecl(anyOf(cxxConstructorDecl(), hasOverloadedOperatorName("=")), |
| 27 | unless(isImplicit()), unless(isDeleted())) |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 28 | .bind("decl"), |
| 29 | this); |
Alexander Kornienko | 3396a8b | 2015-05-22 10:31:17 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Alexander Kornienko | 7ed89bc | 2015-05-27 14:24:11 +0000 | [diff] [blame] | 32 | void NoexceptMoveConstructorCheck::check( |
| 33 | const MatchFinder::MatchResult &Result) { |
Alexander Kornienko | 3396a8b | 2015-05-22 10:31:17 +0000 | [diff] [blame] | 34 | if (const auto *Decl = Result.Nodes.getNodeAs<CXXMethodDecl>("decl")) { |
| 35 | StringRef MethodType = "assignment operator"; |
| 36 | if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Decl)) { |
| 37 | if (!Ctor->isMoveConstructor()) |
| 38 | return; |
| 39 | MethodType = "constructor"; |
| 40 | } else if (!Decl->isMoveAssignmentOperator()) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | const auto *ProtoType = Decl->getType()->getAs<FunctionProtoType>(); |
| 45 | switch(ProtoType->getNoexceptSpec(*Result.Context)) { |
| 46 | case FunctionProtoType::NR_NoNoexcept: |
| 47 | diag(Decl->getLocation(), "move %0s should be marked noexcept") |
| 48 | << MethodType; |
| 49 | // FIXME: Add a fixit. |
| 50 | break; |
| 51 | case FunctionProtoType::NR_Throw: |
| 52 | // Don't complain about nothrow(false), but complain on nothrow(expr) |
| 53 | // where expr evaluates to false. |
| 54 | if (const Expr *E = ProtoType->getNoexceptExpr()) { |
| 55 | if (isa<CXXBoolLiteralExpr>(E)) |
| 56 | break; |
| 57 | diag(E->getExprLoc(), |
| 58 | "noexcept specifier on the move %0 evaluates to 'false'") |
| 59 | << MethodType; |
| 60 | } |
| 61 | break; |
| 62 | case FunctionProtoType::NR_Nothrow: |
| 63 | case FunctionProtoType::NR_Dependent: |
| 64 | case FunctionProtoType::NR_BadNoexcept: |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | } // namespace tidy |
| 71 | } // namespace clang |
| 72 | |