ccanlint.c 10 KB

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