ccanlint.c 11 KB

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