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

Bill Wendling via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 17 14:14:30 PDT 2023


void added a comment.

In D148381#4274857 <https://reviews.llvm.org/D148381#4274857>, @nickdesaulniers wrote:

> Cool!
>
> How about some codegen tests?

I'll add those to the next iteration. :-)

> This looks a lot like the EuroLLVM keynote: https://llvm.swoogo.com/2023eurollvm/agenda (Thursday `-fbounds-safety`). What's your plan for reconciling this with the authors of that? Have you reached out to them with this proposal?

Yes, I've talked with Yeoul about their implementation. I wanted to create a version we could experiment with now so that we could be ready for when their implementation comes out. We're also coordinating with the GCC folks. I expect that things will change drastically once Yeoul's proposal is made public.



================
Comment at: clang/include/clang/Basic/Attr.td:4167
+  let Documentation = [ElementCountDocs];
+  let LangOpts = [COnly];
+}
----------------
nickdesaulniers wrote:
> Does C++ not support VLAs?
C++ doesn't support flexible array members or VLAs.


================
Comment at: clang/include/clang/Basic/AttrDocs.td:6957
+  let Content = [{
+Clang supports the ``__element_count__`` attribute for flexible array members.
+  }];
----------------
nickdesaulniers wrote:
> Maybe worth noting that this the number of elements and not the number of bytes?
There's a lot more to be put here. This is just a placeholder. :-)


================
Comment at: clang/lib/CodeGen/CGExpr.cpp:951-952
+    if (auto *ME = dyn_cast<MemberExpr>(CE->getSubExpr())) {
+      if (ME->isFlexibleArrayMemberLike(CGF.getContext(),
+                                        StrictFlexArraysLevel, true)) {
+        if (auto *MD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
----------------
nickdesaulniers wrote:
> eventually, we may want to support non-FAMs:
> 
> ```
> struct foo {
>   size_t count;
>   char arr [PATH_MAX] __attribute((element_count("count")));
> };
> 
> char *foo (size_t offset) {
>   struct foo my_foo = {
>     .count = sizeof("hello"),
>     .arr = "hello",
>   };
>   return &my_foo.arr[offset];
> }
> ```
Maybe? Though this attribute is really only for the sanitizer, which could all ready use `PATH_MAX` to ensure that it doesn't overrun the array.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D148381/new/

https://reviews.llvm.org/D148381



More information about the cfe-commits mailing list