1 //===--- ThrownExceptionTypeCheck.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 "ThrownExceptionTypeCheck.h"
10 #include "../utils/Matchers.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace cert {
19 
registerMatchers(MatchFinder * Finder)20 void ThrownExceptionTypeCheck::registerMatchers(MatchFinder *Finder) {
21   Finder->addMatcher(
22       traverse(
23           ast_type_traits::TK_AsIs,
24           cxxThrowExpr(has(ignoringParenImpCasts(
25               cxxConstructExpr(hasDeclaration(cxxConstructorDecl(
26                                    isCopyConstructor(), unless(isNoThrow()))))
27                   .bind("expr"))))),
28       this);
29 }
30 
check(const MatchFinder::MatchResult & Result)31 void ThrownExceptionTypeCheck::check(const MatchFinder::MatchResult &Result) {
32   const auto *E = Result.Nodes.getNodeAs<Expr>("expr");
33   diag(E->getExprLoc(),
34        "thrown exception type is not nothrow copy constructible");
35 }
36 
37 } // namespace cert
38 } // namespace tidy
39 } // namespace clang
40