[clang] [Clang] Add __builtin_bswapg (PR #162433)

Oliver Hunt via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 8 23:06:28 PDT 2025


================
@@ -2200,6 +2200,30 @@ static bool BuiltinCpu(Sema &S, const TargetInfo &TI, CallExpr *TheCall,
   return false;
 }
 
+/// Checks that __builtin_bswapg was called with a single argument, which is an
+/// unsigned integer, and overrides the return value type to the integer type.
+static bool BuiltinBswapg(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();
----------------
ojhunt wrote:

Something is a type or value dependent if it is derived from a template parameter, most trivially:

```cpp
template <class T> T my_function(T t) {
   return __bswapg(t);
}
```

It's possible that the general builtin logic prior to dispatching to your custom sema (it's been a while since I did anything with builtins) but I don't recall it doing so.

Handling this is simple though - you should be able to use `Arg->isTypeDependent()` IIRC. If the argument is dependent you would in this case return false, indicating success (@cor3ntin I'm never quite clear on this with builtins: given this builtin is strictly `T(T)` it seems like the result type should be set to Arg->getType() even in the dependent context?)

Then you'll want some c++ tests: something like the above, probably `auto f(auto t) { return __builtin_bswapg(t); }`, as well as both correct an incorrect instantiations, given the first example something like `my_function(1)`, `my_function("not an number")`

https://github.com/llvm/llvm-project/pull/162433


More information about the cfe-commits mailing list