@@ -119,15 +119,6 @@ namespace {
119
119
return Ctx.getLValueReferenceType(E->getType());
120
120
}
121
121
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
-
131
122
/// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr.
132
123
/// This will look through a single cast.
133
124
///
@@ -147,7 +138,7 @@ namespace {
147
138
E = Cast->getSubExpr()->IgnoreParens();
148
139
149
140
if (const auto *CE = dyn_cast<CallExpr>(E))
150
- return getAllocSizeAttr(CE ) ? CE : nullptr;
141
+ return CE->getCalleeAllocSizeAttr( ) ? CE : nullptr;
151
142
return nullptr;
152
143
}
153
144
@@ -9674,57 +9665,6 @@ bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) {
9674
9665
// Pointer Evaluation
9675
9666
//===----------------------------------------------------------------------===//
9676
9667
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
-
9728
9668
/// Convenience function. LVal's base must be a call to an alloc_size
9729
9669
/// function.
9730
9670
static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
@@ -9734,7 +9674,12 @@ static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
9734
9674
"Can't get the size of a non alloc_size function");
9735
9675
const auto *Base = LVal.getLValueBase().get<const Expr *>();
9736
9676
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;
9738
9683
}
9739
9684
9740
9685
/// Attempts to evaluate the given LValueBase as the result of a call to
@@ -10431,7 +10376,7 @@ bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) {
10431
10376
if (ExprEvaluatorBaseTy::VisitCallExpr(E))
10432
10377
return true;
10433
10378
10434
- if (!(InvalidBaseOK && getAllocSizeAttr(E )))
10379
+ if (!(InvalidBaseOK && E->getCalleeAllocSizeAttr( )))
10435
10380
return false;
10436
10381
10437
10382
Result.setInvalid(E);
0 commit comments