[clang] [CIR] Implement __builtin_counted_by_ref (PR #235781)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 4 16:06:23 PST 2026


================
@@ -1856,6 +1905,44 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
     return RValue::get(emitBuiltinObjectSize(e->getArg(0), type, resType,
                                              /*EmittedE=*/nullptr, isDynamic));
   }
+  case Builtin::BI__builtin_counted_by_ref: {
+    // Sema has already checked that the argument is a (side-effect free)
+    // member access of a flexible array member or a pointer, and set the
+    // result type to a pointer to the count field if the member has a
+    // counted_by attribute, and to 'void *' otherwise.
+    mlir::Location loc = getLoc(e->getSourceRange());
+
+    // Default to returning '(void *) 0'.
+    mlir::Value result = builder.getNullPtr(convertType(e->getType()), loc);
+
+    const Expr *arg = e->getArg(0)->IgnoreParenImpCasts();
+    if (const auto *me = dyn_cast<MemberExpr>(arg)) {
----------------
andykaylor wrote:

Shouldn't you also be handling the case where arg is a UnaryOperator (or at least reporting an NYI diagnostic)?

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


More information about the cfe-commits mailing list