ccanlint.c 8.9 KB

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