-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang] Post-commit review for #150028 #155351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) Changes
Full diff: https://github.com/llvm/llvm-project/pull/155351.diff 4 Files Affected:
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());
|
erichkeane
approved these changes
Aug 26, 2025
Collaborator
erichkeane
left a comment
There was a problem hiding this 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.
erichkeane
approved these changes
Aug 26, 2025
1) Return `std::nullopt` instead of `{}`.
2) Rename the new function to evaluate*, it's not a simple getter.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
std::nulloptinstead of{}.