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