Skip to content

Commit

Permalink
[Clang] counted_by attr can apply only to C99 flexible array members (#…
Browse files Browse the repository at this point in the history
…72347)

Ensure that we're dealing only with C99 flexible array members. I.e.
ones with incomplete types:

  struct s {
    int count;
    char array[]; /* note: no size specified */
  };

Authored-by: Bill Wendling <isanbard@gmail.com>
  • Loading branch information
bwendling committed Nov 15, 2023
1 parent 1451411 commit 1a09cfb
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -6412,7 +6412,7 @@ def warn_superclass_variable_sized_type_not_at_end : Warning<
" in superclass %3">, InGroup<ObjCFlexibleArray>;

def err_counted_by_attr_not_on_flexible_array_member : Error<
"'counted_by' only applies to flexible array members">;
"'counted_by' only applies to C99 flexible array members">;
def err_counted_by_attr_refers_to_flexible_array : Error<
"'counted_by' cannot refer to the flexible array %0">;
def err_counted_by_must_be_in_structure : Error<
Expand Down
3 changes: 0 additions & 3 deletions clang/lib/AST/DeclBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,6 @@ bool Decl::isFlexibleArrayMemberLike(
using FAMKind = LangOptions::StrictFlexArraysLevelKind;

llvm::APInt Size = CAT->getSize();
FAMKind StrictFlexArraysLevel =
Ctx.getLangOpts().getStrictFlexArraysLevel();

if (StrictFlexArraysLevel == FAMKind::IncompleteOnly)
return false;

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8430,7 +8430,7 @@ bool Sema::CheckCountedByAttr(Scope *S, const FieldDecl *FD) {
}

LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel =
Context.getLangOpts().getStrictFlexArraysLevel();
LangOptions::StrictFlexArraysLevelKind::IncompleteOnly;

if (!Decl::isFlexibleArrayMemberLike(Context, FD, FD->getType(),
StrictFlexArraysLevel, true)) {
Expand Down
9 changes: 7 additions & 2 deletions clang/test/Sema/attr-counted-by.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %clang_cc1 -fstrict-flex-arrays=3 -fsyntax-only -verify %s
// RUN: %clang_cc1 -fsyntax-only -verify %s

#define __counted_by(f) __attribute__((counted_by(f)))

Expand Down Expand Up @@ -38,7 +38,12 @@ struct array_of_ints_count {

struct not_a_fam {
int count;
struct bar *non_fam __counted_by(count); // expected-error {{'counted_by' only applies to flexible array members}}
struct bar *non_fam __counted_by(count); // expected-error {{'counted_by' only applies to C99 flexible array members}}
};

struct not_a_c99_fam {
int count;
struct bar *non_c99_fam[0] __counted_by(count); // expected-error {{'counted_by' only applies to C99 flexible array members}}
};

struct annotated_with_anon_struct {
Expand Down

0 comments on commit 1a09cfb

Please sign in to comment.