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

John McCall via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 15 11:03:13 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:

How is the first example supposed to work?  Is the expectation that `__counted_by` will only be enforced if the access expression happens to previously pass through some instance of `A`, and it better be the instance we have in mind?  What happens if I just do `someB->member`?

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


More information about the cfe-commits mailing list