@@ -114,15 +114,6 @@ namespace {
114114 return Ctx.getLValueReferenceType(E->getType());
115115 }
116116
117- /// Given a CallExpr, try to get the alloc_size attribute. May return null.
118- static const AllocSizeAttr *getAllocSizeAttr(const CallExpr *CE) {
119- if (const FunctionDecl *DirectCallee = CE->getDirectCallee())
120- return DirectCallee->getAttr<AllocSizeAttr>();
121- if (const Decl *IndirectCallee = CE->getCalleeDecl())
122- return IndirectCallee->getAttr<AllocSizeAttr>();
123- return nullptr;
124- }
125-
126117 /// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr.
127118 /// This will look through a single cast.
128119 ///
@@ -142,7 +133,7 @@ namespace {
142133 E = Cast->getSubExpr()->IgnoreParens();
143134
144135 if (const auto *CE = dyn_cast<CallExpr>(E))
145- return getAllocSizeAttr(CE ) ? CE : nullptr;
136+ return CE->getCalleeAllocSizeAttr( ) ? CE : nullptr;
146137 return nullptr;
147138 }
148139
@@ -9466,57 +9457,6 @@ bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) {
94669457// Pointer Evaluation
94679458//===----------------------------------------------------------------------===//
94689459
9469- /// Attempts to compute the number of bytes available at the pointer
9470- /// returned by a function with the alloc_size attribute. Returns true if we
9471- /// were successful. Places an unsigned number into `Result`.
9472- ///
9473- /// This expects the given CallExpr to be a call to a function with an
9474- /// alloc_size attribute.
9475- static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
9476- const CallExpr *Call,
9477- llvm::APInt &Result) {
9478- const AllocSizeAttr *AllocSize = getAllocSizeAttr(Call);
9479-
9480- assert(AllocSize && AllocSize->getElemSizeParam().isValid());
9481- unsigned SizeArgNo = AllocSize->getElemSizeParam().getASTIndex();
9482- unsigned BitsInSizeT = Ctx.getTypeSize(Ctx.getSizeType());
9483- if (Call->getNumArgs() <= SizeArgNo)
9484- return false;
9485-
9486- auto EvaluateAsSizeT = [&](const Expr *E, APSInt &Into) {
9487- Expr::EvalResult ExprResult;
9488- if (!E->EvaluateAsInt(ExprResult, Ctx, Expr::SE_AllowSideEffects))
9489- return false;
9490- Into = ExprResult.Val.getInt();
9491- if (Into.isNegative() || !Into.isIntN(BitsInSizeT))
9492- return false;
9493- Into = Into.zext(BitsInSizeT);
9494- return true;
9495- };
9496-
9497- APSInt SizeOfElem;
9498- if (!EvaluateAsSizeT(Call->getArg(SizeArgNo), SizeOfElem))
9499- return false;
9500-
9501- if (!AllocSize->getNumElemsParam().isValid()) {
9502- Result = std::move(SizeOfElem);
9503- return true;
9504- }
9505-
9506- APSInt NumberOfElems;
9507- unsigned NumArgNo = AllocSize->getNumElemsParam().getASTIndex();
9508- if (!EvaluateAsSizeT(Call->getArg(NumArgNo), NumberOfElems))
9509- return false;
9510-
9511- bool Overflow;
9512- llvm::APInt BytesAvailable = SizeOfElem.umul_ov(NumberOfElems, Overflow);
9513- if (Overflow)
9514- return false;
9515-
9516- Result = std::move(BytesAvailable);
9517- return true;
9518- }
9519-
95209460/// Convenience function. LVal's base must be a call to an alloc_size
95219461/// function.
95229462static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
@@ -9526,7 +9466,12 @@ static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
95269466 "Can't get the size of a non alloc_size function");
95279467 const auto *Base = LVal.getLValueBase().get<const Expr *>();
95289468 const CallExpr *CE = tryUnwrapAllocSizeCall(Base);
9529- return getBytesReturnedByAllocSizeCall(Ctx, CE, Result);
9469+ std::optional<llvm::APInt> Size = CE->getBytesReturnedByAllocSizeCall(Ctx);
9470+ if (!Size)
9471+ return false;
9472+
9473+ Result = std::move(*Size);
9474+ return true;
95309475}
95319476
95329477/// Attempts to evaluate the given LValueBase as the result of a call to
@@ -10017,7 +9962,7 @@ bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) {
100179962 if (ExprEvaluatorBaseTy::VisitCallExpr(E))
100189963 return true;
100199964
10020- if (!(InvalidBaseOK && getAllocSizeAttr(E )))
9965+ if (!(InvalidBaseOK && E->getCalleeAllocSizeAttr( )))
100219966 return false;
100229967
100239968 Result.setInvalid(E);
0 commit comments