X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=tools%2Fccanlint%2Ftests%2Fhash_if.c;fp=tools%2Fccanlint%2Ftests%2Fhash_if.c;h=3eefd1fdadf51f00e8689b8cb806513c3d8015b8;hp=0000000000000000000000000000000000000000;hb=9450cb3c1dff76e09305d18687291bce4221e9b6;hpb=6c1d3e2739e97674069862ec7369677d25e80312 diff --git a/tools/ccanlint/tests/hash_if.c b/tools/ccanlint/tests/hash_if.c new file mode 100644 index 00000000..3eefd1fd --- /dev/null +++ b/tools/ccanlint/tests/hash_if.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void check_hash_if(struct manifest *m, + bool keep, + unsigned int *timeleft, struct score *score) +{ + struct list_head *list; + const char *explanation = + "\n\t(#if works like #ifdef, but with gcc's -Wundef, we can detect\n" + "\tmistyped or unknown configuration options)"; + + foreach_ptr(list, &m->c_files, &m->h_files, + &m->run_tests, &m->api_tests, + &m->compile_ok_tests, &m->compile_fail_tests, + &m->other_test_c_files) { + struct ccan_file *f; + + list_for_each(list, f, list) { + unsigned int i; + char **lines = get_ccan_file_lines(f); + + for (i = 0; lines[i]; i++) { + const char *line = lines[i]; + char *sym; + + if (!get_token(&line, "#")) + continue; + if (!(get_token(&line, "if") + && get_token(&line, "defined") + && get_token(&line, "(")) + && !get_token(&line, "ifdef")) + continue; + + sym = get_symbol_token(lines, &line); + if (!sym || !strstarts(sym, "HAVE_")) + continue; + score_file_error(score, f, i+1, + "%s should be tested with #if" + "%s", + sym, explanation); + explanation = ""; + } + } + } + + if (!score->error) { + score->pass = true; + score->score = score->total; + } +} + +struct ccanlint hash_if = { + .key = "hash_if", + .name = "Features are checked with #if not #ifdef", + .check = check_hash_if, + .needs = "" +}; + +REGISTER_TEST(hash_if);