-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang][sema][NFC] Clean up builtin arg checking functions #158615
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
+26
−23
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
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesAlways take an Full diff: https://github.com/llvm/llvm-project/pull/158615.diff 2 Files Affected:
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 7e00085685b21..41229e8cb644d 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -2819,26 +2819,27 @@ class Sema final : public SemaBase {
/// BuiltinConstantArg - Handle a check if argument ArgNum of CallExpr
/// TheCall is a constant expression.
- bool BuiltinConstantArg(CallExpr *TheCall, int ArgNum, llvm::APSInt &Result);
+ bool BuiltinConstantArg(CallExpr *TheCall, unsigned ArgNum,
+ llvm::APSInt &Result);
/// BuiltinConstantArgRange - Handle a check if argument ArgNum of CallExpr
/// TheCall is a constant expression in the range [Low, High].
- bool BuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, int Low, int High,
- bool RangeIsError = true);
+ bool BuiltinConstantArgRange(CallExpr *TheCall, unsigned ArgNum, int Low,
+ int High, bool RangeIsError = true);
/// BuiltinConstantArgMultiple - Handle a check if argument ArgNum of CallExpr
/// TheCall is a constant expression is a multiple of Num..
- bool BuiltinConstantArgMultiple(CallExpr *TheCall, int ArgNum,
+ bool BuiltinConstantArgMultiple(CallExpr *TheCall, unsigned ArgNum,
unsigned Multiple);
/// BuiltinConstantArgPower2 - Check if argument ArgNum of TheCall is a
/// constant expression representing a power of 2.
- bool BuiltinConstantArgPower2(CallExpr *TheCall, int ArgNum);
+ bool BuiltinConstantArgPower2(CallExpr *TheCall, unsigned ArgNum);
/// BuiltinConstantArgShiftedByte - Check if argument ArgNum of TheCall is
/// a constant expression representing an arbitrary byte value shifted left by
/// a multiple of 8 bits.
- bool BuiltinConstantArgShiftedByte(CallExpr *TheCall, int ArgNum,
+ bool BuiltinConstantArgShiftedByte(CallExpr *TheCall, unsigned ArgNum,
unsigned ArgBits);
/// BuiltinConstantArgShiftedByteOr0xFF - Check if argument ArgNum of
@@ -2846,7 +2847,7 @@ class Sema final : public SemaBase {
/// or a value of the form 0x??FF (i.e. a member of the arithmetic progression
/// 0x00FF, 0x01FF, ..., 0xFFFF). This strange range check is needed for some
/// Arm MVE intrinsics.
- bool BuiltinConstantArgShiftedByteOrXXFF(CallExpr *TheCall, int ArgNum,
+ bool BuiltinConstantArgShiftedByteOrXXFF(CallExpr *TheCall, unsigned ArgNum,
unsigned ArgBits);
/// Checks that a call expression's argument count is at least the desired
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 077f4311ed729..a3d68cd6c6894 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -5886,23 +5886,27 @@ bool Sema::BuiltinOSLogFormat(CallExpr *TheCall) {
return false;
}
-bool Sema::BuiltinConstantArg(CallExpr *TheCall, int ArgNum,
+bool Sema::BuiltinConstantArg(CallExpr *TheCall, unsigned ArgNum,
llvm::APSInt &Result) {
Expr *Arg = TheCall->getArg(ArgNum);
- DeclRefExpr *DRE =cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
- FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
- if (Arg->isTypeDependent() || Arg->isValueDependent()) return false;
+ if (Arg->isTypeDependent() || Arg->isValueDependent())
+ return false;
std::optional<llvm::APSInt> R;
- if (!(R = Arg->getIntegerConstantExpr(Context)))
+ if (!(R = Arg->getIntegerConstantExpr(Context))) {
+ DeclRefExpr *DRE =
+ cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
+ FunctionDecl *FDecl = cast<FunctionDecl>(DRE->getDecl());
return Diag(TheCall->getBeginLoc(), diag::err_constant_integer_arg_type)
<< FDecl->getDeclName() << Arg->getSourceRange();
+ }
Result = *R;
+
return false;
}
-bool Sema::BuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, int Low,
+bool Sema::BuiltinConstantArgRange(CallExpr *TheCall, unsigned ArgNum, int Low,
int High, bool RangeIsError) {
if (isConstantEvaluatedContext())
return false;
@@ -5933,7 +5937,7 @@ bool Sema::BuiltinConstantArgRange(CallExpr *TheCall, int ArgNum, int Low,
return false;
}
-bool Sema::BuiltinConstantArgMultiple(CallExpr *TheCall, int ArgNum,
+bool Sema::BuiltinConstantArgMultiple(CallExpr *TheCall, unsigned ArgNum,
unsigned Num) {
llvm::APSInt Result;
@@ -5953,7 +5957,7 @@ bool Sema::BuiltinConstantArgMultiple(CallExpr *TheCall, int ArgNum,
return false;
}
-bool Sema::BuiltinConstantArgPower2(CallExpr *TheCall, int ArgNum) {
+bool Sema::BuiltinConstantArgPower2(CallExpr *TheCall, unsigned ArgNum) {
llvm::APSInt Result;
// We can't check the value of a dependent argument.
@@ -5965,9 +5969,7 @@ bool Sema::BuiltinConstantArgPower2(CallExpr *TheCall, int ArgNum) {
if (BuiltinConstantArg(TheCall, ArgNum, Result))
return true;
- // Bit-twiddling to test for a power of 2: for x > 0, x & (x-1) is zero if
- // and only if x is a power of 2.
- if (Result.isStrictlyPositive() && (Result & (Result - 1)) == 0)
+ if (!Result.isPowerOf2())
return false;
return Diag(TheCall->getBeginLoc(), diag::err_argument_not_power_of_2)
@@ -5996,7 +5998,7 @@ static bool IsShiftedByte(llvm::APSInt Value) {
}
}
-bool Sema::BuiltinConstantArgShiftedByte(CallExpr *TheCall, int ArgNum,
+bool Sema::BuiltinConstantArgShiftedByte(CallExpr *TheCall, unsigned ArgNum,
unsigned ArgBits) {
llvm::APSInt Result;
@@ -6020,7 +6022,8 @@ bool Sema::BuiltinConstantArgShiftedByte(CallExpr *TheCall, int ArgNum,
<< Arg->getSourceRange();
}
-bool Sema::BuiltinConstantArgShiftedByteOrXXFF(CallExpr *TheCall, int ArgNum,
+bool Sema::BuiltinConstantArgShiftedByteOrXXFF(CallExpr *TheCall,
+ unsigned ArgNum,
unsigned ArgBits) {
llvm::APSInt Result;
|
cor3ntin
approved these changes
Sep 15, 2025
Fznamznon
reviewed
Sep 15, 2025
4f82e5a
to
e6be033
Compare
e6be033
to
2657f76
Compare
erichkeane
approved these changes
Sep 15, 2025
Fznamznon
approved these changes
Sep 15, 2025
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.
Always take an
unsigned
for the argument index, pull some locals in the closest scope and useAPInt::isPowerOf2()
.