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