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. --- v2: Rebase on top of current trunk. 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 3a3464ccc3f..a7149a85171 100644 --- a/gcc/c-family/c-opts.cc +++ b/gcc/c-family/c-opts.cc @@ -1006,6 +1006,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 220421accf4..4ccf3bbe6e6 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 91918c49204..d9904662b4b 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 70fbf940835..413eaae5fe3 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -7403,7 +7403,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 @@ -7550,7 +7550,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 c73025e2d0e..7ff7e694bc9 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -520,8 +520,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 @@ -10012,6 +10012,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 00000000000..a7f9ad4dd50 --- /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 00000000000..56b1fb0aeba --- /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 00000000000..c9365977dbb --- /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; +} base-commit: 701d8e7e60b85809cae348c1e9edb3b0f4924325