[PATCH] D148381: [WIP][Clang] Add element_count attribute

Bill Wendling via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 14 14:27:46 PDT 2023


void created this revision.
void added reviewers: kees, nickdesaulniers.
Herald added a subscriber: martong.
Herald added a reviewer: shafik.
Herald added a reviewer: aaron.ballman.
Herald added a project: All.
void requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

A very simplisitc implementation of an "element_count" attribute. It's
used for flexible array members to indicate a field in the parent
structure contains the overall element count of the FAM:

  struct foo {
    size_t num_fam_elements;
     // ...
    struct bar *fam[] __attribute__((element_count(num_fam_elements)));
  };
  
  struct foo *foo_alloc(size_t num_elements) {
    struct foo *f = malloc(sizeof(struct foo) + num_elements *
                           sizeof(struct bar *));
  
    f->num_fam_elements = num_elements;
    return f;
  }
  
  $ clang -fsanitize=array-bounds -fstrict-flex-arrays=3 ...

As mentioned, this is for simplistic versions of a structure with a FAM.

Future additions will include supporting, FAMs and counts in
sub-structures.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148381

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/lib/AST/ASTImporter.cpp
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/Sema/SemaDeclAttr.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148381.513749.patch
Type: text/x-patch
Size: 3885 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230414/29cb111b/attachment.bin>


More information about the cfe-commits mailing list