Hi Joseph,

here is the revised patch. I remove the 'fallthrough'
code as suggested, so everything becomes even simpler.
Some tests had to be changed then, but it seems Ok to
me.

Best,
Martin


diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog
index ca844ca775a..c656b5f4cc5 100644
--- a/gcc/c/ChangeLog
+++ b/gcc/c/ChangeLog
@@ -1,3 +1,8 @@
+2020-11-06  Martin Uecker  <muec...@gwdg.de>
+
+       * c-parser.c (c_parser_label): Implement mixing of labels and code.
+       (c_parser_all_labels): Likewise.
+
 2020-10-29  Asher Gordon  <asd...@posteo.net>
 
        * c-typeck.c (free_all_tagged_tu_seen_up_to): Replace free
diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c
index fc97aa3f95f..53179f10985 100644
--- a/gcc/c/c-parser.c
+++ b/gcc/c/c-parser.c
@@ -1521,7 +1521,7 @@ static void c_parser_initval (c_parser *, struct c_expr *,
                              struct obstack *);
 static tree c_parser_compound_statement (c_parser *, location_t * = NULL);
 static location_t c_parser_compound_statement_nostart (c_parser *);
-static void c_parser_label (c_parser *);
+static void c_parser_label (c_parser *, tree);
 static void c_parser_statement (c_parser *, bool *, location_t * = NULL);
 static void c_parser_statement_after_labels (c_parser *, bool *,
                                             vec<tree> * = NULL);
@@ -5523,7 +5523,7 @@ c_parser_initval (c_parser *parser, struct c_expr *after,
 }
 
 /* Parse a compound statement (possibly a function body) (C90 6.6.2,
-   C99 6.8.2, C11 6.8.2).
+   C99 6.8.2, C11 6.8.2, C2X 6.8.2).
 
    compound-statement:
      { block-item-list[opt] }
@@ -5534,6 +5534,7 @@ c_parser_initval (c_parser *parser, struct c_expr *after,
      block-item-list block-item
 
    block-item:
+     label
      nested-declaration
      statement
 
@@ -5674,7 +5675,7 @@ c_parser_compound_statement_nostart (c_parser *parser)
     {
       location_t loc = c_parser_peek_token (parser)->location;
       loc = expansion_point_location_if_in_system_header (loc);
-      /* Standard attributes may start a statement or a declaration.  */
+      /* Standard attributes may start a label, statement or declaration.  */
       bool have_std_attrs
        = c_parser_nth_token_starts_std_attributes (parser, 1);
       tree std_attrs = NULL_TREE;
@@ -5685,7 +5686,6 @@ c_parser_compound_statement_nostart (c_parser *parser)
          || (c_parser_next_token_is (parser, CPP_NAME)
              && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
        {
-         c_warn_unused_attributes (std_attrs);
          if (c_parser_next_token_is_keyword (parser, RID_CASE))
            label_loc = c_parser_peek_2nd_token (parser)->location;
          else
@@ -5693,27 +5693,31 @@ c_parser_compound_statement_nostart (c_parser *parser)
          last_label = true;
          last_stmt = false;
          mark_valid_location_for_stdc_pragma (false);
-         c_parser_label (parser);
+         c_parser_label (parser, std_attrs);
        }
-      else if (!last_label
-              && (c_parser_next_tokens_start_declaration (parser)
-                  || (have_std_attrs
-                      && c_parser_next_token_is (parser, CPP_SEMICOLON))))
+      else if (c_parser_next_tokens_start_declaration (parser)
+              || (have_std_attrs
+                  && c_parser_next_token_is (parser, CPP_SEMICOLON)))
        {
-         last_label = false;
+         if (last_label)
+           pedwarn_c11 (c_parser_peek_token (parser)->location, OPT_Wpedantic,
+                        "a label can only be part of a statement and "
+                        "a declaration is not a statement");
+
          mark_valid_location_for_stdc_pragma (false);
          bool fallthru_attr_p = false;
          c_parser_declaration_or_fndef (parser, true, !have_std_attrs,
                                         true, true, true, NULL,
                                         vNULL, have_std_attrs, std_attrs,
                                         NULL, &fallthru_attr_p);
+
          if (last_stmt && !fallthru_attr_p)
            pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
                         "ISO C90 forbids mixed declarations and code");
          last_stmt = fallthru_attr_p;
+         last_label = false;
        }
-      else if (!last_label
-              && c_parser_next_token_is_keyword (parser, RID_EXTENSION))
+      else if (c_parser_next_token_is_keyword (parser, RID_EXTENSION))
        {
          /* __extension__ can start a declaration, but is also an
             unary operator that can start an expression.  Consume all
@@ -5796,7 +5800,7 @@ c_parser_compound_statement_nostart (c_parser *parser)
       parser->error = false;
     }
   if (last_label)
-    error_at (label_loc, "label at end of compound statement");
+    pedwarn_c11 (label_loc, OPT_Wpedantic, "label at end of compound 
statement");
   location_t endloc = c_parser_peek_token (parser)->location;
   c_parser_consume_token (parser);
   /* Restore the value we started with.  */
@@ -5812,19 +5816,29 @@ c_parser_compound_statement_nostart (c_parser *parser)
 static void
 c_parser_all_labels (c_parser *parser)
 {
+  tree std_attrs = NULL;
   if (c_parser_nth_token_starts_std_attributes (parser, 1))
     {
-      tree std_attrs = c_parser_std_attribute_specifier_sequence (parser);
+      std_attrs = c_parser_std_attribute_specifier_sequence (parser);
       if (c_parser_next_token_is (parser, CPP_SEMICOLON))
        c_parser_error (parser, "expected statement");
-      else
-       c_warn_unused_attributes (std_attrs);
     }
   while (c_parser_next_token_is_keyword (parser, RID_CASE)
         || c_parser_next_token_is_keyword (parser, RID_DEFAULT)
         || (c_parser_next_token_is (parser, CPP_NAME)
             && c_parser_peek_2nd_token (parser)->type == CPP_COLON))
-    c_parser_label (parser);
+    {
+      c_parser_label (parser, std_attrs);
+      std_attrs = NULL;
+      if (c_parser_nth_token_starts_std_attributes (parser, 1))
+       {
+         std_attrs = c_parser_std_attribute_specifier_sequence (parser);
+         if (c_parser_next_token_is (parser, CPP_SEMICOLON))
+           c_parser_error (parser, "expected statement");
+       }
+    }
+   if (std_attrs)
+     c_warn_unused_attributes (std_attrs);
 }
 
 /* Parse a label (C90 6.6.1, C99 6.8.1, C11 6.8.1).
@@ -5846,9 +5860,8 @@ c_parser_all_labels (c_parser *parser)
    in the caller, to distinguish statements from declarations.  Any
    attribute-specifier-sequence after the label is parsed in this
    function.  */
-
 static void
-c_parser_label (c_parser *parser)
+c_parser_label (c_parser *parser, tree std_attrs)
 {
   location_t loc1 = c_parser_peek_token (parser)->location;
   tree label = NULL_TREE;
@@ -5898,8 +5911,13 @@ c_parser_label (c_parser *parser)
       if (tlab)
        {
          decl_attributes (&tlab, attrs, 0);
+         decl_attributes (&tlab, std_attrs, 0);
          label = add_stmt (build_stmt (loc1, LABEL_EXPR, tlab));
        }
+      if (attrs
+         && c_parser_next_tokens_start_declaration (parser))
+         warning_at (loc2, OPT_Wattributes, "GNU-style attribute between"
+                     " label and declaration appertains to the label");
     }
   if (label)
     {
@@ -5907,55 +5925,6 @@ c_parser_label (c_parser *parser)
        FALLTHROUGH_LABEL_P (LABEL_EXPR_LABEL (label)) = fallthrough_p;
       else
        FALLTHROUGH_LABEL_P (CASE_LABEL (label)) = fallthrough_p;
-
-      /* Standard attributes are only allowed here if they start a
-        statement, not a declaration (including the case of an
-        attribute-declaration with only attributes).  */
-      bool have_std_attrs
-       = c_parser_nth_token_starts_std_attributes (parser, 1);
-      tree std_attrs = NULL_TREE;
-      if (have_std_attrs)
-       std_attrs = c_parser_std_attribute_specifier_sequence (parser);
-
-      /* Allow '__attribute__((fallthrough));'.  */
-      if (!have_std_attrs
-         && c_parser_next_token_is_keyword (parser, RID_ATTRIBUTE))
-       {
-         location_t loc = c_parser_peek_token (parser)->location;
-         tree attrs = c_parser_gnu_attributes (parser);
-         if (attribute_fallthrough_p (attrs))
-           {
-             if (c_parser_next_token_is (parser, CPP_SEMICOLON))
-               {
-                 tree fn = build_call_expr_internal_loc (loc,
-                                                         IFN_FALLTHROUGH,
-                                                         void_type_node, 0);
-                 add_stmt (fn);
-               }
-             else
-               warning_at (loc, OPT_Wattributes, "%<fallthrough%> attribute "
-                           "not followed by %<;%>");
-           }
-         else if (attrs != NULL_TREE)
-           warning_at (loc, OPT_Wattributes, "only attribute %<fallthrough%>"
-                       " can be applied to a null statement");
-       }
-      if (c_parser_next_tokens_start_declaration (parser)
-         || (have_std_attrs
-             && c_parser_next_token_is (parser, CPP_SEMICOLON)))
-       {
-         error_at (c_parser_peek_token (parser)->location,
-                   "a label can only be part of a statement and "
-                   "a declaration is not a statement");
-         c_parser_declaration_or_fndef (parser, /*fndef_ok*/ false,
-                                        /*static_assert_ok*/ true,
-                                        /*empty_ok*/ true, /*nested*/ true,
-                                        /*start_attr_ok*/ true, NULL,
-                                        vNULL, have_std_attrs, std_attrs);
-       }
-      else if (std_attrs)
-       /* Nonempty attributes on the following statement are ignored.  */
-       c_warn_unused_attributes (std_attrs);
     }
 }
 
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 7a6ecce6a84..89179e243c6 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -55,7 +55,7 @@ extensions, accepted by GCC in C90 mode and in C++.
 * Designated Inits::    Labeling elements of initializers.
 * Case Ranges::         `case 1 ... 9' and such.
 * Cast to Union::       Casting to union type from any member of the union.
-* Mixed Declarations::  Mixing declarations and code.
+* Mixed Labels and Declarations::  Mixing declarations, labels and code.
 * Function Attributes:: Declaring that functions have no side effects,
                         or that they can never return.
 * Variable Attributes:: Specifying attributes of variables.
@@ -2353,15 +2353,17 @@ void hack (union foo);
 hack ((union foo) x);
 @end smallexample
 
-@node Mixed Declarations
-@section Mixed Declarations and Code
+@node Mixed Labels and Declarations
+@section Mixed Declarations, Labels and Code
 @cindex mixed declarations and code
 @cindex declarations, mixed with code
 @cindex code, mixed with declarations
 
 ISO C99 and ISO C++ allow declarations and code to be freely mixed
-within compound statements.  As an extension, GNU C also allows this in
-C90 mode.  For example, you could do:
+within compound statements.  ISO C2X allows labels to be
+placed before declarations and at the end of a compound statement.
+As an extension, GNU C also allows all this in C90 mode.  For example,
+you could do:
 
 @smallexample
 int i;
@@ -8630,7 +8632,8 @@ details of the exact syntax for using attributes.  Other 
attributes are
 available for functions (@pxref{Function Attributes}), variables 
 (@pxref{Variable Attributes}), enumerators (@pxref{Enumerator Attributes}),
 statements (@pxref{Statement Attributes}), and for types
-(@pxref{Type Attributes}).
+(@pxref{Type Attributes}). A label attribute followed
+by a declaration appertains to the label and not the declaration.
 
 This example uses the @code{cold} label attribute to indicate the 
 @code{ErrorHandling} branch is unlikely to be taken and that the
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 5320e6c1e1e..eeb54fb46d9 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -7592,7 +7592,7 @@ except when the same as the default promotion.
 @opindex Wno-declaration-after-statement
 Warn when a declaration is found after a statement in a block.  This
 construct, known from C++, was introduced with ISO C99 and is by default
-allowed in GCC@.  It is not supported by ISO C90.  @xref{Mixed Declarations}.
+allowed in GCC@.  It is not supported by ISO C90.  @xref{Mixed Labels and 
Declarations}.
 
 @item -Wshadow
 @opindex Wshadow
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 40200222446..2a8233c04e4 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,22 @@
+2020-11-06  Martin Uecker  <muec...@gwdg.de>
+
+       * gcc.dg/20031223-1.c: Update compiler flags and adapt test.
+       * gcc.dg/c11-labels-1.c: New test.
+       * gcc.dg/c11-labels-2.c: New test.
+       * gcc.dg/c11-labels-3.c: New test.
+       * gcc.dg/c2x-attr-syntax-3.c: Adapt test.
+       * gcc.dg/c2x-labels-1.c: New test.
+       * gcc.dg/c2x-labels-2.c: New test.
+       * gcc.dg/c2x-labels-3.c: New test.
+       * gcc.dg/decl-9.c: Update compiler flags and add error.
+       * gcc.dg/c-c++-common/attr-fallthrough-2.c: Update compiler flags.
+       * gcc.dg/c-c++-common/Wimplicit-fallthrough-20.c: Adapt test.
+       * gcc.dg/gomp/barrier-2.c: Update compiler flags and add warning.
+       * gcc.dg/gomp/declare-simd-5.c: Update compiler flags and adapt test.
+       * gcc.dg/gomp/declare-variant-2.c: Update compiler flags and add error.
+       * gcc.dg/label-compound-stmt-1.c: Update compiler flags.
+       * gcc.dg/parse-decl-after-label.c: Update compiler flags.
+
 2020-11-03  Jan Hubicka  <j...@suse.cz>
 
        * gcc.c-torture/execute/pr97695.c: New test.
diff --git a/gcc/testsuite/c-c++-common/Wimplicit-fallthrough-20.c 
b/gcc/testsuite/c-c++-common/Wimplicit-fallthrough-20.c
index d37a840a0d9..810c331b19a 100644
--- a/gcc/testsuite/c-c++-common/Wimplicit-fallthrough-20.c
+++ b/gcc/testsuite/c-c++-common/Wimplicit-fallthrough-20.c
@@ -27,13 +27,13 @@ g (int i)
   switch (i)
     {
     case -1:
-      __attribute__((used)); /* { dg-warning "ignored|only attribute" } */
+      __attribute__((used)); /* { dg-warning "empty declaration" } */
     default:
-      __attribute__((used)); /* { dg-warning "ignored|only attribute" } */
+      __attribute__((used)); /* { dg-warning "empty declaration" } */
     case 1:
       return 6;
     case 2 ... 4:
-      __attribute__((used)); /* { dg-warning "ignored|only attribute" } */
+      __attribute__((used)); /* { dg-warning "empty declaration" } */
     case 5:
       return 7;
     }
diff --git a/gcc/testsuite/c-c++-common/attr-fallthrough-2.c 
b/gcc/testsuite/c-c++-common/attr-fallthrough-2.c
index e8659e572ca..be61d5e6666 100644
--- a/gcc/testsuite/c-c++-common/attr-fallthrough-2.c
+++ b/gcc/testsuite/c-c++-common/attr-fallthrough-2.c
@@ -1,6 +1,6 @@
 /* PR c/7652 */
 /* { dg-do compile } */
-/* { dg-options "-Wall -Wextra -Wpedantic -Wno-unused 
-Wno-implicit-fallthrough" } */
+/* { dg-options "-Wall -Wextra -Wno-unused -Wno-implicit-fallthrough" } */
 
 extern void bar (int);
 void
diff --git a/gcc/testsuite/gcc.dg/20031223-1.c 
b/gcc/testsuite/gcc.dg/20031223-1.c
index 68aa74ffe50..c529739e7ec 100644
--- a/gcc/testsuite/gcc.dg/20031223-1.c
+++ b/gcc/testsuite/gcc.dg/20031223-1.c
@@ -3,11 +3,10 @@
    because GCC was trying to expand the trees to rtl.  */
 
 /* { dg-do compile } */
-/* { dg-options "" } */
+/* { dg-options "-std=c17 -pedantic-errors" } */
 
 void f ()
 {
  l: int; /* { dg-error "a label can only be part of a statement and a 
declaration is not a statement" "not stmt" } */
- /* { dg-warning "useless type name in empty declaration" "type name" { target 
*-*-* } .-1 } */
- /* { dg-error "label at end of compound statement" "label" { target *-*-* } 
.-2 } */
+ /* { dg-error "useless type name in empty declaration" "type name" { target 
*-*-* } .-1 } */
 }
diff --git a/gcc/testsuite/gcc.dg/c2x-attr-syntax-3.c 
b/gcc/testsuite/gcc.dg/c2x-attr-syntax-3.c
index 1f883d825e0..2f0d9f60cc0 100644
--- a/gcc/testsuite/gcc.dg/c2x-attr-syntax-3.c
+++ b/gcc/testsuite/gcc.dg/c2x-attr-syntax-3.c
@@ -25,13 +25,14 @@ f2 (void)
 }
 
 /* Declarations, including attribute declarations, cannot appear after
-   labels.  */
+   labels when a statement is expected.  */
 
 void
 f3 (void)
 {
- x: [[]];; /* { dg-error "can only be part of a statement" } */
-}
+  if (1)
+    x: [[]]; /* { dg-error "expected" } */
+} 
 
 /* Prefix attributes cannot appear on type names.  */
 
diff --git a/gcc/testsuite/gcc.dg/decl-9.c b/gcc/testsuite/gcc.dg/decl-9.c
index eeca8e05e14..9bb15609d83 100644
--- a/gcc/testsuite/gcc.dg/decl-9.c
+++ b/gcc/testsuite/gcc.dg/decl-9.c
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-std=gnu89" } */
+/* { dg-options "-std=gnu89 -pedantic-errors" } */
 
 w *x; /* { dg-error "unknown type name 'w'" } */
 
@@ -12,6 +12,7 @@ int f1()
   int d, e;
   d * e; /* { dg-bogus "unknown type name 'd'" } */
   g * h; /* { dg-error "unknown type name 'g'" } */
+  /* { dg-error "mixed declarations" "" { target *-*-* } .-1 } */
   g i;   /* { dg-error "unknown type name 'g'" } */
 }
 
diff --git a/gcc/testsuite/gcc.dg/gomp/barrier-2.c 
b/gcc/testsuite/gcc.dg/gomp/barrier-2.c
index 5a7091946c4..c0d62f572bb 100644
--- a/gcc/testsuite/gcc.dg/gomp/barrier-2.c
+++ b/gcc/testsuite/gcc.dg/gomp/barrier-2.c
@@ -1,4 +1,5 @@
 /* { dg-do compile } */
+/* { dg-options "-Wall -std=c17 -fopenmp -pedantic-errors" } */
 
 void f1(void)
 {
@@ -16,6 +17,7 @@ void f1(void)
 void f2(void)
 {
   label:       /* { dg-error "label at end of compound statement" } */
+    /* { dg-warning "defined but not used" "" { target *-*-* } .-1 } */
     #pragma omp barrier                /* { dg-error "may only be used in 
compound statements" } */
 }
 
diff --git a/gcc/testsuite/gcc.dg/gomp/declare-simd-5.c 
b/gcc/testsuite/gcc.dg/gomp/declare-simd-5.c
index fe236525d62..b9a4161c0c9 100644
--- a/gcc/testsuite/gcc.dg/gomp/declare-simd-5.c
+++ b/gcc/testsuite/gcc.dg/gomp/declare-simd-5.c
@@ -1,5 +1,6 @@
 /* Test parsing of #pragma omp declare simd */
 /* { dg-do compile } */
+/* { dg-options "-std=c17 -fopenmp -pedantic-errors" } */
 
 int
 f1 (int x)
@@ -14,7 +15,7 @@ f1 (int x)
 lab:
     #pragma omp declare simd simdlen (8) aligned (b : 8 * sizeof (int))
     extern int f5 (int a, int *b, int c);      /* { dg-error "must be followed 
by function declaration or definition" } */
-    x++;                                       /* { dg-error "expected 
expression before" "" { target *-*-* } .-1 } */
+    x++;                                       /* { dg-error "a label can only 
be part of a statement and a declaration is not a statement" "" { target *-*-* 
} .-1 } */
   }
   return x;
 }
diff --git a/gcc/testsuite/gcc.dg/gomp/declare-variant-2.c 
b/gcc/testsuite/gcc.dg/gomp/declare-variant-2.c
index 701d83b0ec3..39c2c1d40ee 100644
--- a/gcc/testsuite/gcc.dg/gomp/declare-variant-2.c
+++ b/gcc/testsuite/gcc.dg/gomp/declare-variant-2.c
@@ -1,5 +1,6 @@
 /* Test parsing of #pragma omp declare variant */
 /* { dg-do compile } */
+/* { dg-options "-std=c17 -fopenmp -pedantic-errors" } */
 
 int f0 (int, int *, int);
 
@@ -16,7 +17,7 @@ f1 (int x)
 lab:
     #pragma omp declare variant (fn0) match (user={condition(0)})
     extern int f5 (int a, int *b, int c);      /* { dg-error "must be followed 
by function declaration or definition" } */
-    x++;                                       /* { dg-error "expected 
expression before" "" { target *-*-* } .-1 } */
+    x++;                                       /* { dg-error "a label can only 
be part of a statement and a declaration is not a statement" "" { target *-*-* 
} .-1 } */
   }
   return x;
 }
diff --git a/gcc/testsuite/gcc.dg/label-compound-stmt-1.c 
b/gcc/testsuite/gcc.dg/label-compound-stmt-1.c
index 2f8fa4e65c2..2ae2b82ef1c 100644
--- a/gcc/testsuite/gcc.dg/label-compound-stmt-1.c
+++ b/gcc/testsuite/gcc.dg/label-compound-stmt-1.c
@@ -1,7 +1,7 @@
 /* Test that labels at ends of compound statements are hard errors.  */
 /* Origin: Joseph Myers <j...@polyomino.org.uk> */
 /* { dg-do compile } */
-/* { dg-options "" } */
+/* { dg-options "-std=c17 -pedantic-errors" } */
 
 void f(void) { g: } /* { dg-bogus "warning" "warning in place of error" } */
 /* { dg-error "label|parse|syntax" "label at end of compound statement" { 
target *-*-* } .-1 } */
diff --git a/gcc/testsuite/gcc.dg/parse-decl-after-label.c 
b/gcc/testsuite/gcc.dg/parse-decl-after-label.c
index 9c9886a108f..6e2a047eb2e 100644
--- a/gcc/testsuite/gcc.dg/parse-decl-after-label.c
+++ b/gcc/testsuite/gcc.dg/parse-decl-after-label.c
@@ -1,6 +1,6 @@
 /* PR 29062 
 { dg-do compile }
-{ dg-options "-fsyntax-only" }
+{ dg-options "-std=c17 -pedantic-errors -fsyntax-only" }
 */
 
 int f(int x)

Reply via email to