10#include "clang/ASTMatchers/ASTMatchFinder.h"
18 ast_matchers::internal::Matcher<QualType>, InnerMatcher) {
19 const unsigned NH = Node.getNumHandlers();
20 for (
unsigned I = 0; I < NH; ++I) {
21 const CXXCatchStmt *CatchS = Node.getHandler(I);
23 if (CatchS->getCaughtType().isNull())
25 ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
26 if (InnerMatcher.matches(CatchS->getCaughtType(), Finder, &Result)) {
27 *Builder = std::move(Result);
35 const FunctionDecl *OperatorNew = Node.getOperatorNew();
38 return !OperatorNew->getType()->castAs<FunctionProtoType>()->isNothrow();
49 recordType(hasDeclaration(cxxRecordDecl(hasName(
"::std::bad_alloc"))));
51 recordType(hasDeclaration(cxxRecordDecl(hasName(
"::std::exception"))));
52 auto BadAllocReferenceType = referenceType(pointee(BadAllocType));
53 auto ExceptionReferenceType = referenceType(pointee(ExceptionType));
55 const auto CatchBadAllocType =
56 qualType(hasCanonicalType(anyOf(BadAllocType, BadAllocReferenceType,
57 ExceptionType, ExceptionReferenceType)));
58 const auto BadAllocCatchingTryBlock =
59 cxxTryStmt(hasHandlerFor(CatchBadAllocType));
61 const auto FunctionMayNotThrow = functionDecl(isNoThrow());
63 Finder->addMatcher(cxxNewExpr(mayThrow(),
64 unless(hasAncestor(BadAllocCatchingTryBlock)),
65 hasAncestor(FunctionMayNotThrow))
71 const MatchFinder::MatchResult &Result) {
72 const auto *MatchedExpr = Result.Nodes.getNodeAs<CXXNewExpr>(
"new-expr");
74 diag(MatchedExpr->getBeginLoc(),
75 "missing exception handler for allocation failure at 'new'");
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
UnhandledExceptionAtNewCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
AST_MATCHER_P(Stmt, isStatementIdenticalToBoundNode, std::string, ID)
AST_MATCHER(BinaryOperator, isRelationalOperator)