re PR c++/14791 (13070 does not fix -Wformat with fprintf)
authorJakub Jelinek <jakub@gcc.gnu.org>
Thu, 10 Jun 2004 08:08:02 +0000 (10:08 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Thu, 10 Jun 2004 08:08:02 +0000 (10:08 +0200)
PR c++/14791
* tree.h (enum tree_index): Add TI_FILEPTR_TYPE.
(fileptr_type_node): Define.
* tree.c (build_common_tree_nodes_2): Initialize
fileptr_type_node to ptr_type_node.
* c-common.c (c_common_nodes_and_builtins): For C++, make
fileptr_type_node a distinct type copy.
* builtin-types.def (BT_FILEPTR, BT_FN_INT_CONST_STRING_FILEPTR,
BT_FN_INT_INT_FILEPTR, BT_FN_INT_FILEPTR_CONST_STRING_VALIST_ARG,
BT_FN_SIZE_CONST_PTR_SIZE_SIZE_FILEPTR,
BT_FN_INT_FILEPTR_CONST_STRING_VAR): Add.
(BT_FN_INT_CONST_STRING_PTR, BT_FN_INT_INT_PTR,
BT_FN_SIZE_CONST_PTR_SIZE_SIZE_PTR, BT_FN_INT_PTR_CONST_STRING_VAR,
BT_FN_INT_PTR_CONST_STRING_VALIST_ARG): Remove.
* builtins.def (BUILT_IN_FPRINTF, BUILT_IN_FPRINTF_UNLOCKED,
BUILT_IN_FPUTC, BUILT_IN_FPUTC_UNLOCKED, BUILT_IN_FPUTS,
BUILT_IN_FPUTS_UNLOCKED, BUILT_IN_FSCANF, BUILT_IN_FWRITE,
BUILT_IN_FWRITE_UNLOCKED, BUILT_IN_VFPRINTF, BUILT_IN_VFSCANF): Use
the above *FILEPTR* types instead of *PTR*.

* decl.c (duplicate_decls): Handle fileptr_type_node arguments
specially.

* g++.dg/opt/builtins1.C: New test.

From-SVN: r82902

gcc/ChangeLog
gcc/builtin-types.def
gcc/builtins.def
gcc/c-common.c
gcc/cp/decl.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/opt/builtins1.C [new file with mode: 0644]
gcc/tree.c
gcc/tree.h

index 014bce20e3ce729399ff5d64ecf501d081141d61..f3386bcecb113ca15f7ced7c1d5717b3bae8086d 100644 (file)
@@ -1,3 +1,28 @@
+2004-06-10  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/14791
+       * tree.h (enum tree_index): Add TI_FILEPTR_TYPE.
+       (fileptr_type_node): Define.
+       * tree.c (build_common_tree_nodes_2): Initialize
+       fileptr_type_node to ptr_type_node.
+       * c-common.c (c_common_nodes_and_builtins): For C++, make
+       fileptr_type_node a distinct type copy.
+       * builtin-types.def (BT_FILEPTR, BT_FN_INT_CONST_STRING_FILEPTR,
+       BT_FN_INT_INT_FILEPTR, BT_FN_INT_FILEPTR_CONST_STRING_VALIST_ARG,
+       BT_FN_SIZE_CONST_PTR_SIZE_SIZE_FILEPTR,
+       BT_FN_INT_FILEPTR_CONST_STRING_VAR): Add.
+       (BT_FN_INT_CONST_STRING_PTR, BT_FN_INT_INT_PTR,
+       BT_FN_SIZE_CONST_PTR_SIZE_SIZE_PTR, BT_FN_INT_PTR_CONST_STRING_VAR,
+       BT_FN_INT_PTR_CONST_STRING_VALIST_ARG): Remove.
+       * builtins.def (BUILT_IN_FPRINTF, BUILT_IN_FPRINTF_UNLOCKED,
+       BUILT_IN_FPUTC, BUILT_IN_FPUTC_UNLOCKED, BUILT_IN_FPUTS,
+       BUILT_IN_FPUTS_UNLOCKED, BUILT_IN_FSCANF, BUILT_IN_FWRITE,
+       BUILT_IN_FWRITE_UNLOCKED, BUILT_IN_VFPRINTF, BUILT_IN_VFSCANF): Use
+       the above *FILEPTR* types instead of *PTR*.
+
+       * decl.c (duplicate_decls): Handle fileptr_type_node arguments
+       specially.
+
 2004-06-09  Daniel Berlin <dberlin@dberlin.org>
 
        * tree-ssa.c (verify_ssa): Verify that vdefs/makes_aliased_stores 
@@ -12,9 +37,9 @@
 
 2004-06-09  Richard Henderson  <rth@redhat.com>
  
-        PR middle-end/15228
-        * function.c (assign_parms): Always set_mem_align with the computed
-        FUNCTION_ARG_BOUNDARY.  Don't clear stack_parm if !STRICT_ALIGNMENT.
+       PR middle-end/15228
+       * function.c (assign_parms): Always set_mem_align with the computed
+       FUNCTION_ARG_BOUNDARY.  Don't clear stack_parm if !STRICT_ALIGNMENT.
 
 2004-06-09  Richard Henderson  <rth@redhat.com>
 
index a809a477928c547505ff200edd07f65aac36872c..7f6fb64f121425ceca141910a2bc41adb5cdc3e0 100644 (file)
@@ -74,6 +74,7 @@ DEF_PRIMITIVE_TYPE (BT_COMPLEX_DOUBLE, complex_double_type_node)
 DEF_PRIMITIVE_TYPE (BT_COMPLEX_LONGDOUBLE, complex_long_double_type_node)
 
 DEF_PRIMITIVE_TYPE (BT_PTR, ptr_type_node)
+DEF_PRIMITIVE_TYPE (BT_FILEPTR, fileptr_type_node)
 DEF_PRIMITIVE_TYPE (BT_CONST_PTR, const_ptr_type_node)
 DEF_PRIMITIVE_TYPE (BT_PTRMODE, (*lang_hooks.types.type_for_mode)(ptr_mode, 0))
 DEF_PRIMITIVE_TYPE (BT_INT_PTR, integer_ptr_type_node)
@@ -163,10 +164,10 @@ DEF_FUNCTION_TYPE_2 (BT_FN_SIZE_CONST_STRING_CONST_STRING,
                     BT_SIZE, BT_CONST_STRING, BT_CONST_STRING)
 DEF_FUNCTION_TYPE_2 (BT_FN_STRING_CONST_STRING_INT,
                     BT_STRING, BT_CONST_STRING, BT_INT)
-DEF_FUNCTION_TYPE_2 (BT_FN_INT_CONST_STRING_PTR,
-                    BT_INT, BT_CONST_STRING, BT_PTR)
-DEF_FUNCTION_TYPE_2 (BT_FN_INT_INT_PTR,
-                    BT_INT, BT_INT, BT_PTR)
+DEF_FUNCTION_TYPE_2 (BT_FN_INT_CONST_STRING_FILEPTR,
+                    BT_INT, BT_CONST_STRING, BT_FILEPTR)
+DEF_FUNCTION_TYPE_2 (BT_FN_INT_INT_FILEPTR,
+                    BT_INT, BT_INT, BT_FILEPTR)
 DEF_FUNCTION_TYPE_2 (BT_FN_VOID_PTRMODE_PTR,
                     BT_VOID, BT_PTRMODE, BT_PTR)
 DEF_FUNCTION_TYPE_2 (BT_FN_VOID_VALIST_REF_VALIST_ARG, 
@@ -249,8 +250,8 @@ DEF_FUNCTION_TYPE_3 (BT_FN_INT_STRING_CONST_STRING_VALIST_ARG,
                     BT_INT, BT_STRING, BT_CONST_STRING, BT_VALIST_ARG)
 DEF_FUNCTION_TYPE_3 (BT_FN_INT_CONST_STRING_CONST_STRING_VALIST_ARG,
                     BT_INT, BT_CONST_STRING, BT_CONST_STRING, BT_VALIST_ARG)
-DEF_FUNCTION_TYPE_3 (BT_FN_INT_PTR_CONST_STRING_VALIST_ARG,
-                    BT_INT, BT_PTR, BT_CONST_STRING, BT_VALIST_ARG)
+DEF_FUNCTION_TYPE_3 (BT_FN_INT_FILEPTR_CONST_STRING_VALIST_ARG,
+                    BT_INT, BT_FILEPTR, BT_CONST_STRING, BT_VALIST_ARG)
 DEF_FUNCTION_TYPE_3 (BT_FN_STRING_CONST_STRING_CONST_STRING_INT,
                     BT_STRING, BT_CONST_STRING, BT_CONST_STRING, BT_INT)
 DEF_FUNCTION_TYPE_3 (BT_FN_FLOAT_FLOAT_FLOAT_FLOAT,
@@ -275,8 +276,8 @@ DEF_FUNCTION_TYPE_3 (BT_FN_VOID_PTR_PTR_PTR, BT_VOID, BT_PTR, BT_PTR, BT_PTR)
 DEF_FUNCTION_TYPE_3 (BT_FN_INT_CONST_STRING_PTR_CONST_STRING_PTR_CONST_STRING,
                     BT_INT, BT_CONST_STRING, BT_PTR_CONST_STRING, BT_PTR_CONST_STRING)
 
-DEF_FUNCTION_TYPE_4 (BT_FN_SIZE_CONST_PTR_SIZE_SIZE_PTR,
-                    BT_SIZE, BT_CONST_PTR, BT_SIZE, BT_SIZE, BT_PTR)
+DEF_FUNCTION_TYPE_4 (BT_FN_SIZE_CONST_PTR_SIZE_SIZE_FILEPTR,
+                    BT_SIZE, BT_CONST_PTR, BT_SIZE, BT_SIZE, BT_FILEPTR)
 DEF_FUNCTION_TYPE_4 (BT_FN_INT_STRING_SIZE_CONST_STRING_VALIST_ARG,
                BT_INT, BT_STRING, BT_SIZE, BT_CONST_STRING, BT_VALIST_ARG)
 DEF_FUNCTION_TYPE_4 (BT_FN_SIZE_STRING_SIZE_CONST_STRING_CONST_PTR,
@@ -293,8 +294,8 @@ DEF_FUNCTION_TYPE_VAR_1 (BT_FN_VOID_CONST_PTR_VAR,
 DEF_FUNCTION_TYPE_VAR_1 (BT_FN_INT_CONST_STRING_VAR,
                         BT_INT, BT_CONST_STRING)
 
-DEF_FUNCTION_TYPE_VAR_2 (BT_FN_INT_PTR_CONST_STRING_VAR,
-                        BT_INT, BT_PTR, BT_CONST_STRING)
+DEF_FUNCTION_TYPE_VAR_2 (BT_FN_INT_FILEPTR_CONST_STRING_VAR,
+                        BT_INT, BT_FILEPTR, BT_CONST_STRING)
 DEF_FUNCTION_TYPE_VAR_2 (BT_FN_INT_STRING_CONST_STRING_VAR,
                         BT_INT, BT_STRING, BT_CONST_STRING)
 DEF_FUNCTION_TYPE_VAR_2 (BT_FN_INT_CONST_STRING_CONST_STRING_VAR,
index b70aff9d74336de67de73f69fbf58c74884101f4..3dd61fa8ae0866e8664069806f3f587a8b3eb270 100644 (file)
@@ -471,15 +471,15 @@ DEF_LIB_BUILTIN        (BUILT_IN_STRSPN, "strspn", BT_FN_SIZE_CONST_STRING_CONST
 DEF_LIB_BUILTIN        (BUILT_IN_STRSTR, "strstr", BT_FN_STRING_CONST_STRING_CONST_STRING, ATTR_PURE_NOTHROW_NONNULL_1_2)
 
 /* Category: stdio builtins.  */
-DEF_LIB_BUILTIN        (BUILT_IN_FPRINTF, "fprintf", BT_FN_INT_PTR_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_2_3)
-DEF_EXT_LIB_BUILTIN    (BUILT_IN_FPRINTF_UNLOCKED, "fprintf_unlocked", BT_FN_INT_PTR_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_2_3)
-DEF_LIB_BUILTIN        (BUILT_IN_FPUTC, "fputc", BT_FN_INT_INT_PTR, ATTR_NOTHROW_NONNULL_2)
-DEF_EXT_LIB_BUILTIN    (BUILT_IN_FPUTC_UNLOCKED, "fputc_unlocked", BT_FN_INT_INT_PTR, ATTR_NOTHROW_NONNULL_2)
-DEF_LIB_BUILTIN        (BUILT_IN_FPUTS, "fputs", BT_FN_INT_CONST_STRING_PTR, ATTR_NOTHROW_NONNULL_1_2)
-DEF_EXT_LIB_BUILTIN    (BUILT_IN_FPUTS_UNLOCKED, "fputs_unlocked", BT_FN_INT_CONST_STRING_PTR, ATTR_NOTHROW_NONNULL_1_2)
-DEF_LIB_BUILTIN        (BUILT_IN_FSCANF, "fscanf", BT_FN_INT_PTR_CONST_STRING_VAR, ATTR_FORMAT_SCANF_2_3)
-DEF_LIB_BUILTIN        (BUILT_IN_FWRITE, "fwrite", BT_FN_SIZE_CONST_PTR_SIZE_SIZE_PTR, ATTR_NOTHROW_NONNULL_1_4)
-DEF_EXT_LIB_BUILTIN    (BUILT_IN_FWRITE_UNLOCKED, "fwrite_unlocked", BT_FN_SIZE_CONST_PTR_SIZE_SIZE_PTR, ATTR_NOTHROW_NONNULL_1_4)
+DEF_LIB_BUILTIN        (BUILT_IN_FPRINTF, "fprintf", BT_FN_INT_FILEPTR_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_2_3)
+DEF_EXT_LIB_BUILTIN    (BUILT_IN_FPRINTF_UNLOCKED, "fprintf_unlocked", BT_FN_INT_FILEPTR_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_2_3)
+DEF_LIB_BUILTIN        (BUILT_IN_FPUTC, "fputc", BT_FN_INT_INT_FILEPTR, ATTR_NOTHROW_NONNULL_2)
+DEF_EXT_LIB_BUILTIN    (BUILT_IN_FPUTC_UNLOCKED, "fputc_unlocked", BT_FN_INT_INT_FILEPTR, ATTR_NOTHROW_NONNULL_2)
+DEF_LIB_BUILTIN        (BUILT_IN_FPUTS, "fputs", BT_FN_INT_CONST_STRING_FILEPTR, ATTR_NOTHROW_NONNULL_1_2)
+DEF_EXT_LIB_BUILTIN    (BUILT_IN_FPUTS_UNLOCKED, "fputs_unlocked", BT_FN_INT_CONST_STRING_FILEPTR, ATTR_NOTHROW_NONNULL_1_2)
+DEF_LIB_BUILTIN        (BUILT_IN_FSCANF, "fscanf", BT_FN_INT_FILEPTR_CONST_STRING_VAR, ATTR_FORMAT_SCANF_2_3)
+DEF_LIB_BUILTIN        (BUILT_IN_FWRITE, "fwrite", BT_FN_SIZE_CONST_PTR_SIZE_SIZE_FILEPTR, ATTR_NOTHROW_NONNULL_1_4)
+DEF_EXT_LIB_BUILTIN    (BUILT_IN_FWRITE_UNLOCKED, "fwrite_unlocked", BT_FN_SIZE_CONST_PTR_SIZE_SIZE_FILEPTR, ATTR_NOTHROW_NONNULL_1_4)
 DEF_LIB_BUILTIN        (BUILT_IN_PRINTF, "printf", BT_FN_INT_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_1_2)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN_PRINTF_UNLOCKED, "printf_unlocked", BT_FN_INT_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_1_2)
 DEF_LIB_BUILTIN        (BUILT_IN_PUTCHAR, "putchar", BT_FN_INT_INT, ATTR_NOTHROW_LIST)
@@ -490,8 +490,8 @@ DEF_LIB_BUILTIN        (BUILT_IN_SCANF, "scanf", BT_FN_INT_CONST_STRING_VAR, ATT
 DEF_C99_BUILTIN        (BUILT_IN_SNPRINTF, "snprintf", BT_FN_INT_STRING_SIZE_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_3_4)
 DEF_LIB_BUILTIN        (BUILT_IN_SPRINTF, "sprintf", BT_FN_INT_STRING_CONST_STRING_VAR, ATTR_FORMAT_PRINTF_2_3)
 DEF_LIB_BUILTIN        (BUILT_IN_SSCANF, "sscanf", BT_FN_INT_CONST_STRING_CONST_STRING_VAR, ATTR_FORMAT_SCANF_2_3)
-DEF_LIB_BUILTIN        (BUILT_IN_VFPRINTF, "vfprintf", BT_FN_INT_PTR_CONST_STRING_VALIST_ARG, ATTR_FORMAT_PRINTF_2_0)
-DEF_C99_BUILTIN        (BUILT_IN_VFSCANF, "vfscanf", BT_FN_INT_PTR_CONST_STRING_VALIST_ARG, ATTR_FORMAT_SCANF_2_0)
+DEF_LIB_BUILTIN        (BUILT_IN_VFPRINTF, "vfprintf", BT_FN_INT_FILEPTR_CONST_STRING_VALIST_ARG, ATTR_FORMAT_PRINTF_2_0)
+DEF_C99_BUILTIN        (BUILT_IN_VFSCANF, "vfscanf", BT_FN_INT_FILEPTR_CONST_STRING_VALIST_ARG, ATTR_FORMAT_SCANF_2_0)
 DEF_LIB_BUILTIN        (BUILT_IN_VPRINTF, "vprintf", BT_FN_INT_CONST_STRING_VALIST_ARG, ATTR_FORMAT_PRINTF_1_0)
 DEF_C99_BUILTIN        (BUILT_IN_VSCANF, "vscanf", BT_FN_INT_CONST_STRING_VALIST_ARG, ATTR_FORMAT_SCANF_1_0)
 DEF_C99_BUILTIN        (BUILT_IN_VSNPRINTF, "vsnprintf", BT_FN_INT_STRING_SIZE_CONST_STRING_VALIST_ARG, ATTR_FORMAT_PRINTF_3_0)
index 89bc80e93536a3d5ce3aec7bb0f71d7eee6b3f09..01a878348f42cf73e55ec6e52a8943aead154edb 100644 (file)
@@ -3297,6 +3297,11 @@ c_common_nodes_and_builtins (void)
     (build_decl (TYPE_DECL, get_identifier ("complex long double"),
                 complex_long_double_type_node));
 
+  if (c_dialect_cxx ())
+    /* For C++, make fileptr_type_node a distinct void * type until
+       FILE type is defined.  */
+    fileptr_type_node = build_type_copy (ptr_type_node);
+
   record_builtin_type (RID_VOID, NULL, void_type_node);
 
   void_zero_node = build_int_2 (0, 0);
index 92f6d140b4b12843d299a64f1ae474a0e8cf395d..ad6226052800db441e1717e60d7d9ce067641bda 100644 (file)
@@ -1200,7 +1200,40 @@ duplicate_decls (tree newdecl, tree olddecl)
        {
           /* Avoid warnings redeclaring anticipated built-ins.  */
           if (DECL_ANTICIPATED (olddecl))
-            ;  /* Do nothing yet.  */
+           {
+             /* Deal with fileptr_type_node.  FILE type is not known
+                at the time we create the builtins.  */
+             tree t1, t2;
+
+             for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
+                  t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
+                  t1 || t2;
+                  t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
+               if (!t1 || !t2)
+                 break;
+               else if (TREE_VALUE (t2) == fileptr_type_node)
+                 {
+                   tree t = TREE_VALUE (t1);
+
+                   if (TREE_CODE (t) == POINTER_TYPE
+                       && TYPE_NAME (TREE_TYPE (t))
+                       && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
+                          == get_identifier ("FILE")
+                       && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
+                     {
+                       tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
+
+                       TYPE_ARG_TYPES (TREE_TYPE (olddecl))
+                         = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
+                       types_match = decls_match (newdecl, olddecl);
+                       if (types_match)
+                         return duplicate_decls (newdecl, olddecl);
+                       TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
+                     }
+                 }
+               else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
+                 break;
+           }
          else if ((DECL_EXTERN_C_P (newdecl)
                    && DECL_EXTERN_C_P (olddecl))
                   || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
index 6c802a847f7f88bc5ab0987ccc4cba81db391c39..ed2a56c9ec714f1734455bf56424f4f978b1763b 100644 (file)
@@ -1,3 +1,8 @@
+2004-06-10  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/14791
+       * g++.dg/opt/builtins1.C: New test.
+
 2004-06-10  Danny Smith  <dannysmith@users.sourceforge.net>
 
        * gcc.dg/i386-bitfield1.c: Add mingw32 and cygwin to targets
diff --git a/gcc/testsuite/g++.dg/opt/builtins1.C b/gcc/testsuite/g++.dg/opt/builtins1.C
new file mode 100644 (file)
index 0000000..8311436
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/14791
+// Test if builtins with FILE * arguments work
+// { dg-options "-O2 -Wformat" }
+
+typedef struct _FILE FILE;
+FILE *stderr;
+extern "C" int printf (__const char *__restrict, ...);
+extern "C" int fprintf (FILE *__restrict, __const char *__restrict, ...);
+
+int main ()
+{
+  printf ("%d\n", 1, 1);               // { dg-warning "too many arguments for format" }
+  fprintf (stderr, "%d\n", 1, 1);      // { dg-warning "too many arguments for format" }
+}
index 045959c5c012d4cf9091cfe9d2826ec13c3d1d84..b3301b3c4421a728dc3208fb7539d080e90c415e 100644 (file)
@@ -5355,6 +5355,7 @@ build_common_tree_nodes_2 (int short_double)
   ptr_type_node = build_pointer_type (void_type_node);
   const_ptr_type_node
     = build_pointer_type (build_type_variant (void_type_node, 1, 0));
+  fileptr_type_node = ptr_type_node;
 
   float_type_node = make_node (REAL_TYPE);
   TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
index 8cdcd92aeeb3b5c0d617a7ae13ebcf53017a09db..e867302965eacfb27ddc5d7b23df309d72b41ad4 100644 (file)
@@ -2508,6 +2508,7 @@ enum tree_index
   TI_PTRDIFF_TYPE,
   TI_VA_LIST_TYPE,
   TI_BOOLEAN_TYPE,
+  TI_FILEPTR_TYPE,
 
   TI_VOID_LIST_NODE,
 
@@ -2572,6 +2573,8 @@ extern GTY(()) tree global_trees[TI_MAX];
 #define pid_type_node                   global_trees[TI_PID_TYPE]
 #define ptrdiff_type_node              global_trees[TI_PTRDIFF_TYPE]
 #define va_list_type_node              global_trees[TI_VA_LIST_TYPE]
+/* The C type `FILE *'.  */
+#define fileptr_type_node              global_trees[TI_FILEPTR_TYPE]
 
 #define boolean_type_node              global_trees[TI_BOOLEAN_TYPE]
 #define boolean_false_node             global_trees[TI_BOOLEAN_FALSE]