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 auto CatchBadAllocType =
56 qualType(hasCanonicalType(anyOf(BadAllocType, BadAllocReferenceType,
57 ExceptionType, ExceptionReferenceType)));
58 auto BadAllocCatchingTryBlock = cxxTryStmt(hasHandlerFor(CatchBadAllocType));
60 auto FunctionMayNotThrow = functionDecl(isNoThrow());
62 Finder->addMatcher(cxxNewExpr(mayThrow(),
63 unless(hasAncestor(BadAllocCatchingTryBlock)),
64 hasAncestor(FunctionMayNotThrow))
70 const MatchFinder::MatchResult &Result) {
71 const auto *MatchedExpr = Result.Nodes.getNodeAs<CXXNewExpr>(
"new-expr");
73 diag(MatchedExpr->getBeginLoc(),
74 "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)