[clang] [clang] Post-commit review for #150028 (PR #155351)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 26 23:57:10 PDT 2025
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/155351
>From df9f96c18433d509abba6b7b3a8128b5510fe824 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 26 Aug 2025 05:57:47 +0200
Subject: [PATCH] [clang] Post-commit review for #150028
1) Return `std::nullopt` instead of `{}`.
2) Rename the new function to evaluate*, it's not a simple getter.
---
clang/include/clang/AST/Expr.h | 7 ++++---
clang/lib/AST/Expr.cpp | 10 +++++-----
clang/lib/AST/ExprConstant.cpp | 3 ++-
clang/lib/Sema/SemaExpr.cpp | 2 +-
4 files changed, 12 insertions(+), 10 deletions(-)
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index d2ffe9c2e7dd3..11d9b04f904df 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -3265,10 +3265,11 @@ class CallExpr : public Expr {
/// Try to get the alloc_size attribute of the callee. May return null.
const AllocSizeAttr *getCalleeAllocSizeAttr() const;
- /// Get the total size in bytes allocated by calling a function decorated with
- /// alloc_size. Returns std::nullopt if the the result cannot be evaluated.
+ /// Evaluates the total size in bytes allocated by calling a function
+ /// decorated with alloc_size. Returns std::nullopt if the the result cannot
+ /// be evaluated.
std::optional<llvm::APInt>
- getBytesReturnedByAllocSizeCall(const ASTContext &Ctx) const;
+ evaluateBytesReturnedByAllocSizeCall(const ASTContext &Ctx) const;
bool isCallToStdMove() const;
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 072d07cb81179..512f152059a01 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -3533,14 +3533,14 @@ const AllocSizeAttr *CallExpr::getCalleeAllocSizeAttr() const {
}
std::optional<llvm::APInt>
-CallExpr::getBytesReturnedByAllocSizeCall(const ASTContext &Ctx) const {
+CallExpr::evaluateBytesReturnedByAllocSizeCall(const ASTContext &Ctx) const {
const AllocSizeAttr *AllocSize = getCalleeAllocSizeAttr();
assert(AllocSize && AllocSize->getElemSizeParam().isValid());
unsigned SizeArgNo = AllocSize->getElemSizeParam().getASTIndex();
unsigned BitsInSizeT = Ctx.getTypeSize(Ctx.getSizeType());
if (getNumArgs() <= SizeArgNo)
- return {};
+ return std::nullopt;
auto EvaluateAsSizeT = [&](const Expr *E, llvm::APSInt &Into) {
Expr::EvalResult ExprResult;
@@ -3556,7 +3556,7 @@ CallExpr::getBytesReturnedByAllocSizeCall(const ASTContext &Ctx) const {
llvm::APSInt SizeOfElem;
if (!EvaluateAsSizeT(getArg(SizeArgNo), SizeOfElem))
- return {};
+ return std::nullopt;
if (!AllocSize->getNumElemsParam().isValid())
return SizeOfElem;
@@ -3564,12 +3564,12 @@ CallExpr::getBytesReturnedByAllocSizeCall(const ASTContext &Ctx) const {
llvm::APSInt NumberOfElems;
unsigned NumArgNo = AllocSize->getNumElemsParam().getASTIndex();
if (!EvaluateAsSizeT(getArg(NumArgNo), NumberOfElems))
- return {};
+ return std::nullopt;
bool Overflow;
llvm::APInt BytesAvailable = SizeOfElem.umul_ov(NumberOfElems, Overflow);
if (Overflow)
- return {};
+ return std::nullopt;
return BytesAvailable;
}
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 19703e40d2696..c93a2186a93e1 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9465,7 +9465,8 @@ static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
"Can't get the size of a non alloc_size function");
const auto *Base = LVal.getLValueBase().get<const Expr *>();
const CallExpr *CE = tryUnwrapAllocSizeCall(Base);
- std::optional<llvm::APInt> Size = CE->getBytesReturnedByAllocSizeCall(Ctx);
+ std::optional<llvm::APInt> Size =
+ CE->evaluateBytesReturnedByAllocSizeCall(Ctx);
if (!Size)
return false;
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 6797353db14bf..29f825b49104e 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -7836,7 +7836,7 @@ static void CheckSufficientAllocSize(Sema &S, QualType DestType,
if (!CE->getCalleeAllocSizeAttr())
return;
std::optional<llvm::APInt> AllocSize =
- CE->getBytesReturnedByAllocSizeCall(S.Context);
+ CE->evaluateBytesReturnedByAllocSizeCall(S.Context);
if (!AllocSize)
return;
auto Size = CharUnits::fromQuantity(AllocSize->getZExtValue());
More information about the cfe-commits
mailing list