Module Name:    src
Committed By:   rillig
Date:           Wed May  1 07:40:11 UTC 2024

Modified Files:
        src/tests/usr.bin/xlint/lint1: expr_precedence.c
            gcc_attribute_aligned.c
        src/usr.bin/xlint/lint1: cgram.y debug.c decl.c externs1.h lint1.h

Log Message:
lint: support _Alignas and __attribute__((__aligned(4)))


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/tests/usr.bin/xlint/lint1/expr_precedence.c
cvs rdiff -u -r1.6 -r1.7 \
    src/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c
cvs rdiff -u -r1.493 -r1.494 src/usr.bin/xlint/lint1/cgram.y
cvs rdiff -u -r1.75 -r1.76 src/usr.bin/xlint/lint1/debug.c
cvs rdiff -u -r1.398 -r1.399 src/usr.bin/xlint/lint1/decl.c
cvs rdiff -u -r1.222 -r1.223 src/usr.bin/xlint/lint1/externs1.h \
    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/expr_precedence.c
diff -u src/tests/usr.bin/xlint/lint1/expr_precedence.c:1.11 src/tests/usr.bin/xlint/lint1/expr_precedence.c:1.12
--- src/tests/usr.bin/xlint/lint1/expr_precedence.c:1.11	Tue Mar 28 14:44:34 2023
+++ src/tests/usr.bin/xlint/lint1/expr_precedence.c	Wed May  1 07:40:11 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: expr_precedence.c,v 1.11 2023/03/28 14:44:34 rillig Exp $	*/
+/*	$NetBSD: expr_precedence.c,v 1.12 2024/05/01 07:40:11 rillig Exp $	*/
 # 3 "expr_precedence.c"
 
 /*
@@ -20,8 +20,9 @@ int init_error = 3, 4;
 int init_syntactically_ok = var = 1 ? 2 : 3;
 
 /*
- * The arguments of __attribute__ must be constant-expression, as assignments
- * don't make sense at that point.
+ * The arguments of __attribute__ must be constant-expression, but for
+ * simplicity of implementation, they are parsed just like function arguments,
+ * even though this allows assignment-expression.
  */
 void __attribute__((format(printf,
     /*
@@ -32,7 +33,6 @@ void __attribute__((format(printf,
      *
      * See lex.c, function 'search', keyword 'in_gcc_attribute'.
      */
-    /* expect+1: error: syntax error '=' [249] */
     var = 1,
     /* Syntactically ok, must be a constant expression though. */
     var > 0 ? 2 : 1)))

Index: src/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c
diff -u src/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c:1.6 src/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c:1.7
--- src/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c:1.6	Fri Jul  7 19:45:22 2023
+++ src/tests/usr.bin/xlint/lint1/gcc_attribute_aligned.c	Wed May  1 07:40:11 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: gcc_attribute_aligned.c,v 1.6 2023/07/07 19:45:22 rillig Exp $	*/
+/*	$NetBSD: gcc_attribute_aligned.c,v 1.7 2024/05/01 07:40:11 rillig Exp $	*/
 # 3 "gcc_attribute_aligned.c"
 
 /*
@@ -47,8 +47,8 @@ struct save87 {
 	struct fpacc87 s87_ac[8];
 };
 
-/* FIXME: @4 2 + @4 2 + @4 2 + @4 8 + @4 8 + @2 (8 * 10) == 108 */
-/* expect+1: error: negative array dimension (-104) [20] */
+/* @4 2 + @4 2 + @4 2 + @4 8 + @4 8 + @2 (8 * 10) == 108 */
+/* expect+1: error: negative array dimension (-108) [20] */
 typedef int sizeof_save87[-(int)sizeof(struct save87)];
 
 
@@ -69,7 +69,6 @@ aligned_struct_member(void)
 	 *
 	 * https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html
 	 */
-	/* TODO: should be -32 instead of -8. */
-	/* expect+1: error: negative array dimension (-8) [20] */
+	/* expect+1: error: negative array dimension (-32) [20] */
 	typedef int ctassert[-(int)sizeof(struct aligned)];
 }

Index: src/usr.bin/xlint/lint1/cgram.y
diff -u src/usr.bin/xlint/lint1/cgram.y:1.493 src/usr.bin/xlint/lint1/cgram.y:1.494
--- src/usr.bin/xlint/lint1/cgram.y:1.493	Fri Mar 29 08:35:32 2024
+++ src/usr.bin/xlint/lint1/cgram.y	Wed May  1 07:40:11 2024
@@ -1,5 +1,5 @@
 %{
-/* $NetBSD: cgram.y,v 1.493 2024/03/29 08:35:32 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.494 2024/05/01 07:40:11 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.493 2024/03/29 08:35:32 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.494 2024/05/01 07:40:11 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -423,7 +423,6 @@ is_either(const char *s, const char *a, 
 /* No type for gcc_attribute_specifier. */
 /* No type for gcc_attribute_list. */
 /* No type for gcc_attribute. */
-/* No type for gcc_attribute_parameters. */
 %type	<y_in_system_header> sys
 
 %%
@@ -937,8 +936,12 @@ type_attribute_opt:
 
 type_attribute:			/* See C11 6.7 declaration-specifiers */
 	gcc_attribute_specifier
-|	T_ALIGNAS T_LPAREN type_specifier T_RPAREN	/* C11 6.7.5 */
-|	T_ALIGNAS T_LPAREN constant_expression T_RPAREN	/* C11 6.7.5 */
+|	T_ALIGNAS T_LPAREN type_specifier T_RPAREN {		/* C11 6.7.5 */
+		dcs_add_alignas(build_sizeof($3));
+	}
+|	T_ALIGNAS T_LPAREN constant_expression T_RPAREN {	/* C11 6.7.5 */
+		dcs_add_alignas($3);
+	}
 |	T_PACKED {
 		dcs_add_packed();
 	}
@@ -2197,18 +2200,18 @@ gcc_attribute:
 			suppress_fallthrough = true;
 	}
 |	T_NAME T_LPAREN T_RPAREN
-|	T_NAME T_LPAREN gcc_attribute_parameters T_RPAREN
+|	T_NAME T_LPAREN argument_expression_list T_RPAREN {
+		const char *name = $1->sb_name;
+		if (is_either(name, "aligned", "__aligned__")
+		    && $3->args_len == 1)
+			dcs_add_alignas($3->args[0]);
+	}
 |	type_qualifier {
 		if (!$1.tq_const)
 			yyerror("Bad attribute");
 	}
 ;
 
-gcc_attribute_parameters:
-	constant_expression
-|	gcc_attribute_parameters T_COMMA constant_expression
-;
-
 sys:
 	/* empty */ {
 		$$ = in_system_header;

Index: src/usr.bin/xlint/lint1/debug.c
diff -u src/usr.bin/xlint/lint1/debug.c:1.75 src/usr.bin/xlint/lint1/debug.c:1.76
--- src/usr.bin/xlint/lint1/debug.c:1.75	Sun Mar 31 20:28:45 2024
+++ src/usr.bin/xlint/lint1/debug.c	Wed May  1 07:40:11 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.75 2024/03/31 20:28:45 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.76 2024/05/01 07:40:11 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.75 2024/03/31 20:28:45 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.76 2024/05/01 07:40:11 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -461,7 +461,9 @@ debug_decl_level(const decl_level *dl)
 	if (dl->d_sou_size_in_bits != 0)
 		debug_printf(" size=%u", dl->d_sou_size_in_bits);
 	if (dl->d_sou_align_in_bits != 0)
-		debug_printf(" align=%u", dl->d_sou_align_in_bits);
+		debug_printf(" sou_align=%u", dl->d_sou_align_in_bits);
+	if (dl->d_mem_align_in_bits != 0)
+		debug_printf(" mem_align=%u", dl->d_mem_align_in_bits);
 
 	debug_word(dl->d_qual.tq_const, "const");
 	debug_word(dl->d_qual.tq_restrict, "restrict");

Index: src/usr.bin/xlint/lint1/decl.c
diff -u src/usr.bin/xlint/lint1/decl.c:1.398 src/usr.bin/xlint/lint1/decl.c:1.399
--- src/usr.bin/xlint/lint1/decl.c:1.398	Sat Mar 30 19:51:00 2024
+++ src/usr.bin/xlint/lint1/decl.c	Wed May  1 07:40:11 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: decl.c,v 1.398 2024/03/30 19:51:00 rillig Exp $ */
+/* $NetBSD: decl.c,v 1.399 2024/05/01 07:40:11 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: decl.c,v 1.398 2024/03/30 19:51:00 rillig Exp $");
+__RCSID("$NetBSD: decl.c,v 1.399 2024/05/01 07:40:11 rillig Exp $");
 #endif
 
 #include <sys/param.h>
@@ -460,6 +460,12 @@ pack_struct_or_union(type_t *tp)
 }
 
 void
+dcs_add_alignas(tnode_t *tn)
+{
+	dcs->d_mem_align_in_bits = to_int_constant(tn, true) * CHAR_SIZE;
+}
+
+void
 dcs_add_packed(void)
 {
 	if (dcs->d_type == NULL)
@@ -1038,7 +1044,9 @@ dcs_add_member(sym_t *mem)
 		    - mem->u.s_member.sm_offset_in_bits;
 		dcs->d_sou_size_in_bits += tp->t_bit_field_width;
 	} else {
-		dcs_align(alignment_in_bits(tp), 0);
+		unsigned int align_in_bits = dcs->d_mem_align_in_bits > 0
+		    ? dcs->d_mem_align_in_bits : alignment_in_bits(tp);
+		dcs_align(align_in_bits, 0);
 		mem->u.s_member.sm_offset_in_bits = dcs->d_sou_size_in_bits;
 		dcs->d_sou_size_in_bits += type_size_in_bits(tp);
 	}

Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.222 src/usr.bin/xlint/lint1/externs1.h:1.223
--- src/usr.bin/xlint/lint1/externs1.h:1.222	Fri Apr 12 05:44:38 2024
+++ src/usr.bin/xlint/lint1/externs1.h	Wed May  1 07:40:11 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: externs1.h,v 1.222 2024/04/12 05:44:38 rillig Exp $	*/
+/*	$NetBSD: externs1.h,v 1.223 2024/05/01 07:40:11 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -215,6 +215,7 @@ void dcs_add_function_specifier(function
 void dcs_add_storage_class(scl_t);
 void dcs_add_type(type_t *);
 void dcs_add_qualifiers(type_qualifiers);
+void dcs_add_alignas(tnode_t *);
 void dcs_add_packed(void);
 void dcs_set_used(void);
 void begin_declaration_level(decl_level_kind);
Index: src/usr.bin/xlint/lint1/lint1.h
diff -u src/usr.bin/xlint/lint1/lint1.h:1.222 src/usr.bin/xlint/lint1/lint1.h:1.223
--- src/usr.bin/xlint/lint1/lint1.h:1.222	Sun Mar 31 20:28:45 2024
+++ src/usr.bin/xlint/lint1/lint1.h	Wed May  1 07:40:11 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.222 2024/03/31 20:28:45 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.223 2024/05/01 07:40:11 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -342,6 +342,8 @@ typedef struct decl_level {
 						 * trailing padding */
 	unsigned int d_sou_align_in_bits;	/* alignment of the structure
 						 * or union being built */
+	unsigned int d_mem_align_in_bits;	/* alignment of the structure
+						 * or union member */
 	type_qualifiers d_qual;	/* in declaration specifiers */
 	bool	d_inline:1;	/* inline in declaration specifiers */
 	bool	d_multiple_storage_classes:1; /* reported in dcs_end_type */

Reply via email to