Module Name:    src
Committed By:   rillig
Date:           Thu May  9 11:08:07 UTC 2024

Modified Files:
        src/tests/usr.bin/xlint/lint1: c23.c
        src/usr.bin/xlint/lint1: cgram.y debug.c externs1.h lex.c lint1.h

Log Message:
lint: in C23 mode, support the nullptr constant


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/tests/usr.bin/xlint/lint1/c23.c
cvs rdiff -u -r1.495 -r1.496 src/usr.bin/xlint/lint1/cgram.y
cvs rdiff -u -r1.77 -r1.78 src/usr.bin/xlint/lint1/debug.c
cvs rdiff -u -r1.224 -r1.225 src/usr.bin/xlint/lint1/externs1.h \
    src/usr.bin/xlint/lint1/lex.c
cvs rdiff -u -r1.225 -r1.226 src/usr.bin/xlint/lint1/lint1.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.10 src/tests/usr.bin/xlint/lint1/c23.c:1.11
--- src/tests/usr.bin/xlint/lint1/c23.c:1.10	Tue May  7 21:13:27 2024
+++ src/tests/usr.bin/xlint/lint1/c23.c	Thu May  9 11:08:07 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: c23.c,v 1.10 2024/05/07 21:13:27 rillig Exp $	*/
+/*	$NetBSD: c23.c,v 1.11 2024/05/09 11:08:07 rillig Exp $	*/
 # 3 "c23.c"
 
 // Tests for the option -Ac23, which allows features from C23 and all earlier
@@ -28,6 +28,20 @@ c99_bool_is_still_valid_in_c23(void)
 }
 
 
+bool
+null_pointer_constant(const char *p, double dbl)
+{
+	/* expect+1: error: operands of '!=' have incompatible types 'double' and 'pointer to void' [107] */
+	if (dbl != nullptr)
+		p++;
+	if (dbl > 0.0)
+		p++;
+	if (*p == '\0')
+		p = nullptr;
+	return p == nullptr;
+}
+
+
 int
 empty_initializer_braces(void)
 {

Index: src/usr.bin/xlint/lint1/cgram.y
diff -u src/usr.bin/xlint/lint1/cgram.y:1.495 src/usr.bin/xlint/lint1/cgram.y:1.496
--- src/usr.bin/xlint/lint1/cgram.y:1.495	Fri May  3 04:04:17 2024
+++ src/usr.bin/xlint/lint1/cgram.y	Thu May  9 11:08:07 2024
@@ -1,5 +1,5 @@
 %{
-/* $NetBSD: cgram.y,v 1.495 2024/05/03 04:04:17 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.496 2024/05/09 11:08:07 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: cgram.y,v 1.495 2024/05/03 04:04:17 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.496 2024/05/09 11:08:07 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -140,6 +140,7 @@ is_either(const char *s, const char *a, 
 	array_size y_array_size;
 	bool	y_in_system_header;
 	designation y_designation;
+	named_constant y_named_constant;
 };
 
 /* for Bison:
@@ -199,6 +200,7 @@ is_either(const char *s, const char *a, 
 			fprintf(yyo, "<scalar>");
 	}
 } <y_designation>
+%printer { fprintf(yyo, "%s", named_constant_name($$)); } <y_named_constant>
 */
 
 %token			T_LBRACE T_RBRACE T_LBRACK T_RBRACK T_LPAREN T_RPAREN
@@ -287,6 +289,7 @@ is_either(const char *s, const char *a, 
 %token	<y_name>	T_NAME
 %token	<y_name>	T_TYPENAME
 %token	<y_val>		T_CON
+%token	<y_named_constant> T_NAMED_CONSTANT
 %token	<y_string>	T_STRING
 
 /* No type for program. */
@@ -484,6 +487,25 @@ primary_expression:
 |	T_CON {
 		$$ = build_constant(gettyp($1->v_tspec), $1);
 	}
+|	T_NAMED_CONSTANT {
+		if ($1 == NC_NULLPTR) {
+			tnode_t *zero = expr_alloc_tnode();
+			zero->tn_op = CON;
+			zero->tn_type = gettyp(INT);
+			zero->u.value.v_tspec = INT;
+
+			type_t *void_ptr = block_derive_type(gettyp(VOID), PTR);
+			$$ = convert(CVT, 0, void_ptr, zero);
+			$$->tn_sys = zero->tn_sys;
+		} else {
+			tnode_t *nc = expr_alloc_tnode();
+			nc->tn_op = CON;
+			nc->tn_type = gettyp(BOOL);
+			nc->u.value.v_tspec = BOOL;
+			nc->u.value.u.integer = $1 == NC_TRUE ? 1 : 0;
+			$$ = nc;
+		}
+	}
 |	string {
 		$$ = build_string($1);
 	}

Index: src/usr.bin/xlint/lint1/debug.c
diff -u src/usr.bin/xlint/lint1/debug.c:1.77 src/usr.bin/xlint/lint1/debug.c:1.78
--- src/usr.bin/xlint/lint1/debug.c:1.77	Fri May  3 04:04:17 2024
+++ src/usr.bin/xlint/lint1/debug.c	Thu May  9 11:08:07 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.77 2024/05/03 04:04:17 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.78 2024/05/09 11:08:07 rillig Exp $ */
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: debug.c,v 1.77 2024/05/03 04:04:17 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.78 2024/05/09 11:08:07 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -361,6 +361,18 @@ function_specifier_name(function_specifi
 	return name[spec];
 }
 
+const char *
+named_constant_name(named_constant nc)
+{
+	static const char *const name[] = {
+	    "false",
+	    "true",
+	    "nullptr",
+	};
+
+	return name[nc];
+}
+
 static void
 debug_word(bool flag, const char *name)
 {

Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.224 src/usr.bin/xlint/lint1/externs1.h:1.225
--- src/usr.bin/xlint/lint1/externs1.h:1.224	Fri May  3 04:04:18 2024
+++ src/usr.bin/xlint/lint1/externs1.h	Thu May  9 11:08:07 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: externs1.h,v 1.224 2024/05/03 04:04:18 rillig Exp $	*/
+/*	$NetBSD: externs1.h,v 1.225 2024/05/09 11:08:07 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -136,6 +136,7 @@ const char *scl_name(scl_t);
 const char *symbol_kind_name(symbol_kind);
 const char *type_qualifiers_string(type_qualifiers);
 const char *function_specifier_name(function_specifier);
+const char *named_constant_name(named_constant);
 void debug_dcs(void);
 void debug_dcs_all(void);
 void debug_node(const tnode_t *);
Index: src/usr.bin/xlint/lint1/lex.c
diff -u src/usr.bin/xlint/lint1/lex.c:1.224 src/usr.bin/xlint/lint1/lex.c:1.225
--- src/usr.bin/xlint/lint1/lex.c:1.224	Tue May  7 21:13:26 2024
+++ src/usr.bin/xlint/lint1/lex.c	Thu May  9 11:08:07 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.224 2024/05/07 21:13:26 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.225 2024/05/09 11:08:07 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.224 2024/05/07 21:13:26 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.225 2024/05/09 11:08:07 rillig Exp $");
 #endif
 
 #include <ctype.h>
@@ -97,8 +97,9 @@ 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_const(name, named_constant,	since, gcc, deco) \
+	kwdef(name, T_NAMED_CONSTANT, \
+	    .u.kw_named_constant = (named_constant), since, gcc, deco)
 #define kwdef_keyword(name, token) \
 	kwdef(name, token, {false},		78, 0, 1)
 
@@ -114,7 +115,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 */
+		named_constant kw_named_constant;
 	} u;
 	bool	kw_added_in_c90:1;
 	bool	kw_added_in_c99_or_c11:1;
@@ -165,6 +166,7 @@ static const struct keyword {
 #endif
 	kwdef_type(	"long",		LONG,			78),
 	kwdef("_Noreturn", T_FUNCTION_SPECIFIER, .u.kw_fs = FS_NORETURN, 11,0,1),
+	kwdef_const(	"nullptr",	NC_NULLPTR,		23,0,1),
 	// XXX: __packed is GCC-specific.
 	kwdef_token(	"__packed",	T_PACKED,		78,0,1),
 	kwdef_token(	"__real__",	T_REAL,			78,1,1),
@@ -374,8 +376,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;
+	if (tok == T_NAMED_CONSTANT)
+		sym->u.s_keyword.u.named_constant = kw->u.kw_named_constant;
 
 	symtab_add(sym);
 }
@@ -455,12 +457,8 @@ 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;
-	}
+	if (tok == T_NAMED_CONSTANT)
+		yylval.y_named_constant = sym->u.s_keyword.u.named_constant;
 	return tok;
 }
 

Index: src/usr.bin/xlint/lint1/lint1.h
diff -u src/usr.bin/xlint/lint1/lint1.h:1.225 src/usr.bin/xlint/lint1/lint1.h:1.226
--- src/usr.bin/xlint/lint1/lint1.h:1.225	Tue May  7 21:13:26 2024
+++ src/usr.bin/xlint/lint1/lint1.h	Thu May  9 11:08:07 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.225 2024/05/07 21:13:26 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.226 2024/05/09 11:08:07 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -190,7 +190,7 @@ typedef enum {
 typedef enum {
 	NC_FALSE,		/* since C23 */
 	NC_TRUE,		/* since C23 */
-	// TODO: null_ptr
+	NC_NULLPTR,		/* since C23 */
 } named_constant;
 
 /* A type, variable, keyword; basically anything that has a name. */
@@ -240,7 +240,7 @@ struct sym {
 				/* if T_FUNCTION_SPECIFIER */
 				function_specifier function_specifier;
 				/* if T_CON */
-				named_constant constant;
+				named_constant named_constant;
 			} u;
 		} s_keyword;
 		sym_t	*s_old_style_params;	/* parameters in an old-style

Reply via email to