blob: b33e908fd8fa9b5e2a572a2f685fc3ffa582122b [file] [log] [blame]
Alexander Kornienko3396a8b2015-05-22 10:31:17 +00001//===--- NoexceptMoveCtorsCheck.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 "NoexceptMoveCtorsCheck.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
18
19void NoexceptMoveCtorsCheck::registerMatchers(MatchFinder *Finder) {
20 Finder->addMatcher(
Alexander Kornienkobf5bd942015-05-26 14:35:09 +000021 methodDecl(anyOf(constructorDecl(), hasOverloadedOperatorName("=")),
22 unless(isImplicit()), unless(isDeleted()))
Alexander Kornienko3396a8b2015-05-22 10:31:17 +000023 .bind("decl"),
24 this);
25}
26
27void NoexceptMoveCtorsCheck::check(const MatchFinder::MatchResult &Result) {
28 if (const auto *Decl = Result.Nodes.getNodeAs<CXXMethodDecl>("decl")) {
29 StringRef MethodType = "assignment operator";
30 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Decl)) {
31 if (!Ctor->isMoveConstructor())
32 return;
33 MethodType = "constructor";
34 } else if (!Decl->isMoveAssignmentOperator()) {
35 return;
36 }
37
38 const auto *ProtoType = Decl->getType()->getAs<FunctionProtoType>();
39 switch(ProtoType->getNoexceptSpec(*Result.Context)) {
40 case FunctionProtoType::NR_NoNoexcept:
41 diag(Decl->getLocation(), "move %0s should be marked noexcept")
42 << MethodType;
43 // FIXME: Add a fixit.
44 break;
45 case FunctionProtoType::NR_Throw:
46 // Don't complain about nothrow(false), but complain on nothrow(expr)
47 // where expr evaluates to false.
48 if (const Expr *E = ProtoType->getNoexceptExpr()) {
49 if (isa<CXXBoolLiteralExpr>(E))
50 break;
51 diag(E->getExprLoc(),
52 "noexcept specifier on the move %0 evaluates to 'false'")
53 << MethodType;
54 }
55 break;
56 case FunctionProtoType::NR_Nothrow:
57 case FunctionProtoType::NR_Dependent:
58 case FunctionProtoType::NR_BadNoexcept:
59 break;
60 }
61 }
62}
63
64} // namespace tidy
65} // namespace clang
66