Sun Jul 11 20:37:21 2021 UTC ()
lint: make _Generic a primary-expression

C11 says so, and unless the _Generic expression was wrapped in
parentheses, it was not possible before to use it as a function call
expression.


(rillig)
diff -r1.7 -r1.8 src/tests/usr.bin/xlint/lint1/c11_generic_expression.c
diff -r1.6 -r1.7 src/tests/usr.bin/xlint/lint1/c11_generic_expression.exp
diff -r1.316 -r1.317 src/usr.bin/xlint/lint1/cgram.y

cvs diff -r1.7 -r1.8 src/tests/usr.bin/xlint/lint1/c11_generic_expression.c (expand / switch to unified diff)

--- src/tests/usr.bin/xlint/lint1/c11_generic_expression.c 2021/07/11 20:34:05 1.7
+++ src/tests/usr.bin/xlint/lint1/c11_generic_expression.c 2021/07/11 20:37:21 1.8
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: c11_generic_expression.c,v 1.7 2021/07/11 20:34:05 rillig Exp $ */ 1/* $NetBSD: c11_generic_expression.c,v 1.8 2021/07/11 20:37:21 rillig Exp $ */
2# 3 "c11_generic_expression.c" 2# 3 "c11_generic_expression.c"
3 3
4/* 4/*
5 * C99 added support for type-generic macros, but these were limited to the 5 * C99 added support for type-generic macros, but these were limited to the
6 * header <tgmath.h>. C11 made this feature generally available. 6 * header <tgmath.h>. C11 made this feature generally available.
7 * 7 *
8 * The generic selection is typically used with macros, but since lint1 works 8 * The generic selection is typically used with macros, but since lint1 works
9 * on the preprocessed source, the test cases look a bit strange. 9 * on the preprocessed source, the test cases look a bit strange.
10 * 10 *
11 * C99 6.5.1.1 "Generic selection" 11 * C99 6.5.1.1 "Generic selection"
12 */ 12 */
13 13
14/* lint1-extra-flags: -Ac11 */ 14/* lint1-extra-flags: -Ac11 */
@@ -82,17 +82,15 @@ comma_expression(char first, double seco @@ -82,17 +82,15 @@ comma_expression(char first, double seco
82 */ 82 */
83/* ARGSUSED */ 83/* ARGSUSED */
84int 84int
85assignment_expression(int first, int second) 85assignment_expression(int first, int second)
86{ 86{
87 return _Generic(first = second, 87 return _Generic(first = second,
88 int: second = first 88 int: second = first
89 ); 89 );
90} 90}
91 91
92int 92int
93primary_expression(void) 93primary_expression(void)
94{ 94{
95 /*FIXME*//* expect+1: syntax error '(' [249] */ 
96 return _Generic(0, int: assignment_expression)(0, 0); 95 return _Generic(0, int: assignment_expression)(0, 0);
97} 96}
98/* expect-1: falls off */ 

cvs diff -r1.6 -r1.7 src/tests/usr.bin/xlint/lint1/Attic/c11_generic_expression.exp (expand / switch to unified diff)

--- src/tests/usr.bin/xlint/lint1/Attic/c11_generic_expression.exp 2021/07/11 20:34:05 1.6
+++ src/tests/usr.bin/xlint/lint1/Attic/c11_generic_expression.exp 2021/07/11 20:37:21 1.7
@@ -1,8 +1,6 @@ @@ -1,8 +1,6 @@
1c11_generic_expression.c(29): warning: function classify_type_without_default expects to return value [214] 1c11_generic_expression.c(29): warning: function classify_type_without_default expects to return value [214]
2c11_generic_expression.c(21): warning: argument 'var' unused in function 'classify_type_without_default' [231] 2c11_generic_expression.c(21): warning: argument 'var' unused in function 'classify_type_without_default' [231]
3c11_generic_expression.c(37): warning: argument 'var' unused in function 'classify_type_with_default' [231] 3c11_generic_expression.c(37): warning: argument 'var' unused in function 'classify_type_with_default' [231]
4c11_generic_expression.c(53): warning: argument 'c' unused in function 'classify_char' [231] 4c11_generic_expression.c(53): warning: argument 'c' unused in function 'classify_char' [231]
5c11_generic_expression.c(72): error: syntax error 'second' [249] 5c11_generic_expression.c(72): error: syntax error 'second' [249]
6c11_generic_expression.c(77): warning: function comma_expression falls off bottom without returning value [217] 6c11_generic_expression.c(77): warning: function comma_expression falls off bottom without returning value [217]
7c11_generic_expression.c(96): error: syntax error '(' [249] 
8c11_generic_expression.c(97): warning: function primary_expression falls off bottom without returning value [217] 

cvs diff -r1.316 -r1.317 src/usr.bin/xlint/lint1/cgram.y (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/cgram.y 2021/07/11 20:25:54 1.316
+++ src/usr.bin/xlint/lint1/cgram.y 2021/07/11 20:37:21 1.317
@@ -1,15 +1,15 @@ @@ -1,15 +1,15 @@
1%{ 1%{
2/* $NetBSD: cgram.y,v 1.316 2021/07/11 20:25:54 rillig Exp $ */ 2/* $NetBSD: cgram.y,v 1.317 2021/07/11 20:37:21 rillig Exp $ */
3 3
4/* 4/*
5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
6 * Copyright (c) 1994, 1995 Jochen Pohl 6 * Copyright (c) 1994, 1995 Jochen Pohl
7 * All Rights Reserved. 7 * All Rights Reserved.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the 15 * notice, this list of conditions and the following disclaimer in the
@@ -25,27 +25,27 @@ @@ -25,27 +25,27 @@
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */ 34 */
35 35
36#include <sys/cdefs.h> 36#include <sys/cdefs.h>
37#if defined(__RCSID) && !defined(lint) 37#if defined(__RCSID) && !defined(lint)
38__RCSID("$NetBSD: cgram.y,v 1.316 2021/07/11 20:25:54 rillig Exp $"); 38__RCSID("$NetBSD: cgram.y,v 1.317 2021/07/11 20:37:21 rillig Exp $");
39#endif 39#endif
40 40
41#include <limits.h> 41#include <limits.h>
42#include <stdlib.h> 42#include <stdlib.h>
43#include <string.h> 43#include <string.h>
44 44
45#include "lint1.h" 45#include "lint1.h"
46 46
47extern char *yytext; 47extern char *yytext;
48 48
49/* 49/*
50 * Contains the level of current declaration, used for symbol table entries. 50 * Contains the level of current declaration, used for symbol table entries.
51 * 0 is the top-level, > 0 is inside a function body. 51 * 0 is the top-level, > 0 is inside a function body.
@@ -1712,51 +1712,51 @@ expr: /* C99 6.5 */ @@ -1712,51 +1712,51 @@ expr: /* C99 6.5 */
1712 | expr T_QUEST expr T_COLON expr { 1712 | expr T_QUEST expr T_COLON expr {
1713 $$ = build(QUEST, $1, build(COLON, $3, $5)); 1713 $$ = build(QUEST, $1, build(COLON, $3, $5));
1714 } 1714 }
1715 | expr T_ASSIGN expr { 1715 | expr T_ASSIGN expr {
1716 $$ = build(ASSIGN, $1, $3); 1716 $$ = build(ASSIGN, $1, $3);
1717 } 1717 }
1718 | expr T_OPASSIGN expr { 1718 | expr T_OPASSIGN expr {
1719 $$ = build($2, $1, $3); 1719 $$ = build($2, $1, $3);
1720 } 1720 }
1721 | expr T_COMMA expr { 1721 | expr T_COMMA expr {
1722 $$ = build(COMMA, $1, $3); 1722 $$ = build(COMMA, $1, $3);
1723 } 1723 }
1724 | cast_expression 1724 | cast_expression
1725 | generic_selection /* TODO: move to primary_expression */ 
1726 ; 1725 ;
1727 1726
1728assignment_expression: /* C99 6.5.16 */ 1727assignment_expression: /* C99 6.5.16 */
1729 expr %prec T_ASSIGN 1728 expr %prec T_ASSIGN
1730 ; 1729 ;
1731 1730
1732primary_expression: /* C99 6.5.1 */ 1731primary_expression: /* C99 6.5.1 */
1733 T_NAME { 1732 T_NAME {
1734 /* XXX really necessary? */ 1733 /* XXX really necessary? */
1735 if (yychar < 0) 1734 if (yychar < 0)
1736 yychar = yylex(); 1735 yychar = yylex();
1737 $$ = new_name_node(getsym($1), yychar); 1736 $$ = new_name_node(getsym($1), yychar);
1738 } 1737 }
1739 | T_CON { 1738 | T_CON {
1740 $$ = expr_new_constant(gettyp($1->v_tspec), $1); 1739 $$ = expr_new_constant(gettyp($1->v_tspec), $1);
1741 } 1740 }
1742 | string { 1741 | string {
1743 $$ = new_string_node($1); 1742 $$ = new_string_node($1);
1744 } 1743 }
1745 | T_LPAREN expr T_RPAREN { 1744 | T_LPAREN expr T_RPAREN {
1746 if ($2 != NULL) 1745 if ($2 != NULL)
1747 $2->tn_parenthesized = true; 1746 $2->tn_parenthesized = true;
1748 $$ = $2; 1747 $$ = $2;
1749 } 1748 }
 1749 | generic_selection
1750 /* GCC primary-expression, see c_parser_postfix_expression */ 1750 /* GCC primary-expression, see c_parser_postfix_expression */
1751 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA identifier T_RPAREN { 1751 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA identifier T_RPAREN {
1752 symtyp = FMEMBER; 1752 symtyp = FMEMBER;
1753 $$ = build_offsetof($3, getsym($5)); 1753 $$ = build_offsetof($3, getsym($5));
1754 } 1754 }
1755 ; 1755 ;
1756 1756
1757postfix_expression: /* C99 6.5.2 */ 1757postfix_expression: /* C99 6.5.2 */
1758 primary_expression 1758 primary_expression
1759 | postfix_expression T_LBRACK expr T_RBRACK { 1759 | postfix_expression T_LBRACK expr T_RBRACK {
1760 $$ = build(INDIR, build(PLUS, $1, $3), NULL); 1760 $$ = build(INDIR, build(PLUS, $1, $3), NULL);
1761 } 1761 }
1762 | postfix_expression T_LPAREN T_RPAREN { 1762 | postfix_expression T_LPAREN T_RPAREN {