Skip to content

Commit 1a09cfb

Browse files
authored
[Clang] counted_by attr can apply only to C99 flexible array members (#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 <[email protected]>
1 parent 1451411 commit 1a09cfb

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6412,7 +6412,7 @@ def warn_superclass_variable_sized_type_not_at_end : Warning<
64126412
" in superclass %3">, InGroup<ObjCFlexibleArray>;
64136413

64146414
def err_counted_by_attr_not_on_flexible_array_member : Error<
6415-
"'counted_by' only applies to flexible array members">;
6415+
"'counted_by' only applies to C99 flexible array members">;
64166416
def err_counted_by_attr_refers_to_flexible_array : Error<
64176417
"'counted_by' cannot refer to the flexible array %0">;
64186418
def err_counted_by_must_be_in_structure : Error<

clang/lib/AST/DeclBase.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,6 @@ bool Decl::isFlexibleArrayMemberLike(
421421
using FAMKind = LangOptions::StrictFlexArraysLevelKind;
422422

423423
llvm::APInt Size = CAT->getSize();
424-
FAMKind StrictFlexArraysLevel =
425-
Ctx.getLangOpts().getStrictFlexArraysLevel();
426-
427424
if (StrictFlexArraysLevel == FAMKind::IncompleteOnly)
428425
return false;
429426

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8430,7 +8430,7 @@ bool Sema::CheckCountedByAttr(Scope *S, const FieldDecl *FD) {
84308430
}
84318431

84328432
LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel =
8433-
Context.getLangOpts().getStrictFlexArraysLevel();
8433+
LangOptions::StrictFlexArraysLevelKind::IncompleteOnly;
84348434

84358435
if (!Decl::isFlexibleArrayMemberLike(Context, FD, FD->getType(),
84368436
StrictFlexArraysLevel, true)) {

clang/test/Sema/attr-counted-by.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -fstrict-flex-arrays=3 -fsyntax-only -verify %s
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s
22

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

@@ -38,7 +38,12 @@ struct array_of_ints_count {
3838

3939
struct not_a_fam {
4040
int count;
41-
struct bar *non_fam __counted_by(count); // expected-error {{'counted_by' only applies to flexible array members}}
41+
struct bar *non_fam __counted_by(count); // expected-error {{'counted_by' only applies to C99 flexible array members}}
42+
};
43+
44+
struct not_a_c99_fam {
45+
int count;
46+
struct bar *non_c99_fam[0] __counted_by(count); // expected-error {{'counted_by' only applies to C99 flexible array members}}
4247
};
4348

4449
struct annotated_with_anon_struct {

0 commit comments

Comments
 (0)