]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Turn -Wimplicit-int into a permerror
authorFlorian Weimer <fweimer@redhat.com>
Fri, 1 Dec 2023 07:10:13 +0000 (08:10 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Fri, 1 Dec 2023 07:10:13 +0000 (08:10 +0100)
Most of these new permerrors are currently not diagnosed in system
headers.

gcc/

PR c/91093
PR c/96284
* doc/invoke.texi (Warning Options): Document changes.

gcc/c/

* c-decl.cc (warn_defaults_to): Remove.
(grok_declarator, start_function): Call permerror_opt
instead of warn_defaults_to.
(store_parm_decls_oldstyle): Call permerror_opt for
OPT_Wimplicit_int.

gcc/testsuite/

* gcc.dg/permerror-default.c (implicit_int_1, implicit_int_2)
(implicit_int_3, implicit_int_4): Expect new permerror.
* gcc.dg/permerror-system.c: Expect a single new permerror.
* gcc.dg/Wimplicit-int-1.c: Compile with -fpermissive due to
expected warning.
* gcc.dg/Wimplicit-int-4.c: Likewise.
* gcc.dg/Wimplicit-int-1a.c: New test.  Copied from
gcc.dg/Wimplicit-int-1.c, but expect errors.
* gcc.dg/Wimplicit-int-4a.c: New test.  Copied from
gcc.dg/Wimplicit-int-4.c, but expect errors.
* gcc.dg/gnu23-attr-syntax-2.c: Compile with -fpermissive
due to expected implicit-int error.
* gcc.dg/gnu23-attr-syntax-3.c: New test.  Copied from
gcc.dg/gnu23-attr-syntax-2.c, but expect an error.
* gcc.dg/pr105635.c: Build with -fpermissive due to implicit
int.
* gcc.dg/pr105635-2.c: New test.  Copied from
gcc.dg/pr105635.c.  Expect implicit int error.
* gcc.dg/noncompile/pr79758.c: Build with -fpermissive due to
implicit int.
* gcc.dg/noncompile/pr79758-2.c: New test.  Copied from
gcc.dg/noncompile/pr79758.c.  Expect implicit int error.

14 files changed:
gcc/c/c-decl.cc
gcc/doc/invoke.texi
gcc/testsuite/gcc.dg/Wimplicit-int-1.c
gcc/testsuite/gcc.dg/Wimplicit-int-1a.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wimplicit-int-4.c
gcc/testsuite/gcc.dg/Wimplicit-int-4a.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/gnu23-attr-syntax-2.c
gcc/testsuite/gcc.dg/gnu23-attr-syntax-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/noncompile/pr79758-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/noncompile/pr79758.c
gcc/testsuite/gcc.dg/permerror-default.c
gcc/testsuite/gcc.dg/permerror-system.c
gcc/testsuite/gcc.dg/pr105635-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr105635.c

index 4d17f055604b851726785c5a303390b34ea8dd35..14d66ff57ad8289d06851513d29223e726d4b6a6 100644 (file)
@@ -647,8 +647,6 @@ static tree grokdeclarator (const struct c_declarator *,
                            bool *, enum deprecated_states);
 static tree grokparms (struct c_arg_info *, bool);
 static void layout_array_type (tree);
-static void warn_defaults_to (location_t, int, const char *, ...)
-    ATTRIBUTE_GCC_DIAG(3,4);
 static const char *header_for_builtin_fn (tree);
 \f
 /* T is a statement.  Add it to the statement-tree.  This is the
@@ -6576,23 +6574,6 @@ warn_variable_length_array (tree name, tree size)
     }
 }
 
-/* Print warning about defaulting to int if necessary.  */
-
-static void
-warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
-{
-  diagnostic_info diagnostic;
-  va_list ap;
-  rich_location richloc (line_table, location);
-
-  va_start (ap, gmsgid);
-  diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
-                       flag_isoc99 ? DK_PEDWARN : DK_WARNING);
-  diagnostic.option_index = opt;
-  diagnostic_report_diagnostic (global_dc, &diagnostic);
-  va_end (ap);
-}
-
 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
    considering only those c_declspec_words found in LIST, which
    must be terminated by cdw_number_of_elements.  */
@@ -6881,12 +6862,12 @@ grokdeclarator (const struct c_declarator *declarator,
       else
        {
          if (name)
-           warn_defaults_to (loc, OPT_Wimplicit_int,
-                             "type defaults to %<int%> in declaration "
-                             "of %qE", name);
+           permerror_opt (loc, OPT_Wimplicit_int,
+                          "type defaults to %<int%> in declaration "
+                          "of %qE", name);
          else
-           warn_defaults_to (loc, OPT_Wimplicit_int,
-                             "type defaults to %<int%> in type name");
+           permerror_opt (loc, OPT_Wimplicit_int,
+                          "type defaults to %<int%> in type name");
        }
     }
 
@@ -10300,10 +10281,10 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
     }
 
   if (warn_about_return_type)
-    warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
-                          : (warn_return_type > 0 ? OPT_Wreturn_type
-                             : OPT_Wimplicit_int),
-                     "return type defaults to %<int%>");
+    permerror_opt (loc, flag_isoc99 ? OPT_Wimplicit_int
+                  : (warn_return_type > 0 ? OPT_Wreturn_type
+                     : OPT_Wimplicit_int),
+                  "return type defaults to %<int%>");
 
   /* Make the init_value nonzero so pushdecl knows this is not tentative.
      error_mark_node is replaced below (in pop_scope) with the BLOCK.  */
@@ -10645,9 +10626,9 @@ store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
          warn_if_shadowing (decl);
 
          if (flag_isoc99)
-           pedwarn (DECL_SOURCE_LOCATION (decl),
-                    OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
-                    decl);
+           permerror_opt (DECL_SOURCE_LOCATION (decl),
+                          OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
+                          decl);
          else
            warning_at (DECL_SOURCE_LOCATION (decl),
                        OPT_Wmissing_parameter_type,
index 5244958f01d36f09ebda7a70cd90845dc47dfa1c..51bf19db73aeeed34983d0f8aa677229fb24e533 100644 (file)
@@ -6185,6 +6185,7 @@ that have their own flag:
 
 @gccoptlist{
 -Wimplicit-function-declaration @r{(C and Objective-C only)}
+-Wimplicit-int @r{(C and Objective-C only)}
 -Wint-conversion @r{(C and Objective-C only)}
 -Wnarrowing @r{(C++)}
 }
@@ -6856,8 +6857,10 @@ This warning is enabled by @option{-Wall} in C++.
 @opindex Wno-implicit-int
 @item -Wno-implicit-int @r{(C and Objective-C only)}
 This option controls warnings when a declaration does not specify a type.
-This warning is enabled by default in C99 and later dialects of C,
-and also by @option{-Wall}.
+This warning is enabled by default, as an error, in C99 and later
+dialects of C, and also by @option{-Wall}.  The error can be downgraded
+to a warning using @option{-fpermissive} (along with certain other
+errors), or for this error alone, with @option{-Wno-error=implicit-int}.
 
 This warning is upgraded to an error by @option{-pedantic-errors}.
 
index 4a96e8f505d98d87599e2a1769262164caf7d3cb..fc7726c517e6a0a1039e228c06a1fb12425d17b8 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "" } */
+/* { dg-options "-fpermissive" } */
 
 static l; /* { dg-warning "type defaults to" } */
 
diff --git a/gcc/testsuite/gcc.dg/Wimplicit-int-1a.c b/gcc/testsuite/gcc.dg/Wimplicit-int-1a.c
new file mode 100644 (file)
index 0000000..ef1835e
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+static l; /* { dg-error "type defaults to" } */
+
+foo (a) /* { dg-error "return type defaults to" } */
+/* { dg-error "type of .a. defaults to .int." "type" { target *-*-* } .-1 } */
+{
+  auto p; /* { dg-error "type defaults to" } */
+  typedef bar; /* { dg-error "type defaults to" } */
+}
index c9c6e8e5e95674076dcf96e937816fc79ed9b1d5..99c61a76ecfe9d8c23fe520696b934df26eb6d34 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-Wno-implicit -Wimplicit-int" } */
+/* { dg-options "-fpermissive -Wno-implicit -Wimplicit-int" } */
 
 static l; /* { dg-warning "type defaults to" } */
 
diff --git a/gcc/testsuite/gcc.dg/Wimplicit-int-4a.c b/gcc/testsuite/gcc.dg/Wimplicit-int-4a.c
new file mode 100644 (file)
index 0000000..920a088
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-Wno-implicit -Wimplicit-int" } */
+
+static l; /* { dg-error "type defaults to" } */
+
+foo (a) /* { dg-error "return type defaults to" } */
+/* { dg-error "type of .a. defaults to .int." "type" { target *-*-* } .-1 } */
+{
+  auto p; /* { dg-error "type defaults to" } */
+  typedef bar; /* { dg-error "type defaults to" } */
+}
index ba60f7a095ab32cdf86914f90ee953f171c5d6c6..8943534f2b2f37dc384510ed28e8b9e730951276 100644 (file)
@@ -1,7 +1,7 @@
 /* Test C23 attribute syntax.  Invalid uses of attributes with GNU C
    features.  */
 /* { dg-do compile } */
-/* { dg-options "-std=gnu23 -w" } */
+/* { dg-options "-fpermissive -std=gnu23 -w" } */
 
 /* Attributes cannot be used as prefix attributes on old-style
    parameter declarations or on function declarators with identifier
diff --git a/gcc/testsuite/gcc.dg/gnu23-attr-syntax-3.c b/gcc/testsuite/gcc.dg/gnu23-attr-syntax-3.c
new file mode 100644 (file)
index 0000000..d73d6a3
--- /dev/null
@@ -0,0 +1,17 @@
+/* Test C23 attribute syntax.  Invalid uses of attributes with GNU C
+   features.  Non-permissive variant.  */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu23 -w" } */
+
+/* Attributes cannot be used as prefix attributes on old-style
+   parameter declarations or on function declarators with identifier
+   lists (removed from C23).  */
+
+void (*f(a, b) [[]])() int a, b; { } /* { dg-error "expected" } */
+
+void f(x, y) int x; [[]] int y; { } /* { dg-error "expected" } */
+/* { dg-error "type of 'y' defaults to 'int'" "" { target *-*-* } .-1 } */
+
+/* Nonempty attributes cannot be used as postfix attributes with
+   __auto_type.  */
+__auto_type [[gnu::no_such_attr]] x = 1; /* { dg-error "'__auto_type' followed by" } */
diff --git a/gcc/testsuite/gcc.dg/noncompile/pr79758-2.c b/gcc/testsuite/gcc.dg/noncompile/pr79758-2.c
new file mode 100644 (file)
index 0000000..e6a27f9
--- /dev/null
@@ -0,0 +1,6 @@
+/* PR c/79758 */
+/* { dg-do compile } */
+
+void fn1 (int[a]) { }; /* { dg-error "undeclared here" } */
+void fn1 (b) { }; /* { dg-error "redefinition" } */
+/* { dg-error "defaults to 'int'" "" { target *-*-* } .-1 } */
index a31216068f002378cf6e120890695f143863088b..e42a44380fa6c5934d5d8fd7817bf99e3f2ed046 100644 (file)
@@ -1,5 +1,6 @@
 /* PR c/79758 */
 /* { dg-do compile } */
+/* { dg-additional-options "-fpermissive" } */
 
 void fn1 (int[a]) { }; /* { dg-error "undeclared here" } */
 void fn1 (b) { }; /* { dg-error "redefinition" } */
index f37f9189e41cef174992109f1c1f04f89094a56f..90f2220037c59000e65196eb688b89bbdd2cffd4 100644 (file)
@@ -10,13 +10,13 @@ implicit_function_declaration (void)
   f1 (); /* { dg-error "'f1' \\\[-Wimplicit-function-declaration\\\]" } */
 }
 
-extern implicit_int_1; /* { dg-warning "'implicit_int_1' \\\[-Wimplicit-int\\\]" } */
-typedef implicit_int_2; /* { dg-warning "'implicit_int_2' \\\[-Wimplicit-int\\\]" } */
-extern implicit_int_3 (void); /* { dg-warning "'implicit_int_3' \\\[-Wimplicit-int\\]" } */
-implicit_int_4 (i) /* { dg-warning "return type defaults to 'int' \\\[-Wimplicit-int\\\]" } */
-/* { dg-warning "type of 'i' defaults to 'int' \\\[-Wimplicit-int\\\]" "" { target *-*-*} .-1 } */
+extern implicit_int_1; /* { dg-error "'implicit_int_1' \\\[-Wimplicit-int\\\]" } */
+typedef implicit_int_2; /* { dg-error "'implicit_int_2' \\\[-Wimplicit-int\\\]" } */
+extern implicit_int_3 (void); /* { dg-error "'implicit_int_3' \\\[-Wimplicit-int\\]" } */
+implicit_int_4 (i) /* { dg-error "return type defaults to 'int' \\\[-Wimplicit-int\\\]" } */
+/* { dg-error "type of 'i' defaults to 'int' \\\[-Wimplicit-int\\\]" "" { target *-*-*} .-1 } */
 {
-  (const) 0; /* { dg-warning "type defaults to 'int' in type name \\\[-Wimplicit-int\\\]" } */
+  (const) 0; /* { dg-error "type defaults to 'int' in type name \\\[-Wimplicit-int\\\]" } */
 }
 
 extern int missing_parameter_type (i); /* { dg-warning "parameter names \\\(without types\\\) in function declaration\n" } */
index e4da4a99e9a995905d5451527a6041689d8a9fbf..60c65ffc1d4a9caf7e6d4946ead92ae9abbb4028 100644 (file)
@@ -10,6 +10,8 @@
 
 /* { dg-error "'f1' \\\[-Wimplicit-function-declaration\\\]" "" { target *-*-* } 10 } */
 
+/* { dg-error "type of 'i' defaults to 'int' \\\[-Wimplicit-int\\\]" "" { target *-*-*} 16 } */
+
 /* { dg-error "pointer/integer type mismatch in conditional expression \\\[-Wint-conversion\\\]" "" { target *-*-* } 29 } */
 /* { dg-error "pointer/integer type mismatch in conditional expression \\\[-Wint-conversion\\\]" "" { target *-*-* } 30 } */
 /* { dg-error "passing argument 1 of 'f2' makes pointer from integer without a cast \\\[-Wint-conversion\\\]" "" { target *-*-* } 31 } */
diff --git a/gcc/testsuite/gcc.dg/pr105635-2.c b/gcc/testsuite/gcc.dg/pr105635-2.c
new file mode 100644 (file)
index 0000000..807eef0
--- /dev/null
@@ -0,0 +1,11 @@
+/* PR c/105635 */
+/* { dg-do compile } */
+/* { dg-options "-Wall" } */
+
+void foo (int, int[*]);        /* { dg-message "previous declaration of 'foo' with type" } */
+
+foo (int x, int y)     /* { dg-error "return type defaults to 'int'" } */
+{                      /* { dg-warning "conflicting types for 'foo'" "" { target *-*-* } .-1 } */
+                       /* { dg-message "declared here" "" { target *-*-* } .-2 } */
+  return (x >= 0) != (y < 0);  /* { dg-warning "'return' with a value, in function returning void" } */
+}
index aa02f593bfafe36a25ba831aeaa8ac5ce9d00b12..b98ea1b3c3b007e5b6848c114d4b91379d71dd7b 100644 (file)
@@ -1,6 +1,6 @@
 /* PR c/105635 */
 /* { dg-do compile } */
-/* { dg-options "-Wall" } */
+/* { dg-options "-fpermissive -Wall" } */
 
 void foo (int, int[*]);        /* { dg-message "previous declaration of 'foo' with type" } */