@@ -114,15 +114,6 @@ namespace {
114
114
return Ctx.getLValueReferenceType(E->getType());
115
115
}
116
116
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
-
126
117
/// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr.
127
118
/// This will look through a single cast.
128
119
///
@@ -142,7 +133,7 @@ namespace {
142
133
E = Cast->getSubExpr()->IgnoreParens();
143
134
144
135
if (const auto *CE = dyn_cast<CallExpr>(E))
145
- return getAllocSizeAttr(CE ) ? CE : nullptr;
136
+ return CE->getCalleeAllocSizeAttr( ) ? CE : nullptr;
146
137
return nullptr;
147
138
}
148
139
@@ -9466,57 +9457,6 @@ bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) {
9466
9457
// Pointer Evaluation
9467
9458
//===----------------------------------------------------------------------===//
9468
9459
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
-
9520
9460
/// Convenience function. LVal's base must be a call to an alloc_size
9521
9461
/// function.
9522
9462
static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
@@ -9526,7 +9466,12 @@ static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
9526
9466
"Can't get the size of a non alloc_size function");
9527
9467
const auto *Base = LVal.getLValueBase().get<const Expr *>();
9528
9468
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;
9530
9475
}
9531
9476
9532
9477
/// Attempts to evaluate the given LValueBase as the result of a call to
@@ -10017,7 +9962,7 @@ bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) {
10017
9962
if (ExprEvaluatorBaseTy::VisitCallExpr(E))
10018
9963
return true;
10019
9964
10020
- if (!(InvalidBaseOK && getAllocSizeAttr(E )))
9965
+ if (!(InvalidBaseOK && E->getCalleeAllocSizeAttr( )))
10021
9966
return false;
10022
9967
10023
9968
Result.setInvalid(E);
0 commit comments