blob: 1a22e174924ca60d018265a4f7b3469c1012d52f [file] [log] [blame]
Alexander Kornienko7ed89bc2015-05-27 14:24:11 +00001//===--- NoexceptMoveConstructorCheck.cpp - clang-tidy---------------------===//
Alexander Kornienko3396a8b2015-05-22 10:31:17 +00002//
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 Kornienko7ed89bc2015-05-27 14:24:11 +000010#include "NoexceptMoveConstructorCheck.h"
Alexander Kornienko3396a8b2015-05-22 10:31:17 +000011#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
18
Alexander Kornienko7ed89bc2015-05-27 14:24:11 +000019void NoexceptMoveConstructorCheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballman327e97b2015-08-28 19:27:19 +000020 // Only register the matchers for C++11; the functionality currently does not
21 // provide any benefit to other languages, despite being benign.
Aaron Ballmanbf891092015-08-31 15:28:57 +000022 if (!getLangOpts().CPlusPlus11)
23 return;
24
25 Finder->addMatcher(
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000026 cxxMethodDecl(anyOf(cxxConstructorDecl(), hasOverloadedOperatorName("=")),
27 unless(isImplicit()), unless(isDeleted()))
Aaron Ballmanbf891092015-08-31 15:28:57 +000028 .bind("decl"),
29 this);
Alexander Kornienko3396a8b2015-05-22 10:31:17 +000030}
31
Alexander Kornienko7ed89bc2015-05-27 14:24:11 +000032void NoexceptMoveConstructorCheck::check(
33 const MatchFinder::MatchResult &Result) {
Alexander Kornienko3396a8b2015-05-22 10:31:17 +000034 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