blob: 7bc7835efcb191ace0c0158a1387cd111db486e1 [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) {
Alexander Kornienko3396a8b2015-05-22 10:31:17 +000020 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
Alexander Kornienko7ed89bc2015-05-27 14:24:11 +000027void NoexceptMoveConstructorCheck::check(
28 const MatchFinder::MatchResult &Result) {
Alexander Kornienko3396a8b2015-05-22 10:31:17 +000029 if (const auto *Decl = Result.Nodes.getNodeAs<CXXMethodDecl>("decl")) {
30 StringRef MethodType = "assignment operator";
31 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Decl)) {
32 if (!Ctor->isMoveConstructor())
33 return;
34 MethodType = "constructor";
35 } else if (!Decl->isMoveAssignmentOperator()) {
36 return;
37 }
38
39 const auto *ProtoType = Decl->getType()->getAs<FunctionProtoType>();
40 switch(ProtoType->getNoexceptSpec(*Result.Context)) {
41 case FunctionProtoType::NR_NoNoexcept:
42 diag(Decl->getLocation(), "move %0s should be marked noexcept")
43 << MethodType;
44 // FIXME: Add a fixit.
45 break;
46 case FunctionProtoType::NR_Throw:
47 // Don't complain about nothrow(false), but complain on nothrow(expr)
48 // where expr evaluates to false.
49 if (const Expr *E = ProtoType->getNoexceptExpr()) {
50 if (isa<CXXBoolLiteralExpr>(E))
51 break;
52 diag(E->getExprLoc(),
53 "noexcept specifier on the move %0 evaluates to 'false'")
54 << MethodType;
55 }
56 break;
57 case FunctionProtoType::NR_Nothrow:
58 case FunctionProtoType::NR_Dependent:
59 case FunctionProtoType::NR_BadNoexcept:
60 break;
61 }
62 }
63}
64
65} // namespace tidy
66} // namespace clang
67