aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2024-11-27 09:32:31 +0100
committerFlorian Weimer <fweimer@redhat.com>2024-11-27 09:32:31 +0100
commit631cd92b3b3d187860df004d212c4d7f6db517b7 (patch)
tree28f97c75481b765c760081250d82204f5bacc92d
parent2fd9aef1db1a4260ee823bc3a3d4cfc22e95c543 (diff)
downloadgcc-631cd92b3b3d187860df004d212c4d7f6db517b7.zip
gcc-631cd92b3b3d187860df004d212c4d7f6db517b7.tar.gz
gcc-631cd92b3b3d187860df004d212c4d7f6db517b7.tar.bz2
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.
-rw-r--r--gcc/c-family/c-opts.cc5
-rw-r--r--gcc/c-family/c.opt4
-rw-r--r--gcc/c-family/c.opt.urls3
-rw-r--r--gcc/c/c-parser.cc5
-rw-r--r--gcc/doc/invoke.texi15
-rw-r--r--gcc/testsuite/gcc.dg/Wfree-labels-1.c18
-rw-r--r--gcc/testsuite/gcc.dg/Wfree-labels-2.c18
-rw-r--r--gcc/testsuite/gcc.dg/Wfree-labels-3.c18
8 files changed, 82 insertions, 4 deletions
diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
index a02d011..9917968 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 26872547..467d361 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 daaf70c..56c814e 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 4868357..47668ec 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 346ac13..51dc871 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 0000000..a7f9ad4
--- /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 0000000..56b1fb0
--- /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 0000000..c936597
--- /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;
+}