brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · 340b136 Raw
78 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "UnhandledExceptionAtNewCheck.h"10#include "clang/ASTMatchers/ASTMatchFinder.h"11 12using namespace clang::ast_matchers;13 14namespace clang::tidy::bugprone {15namespace {16 17AST_MATCHER_P(CXXTryStmt, hasHandlerFor,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);22    // Check for generic catch handler (match anything).23    if (CatchS->getCaughtType().isNull())24      return true;25    ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);26    if (InnerMatcher.matches(CatchS->getCaughtType(), Finder, &Result)) {27      *Builder = std::move(Result);28      return true;29    }30  }31  return false;32}33 34AST_MATCHER(CXXNewExpr, mayThrow) {35  const FunctionDecl *OperatorNew = Node.getOperatorNew();36  if (!OperatorNew)37    return false;38  return !OperatorNew->getType()->castAs<FunctionProtoType>()->isNothrow();39}40 41} // namespace42 43UnhandledExceptionAtNewCheck::UnhandledExceptionAtNewCheck(44    StringRef Name, ClangTidyContext *Context)45    : ClangTidyCheck(Name, Context) {}46 47void UnhandledExceptionAtNewCheck::registerMatchers(MatchFinder *Finder) {48  auto BadAllocType =49      recordType(hasDeclaration(cxxRecordDecl(hasName("::std::bad_alloc"))));50  auto ExceptionType =51      recordType(hasDeclaration(cxxRecordDecl(hasName("::std::exception"))));52  auto BadAllocReferenceType = referenceType(pointee(BadAllocType));53  auto ExceptionReferenceType = referenceType(pointee(ExceptionType));54 55  auto CatchBadAllocType =56      qualType(hasCanonicalType(anyOf(BadAllocType, BadAllocReferenceType,57                                      ExceptionType, ExceptionReferenceType)));58  auto BadAllocCatchingTryBlock = cxxTryStmt(hasHandlerFor(CatchBadAllocType));59 60  auto FunctionMayNotThrow = functionDecl(isNoThrow());61 62  Finder->addMatcher(cxxNewExpr(mayThrow(),63                                unless(hasAncestor(BadAllocCatchingTryBlock)),64                                hasAncestor(FunctionMayNotThrow))65                         .bind("new-expr"),66                     this);67}68 69void UnhandledExceptionAtNewCheck::check(70    const MatchFinder::MatchResult &Result) {71  const auto *MatchedExpr = Result.Nodes.getNodeAs<CXXNewExpr>("new-expr");72  if (MatchedExpr)73    diag(MatchedExpr->getBeginLoc(),74         "missing exception handler for allocation failure at 'new'");75}76 77} // namespace clang::tidy::bugprone78