https://gcc.gnu.org/g:631cd92b3b3d187860df004d212c4d7f6db517b7

commit r15-5715-g631cd92b3b3d187860df004d212c4d7f6db517b7
Author: Florian Weimer <fwei...@redhat.com>
Date:   Wed Nov 27 09:32:31 2024 +0100

    c: Introduce -Wfree-labels
    
    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.

Diff:
---
 gcc/c-family/c-opts.cc                |  5 +++++
 gcc/c-family/c.opt                    |  4 ++++
 gcc/c-family/c.opt.urls               |  3 +++
 gcc/c/c-parser.cc                     |  5 +++--
 gcc/doc/invoke.texi                   | 15 +++++++++++++--
 gcc/testsuite/gcc.dg/Wfree-labels-1.c | 18 ++++++++++++++++++
 gcc/testsuite/gcc.dg/Wfree-labels-2.c | 18 ++++++++++++++++++
 gcc/testsuite/gcc.dg/Wfree-labels-3.c | 18 ++++++++++++++++++
 8 files changed, 82 insertions(+), 4 deletions(-)

diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
index a02d011abc1f..991796831e6a 100644
--- a/gcc/c-family/c-opts.cc
+++ b/gcc/c-family/c-opts.cc
@@ -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;
 
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 268725471329..467d361bb3a7 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -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.
diff --git a/gcc/c-family/c.opt.urls b/gcc/c-family/c.opt.urls
index daaf70cdc45f..56c814e1b5ca 100644
--- a/gcc/c-family/c.opt.urls
+++ b/gcc/c-family/c.opt.urls
@@ -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)
 
diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index 48683572d7cd..47668eceeded 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -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);
 
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 346ac1369b87..51dc871e6bc1 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -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
index 000000000000..a7f9ad4dd502
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wfree-labels-1.c
@@ -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
index 000000000000..56b1fb0aebac
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wfree-labels-2.c
@@ -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
index 000000000000..c9365977dbbc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wfree-labels-3.c
@@ -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;
+}

Reply via email to