[PATCH] D126864: [clang] Introduce -fstrict-flex-arrays=<n> for stricter handling of flexible arrays

Balázs Benics via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 13 03:18:30 PDT 2023


steakhal added a comment.

I notices some inconsistency between this `-fstrict-flex-arrays=N` flag and what the `RecordDecl::hasFlexibleArrayMember()` returns for an example like this:

  typedef unsigned long size_t;
  void *malloc(size_t);
  void free(void *);
  
  void field(void) {
    struct vec { size_t len; int data[0]; };
    struct vec *a = (struct vec*) malloc(sizeof(struct vec) + 10*sizeof(int));
    free(a);
  }

In the example, I use don't specify the `-fstrict-flex-arrays` flag, thus it should default to `0`, which means that any trailing arrays (let it be incomplete or of any concrete size), to be considered as a flexible-array-member.
In the AST, for the `RecordDecl`, I'd expect that the `hasFlexibleArrayMember()` member function would reflect this, and return `true`, however, it returns `false` instead.
It does so because in SemaDecl.cpp Sema::ActOnFields() <https://github.com/llvm/llvm-project/blob/0ce056a814f8dbfd8d9b7720ce1df489c6ba5ddb/clang/lib/Sema/SemaDecl.cpp#L18884-L18893> before doing some FAM-related checks and diagnostics it performs this check, guarding that branch:

  bool IsLastField = (i + 1 == Fields.end());
  if (FDTy->isFunctionType()) {
    // ...
  } else if (FDTy->isIncompleteArrayType() &&
             (Record || isa<ObjCContainerDecl>(EnclosingDecl))) {
    //This is the FAM handling branch.
    // ...
  }

Consequently, Sema will only set the bit for `hasFlexibleArrayMember` if the array is //incomplete//.
So my question is, if the `RecordDecl::hasFlexibleArrayMember()` should be consistent with the `-fstrict-flex-arrays` flag, or not?
@serge-sans-paille @aaron.ballman


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D126864



More information about the cfe-commits mailing list