2015-05-27 22:24:11 +08:00
|
|
|
//===--- NoexceptMoveConstructorCheck.cpp - clang-tidy---------------------===//
|
2015-05-22 18:31:17 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-05-22 18:31:17 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-05-27 22:24:11 +08:00
|
|
|
#include "NoexceptMoveConstructorCheck.h"
|
2015-05-22 18:31:17 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2017-11-29 00:41:03 +08:00
|
|
|
namespace performance {
|
2015-05-22 18:31:17 +08:00
|
|
|
|
2015-05-27 22:24:11 +08:00
|
|
|
void NoexceptMoveConstructorCheck::registerMatchers(MatchFinder *Finder) {
|
2015-08-29 03:27:19 +08:00
|
|
|
// Only register the matchers for C++11; the functionality currently does not
|
|
|
|
// provide any benefit to other languages, despite being benign.
|
2017-06-09 15:34:58 +08:00
|
|
|
if (!getLangOpts().CPlusPlus11)
|
2015-08-31 23:28:57 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
Finder->addMatcher(
|
2015-09-17 21:31:25 +08:00
|
|
|
cxxMethodDecl(anyOf(cxxConstructorDecl(), hasOverloadedOperatorName("=")),
|
|
|
|
unless(isImplicit()), unless(isDeleted()))
|
2015-08-31 23:28:57 +08:00
|
|
|
.bind("decl"),
|
|
|
|
this);
|
2015-05-22 18:31:17 +08:00
|
|
|
}
|
|
|
|
|
2015-05-27 22:24:11 +08:00
|
|
|
void NoexceptMoveConstructorCheck::check(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
2015-05-22 18:31:17 +08:00
|
|
|
if (const auto *Decl = Result.Nodes.getNodeAs<CXXMethodDecl>("decl")) {
|
|
|
|
StringRef MethodType = "assignment operator";
|
|
|
|
if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Decl)) {
|
|
|
|
if (!Ctor->isMoveConstructor())
|
|
|
|
return;
|
|
|
|
MethodType = "constructor";
|
|
|
|
} else if (!Decl->isMoveAssignmentOperator()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto *ProtoType = Decl->getType()->getAs<FunctionProtoType>();
|
2017-03-18 00:40:34 +08:00
|
|
|
|
|
|
|
if (isUnresolvedExceptionSpec(ProtoType->getExceptionSpecType()))
|
|
|
|
return;
|
|
|
|
|
2018-05-03 11:59:50 +08:00
|
|
|
if (!isNoexceptExceptionSpec(ProtoType->getExceptionSpecType())) {
|
2017-11-29 00:41:03 +08:00
|
|
|
diag(Decl->getLocation(), "move %0s should be marked noexcept")
|
|
|
|
<< MethodType;
|
|
|
|
// FIXME: Add a fixit.
|
2018-05-03 11:59:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't complain about nothrow(false), but complain on nothrow(expr)
|
|
|
|
// where expr evaluates to false.
|
|
|
|
if (ProtoType->canThrow() == CT_Can) {
|
|
|
|
Expr *E = ProtoType->getNoexceptExpr();
|
2018-11-09 09:32:30 +08:00
|
|
|
E = E->IgnoreImplicit();
|
|
|
|
if (!isa<CXXBoolLiteralExpr>(E)) {
|
2017-11-29 00:41:03 +08:00
|
|
|
diag(E->getExprLoc(),
|
|
|
|
"noexcept specifier on the move %0 evaluates to 'false'")
|
2015-05-22 18:31:17 +08:00
|
|
|
<< MethodType;
|
2017-11-29 00:41:03 +08:00
|
|
|
}
|
2015-05-22 18:31:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 00:41:03 +08:00
|
|
|
} // namespace performance
|
2015-05-22 18:31:17 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|