[clang] [clang]Implement the c23 stdc bit builtins (PR #185978)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 12 06:17:02 PDT 2026
================
@@ -2345,6 +2345,47 @@ static bool BuiltinPopcountg(Sema &S, CallExpr *TheCall) {
return false;
}
+/// Checks stdc bit-utility builtins (__builtin_stdc_*):
+/// bit_ceil, bit_floor, bit_width, count_ones, count_zeros,
+/// first_leading_one, first_leading_zero, first_trailing_one,
+/// first_trailing_zero, has_single_bit, leading_ones, leading_zeros,
+/// trailing_ones, trailing_zeros. They all take a single unsigned integer
+/// argument and return either int, bool, or the argument type depending on the
+/// specific builtin.
+static bool BuiltinStdCBuiltin(Sema &S, CallExpr *TheCall) {
+ if (S.checkArgCount(TheCall, 1))
+ return true;
+
+ ExprResult ArgRes = S.DefaultLvalueConversion(TheCall->getArg(0));
+ if (ArgRes.isInvalid())
+ return true;
+
+ Expr *Arg = ArgRes.get();
+ TheCall->setArg(0, Arg);
+
+ QualType ArgTy = Arg->getType();
+ if (!ArgTy->isUnsignedIntegerType() && !ArgTy->isExtVectorBoolType()) {
+ S.Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
+ << 1 << /* scalar */ 1 << /* unsigned integer ty */ 3 << /* no fp */ 0
+ << ArgTy;
+ return true;
+ }
+
+ switch (TheCall->getBuiltinCallee()) {
----------------
erichkeane wrote:
Instead of this switch, make the 'return' type an argument, either via an enum or by passing something. We've already done a switch on these below, doing it again seems silly.
https://github.com/llvm/llvm-project/pull/185978
More information about the cfe-commits
mailing list