ccanlint: remove argument to -k/--keep
[ccan] / tools / ccanlint / tests / hash_if.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
5 #include <ccan/str_talloc/str_talloc.h>
6 #include <ccan/foreach/foreach.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <limits.h>
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <err.h>
16 #include <string.h>
17 #include <ctype.h>
18
19 static void check_hash_if(struct manifest *m,
20                           unsigned int *timeleft, struct score *score)
21 {
22         struct list_head *list;
23         const char *explanation =
24         "\n\t(#if works like #ifdef, but with gcc's -Wundef, we can detect\n"
25         "\tmistyped or unknown configuration options)";
26
27         /* We don't fail ccanlint for this. */ 
28         score->pass = true;
29
30         foreach_ptr(list, &m->c_files, &m->h_files,
31                     &m->run_tests, &m->api_tests,
32                     &m->compile_ok_tests, &m->compile_fail_tests,
33                     &m->other_test_c_files) {
34                 struct ccan_file *f;
35
36                 list_for_each(list, f, list) {
37                         unsigned int i;
38                         char **lines = get_ccan_file_lines(f);
39
40                         for (i = 0; lines[i]; i++) {
41                                 const char *line = lines[i];
42                                 char *sym;
43
44                                 if (!get_token(&line, "#"))
45                                         continue;
46                                 if (!(get_token(&line, "if")
47                                       && get_token(&line, "defined")
48                                       && get_token(&line, "("))
49                                     && !get_token(&line, "ifdef"))
50                                         continue;
51
52                                 sym = get_symbol_token(lines, &line);
53                                 if (!sym || !strstarts(sym, "HAVE_"))
54                                         continue;
55                                 score_file_error(score, f, i+1,
56                                                  "%s should be tested with #if"
57                                                  "%s",
58                                                  sym, explanation);
59                                 explanation = "";
60                         }
61                 }
62         }
63
64         if (!score->error) {
65                 score->score = score->total;
66         }
67 }
68
69 struct ccanlint hash_if = {
70         .key = "hash_if",
71         .name = "Features are checked with #if not #ifdef",
72         .check = check_hash_if,
73         .needs = "info_exists"
74 };
75
76 REGISTER_TEST(hash_if);