ccanlint: exit with non-zero exit status if a test fails.
[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                                    bool keep,
21                                    unsigned int *timeleft, struct score *score)
22 {
23         struct list_head *list;
24         const char *explanation =
25         "\n\t(#if works like #ifdef, but with gcc's -Wundef, we can detect\n"
26         "\tmistyped or unknown configuration options)";
27
28         /* We don't fail ccanlint for this. */ 
29         score->pass = true;
30
31         foreach_ptr(list, &m->c_files, &m->h_files,
32                     &m->run_tests, &m->api_tests,
33                     &m->compile_ok_tests, &m->compile_fail_tests,
34                     &m->other_test_c_files) {
35                 struct ccan_file *f;
36
37                 list_for_each(list, f, list) {
38                         unsigned int i;
39                         char **lines = get_ccan_file_lines(f);
40
41                         for (i = 0; lines[i]; i++) {
42                                 const char *line = lines[i];
43                                 char *sym;
44
45                                 if (!get_token(&line, "#"))
46                                         continue;
47                                 if (!(get_token(&line, "if")
48                                       && get_token(&line, "defined")
49                                       && get_token(&line, "("))
50                                     && !get_token(&line, "ifdef"))
51                                         continue;
52
53                                 sym = get_symbol_token(lines, &line);
54                                 if (!sym || !strstarts(sym, "HAVE_"))
55                                         continue;
56                                 score_file_error(score, f, i+1,
57                                                  "%s should be tested with #if"
58                                                  "%s",
59                                                  sym, explanation);
60                                 explanation = "";
61                         }
62                 }
63         }
64
65         if (!score->error) {
66                 score->score = score->total;
67         }
68 }
69
70 struct ccanlint hash_if = {
71         .key = "hash_if",
72         .name = "Features are checked with #if not #ifdef",
73         .check = check_hash_if,
74         .needs = ""
75 };
76
77 REGISTER_TEST(hash_if);