10#include "clang/ASTMatchers/ASTMatchFinder.h"
18 ast_matchers::internal::Matcher<QualType>, InnerMatcher) {
19 for (
unsigned NH = Node.getNumHandlers(), I = 0; I < NH; ++I) {
20 const CXXCatchStmt *CatchS = Node.getHandler(I);
22 if (CatchS->getCaughtType().isNull())
24 ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
25 if (InnerMatcher.matches(CatchS->getCaughtType(), Finder, &Result)) {
26 *Builder = std::move(Result);
34 FunctionDecl *OperatorNew = Node.getOperatorNew();
37 return !OperatorNew->getType()->castAs<FunctionProtoType>()->isNothrow();
48 recordType(hasDeclaration(cxxRecordDecl(hasName(
"::std::bad_alloc"))));
50 recordType(hasDeclaration(cxxRecordDecl(hasName(
"::std::exception"))));
51 auto BadAllocReferenceType = referenceType(pointee(BadAllocType));
52 auto ExceptionReferenceType = referenceType(pointee(ExceptionType));
54 auto CatchBadAllocType =
55 qualType(hasCanonicalType(anyOf(BadAllocType, BadAllocReferenceType,
56 ExceptionType, ExceptionReferenceType)));
57 auto BadAllocCatchingTryBlock = cxxTryStmt(hasHandlerFor(CatchBadAllocType));
59 auto FunctionMayNotThrow = functionDecl(isNoThrow());
61 Finder->addMatcher(cxxNewExpr(mayThrow(),
62 unless(hasAncestor(BadAllocCatchingTryBlock)),
63 hasAncestor(FunctionMayNotThrow))
69 const MatchFinder::MatchResult &Result) {
70 const auto *MatchedExpr = Result.Nodes.getNodeAs<CXXNewExpr>(
"new-expr");
72 diag(MatchedExpr->getBeginLoc(),
73 "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)