Commit 18636637 authored by Rusty Russell's avatar Rusty Russell

modules: update documentation examples so they compile under ccanlint.

This is everything up to the list module... now it's time for sleep.
parent b1801a00
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
* // We currently use 32 random values. * // We currently use 32 random values.
* static unsigned int vals[32]; * static unsigned int vals[32];
* *
* void init_values(void) * static void init_values(void)
* { * {
* unsigned int i; * unsigned int i;
* for (i = 0; i < ARRAY_SIZE(vals); i++) * for (i = 0; i < ARRAY_SIZE(vals); i++)
......
...@@ -19,12 +19,12 @@ ...@@ -19,12 +19,12 @@
* #include <stdio.h> * #include <stdio.h>
* #include <string.h> * #include <string.h>
* *
* static int cmp(const char **a, const char **n, bool *casefold) * static int cmp(char *const *a, char *const *n, bool *casefold)
* { * {
* if (*casefold) * if (*casefold)
* return strcasecmp(*a, *b); * return strcasecmp(*a, *n);
* else * else
* return strcmp(*a, *b); * return strcmp(*a, *n);
* } * }
* *
* int main(int argc, char *argv[]) * int main(int argc, char *argv[])
......
...@@ -29,12 +29,15 @@ ...@@ -29,12 +29,15 @@
* int array[] = {0,1,1,2,3,5,8,13,21,34}; * int array[] = {0,1,1,2,3,5,8,13,21,34};
* int *array_copy = block_pool_memdup(bp, array, sizeof(array)); * 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); * block_pool_free(bp);
* return 0; * return 0;
* } * }
* *
* Author: Joey Adams * Author: Joey Adams
* License: BSD * License: BSD
*/ */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
* }; * };
* *
* //Define the ordering function order_by_letter_set * //Define the ordering function order_by_letter_set
* btree_search_implement * static btree_search_implement
* ( * (
* order_by_letter_set, * order_by_letter_set,
* struct word *, * struct word *,
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
* char *chomp(char *str); * char *chomp(char *str);
* char *make_letter_set(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; * btree_iterator iter;
* *
...@@ -61,7 +61,7 @@ ...@@ -61,7 +61,7 @@
* btree_insert_at(iter, word); * 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; * btree_iterator iter, end;
* struct word key = { * struct word key = {
...@@ -86,7 +86,7 @@ ...@@ -86,7 +86,7 @@
* } * }
* } * }
* *
* int destroy_word(struct word *word, void *ctx) * static int destroy_word(struct word *word, void *ctx)
* { * {
* (void) ctx; * (void) ctx;
* *
...@@ -97,7 +97,7 @@ ...@@ -97,7 +97,7 @@
* return 1; * return 1;
* } * }
* *
* struct btree *read_dictionary(const char *filename) * static struct btree *read_dictionary(const char *filename)
* { * {
* FILE *f; * FILE *f;
* char line[256]; * char line[256];
...@@ -129,7 +129,7 @@ ...@@ -129,7 +129,7 @@
* *
* return btree; * return btree;
* *
* fail: * fail:
* btree_delete(btree); * btree_delete(btree);
* fprintf(stderr, "%s: %s\n", filename, strerror(errno)); * fprintf(stderr, "%s: %s\n", filename, strerror(errno));
* return NULL; * return NULL;
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
* int x; * 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 * // This trick requires that the string be first in the structure
* BUILD_ASSERT(offsetof(struct foo, string) == 0); * BUILD_ASSERT(offsetof(struct foo, string) == 0);
......
...@@ -9,7 +9,9 @@ ...@@ -9,7 +9,9 @@
* by the compiler. This can only be used within a function. * by the compiler. This can only be used within a function.
* *
* Example: * Example:
* char *foo_to_char(struct foo *foo) * #include <stddef.h>
* ...
* static char *foo_to_char(struct foo *foo)
* { * {
* // This code needs string to be at start of foo. * // This code needs string to be at start of foo.
* BUILD_ASSERT(offsetof(struct foo, string) == 0); * BUILD_ASSERT(offsetof(struct foo, string) == 0);
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* #include <ccan/grab_file/grab_file.h> * #include <ccan/grab_file/grab_file.h>
* #include <err.h> * #include <err.h>
* *
* 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 comment=0, white=0, stray=0, code=0, total=0;
* size_t count = 0; * size_t count = 0;
* const struct token *i; * const struct token *i;
......
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
* if (!file) * if (!file)
* err(1, "Could not read file %s", argv[1]); * 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"); * printf("File contents are %s UTF-8\n", valid ? "valid" : "invalid");
* *
* talloc_free(file); * talloc_free(file);
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* compiler - macros for common compiler extensions * compiler - macros for common compiler extensions
* *
* Abstracts away some compiler hints. Currently these include: * Abstracts away some compiler hints. Currently these include:
* - UNLIKELY_FUNCTION_ATTRIBUTE * - COLD_ATTRIBUTE
* For functions not called in fast paths (aka. cold functions) * For functions not called in fast paths (aka. cold functions)
* - PRINTF_ATTRIBUTE * - PRINTF_ATTRIBUTE
* For functions which take printf-style parameters. * For functions which take printf-style parameters.
...@@ -23,13 +23,13 @@ ...@@ -23,13 +23,13 @@
* Author: Rusty Russell <rusty@rustcorp.com.au> * Author: Rusty Russell <rusty@rustcorp.com.au>
* *
* Example: * Example:
* #include <ccan/compiler/attributs.h> * #include <ccan/compiler/compiler.h>
* #include <stdio.h> * #include <stdio.h>
* #include <stdarg.h> * #include <stdarg.h>
* *
* // Example of a (slow-path) logging function. * // Example of a (slow-path) logging function.
* static int log_threshold = 2; * 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, ...) * logger(int level, const char *fmt, ...)
* { * {
* va_list ap; * va_list ap;
......
...@@ -10,10 +10,10 @@ ...@@ -10,10 +10,10 @@
* It is usually used on logging or error routines. * It is usually used on logging or error routines.
* *
* Example: * Example:
* void COLD_ATTRIBUTE moan(const char *reason) * static void COLD_ATTRIBUTE moan(const char *reason)
* { * {
* fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno)); * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
* } * }
*/ */
#define COLD_ATTRIBUTE __attribute__((cold)) #define COLD_ATTRIBUTE __attribute__((cold))
#else #else
...@@ -23,13 +23,14 @@ ...@@ -23,13 +23,14 @@
#if HAVE_ATTRIBUTE_PRINTF #if HAVE_ATTRIBUTE_PRINTF
/** /**
* PRINTF_ATTRIBUTE - a function takes printf-style arguments * PRINTF_ATTRIBUTE - a function takes printf-style arguments
* nfmt: the 1-based number of the function's format argument. * @nfmt: the 1-based number of the function's format argument.
* narg: the 1-based number of the function's first variable 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(). * This allows the compiler to check your parameters as it does for printf().
* *
* Example: * 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) \ #define PRINTF_ATTRIBUTE(nfmt, narg) \
__attribute__((format(__printf__, nfmt, narg))) __attribute__((format(__printf__, nfmt, narg)))
...@@ -58,18 +59,14 @@ ...@@ -58,18 +59,14 @@
* the compiler that if it is unused it need not emit it into the source code. * the compiler that if it is unused it need not emit it into the source code.
* *
* Example: * Example:
* // With some config options, this is unnecessary. * // With some preprocessor options, this is unnecessary.
* static UNNEEDED_ATTRIBUTE int counter; * static UNNEEDED_ATTRIBUTE int counter;
* ... *
* #ifdef DEBUG * // With some preprocessor options, this is unnecessary.
* counter++; * static UNNEEDED_ATTRIBUTE void add_to_counter(int add)
* #endif * {
* ... * counter += add;
* // With some config options, this is unnecessary. * }
* static UNNEEDED_ATTRIBUTE int add_to_counter(int add)
* {
* counter += add;
* }
*/ */
#define UNNEEDED_ATTRIBUTE __attribute__((unused)) #define UNNEEDED_ATTRIBUTE __attribute__((unused))
...@@ -117,7 +114,7 @@ ...@@ -117,7 +114,7 @@
* const char *greek_name(enum greek greek); * const char *greek_name(enum greek greek);
* *
* // Inline version. * // Inline version.
* static inline _greek_name(enum greek greek) * static inline char *_greek_name(enum greek greek)
* { * {
* switch (greek) { * switch (greek) {
* case ALPHA: return "alpha"; * case ALPHA: return "alpha";
......
...@@ -26,13 +26,18 @@ ...@@ -26,13 +26,18 @@
* struct timer timer; * struct timer timer;
* }; * };
* *
* static void register_timer(struct timer *timer)
* {
* //...
* }
*
* static void my_timer_callback(struct timer *timer) * static void my_timer_callback(struct timer *timer)
* { * {
* struct info *info = container_of(timer, struct info, timer); * struct info *info = container_of(timer, struct info, timer);
* printf("my_stuff is %u\n", info->my_stuff); * printf("my_stuff is %u\n", info->my_stuff);
* } * }
* *
* int main() * int main(void)
* { * {
* struct info info = { .my_stuff = 1 }; * struct info info = { .my_stuff = 1 };
* *
......
...@@ -15,13 +15,16 @@ ...@@ -15,13 +15,16 @@
* subtraction to return the pointer to the enclosing type. * subtraction to return the pointer to the enclosing type.
* *
* Example: * Example:
* struct info * struct foo {
* { * int fielda, fieldb;
* // ...
* };
* struct info {
* int some_other_field; * int some_other_field;
* struct foo my_foo; * 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); * return container_of(foo, struct info, my_foo);
* } * }
...@@ -42,13 +45,7 @@ ...@@ -42,13 +45,7 @@
* subtraction to return the pointer to the enclosing type. * subtraction to return the pointer to the enclosing type.
* *
* Example: * Example:
* struct info * static struct info *foo_to_i(struct foo *foo)
* {
* int some_other_field;
* struct foo my_foo;
* };
*
* struct info *foo_to_info(struct foo *foop)
* { * {
* struct info *i = container_of_var(foo, i, my_foo); * struct info *i = container_of_var(foo, i, my_foo);
* return i; * return i;
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* detect a single error burst of up to 32 bits. * detect a single error burst of up to 32 bits.
* *
* Example: * Example:
* #include <ccan/crc.h> * #include <ccan/crc/crc.h>
* #include <stdio.h> * #include <stdio.h>
* #include <stdlib.h> * #include <stdlib.h>
* *
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
* "Prints 32 bit CRC of the string\n", argv[0]); * "Prints 32 bit CRC of the string\n", argv[0]);
* exit(1); * 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); * exit(0);
* } * }
* *
......
...@@ -29,11 +29,13 @@ ...@@ -29,11 +29,13 @@
* as 0 the first time, and the current crc result from then on. * as 0 the first time, and the current crc result from then on.
* *
* Example: * Example:
* #include <sys/uio.h>
* ...
* // Check that iovec has the crc we expect (Castagnoli version) * // 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; * uint32_t crc = 0;
* while (iovcnt >= 0) { * while (l >= 0) {
* crc = crc32c(crc, iov->iov_base, iov->iov_len); * crc = crc32c(crc, iov->iov_base, iov->iov_len);
* iov++; * iov++;
* } * }
...@@ -52,7 +54,7 @@ uint32_t crc32c(uint32_t start_crc, const void *buf, size_t size); ...@@ -52,7 +54,7 @@ uint32_t crc32c(uint32_t start_crc, const void *buf, size_t size);
* *
* Example: * Example:
* // This dumb code only handles Castagnoli, so assert that here. * // 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(); * const uint32_t *ctab = crc32c_table();
* if (!ctab || memcmp(ctab, usertab, 1024) != 0) * if (!ctab || memcmp(ctab, usertab, 1024) != 0)
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
* size_t len, used, blocksize; * size_t len, used, blocksize;
* char *file; * char *file;
* struct crc_context *ctx; * struct crc_context *ctx;
* uint32_t *crcs; * uint64_t *crcs;
* long res, i; * long res, i;
* *
* if (argc < 3 || (blocksize = atoi(argv[1])) == 0) * if (argc < 3 || (blocksize = atoi(argv[1])) == 0)
...@@ -44,10 +44,10 @@ ...@@ -44,10 +44,10 @@
* if (argc == 3) { * if (argc == 3) {
* // Short form prints CRCs of file for use in long form. * // Short form prints CRCs of file for use in long form.
* used = (len + blocksize - 1) / blocksize; * 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); * crc_of_blocks(file, len, blocksize, 32, crcs);
* for (i = 0; i < used; i++) * for (i = 0; i < used; i++)
* printf("%i ", crcs[i]); * printf("%llu ", (long long)crcs[i]);
* printf("\n"); * printf("\n");
* return 0; * return 0;
* } * }
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
* for (i = 0; i < argc-3; i++) * for (i = 0; i < argc-3; i++)
* crcs[i] = atoi(argv[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; ) { * for (used = 0; used < len; ) {
* used += crc_read_block(ctx, &res, file+used, len-used); * used += crc_read_block(ctx, &res, file+used, len-used);
* print_result(res); * print_result(res);
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
* if (argc != 2) * if (argc != 2)
* errx(1, "Usage: %s <value>", argv[0]); * errx(1, "Usage: %s <value>", argv[0]);
* *
* value = atoi(argv[1]);
* printf("native: %08x\n", value); * printf("native: %08x\n", value);
* printf("little-endian: %08x\n", cpu_to_le32(value)); * printf("little-endian: %08x\n", cpu_to_le32(value));
* printf("big-endian: %08x\n", cpu_to_be32(value)); * printf("big-endian: %08x\n", cpu_to_be32(value));
......
...@@ -14,8 +14,11 @@ ...@@ -14,8 +14,11 @@
* byte after the end of the content will always be NUL. * byte after the end of the content will always be NUL.
* *
* Example: * Example:
* #include <ccan/str_talloc/str_talloc.h>
* #include <ccan/talloc/talloc.h>
* ...
* // Return all of standard input, as lines. * // Return all of standard input, as lines.
* char **read_as_lines(void) * static char **read_stdin_as_lines(void)
* { * {
* char **lines, *all; * char **lines, *all;
* *
...@@ -42,7 +45,7 @@ void *grab_fd(const void *ctx, int fd, size_t *size); ...@@ -42,7 +45,7 @@ void *grab_fd(const void *ctx, int fd, size_t *size);
* *
* Example: * Example:
* // Return all of a given file, as lines. * // 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; * char **lines, *all;
* *
......
...@@ -30,7 +30,8 @@ ...@@ -30,7 +30,8 @@
* argv[i], idtree_add(idtree, argv[i], -1)); * argv[i], idtree_add(idtree, argv[i], -1));
* } * }
* for (i = 0; i < argc; i++) { * 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; * return 0;
* } * }
......
...@@ -9,14 +9,14 @@ ...@@ -9,14 +9,14 @@
* Allocate an empty id tree. You can free it with talloc_free(). * Allocate an empty id tree. You can free it with talloc_free().
* *
* Example: * Example:
* #include <err.h>
*
* static struct idtree *ids; * static struct idtree *ids;
* *
* ... * static void init(void)
* {
* ids = idtree_new(NULL); * ids = idtree_new(NULL);
* if (!ids) * if (!ids)
* err(1, "Failed to allocate idtree"); * err(1, "Failed to allocate idtree");
* }
*/ */
struct idtree *idtree_new(void *mem_ctx); struct idtree *idtree_new(void *mem_ctx);
...@@ -31,14 +31,14 @@ struct idtree *idtree_new(void *mem_ctx); ...@@ -31,14 +31,14 @@ struct idtree *idtree_new(void *mem_ctx);
* Example: * Example:
* struct foo { * struct foo {
* unsigned int id; * unsigned int id;
* ... * // ...
* }; * };
* *
* // Create a new foo, assigning an id. * // Create a new foo, assigning an id.
* struct foo *new_foo(void) * static struct foo *new_foo(void)
* { * {
* int id; * int id;
* foo = malloc(sizeof(*foo)); * struct foo *foo = malloc(sizeof(*foo));
* if (!foo) * if (!foo)
* return NULL; * return NULL;
* *
...@@ -62,17 +62,13 @@ int idtree_add(struct idtree *idtree, const void *ptr, int limit); ...@@ -62,17 +62,13 @@ int idtree_add(struct idtree *idtree, const void *ptr, int limit);
* *
* Example: * Example:
* static int last_id = -1; * static int last_id = -1;
* struct foo {
* unsigned int id;
* ...
* };
* *
* // Create a new foo, assigning a consecutive id. * // Create a new foo, assigning a consecutive id.
* // This maximizes the time before ids roll. * // This maximizes the time before ids roll.
* struct foo *new_foo(void) * static struct foo *new_foo_inc_id(void)
* { * {
* int id; * int id;
* foo = malloc(sizeof(*foo)); * struct foo *foo = malloc(sizeof(*foo));
* if (!foo) * if (!foo)
* return NULL; * return NULL;
* *
...@@ -102,7 +98,7 @@ int idtree_add_above(struct idtree *idtree, const void *ptr, ...@@ -102,7 +98,7 @@ int idtree_add_above(struct idtree *idtree, const void *ptr,
* *
* Example: * Example:
* // Look up a foo for a given ID. * // 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); * return idtree_lookup(ids, id);
* } * }
...@@ -117,10 +113,8 @@ void *idtree_lookup(const struct idtree *idtree, int id); ...@@ -117,10 +113,8 @@ void *idtree_lookup(const struct idtree *idtree, int id);
* Returns false if the id was not in the tree. * Returns false if the id was not in the tree.
* *
* Example: * Example:
* #include <assert.h>
*
* // Look up a foo for a given ID. * // 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); * bool exists = idtree_remove(ids, foo->id);
* assert(exists); * assert(exists);
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
* printf("ILOG_32(0x%08X)=%i\n",0,ILOG_32(0)); * printf("ILOG_32(0x%08X)=%i\n",0,ILOG_32(0));
* for(i=1;i<=STATIC_ILOG_32(USHRT_MAX);i++){ * for(i=1;i<=STATIC_ILOG_32(USHRT_MAX);i++){
* uint32_t v; * uint32_t v;
* v=(uint32_t)1U<<i-1; * v=(uint32_t)1U<<(i-1);
* //Here we know v is non-zero, so we can use ILOGNZ_32(). * //Here we know v is non-zero, so we can use ILOGNZ_32().
* printf("ILOG_32(0x%08X)=%i\n",v,ILOGNZ_32(v)); * printf("ILOG_32(0x%08X)=%i\n",v,ILOGNZ_32(v));
* } * }
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
* // Returns false if we overflow. * // Returns false if we overflow.
* static inline bool inc_int(unsigned int *val) * static inline bool inc_int(unsigned int *val)
* { * {
* *(val)++; * (*val)++;
* if (likely(*val)) * if (likely(*val))
* return true; * return true;
* return false; * return false;
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
* // Prints a warning if we overflow. * // Prints a warning if we overflow.
* static inline void inc_int(unsigned int *val) * static inline void inc_int(unsigned int *val)
* { * {
* *(val)++; * (*val)++;
* if (unlikely(*val == 0)) * if (unlikely(*val == 0))
* fprintf(stderr, "Overflow!"); * fprintf(stderr, "Overflow!");
* } * }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment