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

Bill Wendling via libc-commits libc-commits at lists.llvm.org
Fri Dec 15 09:37:14 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();
----------------
bwendling wrote:

The first example is why I'm using `getOuterLexicalRecordContext`, because we want to start looking for the count field from the beginning of the most-enclosing struct.

The second example isn't currently allowed (you'll get an error unless the `count` field is in the outer structure). It's probably a bug when sema checking the attribute. I'll need to address that, but I'd like to do it as a separate patch. If the second example is an anonymous struct, then it's iffy if you can define that as a flexible array on its own. If it's a "named" struct, then it should be quite possible to do that.

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


More information about the libc-commits mailing list