@@ -119,15 +119,6 @@ namespace {
119119 return Ctx.getLValueReferenceType(E->getType());
120120 }
121121
122- /// Given a CallExpr, try to get the alloc_size attribute. May return null.
123- static const AllocSizeAttr *getAllocSizeAttr(const CallExpr *CE) {
124- if (const FunctionDecl *DirectCallee = CE->getDirectCallee())
125- return DirectCallee->getAttr<AllocSizeAttr>();
126- if (const Decl *IndirectCallee = CE->getCalleeDecl())
127- return IndirectCallee->getAttr<AllocSizeAttr>();
128- return nullptr;
129- }
130-
131122 /// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr.
132123 /// This will look through a single cast.
133124 ///
@@ -147,7 +138,7 @@ namespace {
147138 E = Cast->getSubExpr()->IgnoreParens();
148139
149140 if (const auto *CE = dyn_cast<CallExpr>(E))
150- return getAllocSizeAttr(CE ) ? CE : nullptr;
141+ return CE->getCalleeAllocSizeAttr( ) ? CE : nullptr;
151142 return nullptr;
152143 }
153144
@@ -9674,57 +9665,6 @@ bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) {
96749665// Pointer Evaluation
96759666//===----------------------------------------------------------------------===//
96769667
9677- /// Attempts to compute the number of bytes available at the pointer
9678- /// returned by a function with the alloc_size attribute. Returns true if we
9679- /// were successful. Places an unsigned number into `Result`.
9680- ///
9681- /// This expects the given CallExpr to be a call to a function with an
9682- /// alloc_size attribute.
9683- static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
9684- const CallExpr *Call,
9685- llvm::APInt &Result) {
9686- const AllocSizeAttr *AllocSize = getAllocSizeAttr(Call);
9687-
9688- assert(AllocSize && AllocSize->getElemSizeParam().isValid());
9689- unsigned SizeArgNo = AllocSize->getElemSizeParam().getASTIndex();
9690- unsigned BitsInSizeT = Ctx.getTypeSize(Ctx.getSizeType());
9691- if (Call->getNumArgs() <= SizeArgNo)
9692- return false;
9693-
9694- auto EvaluateAsSizeT = [&](const Expr *E, APSInt &Into) {
9695- Expr::EvalResult ExprResult;
9696- if (!E->EvaluateAsInt(ExprResult, Ctx, Expr::SE_AllowSideEffects))
9697- return false;
9698- Into = ExprResult.Val.getInt();
9699- if (Into.isNegative() || !Into.isIntN(BitsInSizeT))
9700- return false;
9701- Into = Into.zext(BitsInSizeT);
9702- return true;
9703- };
9704-
9705- APSInt SizeOfElem;
9706- if (!EvaluateAsSizeT(Call->getArg(SizeArgNo), SizeOfElem))
9707- return false;
9708-
9709- if (!AllocSize->getNumElemsParam().isValid()) {
9710- Result = std::move(SizeOfElem);
9711- return true;
9712- }
9713-
9714- APSInt NumberOfElems;
9715- unsigned NumArgNo = AllocSize->getNumElemsParam().getASTIndex();
9716- if (!EvaluateAsSizeT(Call->getArg(NumArgNo), NumberOfElems))
9717- return false;
9718-
9719- bool Overflow;
9720- llvm::APInt BytesAvailable = SizeOfElem.umul_ov(NumberOfElems, Overflow);
9721- if (Overflow)
9722- return false;
9723-
9724- Result = std::move(BytesAvailable);
9725- return true;
9726- }
9727-
97289668/// Convenience function. LVal's base must be a call to an alloc_size
97299669/// function.
97309670static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
@@ -9734,7 +9674,12 @@ static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
97349674 "Can't get the size of a non alloc_size function");
97359675 const auto *Base = LVal.getLValueBase().get<const Expr *>();
97369676 const CallExpr *CE = tryUnwrapAllocSizeCall(Base);
9737- return getBytesReturnedByAllocSizeCall(Ctx, CE, Result);
9677+ std::optional<llvm::APInt> Size = CE->getBytesReturnedByAllocSizeCall(Ctx);
9678+ if (!Size)
9679+ return false;
9680+
9681+ Result = std::move(*Size);
9682+ return true;
97389683}
97399684
97409685/// Attempts to evaluate the given LValueBase as the result of a call to
@@ -10431,7 +10376,7 @@ bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) {
1043110376 if (ExprEvaluatorBaseTy::VisitCallExpr(E))
1043210377 return true;
1043310378
10434- if (!(InvalidBaseOK && getAllocSizeAttr(E )))
10379+ if (!(InvalidBaseOK && E->getCalleeAllocSizeAttr( )))
1043510380 return false;
1043610381
1043710382 Result.setInvalid(E);
0 commit comments