1 /* $NetBSD: msg_346.c,v 1.10 2024/08/31 06:57:31 rillig Exp $ */ 2 # 3 "msg_346.c" 3 4 // Test for message: call to '%s' effectively discards 'const' from argument [346] 5 6 /* lint1-extra-flags: -X 351 */ 7 8 typedef unsigned long size_t; 9 10 void *memchr(const void *, int, size_t); /* C99 7.21.5.1 */ 11 char *strchr(const char *, int); /* C99 7.21.5.2 */ 12 char *strpbrk(const char *, const char *); /* C99 7.21.5.4 */ 13 char *strrchr(const char *, int); /* C99 7.21.5.5 */ 14 char *strstr(const char *, const char *); /* C99 7.21.5.7 */ 15 16 void take_const_char_ptr(const char *); 17 void take_char_ptr(char *); 18 19 void 20 example(void) 21 { 22 const char *ccp = "const char *"; 23 char *cp = "char *"; 24 25 ccp = strchr(ccp, 'c'); 26 ccp = strchr(cp, 'c'); 27 /* expect+1: warning: call to 'strchr' effectively discards 'const' from argument [346] */ 28 cp = strchr(ccp, 'c'); 29 cp = strchr(cp, 'c'); 30 31 take_const_char_ptr(strchr(ccp, 'c')); 32 take_const_char_ptr(strchr(cp, 'c')); 33 /* expect+1: warning: call to 'strchr' effectively discards 'const' from argument [346] */ 34 take_char_ptr(strchr(ccp, 'c')); 35 take_char_ptr(strchr(cp, 'c')); 36 37 take_const_char_ptr(strchr("literal", 'c')); 38 /* expect+1: warning: call to 'strchr' effectively discards 'const' from argument [346] */ 39 take_char_ptr(strchr("literal", 'c')); 40 } 41 42 void 43 all_functions(void) 44 { 45 /* expect+1: warning: call to 'memchr' effectively discards 'const' from argument [346] */ 46 take_char_ptr(memchr("string", 'c', 7)); 47 /* expect+1: warning: call to 'strchr' effectively discards 'const' from argument [346] */ 48 take_char_ptr(strchr("string", 'c')); 49 /* expect+1: warning: call to 'strpbrk' effectively discards 'const' from argument [346] */ 50 take_char_ptr(strpbrk("string", "c")); 51 /* expect+1: warning: call to 'strrchr' effectively discards 'const' from argument [346] */ 52 take_char_ptr(strrchr("string", 'c')); 53 /* expect+1: warning: call to 'strstr' effectively discards 'const' from argument [346] */ 54 take_char_ptr(strstr("string", "c")); 55 } 56 57 void 58 edge_cases(void) 59 { 60 /* No arguments, to cover the 'an == NULL' in is_first_arg_const. */ 61 /* expect+1: error: argument mismatch: 0 arguments passed, 2 expected [150] */ 62 take_char_ptr(strchr()); 63 } 64 65 /* 66 * Bsearch is another standard function that effectively discards the 'const' 67 * modifier, but from the second argument, not the first. 68 */ 69 70 void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, 71 int (*compar) (const void *, const void *)); 72 73 int cmp(const void *, const void *); 74 75 void take_void_ptr(void *); 76 void take_const_void_ptr(const void *); 77 78 void 79 bsearch_example(void) 80 { 81 int arr[] = { 1 }; 82 const int const_arr[] = { 1 }; 83 84 take_const_void_ptr(bsearch("", const_arr, 4, 1, cmp)); 85 take_const_void_ptr(bsearch("", arr, 4, 1, cmp)); 86 take_void_ptr(bsearch("", arr, 4, 1, cmp)); 87 88 /* expect+1: warning: call to 'bsearch' effectively discards 'const' from argument [346] */ 89 take_void_ptr(bsearch("", const_arr, 4, 1, cmp)); 90 } 91