| 1 |
//===--- NoexceptMoveConstructorCheck.cpp - clang-tidy---------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "NoexceptMoveConstructorCheck.h" |
| 10 |
#include "clang/AST/ASTContext.h" |
| 11 |
#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 |
#include "clang/Lex/Lexer.h" |
| 13 |
#include "clang/Tooling/FixIt.h" |
| 14 |
|
| 15 |
using namespace clang::ast_matchers; |
| 16 |
|
| 17 |
namespace clang::tidy::performance { |
| 18 |
|
| 19 |
void NoexceptMoveConstructorCheck::registerMatchers(MatchFinder *Finder) { |
| 20 |
Finder->addMatcher( |
| 21 |
cxxMethodDecl(anyOf(cxxConstructorDecl(), hasOverloadedOperatorName("=")), |
| 22 |
unless(isImplicit()), unless(isDeleted())) |
| 23 |
.bind("decl"), |
| 24 |
this); |
| 25 |
} |
| 26 |
|
| 27 |
void NoexceptMoveConstructorCheck::check( |
| 28 |
const MatchFinder::MatchResult &Result) { |
| 29 |
if (const auto *Decl = Result.Nodes.getNodeAs<CXXMethodDecl>("decl")) { |
| 30 |
bool IsConstructor = false; |
| 31 |
if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Decl)) { |
| 32 |
if (!Ctor->isMoveConstructor()) |
| 33 |
return; |
| 34 |
IsConstructor = true; |
| 35 |
} else if (!Decl->isMoveAssignmentOperator()) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
const auto *ProtoType = Decl->getType()->castAs<FunctionProtoType>(); |
| 40 |
|
| 41 |
if (isUnresolvedExceptionSpec(ProtoType->getExceptionSpecType())) |
| 42 |
return; |
| 43 |
|
| 44 |
if (!isNoexceptExceptionSpec(ProtoType->getExceptionSpecType())) { |
| 45 |
auto Diag = diag(Decl->getLocation(), |
| 46 |
"move %select{assignment operator|constructor}0s should " |
| 47 |
"be marked noexcept") |
| 48 |
<< IsConstructor; |
| 49 |
// Add FixIt hints. |
| 50 |
SourceManager &SM = *Result.SourceManager; |
| 51 |
assert(Decl->getNumParams() > 0); |
| 52 |
SourceLocation NoexceptLoc = Decl->getParamDecl(Decl->getNumParams() - 1) |
| 53 |
->getSourceRange() |
| 54 |
.getEnd(); |
| 55 |
if (NoexceptLoc.isValid()) |
| 56 |
NoexceptLoc = Lexer::findLocationAfterToken( |
| 57 |
NoexceptLoc, tok::r_paren, SM, Result.Context->getLangOpts(), true); |
| 58 |
if (NoexceptLoc.isValid()) |
| 59 |
Diag << FixItHint::CreateInsertion(NoexceptLoc, " noexcept "); |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
// Don't complain about nothrow(false), but complain on nothrow(expr) |
| 64 |
// where expr evaluates to false. |
| 65 |
if (ProtoType->canThrow() == CT_Can) { |
| 66 |
Expr *E = ProtoType->getNoexceptExpr(); |
| 67 |
E = E->IgnoreImplicit(); |
| 68 |
if (!isa<CXXBoolLiteralExpr>(E)) { |
| 69 |
diag(E->getExprLoc(), |
| 70 |
"noexcept specifier on the move %select{assignment " |
| 71 |
"operator|constructor}0 evaluates to 'false'") |
| 72 |
<< IsConstructor; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
} // namespace clang::tidy::performance |