[clang] [Clang][counted_by] Use the expr's RecordDecl if available (PR #205903)

Yeoul Na via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 25 16:07:05 PDT 2026


================
@@ -1205,12 +1204,21 @@ llvm::Value *CodeGenFunction::GetCountedByFieldExprGEP(
   // Using getOuterLexicalRecordContext() here would be wrong because it walks
   // past named nested structs to the outermost record, causing a crash when a
   // struct with a counted_by FAM is defined nested inside another struct.
-  const RecordDecl *RD = CountDecl->getParent();
-  while (RD->isAnonymousStructOrUnion()) {
-    const auto *Parent = dyn_cast<RecordDecl>(RD->getLexicalParent());
-    if (!Parent)
-      break;
-    RD = Parent;
+  //
+  // The Base expression may itself be a pointer to a field of type 'struct'. If
+  // so, then we want to use that as the "base" RecordDecl.
+  QualType Ty = Base->IgnoreParenNoopCasts(getContext())->getType();
----------------
rapidsna wrote:

Got it! Yes, it seems `IgnoreParenNoopCasts` ignores casts between same widths, so it should work.

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


More information about the cfe-commits mailing list