extend.texi: Deprecate C++ min/max operators.
authorMark Mitchell <mark@codesourcery.com>
Tue, 22 Mar 2005 23:11:31 +0000 (23:11 +0000)
committerMark Mitchell <mmitchel@gcc.gnu.org>
Tue, 22 Mar 2005 23:11:31 +0000 (23:11 +0000)
* doc/extend.texi: Deprecate C++ min/max operators.

* parser.c (cp_parser_warn_min_max): New function.
(cp_parser_binary_expression): Use it.
(cp_parser_assignment_operator_opt): Likewise.
(cp_parser_operator): Likewise.

* g++.dg/opt/max1.C: Run with -Wno-deprecated.
* g++.dg/opt/pr7503-2.C: Likewise.
* g++.dg/opt/pr7503-3.C: Likewise.
* g++.dg/opt/pr7503-4.C: Likewise.
* g++.dg/opt/pr7503-5.C: Likewise.
* g++.dg/warn/minmax.C: New test.

From-SVN: r96899

gcc/ChangeLog
gcc/cp/ChangeLog
gcc/cp/parser.c
gcc/doc/extend.texi
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/opt/max1.C
gcc/testsuite/g++.dg/opt/pr7503-2.C
gcc/testsuite/g++.dg/opt/pr7503-3.C
gcc/testsuite/g++.dg/opt/pr7503-4.C
gcc/testsuite/g++.dg/opt/pr7503-5.C
gcc/testsuite/g++.dg/warn/minmax.C [new file with mode: 0644]

index 9d0a7ef449993b7594fc2bed1633b2b3583683c1..2da080afb841f155712ce95708a51856b817c0dc 100644 (file)
@@ -1,3 +1,7 @@
+2005-03-22  Mark Mitchell  <mark@codesourcery.com>
+
+       * doc/extend.texi: Deprecate C++ min/max operators.
+
 2005-03-22  Zdenek Dvorak  <dvorakz@suse.cz>
 
        * tree-ssa-loop-ivopts.c (determine_iv_cost): Do not try to preserve
index 73dd59fde704da313ddab65ca85e7d5c6fbe5400..237fe33a0189a419df05a6dee0e640fbb3659613 100644 (file)
@@ -1,3 +1,10 @@
+2005-03-22  Mark Mitchell  <mark@codesourcery.com>
+
+       * parser.c (cp_parser_warn_min_max): New function.
+       (cp_parser_binary_expression): Use it.
+       (cp_parser_assignment_operator_opt): Likewise.
+       (cp_parser_operator): Likewise.
+
 2005-03-22  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>
 
        PR c++/19980
index a6145406de9800261e6788c7633777e37fa024ea..d682f3a38aa366dd917c91b6deae51c6da6665c6 100644 (file)
@@ -1787,6 +1787,16 @@ cp_parser_is_keyword (cp_token* token, enum rid keyword)
   return token->keyword == keyword;
 }
 
+/* A minimum or maximum operator has been seen.  As these are
+   deprecated, issue a warning.  */
+
+static inline void
+cp_parser_warn_min_max (void)
+{
+  if (warn_deprecated && !in_system_header)
+    warning ("minimum/maximum operators are deprecated");
+}
+
 /* If not parsing tentatively, issue a diagnostic of the form
       FILE:LINE: MESSAGE before TOKEN
    where TOKEN is the next token in the input stream.  MESSAGE
@@ -5401,6 +5411,9 @@ cp_parser_binary_expression (cp_parser* parser, bool cast_p)
     {
       /* Get an operator token.  */
       token = cp_lexer_peek_token (parser->lexer);
+      if (token->type == CPP_MIN || token->type == CPP_MAX)
+       cp_parser_warn_min_max ();
+
       new_prec = TOKEN_PRECEDENCE (token);
 
       /* Popping an entry off the stack means we completed a subexpression:
@@ -5656,10 +5669,12 @@ cp_parser_assignment_operator_opt (cp_parser* parser)
 
     case CPP_MIN_EQ:
       op = MIN_EXPR;
+      cp_parser_warn_min_max ();
       break;
 
     case CPP_MAX_EQ:
       op = MAX_EXPR;
+      cp_parser_warn_min_max ();
       break;
 
     default:
@@ -8039,18 +8054,22 @@ cp_parser_operator (cp_parser* parser)
       /* Extensions.  */
     case CPP_MIN:
       id = ansi_opname (MIN_EXPR);
+      cp_parser_warn_min_max ();
       break;
 
     case CPP_MAX:
       id = ansi_opname (MAX_EXPR);
+      cp_parser_warn_min_max ();
       break;
 
     case CPP_MIN_EQ:
       id = ansi_assopname (MIN_EXPR);
+      cp_parser_warn_min_max ();
       break;
 
     case CPP_MAX_EQ:
       id = ansi_assopname (MAX_EXPR);
+      cp_parser_warn_min_max ();
       break;
 
     default:
index 1258a60949c16f1177fd5c55ecca4aea55dbe6c7..f09338fbbfe32d2563b2b1830cac1b7c21235d97 100644 (file)
@@ -9130,7 +9130,6 @@ test specifically for GNU C++ (@pxref{Common Predefined Macros,,
 Predefined Macros,cpp,The GNU C Preprocessor}).
 
 @menu
-* Min and Max::                C++ Minimum and maximum operators.
 * Volatiles::          What constitutes an access to a volatile object.
 * Restricted Pointers:: C99 restricted pointers and references.
 * Vague Linkage::       Where G++ puts inlines, vtables and such.
@@ -9147,51 +9146,6 @@ Predefined Macros,cpp,The GNU C Preprocessor}).
 * Backwards Compatibility:: Compatibilities with earlier definitions of C++.
 @end menu
 
-@node Min and Max
-@section Minimum and Maximum Operators in C++
-
-It is very convenient to have operators which return the ``minimum'' or the
-``maximum'' of two arguments.  In GNU C++ (but not in GNU C),
-
-@table @code
-@item @var{a} <? @var{b}
-@findex <?
-@cindex minimum operator
-is the @dfn{minimum}, returning the smaller of the numeric values
-@var{a} and @var{b};
-
-@item @var{a} >? @var{b}
-@findex >?
-@cindex maximum operator
-is the @dfn{maximum}, returning the larger of the numeric values @var{a}
-and @var{b}.
-@end table
-
-These operations are not primitive in ordinary C++, since you can
-use a macro to return the minimum of two things in C++, as in the
-following example.
-
-@smallexample
-#define MIN(X,Y) ((X) < (Y) ? : (X) : (Y))
-@end smallexample
-
-@noindent
-You might then use @w{@samp{int min = MIN (i, j);}} to set @var{min} to
-the minimum value of variables @var{i} and @var{j}.
-
-However, side effects in @code{X} or @code{Y} may cause unintended
-behavior.  For example, @code{MIN (i++, j++)} will fail, incrementing
-the smaller counter twice.  The GNU C @code{typeof} extension allows you
-to write safe macros that avoid this kind of problem (@pxref{Typeof}).
-However, writing @code{MIN} and @code{MAX} as macros also forces you to
-use function-call notation for a fundamental arithmetic operation.
-Using GNU C++ extensions, you can write @w{@samp{int min = i <? j;}}
-instead.
-
-Since @code{<?} and @code{>?} are built into the compiler, they properly
-handle expressions with side-effects;  @w{@samp{int min = i++ <? j++;}}
-works correctly.
-
 @node Volatiles
 @section When is a Volatile Object Accessed?
 @cindex accessing volatiles
@@ -9838,6 +9792,11 @@ by one returning a different pointer type.  This extension to the
 covariant return type rules is now deprecated and will be removed from a
 future version.
 
+The G++ minimum and maximum operators (@samp{<?} and @samp{>?}) and
+their compound forms (@samp{<?=}) and @samp{>?=}) have been deprecated
+and will be removed in a future version.  Code using these operators
+should be modified to use @code{std::min} and @code{std::max} instead.
+
 The named return value extension has been deprecated, and is now
 removed from G++.
 
index 95fa9495bfdd02807cc2656b9200f8cf36c2317a..71347381142b599f9b16fe916a94503acebcd96b 100644 (file)
@@ -1,3 +1,12 @@
+2005-03-22  Mark Mitchell  <mark@codesourcery.com>
+
+       * g++.dg/opt/max1.C: Run with -Wno-deprecated.
+       * g++.dg/opt/pr7503-2.C: Likewise.
+       * g++.dg/opt/pr7503-3.C: Likewise.
+       * g++.dg/opt/pr7503-4.C: Likewise.
+       * g++.dg/opt/pr7503-5.C: Likewise.
+       * g++.dg/warn/minmax.C: New test.
+
 2005-03-22  Francois-Xavier Coudert  <coudert@clipper.ens.fr>
 
        * g77_intrinsics_funcs.f: New test.
index 61b7021da66d704883bb9219f8deb534961b1a48..10a6e57d92dd10ab75549a567c1da45f9b51ddff 100644 (file)
@@ -1,7 +1,7 @@
 /* PR middle-end/19068 */
 /* Test case by Andrew Pinski <pinskia@physics.uc.edu> */
 /* { dg-do run } */
-/* { dg-options "-O2" } */
+/* { dg-options "-O2 -Wno-deprecated" } */
 
 extern "C" void abort (void);
 
index 68bb143e45ee88f343308f75a122f0182ee68306..02ce5988d703532aeb5f194f4e960cebe3111a44 100644 (file)
@@ -1,6 +1,6 @@
 // PR c++/7503
 // { dg-do run }
-// { dg-options "-O2" }
+// { dg-options "-O2 -Wno-deprecated" }
 
 extern "C" void abort();
 
index 34d91ddb6c0d5aba98397185d98011aa7992bbec..4e8de0f207ca52ea03db03f9b9ca01adb4f44872 100644 (file)
@@ -1,6 +1,6 @@
 // PR c++/7503
 // { dg-do compile }
-// { dg-options "-O2" }
+// { dg-options "-O2 -Wno-deprecated" }
 
 extern int A, B;
 
index 06ac901229ffbbf6d2609978805bc93dee08b5f3..7ed70946984346e5d6c2ed0a0d3849b8d73e1e88 100644 (file)
@@ -1,6 +1,6 @@
 // PR c++/7503
 // { dg-do run }
-// { dg-options "-O2" }
+// { dg-options "-O2 -Wno-deprecated" }
 
 extern "C" void abort();
 
index 9e1e719f5c2c2956b0cb89f8f1a0a42ea67a4dfb..236ac93aa3aece57b6a4a247739b32abf6743221 100644 (file)
@@ -1,6 +1,6 @@
 // PR c++/7503
 // { dg-do run }
-// { dg-options "-O2" }
+// { dg-options "-O2 -Wno-deprecated" }
 
 extern "C" void abort();
 
diff --git a/gcc/testsuite/g++.dg/warn/minmax.C b/gcc/testsuite/g++.dg/warn/minmax.C
new file mode 100644 (file)
index 0000000..7431bfc
--- /dev/null
@@ -0,0 +1,15 @@
+int i, j, k;
+
+void f() {
+  i = j <? k; // { dg-warning "deprecated" }
+  i = j >? k; // { dg-warning "deprecated" }
+  i <?= j; // { dg-warning "deprecated" }
+  i >?= j; // { dg-warning "deprecated" }
+}
+
+struct S {
+  void operator<?(int); // { dg-warning "deprecated" }
+  void operator>?(int); // { dg-warning "deprecated" }
+  void operator<?=(int); // { dg-warning "deprecated" }
+  void operator>?=(int); // { dg-warning "deprecated" }
+};