1 1.4 rillig /* $NetBSD: msg_166.c,v 1.4 2022/06/16 16:58:36 rillig Exp $ */ 2 1.1 rillig # 3 "msg_166.c" 3 1.1 rillig 4 1.1 rillig // Test for message: precision lost in bit-field assignment [166] 5 1.1 rillig 6 1.2 rillig /* lint1-extra-flags: -hp */ 7 1.2 rillig 8 1.2 rillig struct bit_set { 9 1.2 rillig 10 1.2 rillig /* 11 1.2 rillig * C99 6.7.2p5 and 6.7.2.1p9 footnote 104 say that for bit-fields of 12 1.2 rillig * underlying type 'int', "it is implementation-defined whether the 13 1.2 rillig * specifier 'int' designates the same type as 'signed int' or the 14 1.2 rillig * same type as 'unsigned int'". 15 1.2 rillig * 16 1.2 rillig * https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations 17 1.2 rillig * -and-bit-fields-implementation.html says: "By default it is treated 18 1.2 rillig * as 'signed int' but this may be changed by the 19 1.2 rillig * '-funsigned-bitfields' option". 20 1.2 rillig * 21 1.2 rillig * Clang doesn't document implementation-defined behavior, see 22 1.2 rillig * https://bugs.llvm.org/show_bug.cgi?id=11272. 23 1.2 rillig */ 24 1.2 rillig 25 1.4 rillig /* expect+1: warning: bit-field of type plain 'int' has implementation-defined signedness [344] */ 26 1.4 rillig int minus_1_to_0: 1; 27 1.4 rillig /* expect+1: warning: bit-field of type plain 'int' has implementation-defined signedness [344] */ 28 1.4 rillig int minus_8_to_7: 4; 29 1.2 rillig unsigned zero_to_1: 1; 30 1.2 rillig unsigned zero_to_15: 4; 31 1.2 rillig }; 32 1.2 rillig 33 1.2 rillig void example(void) { 34 1.2 rillig struct bit_set bits; 35 1.2 rillig 36 1.2 rillig /* Clang doesn't warn about the 1. */ 37 1.4 rillig /* expect+1: warning: precision lost in bit-field assignment [166] */ 38 1.4 rillig bits.minus_1_to_0 = -2; 39 1.2 rillig bits.minus_1_to_0 = -1; 40 1.2 rillig bits.minus_1_to_0 = 0; 41 1.4 rillig /* expect+1: warning: precision lost in bit-field assignment [166] */ 42 1.4 rillig bits.minus_1_to_0 = 1; 43 1.4 rillig /* expect+1: warning: precision lost in bit-field assignment [166] */ 44 1.4 rillig bits.minus_1_to_0 = 2; 45 1.2 rillig 46 1.4 rillig /* expect+1: warning: precision lost in bit-field assignment [166] */ 47 1.4 rillig bits.minus_8_to_7 = -9; 48 1.2 rillig bits.minus_8_to_7 = -8; 49 1.2 rillig bits.minus_8_to_7 = 7; 50 1.4 rillig /* expect+1: warning: precision lost in bit-field assignment [166] */ 51 1.4 rillig bits.minus_8_to_7 = 8; 52 1.2 rillig 53 1.2 rillig /* Clang doesn't warn about the -1. */ 54 1.4 rillig /* expect+1: warning: assignment of negative constant to unsigned type [164] */ 55 1.4 rillig bits.zero_to_1 = -2; 56 1.4 rillig /* expect+1: warning: assignment of negative constant to unsigned type [164] */ 57 1.4 rillig bits.zero_to_1 = -1; 58 1.2 rillig bits.zero_to_1 = 0; 59 1.2 rillig bits.zero_to_1 = 1; 60 1.4 rillig /* expect+1: warning: precision lost in bit-field assignment [166] */ 61 1.4 rillig bits.zero_to_1 = 2; 62 1.2 rillig 63 1.2 rillig /* Clang doesn't warn about the -8. */ 64 1.4 rillig /* expect+1: warning: assignment of negative constant to unsigned type [164] */ 65 1.4 rillig bits.zero_to_15 = -9; 66 1.4 rillig /* expect+1: warning: assignment of negative constant to unsigned type [164] */ 67 1.4 rillig bits.zero_to_15 = -8; 68 1.2 rillig bits.zero_to_15 = 0; 69 1.2 rillig bits.zero_to_15 = 15; 70 1.4 rillig /* expect+1: warning: precision lost in bit-field assignment [166] */ 71 1.4 rillig bits.zero_to_15 = 16; 72 1.2 rillig } 73