]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Introduce -Wfree-labels
authorFlorian Weimer <fweimer@redhat.com>
Wed, 27 Nov 2024 08:32:31 +0000 (09:32 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Wed, 27 Nov 2024 08:32:31 +0000 (09:32 +0100)
This is another recent GCC extension whose use is apparently
difficult to spot in code reviews.

The name of the option is due to Jonathan Wakely.  Part of it
could apply to C++ as well (for labels at the end of a compound
statement).

gcc/c-family/

* c-opts.cc (c_common_post_options): Initialize
warn_free_labels.
* c.opt (Wfree-labels): New option.
* c.opt.urls: Regenerate.

gcc/c/

* c-parser.cc (c_parser_compound_statement_nostart): Use
OPT_Wfree_labels for warning about labels on declarations.
(c_parser_compound_statement_nostart): Use OPT_Wfree_labels
for warning about labels at end of compound statements.

gcc/

* doc/invoke.texi: Document -Wfree-labels.

gcc/testsuite/

* gcc.dg/Wfree-labels-1.c: New test.
* gcc.dg/Wfree-labels-2.c: New test.
* gcc.dg/Wfree-labels-3.c: New test.

gcc/c-family/c-opts.cc
gcc/c-family/c.opt
gcc/c-family/c.opt.urls
gcc/c/c-parser.cc
gcc/doc/invoke.texi
gcc/testsuite/gcc.dg/Wfree-labels-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wfree-labels-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Wfree-labels-3.c [new file with mode: 0644]

index a02d011abc1fb92a8c0be42dd1e2da7f218445a9..991796831e6a16f6ebf5eb1b4673c4c82aab97a5 100644 (file)
@@ -1005,6 +1005,11 @@ c_common_post_options (const char **pfilename)
       = ((pedantic && !flag_isoc23 && warn_c11_c23_compat != 0)
         || warn_c11_c23_compat > 0);
 
+  /* Likewise for -Wfree-labels.  */
+  if (warn_free_labels == -1)
+    warn_free_labels = ((pedantic && !flag_isoc23 && warn_c11_c23_compat != 0)
+                       || warn_c11_c23_compat > 0);
+
   if (warn_deprecated_non_prototype == -1)
     warn_deprecated_non_prototype = warn_c11_c23_compat > 0;
 
index 268725471329972a181155e834fa1cb56b2374f1..467d361bb3a7606ef2ef5e210bf72a0a89a25b93 100644 (file)
@@ -832,6 +832,10 @@ Wframe-address
 C ObjC C++ ObjC++ Var(warn_frame_address) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall)
 Warn when __builtin_frame_address or __builtin_return_address is used unsafely.
 
+Wfree-labels
+C ObjC Var(warn_free_labels) Init(-1) Warning
+Warn about labels on declarations and at the end of compound statements.
+
 Wglobal-module
 C++ ObjC++ Var(warn_global_module) Warning Init(1)
 Warn about the global module fragment not containing only preprocessing directives.
index daaf70cdc45f32a10d86a703451cdffa47bf461d..56c814e1b5cadf9246c28c471b33ae3412eabc6d 100644 (file)
@@ -421,6 +421,9 @@ UrlSuffix(gcc/Warning-Options.html#index-Wformat)
 Wframe-address
 UrlSuffix(gcc/Warning-Options.html#index-Wframe-address)
 
+Wfree-labels
+UrlSuffix(gcc/Warning-Options.html#index-Wfree-labels)
+
 Wglobal-module
 UrlSuffix(gcc/C_002b_002b-Dialect-Options.html#index-Wglobal-module)
 
index 48683572d7cd76f7aa4dc5b120d2d40dd0d96703..47668eceeded12cb910ad73b3a94ce6e54c18961 100644 (file)
@@ -7406,7 +7406,7 @@ c_parser_compound_statement_nostart (c_parser *parser)
                   && (have_std_attrs = true)))
        {
          if (last_label)
-           pedwarn_c11 (c_parser_peek_token (parser)->location, OPT_Wpedantic,
+           pedwarn_c11 (c_parser_peek_token (parser)->location, OPT_Wfree_labels,
                         "a label can only be part of a statement and "
                         "a declaration is not a statement");
          /* It's unlikely we'll see a nested loop in a declaration in
@@ -7553,7 +7553,8 @@ c_parser_compound_statement_nostart (c_parser *parser)
       parser->error = false;
     }
   if (last_label)
-    pedwarn_c11 (label_loc, OPT_Wpedantic, "label at end of compound statement");
+    pedwarn_c11 (label_loc, OPT_Wfree_labels,
+                "label at end of compound statement");
   location_t endloc = c_parser_peek_token (parser)->location;
   c_parser_consume_token (parser);
 
index 346ac1369b87f72e06bd1314b5c7cb0371ca22c3..51dc871e6bc157adffa34df26de2490421841a1b 100644 (file)
@@ -522,8 +522,8 @@ Objective-C and Objective-C++ Dialects}.
 }
 
 @item C and Objective-C-only Warning Options
-@gccoptlist{-Wbad-function-cast -Wdeprecated-non-prototype -Wmissing-declarations
--Wmissing-parameter-name -Wmissing-parameter-type
+@gccoptlist{-Wbad-function-cast -Wdeprecated-non-prototype -Wfree-labels
+-Wmissing-declarations -Wmissing-parameter-name -Wmissing-parameter-type
 -Wdeclaration-missing-parameter-type -Wmissing-prototypes
 -Wmissing-variable-declarations -Wnested-externs -Wold-style-declaration
 -Wold-style-definition -Wstrict-prototypes -Wtraditional
@@ -10048,6 +10048,17 @@ Do not warn if certain built-in macros are redefined.  This suppresses
 warnings for redefinition of @code{__TIMESTAMP__}, @code{__TIME__},
 @code{__DATE__}, @code{__FILE__}, and @code{__BASE_FILE__}.
 
+@opindex Wfree-labels
+@opindex Wno-free-labels
+@item -Wfree-labels @r{(C and Objective-C only)}
+Warn if a label is applied to a non-statement, or occurs at the end of a
+compound statement.  Such labels are allowed by C23 and later dialects
+of C, and are available as a GCC extension in all other dialects.
+
+This warning is also enabled by @option{-Wc11-c23-compat}.  It is turned
+into an error if building for a C version before C23 by
+@option{-pedantic-errors}.
+
 @opindex Wheader-guard
 @item -Wheader-guard
 Warn if a valid preprocessor header multiple inclusion guard has
diff --git a/gcc/testsuite/gcc.dg/Wfree-labels-1.c b/gcc/testsuite/gcc.dg/Wfree-labels-1.c
new file mode 100644 (file)
index 0000000..a7f9ad4
--- /dev/null
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-Wfree-labels" } */
+
+void
+f (void)
+{
+  goto l;
+ l: /* { dg-warning "label at end of compound statement" } */
+}
+
+int
+g (void)
+{
+  goto l;
+ l:
+  int x = 0; /* { dg-warning "a label can only be part of a statement" } */
+  return x;
+}
diff --git a/gcc/testsuite/gcc.dg/Wfree-labels-2.c b/gcc/testsuite/gcc.dg/Wfree-labels-2.c
new file mode 100644 (file)
index 0000000..56b1fb0
--- /dev/null
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c11 -pedantic -Wno-free-labels" } */
+
+void
+f (void)
+{
+  goto l;
+ l:
+}
+
+int
+g (void)
+{
+  goto l;
+ l:
+  int x = 0;
+  return x;
+}
diff --git a/gcc/testsuite/gcc.dg/Wfree-labels-3.c b/gcc/testsuite/gcc.dg/Wfree-labels-3.c
new file mode 100644 (file)
index 0000000..c936597
--- /dev/null
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-Wc11-c23-compat -Wno-free-labels" } */
+
+void
+f (void)
+{
+  goto l;
+ l:
+}
+
+int
+g (void)
+{
+  goto l;
+ l:
+  int x = 0;
+  return x;
+}