Home | History | Annotate | Line # | Download | only in lint1
msg_348.c revision 1.4
      1 /*	$NetBSD: msg_348.c,v 1.4 2021/11/01 18:11:26 rillig Exp $	*/
      2 # 3 "msg_348.c"
      3 
      4 // Test for message 348: maximum value %d of '%s' does not match maximum array index %d [348]
      5 
      6 /* lint1-extra-flags: -r */
      7 
      8 enum color {
      9 	red,
     10 	green,
     11 	/* expect+2: previous declaration of blue [260] */
     12 	/* expect+1: previous declaration of blue [260] */
     13 	blue
     14 };
     15 
     16 const char *
     17 color_name(enum color color)
     18 {
     19 	static const char *name[] = {
     20 	    "red",
     21 	    "green",
     22 	    "blue"
     23 	};
     24 	/* No warning since the maximum enum value matches the array size. */
     25 	return name[color];
     26 }
     27 
     28 const char *
     29 color_name_too_few(enum color color)
     30 {
     31 	static const char *name[] = {
     32 	    "red",
     33 	    "green"
     34 	};
     35 	/* expect+1: warning: maximum value 2 of 'enum color' does not match maximum array index 1 [348] */
     36 	return name[color];
     37 }
     38 
     39 const char *
     40 color_name_too_many(enum color color)
     41 {
     42 	static const char *name[] = {
     43 	    "red",
     44 	    "green",
     45 	    "blue",
     46 	    "black"
     47 	};
     48 	/* expect+1: warning: maximum value 2 of 'enum color' does not match maximum array index 3 [348] */
     49 	return name[color];
     50 }
     51 
     52 const char *
     53 color_name_computed_index(enum color color)
     54 {
     55 	static const char *name[] = {
     56 	    "unused",
     57 	    "red",
     58 	    "green",
     59 	    "blue"
     60 	};
     61 	/* No warning since the array index is not a name. */
     62 	return name[color + 1];
     63 }
     64 
     65 const char *
     66 color_name_cast_from_int(int c)
     67 {
     68 	static const char *name[] = {
     69 	    "unused",
     70 	    "red",
     71 	    "green",
     72 	    "blue"
     73 	};
     74 	/* No warning since the array index before conversion is not a name. */
     75 	return name[(enum color)(c + 1)];
     76 }
     77 
     78 const char *
     79 color_name_explicit_cast_to_int(enum color color)
     80 {
     81 	static const char *name[] = {
     82 	    "red",
     83 	    "green",
     84 	};
     85 	/* No warning due to the explicit cast. */
     86 	return name[(int)color];
     87 }
     88 
     89 const char *
     90 color_name_computed_pointer(enum color color, const char *name)
     91 {
     92 	/* No warning since 'name' is not an array. */
     93 	return (&name)[color];
     94 }
     95 
     96 extern const char *incomplete_color_name[];
     97 
     98 const char *
     99 color_name_incomplete_array(enum color color)
    100 {
    101 	/* No warning since 'incomplete_color_name' is incomplete. */
    102 	return incomplete_color_name[color];
    103 }
    104 
    105 enum large {
    106 	/* expect+1: warning: integral constant too large [56] */
    107 	min = -1LL << 40,
    108 	/* expect+1: warning: integral constant too large [56] */
    109 	max = 1LL << 40,
    110 	zero = 0
    111 };
    112 
    113 const char *
    114 large_name(enum large large)
    115 {
    116 	static const char *name[] = {
    117 	    "dummy",
    118 	};
    119 	/* No warning since at least 1 enum constant is outside of INT. */
    120 	return name[large];
    121 }
    122 
    123 enum color_with_count {
    124 	cc_red,
    125 	cc_green,
    126 	cc_blue,
    127 	cc_num_values
    128 };
    129 
    130 const char *
    131 color_with_count_name(enum color_with_count color)
    132 {
    133 	static const char *const name[] = { "red", "green", "blue" };
    134 	/* No warning since the maximum enum constant is a count. */
    135 	return name[color];
    136 }
    137 
    138 /*
    139  * If the last enum constant contains "num" in its name, it is not
    140  * necessarily the count of the other enum values, it may also be a
    141  * legitimate application value, therefore don't warn in this case.
    142  */
    143 const char *
    144 color_with_num(enum color_with_count color)
    145 {
    146 	static const char *const name[] = { "r", "g", "b", "num" };
    147 	/* No warning since the maximum values already match. */
    148 	return name[color];
    149 }
    150 
    151 enum color_with_uc_count {
    152 	CC_RED,
    153 	CC_GREEN,
    154 	CC_BLUE,
    155 	CC_NUM_VALUES
    156 };
    157 
    158 const char *
    159 color_with_uc_count_name(enum color_with_uc_count color)
    160 {
    161 	static const char *const name[] = { "red", "green", "blue" };
    162 	/* No warning since the maximum enum constant is a count. */
    163 	return name[color];
    164 }
    165