Module Name: src Committed By: rillig Date: Tue May 7 21:13:27 UTC 2024
Modified Files: src/tests/usr.bin/xlint/lint1: c23.c src/usr.bin/xlint/lint1: lex.c lint1.h src/usr.bin/xlint/xlint: strict-bool-stdbool.h Log Message: lint: in C23 mode, support the keywords 'bool', 'false' and 'true' To generate a diff of this commit: cvs rdiff -u -r1.9 -r1.10 src/tests/usr.bin/xlint/lint1/c23.c cvs rdiff -u -r1.223 -r1.224 src/usr.bin/xlint/lint1/lex.c cvs rdiff -u -r1.224 -r1.225 src/usr.bin/xlint/lint1/lint1.h cvs rdiff -u -r1.1 -r1.2 src/usr.bin/xlint/xlint/strict-bool-stdbool.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/usr.bin/xlint/lint1/c23.c diff -u src/tests/usr.bin/xlint/lint1/c23.c:1.9 src/tests/usr.bin/xlint/lint1/c23.c:1.10 --- src/tests/usr.bin/xlint/lint1/c23.c:1.9 Tue May 7 19:32:35 2024 +++ src/tests/usr.bin/xlint/lint1/c23.c Tue May 7 21:13:27 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: c23.c,v 1.9 2024/05/07 19:32:35 rillig Exp $ */ +/* $NetBSD: c23.c,v 1.10 2024/05/07 21:13:27 rillig Exp $ */ # 3 "c23.c" // Tests for the option -Ac23, which allows features from C23 and all earlier @@ -14,13 +14,8 @@ int bool_is_predefined_in_c23(void) { - /* expect+1: error: syntax error 't' [249] */ bool t = true; bool f = false; - /* expect+4: error: 't' undefined [99] */ - /* expect+3: error: 'true' undefined [99] */ - /* expect+2: error: 'f' undefined [99] */ - /* expect+1: error: 'false' undefined [99] */ return (t == true ? 20 : 0) + (f == false ? 3 : 0); } Index: src/usr.bin/xlint/lint1/lex.c diff -u src/usr.bin/xlint/lint1/lex.c:1.223 src/usr.bin/xlint/lint1/lex.c:1.224 --- src/usr.bin/xlint/lint1/lex.c:1.223 Fri Mar 29 08:35:32 2024 +++ src/usr.bin/xlint/lint1/lex.c Tue May 7 21:13:26 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: lex.c,v 1.223 2024/03/29 08:35:32 rillig Exp $ */ +/* $NetBSD: lex.c,v 1.224 2024/05/07 21:13:26 rillig Exp $ */ /* * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. @@ -38,7 +38,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) -__RCSID("$NetBSD: lex.c,v 1.223 2024/03/29 08:35:32 rillig Exp $"); +__RCSID("$NetBSD: lex.c,v 1.224 2024/05/07 21:13:26 rillig Exp $"); #endif #include <ctype.h> @@ -68,6 +68,8 @@ bool in_gcc_attribute; bool in_system_header; /* + * Define a keyword that cannot be overridden by identifiers. + * * Valid values for 'since' are 78, 90, 99, 11, 23. * * The C11 keywords are all taken from the reserved namespace. They are added @@ -95,6 +97,8 @@ bool in_system_header; kwdef(name, T_TYPE, .u.kw_tspec = (tspec), since, 0, 1) #define kwdef_tqual(name, tqual, since, gcc, deco) \ kwdef(name, T_QUAL, .u.kw_tqual = {.tqual = true}, since, gcc, deco) +#define kwdef_const(name, constant, since, gcc, deco) \ + kwdef(name, T_CON, .u.kw_const = (constant), since, gcc, deco) #define kwdef_keyword(name, token) \ kwdef(name, token, {false}, 78, 0, 1) @@ -110,6 +114,7 @@ static const struct keyword { type_qualifiers kw_tqual; /* if kw_token is T_QUAL */ function_specifier kw_fs; /* if kw_token is * T_FUNCTION_SPECIFIER */ + named_constant kw_const; /* if kw_token is T_CON */ } u; bool kw_added_in_c90:1; bool kw_added_in_c99_or_c11:1; @@ -130,6 +135,7 @@ static const struct keyword { kwdef_token( "attribute", T_ATTRIBUTE, 78,1,6), kwdef_sclass( "auto", AUTO, 78,0,1), kwdef_type( "_Bool", BOOL, 99), + kwdef_type( "bool", BOOL, 23), kwdef_keyword( "break", T_BREAK), kwdef_token( "__builtin_offsetof", T_BUILTIN_OFFSETOF, 78,1,1), kwdef_keyword( "case", T_CASE), @@ -145,6 +151,7 @@ static const struct keyword { kwdef_keyword( "enum", T_ENUM), kwdef_token( "__extension__",T_EXTENSION, 78,1,1), kwdef_sclass( "extern", EXTERN, 78,0,1), + kwdef_const( "false", NC_FALSE, 23,0,1), kwdef_type( "float", FLOAT, 78), kwdef_keyword( "for", T_FOR), kwdef_token( "_Generic", T_GENERIC, 11,0,1), @@ -176,6 +183,7 @@ static const struct keyword { kwdef_sclass( "__thread", THREAD_LOCAL, 78,1,1), kwdef_sclass( "_Thread_local", THREAD_LOCAL, 11,0,1), kwdef_sclass( "thread_local", THREAD_LOCAL, 23,0,1), + kwdef_const( "true", NC_TRUE, 23,0,1), kwdef_sclass( "typedef", TYPEDEF, 78,0,1), kwdef_token( "typeof", T_TYPEOF, 78,1,7), #ifdef INT128_SIZE @@ -366,6 +374,8 @@ register_keyword(const struct keyword *k sym->u.s_keyword.u.sk_type_qualifier = kw->u.kw_tqual; if (tok == T_FUNCTION_SPECIFIER) sym->u.s_keyword.u.function_specifier = kw->u.kw_fs; + if (tok == T_CON) + sym->u.s_keyword.u.constant = kw->u.kw_const; symtab_add(sym); } @@ -445,6 +455,12 @@ lex_keyword(sym_t *sym) if (tok == T_FUNCTION_SPECIFIER) yylval.y_function_specifier = sym->u.s_keyword.u.function_specifier; + if (tok == T_CON) { + val_t *v = xcalloc(1, sizeof(*v)); + v->v_tspec = BOOL; + v->u.integer = sym->u.s_keyword.u.constant == NC_TRUE ? 1 : 0; + yylval.y_val = v; + } return tok; } Index: src/usr.bin/xlint/lint1/lint1.h diff -u src/usr.bin/xlint/lint1/lint1.h:1.224 src/usr.bin/xlint/lint1/lint1.h:1.225 --- src/usr.bin/xlint/lint1/lint1.h:1.224 Fri May 3 04:04:18 2024 +++ src/usr.bin/xlint/lint1/lint1.h Tue May 7 21:13:26 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: lint1.h,v 1.224 2024/05/03 04:04:18 rillig Exp $ */ +/* $NetBSD: lint1.h,v 1.225 2024/05/07 21:13:26 rillig Exp $ */ /* * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. @@ -187,6 +187,12 @@ typedef enum { FS_NORETURN, /* since C11 */ } function_specifier; +typedef enum { + NC_FALSE, /* since C23 */ + NC_TRUE, /* since C23 */ + // TODO: null_ptr +} named_constant; + /* A type, variable, keyword; basically anything that has a name. */ struct sym { const char *s_name; @@ -233,6 +239,8 @@ struct sym { type_qualifiers sk_type_qualifier; /* if T_FUNCTION_SPECIFIER */ function_specifier function_specifier; + /* if T_CON */ + named_constant constant; } u; } s_keyword; sym_t *s_old_style_params; /* parameters in an old-style Index: src/usr.bin/xlint/xlint/strict-bool-stdbool.h diff -u src/usr.bin/xlint/xlint/strict-bool-stdbool.h:1.1 src/usr.bin/xlint/xlint/strict-bool-stdbool.h:1.2 --- src/usr.bin/xlint/xlint/strict-bool-stdbool.h:1.1 Sat Jan 16 16:03:46 2021 +++ src/usr.bin/xlint/xlint/strict-bool-stdbool.h Tue May 7 21:13:26 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: strict-bool-stdbool.h,v 1.1 2021/01/16 16:03:46 rillig Exp $ */ +/* $NetBSD: strict-bool-stdbool.h,v 1.2 2024/05/07 21:13:26 rillig Exp $ */ /*- * Copyright (c) 2021 The NetBSD Foundation, Inc. @@ -32,9 +32,11 @@ #ifndef _LINT_STDBOOL_H #define _LINT_STDBOOL_H +#if __STDC_VERSION__ >= 199901 && __STDC_VERSION__ < 202311 #define bool _Bool #define false __lint_false #define true __lint_true +#endif #define __bool_true_false_are_defined 1