[clang] [Clang][counted_by] Load the count through unions by byte offset (PR #228309)

Bill Wendling via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 12 15:54:23 PDT 2026


================
@@ -1258,16 +1252,19 @@ llvm::Value *CodeGenFunction::GetCountedByFieldExprGEP(
     return nullptr;
   }
 
-  RecIndicesTy Indices;
-  getGEPIndicesToField(*this, RD, CountDecl, Indices);
-  if (Indices.empty())
+  // A bit-field count has no byte offset of its own to load from.
+  if (CountDecl->isBitField())
+    return nullptr;
+
+  // Struct GEP indices can't reach a count inside a union, because the union
+  // lowers to its largest member and the indices would walk that one instead.
+  int64_t Offset = 0;
+  if (!getFieldOffsetInBits(*this, RD, CountDecl, Offset))
     return nullptr;
----------------
bwendling wrote:

Please put this code before the if-statement (from `if (CountDecl->isBitField())` to here) above line 1248 (the `if (StructBase->getType()->isPointerType())` if). I want to avoid emitting any code that won't be used.

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


More information about the cfe-commits mailing list