This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] add more detail to -Wconversion and -Woverflow (PR 80731)


On 05/24/2017 11:08 AM, Joseph Myers wrote:
On Wed, 17 May 2017, Martin Sebor wrote:

@@ -1036,31 +1079,76 @@ warnings_for_convert_and_check (location_t loc, tree type, tree expr,
 	  /* This detects cases like converting -129 or 256 to
 	     unsigned char.  */
 	  if (!int_fits_type_p (expr, c_common_signed_type (type)))
-	    warning_at (loc, OPT_Woverflow,
-			"large integer implicitly truncated to unsigned type");
+	    {
+	      if (cst)
+		warning_at (loc, OPT_Woverflow,
+			    (TYPE_UNSIGNED (exprtype)
+			     ? "conversion from %qT to %qT "
+			     "changes value from %qE to %qE"
+			     : "unsigned conversion from %qT to %qT "
+			     "changes value from %qE to %qE"),
+			    exprtype, type, expr, result);
+	      else
+		warning_at (loc, OPT_Woverflow,
+			    (TYPE_UNSIGNED (exprtype)
+			     ? "conversion from %qT to %qT "
+			     "changes the value of %qE"
+			     : "unsigned conversion from %qT to %qT "
+			     "changes the value of %qE"),
+			    exprtype, type, expr);
+	    }

You need to use G_() around both arguments to ?:, otherwise only one will
get extracted for translation.

diff --git a/gcc/testsuite/c-c++-common/pr68657-1.c b/gcc/testsuite/c-c++-common/pr68657-1.c
index 84f3e54..33fdf86 100644
--- a/gcc/testsuite/c-c++-common/pr68657-1.c
+++ b/gcc/testsuite/c-c++-common/pr68657-1.c
@@ -5,14 +5,14 @@
 void
 f1 (void)
 {
-  unsigned int a = -5;	/* { dg-error "negative integer implicitly converted to unsigned type" } */
+  unsigned int a = -5;	/* { dg-error "unsigned conversion from .int. to .unsigned int. changes value from .-5. to .4294967291." } */

The more specific match would fail for targets with 16-bit int.  You need
to keep it less specific in this test (if you want to test the more
specific text as well, another test could be added for that, restricted to
the int32 effective-target).

(The changes to Wconversion-real-integer-3.C and
Wconversion-real-integer2.C are OK in that those tests are restricted to
int32plus, although in theory 64-bit int would be an issue there.)

+  /* According to 6.3.1.3 of C11:
+     -3-  Otherwise, the new type is signed and the value cannot be
+          represented in it; either the result is implementation-defined
+	  or an implementation-defined signal is raised.
+
+     In GCC such conversios wrap and diagnosed by mentioning "overflow"
+     if the absolut value of the operand is in excess of the maximum of
+     the destination of type, and "conversion" otherwise, as follows:  */

s/conversios/conversions/; s/absolut/absolute/

OK with those changes.

Thanks for the careful review!  Done and committed in r248431.

Martin


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]