]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c-typeck.c (build_c_cast): Always warn when casting from a pointer to an integer...
authorCarlos O'Donell <carlos@codesourcery.com>
Thu, 5 Jan 2006 19:07:16 +0000 (19:07 +0000)
committerCarlos O'Donell <carlos@gcc.gnu.org>
Thu, 5 Jan 2006 19:07:16 +0000 (19:07 +0000)
gcc/

2006-01-05  Carlos O'Donell  <carlos@codesourcery.com>

* c-typeck.c (build_c_cast): Always warn when casting
from a pointer to an integer of different size, even if
the node was constant.

gcc/testsuite

2006-01-05  Carlos O'Donell  <carlos@codesourcery.com>

* gcc.dg/cast-1.c: Add new warning.
* gcc.dg/cast-2.c: Likewise.
* gcc.dg/cast-3.c: Likewise.
* gcc.dg/format/cast-1.c: Likewise.
* gcc.dg/cast-4.c: New testcase.

From-SVN: r109386

gcc/ChangeLog
gcc/c-typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/cast-1.c
gcc/testsuite/gcc.dg/cast-2.c
gcc/testsuite/gcc.dg/cast-3.c
gcc/testsuite/gcc.dg/cast-4.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/format/cast-1.c

index 026dade34713197cb962cce8e34a617ffefd2be8..6f86398ba30f348f30b7af676acb2d21c5d1367f 100644 (file)
@@ -1,3 +1,9 @@
+2006-01-05  Carlos O'Donell  <carlos@codesourcery.com>
+
+       * c-typeck.c (build_c_cast): Always warn when casting
+       from a pointer to an integer of different size, even if
+       the node was constant.
+
 2006-01-05  Richard Guenther  <rguenther@suse.de>
 
        PR tree-optimization/22555
index 81fc40b3945305be46700443b0575d4af4f787b0..176ba7e8eecd36daa4e0d3ba1f00b2028258a167 100644 (file)
@@ -3508,8 +3508,12 @@ build_c_cast (tree type, tree expr)
 
       if (TREE_CODE (type) == INTEGER_TYPE
          && TREE_CODE (otype) == POINTER_TYPE
-         && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
-         && !TREE_CONSTANT (value))
+         && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
+      /* Unlike conversion of integers to pointers, where the 
+         warning is disabled for converting constants because 
+         of cases such as SIG_*, warn about converting constant 
+         pointers to integers. In some cases it may cause unwanted 
+         sign extension, and a warning is appropriate.  */
        warning (OPT_Wpointer_to_int_cast,
                 "cast from pointer to integer of different size");
 
index 640c92605e008c29ec2ba078158df7cb008607e2..eb11257b9b1ae02a8e82dcc34b16da94d8a36737 100644 (file)
@@ -1,3 +1,11 @@
+2006-01-05  Carlos O'Donell  <carlos@codesourcery.com>
+
+       * gcc.dg/cast-1.c: Add new warning.
+       * gcc.dg/cast-2.c: Likewise.
+       * gcc.dg/cast-3.c: Likewise.
+       * gcc.dg/format/cast-1.c: Likewise.
+       * gcc.dg/cast-4.c: New testcase.
+
 2006-01-05  Richard Guenther  <rguenther@suse.de>
 
        PR tree-optimization/22555
index e3d79db6be2054882d0de6874be6e18d49c27938..892426467f1ca9f2b0667fae5f84cddc515f431e 100644 (file)
@@ -37,5 +37,5 @@ f (void)
   (void *) c; /* { dg-warning "warning: cast to pointer from integer of different size" } */
   (void *) (char) 1;
   (char) p; /* { dg-warning "warning: cast from pointer to integer of different size" } */
-  (char) (void *) 1;
+  (char) (void *) 1; /* { dg-warning "warning: cast from pointer to integer of different size" } */
 }
index 0f84c22fbd85504842a26ea9713f16dc7df3a88d..eff465e7bb489038f0b92eb8b66f1307c86da8d8 100644 (file)
@@ -37,5 +37,5 @@ f (void)
   (void *) c; /* { dg-warning "warning: cast to pointer from integer of different size" } */
   (void *) (char) 1;
   (char) p; /* { dg-warning "warning: cast from pointer to integer of different size" } */
-  (char) (void *) 1;
+  (char) (void *) 1; /* { dg-warning "warning: cast from pointer to integer of different size" } */
 }
index 656bf6dc7959967d8591be67cfa7c278b0c3ee33..26a38cbbda0c35825f43bb78faffa65cf5c82477 100644 (file)
@@ -37,5 +37,5 @@ f (void)
   (void *) c; /* { dg-warning "warning: cast to pointer from integer of different size" } */
   (void *) (char) 1;
   (char) p; /* { dg-warning "warning: cast from pointer to integer of different size" } */
-  (char) (void *) 1;
+  (char) (void *) 1; /* { dg-warning "warning: cast from pointer to integer of different size" } */
 }
diff --git a/gcc/testsuite/gcc.dg/cast-4.c b/gcc/testsuite/gcc.dg/cast-4.c
new file mode 100644 (file)
index 0000000..32b02cb
--- /dev/null
@@ -0,0 +1,13 @@
+/* Test warnings when casting from a constant integer to pointer.  
+   Test with -pedantic-errors.  */
+/* Origin: Carlos O'Donell <carlos@codesourcery.com> */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99 -pedantic-errors" } */
+
+extern int i;
+char c;
+void
+f (void)
+{
+  c = (char)&i; /* { dg-warning "warning: cast from pointer to integer of different size" } */
+}
index 78a4f527d9baf03fc67796b6ee413038eb4415f7..76e7cc52a01c489d9d37c57f343fddcc6841d1db 100644 (file)
@@ -12,5 +12,5 @@ f (int x)
 {
   printf("%s", x); /* { dg-warning "format" } */
   printf((char *)(size_t)"%s", x); /* { dg-warning "format" } */
-  printf((char *)(char)"%s", x);
+  printf((char *)(char)"%s", x); /* { dg-warning "warning: cast from pointer to integer of different size" } */
 }