From 18636637ee013ef828cb04b2b7bb4a4922324475 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 7 Oct 2010 03:31:55 +1030 Subject: [PATCH] modules: update documentation examples so they compile under ccanlint. This is everything up to the list module... now it's time for sleep. --- ccan/array_size/_info | 2 +- ccan/asort/_info | 6 +++--- ccan/block_pool/_info | 7 ++++-- ccan/btree/_info | 12 +++++------ ccan/build_assert/_info | 2 +- ccan/build_assert/build_assert.h | 4 +++- ccan/ccan_tokenizer/_info | 2 +- ccan/charset/_info | 2 +- ccan/compiler/_info | 6 +++--- ccan/compiler/compiler.h | 37 +++++++++++++++----------------- ccan/container_of/_info | 7 +++++- ccan/container_of/container_of.h | 17 ++++++--------- ccan/crc/_info | 4 ++-- ccan/crc/crc.h | 8 ++++--- ccan/crcsync/_info | 8 +++---- ccan/endian/_info | 1 + ccan/grab_file/grab_file.h | 7 ++++-- ccan/idtree/_info | 3 ++- ccan/idtree/idtree.h | 26 +++++++++------------- ccan/ilog/_info | 2 +- ccan/likely/likely.h | 4 ++-- 21 files changed, 86 insertions(+), 81 deletions(-) diff --git a/ccan/array_size/_info b/ccan/array_size/_info index cbe332ad..1722ba29 100644 --- a/ccan/array_size/_info +++ b/ccan/array_size/_info @@ -18,7 +18,7 @@ * // We currently use 32 random values. * static unsigned int vals[32]; * - * void init_values(void) + * static void init_values(void) * { * unsigned int i; * for (i = 0; i < ARRAY_SIZE(vals); i++) diff --git a/ccan/asort/_info b/ccan/asort/_info index e848ff1a..c5b2a0bb 100644 --- a/ccan/asort/_info +++ b/ccan/asort/_info @@ -19,12 +19,12 @@ * #include * #include * - * static int cmp(const char **a, const char **n, bool *casefold) + * static int cmp(char *const *a, char *const *n, bool *casefold) * { * if (*casefold) - * return strcasecmp(*a, *b); + * return strcasecmp(*a, *n); * else - * return strcmp(*a, *b); + * return strcmp(*a, *n); * } * * int main(int argc, char *argv[]) diff --git a/ccan/block_pool/_info b/ccan/block_pool/_info index 53afc7bd..245ed1a9 100644 --- a/ccan/block_pool/_info +++ b/ccan/block_pool/_info @@ -29,12 +29,15 @@ * int array[] = {0,1,1,2,3,5,8,13,21,34}; * int *array_copy = block_pool_memdup(bp, array, sizeof(array)); * + * memset(buffer, 0xff, 4096); + * printf("string = %s\n", string); + * printf("array_copy[0] == %i\n", array_copy[0]); * block_pool_free(bp); * return 0; * } * - * Author: Joey Adams - * License: BSD + * Author: Joey Adams + * License: BSD */ int main(int argc, char *argv[]) { diff --git a/ccan/btree/_info b/ccan/btree/_info index 46877229..5d72c490 100644 --- a/ccan/btree/_info +++ b/ccan/btree/_info @@ -37,7 +37,7 @@ * }; * * //Define the ordering function order_by_letter_set - * btree_search_implement + * static btree_search_implement * ( * order_by_letter_set, * struct word *, @@ -50,7 +50,7 @@ * char *chomp(char *str); * char *make_letter_set(char *str); * - * void insert_word(struct btree *btree, struct word *word) + * static void insert_word(struct btree *btree, struct word *word) * { * btree_iterator iter; * @@ -61,7 +61,7 @@ * btree_insert_at(iter, word); * } * - * void print_anagrams(struct btree *btree, char *line) + * static void print_anagrams(struct btree *btree, char *line) * { * btree_iterator iter, end; * struct word key = { @@ -86,7 +86,7 @@ * } * } * - * int destroy_word(struct word *word, void *ctx) + * static int destroy_word(struct word *word, void *ctx) * { * (void) ctx; * @@ -97,7 +97,7 @@ * return 1; * } * - * struct btree *read_dictionary(const char *filename) + * static struct btree *read_dictionary(const char *filename) * { * FILE *f; * char line[256]; @@ -129,7 +129,7 @@ * * return btree; * - * fail: + * fail: * btree_delete(btree); * fprintf(stderr, "%s: %s\n", filename, strerror(errno)); * return NULL; diff --git a/ccan/build_assert/_info b/ccan/build_assert/_info index cf939d79..16a4a94c 100644 --- a/ccan/build_assert/_info +++ b/ccan/build_assert/_info @@ -26,7 +26,7 @@ * int x; * }; * - * char *foo_string(struct foo *foo) + * static char *foo_string(struct foo *foo) * { * // This trick requires that the string be first in the structure * BUILD_ASSERT(offsetof(struct foo, string) == 0); diff --git a/ccan/build_assert/build_assert.h b/ccan/build_assert/build_assert.h index 4b0d75e4..c6ee362f 100644 --- a/ccan/build_assert/build_assert.h +++ b/ccan/build_assert/build_assert.h @@ -9,7 +9,9 @@ * by the compiler. This can only be used within a function. * * Example: - * char *foo_to_char(struct foo *foo) + * #include + * ... + * static char *foo_to_char(struct foo *foo) * { * // This code needs string to be at start of foo. * BUILD_ASSERT(offsetof(struct foo, string) == 0); diff --git a/ccan/ccan_tokenizer/_info b/ccan/ccan_tokenizer/_info index 8c3c9df2..754b3cf6 100644 --- a/ccan/ccan_tokenizer/_info +++ b/ccan/ccan_tokenizer/_info @@ -14,7 +14,7 @@ * #include * #include * - * void token_list_stats(const struct token_list *tl) { + * static void token_list_stats(const struct token_list *tl) { * size_t comment=0, white=0, stray=0, code=0, total=0; * size_t count = 0; * const struct token *i; diff --git a/ccan/charset/_info b/ccan/charset/_info index 4319ecb3..a7086ba1 100644 --- a/ccan/charset/_info +++ b/ccan/charset/_info @@ -32,7 +32,7 @@ * if (!file) * err(1, "Could not read file %s", argv[1]); * - * valid = utf8_validate(file, len)); + * valid = utf8_validate(file, len); * printf("File contents are %s UTF-8\n", valid ? "valid" : "invalid"); * * talloc_free(file); diff --git a/ccan/compiler/_info b/ccan/compiler/_info index ea4fe702..06645b1d 100644 --- a/ccan/compiler/_info +++ b/ccan/compiler/_info @@ -6,7 +6,7 @@ * compiler - macros for common compiler extensions * * Abstracts away some compiler hints. Currently these include: - * - UNLIKELY_FUNCTION_ATTRIBUTE + * - COLD_ATTRIBUTE * For functions not called in fast paths (aka. cold functions) * - PRINTF_ATTRIBUTE * For functions which take printf-style parameters. @@ -23,13 +23,13 @@ * Author: Rusty Russell * * Example: - * #include + * #include * #include * #include * * // Example of a (slow-path) logging function. * static int log_threshold = 2; - * static void UNLIKELY_FUNCTION_ATTRIBUTE PRINTF_ATTRIBUTE(2,3) + * static void COLD_ATTRIBUTE PRINTF_ATTRIBUTE(2,3) * logger(int level, const char *fmt, ...) * { * va_list ap; diff --git a/ccan/compiler/compiler.h b/ccan/compiler/compiler.h index be229670..242ef84d 100644 --- a/ccan/compiler/compiler.h +++ b/ccan/compiler/compiler.h @@ -10,10 +10,10 @@ * It is usually used on logging or error routines. * * Example: - * void COLD_ATTRIBUTE moan(const char *reason) - * { - * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno)); - * } + * static void COLD_ATTRIBUTE moan(const char *reason) + * { + * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno)); + * } */ #define COLD_ATTRIBUTE __attribute__((cold)) #else @@ -23,13 +23,14 @@ #if HAVE_ATTRIBUTE_PRINTF /** * PRINTF_ATTRIBUTE - a function takes printf-style arguments - * nfmt: the 1-based number of the function's format argument. - * narg: the 1-based number of the function's first variable argument. + * @nfmt: the 1-based number of the function's format argument. + * @narg: the 1-based number of the function's first variable argument. * * This allows the compiler to check your parameters as it does for printf(). * * Example: - * void PRINTF_ATTRIBUTE(2,3) my_printf(char *prefix, char *format, ...); + * void PRINTF_ATTRIBUTE(2,3) my_printf(const char *prefix, + * const char *fmt, ...); */ #define PRINTF_ATTRIBUTE(nfmt, narg) \ __attribute__((format(__printf__, nfmt, narg))) @@ -58,18 +59,14 @@ * the compiler that if it is unused it need not emit it into the source code. * * Example: - * // With some config options, this is unnecessary. - * static UNNEEDED_ATTRIBUTE int counter; - * ... - * #ifdef DEBUG - * counter++; - * #endif - * ... - * // With some config options, this is unnecessary. - * static UNNEEDED_ATTRIBUTE int add_to_counter(int add) - * { - * counter += add; - * } + * // With some preprocessor options, this is unnecessary. + * static UNNEEDED_ATTRIBUTE int counter; + * + * // With some preprocessor options, this is unnecessary. + * static UNNEEDED_ATTRIBUTE void add_to_counter(int add) + * { + * counter += add; + * } */ #define UNNEEDED_ATTRIBUTE __attribute__((unused)) @@ -117,7 +114,7 @@ * const char *greek_name(enum greek greek); * * // Inline version. - * static inline _greek_name(enum greek greek) + * static inline char *_greek_name(enum greek greek) * { * switch (greek) { * case ALPHA: return "alpha"; diff --git a/ccan/container_of/_info b/ccan/container_of/_info index 2b9da338..8580526a 100644 --- a/ccan/container_of/_info +++ b/ccan/container_of/_info @@ -26,13 +26,18 @@ * struct timer timer; * }; * + * static void register_timer(struct timer *timer) + * { + * //... + * } + * * static void my_timer_callback(struct timer *timer) * { * struct info *info = container_of(timer, struct info, timer); * printf("my_stuff is %u\n", info->my_stuff); * } * - * int main() + * int main(void) * { * struct info info = { .my_stuff = 1 }; * diff --git a/ccan/container_of/container_of.h b/ccan/container_of/container_of.h index ec66f106..de3f4505 100644 --- a/ccan/container_of/container_of.h +++ b/ccan/container_of/container_of.h @@ -15,13 +15,16 @@ * subtraction to return the pointer to the enclosing type. * * Example: - * struct info - * { + * struct foo { + * int fielda, fieldb; + * // ... + * }; + * struct info { * int some_other_field; * struct foo my_foo; * }; * - * struct info *foo_to_info(struct foo *foop) + * static struct info *foo_to_info(struct foo *foo) * { * return container_of(foo, struct info, my_foo); * } @@ -42,13 +45,7 @@ * subtraction to return the pointer to the enclosing type. * * Example: - * struct info - * { - * int some_other_field; - * struct foo my_foo; - * }; - * - * struct info *foo_to_info(struct foo *foop) + * static struct info *foo_to_i(struct foo *foo) * { * struct info *i = container_of_var(foo, i, my_foo); * return i; diff --git a/ccan/crc/_info b/ccan/crc/_info index c505a2b6..7812f212 100644 --- a/ccan/crc/_info +++ b/ccan/crc/_info @@ -11,7 +11,7 @@ * detect a single error burst of up to 32 bits. * * Example: - * #include + * #include * #include * #include * @@ -22,7 +22,7 @@ * "Prints 32 bit CRC of the string\n", argv[0]); * exit(1); * } - * printf("0x%08x\n", crc32c(argv[1], strlen(argv[1]))); + * printf("0x%08x\n", crc32c(0, argv[1], strlen(argv[1]))); * exit(0); * } * diff --git a/ccan/crc/crc.h b/ccan/crc/crc.h index d3184cb1..6338000e 100644 --- a/ccan/crc/crc.h +++ b/ccan/crc/crc.h @@ -29,11 +29,13 @@ * as 0 the first time, and the current crc result from then on. * * Example: + * #include + * ... * // Check that iovec has the crc we expect (Castagnoli version) - * bool check_crc(uint32_t expected, const struct iovec *iov, int iovcnt) + * static bool check_crc(uint32_t expected, const struct iovec *iov, int l) * { * uint32_t crc = 0; - * while (iovcnt >= 0) { + * while (l >= 0) { * crc = crc32c(crc, iov->iov_base, iov->iov_len); * iov++; * } @@ -52,7 +54,7 @@ uint32_t crc32c(uint32_t start_crc, const void *buf, size_t size); * * Example: * // This dumb code only handles Castagnoli, so assert that here. - * void check_user_crc_table(const uint32_t *usertab) + * static void check_user_crc_table(const uint32_t *usertab) * { * const uint32_t *ctab = crc32c_table(); * if (!ctab || memcmp(ctab, usertab, 1024) != 0) diff --git a/ccan/crcsync/_info b/ccan/crcsync/_info index 3a98951c..79368c1d 100644 --- a/ccan/crcsync/_info +++ b/ccan/crcsync/_info @@ -30,7 +30,7 @@ * size_t len, used, blocksize; * char *file; * struct crc_context *ctx; - * uint32_t *crcs; + * uint64_t *crcs; * long res, i; * * if (argc < 3 || (blocksize = atoi(argv[1])) == 0) @@ -44,10 +44,10 @@ * if (argc == 3) { * // Short form prints CRCs of file for use in long form. * used = (len + blocksize - 1) / blocksize; - * crcs = malloc(used * sizeof(uint32_t)); + * crcs = malloc(used * sizeof(crcs[0])); * crc_of_blocks(file, len, blocksize, 32, crcs); * for (i = 0; i < used; i++) - * printf("%i ", crcs[i]); + * printf("%llu ", (long long)crcs[i]); * printf("\n"); * return 0; * } @@ -56,7 +56,7 @@ * for (i = 0; i < argc-3; i++) * crcs[i] = atoi(argv[3+i]); * - * ctx = crc_context_new(blocksize, 32, crcs, argc-3); + * ctx = crc_context_new(blocksize, 32, crcs, argc-3, 0); * for (used = 0; used < len; ) { * used += crc_read_block(ctx, &res, file+used, len-used); * print_result(res); diff --git a/ccan/endian/_info b/ccan/endian/_info index 52e91936..400f7745 100644 --- a/ccan/endian/_info +++ b/ccan/endian/_info @@ -29,6 +29,7 @@ * if (argc != 2) * errx(1, "Usage: %s ", argv[0]); * + * value = atoi(argv[1]); * printf("native: %08x\n", value); * printf("little-endian: %08x\n", cpu_to_le32(value)); * printf("big-endian: %08x\n", cpu_to_be32(value)); diff --git a/ccan/grab_file/grab_file.h b/ccan/grab_file/grab_file.h index 5f7e37c6..b2bd31ad 100644 --- a/ccan/grab_file/grab_file.h +++ b/ccan/grab_file/grab_file.h @@ -14,8 +14,11 @@ * byte after the end of the content will always be NUL. * * Example: + * #include + * #include + * ... * // Return all of standard input, as lines. - * char **read_as_lines(void) + * static char **read_stdin_as_lines(void) * { * char **lines, *all; * @@ -42,7 +45,7 @@ void *grab_fd(const void *ctx, int fd, size_t *size); * * Example: * // Return all of a given file, as lines. - * char **read_as_lines(const char *filename) + * static char **read_file_as_lines(const char *filename) * { * char **lines, *all; * diff --git a/ccan/idtree/_info b/ccan/idtree/_info index 058b32b4..c0db3632 100644 --- a/ccan/idtree/_info +++ b/ccan/idtree/_info @@ -30,7 +30,8 @@ * argv[i], idtree_add(idtree, argv[i], -1)); * } * for (i = 0; i < argc; i++) { - * printf("id %i -> '%s'\n", i, idtree_lookup(idtree, i)); + * printf("id %i -> '%s'\n", + * i, (char *)idtree_lookup(idtree, i)); * } * return 0; * } diff --git a/ccan/idtree/idtree.h b/ccan/idtree/idtree.h index cc84a3a9..3835d7f5 100644 --- a/ccan/idtree/idtree.h +++ b/ccan/idtree/idtree.h @@ -9,14 +9,14 @@ * Allocate an empty id tree. You can free it with talloc_free(). * * Example: - * #include - * * static struct idtree *ids; * - * ... + * static void init(void) + * { * ids = idtree_new(NULL); * if (!ids) * err(1, "Failed to allocate idtree"); + * } */ struct idtree *idtree_new(void *mem_ctx); @@ -31,14 +31,14 @@ struct idtree *idtree_new(void *mem_ctx); * Example: * struct foo { * unsigned int id; - * ... + * // ... * }; * * // Create a new foo, assigning an id. - * struct foo *new_foo(void) + * static struct foo *new_foo(void) * { * int id; - * foo = malloc(sizeof(*foo)); + * struct foo *foo = malloc(sizeof(*foo)); * if (!foo) * return NULL; * @@ -62,17 +62,13 @@ int idtree_add(struct idtree *idtree, const void *ptr, int limit); * * Example: * static int last_id = -1; - * struct foo { - * unsigned int id; - * ... - * }; * * // Create a new foo, assigning a consecutive id. * // This maximizes the time before ids roll. - * struct foo *new_foo(void) + * static struct foo *new_foo_inc_id(void) * { * int id; - * foo = malloc(sizeof(*foo)); + * struct foo *foo = malloc(sizeof(*foo)); * if (!foo) * return NULL; * @@ -102,7 +98,7 @@ int idtree_add_above(struct idtree *idtree, const void *ptr, * * Example: * // Look up a foo for a given ID. - * struct foo *find_foo(unsigned int id) + * static struct foo *find_foo(unsigned int id) * { * return idtree_lookup(ids, id); * } @@ -117,10 +113,8 @@ void *idtree_lookup(const struct idtree *idtree, int id); * Returns false if the id was not in the tree. * * Example: - * #include - * * // Look up a foo for a given ID. - * void *free_foo(struct foo *foo) + * static void free_foo(struct foo *foo) * { * bool exists = idtree_remove(ids, foo->id); * assert(exists); diff --git a/ccan/ilog/_info b/ccan/ilog/_info index 2b4aeb2e..59e55665 100644 --- a/ccan/ilog/_info +++ b/ccan/ilog/_info @@ -22,7 +22,7 @@ * printf("ILOG_32(0x%08X)=%i\n",0,ILOG_32(0)); * for(i=1;i<=STATIC_ILOG_32(USHRT_MAX);i++){ * uint32_t v; - * v=(uint32_t)1U<