2 * ccanlint: assorted checks and advice for a ccan package
3 * Copyright (C) 2008 Rusty Russell, Idris Soule
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <ccan/btree/btree.h>
30 #include <ccan/str/str.h>
31 #include <ccan/str_talloc/str_talloc.h>
32 #include <ccan/talloc/talloc.h>
34 static unsigned int verbose = 0;
35 static LIST_HEAD(compulsory_tests);
36 static LIST_HEAD(normal_tests);
37 static LIST_HEAD(finished_tests);
38 bool safe_mode = false;
39 static struct btree *exclude;
41 static void usage(const char *name)
43 fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-d <dirname>]\n"
45 " -s: simply give one line summary\n"
46 " -d: use this directory instead of the current one\n"
47 " -n: do not compile anything\n"
48 " -l: list tests ccanlint performs\n"
49 " -x: exclude tests (e.g. -x trailing_whitespace,valgrind)\n",
55 static void indent_print(const char *string)
58 unsigned int line = strcspn(string, "\n");
59 printf("\t%.*s", line, string);
60 if (string[line] == '\n') {
69 bool ask(const char *question)
73 printf("%s ", question);
76 return fgets(reply, sizeof(reply), stdin) != NULL
77 && toupper(reply[0]) == 'Y';
80 static const char *should_skip(struct manifest *m, struct ccanlint *i)
82 if (btree_lookup(exclude, i->key))
83 return "excluded on command line";
86 return "dependency failed";
89 return "dependency was skipped";
96 static bool run_test(struct ccanlint *i,
99 unsigned int *total_score,
103 unsigned int this_score;
104 const struct dependent *d;
107 //one less test to run through
108 list_for_each(&i->dependencies, d, node)
109 d->dependent->num_depends--;
111 skip = should_skip(m, i);
114 printf(" %s: skipped (%s)\n", i->name, skip);
116 /* If we're skipping this because a prereq failed, we fail. */
118 *total_score += i->total_score;
121 list_add_tail(&finished_tests, &i->list);
122 list_for_each(&i->dependencies, d, node) {
123 d->dependent->skip = true;
124 d->dependent->skip_fail = i->skip_fail;
129 result = i->check(m);
132 printf(" %s: OK", i->name);
135 i->total_score, i->total_score);
138 if (i->total_score) {
139 *score += i->total_score;
140 *total_score += i->total_score;
144 list_add_tail(&finished_tests, &i->list);
149 this_score = i->score(m, result);
154 list_add_tail(&finished_tests, &i->list);
156 *total_score += i->total_score;
157 *score += this_score;
159 printf(" %s: FAIL (+%u/%u)\n",
160 i->name, this_score, i->total_score);
163 printf("%s\n", i->describe(m, result));
166 i->handle(m, result);
169 /* Skip any tests which depend on this one. */
170 list_for_each(&i->dependencies, d, node) {
171 d->dependent->skip = true;
172 d->dependent->skip_fail = true;
178 static void register_test(struct list_head *h, struct ccanlint *test, ...)
181 struct ccanlint *depends;
182 struct dependent *dchild;
184 list_add(h, &test->list);
187 /* Careful: we might have been initialized by a dependent. */
188 if (test->dependencies.n.next == NULL)
189 list_head_init(&test->dependencies);
191 //dependent(s) args (if any), last one is NULL
192 while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
193 dchild = malloc(sizeof(*dchild));
194 dchild->dependent = test;
195 /* The thing we depend on might not be initialized yet! */
196 if (depends->dependencies.n.next == NULL)
197 list_head_init(&depends->dependencies);
198 list_add_tail(&depends->dependencies, &dchild->node);
205 * get_next_test - retrieves the next test to be processed
207 static inline struct ccanlint *get_next_test(struct list_head *test)
211 if (list_empty(test))
214 list_for_each(test, i, list) {
215 if (i->num_depends == 0)
218 errx(1, "Can't make process; test dependency cycle");
221 static void init_tests(void)
223 const struct ccanlint *i;
224 struct btree *keys, *names;
227 #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__)
228 #include "generated-normal-tests"
230 #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__)
231 #include "generated-compulsory-tests"
233 /* Self-consistency check: make sure no two tests
234 have the same key or name. */
235 keys = btree_new(btree_strcmp);
236 names = btree_new(btree_strcmp);
237 list_for_each(&compulsory_tests, i, list) {
238 if (!btree_insert(keys, i->key))
239 errx(1, "BUG: Duplicate test key '%s'", i->key);
240 if (!btree_insert(keys, i->name))
241 errx(1, "BUG: Duplicate test name '%s'", i->name);
243 list_for_each(&normal_tests, i, list) {
244 if (!btree_insert(keys, i->key))
245 errx(1, "BUG: Duplicate test key '%s'", i->key);
246 if (!btree_insert(keys, i->name))
247 errx(1, "BUG: Duplicate test name '%s'", i->name);
255 printf("\nCompulsory Tests\n");
256 list_for_each(&compulsory_tests, i, list) {
257 printf("%s depends on %u others\n", i->name, i->num_depends);
258 if (!list_empty(&i->dependencies)) {
259 const struct dependent *d;
260 printf("These depend on us:\n");
261 list_for_each(&i->dependencies, d, node)
262 printf("\t%s\n", d->dependent->name);
266 printf("\nNormal Tests\n");
267 list_for_each(&normal_tests, i, list) {
268 printf("%s depends on %u others\n", i->name, i->num_depends);
269 if (!list_empty(&i->dependencies)) {
270 const struct dependent *d;
271 printf("These depend on us:\n");
272 list_for_each(&i->dependencies, d, node)
273 printf("\t%s\n", d->dependent->name);
278 static void print_tests(struct list_head *tests, const char *type)
282 printf("%s tests:\n", type);
283 /* This makes them print in topological order. */
284 while ((i = get_next_test(tests)) != NULL) {
285 const struct dependent *d;
286 printf(" %-25s %s\n", i->key, i->name);
288 list_for_each(&i->dependencies, d, node)
289 d->dependent->num_depends--;
293 static void list_tests(void)
296 print_tests(&compulsory_tests, "Compulsory");
297 print_tests(&normal_tests, "Normal");
301 int main(int argc, char *argv[])
304 bool summary = false;
305 unsigned int score = 0, total_score = 0;
308 const char *prefix = "", *dir = ".";
310 exclude = btree_new(btree_strcmp);
312 /* I'd love to use long options, but that's not standard. */
313 /* FIXME: getopt_long ccan package? */
314 while ((c = getopt(argc, argv, "sd:vnlx:")) != -1) {
318 prefix = talloc_append_string(talloc_basename(NULL,
334 char **exclude_strs = strsplit(NULL, optarg, ",", NULL);
336 for (i = 0; exclude_strs[i]; i++)
337 btree_insert(exclude, exclude_strs[i]);
347 m = get_manifest(talloc_autofree_context(), dir);
351 /* If you don't pass the compulsory tests, you don't even get a score */
353 printf("Compulsory tests:\n");
355 while ((i = get_next_test(&compulsory_tests)) != NULL) {
356 if (!run_test(i, summary, &score, &total_score, m)) {
357 errx(1, "%s%s failed", prefix, i->name);
362 printf("\nNormal tests:\n");
363 while ((i = get_next_test(&normal_tests)) != NULL)
364 run_test(i, summary, &score, &total_score, m);
366 printf("%sTotal score: %u/%u\n", prefix, score, total_score);