ccanlint.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * ccanlint: assorted checks and advice for a ccan package
  3. * Copyright (C) 2008 Rusty Russell, Idris Soule
  4. *
  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)
  8. * any later version.
  9. *
  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
  13. * more details.
  14. *
  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.
  18. */
  19. #include "ccanlint.h"
  20. #include "../tools.h"
  21. #include <unistd.h>
  22. #include <getopt.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <err.h>
  28. #include <ctype.h>
  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>
  33. static unsigned int verbose = 0;
  34. static LIST_HEAD(compulsory_tests);
  35. static LIST_HEAD(normal_tests);
  36. static LIST_HEAD(finished_tests);
  37. bool safe_mode = false;
  38. static struct btree *exclude;
  39. static unsigned int timeout;
  40. static void usage(const char *name)
  41. {
  42. fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-t <ms>] [-d <dirname>] [-x <tests>]\n"
  43. " -v: verbose mode\n"
  44. " -s: simply give one line summary\n"
  45. " -d: use this directory instead of the current one\n"
  46. " -n: do not compile anything\n"
  47. " -l: list tests ccanlint performs\n"
  48. " -x: exclude tests (e.g. -x trailing_whitespace,valgrind)\n"
  49. " -t: ignore (terminate) tests that are slower than this\n",
  50. name);
  51. exit(1);
  52. }
  53. #if 0
  54. static void indent_print(const char *string)
  55. {
  56. while (*string) {
  57. unsigned int line = strcspn(string, "\n");
  58. printf("\t%.*s", line, string);
  59. if (string[line] == '\n') {
  60. printf("\n");
  61. line++;
  62. }
  63. string += line;
  64. }
  65. }
  66. #endif
  67. bool ask(const char *question)
  68. {
  69. char reply[2];
  70. printf("%s ", question);
  71. fflush(stdout);
  72. return fgets(reply, sizeof(reply), stdin) != NULL
  73. && toupper(reply[0]) == 'Y';
  74. }
  75. static const char *should_skip(struct manifest *m, struct ccanlint *i)
  76. {
  77. if (btree_lookup(exclude, i->key))
  78. return "excluded on command line";
  79. if (i->skip_fail)
  80. return "dependency failed";
  81. if (i->skip)
  82. return "dependency was skipped";
  83. if (i->can_run)
  84. return i->can_run(m);
  85. return NULL;
  86. }
  87. static bool run_test(struct ccanlint *i,
  88. bool quiet,
  89. unsigned int *score,
  90. unsigned int *total_score,
  91. struct manifest *m)
  92. {
  93. void *result;
  94. unsigned int this_score, timeleft;
  95. const struct dependent *d;
  96. const char *skip;
  97. //one less test to run through
  98. list_for_each(&i->dependencies, d, node)
  99. d->dependent->num_depends--;
  100. skip = should_skip(m, i);
  101. if (skip) {
  102. skip:
  103. if (verbose)
  104. printf(" %s: skipped (%s)\n", i->name, skip);
  105. /* If we're skipping this because a prereq failed, we fail. */
  106. if (i->skip_fail)
  107. *total_score += i->total_score;
  108. list_del(&i->list);
  109. list_add_tail(&finished_tests, &i->list);
  110. list_for_each(&i->dependencies, d, node) {
  111. d->dependent->skip = true;
  112. d->dependent->skip_fail = i->skip_fail;
  113. }
  114. return true;
  115. }
  116. timeleft = timeout ? timeout : default_timeout_ms;
  117. result = i->check(m, &timeleft);
  118. if (timeout && timeleft == 0) {
  119. skip = "timeout";
  120. goto skip;
  121. }
  122. if (!result) {
  123. if (verbose) {
  124. printf(" %s: OK", i->name);
  125. if (i->total_score)
  126. printf(" (+%u/%u)",
  127. i->total_score, i->total_score);
  128. printf("\n");
  129. }
  130. if (i->total_score) {
  131. *score += i->total_score;
  132. *total_score += i->total_score;
  133. }
  134. list_del(&i->list);
  135. list_add_tail(&finished_tests, &i->list);
  136. return true;
  137. }
  138. if (i->score)
  139. this_score = i->score(m, result);
  140. else
  141. this_score = 0;
  142. list_del(&i->list);
  143. list_add_tail(&finished_tests, &i->list);
  144. *total_score += i->total_score;
  145. *score += this_score;
  146. if (verbose) {
  147. printf(" %s: FAIL (+%u/%u)\n",
  148. i->name, this_score, i->total_score);
  149. }
  150. if (!quiet) {
  151. printf("%s\n", i->describe(m, result));
  152. if (i->handle)
  153. i->handle(m, result);
  154. }
  155. /* Skip any tests which depend on this one. */
  156. list_for_each(&i->dependencies, d, node) {
  157. d->dependent->skip = true;
  158. d->dependent->skip_fail = true;
  159. }
  160. return false;
  161. }
  162. static void register_test(struct list_head *h, struct ccanlint *test, ...)
  163. {
  164. va_list ap;
  165. struct ccanlint *depends;
  166. struct dependent *dchild;
  167. list_add(h, &test->list);
  168. va_start(ap, test);
  169. /* Careful: we might have been initialized by a dependent. */
  170. if (test->dependencies.n.next == NULL)
  171. list_head_init(&test->dependencies);
  172. //dependent(s) args (if any), last one is NULL
  173. while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
  174. dchild = malloc(sizeof(*dchild));
  175. dchild->dependent = test;
  176. /* The thing we depend on might not be initialized yet! */
  177. if (depends->dependencies.n.next == NULL)
  178. list_head_init(&depends->dependencies);
  179. list_add_tail(&depends->dependencies, &dchild->node);
  180. test->num_depends++;
  181. }
  182. va_end(ap);
  183. }
  184. /**
  185. * get_next_test - retrieves the next test to be processed
  186. **/
  187. static inline struct ccanlint *get_next_test(struct list_head *test)
  188. {
  189. struct ccanlint *i;
  190. if (list_empty(test))
  191. return NULL;
  192. list_for_each(test, i, list) {
  193. if (i->num_depends == 0)
  194. return i;
  195. }
  196. errx(1, "Can't make process; test dependency cycle");
  197. }
  198. static void init_tests(void)
  199. {
  200. const struct ccanlint *i;
  201. struct btree *keys, *names;
  202. #undef REGISTER_TEST
  203. #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__)
  204. #include "generated-normal-tests"
  205. #undef REGISTER_TEST
  206. #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__)
  207. #include "generated-compulsory-tests"
  208. /* Self-consistency check: make sure no two tests
  209. have the same key or name. */
  210. keys = btree_new(btree_strcmp);
  211. names = btree_new(btree_strcmp);
  212. list_for_each(&compulsory_tests, i, list) {
  213. if (!btree_insert(keys, i->key))
  214. errx(1, "BUG: Duplicate test key '%s'", i->key);
  215. if (!btree_insert(keys, i->name))
  216. errx(1, "BUG: Duplicate test name '%s'", i->name);
  217. }
  218. list_for_each(&normal_tests, i, list) {
  219. if (!btree_insert(keys, i->key))
  220. errx(1, "BUG: Duplicate test key '%s'", i->key);
  221. if (!btree_insert(keys, i->name))
  222. errx(1, "BUG: Duplicate test name '%s'", i->name);
  223. }
  224. btree_delete(keys);
  225. btree_delete(names);
  226. if (!verbose)
  227. return;
  228. printf("\nCompulsory Tests\n");
  229. list_for_each(&compulsory_tests, i, list) {
  230. printf("%s depends on %u others\n", i->name, i->num_depends);
  231. if (!list_empty(&i->dependencies)) {
  232. const struct dependent *d;
  233. printf("These depend on us:\n");
  234. list_for_each(&i->dependencies, d, node)
  235. printf("\t%s\n", d->dependent->name);
  236. }
  237. }
  238. printf("\nNormal Tests\n");
  239. list_for_each(&normal_tests, i, list) {
  240. printf("%s depends on %u others\n", i->name, i->num_depends);
  241. if (!list_empty(&i->dependencies)) {
  242. const struct dependent *d;
  243. printf("These depend on us:\n");
  244. list_for_each(&i->dependencies, d, node)
  245. printf("\t%s\n", d->dependent->name);
  246. }
  247. }
  248. }
  249. static void print_tests(struct list_head *tests, const char *type)
  250. {
  251. struct ccanlint *i;
  252. printf("%s tests:\n", type);
  253. /* This makes them print in topological order. */
  254. while ((i = get_next_test(tests)) != NULL) {
  255. const struct dependent *d;
  256. printf(" %-25s %s\n", i->key, i->name);
  257. list_del(&i->list);
  258. list_for_each(&i->dependencies, d, node)
  259. d->dependent->num_depends--;
  260. }
  261. }
  262. static void list_tests(void)
  263. {
  264. init_tests();
  265. print_tests(&compulsory_tests, "Compulsory");
  266. print_tests(&normal_tests, "Normal");
  267. exit(0);
  268. }
  269. int main(int argc, char *argv[])
  270. {
  271. int c;
  272. bool summary = false;
  273. unsigned int score = 0, total_score = 0;
  274. struct manifest *m;
  275. struct ccanlint *i;
  276. const char *prefix = "", *dir = ".";
  277. exclude = btree_new(btree_strcmp);
  278. /* I'd love to use long options, but that's not standard. */
  279. /* FIXME: getopt_long ccan package? */
  280. while ((c = getopt(argc, argv, "sd:vnlx:t:")) != -1) {
  281. switch (c) {
  282. case 'd':
  283. dir = optarg;
  284. prefix = talloc_append_string(talloc_basename(NULL,
  285. optarg),
  286. ": ");
  287. break;
  288. case 'l':
  289. list_tests();
  290. case 's':
  291. summary = true;
  292. break;
  293. case 'v':
  294. verbose++;
  295. break;
  296. case 'n':
  297. safe_mode = true;
  298. break;
  299. case 'x': {
  300. char **exclude_strs = strsplit(NULL, optarg, ",", NULL);
  301. size_t i;
  302. for (i = 0; exclude_strs[i]; i++)
  303. btree_insert(exclude, exclude_strs[i]);
  304. } break;
  305. case 't':
  306. timeout = atoi(optarg);
  307. if (!timeout)
  308. errx(1, "Invalid timeout %s: 1 ms minumum",
  309. optarg);
  310. break;
  311. default:
  312. usage(argv[0]);
  313. }
  314. }
  315. if (optind < argc)
  316. usage(argv[0]);
  317. m = get_manifest(talloc_autofree_context(), dir);
  318. init_tests();
  319. /* If you don't pass the compulsory tests, you don't even get a score */
  320. if (verbose)
  321. printf("Compulsory tests:\n");
  322. while ((i = get_next_test(&compulsory_tests)) != NULL) {
  323. if (!run_test(i, summary, &score, &total_score, m)) {
  324. errx(1, "%s%s failed", prefix, i->name);
  325. }
  326. }
  327. if (verbose)
  328. printf("\nNormal tests:\n");
  329. while ((i = get_next_test(&normal_tests)) != NULL)
  330. run_test(i, summary, &score, &total_score, m);
  331. printf("%sTotal score: %u/%u\n", prefix, score, total_score);
  332. return 0;
  333. }