24 E = E->IgnoreParenCasts();
26 ParentMapContext &PMap = C.getParentMapContext();
27 DynTypedNodeList
P = PMap.getParents(*E);
30 const Expr *ParentE =
nullptr;
31 while ((ParentE =
P[0].get<Expr>()) && ParentE->IgnoreParenCasts() == E) {
32 P = PMap.getParents(
P[0]);
37 if (
const auto *ParentVarD =
P[0].get<VarDecl>())
38 return ParentVarD->getInit()->IgnoreParenCasts() == E;
43 if (
const auto *BinOp = dyn_cast<BinaryOperator>(ParentE))
44 return BinOp->getOpcode() == BO_Assign &&
45 BinOp->getRHS()->IgnoreParenCasts() == E;
47 return isa<CallExpr, CXXConstructExpr>(ParentE);
80 recordType(hasDeclaration(cxxRecordDecl(hasName(
"::std::bad_alloc"))));
82 recordType(hasDeclaration(cxxRecordDecl(hasName(
"::std::exception"))));
83 auto BadAllocReferenceType = referenceType(pointee(BadAllocType));
84 auto ExceptionReferenceType = referenceType(pointee(ExceptionType));
86 auto CatchBadAllocType =
87 qualType(hasCanonicalType(anyOf(BadAllocType, BadAllocReferenceType,
88 ExceptionType, ExceptionReferenceType)));
89 auto BadAllocCatchingTryBlock = cxxTryStmt(hasHandlerFor(CatchBadAllocType));
91 auto NewExprMayThrow = cxxNewExpr(mayThrow());
92 auto HasNewExpr1 = expr(anyOf(NewExprMayThrow.bind(
"new1"),
93 hasDescendant(NewExprMayThrow.bind(
"new1"))));
94 auto HasNewExpr2 = expr(anyOf(NewExprMayThrow.bind(
"new2"),
95 hasDescendant(NewExprMayThrow.bind(
"new2"))));
99 hasAnyArgument(expr(HasNewExpr1).bind(
"arg1")),
101 expr(HasNewExpr2, unless(equalsBoundNode(
"arg1"))).bind(
"arg2")),
102 hasAncestor(BadAllocCatchingTryBlock)),
106 hasAnyArgument(expr(HasNewExpr1).bind(
"arg1")),
108 expr(HasNewExpr2, unless(equalsBoundNode(
"arg1"))).bind(
"arg2")),
109 unless(isListInitialization()),
110 hasAncestor(BadAllocCatchingTryBlock)),
112 Finder->addMatcher(binaryOperator(hasLHS(HasNewExpr1), hasRHS(HasNewExpr2),
113 unless(hasAnyOperatorName(
"&&",
"||",
",")),
114 hasAncestor(BadAllocCatchingTryBlock)),
117 cxxNewExpr(mayThrow(),
118 hasDescendant(NewExprMayThrow.bind(
"new2_in_new1")),
119 hasAncestor(BadAllocCatchingTryBlock))
125 const MatchFinder::MatchResult &Result) {
126 const auto *NewExpr1 = Result.Nodes.getNodeAs<CXXNewExpr>(
"new1");
127 const auto *NewExpr2 = Result.Nodes.getNodeAs<CXXNewExpr>(
"new2");
128 const auto *NewExpr2InNewExpr1 =
129 Result.Nodes.getNodeAs<CXXNewExpr>(
"new2_in_new1");
131 NewExpr2 = NewExpr2InNewExpr1;
132 assert(NewExpr1 && NewExpr2 &&
"Bound nodes not found.");
144 if (getLangOpts().CPlusPlus17 && NewExpr2InNewExpr1)
145 diag(NewExpr1->getBeginLoc(),
146 "memory allocation may leak if an other allocation is sequenced after "
147 "it and throws an exception")
148 << NewExpr1->getSourceRange() << NewExpr2->getSourceRange();
150 diag(NewExpr1->getBeginLoc(),
151 "memory allocation may leak if an other allocation is sequenced after "
152 "it and throws an exception; order of these allocations is undefined")
153 << NewExpr1->getSourceRange() << NewExpr2->getSourceRange();