re PR c++/50810 (c++0x-compat does not warn about narrowing conversions)

PR c++/50810
gcc/c-family
	* c-opts.c (c_common_handle_option): Enable -Wnarrowing as part
	of -Wall; include -Wnarrowing in -Wc++0x-compat; adjust default
	Wnarrowing for C++0x and C++98.
	* c.opt ([Wnarrowing]): Update.
gcc/cp
	* typeck2.c (check_narrowing): Adjust OPT_Wnarrowing diagnostics.
	(digest_init_r): Call check_narrowing irrespective of the C++ dialect.
	* decl.c (check_initializer): Likewise.
	* semantics.c (finish_compound_literal): Likewise.
gcc/
	* configure.ac: Add -Wno-narrowing to warning options.
libcpp/
	* configure.ac: Add -Wno-narrowing to warning options.

From-SVN: r180794
This commit is contained in:
Jason Merrill 2011-11-02 16:16:43 -04:00
parent 24748eb48e
commit 25339f1097
17 changed files with 91 additions and 21 deletions

View File

@ -1,3 +1,10 @@
2011-11-02 Paolo Carlini <paolo.carlini@oracle.com>
Jason Merrill <jason@redhat.com>
PR c++/50810
* configure.ac: Add -Wno-narrowing to warning options.
* doc/invoke.texi ([-Wnarrowing], [-Wc++0x-compat]): Update.
2011-11-02 Eric Botcazou <ebotcazou@adacore.com> 2011-11-02 Eric Botcazou <ebotcazou@adacore.com>
PR target/50945 PR target/50945

View File

@ -1,3 +1,11 @@
2011-11-02 Paolo Carlini <paolo.carlini@oracle.com>
PR c++/50810
* c-opts.c (c_common_handle_option): Enable -Wnarrowing as part
of -Wall; include -Wnarrowing in -Wc++0x-compat; adjust default
Wnarrowing for C++0x and C++98.
* c.opt ([Wnarrowing]): Update.
2011-11-01 Paolo Carlini <paolo.carlini@oracle.com> 2011-11-01 Paolo Carlini <paolo.carlini@oracle.com>
PR c++/44277 PR c++/44277

View File

@ -406,6 +406,7 @@ c_common_handle_option (size_t scode, const char *arg, int value,
warn_reorder = value; warn_reorder = value;
warn_cxx0x_compat = value; warn_cxx0x_compat = value;
warn_delnonvdtor = value; warn_delnonvdtor = value;
warn_narrowing = value;
} }
cpp_opts->warn_trigraphs = value; cpp_opts->warn_trigraphs = value;
@ -436,6 +437,10 @@ c_common_handle_option (size_t scode, const char *arg, int value,
cpp_opts->warn_cxx_operator_names = value; cpp_opts->warn_cxx_operator_names = value;
break; break;
case OPT_Wc__0x_compat:
warn_narrowing = value;
break;
case OPT_Wdeprecated: case OPT_Wdeprecated:
cpp_opts->cpp_warn_deprecated = value; cpp_opts->cpp_warn_deprecated = value;
break; break;
@ -997,10 +1002,17 @@ c_common_post_options (const char **pfilename)
if (warn_implicit_function_declaration == -1) if (warn_implicit_function_declaration == -1)
warn_implicit_function_declaration = flag_isoc99; warn_implicit_function_declaration = flag_isoc99;
/* If we're allowing C++0x constructs, don't warn about C++0x
compatibility problems. */
if (cxx_dialect == cxx0x) if (cxx_dialect == cxx0x)
warn_cxx0x_compat = 0; {
/* If we're allowing C++0x constructs, don't warn about C++98
identifiers which are keywords in C++0x. */
warn_cxx0x_compat = 0;
if (warn_narrowing == -1)
warn_narrowing = 1;
}
else if (warn_narrowing == -1)
warn_narrowing = 0;
if (flag_preprocess_only) if (flag_preprocess_only)
{ {

View File

@ -494,8 +494,8 @@ C ObjC C++ ObjC++ Warning
Warn about use of multi-character character constants Warn about use of multi-character character constants
Wnarrowing Wnarrowing
C ObjC C++ ObjC++ Warning Var(warn_narrowing) Init(1) C ObjC C++ ObjC++ Warning Var(warn_narrowing) Init(-1)
-Wno-narrowing In C++0x mode, ignore ill-formed narrowing conversions within { } Warn about narrowing conversions within { } that are ill-formed in C++11
Wnested-externs Wnested-externs
C ObjC Var(warn_nested_externs) Warning C ObjC Var(warn_nested_externs) Warning

3
gcc/configure vendored
View File

@ -6394,11 +6394,12 @@ fi
# * 'long long' # * 'long long'
# * variadic macros # * variadic macros
# * overlong strings # * overlong strings
# * C++11 narrowing conversions in { }
# So, we only use -pedantic if we can disable those warnings. # So, we only use -pedantic if we can disable those warnings.
loose_warn= loose_warn=
save_CFLAGS="$CFLAGS" save_CFLAGS="$CFLAGS"
for option in -W -Wall -Wwrite-strings -Wcast-qual; do for option in -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual; do
as_acx_Woption=`$as_echo "acx_cv_prog_cc_warning_$option" | $as_tr_sh` as_acx_Woption=`$as_echo "acx_cv_prog_cc_warning_$option" | $as_tr_sh`
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CC supports $option" >&5 { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CC supports $option" >&5

View File

@ -329,10 +329,11 @@ GCC_STDINT_TYPES
# * 'long long' # * 'long long'
# * variadic macros # * variadic macros
# * overlong strings # * overlong strings
# * C++11 narrowing conversions in { }
# So, we only use -pedantic if we can disable those warnings. # So, we only use -pedantic if we can disable those warnings.
ACX_PROG_CC_WARNING_OPTS( ACX_PROG_CC_WARNING_OPTS(
m4_quote(m4_do([-W -Wall -Wwrite-strings -Wcast-qual])), [loose_warn]) m4_quote(m4_do([-W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual])), [loose_warn])
ACX_PROG_CC_WARNING_OPTS( ACX_PROG_CC_WARNING_OPTS(
m4_quote(m4_do([-Wstrict-prototypes -Wmissing-prototypes])), m4_quote(m4_do([-Wstrict-prototypes -Wmissing-prototypes])),
[c_loose_warn]) [c_loose_warn])

View File

@ -1,3 +1,11 @@
2011-11-02 Paolo Carlini <paolo.carlini@oracle.com>
PR c++/50810
* typeck2.c (check_narrowing): Adjust OPT_Wnarrowing diagnostics.
(digest_init_r): Call check_narrowing irrespective of the C++ dialect.
* decl.c (check_initializer): Likewise.
* semantics.c (finish_compound_literal): Likewise.
2011-11-02 Paolo Carlini <paolo.carlini@oracle.com> 2011-11-02 Paolo Carlini <paolo.carlini@oracle.com>
PR c++/50956 PR c++/50956

View File

@ -5538,7 +5538,7 @@ check_initializer (tree decl, tree init, int flags, tree *cleanup)
else else
{ {
init = reshape_init (type, init, tf_warning_or_error); init = reshape_init (type, init, tf_warning_or_error);
if (cxx_dialect >= cxx0x && SCALAR_TYPE_P (type)) if (SCALAR_TYPE_P (type))
check_narrowing (type, init); check_narrowing (type, init);
} }
} }

View File

@ -2369,7 +2369,7 @@ finish_compound_literal (tree type, tree compound_literal,
&& check_array_initializer (NULL_TREE, type, compound_literal)) && check_array_initializer (NULL_TREE, type, compound_literal))
return error_mark_node; return error_mark_node;
compound_literal = reshape_init (type, compound_literal, complain); compound_literal = reshape_init (type, compound_literal, complain);
if (cxx_dialect >= cxx0x && SCALAR_TYPE_P (type) if (SCALAR_TYPE_P (type)
&& !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)) && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal))
check_narrowing (type, compound_literal); check_narrowing (type, compound_literal);
if (TREE_CODE (type) == ARRAY_TYPE if (TREE_CODE (type) == ARRAY_TYPE

View File

@ -804,8 +804,16 @@ check_narrowing (tree type, tree init)
} }
if (!ok) if (!ok)
pedwarn (input_location, OPT_Wnarrowing, "narrowing conversion of %qE " {
"from %qT to %qT inside { }", init, ftype, type); if (cxx_dialect >= cxx0x)
pedwarn (EXPR_LOC_OR_HERE (init), OPT_Wnarrowing,
"narrowing conversion of %qE from %qT to %qT inside { }",
init, ftype, type);
else
warning_at (EXPR_LOC_OR_HERE (init), OPT_Wnarrowing,
"narrowing conversion of %qE from %qT to %qT inside { } "
"is ill-formed in C++11", init, ftype, type);
}
} }
/* Process the initializer INIT for a variable of type TYPE, emitting /* Process the initializer INIT for a variable of type TYPE, emitting
@ -902,7 +910,7 @@ digest_init_r (tree type, tree init, bool nested, int flags,
{ {
tree *exp; tree *exp;
if (cxx_dialect != cxx98 && nested) if (nested)
check_narrowing (type, init); check_narrowing (type, init);
init = convert_for_initialization (0, type, init, flags, init = convert_for_initialization (0, type, init, flags,
ICR_INIT, NULL_TREE, 0, ICR_INIT, NULL_TREE, 0,

View File

@ -2363,17 +2363,22 @@ an instance of a derived class through a pointer to a base class if the
base class does not have a virtual destructor. This warning is enabled base class does not have a virtual destructor. This warning is enabled
by @option{-Wall}. by @option{-Wall}.
@item -Wno-narrowing @r{(C++ and Objective-C++ only)} @item -Wnarrowing @r{(C++ and Objective-C++ only)}
@opindex Wnarrowing @opindex Wnarrowing
@opindex Wno-narrowing @opindex Wno-narrowing
With -std=c++11, suppress the diagnostic required by the standard for Warn when a narrowing conversion prohibited by C++11 occurs within
narrowing conversions within @samp{@{ @}}, e.g. @samp{@{ @}}, e.g.
@smallexample @smallexample
int i = @{ 2.2 @}; // error: narrowing from double to int int i = @{ 2.2 @}; // error: narrowing from double to int
@end smallexample @end smallexample
This flag can be useful for compiling valid C++98 code in C++11 mode. This flag is included in @option{-Wall} and @option{-Wc++11-compat}.
With -std=c++11, @option{-Wno-narrowing} suppresses the diagnostic
required by the standard. Note that this does not affect the meaning
of well-formed code; narrowing conversions are still considered
ill-formed in SFINAE context.
@item -Wnoexcept @r{(C++ and Objective-C++ only)} @item -Wnoexcept @r{(C++ and Objective-C++ only)}
@opindex Wnoexcept @opindex Wnoexcept
@ -4062,9 +4067,10 @@ ISO C and ISO C++, e.g.@: request for implicit conversion from
@code{void *} to a pointer to non-@code{void} type. @code{void *} to a pointer to non-@code{void} type.
@item -Wc++11-compat @r{(C++ and Objective-C++ only)} @item -Wc++11-compat @r{(C++ and Objective-C++ only)}
Warn about C++ constructs whose meaning differs between ISO C++ 1998 and Warn about C++ constructs whose meaning differs between ISO C++ 1998
ISO C++ 2011, e.g., identifiers in ISO C++ 1998 that will become keywords and ISO C++ 2011, e.g., identifiers in ISO C++ 1998 that are keywords
in ISO C++ 2011. This warning is enabled by @option{-Wall}. in ISO C++ 2011. This warning turns on @option{-Wnarrowing} and is
enabled by @option{-Wall}.
@item -Wcast-qual @item -Wcast-qual
@opindex Wcast-qual @opindex Wcast-qual

View File

@ -1,3 +1,9 @@
2011-11-02 Paolo Carlini <paolo.carlini@oracle.com>
PR c++/50810
* g++.dg/cpp0x/warn_cxx0x2.C: New.
* g++.dg/cpp0x/warn_cxx0x3.C: Likewise.
2011-11-02 Tom de Vries <tom@codesourcery.com> 2011-11-02 Tom de Vries <tom@codesourcery.com>
PR tree-optimization/50769 PR tree-optimization/50769

View File

@ -0,0 +1,4 @@
// PR c++/50810
// { dg-options "-std=gnu++98 -Wc++11-compat" }
signed char data[] = { 0xff }; // { dg-warning "narrowing" }

View File

@ -0,0 +1,4 @@
// PR c++/50810
// { dg-options "-std=gnu++98 -Wc++11-compat -Wno-narrowing" }
signed char data[] = { 0xff };

View File

@ -1,3 +1,8 @@
2011-11-02 Jason Merrill <jason@redhat.com>
PR c++/50810
* configure.ac: Add -Wno-narrowing to warning options.
2011-10-31 Jason Merrill <jason@redhat.com> 2011-10-31 Jason Merrill <jason@redhat.com>
PR libstdc++/1773 PR libstdc++/1773

2
libcpp/configure vendored
View File

@ -4687,7 +4687,7 @@ test -n "$AUTOHEADER" || AUTOHEADER="$MISSING autoheader"
warn= warn=
save_CFLAGS="$CFLAGS" save_CFLAGS="$CFLAGS"
for option in -W -Wall -Wwrite-strings \ for option in -W -Wall -Wno-narrowing -Wwrite-strings \
-Wmissing-format-attribute; do -Wmissing-format-attribute; do
as_acx_Woption=`$as_echo "acx_cv_prog_cc_warning_$option" | $as_tr_sh` as_acx_Woption=`$as_echo "acx_cv_prog_cc_warning_$option" | $as_tr_sh`

View File

@ -33,7 +33,7 @@ AC_CHECK_PROGS([AUTOHEADER], [autoheader], [$MISSING autoheader])
# Figure out what compiler warnings we can enable. # Figure out what compiler warnings we can enable.
# See config/warnings.m4 for details. # See config/warnings.m4 for details.
ACX_PROG_CC_WARNING_OPTS([-W -Wall -Wwrite-strings \ ACX_PROG_CC_WARNING_OPTS([-W -Wall -Wno-narrowing -Wwrite-strings \
-Wmissing-format-attribute], [warn]) -Wmissing-format-attribute], [warn])
ACX_PROG_CC_WARNING_OPTS([-Wstrict-prototypes -Wmissing-prototypes \ ACX_PROG_CC_WARNING_OPTS([-Wstrict-prototypes -Wmissing-prototypes \
-Wold-style-definition -Wc++-compat], [c_warn]) -Wold-style-definition -Wc++-compat], [c_warn])