[flang-commits] [clang-tools-extra] [llvm] [compiler-rt] [clang] [flang] [libcxx] [libc] [Clang] Generate the GEP instead of adding AST nodes (PR #73730)

John McCall via flang-commits flang-commits at lists.llvm.org
Thu Dec 14 23:09:46 PST 2023


================
@@ -4073,6 +4221,51 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
       ArrayLV = EmitLValue(Array);
     auto *Idx = EmitIdxAfterBase(/*Promote*/true);
 
+    if (SanOpts.has(SanitizerKind::ArrayBounds)) {
+      // If the array being accessed has a "counted_by" attribute, generate
+      // bounds checking code. The "count" field is at the top level of the
+      // struct or in an anonymous struct, that's also at the top level. Future
+      // expansions may allow the "count" to reside at any place in the struct,
+      // but the value of "counted_by" will be a "simple" path to the count,
+      // i.e. "a.b.count", so we shouldn't need the full force of EmitLValue or
+      // similar to emit the correct GEP.
+      const LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel =
+          getLangOpts().getStrictFlexArraysLevel();
+
+      if (const auto *ME = dyn_cast<MemberExpr>(Array);
+          ME &&
+          ME->isFlexibleArrayMemberLike(getContext(), StrictFlexArraysLevel) &&
+          ME->getMemberDecl()->hasAttr<CountedByAttr>()) {
+        RecordDecl *RD = ME->getMemberDecl()
+                             ->getDeclContext()
+                             ->getOuterLexicalRecordContext();
----------------
rjmccall wrote:

Hmm.  Is the use of `getOuterLexicalRecordContext()` just trying to look through anonymous records?  It makes sense that `counted_by` paths for members of anonymous records would be specified starting from the containing non-anonymous type, so we need to find that type.  But I think `getOuterLexicalRecordContext()` does too much here: it's actively wrong whenever you have a record defined lexically within some other record, which doesn't always look like the anonymous record pattern.

I think `getNonTransparentContext()` would be right, but you should also be able to get the base type of the `counted_by` path from the attribute itself.

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


More information about the flang-commits mailing list