Skip to content

Commit b424207

Browse files
authored
[clang] Post-commit review for #150028 (#155351)
1) Return `std::nullopt` instead of `{}`. 2) Rename the new function to evaluate*, it's not a simple getter.
1 parent 8a9e333 commit b424207

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

clang/include/clang/AST/Expr.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3265,10 +3265,11 @@ class CallExpr : public Expr {
32653265
/// Try to get the alloc_size attribute of the callee. May return null.
32663266
const AllocSizeAttr *getCalleeAllocSizeAttr() const;
32673267

3268-
/// Get the total size in bytes allocated by calling a function decorated with
3269-
/// alloc_size. Returns std::nullopt if the the result cannot be evaluated.
3268+
/// Evaluates the total size in bytes allocated by calling a function
3269+
/// decorated with alloc_size. Returns std::nullopt if the the result cannot
3270+
/// be evaluated.
32703271
std::optional<llvm::APInt>
3271-
getBytesReturnedByAllocSizeCall(const ASTContext &Ctx) const;
3272+
evaluateBytesReturnedByAllocSizeCall(const ASTContext &Ctx) const;
32723273

32733274
bool isCallToStdMove() const;
32743275

clang/lib/AST/Expr.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3533,14 +3533,14 @@ const AllocSizeAttr *CallExpr::getCalleeAllocSizeAttr() const {
35333533
}
35343534

35353535
std::optional<llvm::APInt>
3536-
CallExpr::getBytesReturnedByAllocSizeCall(const ASTContext &Ctx) const {
3536+
CallExpr::evaluateBytesReturnedByAllocSizeCall(const ASTContext &Ctx) const {
35373537
const AllocSizeAttr *AllocSize = getCalleeAllocSizeAttr();
35383538

35393539
assert(AllocSize && AllocSize->getElemSizeParam().isValid());
35403540
unsigned SizeArgNo = AllocSize->getElemSizeParam().getASTIndex();
35413541
unsigned BitsInSizeT = Ctx.getTypeSize(Ctx.getSizeType());
35423542
if (getNumArgs() <= SizeArgNo)
3543-
return {};
3543+
return std::nullopt;
35443544

35453545
auto EvaluateAsSizeT = [&](const Expr *E, llvm::APSInt &Into) {
35463546
Expr::EvalResult ExprResult;
@@ -3556,20 +3556,20 @@ CallExpr::getBytesReturnedByAllocSizeCall(const ASTContext &Ctx) const {
35563556

35573557
llvm::APSInt SizeOfElem;
35583558
if (!EvaluateAsSizeT(getArg(SizeArgNo), SizeOfElem))
3559-
return {};
3559+
return std::nullopt;
35603560

35613561
if (!AllocSize->getNumElemsParam().isValid())
35623562
return SizeOfElem;
35633563

35643564
llvm::APSInt NumberOfElems;
35653565
unsigned NumArgNo = AllocSize->getNumElemsParam().getASTIndex();
35663566
if (!EvaluateAsSizeT(getArg(NumArgNo), NumberOfElems))
3567-
return {};
3567+
return std::nullopt;
35683568

35693569
bool Overflow;
35703570
llvm::APInt BytesAvailable = SizeOfElem.umul_ov(NumberOfElems, Overflow);
35713571
if (Overflow)
3572-
return {};
3572+
return std::nullopt;
35733573

35743574
return BytesAvailable;
35753575
}

clang/lib/AST/ExprConstant.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9465,7 +9465,8 @@ static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
94659465
"Can't get the size of a non alloc_size function");
94669466
const auto *Base = LVal.getLValueBase().get<const Expr *>();
94679467
const CallExpr *CE = tryUnwrapAllocSizeCall(Base);
9468-
std::optional<llvm::APInt> Size = CE->getBytesReturnedByAllocSizeCall(Ctx);
9468+
std::optional<llvm::APInt> Size =
9469+
CE->evaluateBytesReturnedByAllocSizeCall(Ctx);
94699470
if (!Size)
94709471
return false;
94719472

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7836,7 +7836,7 @@ static void CheckSufficientAllocSize(Sema &S, QualType DestType,
78367836
if (!CE->getCalleeAllocSizeAttr())
78377837
return;
78387838
std::optional<llvm::APInt> AllocSize =
7839-
CE->getBytesReturnedByAllocSizeCall(S.Context);
7839+
CE->evaluateBytesReturnedByAllocSizeCall(S.Context);
78407840
if (!AllocSize)
78417841
return;
78427842
auto Size = CharUnits::fromQuantity(AllocSize->getZExtValue());

0 commit comments

Comments
 (0)