Debian Codegraph @

clang/test/Sema/c2x-fallthrough.c

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