aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorQing Zhao <qing.zhao@oracle.com>2024-09-03 19:28:23 +0000
committerQing Zhao <qing.zhao@oracle.com>2024-09-03 22:35:21 +0000
commitf9642ffe7814396f31203f4366f78a43a01a215c (patch)
tree34978640bfac8c1cb13c73c06fe0210276094ae1 /gcc
parent3775f71c8909b3531fe002138814fa2504ec2e8b (diff)
downloadgcc-f9642ffe7814396f31203f4366f78a43a01a215c.zip
gcc-f9642ffe7814396f31203f4366f78a43a01a215c.tar.gz
gcc-f9642ffe7814396f31203f4366f78a43a01a215c.tar.bz2
Explicitly document that the "counted_by" attribute is only supported in C.
The "counted_by" attribute currently is only supported in C, mention this explicitly in documentation and also issue warnings when see "counted_by" attribute in C++ with -Wattributes. gcc/c-family/ChangeLog: * c-attribs.cc (handle_counted_by_attribute): Is ignored and issues warning with -Wattributes in C++ for now. gcc/ChangeLog: * doc/extend.texi: Explicitly mentions counted_by is available only in C for now. gcc/testsuite/ChangeLog: * g++.dg/ext/flex-array-counted-by.C: New test. * g++.dg/ext/flex-array-counted-by-2.C: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/c-family/c-attribs.cc10
-rw-r--r--gcc/doc/extend.texi4
-rw-r--r--gcc/testsuite/g++.dg/ext/flex-array-counted-by-2.C13
-rw-r--r--gcc/testsuite/g++.dg/ext/flex-array-counted-by.C11
4 files changed, 37 insertions, 1 deletions
diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index cf27cd6..7930351 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 5845bce..ebfa677 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 0000000..6ac2b50
--- /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 0000000..8bc79d4
--- /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" } */
+};