clang-tools 17.0.0git
NoexceptMoveConstructorCheck.cpp
Go to the documentation of this file.
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
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
15using namespace clang::ast_matchers;
16
18
20 Finder->addMatcher(
21 cxxMethodDecl(anyOf(cxxConstructorDecl(), hasOverloadedOperatorName("=")),
22 unless(isImplicit()), unless(isDeleted()))
23 .bind("decl"),
24 this);
25}
26
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
const Expr * E
const FunctionDecl * Decl
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.