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

serge via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 29 12:55:24 PDT 2023


serge-sans-paille added a comment.

Jumping a bit late in the thread, apologize in advance if I missed something.

The GCC version of the attributes seems to be `element_count` (as found in the link https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896 mentionned by @kees) I guess we could align (I personally prefer the GCC name, FWIW)

I may be wrong , but it seems to me that there is nothing specific to FAM there, and this would work as well for

  struct foo {
    size_t num_elements;
     // ...
    struct bar ** data __attribute__((counted_by(num_elements)));
  };
  
  struct foo make_foo(size_t num_elements) {
  struct foo f;
    f.data = malloc(num_elements *
                           sizeof(struct bar *));
  
    f.num_elements = num_elements;
    return f;
  }

Which makes us very close to:

  void stuff(int n, int *data __attribute__((counted_by(n)))) {
      for(int i = 0; i < n; ++i)
        other_stuff(data[i]);
  }

which is very close to VLA, right?



================
Comment at: clang/lib/AST/Expr.cpp:286
-
-  return false;
 }
----------------
I really like this cleanup!


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