https://gcc.gnu.org/g:f9642ffe7814396f31203f4366f78a43a01a215c

commit r15-3434-gf9642ffe7814396f31203f4366f78a43a01a215c
Author: Qing Zhao <qing.z...@oracle.com>
Date:   Tue Sep 3 19:28:23 2024 +0000

    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.

Diff:
---
 gcc/c-family/c-attribs.cc                          | 10 +++++++++-
 gcc/doc/extend.texi                                |  4 ++++
 gcc/testsuite/g++.dg/ext/flex-array-counted-by-2.C | 13 +++++++++++++
 gcc/testsuite/g++.dg/ext/flex-array-counted-by.C   | 11 +++++++++++
 4 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index cf27cd6d5212..79303518dcb7 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 5845bcedf6e5..ebfa6779becb 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 000000000000..6ac2b509b687
--- /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 000000000000..8bc79d459dfc
--- /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" } */
+};

Reply via email to