10#include "clang/AST/ASTContext.h"
11#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)) {
34 FunctionDecl *OperatorNew =
Node.getOperatorNew();
37 return !OperatorNew->getType()->castAs<FunctionProtoType>()->isNothrow();
46 recordType(hasDeclaration(cxxRecordDecl(hasName(
"::std::bad_alloc"))));
48 recordType(hasDeclaration(cxxRecordDecl(hasName(
"::std::exception"))));
49 auto BadAllocReferenceType = referenceType(pointee(BadAllocType));
50 auto ExceptionReferenceType = referenceType(pointee(ExceptionType));
52 auto CatchBadAllocType =
53 qualType(hasCanonicalType(anyOf(BadAllocType, BadAllocReferenceType,
54 ExceptionType, ExceptionReferenceType)));
55 auto BadAllocCatchingTryBlock = cxxTryStmt(hasHandlerFor(CatchBadAllocType));
57 auto FunctionMayNotThrow = functionDecl(isNoThrow());
59 Finder->addMatcher(cxxNewExpr(mayThrow(),
60 unless(hasAncestor(BadAllocCatchingTryBlock)),
61 hasAncestor(FunctionMayNotThrow))
67 const MatchFinder::MatchResult &Result) {
68 const auto *MatchedExpr = Result.Nodes.getNodeAs<CXXNewExpr>(
"new-expr");
70 diag(MatchedExpr->getBeginLoc(),
71 "missing exception handler for allocation failure at 'new'");
llvm::SmallString< 256U > Name
CodeCompletionBuilder Builder
::clang::DynTypedNode Node
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
UnhandledExceptionAtNewCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
AST_MATCHER_P(FunctionDecl, parameterCountGE, unsigned, N)
Matches functions that have at least the specified amount of parameters.
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)