As discussed in PR116016:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116016#c48
We should explicitly document this limitation and issue error messages for C++. The "counted_by" attribute currently is only supported in C, mention this explicitly in documentation and also issue error when see "counted_by" attribute in C++. The patch has been bootstrappped and regression tested on both aarch64 and X86, no issue. Okay for committing? thanks. Qing gcc/c-family/ChangeLog: * c-attribs.cc (handle_counted_by_attribute): Issue error for C++. gcc/ChangeLog: * doc/extend.texi: Explicitly mentions counted_by is available only for C. gcc/testsuite/ChangeLog: * g++.dg/flex-array-counted-by.C: New test. --- gcc/c-family/c-attribs.cc | 9 ++++++++- gcc/doc/extend.texi | 1 + gcc/testsuite/g++.dg/flex-array-counted-by.C | 11 +++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/flex-array-counted-by.C diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc index 685f212683f..f936058800b 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -2859,8 +2859,15 @@ handle_counted_by_attribute (tree *node, tree name, tree argval = TREE_VALUE (args); tree old_counted_by = lookup_attribute ("counted_by", DECL_ATTRIBUTES (decl)); + /* This attribute is not supported in C++. */ + if (c_dialect_cxx ()) + { + error_at (DECL_SOURCE_LOCATION (decl), + "%qE attribute is not supported for C++", name); + *no_add_attrs = true; + } /* This attribute only applies to field decls of a structure. */ - if (TREE_CODE (decl) != FIELD_DECL) + else if (TREE_CODE (decl) != FIELD_DECL) { error_at (DECL_SOURCE_LOCATION (decl), "%qE attribute is not allowed for a non-field" diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 48b27ff9f39..f31f3bdb53d 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -7848,6 +7848,7 @@ The @code{counted_by} attribute may be attached to the C99 flexible array member of a structure. It indicates that the number of the elements of the array is given by the field "@var{count}" in the same structure as the flexible array member. +This attribute is available only for C. GCC may use this information to improve detection of object size information for such structures and provide better results in compile-time diagnostics and runtime features like the array bound sanitizer and diff --git a/gcc/testsuite/g++.dg/flex-array-counted-by.C b/gcc/testsuite/g++.dg/flex-array-counted-by.C new file mode 100644 index 00000000000..7f1a345615e --- /dev/null +++ b/gcc/testsuite/g++.dg/flex-array-counted-by.C @@ -0,0 +1,11 @@ +/* Testing the fact that the attribute counted_by is not supported in C++. */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +int size; +int x __attribute ((counted_by (size))); /* { dg-error "attribute is not supported for C\\+\\+" } */ + +struct trailing { + int count; + int field[] __attribute ((counted_by (count))); /* { dg-error "attribute is not supported for C\\+\\+" } */ +}; -- 2.31.1