[PATCH] c++: Implement C++20 -Wdeprecated-array-compare [PR97573]

2021-09-30 Thread Marek Polacek via Gcc-patches
This patch addresses one of my leftovers from GCC 11.  C++20 introduced
[depr.array.comp]:
"Equality and relational comparisons between two operands of array type are
deprecated."
so this patch adds -Wdeprecated-array-compare (enabled by default in C++20).

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

PR c++/97573

gcc/c-family/ChangeLog:

* c-opts.c (c_common_post_options): In C++20, turn on
-Wdeprecated-array-compare.
* c.opt (Wdeprecated-array-compare): New option.

gcc/cp/ChangeLog:

* typeck.c (do_warn_deprecated_array_compare): New.
(cp_build_binary_op): Call it for equality and relational comparisons.

gcc/ChangeLog:

* doc/invoke.texi: Document -Wdeprecated-array-compare.

gcc/testsuite/ChangeLog:

* g++.dg/tree-ssa/pr15791-1.C: Add dg-warning.
* g++.dg/cpp2a/array-comp1.C: New test.
* g++.dg/cpp2a/array-comp2.C: New test.
* g++.dg/cpp2a/array-comp3.C: New test.
---
 gcc/c-family/c-opts.c |  5 
 gcc/c-family/c.opt|  4 +++
 gcc/cp/typeck.c   | 28 +++
 gcc/doc/invoke.texi   | 19 -
 gcc/testsuite/g++.dg/cpp2a/array-comp1.C  | 34 +++
 gcc/testsuite/g++.dg/cpp2a/array-comp2.C  | 31 +
 gcc/testsuite/g++.dg/cpp2a/array-comp3.C  | 29 +++
 gcc/testsuite/g++.dg/tree-ssa/pr15791-1.C |  2 +-
 8 files changed, 150 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp1.C
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp2.C
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp3.C

diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index 3eaab5e1530..00b52cc5e12 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -962,6 +962,11 @@ c_common_post_options (const char **pfilename)
   warn_deprecated_enum_float_conv,
   cxx_dialect >= cxx20 && warn_deprecated);
 
+  /* -Wdeprecated-array-compare is enabled by default in C++20.  */
+  SET_OPTION_IF_UNSET (&global_options, &global_options_set,
+  warn_deprecated_array_compare,
+  cxx_dialect >= cxx20 && warn_deprecated);
+
   /* Declone C++ 'structors if -Os.  */
   if (flag_declone_ctor_dtor == -1)
 flag_declone_ctor_dtor = optimize_size;
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 9c151d19870..a4f0ea68594 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -540,6 +540,10 @@ Wdeprecated
 C C++ ObjC ObjC++ CPP(cpp_warn_deprecated) CppReason(CPP_W_DEPRECATED)
 ; Documented in common.opt
 
+Wdeprecated-array-compare
+C++ ObjC++ Var(warn_deprecated_array_compare) Warning
+Warn about deprecated comparisons between two operands of array type.
+
 Wdeprecated-copy
 C++ ObjC++ Var(warn_deprecated_copy) Warning LangEnabledBy(C++ ObjC++, Wextra)
 Mark implicitly-declared copy operations as deprecated if the class has a
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index a2398dbe660..1e3a41104d6 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -40,6 +40,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "attribs.h"
 #include "asan.h"
 #include "gimplify.h"
+#include "tree-pretty-print.h"
 
 static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
 static tree cp_build_function_call (tree, tree, tsubst_flags_t);
@@ -4725,6 +4726,21 @@ do_warn_enum_conversions (location_t loc, enum tree_code 
code, tree type0,
 }
 }
 
+/* Warn about C++20 [depr.array.comp] array comparisons: "Equality
+   and relational comparisons between two operands of array type are
+   deprecated."  */
+
+static inline void
+do_warn_deprecated_array_compare (location_t location, tree_code code,
+ tree op0, tree op1)
+{
+  if (warning_at (location, OPT_Wdeprecated_array_compare,
+ "comparison between two arrays is deprecated"))
+inform (location, "use unary %<+%> which decays operands to pointers "
+   "or %<&%D[0] %s &%D[0]%> to compare the addresses",
+   op0, op_symbol_code (code), op1);
+}
+
 /* Build a binary-operation expression without default conversions.
CODE is the kind of expression to build.
LOCATION is the location_t of the operator in the source code.
@@ -5289,6 +5305,11 @@ cp_build_binary_op (const op_location_t &location,
warning_at (location, OPT_Waddress,
"comparison with string literal results in "
"unspecified behavior");
+ else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
+  && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
+   do_warn_deprecated_array_compare (location, code,
+ stripped_orig_op0,
+ stripped_orig_op1);
}
 
   build_type = boolean

Re: [PATCH] c++: Implement C++20 -Wdeprecated-array-compare [PR97573]

2021-09-30 Thread Jason Merrill via Gcc-patches

On 9/30/21 10:50, Marek Polacek wrote:

This patch addresses one of my leftovers from GCC 11.  C++20 introduced
[depr.array.comp]:
"Equality and relational comparisons between two operands of array type are
deprecated."
so this patch adds -Wdeprecated-array-compare (enabled by default in C++20).


Why not enable it by default in all modes?  It was always pretty dubious 
code.



Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

PR c++/97573

gcc/c-family/ChangeLog:

* c-opts.c (c_common_post_options): In C++20, turn on
-Wdeprecated-array-compare.
* c.opt (Wdeprecated-array-compare): New option.

gcc/cp/ChangeLog:

* typeck.c (do_warn_deprecated_array_compare): New.
(cp_build_binary_op): Call it for equality and relational comparisons.

gcc/ChangeLog:

* doc/invoke.texi: Document -Wdeprecated-array-compare.

gcc/testsuite/ChangeLog:

* g++.dg/tree-ssa/pr15791-1.C: Add dg-warning.
* g++.dg/cpp2a/array-comp1.C: New test.
* g++.dg/cpp2a/array-comp2.C: New test.
* g++.dg/cpp2a/array-comp3.C: New test.
---
  gcc/c-family/c-opts.c |  5 
  gcc/c-family/c.opt|  4 +++
  gcc/cp/typeck.c   | 28 +++
  gcc/doc/invoke.texi   | 19 -
  gcc/testsuite/g++.dg/cpp2a/array-comp1.C  | 34 +++
  gcc/testsuite/g++.dg/cpp2a/array-comp2.C  | 31 +
  gcc/testsuite/g++.dg/cpp2a/array-comp3.C  | 29 +++
  gcc/testsuite/g++.dg/tree-ssa/pr15791-1.C |  2 +-
  8 files changed, 150 insertions(+), 2 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp1.C
  create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp2.C
  create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp3.C

diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index 3eaab5e1530..00b52cc5e12 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -962,6 +962,11 @@ c_common_post_options (const char **pfilename)
   warn_deprecated_enum_float_conv,
   cxx_dialect >= cxx20 && warn_deprecated);
  
+  /* -Wdeprecated-array-compare is enabled by default in C++20.  */

+  SET_OPTION_IF_UNSET (&global_options, &global_options_set,
+  warn_deprecated_array_compare,
+  cxx_dialect >= cxx20 && warn_deprecated);
+
/* Declone C++ 'structors if -Os.  */
if (flag_declone_ctor_dtor == -1)
  flag_declone_ctor_dtor = optimize_size;
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 9c151d19870..a4f0ea68594 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -540,6 +540,10 @@ Wdeprecated
  C C++ ObjC ObjC++ CPP(cpp_warn_deprecated) CppReason(CPP_W_DEPRECATED)
  ; Documented in common.opt
  
+Wdeprecated-array-compare

+C++ ObjC++ Var(warn_deprecated_array_compare) Warning
+Warn about deprecated comparisons between two operands of array type.
+
  Wdeprecated-copy
  C++ ObjC++ Var(warn_deprecated_copy) Warning LangEnabledBy(C++ ObjC++, Wextra)
  Mark implicitly-declared copy operations as deprecated if the class has a
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index a2398dbe660..1e3a41104d6 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -40,6 +40,7 @@ along with GCC; see the file COPYING3.  If not see
  #include "attribs.h"
  #include "asan.h"
  #include "gimplify.h"
+#include "tree-pretty-print.h"
  
  static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);

  static tree cp_build_function_call (tree, tree, tsubst_flags_t);
@@ -4725,6 +4726,21 @@ do_warn_enum_conversions (location_t loc, enum tree_code 
code, tree type0,
  }
  }
  
+/* Warn about C++20 [depr.array.comp] array comparisons: "Equality

+   and relational comparisons between two operands of array type are
+   deprecated."  */
+
+static inline void
+do_warn_deprecated_array_compare (location_t location, tree_code code,
+ tree op0, tree op1)
+{
+  if (warning_at (location, OPT_Wdeprecated_array_compare,
+ "comparison between two arrays is deprecated"))
+inform (location, "use unary %<+%> which decays operands to pointers "
+   "or %<&%D[0] %s &%D[0]%> to compare the addresses",
+   op0, op_symbol_code (code), op1);
+}
+
  /* Build a binary-operation expression without default conversions.
 CODE is the kind of expression to build.
 LOCATION is the location_t of the operator in the source code.
@@ -5289,6 +5305,11 @@ cp_build_binary_op (const op_location_t &location,
warning_at (location, OPT_Waddress,
"comparison with string literal results in "
"unspecified behavior");
+ else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
+  && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
+   do_warn_deprecated_array_compare (location, code,
+  

Re: [PATCH] c++: Implement C++20 -Wdeprecated-array-compare [PR97573]

2021-09-30 Thread Marek Polacek via Gcc-patches
On Thu, Sep 30, 2021 at 03:34:24PM -0400, Jason Merrill wrote:
> On 9/30/21 10:50, Marek Polacek wrote:
> > This patch addresses one of my leftovers from GCC 11.  C++20 introduced
> > [depr.array.comp]:
> > "Equality and relational comparisons between two operands of array type are
> > deprecated."
> > so this patch adds -Wdeprecated-array-compare (enabled by default in C++20).
> 
> Why not enable it by default in all modes?  It was always pretty dubious
> code.

Sure, it could be done, but it kind of complicates things: we'd probably
need a different option and a different message because it seems incorrect
to say "deprecated" in e.g. C++17 when this was only deprecated in C++20.
I'd rather not add another option; if it stays -Wdeprecated-array-compare
but -Wno-deprecated doesn't turn it off that also seems weird.

I could rename it to -Warray-compare, enable by -Wall, and only
append "is deprecated" to the warning message in C++20.  Does that seem
better?
 
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > 
> > PR c++/97573
> > 
> > gcc/c-family/ChangeLog:
> > 
> > * c-opts.c (c_common_post_options): In C++20, turn on
> > -Wdeprecated-array-compare.
> > * c.opt (Wdeprecated-array-compare): New option.
> > 
> > gcc/cp/ChangeLog:
> > 
> > * typeck.c (do_warn_deprecated_array_compare): New.
> > (cp_build_binary_op): Call it for equality and relational comparisons.
> > 
> > gcc/ChangeLog:
> > 
> > * doc/invoke.texi: Document -Wdeprecated-array-compare.
> > 
> > gcc/testsuite/ChangeLog:
> > 
> > * g++.dg/tree-ssa/pr15791-1.C: Add dg-warning.
> > * g++.dg/cpp2a/array-comp1.C: New test.
> > * g++.dg/cpp2a/array-comp2.C: New test.
> > * g++.dg/cpp2a/array-comp3.C: New test.
> > ---
> >   gcc/c-family/c-opts.c |  5 
> >   gcc/c-family/c.opt|  4 +++
> >   gcc/cp/typeck.c   | 28 +++
> >   gcc/doc/invoke.texi   | 19 -
> >   gcc/testsuite/g++.dg/cpp2a/array-comp1.C  | 34 +++
> >   gcc/testsuite/g++.dg/cpp2a/array-comp2.C  | 31 +
> >   gcc/testsuite/g++.dg/cpp2a/array-comp3.C  | 29 +++
> >   gcc/testsuite/g++.dg/tree-ssa/pr15791-1.C |  2 +-
> >   8 files changed, 150 insertions(+), 2 deletions(-)
> >   create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp1.C
> >   create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp2.C
> >   create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp3.C
> > 
> > diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
> > index 3eaab5e1530..00b52cc5e12 100644
> > --- a/gcc/c-family/c-opts.c
> > +++ b/gcc/c-family/c-opts.c
> > @@ -962,6 +962,11 @@ c_common_post_options (const char **pfilename)
> >warn_deprecated_enum_float_conv,
> >cxx_dialect >= cxx20 && warn_deprecated);
> > +  /* -Wdeprecated-array-compare is enabled by default in C++20.  */
> > +  SET_OPTION_IF_UNSET (&global_options, &global_options_set,
> > +  warn_deprecated_array_compare,
> > +  cxx_dialect >= cxx20 && warn_deprecated);
> > +
> > /* Declone C++ 'structors if -Os.  */
> > if (flag_declone_ctor_dtor == -1)
> >   flag_declone_ctor_dtor = optimize_size;
> > diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> > index 9c151d19870..a4f0ea68594 100644
> > --- a/gcc/c-family/c.opt
> > +++ b/gcc/c-family/c.opt
> > @@ -540,6 +540,10 @@ Wdeprecated
> >   C C++ ObjC ObjC++ CPP(cpp_warn_deprecated) CppReason(CPP_W_DEPRECATED)
> >   ; Documented in common.opt
> > +Wdeprecated-array-compare
> > +C++ ObjC++ Var(warn_deprecated_array_compare) Warning
> > +Warn about deprecated comparisons between two operands of array type.
> > +
> >   Wdeprecated-copy
> >   C++ ObjC++ Var(warn_deprecated_copy) Warning LangEnabledBy(C++ ObjC++, 
> > Wextra)
> >   Mark implicitly-declared copy operations as deprecated if the class has a
> > diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
> > index a2398dbe660..1e3a41104d6 100644
> > --- a/gcc/cp/typeck.c
> > +++ b/gcc/cp/typeck.c
> > @@ -40,6 +40,7 @@ along with GCC; see the file COPYING3.  If not see
> >   #include "attribs.h"
> >   #include "asan.h"
> >   #include "gimplify.h"
> > +#include "tree-pretty-print.h"
> >   static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
> >   static tree cp_build_function_call (tree, tree, tsubst_flags_t);
> > @@ -4725,6 +4726,21 @@ do_warn_enum_conversions (location_t loc, enum 
> > tree_code code, tree type0,
> >   }
> >   }
> > +/* Warn about C++20 [depr.array.comp] array comparisons: "Equality
> > +   and relational comparisons between two operands of array type are
> > +   deprecated."  */
> > +
> > +static inline void
> > +do_warn_deprecated_array_compare (location_t location, tree_code code,
> > + tree op0, tree op1)
> > +{
> > +  if (warning_at (location, OPT_Wdeprecated_array_compare,
> 

Re: [PATCH] c++: Implement C++20 -Wdeprecated-array-compare [PR97573]

2021-10-01 Thread Jason Merrill via Gcc-patches

On 9/30/21 17:56, Marek Polacek wrote:

On Thu, Sep 30, 2021 at 03:34:24PM -0400, Jason Merrill wrote:

On 9/30/21 10:50, Marek Polacek wrote:

This patch addresses one of my leftovers from GCC 11.  C++20 introduced
[depr.array.comp]:
"Equality and relational comparisons between two operands of array type are
deprecated."
so this patch adds -Wdeprecated-array-compare (enabled by default in C++20).


Why not enable it by default in all modes?  It was always pretty dubious
code.


Sure, it could be done, but it kind of complicates things: we'd probably
need a different option and a different message because it seems incorrect
to say "deprecated" in e.g. C++17 when this was only deprecated in C++20.


The warning could say "deprecated in C++20", which is always true?


I'd rather not add another option; if it stays -Wdeprecated-array-compare
but -Wno-deprecated doesn't turn it off that also seems weird.

I could rename it to -Warray-compare, enable by -Wall, and only
append "is deprecated" to the warning message in C++20.  Does that seem
better?


That sounds fine too.


Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

PR c++/97573

gcc/c-family/ChangeLog:

* c-opts.c (c_common_post_options): In C++20, turn on
-Wdeprecated-array-compare.
* c.opt (Wdeprecated-array-compare): New option.

gcc/cp/ChangeLog:

* typeck.c (do_warn_deprecated_array_compare): New.
(cp_build_binary_op): Call it for equality and relational comparisons.

gcc/ChangeLog:

* doc/invoke.texi: Document -Wdeprecated-array-compare.

gcc/testsuite/ChangeLog:

* g++.dg/tree-ssa/pr15791-1.C: Add dg-warning.
* g++.dg/cpp2a/array-comp1.C: New test.
* g++.dg/cpp2a/array-comp2.C: New test.
* g++.dg/cpp2a/array-comp3.C: New test.
---
   gcc/c-family/c-opts.c |  5 
   gcc/c-family/c.opt|  4 +++
   gcc/cp/typeck.c   | 28 +++
   gcc/doc/invoke.texi   | 19 -
   gcc/testsuite/g++.dg/cpp2a/array-comp1.C  | 34 +++
   gcc/testsuite/g++.dg/cpp2a/array-comp2.C  | 31 +
   gcc/testsuite/g++.dg/cpp2a/array-comp3.C  | 29 +++
   gcc/testsuite/g++.dg/tree-ssa/pr15791-1.C |  2 +-
   8 files changed, 150 insertions(+), 2 deletions(-)
   create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp1.C
   create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp2.C
   create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp3.C

diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index 3eaab5e1530..00b52cc5e12 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -962,6 +962,11 @@ c_common_post_options (const char **pfilename)
   warn_deprecated_enum_float_conv,
   cxx_dialect >= cxx20 && warn_deprecated);
+  /* -Wdeprecated-array-compare is enabled by default in C++20.  */
+  SET_OPTION_IF_UNSET (&global_options, &global_options_set,
+  warn_deprecated_array_compare,
+  cxx_dialect >= cxx20 && warn_deprecated);
+
 /* Declone C++ 'structors if -Os.  */
 if (flag_declone_ctor_dtor == -1)
   flag_declone_ctor_dtor = optimize_size;
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 9c151d19870..a4f0ea68594 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -540,6 +540,10 @@ Wdeprecated
   C C++ ObjC ObjC++ CPP(cpp_warn_deprecated) CppReason(CPP_W_DEPRECATED)
   ; Documented in common.opt
+Wdeprecated-array-compare
+C++ ObjC++ Var(warn_deprecated_array_compare) Warning
+Warn about deprecated comparisons between two operands of array type.
+
   Wdeprecated-copy
   C++ ObjC++ Var(warn_deprecated_copy) Warning LangEnabledBy(C++ ObjC++, 
Wextra)
   Mark implicitly-declared copy operations as deprecated if the class has a
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index a2398dbe660..1e3a41104d6 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -40,6 +40,7 @@ along with GCC; see the file COPYING3.  If not see
   #include "attribs.h"
   #include "asan.h"
   #include "gimplify.h"
+#include "tree-pretty-print.h"
   static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
   static tree cp_build_function_call (tree, tree, tsubst_flags_t);
@@ -4725,6 +4726,21 @@ do_warn_enum_conversions (location_t loc, enum tree_code 
code, tree type0,
   }
   }
+/* Warn about C++20 [depr.array.comp] array comparisons: "Equality
+   and relational comparisons between two operands of array type are
+   deprecated."  */
+
+static inline void
+do_warn_deprecated_array_compare (location_t location, tree_code code,
+ tree op0, tree op1)
+{
+  if (warning_at (location, OPT_Wdeprecated_array_compare,
+ "comparison between two arrays is deprecated"))
+inform (location, "use unary %<+%> which decays operands to pointers "
+   "or %<&%D[0] %s &

Re: [PATCH] c++: Implement C++20 -Wdeprecated-array-compare [PR97573]

2021-10-01 Thread Martin Sebor via Gcc-patches

On 9/30/21 8:50 AM, Marek Polacek via Gcc-patches wrote:

This patch addresses one of my leftovers from GCC 11.  C++20 introduced
[depr.array.comp]:
"Equality and relational comparisons between two operands of array type are
deprecated."
so this patch adds -Wdeprecated-array-compare (enabled by default in C++20).


A warning like this would be useful in C as well even though there
array equality is not deprecated (though relational expressions
involving distinct objects are undefined).  Recently, while working
on my -Waddress enhancement to "warn for more impossible null
pointer tests​, I noticed Clang warns for some of these equality
tests in both languages (it issues -Wtautological-compare).

Rather that referring to deprecation, if one is necessary, I would
suggest to choose a name for the option that reflects the problem
the warning (and presumably the deprecation in C++) tries to prevent.
That said, since GCC already has both -Waddress and -Wtautological-
compare for these problems, the warning could be issued under either
of these.

Martin



Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

PR c++/97573

gcc/c-family/ChangeLog:

* c-opts.c (c_common_post_options): In C++20, turn on
-Wdeprecated-array-compare.
* c.opt (Wdeprecated-array-compare): New option.

gcc/cp/ChangeLog:

* typeck.c (do_warn_deprecated_array_compare): New.
(cp_build_binary_op): Call it for equality and relational comparisons.

gcc/ChangeLog:

* doc/invoke.texi: Document -Wdeprecated-array-compare.

gcc/testsuite/ChangeLog:

* g++.dg/tree-ssa/pr15791-1.C: Add dg-warning.
* g++.dg/cpp2a/array-comp1.C: New test.
* g++.dg/cpp2a/array-comp2.C: New test.
* g++.dg/cpp2a/array-comp3.C: New test.
---
  gcc/c-family/c-opts.c |  5 
  gcc/c-family/c.opt|  4 +++
  gcc/cp/typeck.c   | 28 +++
  gcc/doc/invoke.texi   | 19 -
  gcc/testsuite/g++.dg/cpp2a/array-comp1.C  | 34 +++
  gcc/testsuite/g++.dg/cpp2a/array-comp2.C  | 31 +
  gcc/testsuite/g++.dg/cpp2a/array-comp3.C  | 29 +++
  gcc/testsuite/g++.dg/tree-ssa/pr15791-1.C |  2 +-
  8 files changed, 150 insertions(+), 2 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp1.C
  create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp2.C
  create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp3.C

diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index 3eaab5e1530..00b52cc5e12 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -962,6 +962,11 @@ c_common_post_options (const char **pfilename)
   warn_deprecated_enum_float_conv,
   cxx_dialect >= cxx20 && warn_deprecated);
  
+  /* -Wdeprecated-array-compare is enabled by default in C++20.  */

+  SET_OPTION_IF_UNSET (&global_options, &global_options_set,
+  warn_deprecated_array_compare,
+  cxx_dialect >= cxx20 && warn_deprecated);
+
/* Declone C++ 'structors if -Os.  */
if (flag_declone_ctor_dtor == -1)
  flag_declone_ctor_dtor = optimize_size;
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 9c151d19870..a4f0ea68594 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -540,6 +540,10 @@ Wdeprecated
  C C++ ObjC ObjC++ CPP(cpp_warn_deprecated) CppReason(CPP_W_DEPRECATED)
  ; Documented in common.opt
  
+Wdeprecated-array-compare

+C++ ObjC++ Var(warn_deprecated_array_compare) Warning
+Warn about deprecated comparisons between two operands of array type.
+
  Wdeprecated-copy
  C++ ObjC++ Var(warn_deprecated_copy) Warning LangEnabledBy(C++ ObjC++, Wextra)
  Mark implicitly-declared copy operations as deprecated if the class has a
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index a2398dbe660..1e3a41104d6 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -40,6 +40,7 @@ along with GCC; see the file COPYING3.  If not see
  #include "attribs.h"
  #include "asan.h"
  #include "gimplify.h"
+#include "tree-pretty-print.h"
  
  static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);

  static tree cp_build_function_call (tree, tree, tsubst_flags_t);
@@ -4725,6 +4726,21 @@ do_warn_enum_conversions (location_t loc, enum tree_code 
code, tree type0,
  }
  }
  
+/* Warn about C++20 [depr.array.comp] array comparisons: "Equality

+   and relational comparisons between two operands of array type are
+   deprecated."  */
+
+static inline void
+do_warn_deprecated_array_compare (location_t location, tree_code code,
+ tree op0, tree op1)
+{
+  if (warning_at (location, OPT_Wdeprecated_array_compare,
+ "comparison between two arrays is deprecated"))
+inform (location, "use unary %<+%> which decays operands to pointers "
+   "or %<&%D[0] %s &%D[0]%> to compare the addresses

Re: [PATCH] c++: Implement C++20 -Wdeprecated-array-compare [PR97573]

2021-10-01 Thread Marek Polacek via Gcc-patches
On Fri, Oct 01, 2021 at 09:16:53AM -0600, Martin Sebor wrote:
> On 9/30/21 8:50 AM, Marek Polacek via Gcc-patches wrote:
> > This patch addresses one of my leftovers from GCC 11.  C++20 introduced
> > [depr.array.comp]:
> > "Equality and relational comparisons between two operands of array type are
> > deprecated."
> > so this patch adds -Wdeprecated-array-compare (enabled by default in C++20).
> 
> A warning like this would be useful in C as well even though there
> array equality is not deprecated (though relational expressions
> involving distinct objects are undefined).  Recently, while working
> on my -Waddress enhancement to "warn for more impossible null
> pointer tests​, I noticed Clang warns for some of these equality
> tests in both languages (it issues -Wtautological-compare).

I'll look into adding this warning to the C FE; it should be trivial.
 
> Rather that referring to deprecation, if one is necessary, I would
> suggest to choose a name for the option that reflects the problem
> the warning (and presumably the deprecation in C++) tries to prevent.
> That said, since GCC already has both -Waddress and -Wtautological-
> compare for these problems, the warning could be issued under either
> of these.

In my previous email I suggested -Warray-compare -- I wanted to avoid
the "deprecated" part outside C++20.

I also noticed the -Wtautological warning clang emits but I don't have
time to look into it.  It probably won't warn for arrays declared with
__attribute__((weak)) so -Warray-compare still makes sense.

> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > 
> > PR c++/97573
> > 
> > gcc/c-family/ChangeLog:
> > 
> > * c-opts.c (c_common_post_options): In C++20, turn on
> > -Wdeprecated-array-compare.
> > * c.opt (Wdeprecated-array-compare): New option.
> > 
> > gcc/cp/ChangeLog:
> > 
> > * typeck.c (do_warn_deprecated_array_compare): New.
> > (cp_build_binary_op): Call it for equality and relational comparisons.
> > 
> > gcc/ChangeLog:
> > 
> > * doc/invoke.texi: Document -Wdeprecated-array-compare.
> > 
> > gcc/testsuite/ChangeLog:
> > 
> > * g++.dg/tree-ssa/pr15791-1.C: Add dg-warning.
> > * g++.dg/cpp2a/array-comp1.C: New test.
> > * g++.dg/cpp2a/array-comp2.C: New test.
> > * g++.dg/cpp2a/array-comp3.C: New test.
> > ---
> >   gcc/c-family/c-opts.c |  5 
> >   gcc/c-family/c.opt|  4 +++
> >   gcc/cp/typeck.c   | 28 +++
> >   gcc/doc/invoke.texi   | 19 -
> >   gcc/testsuite/g++.dg/cpp2a/array-comp1.C  | 34 +++
> >   gcc/testsuite/g++.dg/cpp2a/array-comp2.C  | 31 +
> >   gcc/testsuite/g++.dg/cpp2a/array-comp3.C  | 29 +++
> >   gcc/testsuite/g++.dg/tree-ssa/pr15791-1.C |  2 +-
> >   8 files changed, 150 insertions(+), 2 deletions(-)
> >   create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp1.C
> >   create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp2.C
> >   create mode 100644 gcc/testsuite/g++.dg/cpp2a/array-comp3.C
> > 
> > diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
> > index 3eaab5e1530..00b52cc5e12 100644
> > --- a/gcc/c-family/c-opts.c
> > +++ b/gcc/c-family/c-opts.c
> > @@ -962,6 +962,11 @@ c_common_post_options (const char **pfilename)
> >warn_deprecated_enum_float_conv,
> >cxx_dialect >= cxx20 && warn_deprecated);
> > +  /* -Wdeprecated-array-compare is enabled by default in C++20.  */
> > +  SET_OPTION_IF_UNSET (&global_options, &global_options_set,
> > +  warn_deprecated_array_compare,
> > +  cxx_dialect >= cxx20 && warn_deprecated);
> > +
> > /* Declone C++ 'structors if -Os.  */
> > if (flag_declone_ctor_dtor == -1)
> >   flag_declone_ctor_dtor = optimize_size;
> > diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
> > index 9c151d19870..a4f0ea68594 100644
> > --- a/gcc/c-family/c.opt
> > +++ b/gcc/c-family/c.opt
> > @@ -540,6 +540,10 @@ Wdeprecated
> >   C C++ ObjC ObjC++ CPP(cpp_warn_deprecated) CppReason(CPP_W_DEPRECATED)
> >   ; Documented in common.opt
> > +Wdeprecated-array-compare
> > +C++ ObjC++ Var(warn_deprecated_array_compare) Warning
> > +Warn about deprecated comparisons between two operands of array type.
> > +
> >   Wdeprecated-copy
> >   C++ ObjC++ Var(warn_deprecated_copy) Warning LangEnabledBy(C++ ObjC++, 
> > Wextra)
> >   Mark implicitly-declared copy operations as deprecated if the class has a
> > diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
> > index a2398dbe660..1e3a41104d6 100644
> > --- a/gcc/cp/typeck.c
> > +++ b/gcc/cp/typeck.c
> > @@ -40,6 +40,7 @@ along with GCC; see the file COPYING3.  If not see
> >   #include "attribs.h"
> >   #include "asan.h"
> >   #include "gimplify.h"
> > +#include "tree-pretty-print.h"
> >   static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
> >   static tree cp_build_fun

Re: [PATCH] c++: Implement C++20 -Wdeprecated-array-compare [PR97573]

2021-10-01 Thread Marek Polacek via Gcc-patches
On Fri, Oct 01, 2021 at 09:30:41AM -0400, Jason Merrill wrote:
> On 9/30/21 17:56, Marek Polacek wrote:
> > On Thu, Sep 30, 2021 at 03:34:24PM -0400, Jason Merrill wrote:
> > > On 9/30/21 10:50, Marek Polacek wrote:
> > > > This patch addresses one of my leftovers from GCC 11.  C++20 introduced
> > > > [depr.array.comp]:
> > > > "Equality and relational comparisons between two operands of array type 
> > > > are
> > > > deprecated."
> > > > so this patch adds -Wdeprecated-array-compare (enabled by default in 
> > > > C++20).
> > > 
> > > Why not enable it by default in all modes?  It was always pretty dubious
> > > code.
> > 
> > Sure, it could be done, but it kind of complicates things: we'd probably
> > need a different option and a different message because it seems incorrect
> > to say "deprecated" in e.g. C++17 when this was only deprecated in C++20.
> 
> The warning could say "deprecated in C++20", which is always true?
> 
> > I'd rather not add another option; if it stays -Wdeprecated-array-compare
> > but -Wno-deprecated doesn't turn it off that also seems weird.
> > 
> > I could rename it to -Warray-compare, enable by -Wall, and only
> > append "is deprecated" to the warning message in C++20.  Does that seem
> > better?
> 
> That sounds fine too.

I did this so that I can add the warning to the C FE too.  Please
take a look at

if you get the chance, thanks.

So consider this patch discarded.  I wonder what I could do so that
Patchwork marks it as such, too.

Marek



Re: [PATCH] c++: Implement C++20 -Wdeprecated-array-compare [PR97573]

2021-10-05 Thread Jason Merrill via Gcc-patches

On 10/1/21 16:14, Marek Polacek wrote:

On Fri, Oct 01, 2021 at 09:30:41AM -0400, Jason Merrill wrote:

On 9/30/21 17:56, Marek Polacek wrote:

On Thu, Sep 30, 2021 at 03:34:24PM -0400, Jason Merrill wrote:

On 9/30/21 10:50, Marek Polacek wrote:

This patch addresses one of my leftovers from GCC 11.  C++20 introduced
[depr.array.comp]:
"Equality and relational comparisons between two operands of array type are
deprecated."
so this patch adds -Wdeprecated-array-compare (enabled by default in C++20).


Why not enable it by default in all modes?  It was always pretty dubious
code.


Sure, it could be done, but it kind of complicates things: we'd probably
need a different option and a different message because it seems incorrect
to say "deprecated" in e.g. C++17 when this was only deprecated in C++20.


The warning could say "deprecated in C++20", which is always true?


I'd rather not add another option; if it stays -Wdeprecated-array-compare
but -Wno-deprecated doesn't turn it off that also seems weird.

I could rename it to -Warray-compare, enable by -Wall, and only
append "is deprecated" to the warning message in C++20.  Does that seem
better?


That sounds fine too.


I did this so that I can add the warning to the C FE too.  Please
take a look at

if you get the chance, thanks.

So consider this patch discarded.  I wonder what I could do so that
Patchwork marks it as such, too.


Yes, I'm surprised that there don't seem to be any support for email 
annotations to direct patchwork, only someone with maintainer privs can 
retire a patch.


Jason