Skip to content

Conversation

@tbaederr
Copy link
Contributor

  1. Return std::nullopt instead of {}.
  2. Rename the new function to evaluate*, it's not a simple getter.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 26, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes
  1. Return std::nullopt instead of {}.
  2. Rename the new function to evaluate*, it's not a simple getter.

Full diff: https://github.com/llvm/llvm-project/pull/155351.diff

4 Files Affected:

  • (modified) clang/include/clang/AST/Expr.h (+1-1)
  • (modified) clang/lib/AST/Expr.cpp (+5-5)
  • (modified) clang/lib/AST/ExprConstant.cpp (+2-1)
  • (modified) clang/lib/Sema/SemaExpr.cpp (+1-1)
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index d2ffe9c2e7dd3..b27648175a3e9 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -3268,7 +3268,7 @@ class CallExpr : public Expr {
   /// 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.
   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 9d1490c2ef834..d84d548acd8ca 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -3542,14 +3542,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;
@@ -3565,7 +3565,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;
@@ -3573,12 +3573,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 ee0ac4effab0e..0ad5504aeda91 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9466,7 +9466,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 6e5cc7837ecc1..779980a405c94 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());

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might suggest updating the comment as well to match the new name, but else, lgtm.

 1) Return `std::nullopt` instead of `{}`.
 2) Rename the new function to evaluate*, it's not a simple getter.
@tbaederr tbaederr merged commit b424207 into llvm:main Aug 27, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants