xref: /llvm-project/clang/test/Sema/c2x-fallthrough.c (revision b8266f512a6218e8319379b65a637fa82b3900f6)
1 // RUN: %clang_cc1 -fsyntax-only -std=c2x -verify %s
2 
3 // This is the latest version of fallthrough that we support.
4 _Static_assert(__has_c_attribute(fallthrough) == 201910L);
5 
f(int n)6 void f(int n) {
7   switch (n) {
8   case 0:
9     n += 1;
10     [[fallthrough]]; // ok
11   case 1:
12     if (n) {
13       [[fallthrough]]; // ok
14     } else {
15       return;
16     }
17   case 2:
18     for (int n = 0; n != 10; ++n)
19       [[fallthrough]]; // expected-error {{does not directly precede switch label}}
20   case 3:
21     while (1)
22       [[fallthrough]]; // expected-error {{does not directly precede switch label}}
23   case 4:
24     while (0)
25       [[fallthrough]]; // expected-error {{does not directly precede switch label}}
26   case 5:
27     do [[fallthrough]]; while (1); // expected-error {{does not directly precede switch label}}
28   case 6:
29     do [[fallthrough]]; while (0); // expected-error {{does not directly precede switch label}}
30   case 7:
31     switch (n) {
32     case 0:
33       // FIXME: This should be an error, even though the next thing we do is to
34       // fall through in an outer switch statement.
35       [[fallthrough]];
36     }
37   case 8:
38     [[fallthrough]]; // expected-error {{does not directly precede switch label}}
39     goto label;
40   label:
41   case 9:
42     n += 1;
43   case 10: // no warning, -Wimplicit-fallthrough is not enabled in this test, and does not need to
44            // be enabled for these diagnostics to be produced.
45     break;
46   }
47 }
48 
49 [[fallthrough]] typedef int n1; // expected-error {{'fallthrough' attribute cannot be applied to a declaration}}
50 typedef int [[fallthrough]] n2; // expected-error {{'fallthrough' attribute cannot be applied to types}}
51 typedef int n3 [[fallthrough]]; // expected-error {{'fallthrough' attribute cannot be applied to a declaration}}
52 
53 enum [[fallthrough]] E { // expected-error {{'fallthrough' attribute cannot be applied to a declaration}}
54   One
55 };
56 struct [[fallthrough]] S { // expected-error {{'fallthrough' attribute cannot be applied to a declaration}}
57   int i;
58 };
59 
60 [[fallthrough]] // expected-error {{'fallthrough' attribute cannot be applied to a declaration}}
g(void)61 void g(void) {
62   [[fallthrough]] int n; // expected-error {{'fallthrough' attribute cannot be applied to a declaration}}
63   [[fallthrough]] ++n; // expected-error {{'fallthrough' attribute only applies to empty statements}}
64 
65   switch (n) {
66     // FIXME: This should be an error.
67     [[fallthrough]];
68     return;
69 
70   case 0:
71     [[fallthrough, fallthrough]]; // ok
72   case 1:
73     [[fallthrough(0)]]; // expected-error {{argument list}}
74   case 2:
75     break;
76   }
77 }
78 
79