diff mbox series

[v2] Explicitly document that the "counted_by" attribute is only supported in C.

Message ID 20240805201719.1790276-1-qing.zhao@oracle.com
State New
Headers show
Series [v2] Explicitly document that the "counted_by" attribute is only supported in C. | expand

Commit Message

Qing Zhao Aug. 5, 2024, 8:17 p.m. UTC
Compared to the first version, the major changes are:

1. Changed the error as a warning with -Wattributes per Jakub and Jason's
   comments.
2. Update documentation accordingly.
3. Move the testing case to g++.dg/ext
   Add one more new testing case for C++11
   Adjust the testing case according to the new warning.

Bootstrapped and regression tested on both aarch64 and x86.
Okay for committing?

thanks.

Qing.

=================

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.
---
 gcc/c-family/c-attribs.cc                          | 10 +++++++++-
 gcc/doc/extend.texi                                |  3 +++
 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, 36 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/ext/flex-array-counted-by-2.C
 create mode 100644 gcc/testsuite/g++.dg/ext/flex-array-counted-by.C

Comments

Qing Zhao Aug. 12, 2024, 1:51 p.m. UTC | #1
Gentle ping on this simple patch.

thanks.

Qing



> On Aug 5, 2024, at 16:17, Qing Zhao <qing.zhao@oracle.com> wrote:
> 
> Compared to the first version, the major changes are:
> 
> 1. Changed the error as a warning with -Wattributes per Jakub and Jason's
>   comments.
> 2. Update documentation accordingly.
> 3. Move the testing case to g++.dg/ext
>   Add one more new testing case for C++11
>   Adjust the testing case according to the new warning.
> 
> Bootstrapped and regression tested on both aarch64 and x86.
> Okay for committing?
> 
> thanks.
> 
> Qing.
> 
> =================
> 
> 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.
> ---
> gcc/c-family/c-attribs.cc                          | 10 +++++++++-
> gcc/doc/extend.texi                                |  3 +++
> 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, 36 insertions(+), 1 deletion(-)
> create mode 100644 gcc/testsuite/g++.dg/ext/flex-array-counted-by-2.C
> create mode 100644 gcc/testsuite/g++.dg/ext/flex-array-counted-by.C
> 
> diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
> index 685f212683f..4f064457dc4 100644
> --- a/gcc/c-family/c-attribs.cc
> +++ b/gcc/c-family/c-attribs.cc
> @@ -2859,8 +2859,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 48b27ff9f39..827044670e8 100644
> --- a/gcc/doc/extend.texi
> +++ b/gcc/doc/extend.texi
> @@ -7848,6 +7848,9 @@ 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 by default, and the compiler issues a
> +warning with @option{-Wattributes}.
> 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 00000000000..6ac2b509b68
> --- /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 00000000000..8bc79d459df
> --- /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" } */
> +};
> -- 
> 2.31.1
>
Qing Zhao Sept. 3, 2024, 1:59 p.m. UTC | #2
Hi, Jakub, 

I’d like to ping this simple patch again. It’s based on your suggestion in PR116016

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116016#c28

Could you please take a look at the patch and let me know whether its okay for committing to trunk?

thanks.

Qing

> On Aug 12, 2024, at 09:51, Qing Zhao <qing.zhao@oracle.com> wrote:
> 
> Gentle ping on this simple patch.
> 
> thanks.
> 
> Qing
> 
> 
> 
>> On Aug 5, 2024, at 16:17, Qing Zhao <qing.zhao@oracle.com> wrote:
>> 
>> Compared to the first version, the major changes are:
>> 
>> 1. Changed the error as a warning with -Wattributes per Jakub and Jason's
>>  comments.
>> 2. Update documentation accordingly.
>> 3. Move the testing case to g++.dg/ext
>>  Add one more new testing case for C++11
>>  Adjust the testing case according to the new warning.
>> 
>> Bootstrapped and regression tested on both aarch64 and x86.
>> Okay for committing?
>> 
>> thanks.
>> 
>> Qing.
>> 
>> =================
>> 
>> 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.
>> ---
>> gcc/c-family/c-attribs.cc                          | 10 +++++++++-
>> gcc/doc/extend.texi                                |  3 +++
>> 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, 36 insertions(+), 1 deletion(-)
>> create mode 100644 gcc/testsuite/g++.dg/ext/flex-array-counted-by-2.C
>> create mode 100644 gcc/testsuite/g++.dg/ext/flex-array-counted-by.C
>> 
>> diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
>> index 685f212683f..4f064457dc4 100644
>> --- a/gcc/c-family/c-attribs.cc
>> +++ b/gcc/c-family/c-attribs.cc
>> @@ -2859,8 +2859,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 48b27ff9f39..827044670e8 100644
>> --- a/gcc/doc/extend.texi
>> +++ b/gcc/doc/extend.texi
>> @@ -7848,6 +7848,9 @@ 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 by default, and the compiler issues a
>> +warning with @option{-Wattributes}.
>> 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 00000000000..6ac2b509b68
>> --- /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 00000000000..8bc79d459df
>> --- /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" } */
>> +};
>> -- 
>> 2.31.1
>> 
>
Jakub Jelinek Sept. 3, 2024, 2:09 p.m. UTC | #3
On Tue, Sep 03, 2024 at 01:59:45PM +0000, Qing Zhao wrote:
> Hi, Jakub, 
> 
> I’d like to ping this simple patch again. It’s based on your suggestion in PR116016
> 
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116016#c28
> 
> Could you please take a look at the patch and let me know whether its okay for committing to trunk?

Ok with a nit.

> >> --- a/gcc/doc/extend.texi
> >> +++ b/gcc/doc/extend.texi
> >> @@ -7848,6 +7848,9 @@ 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 by default, and the compiler issues a
> >> +warning with @option{-Wattributes}.

Just replace the last 2 lines with
In C++ this attribute is ignored.

	Jakub
Qing Zhao Sept. 3, 2024, 10:37 p.m. UTC | #4
thanks.

Updated per your suggestion and pushed:

https://gcc.gnu.org/pipermail/gcc-cvs/2024-September/408749.html

Qing
> On Sep 3, 2024, at 10:09, Jakub Jelinek <jakub@redhat.com> wrote:
> 
> On Tue, Sep 03, 2024 at 01:59:45PM +0000, Qing Zhao wrote:
>> Hi, Jakub, 
>> 
>> I’d like to ping this simple patch again. It’s based on your suggestion in PR116016
>> 
>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116016#c28
>> 
>> Could you please take a look at the patch and let me know whether its okay for committing to trunk?
> 
> Ok with a nit.
> 
>>>> --- a/gcc/doc/extend.texi
>>>> +++ b/gcc/doc/extend.texi
>>>> @@ -7848,6 +7848,9 @@ 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 by default, and the compiler issues a
>>>> +warning with @option{-Wattributes}.
> 
> Just replace the last 2 lines with
> In C++ this attribute is ignored.
> 
> Jakub
>
diff mbox series

Patch

diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index 685f212683f..4f064457dc4 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -2859,8 +2859,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 48b27ff9f39..827044670e8 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -7848,6 +7848,9 @@  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 by default, and the compiler issues a
+warning with @option{-Wattributes}.
 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 00000000000..6ac2b509b68
--- /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 00000000000..8bc79d459df
--- /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" } */
+};