Module Name: src Committed By: rillig Date: Sat Jan 21 13:48:40 UTC 2023
Modified Files: src/tests/usr.bin/xlint/lint1: c11_atomic.c c99_atomic.c src/usr.bin/xlint/lint1: cgram.y lex.c Log Message: lint: add support for C11 '_Atomic' as atomic-type-specifier Following the C11 grammar, the keyword '_Atomic' needs to be a separate syntactic category, to avoid further conflicts in the grammar. The two newly added conflicts in the grammar would come into play when mixing traditional C with C11, in a type name without an implicit 'int'. If the type '_Atomic(int)*' were parsed as '_Atomic int(int)*', the trailing '*' would be a syntax error. To generate a diff of this commit: cvs rdiff -u -r1.2 -r1.3 src/tests/usr.bin/xlint/lint1/c11_atomic.c \ src/tests/usr.bin/xlint/lint1/c99_atomic.c cvs rdiff -u -r1.430 -r1.431 src/usr.bin/xlint/lint1/cgram.y cvs rdiff -u -r1.141 -r1.142 src/usr.bin/xlint/lint1/lex.c 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/c11_atomic.c diff -u src/tests/usr.bin/xlint/lint1/c11_atomic.c:1.2 src/tests/usr.bin/xlint/lint1/c11_atomic.c:1.3 --- src/tests/usr.bin/xlint/lint1/c11_atomic.c:1.2 Sat Jan 21 13:07:22 2023 +++ src/tests/usr.bin/xlint/lint1/c11_atomic.c Sat Jan 21 13:48:40 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: c11_atomic.c,v 1.2 2023/01/21 13:07:22 rillig Exp $ */ +/* $NetBSD: c11_atomic.c,v 1.3 2023/01/21 13:48:40 rillig Exp $ */ # 3 "c11_atomic.c" /* @@ -19,4 +19,11 @@ typedef _Atomic struct { int field; } atomic_struct; -/* TODO: C11 6.7.2.4 "Atomic type specifiers" */ +/* C11 6.7.2.4 "Atomic type specifiers" */ +double * +atomic_ptr_cmpexch(_Atomic(double *)*ptr_var, _Atomic(double *) new_value) +{ + double *old = *ptr_var; + *ptr_var = new_value; + return old; +} Index: src/tests/usr.bin/xlint/lint1/c99_atomic.c diff -u src/tests/usr.bin/xlint/lint1/c99_atomic.c:1.2 src/tests/usr.bin/xlint/lint1/c99_atomic.c:1.3 --- src/tests/usr.bin/xlint/lint1/c99_atomic.c:1.2 Sat Jan 21 13:07:22 2023 +++ src/tests/usr.bin/xlint/lint1/c99_atomic.c Sat Jan 21 13:48:40 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: c99_atomic.c,v 1.2 2023/01/21 13:07:22 rillig Exp $ */ +/* $NetBSD: c99_atomic.c,v 1.3 2023/01/21 13:48:40 rillig Exp $ */ # 3 "c99_atomic.c" /* @@ -13,3 +13,13 @@ typedef _Atomic int atomic_int; typedef _Atomic struct { int field; } atomic_struct; + +/* expect+3: error: '_Atomic' requires C11 or later [350] */ +/* expect+2: error: '_Atomic' requires C11 or later [350] */ +double * +atomic_ptr_cmpexch(_Atomic(double *)*ptr_var, _Atomic(double *) new_value) +{ + double *old = *ptr_var; + *ptr_var = new_value; + return old; +} Index: src/usr.bin/xlint/lint1/cgram.y diff -u src/usr.bin/xlint/lint1/cgram.y:1.430 src/usr.bin/xlint/lint1/cgram.y:1.431 --- src/usr.bin/xlint/lint1/cgram.y:1.430 Sat Jan 21 13:07:22 2023 +++ src/usr.bin/xlint/lint1/cgram.y Sat Jan 21 13:48:40 2023 @@ -1,5 +1,5 @@ %{ -/* $NetBSD: cgram.y,v 1.430 2023/01/21 13:07:22 rillig Exp $ */ +/* $NetBSD: cgram.y,v 1.431 2023/01/21 13:48:40 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.430 2023/01/21 13:07:22 rillig Exp $"); +__RCSID("$NetBSD: cgram.y,v 1.431 2023/01/21 13:48:40 rillig Exp $"); #endif #include <limits.h> @@ -143,7 +143,7 @@ is_either(const char *s, const char *a, %} -%expect 129 +%expect 131 %union { val_t *y_val; @@ -206,8 +206,9 @@ is_either(const char *s, const char *a, */ %token <y_tspec> T_TYPE -/* qualifiers (const, volatile, restrict, _Thread_local, _Atomic) */ +/* qualifiers (const, volatile, restrict, _Thread_local) */ %token <y_tqual> T_QUAL +%token <y_tqual> T_ATOMIC /* struct or union */ %token <y_tspec> T_STRUCT_OR_UNION @@ -276,6 +277,7 @@ is_either(const char *s, const char *a, %type <y_type> begin_type_typespec %type <y_type> type_specifier %type <y_type> notype_type_specifier +%type <y_type> atomic_type_specifier %type <y_type> struct_or_union_specifier %type <y_tspec> struct_or_union %type <y_sym> braced_struct_declaration_list @@ -854,6 +856,7 @@ notype_type_specifier: /* see C99 6.7.2 $$ = $3 != NULL ? block_dup_type($3->tn_type) : gettyp(INT); $$->t_typeof = true; } + | atomic_type_specifier | struct_or_union_specifier { end_declaration_level(); $$ = $1; @@ -864,6 +867,13 @@ notype_type_specifier: /* see C99 6.7.2 } ; +/* K&R ---, C90 ---, C99 ---, C11 6.7.2.4 */ +atomic_type_specifier: + atomic T_LPAREN type_name T_RPAREN { + $$ = $3; + } + ; + struct_or_union_specifier: /* C99 6.7.2.1 */ struct_or_union identifier_sym { /* @@ -1099,12 +1109,18 @@ enumerator: /* C99 6.7.2.2 */ ; type_qualifier: /* C99 6.7.3 */ - T_QUAL { + T_QUAL + | atomic { + $$ = ATOMIC; + } + ; + +atomic: /* helper */ + T_ATOMIC { /* TODO: First fix c11ism, then use it here. */ - if ($1 == ATOMIC && !allow_c11) + if (!allow_c11) /* '_Atomic' requires C11 or later */ error(350); - $$ = $1; } ; Index: src/usr.bin/xlint/lint1/lex.c diff -u src/usr.bin/xlint/lint1/lex.c:1.141 src/usr.bin/xlint/lint1/lex.c:1.142 --- src/usr.bin/xlint/lint1/lex.c:1.141 Sat Jan 21 13:07:22 2023 +++ src/usr.bin/xlint/lint1/lex.c Sat Jan 21 13:48:40 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: lex.c,v 1.141 2023/01/21 13:07:22 rillig Exp $ */ +/* $NetBSD: lex.c,v 1.142 2023/01/21 13:48:40 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.141 2023/01/21 13:07:22 rillig Exp $"); +__RCSID("$NetBSD: lex.c,v 1.142 2023/01/21 13:48:40 rillig Exp $"); #endif #include <ctype.h> @@ -112,7 +112,7 @@ static const struct keyword { kwdef_keyword( "_Alignas", T_ALIGNAS), kwdef_keyword( "_Alignof", T_ALIGNOF), kwdef_token( "alignof", T_ALIGNOF, 78,0,6), - kwdef_tqual( "_Atomic", ATOMIC, 11,0,1), + kwdef_token( "_Atomic", T_ATOMIC, 11,0,1), kwdef_token( "asm", T_ASM, 78,1,7), kwdef_token( "attribute", T_ATTRIBUTE, 78,1,6), kwdef_sclass( "auto", AUTO, 78,0,1),