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