blob: 111f24d8ebe92813b00bf13d0bd1c3c782ed3bef [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 {
Alexander Kornienko1bfcba82017-11-28 16:41:03 +000018namespace performance {
Alexander Kornienko3396a8b2015-05-22 10:31:17 +000019
Alexander Kornienko7ed89bc2015-05-27 14:24:11 +000020void NoexceptMoveConstructorCheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballman327e97b2015-08-28 19:27:19 +000021 // Only register the matchers for C++11; the functionality currently does not
22 // provide any benefit to other languages, despite being benign.
Alexander Kornienko564f1c72017-06-09 07:34:58 +000023 if (!getLangOpts().CPlusPlus11)
Aaron Ballmanbf891092015-08-31 15:28:57 +000024 return;
25
26 Finder->addMatcher(
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000027 cxxMethodDecl(anyOf(cxxConstructorDecl(), hasOverloadedOperatorName("=")),
28 unless(isImplicit()), unless(isDeleted()))
Aaron Ballmanbf891092015-08-31 15:28:57 +000029 .bind("decl"),
30 this);
Alexander Kornienko3396a8b2015-05-22 10:31:17 +000031}
32
Alexander Kornienko7ed89bc2015-05-27 14:24:11 +000033void NoexceptMoveConstructorCheck::check(
34 const MatchFinder::MatchResult &Result) {
Alexander Kornienko3396a8b2015-05-22 10:31:17 +000035 if (const auto *Decl = Result.Nodes.getNodeAs<CXXMethodDecl>("decl")) {
36 StringRef MethodType = "assignment operator";
37 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Decl)) {
38 if (!Ctor->isMoveConstructor())
39 return;
40 MethodType = "constructor";
41 } else if (!Decl->isMoveAssignmentOperator()) {
42 return;
43 }
44
45 const auto *ProtoType = Decl->getType()->getAs<FunctionProtoType>();
Alexander Kornienkob10daaf2017-03-17 16:40:34 +000046
47 if (isUnresolvedExceptionSpec(ProtoType->getExceptionSpecType()))
48 return;
49
Richard Smith800508b2018-05-03 03:59:50 +000050 if (!isNoexceptExceptionSpec(ProtoType->getExceptionSpecType())) {
Alexander Kornienko1bfcba82017-11-28 16:41:03 +000051 diag(Decl->getLocation(), "move %0s should be marked noexcept")
52 << MethodType;
53 // FIXME: Add a fixit.
Richard Smith800508b2018-05-03 03:59:50 +000054 return;
55 }
56
57 // Don't complain about nothrow(false), but complain on nothrow(expr)
58 // where expr evaluates to false.
59 if (ProtoType->canThrow() == CT_Can) {
60 Expr *E = ProtoType->getNoexceptExpr();
61 if (!isa<CXXBoolLiteralExpr>(ProtoType->getNoexceptExpr())) {
Alexander Kornienko1bfcba82017-11-28 16:41:03 +000062 diag(E->getExprLoc(),
63 "noexcept specifier on the move %0 evaluates to 'false'")
Alexander Kornienko3396a8b2015-05-22 10:31:17 +000064 << MethodType;
Alexander Kornienko1bfcba82017-11-28 16:41:03 +000065 }
Alexander Kornienko3396a8b2015-05-22 10:31:17 +000066 }
67 }
68}
69
Alexander Kornienko1bfcba82017-11-28 16:41:03 +000070} // namespace performance
Alexander Kornienko3396a8b2015-05-22 10:31:17 +000071} // namespace tidy
72} // namespace clang