re PR c++/50810 (c++0x-compat does not warn about narrowing conversions)
authorPaolo Carlini <paolo.carlini@oracle.com>
Sun, 23 Oct 2011 18:34:45 +0000 (18:34 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Sun, 23 Oct 2011 18:34:45 +0000 (18:34 +0000)
/c-family
2011-10-23  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.

/cp
2011-10-23  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.

/testsuite
2011-10-23  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/50810
* g++.dg/cpp0x/warn_cxx0x2.C: New.
* g++.dg/cpp0x/warn_cxx0x3.C: Likewise.

2011-10-23  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/50810
* doc/invoke.texi ([-Wnarrowing], [-Wc++0x-compat]): Update.

From-SVN: r180343

12 files changed:
gcc/ChangeLog
gcc/c-family/ChangeLog
gcc/c-family/c-opts.c
gcc/c-family/c.opt
gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/cp/semantics.c
gcc/cp/typeck2.c
gcc/doc/invoke.texi
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp0x/warn_cxx0x2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/warn_cxx0x3.C [new file with mode: 0644]

index 525ced515199244ae1b0e4eae152cbb9d4babfaa..0c884cc3cad91bc6c8f07ceaa3eeef710a08f067 100644 (file)
@@ -1,3 +1,8 @@
+2011-10-23  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/50810
+       * doc/invoke.texi ([-Wnarrowing], [-Wc++0x-compat]): Update.
+
 2011-10-23  Tom de Vries  <tom@codesourcery.com>
 
        PR tree-optimization/50763
index 706b27378d56fca10be900799580face1953ea71..9cd0b8314c386fa55e92cdf76160f749e79c8695 100644 (file)
@@ -1,3 +1,11 @@
+2011-10-23  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-10-21  Paolo Carlini  <paolo.carlini@oracle.com>
 
        PR c++/45385
index 6869d5c35ef005f03c7660222ac88740e02695c5..14e49332c27d303aeb78337ccefac3cbdd1daef0 100644 (file)
@@ -406,6 +406,7 @@ c_common_handle_option (size_t scode, const char *arg, int value,
          warn_reorder = value;
           warn_cxx0x_compat = value;
           warn_delnonvdtor = value;
+         warn_narrowing = 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;
       break;
 
+    case OPT_Wc__0x_compat:
+      warn_narrowing = value;
+      break;
+
     case OPT_Wdeprecated:
       cpp_opts->cpp_warn_deprecated = value;
       break;
@@ -997,10 +1002,17 @@ c_common_post_options (const char **pfilename)
   if (warn_implicit_function_declaration == -1)
     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)
-    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)
     {
index bfc1a7c922ce30d86e26b6779cfb42ba2996f120..0b9e44a928d6685f59e9029d36499964433ee2a4 100644 (file)
@@ -490,8 +490,8 @@ C ObjC C++ ObjC++ Warning
 Warn about use of multi-character character constants
 
 Wnarrowing
-C ObjC C++ ObjC++ Warning Var(warn_narrowing) Init(1)
--Wno-narrowing   In C++0x mode, ignore ill-formed narrowing conversions within { }
+C ObjC C++ ObjC++ Warning Var(warn_narrowing) Init(-1) Warning
+Warn about ill-formed narrowing conversions within { }
 
 Wnested-externs
 C ObjC Var(warn_nested_externs) Warning
index f3eea05d5f93c23ff9d74c6f3836947e7ec9d121..de5fab9cfbb7237e7a8437184acb8767d991b409 100644 (file)
@@ -1,3 +1,11 @@
+2011-10-23  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-10-21  Paolo Carlini  <paolo.carlini@oracle.com>
 
        PR c++/45385
index a21cf461aabe9cc55ee60a84801ff7394d5d7418..f1ecca240d3bdb991fb622ccf830952acdcafd83 100644 (file)
@@ -5523,7 +5523,7 @@ check_initializer (tree decl, tree init, int flags, tree *cleanup)
          else
            {
              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);
            }
        }
index 42195be4a81713340ca53d23bf4d358952bd21ee..9fec33422b30ddf8a4e3e584912f6f0a16fc2c53 100644 (file)
@@ -2369,7 +2369,7 @@ finish_compound_literal (tree type, tree compound_literal,
       && check_array_initializer (NULL_TREE, type, compound_literal))
     return error_mark_node;
   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))
     check_narrowing (type, compound_literal);
   if (TREE_CODE (type) == ARRAY_TYPE
index 0cb1104dc384336b8486b58c65b851d2e8f9473a..76006b6cbbda9acf23c77970302c609b62779291 100644 (file)
@@ -803,8 +803,10 @@ check_narrowing (tree type, tree init)
     }
 
   if (!ok)
-    pedwarn (input_location, OPT_Wnarrowing, "narrowing conversion of %qE "
-            "from %qT to %qT inside { }", init, ftype, type);
+    emit_diagnostic ((cxx_dialect != cxx98) ? DK_PEDWARN : DK_WARNING,
+                    input_location, OPT_Wnarrowing,
+                    "narrowing conversion of %qE from %qT to %qT inside { }",
+                    init, ftype, type);
 }
 
 /* Process the initializer INIT for a variable of type TYPE, emitting
@@ -901,7 +903,7 @@ digest_init_r (tree type, tree init, bool nested, int flags,
     {
       tree *exp;
 
-      if (cxx_dialect != cxx98 && nested)
+      if (nested)
        check_narrowing (type, init);
       init = convert_for_initialization (0, type, init, flags,
                                         ICR_INIT, NULL_TREE, 0,
index 09e115c55c7fbaa0bffcfaab2b1267e0f72378cf..42f065137aaf46bf88edf6e05ed2a711d42a5a62 100644 (file)
@@ -2365,17 +2365,18 @@ 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
 by @option{-Wall}.
 
-@item -Wno-narrowing @r{(C++ and Objective-C++ only)}
+@item -Wnarrowing @r{(C++ and Objective-C++ only)}
 @opindex Wnarrowing
 @opindex Wno-narrowing
-With -std=c++0x, suppress the diagnostic required by the standard for
-narrowing conversions within @samp{@{ @}}, e.g.
+Warn when a narrowing conversion occurs within @samp{@{ @}}, e.g.
 
 @smallexample
 int i = @{ 2.2 @}; // error: narrowing from double to int
 @end smallexample
 
-This flag can be useful for compiling valid C++98 code in C++0x mode.
+This flag is included in @option{-Wall} and @option{-Wc++0x-compat}.
+With -std=c++0x, @option{-Wno-narrowing} suppresses the diagnostic
+required by the standard.
 
 @item -Wnoexcept @r{(C++ and Objective-C++ only)}
 @opindex Wnoexcept
@@ -4066,7 +4067,8 @@ ISO C and ISO C++, e.g.@: request for implicit conversion from
 @item -Wc++0x-compat @r{(C++ and Objective-C++ only)}
 Warn about C++ constructs whose meaning differs between ISO C++ 1998 and
 ISO C++ 200x, e.g., identifiers in ISO C++ 1998 that will become keywords
-in ISO C++ 200x.  This warning is enabled by @option{-Wall}.
+in ISO C++ 200x.  This warning turns on @option{-Wnarrowing} and is
+enabled by @option{-Wall}.
 
 @item -Wcast-qual
 @opindex Wcast-qual
index b0c282d578d54c54eecddcc7ccb6d31b342f00a1..036da06e5e10133d48512e9fc3dea1442bd66070 100644 (file)
@@ -1,3 +1,9 @@
+2011-10-23  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/50810
+       * g++.dg/cpp0x/warn_cxx0x2.C: New.
+       * g++.dg/cpp0x/warn_cxx0x3.C: Likewise.
+
 2011-10-23  Tom de Vries  <tom@codesourcery.com>
 
        PR tree-optimization/50763
diff --git a/gcc/testsuite/g++.dg/cpp0x/warn_cxx0x2.C b/gcc/testsuite/g++.dg/cpp0x/warn_cxx0x2.C
new file mode 100644 (file)
index 0000000..1fc9c6b
--- /dev/null
@@ -0,0 +1,4 @@
+// PR c++/50810
+// { dg-options "-std=gnu++98 -Wc++0x-compat" }
+
+signed char data[] = { 0xff }; // { dg-warning "narrowing" }
diff --git a/gcc/testsuite/g++.dg/cpp0x/warn_cxx0x3.C b/gcc/testsuite/g++.dg/cpp0x/warn_cxx0x3.C
new file mode 100644 (file)
index 0000000..91faf12
--- /dev/null
@@ -0,0 +1,4 @@
+// PR c++/50810
+// { dg-options "-std=gnu++98 -Wc++0x-compat -Wno-narrowing" }
+
+signed char data[] = { 0xff };