diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc index cf27cd6d52120296300c82706e574f5a77dc739a..79303518dcb72662c8d367fba9c4047f8852e9c0 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -2867,8 +2867,16 @@ 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 ()) + { + warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wattributes, + "%qE attribute is not supported for C++ for now, ignored", + 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 5845bcedf6e546645570a5632622b9a6ab9029f8..ebfa6779becbbf6bd441e16feb458f71d3b8fc5d 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -7926,6 +7926,10 @@ 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 in C for now. +In C++ this attribute is ignored. + 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/ext/flex-array-counted-by-2.C b/gcc/testsuite/g++.dg/ext/flex-array-counted-by-2.C new file mode 100644 index 0000000000000000000000000000000000000000..6ac2b509b6876ff46cf5fc6058c55d80228037f5 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/flex-array-counted-by-2.C @@ -0,0 +1,13 @@ +/* Testing the fact that the attribute counted_by is not supported in C++. */ +/* { dg-do compile { target c++11 } } */ +/* { dg-options "-Wattributes" } */ + +struct trailing { + int count; + int field [[gnu::counted_by (count)]] []; /* { dg-warning "attribute is not supported for C\\+\\+ for now, ignored" } */ +}; + +struct trailing1 { + int count1; + [[gnu::counted_by (count)]] int field []; /* { dg-warning "attribute is not supported for C\\+\\+ for now, ignored" } */ +}; diff --git a/gcc/testsuite/g++.dg/ext/flex-array-counted-by.C b/gcc/testsuite/g++.dg/ext/flex-array-counted-by.C new file mode 100644 index 0000000000000000000000000000000000000000..8bc79d459dfcc661dbdb62a4cc842f068390d06e --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/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 "-Wattributes" } */ + +int size; +int x __attribute ((counted_by (size))); /* { dg-warning "attribute is not supported for C\\+\\+ for now, ignored" } */ + +struct trailing { + int count; + int field[] __attribute ((counted_by (count))); /* { dg-warning "attribute is not supported for C\\+\\+ for now, ignored" } */ +};