Home | History | Annotate | Line # | Download | only in lint1
lang_level_c99.c revision 1.2
      1  1.2  rillig /*	$NetBSD: lang_level_c99.c,v 1.2 2023/08/06 19:44:50 rillig Exp $	*/
      2  1.1  rillig # 3 "lang_level_c99.c"
      3  1.1  rillig 
      4  1.1  rillig /*
      5  1.1  rillig  * Tests that are specific to the C99 language level, in particular, features
      6  1.1  rillig  * that were added in C99.
      7  1.1  rillig  *
      8  1.1  rillig  * In the below comments, [-] means unsupported and [x] means supported.
      9  1.1  rillig  */
     10  1.1  rillig 
     11  1.1  rillig /* lint1-flags: -S -w -X 351 */
     12  1.1  rillig 
     13  1.1  rillig /* C99 Foreword */
     14  1.1  rillig 
     15  1.1  rillig // [-] restricted character set support via digraphs and <iso646.h>
     16  1.1  rillig //
     17  1.1  rillig // Lint neither parses digraphs nor trigraphs.
     18  1.1  rillig 
     19  1.1  rillig // [x] wide character library support in <wchar.h> and <wctype.h>
     20  1.1  rillig //
     21  1.1  rillig // On all supported platforms, 'wchar_t' == 'int'.
     22  1.1  rillig 
     23  1.1  rillig const int wide_string[] = L"wide";
     24  1.1  rillig 
     25  1.1  rillig // [x] more precise aliasing rules via effective type
     26  1.1  rillig //
     27  1.1  rillig // Irrelevant, as lint does not check the runtime behavior.
     28  1.1  rillig 
     29  1.1  rillig // [x] restricted pointers
     30  1.1  rillig //
     31  1.1  rillig // Can be parsed, are otherwise ignored.
     32  1.1  rillig 
     33  1.1  rillig // [-] variable length arrays
     34  1.1  rillig //
     35  1.1  rillig // Variable length arrays are handled as if the number of elements in the array
     36  1.1  rillig // were always 1.
     37  1.1  rillig 
     38  1.1  rillig /* FIXME: Parameter 'n' _is_ actually used. */
     39  1.1  rillig /* expect+2: warning: parameter 'n' unused in function 'variable_length_arrays' [231] */
     40  1.1  rillig unsigned long long
     41  1.1  rillig variable_length_arrays(int n)
     42  1.1  rillig {
     43  1.1  rillig 	int vla[n];
     44  1.1  rillig 	/* FIXME: The array dimension is not constant, but still negative. */
     45  1.1  rillig 	/* expect+1: error: negative array dimension (-4) [20] */
     46  1.1  rillig 	typedef int sizeof_vla[-(int)sizeof(vla)];
     47  1.1  rillig 	return sizeof(vla);
     48  1.1  rillig }
     49  1.1  rillig 
     50  1.1  rillig // [x] flexible array members
     51  1.1  rillig //
     52  1.1  rillig // Flexible array members are parsed but not validated thoroughly.
     53  1.1  rillig 
     54  1.1  rillig void
     55  1.1  rillig flexible_array_members(void)
     56  1.1  rillig {
     57  1.1  rillig 	struct {
     58  1.1  rillig 		int regular;
     59  1.1  rillig 		int flexible[];
     60  1.1  rillig 	} s = {
     61  1.1  rillig 		0,
     62  1.1  rillig 		// Flexible array member must not be initialized.  Lint does
     63  1.1  rillig 		// not detect this, leaving the job to the C99 compiler.
     64  1.1  rillig 		{ 1, 3, 4, }
     65  1.1  rillig 	};
     66  1.1  rillig 	/* expect+1: error: negative array dimension (-4) [20] */
     67  1.1  rillig 	typedef int sizeof_s[-(int)sizeof(s)];
     68  1.1  rillig }
     69  1.1  rillig 
     70  1.1  rillig // [x] static and type qualifiers in parameter array declarators
     71  1.1  rillig //
     72  1.1  rillig // Can be parsed, are otherwise ignored.
     73  1.1  rillig 
     74  1.1  rillig // [-] complex (and imaginary) support in <complex.h>
     75  1.1  rillig //
     76  1.1  rillig // Lint does not keep track of which parts of a complex object are initialized.
     77  1.1  rillig //
     78  1.1  rillig // Lint does not support '_Imaginary'.
     79  1.1  rillig 
     80  1.1  rillig // [x] type-generic math macros in <tgmath.h>
     81  1.1  rillig //
     82  1.1  rillig // Irrelevant, as lint only sees the preprocessed source code.
     83  1.1  rillig 
     84  1.1  rillig // [x] the long long int type and library functions
     85  1.1  rillig //
     86  1.1  rillig // On all platforms supported by lint, 'long long' is 64 bits wide.  The other
     87  1.1  rillig // fixed-width types are 'char', 'short', 'int' and (only on 64-bit platforms)
     88  1.1  rillig // '__int128_t'.
     89  1.1  rillig //
     90  1.1  rillig // The lint standard libraries -lstdc and -lposix do not contain the
     91  1.1  rillig // functions added in C99.
     92  1.1  rillig 
     93  1.1  rillig /* expect+1: error: negative array dimension (-1) [20] */
     94  1.1  rillig typedef int sizeof_char[-(int)sizeof(char)];
     95  1.1  rillig /* expect+1: error: negative array dimension (-2) [20] */
     96  1.1  rillig typedef int sizeof_short[-(int)sizeof(short)];
     97  1.1  rillig /* expect+1: error: negative array dimension (-4) [20] */
     98  1.1  rillig typedef int sizeof_int[-(int)sizeof(int)];
     99  1.1  rillig /* expect+1: error: negative array dimension (-8) [20] */
    100  1.1  rillig typedef int sizeof_long_long[-(int)sizeof(long long)];
    101  1.1  rillig 
    102  1.1  rillig // [x] increased minimum translation limits
    103  1.1  rillig //
    104  1.1  rillig // Irrelevant, as lint does not have any hard-coded limits.
    105  1.1  rillig 
    106  1.1  rillig // [x] additional floating-point characteristics in <float.h>
    107  1.1  rillig //
    108  1.1  rillig // Lint has very limited support for floating point numbers, as it fully relies
    109  1.1  rillig // on the host platform.  This is noticeable when cross-compiling between
    110  1.1  rillig // platforms with different size or representation of 'long double'.
    111  1.1  rillig 
    112  1.1  rillig // [x] remove implicit int
    113  1.1  rillig //
    114  1.1  rillig // Lint parses old-style declarations and marks them as errors.
    115  1.1  rillig 
    116  1.1  rillig // [x] reliable integer division
    117  1.1  rillig //
    118  1.1  rillig // The lint source code requires a C99 compiler, so when mapping the integer
    119  1.1  rillig // operations to those from the host platform, lint uses these.
    120  1.1  rillig 
    121  1.1  rillig // [-] universal character names (\u and \U)
    122  1.1  rillig //
    123  1.1  rillig // No, as nothing in the NetBSD source tree uses this feature.
    124  1.1  rillig 
    125  1.1  rillig // [-] extended identifiers
    126  1.1  rillig //
    127  1.1  rillig // No, as nothing in the NetBSD source tree uses this feature.
    128  1.1  rillig 
    129  1.1  rillig // [x] hexadecimal floating-point constants and %a and %A printf/scanf
    130  1.1  rillig // conversion specifiers
    131  1.1  rillig 
    132  1.1  rillig void pf();			/* no prototype parameters */
    133  1.1  rillig 
    134  1.1  rillig void
    135  1.1  rillig hexadecimal_floating_point_constants(void)
    136  1.1  rillig {
    137  1.1  rillig 	double hex = 0x1.0p34;
    138  1.1  rillig 	pf("%s %a\n", "hex", hex);
    139  1.1  rillig }
    140  1.1  rillig 
    141  1.1  rillig // [x] compound literals
    142  1.1  rillig //
    143  1.1  rillig // See d_c99_compound_literal_comma.c.
    144  1.1  rillig 
    145  1.1  rillig // [x] designated initializers
    146  1.1  rillig //
    147  1.1  rillig // See d_c99_init.c.
    148  1.1  rillig 
    149  1.1  rillig // [x] // comments
    150  1.1  rillig //
    151  1.1  rillig // Also supported in GCC mode.
    152  1.1  rillig 
    153  1.1  rillig // [?] extended integer types and library functions in <inttypes.h> and
    154  1.1  rillig // <stdint.h>
    155  1.1  rillig //
    156  1.1  rillig // TODO
    157  1.1  rillig 
    158  1.1  rillig // [x] remove implicit function declaration
    159  1.1  rillig 
    160  1.1  rillig void
    161  1.1  rillig call_implicitly_declared_function(void)
    162  1.1  rillig {
    163  1.1  rillig 	/* expect+1: error: function 'implicitly_declared_function' implicitly declared to return int [215] */
    164  1.1  rillig 	implicitly_declared_function(0);
    165  1.1  rillig }
    166  1.1  rillig 
    167  1.1  rillig // [x] preprocessor arithmetic done in intmax_t/uintmax_t
    168  1.1  rillig //
    169  1.1  rillig // Irrelevant, as lint only sees the preprocessed source code.
    170  1.1  rillig 
    171  1.1  rillig // [x] mixed declarations and code
    172  1.1  rillig 
    173  1.1  rillig // [x] new block scopes for selection and iteration statements
    174  1.1  rillig 
    175  1.1  rillig // [?] integer constant type rules
    176  1.1  rillig //
    177  1.1  rillig // TODO
    178  1.1  rillig 
    179  1.1  rillig // [?] integer promotion rules
    180  1.1  rillig //
    181  1.1  rillig // TODO
    182  1.1  rillig 
    183  1.1  rillig // [x] macros with a variable number of arguments
    184  1.1  rillig //
    185  1.1  rillig // Irrelevant, as lint only sees the preprocessed source code.
    186  1.1  rillig 
    187  1.1  rillig // [x] the vscanf family of functions in <stdio.h> and <wchar.h>
    188  1.1  rillig //
    189  1.1  rillig // Irrelevant, as typical C99 compilers already check these.
    190  1.1  rillig 
    191  1.1  rillig // [x] additional math library functions in <math.h>
    192  1.1  rillig //
    193  1.1  rillig // Irrelevant, as lint does not check arithmetic expressions.
    194  1.1  rillig //
    195  1.1  rillig // Lint also does not generate its own standard library definition for libm.
    196  1.1  rillig 
    197  1.1  rillig // [x] treatment of error conditions by math library functions
    198  1.1  rillig // (math_errhandling)
    199  1.1  rillig //
    200  1.1  rillig // Irrelevant, as lint does not check for error handling.
    201  1.1  rillig 
    202  1.1  rillig // [x] floating-point environment access in <fenv.h>
    203  1.1  rillig //
    204  1.1  rillig // TODO
    205  1.1  rillig 
    206  1.1  rillig // [x] IEC 60559 (also known as IEC 559 or IEEE arithmetic) support
    207  1.1  rillig //
    208  1.1  rillig // On platforms that conform to IEC 60559, lint performs the arithmetic
    209  1.1  rillig // operations accordingly.  When cross-compiling on a vax host for other target
    210  1.1  rillig // platforms, no such support is available.
    211  1.1  rillig 
    212  1.1  rillig // [x] trailing comma allowed in enum declaration
    213  1.1  rillig //
    214  1.1  rillig // Yes, see the grammar rule 'enums_with_opt_comma'.
    215  1.1  rillig 
    216  1.1  rillig // [-] %lf conversion specifier allowed in printf
    217  1.1  rillig //
    218  1.1  rillig // TODO: see tests/lint2/msg_013.exp.
    219  1.1  rillig 
    220  1.1  rillig // [x] inline functions
    221  1.1  rillig //
    222  1.1  rillig // Yes, also allowed in GCC mode.
    223  1.1  rillig 
    224  1.1  rillig // [x] the snprintf family of functions in <stdio.h>
    225  1.1  rillig //
    226  1.1  rillig // The snprintf functions are treated like all other functions.  The checks for
    227  1.1  rillig // matching format strings targets traditional C only and thus does not apply
    228  1.1  rillig // to these functions, as they have a prototype definition.
    229  1.1  rillig 
    230  1.1  rillig // [x] boolean type in <stdbool.h>
    231  1.1  rillig //
    232  1.1  rillig // Yes.  Conversion to and from boolean follows 6.3.1.2.  See also the -T flag,
    233  1.1  rillig // which enables 'strict bool mode'.
    234  1.1  rillig 
    235  1.1  rillig // [x] idempotent type qualifiers
    236  1.1  rillig //
    237  1.1  rillig // Lint warns about duplicate type qualifiers but accepts them otherwise.
    238  1.1  rillig 
    239  1.1  rillig /* expect+1: warning: duplicate 'const' [10] */
    240  1.1  rillig const const int duplicate_type_qualifier = 2;
    241  1.1  rillig 
    242  1.1  rillig // [x] empty macro arguments
    243  1.1  rillig //
    244  1.1  rillig // Irrelevant, as lint only sees the preprocessed source code.
    245  1.1  rillig 
    246  1.1  rillig // [?] new structure type compatibility rules (tag compatibility)
    247  1.1  rillig //
    248  1.1  rillig // TODO
    249  1.1  rillig 
    250  1.1  rillig // [x] additional predefined macro names
    251  1.1  rillig //
    252  1.1  rillig // Irrelevant, as lint only sees the preprocessed source code.
    253  1.1  rillig 
    254  1.1  rillig // [-] _Pragma preprocessing operator
    255  1.1  rillig //
    256  1.1  rillig // No, not yet asked for.
    257  1.1  rillig 
    258  1.1  rillig // [-] standard pragmas
    259  1.1  rillig //
    260  1.1  rillig // No, not yet asked for.
    261  1.1  rillig 
    262  1.1  rillig // [x] __func__ predefined identifier
    263  1.1  rillig //
    264  1.1  rillig // Yes, see 'fallback_symbol'.
    265  1.1  rillig 
    266  1.1  rillig // [x] va_copy macro
    267  1.1  rillig //
    268  1.1  rillig // Irrelevant, as lint only sees the preprocessed source code.
    269  1.1  rillig 
    270  1.1  rillig // [x] additional strftime conversion specifiers
    271  1.1  rillig //
    272  1.1  rillig // Irrelevant, as lint does not check strftime in depth.
    273  1.1  rillig 
    274  1.1  rillig // [?] LIA compatibility annex
    275  1.1  rillig //
    276  1.1  rillig // TODO
    277  1.1  rillig 
    278  1.1  rillig // [x] deprecate ungetc at the beginning of a binary file
    279  1.1  rillig //
    280  1.1  rillig // Irrelevant, as lint's analysis is not that deep into the runtime behavior.
    281  1.1  rillig 
    282  1.1  rillig // [x] remove deprecation of aliased array parameters
    283  1.1  rillig //
    284  1.1  rillig // Irrelevant, as lint does not check for aliasing.
    285  1.1  rillig 
    286  1.1  rillig // [?] conversion of array to pointer not limited to lvalues
    287  1.1  rillig //
    288  1.1  rillig // TODO
    289  1.1  rillig 
    290  1.1  rillig // [x] relaxed constraints on aggregate and union initialization
    291  1.1  rillig //
    292  1.1  rillig // Yes, struct and union members can be initialized with non-constant
    293  1.1  rillig // expressions.  Members that have struct or union type can be initialized with
    294  1.1  rillig // an expression of the same type.
    295  1.1  rillig 
    296  1.1  rillig // [x] relaxed restrictions on portable header names
    297  1.1  rillig //
    298  1.1  rillig // Irrelevant, as lint only sees the preprocessed source code.
    299  1.1  rillig 
    300  1.2  rillig // [x] return without expression not permitted in function that returns a value
    301  1.1  rillig // (and vice versa)
    302  1.1  rillig 
    303  1.1  rillig void
    304  1.1  rillig return_no_expr(int x)
    305  1.1  rillig {
    306  1.1  rillig 	x++;
    307  1.1  rillig 	/* expect+1: error: void function 'return_no_expr' cannot return value [213] */
    308  1.1  rillig 	return x;
    309  1.1  rillig }
    310  1.1  rillig 
    311  1.1  rillig int
    312  1.1  rillig return_expr(void)
    313  1.1  rillig {
    314  1.2  rillig 	/* expect+1: error: function 'return_expr' expects to return value [214] */
    315  1.1  rillig 	return;
    316  1.1  rillig }
    317