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