1 /*        $NetBSD: gcc.c,v 1.4 2024/08/29 20:35:19 rillig Exp $       */
2 # 3 "gcc.c"
3 
4 /*
5  * Miscellaneous tests that are specific to lint's GCC mode.
6  */
7 
8 /* lint1-extra-flags: -X 351 */
9 
10 // Before C99 introduced __func__, GCC already had __FUNCTION__ with the same
11 // semantics.
12 const char *
gcc_function(void)13 gcc_function(void)
14 {
15           /* expect+1: error: negative array dimension (-13) [20] */
16           typedef int size[-(int)sizeof __FUNCTION__];
17 
18           return __FUNCTION__;
19 }
20 
21 // Before C99 introduced designators in initializers, GCC already had them,
22 // although with a different syntax for struct/union members and with the
23 // a...b for ranges of array elements.
24 int array_range_initializers[256] = {
25           [2] = 1,
26           [3] = 2,
27           [4 ... 5] = 3
28 };
29 
30 _Bool dbl_isinf(double);
31 
32 // Test that the GCC '__extension__' and '__typeof' are recognized.
33 void
extension_and_typeof(void)34 extension_and_typeof(void)
35 {
36           double __logbw = 1;
37           if (__extension__(({
38                     __typeof((__logbw)) x_ = (__logbw);
39                     !dbl_isinf((x_));
40           })))
41                     __logbw = 1;
42 }
43 
44 int
range_in_case_label(int i)45 range_in_case_label(int i)
46 {
47           switch (i) {
48           case 1 ... 40:                // This is a GCC extension.
49                     return 1;
50           default:
51                     return 2;
52           }
53 }
54 
55 union {
56           int i;
57           char *s;
58 } initialize_union_with_mixed_designators[] = {
59           { i: 1 },           /* GCC-style */
60           { s: "foo" },                 /* GCC-style */
61           { .i = 1 },                   /* C99-style */
62           { .s = "foo" }                /* C99-style */
63 };
64 
65 union {
66           int i[10];
67           short s;
68 } initialize_union_with_gcc_designators[] = {
69           { s: 2 },
70           { i: { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } },
71 };
72 
73 void
declaration_of_variable_array(int i)74 declaration_of_variable_array(int i)
75 {
76           int array[i];
77           while (i-- > 0)
78                     array[i] = 0;
79 }
80 
81 /*
82  * Before cgram.y 1.226 from 2021-05-03, lint could not parse typeof(...) if
83  * there was a statement before it.
84  */
85 void *
typeof_after_statement(void ** ptr)86 typeof_after_statement(void **ptr)
87 {
88           return ({
89                     if (*ptr != (void *)0)
90                               ptr++;
91                     __typeof__(*ptr) ret = *ptr;
92                     ret;
93           });
94 }
95 
96 const char *
auto_type(const char * ptr)97 auto_type(const char *ptr)
98 {
99           __auto_type pp = &ptr;
100           return *pp;
101 }
102