diff mbox series

[2/2] c++: -Wdeprecated enables later standard deprecations

Message ID 20241003164057.1087609-2-jason@redhat.com
State New
Headers show
Series [1/2] c++: add -Wdeprecated-literal-operator [CWG2521] | expand

Commit Message

Jason Merrill Oct. 3, 2024, 4:40 p.m. UTC
Tested x86_64-pc-linux-gnu, applying to trunk.

-- 8< --

By default -Wdeprecated warns about deprecations in the active standard.
When specified explicitly, let's also warn about deprecations in later
standards.

gcc/c-family/ChangeLog:

	* c-opts.cc (c_common_post_options): Explicit -Wdeprecated enables
	deprecations from later standards.

gcc/ChangeLog:

	* doc/invoke.texi: Explicit -Wdeprecated enables more warnings.
---
 gcc/doc/invoke.texi    | 22 ++++++++++++++++------
 gcc/c-family/c-opts.cc | 17 ++++++++++++-----
 2 files changed, 28 insertions(+), 11 deletions(-)

Comments

Eric Gallager Oct. 3, 2024, 11:09 p.m. UTC | #1
On Thu, Oct 3, 2024 at 12:41 PM Jason Merrill <jason@redhat.com> wrote:
>
> Tested x86_64-pc-linux-gnu, applying to trunk.
>
> -- 8< --
>
> By default -Wdeprecated warns about deprecations in the active standard.
> When specified explicitly, let's also warn about deprecations in later
> standards.
>
> gcc/c-family/ChangeLog:
>
>         * c-opts.cc (c_common_post_options): Explicit -Wdeprecated enables
>         deprecations from later standards.
>
> gcc/ChangeLog:
>
>         * doc/invoke.texi: Explicit -Wdeprecated enables more warnings.
> ---

This strikes me as slightly dangerous. At the very least this should
get a note in the "Caveats" and/or "Porting To" section of the release
notes, as I can see the change breaking some builds that also use
-Werror.

>  gcc/doc/invoke.texi    | 22 ++++++++++++++++------
>  gcc/c-family/c-opts.cc | 17 ++++++++++++-----
>  2 files changed, 28 insertions(+), 11 deletions(-)
>
> diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
> index c90f5b4d58e..d38c1feb86f 100644
> --- a/gcc/doc/invoke.texi
> +++ b/gcc/doc/invoke.texi
> @@ -3864,8 +3864,10 @@ for code that is not valid in C++23 but used to be valid but deprecated
>  in C++20 with a pedantic warning that can be disabled with
>  @option{-Wno-comma-subscript}.
>
> -Enabled by default with @option{-std=c++20} unless @option{-Wno-deprecated},
> -and with @option{-std=c++23} regardless of @option{-Wno-deprecated}.
> +Enabled by default with @option{-std=c++20} unless
> +@option{-Wno-deprecated}, and after @option{-std=c++23} regardless of
> +@option{-Wno-deprecated}.  Before @option{-std=c++20}, enabled with
> +explicit @option{-Wdeprecated}.
>
>  This warning is upgraded to an error by @option{-pedantic-errors} in
>  C++23 mode or later.
> @@ -4012,7 +4014,7 @@ int k = f - e;
>
>  @option{-Wdeprecated-enum-enum-conversion} is enabled by default with
>  @option{-std=c++20}.  In pre-C++20 dialects, this warning can be enabled
> -by @option{-Wenum-conversion}.
> +by @option{-Wenum-conversion} or @option{-Wdeprecated}.
>
>  @opindex Wdeprecated-enum-float-conversion
>  @opindex Wno-deprecated-enum-float-conversion
> @@ -4030,14 +4032,14 @@ bool b = e <= 3.7;
>
>  @option{-Wdeprecated-enum-float-conversion} is enabled by default with
>  @option{-std=c++20}.  In pre-C++20 dialects, this warning can be enabled
> -by @option{-Wenum-conversion}.
> +by @option{-Wenum-conversion} or @option{-Wdeprecated}.
>
>  @opindex Wdeprecated-literal-operator
>  @opindex Wno-deprecated-literal-operator
>  @item -Wdeprecated-literal-operator @r{(C++ and Objective-C++ only)}
>  Warn that the declaration of a user-defined literal operator with a
>  space before the suffix is deprecated.  This warning is enabled by
> -default in C++23.
> +default in C++23, or with explicit @option{-Wdeprecated}.
>
>  @smallexample
>  string operator "" _i18n(const char*, std::size_t); // deprecated
> @@ -4740,7 +4742,8 @@ non-class type, @code{volatile}-qualified function return type,
>  @code{volatile}-qualified parameter type, and structured bindings of a
>  @code{volatile}-qualified type.  This usage was deprecated in C++20.
>
> -Enabled by default with @option{-std=c++20}.
> +Enabled by default with @option{-std=c++20}.  Before
> +@option{-std=c++20}, enabled with explicit @option{-Wdeprecated}.
>
>  @opindex Wzero-as-null-pointer-constant
>  @opindex Wno-zero-as-null-pointer-constant
> @@ -10389,6 +10392,13 @@ disable the error when compiled with @option{-Werror} flag.
>  @item -Wno-deprecated
>  Do not warn about usage of deprecated features.  @xref{Deprecated Features}.
>
> +In C++, explicitly specifying @option{-Wdeprecated} also enables
> +warnings about some features that are deprecated in later language
> +standards, specifically @option{-Wcomma-subscript},
> +@option{-Wvolatile}, @option{-Wdeprecated-enum-float-conversion},
> +@option{-Wdeprecated-enum-enum-conversion}, and
> +@option{-Wdeprecated-literal-operator}.
> +
>  @opindex Wno-deprecated-declarations
>  @opindex Wdeprecated-declarations
>  @item -Wno-deprecated-declarations
> diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
> index 8ff3d966bb6..510e0870140 100644
> --- a/gcc/c-family/c-opts.cc
> +++ b/gcc/c-family/c-opts.cc
> @@ -996,30 +996,37 @@ c_common_post_options (const char **pfilename)
>    SET_OPTION_IF_UNSET (&global_options, &global_options_set, warn_register,
>                        cxx_dialect >= cxx17);
>
> +  /* Explicit -Wdeprecated turns on warnings from later standards.  */
> +  auto deprecated_in = [&](enum cxx_dialect d)
> +  {
> +    if (OPTION_SET_P (warn_deprecated)) return !!warn_deprecated;
> +    return (warn_deprecated && cxx_dialect >= d);
> +  };
> +
>    /* -Wcomma-subscript is enabled by default in C++20.  */
>    SET_OPTION_IF_UNSET (&global_options, &global_options_set,
>                        warn_comma_subscript,
>                        cxx_dialect >= cxx23
> -                      || (cxx_dialect == cxx20 && warn_deprecated));
> +                      || deprecated_in (cxx20));
>
>    /* -Wvolatile is enabled by default in C++20.  */
>    SET_OPTION_IF_UNSET (&global_options, &global_options_set, warn_volatile,
> -                      cxx_dialect >= cxx20 && warn_deprecated);
> +                      deprecated_in (cxx20));
>
>    /* -Wdeprecated-enum-enum-conversion is enabled by default in C++20.  */
>    SET_OPTION_IF_UNSET (&global_options, &global_options_set,
>                        warn_deprecated_enum_enum_conv,
> -                      cxx_dialect >= cxx20 && warn_deprecated);
> +                      deprecated_in (cxx20));
>
>    /* -Wdeprecated-enum-float-conversion is enabled by default in C++20.  */
>    SET_OPTION_IF_UNSET (&global_options, &global_options_set,
>                        warn_deprecated_enum_float_conv,
> -                      cxx_dialect >= cxx20 && warn_deprecated);
> +                      deprecated_in (cxx20));
>
>    /* -Wdeprecated-literal-operator is enabled by default in C++23.  */
>    SET_OPTION_IF_UNSET (&global_options, &global_options_set,
>                        warn_deprecated_literal_operator,
> -                      cxx_dialect >= cxx23 && warn_deprecated);
> +                      deprecated_in (cxx23));
>
>    /* -Wtemplate-id-cdtor is enabled by default in C++20.  */
>    SET_OPTION_IF_UNSET (&global_options, &global_options_set,
> --
> 2.46.2
>
Jason Merrill Oct. 4, 2024, 1:50 a.m. UTC | #2
On 10/3/24 7:09 PM, Eric Gallager wrote:
> On Thu, Oct 3, 2024 at 12:41 PM Jason Merrill <jason@redhat.com> wrote:
>>
>> By default -Wdeprecated warns about deprecations in the active standard.
>> When specified explicitly, let's also warn about deprecations in later
>> standards.
> 
> This strikes me as slightly dangerous. At the very least this should
> get a note in the "Caveats" and/or "Porting To" section of the release
> notes, as I can see the change breaking some builds that also use
> -Werror.

Agreed, but do you think people are using explicit -Wdeprecated?  It's 
had little effect before this patch, since the flag is on by default.

Jason
Sam James Oct. 4, 2024, 1:56 a.m. UTC | #3
Jason Merrill <jason@redhat.com> writes:

> On 10/3/24 7:09 PM, Eric Gallager wrote:
>> On Thu, Oct 3, 2024 at 12:41 PM Jason Merrill <jason@redhat.com> wrote:
>>>
>>> By default -Wdeprecated warns about deprecations in the active standard.
>>> When specified explicitly, let's also warn about deprecations in later
>>> standards.
>> This strikes me as slightly dangerous. At the very least this should
>> get a note in the "Caveats" and/or "Porting To" section of the release
>> notes, as I can see the change breaking some builds that also use
>> -Werror.
>
> Agreed, but do you think people are using explicit -Wdeprecated?  It's
> had little effect before this patch, since the flag is on by default.

On a machine with with 2100 installed packages, I only see aflplusplus,
enchant, catch, gcc, and pam.

thanks,
sam
diff mbox series

Patch

diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index c90f5b4d58e..d38c1feb86f 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -3864,8 +3864,10 @@  for code that is not valid in C++23 but used to be valid but deprecated
 in C++20 with a pedantic warning that can be disabled with
 @option{-Wno-comma-subscript}.
 
-Enabled by default with @option{-std=c++20} unless @option{-Wno-deprecated},
-and with @option{-std=c++23} regardless of @option{-Wno-deprecated}.
+Enabled by default with @option{-std=c++20} unless
+@option{-Wno-deprecated}, and after @option{-std=c++23} regardless of
+@option{-Wno-deprecated}.  Before @option{-std=c++20}, enabled with
+explicit @option{-Wdeprecated}.
 
 This warning is upgraded to an error by @option{-pedantic-errors} in
 C++23 mode or later.
@@ -4012,7 +4014,7 @@  int k = f - e;
 
 @option{-Wdeprecated-enum-enum-conversion} is enabled by default with
 @option{-std=c++20}.  In pre-C++20 dialects, this warning can be enabled
-by @option{-Wenum-conversion}.
+by @option{-Wenum-conversion} or @option{-Wdeprecated}.
 
 @opindex Wdeprecated-enum-float-conversion
 @opindex Wno-deprecated-enum-float-conversion
@@ -4030,14 +4032,14 @@  bool b = e <= 3.7;
 
 @option{-Wdeprecated-enum-float-conversion} is enabled by default with
 @option{-std=c++20}.  In pre-C++20 dialects, this warning can be enabled
-by @option{-Wenum-conversion}.
+by @option{-Wenum-conversion} or @option{-Wdeprecated}.
 
 @opindex Wdeprecated-literal-operator
 @opindex Wno-deprecated-literal-operator
 @item -Wdeprecated-literal-operator @r{(C++ and Objective-C++ only)}
 Warn that the declaration of a user-defined literal operator with a
 space before the suffix is deprecated.  This warning is enabled by
-default in C++23.
+default in C++23, or with explicit @option{-Wdeprecated}.
 
 @smallexample
 string operator "" _i18n(const char*, std::size_t); // deprecated
@@ -4740,7 +4742,8 @@  non-class type, @code{volatile}-qualified function return type,
 @code{volatile}-qualified parameter type, and structured bindings of a
 @code{volatile}-qualified type.  This usage was deprecated in C++20.
 
-Enabled by default with @option{-std=c++20}.
+Enabled by default with @option{-std=c++20}.  Before
+@option{-std=c++20}, enabled with explicit @option{-Wdeprecated}.
 
 @opindex Wzero-as-null-pointer-constant
 @opindex Wno-zero-as-null-pointer-constant
@@ -10389,6 +10392,13 @@  disable the error when compiled with @option{-Werror} flag.
 @item -Wno-deprecated
 Do not warn about usage of deprecated features.  @xref{Deprecated Features}.
 
+In C++, explicitly specifying @option{-Wdeprecated} also enables
+warnings about some features that are deprecated in later language
+standards, specifically @option{-Wcomma-subscript},
+@option{-Wvolatile}, @option{-Wdeprecated-enum-float-conversion},
+@option{-Wdeprecated-enum-enum-conversion}, and
+@option{-Wdeprecated-literal-operator}.
+
 @opindex Wno-deprecated-declarations
 @opindex Wdeprecated-declarations
 @item -Wno-deprecated-declarations
diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
index 8ff3d966bb6..510e0870140 100644
--- a/gcc/c-family/c-opts.cc
+++ b/gcc/c-family/c-opts.cc
@@ -996,30 +996,37 @@  c_common_post_options (const char **pfilename)
   SET_OPTION_IF_UNSET (&global_options, &global_options_set, warn_register,
 		       cxx_dialect >= cxx17);
 
+  /* Explicit -Wdeprecated turns on warnings from later standards.  */
+  auto deprecated_in = [&](enum cxx_dialect d)
+  {
+    if (OPTION_SET_P (warn_deprecated)) return !!warn_deprecated;
+    return (warn_deprecated && cxx_dialect >= d);
+  };
+
   /* -Wcomma-subscript is enabled by default in C++20.  */
   SET_OPTION_IF_UNSET (&global_options, &global_options_set,
 		       warn_comma_subscript,
 		       cxx_dialect >= cxx23
-		       || (cxx_dialect == cxx20 && warn_deprecated));
+		       || deprecated_in (cxx20));
 
   /* -Wvolatile is enabled by default in C++20.  */
   SET_OPTION_IF_UNSET (&global_options, &global_options_set, warn_volatile,
-		       cxx_dialect >= cxx20 && warn_deprecated);
+		       deprecated_in (cxx20));
 
   /* -Wdeprecated-enum-enum-conversion is enabled by default in C++20.  */
   SET_OPTION_IF_UNSET (&global_options, &global_options_set,
 		       warn_deprecated_enum_enum_conv,
-		       cxx_dialect >= cxx20 && warn_deprecated);
+		       deprecated_in (cxx20));
 
   /* -Wdeprecated-enum-float-conversion is enabled by default in C++20.  */
   SET_OPTION_IF_UNSET (&global_options, &global_options_set,
 		       warn_deprecated_enum_float_conv,
-		       cxx_dialect >= cxx20 && warn_deprecated);
+		       deprecated_in (cxx20));
 
   /* -Wdeprecated-literal-operator is enabled by default in C++23.  */
   SET_OPTION_IF_UNSET (&global_options, &global_options_set,
 		       warn_deprecated_literal_operator,
-		       cxx_dialect >= cxx23 && warn_deprecated);
+		       deprecated_in (cxx23));
 
   /* -Wtemplate-id-cdtor is enabled by default in C++20.  */
   SET_OPTION_IF_UNSET (&global_options, &global_options_set,