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