ccanlint.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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, max_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. max_score = i->total_score;
  128. if (!max_score)
  129. max_score = 1;
  130. if (!result)
  131. this_score = max_score;
  132. else if (i->score)
  133. this_score = i->score(m, result);
  134. else
  135. this_score = 0;
  136. bad = (this_score == 0);
  137. good = (this_score >= max_score);
  138. if (verbose || (!good && !quiet)) {
  139. printf(" %s: %s", i->name,
  140. bad ? "FAIL" : good ? "PASS" : "PARTIAL");
  141. if (max_score > 1)
  142. printf(" (+%u/%u)", this_score, max_score);
  143. printf("\n");
  144. }
  145. if (!quiet && result) {
  146. const char *desc;
  147. if (i->describe && (desc = i->describe(m, result)) != NULL)
  148. printf(" %s\n", desc);
  149. if (i->handle)
  150. i->handle(m, result);
  151. }
  152. if (i->total_score) {
  153. *score += this_score;
  154. *total_score += i->total_score;
  155. }
  156. list_del(&i->list);
  157. list_add_tail(&finished_tests, &i->list);
  158. if (bad) {
  159. /* Skip any tests which depend on this one. */
  160. list_for_each(&i->dependencies, d, node) {
  161. d->dependent->skip = true;
  162. d->dependent->skip_fail = true;
  163. }
  164. }
  165. return good;
  166. }
  167. static void register_test(struct list_head *h, struct ccanlint *test, ...)
  168. {
  169. va_list ap;
  170. struct ccanlint *depends;
  171. struct dependent *dchild;
  172. list_add(h, &test->list);
  173. va_start(ap, test);
  174. /* Careful: we might have been initialized by a dependent. */
  175. if (test->dependencies.n.next == NULL)
  176. list_head_init(&test->dependencies);
  177. //dependent(s) args (if any), last one is NULL
  178. while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
  179. dchild = malloc(sizeof(*dchild));
  180. dchild->dependent = test;
  181. /* The thing we depend on might not be initialized yet! */
  182. if (depends->dependencies.n.next == NULL)
  183. list_head_init(&depends->dependencies);
  184. list_add_tail(&depends->dependencies, &dchild->node);
  185. test->num_depends++;
  186. }
  187. va_end(ap);
  188. }
  189. /**
  190. * get_next_test - retrieves the next test to be processed
  191. **/
  192. static inline struct ccanlint *get_next_test(struct list_head *test)
  193. {
  194. struct ccanlint *i;
  195. if (list_empty(test))
  196. return NULL;
  197. list_for_each(test, i, list) {
  198. if (i->num_depends == 0)
  199. return i;
  200. }
  201. errx(1, "Can't make process; test dependency cycle");
  202. }
  203. static void init_tests(void)
  204. {
  205. const struct ccanlint *i;
  206. struct btree *keys, *names;
  207. #undef REGISTER_TEST
  208. #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__, NULL)
  209. #include "generated-normal-tests"
  210. #undef REGISTER_TEST
  211. #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__, NULL)
  212. #include "generated-compulsory-tests"
  213. /* Self-consistency check: make sure no two tests
  214. have the same key or name. */
  215. keys = btree_new(btree_strcmp);
  216. names = btree_new(btree_strcmp);
  217. list_for_each(&compulsory_tests, i, list) {
  218. if (!btree_insert(keys, i->key))
  219. errx(1, "BUG: Duplicate test key '%s'", i->key);
  220. if (!btree_insert(keys, i->name))
  221. errx(1, "BUG: Duplicate test name '%s'", i->name);
  222. }
  223. list_for_each(&normal_tests, i, list) {
  224. if (!btree_insert(keys, i->key))
  225. errx(1, "BUG: Duplicate test key '%s'", i->key);
  226. if (!btree_insert(keys, i->name))
  227. errx(1, "BUG: Duplicate test name '%s'", i->name);
  228. }
  229. btree_delete(keys);
  230. btree_delete(names);
  231. if (!verbose)
  232. return;
  233. printf("\nCompulsory Tests\n");
  234. list_for_each(&compulsory_tests, i, list) {
  235. printf("%s depends on %u others\n", i->name, i->num_depends);
  236. if (!list_empty(&i->dependencies)) {
  237. const struct dependent *d;
  238. printf("These depend on us:\n");
  239. list_for_each(&i->dependencies, d, node)
  240. printf("\t%s\n", d->dependent->name);
  241. }
  242. }
  243. printf("\nNormal Tests\n");
  244. list_for_each(&normal_tests, i, list) {
  245. printf("%s depends on %u others\n", i->name, i->num_depends);
  246. if (!list_empty(&i->dependencies)) {
  247. const struct dependent *d;
  248. printf("These depend on us:\n");
  249. list_for_each(&i->dependencies, d, node)
  250. printf("\t%s\n", d->dependent->name);
  251. }
  252. }
  253. }
  254. static struct ccanlint *find_test(const char *key)
  255. {
  256. struct ccanlint *i;
  257. list_for_each(&compulsory_tests, i, list)
  258. if (streq(i->key, key))
  259. return i;
  260. list_for_each(&normal_tests, i, list)
  261. if (streq(i->key, key))
  262. return i;
  263. return NULL;
  264. }
  265. static void keep_test(const char *testname)
  266. {
  267. struct ccanlint *i = find_test(testname);
  268. if (!i)
  269. errx(1, "No test %s to --keep", testname);
  270. i->keep_results = true;
  271. }
  272. static void print_tests(struct list_head *tests, const char *type)
  273. {
  274. struct ccanlint *i;
  275. printf("%s tests:\n", type);
  276. /* This makes them print in topological order. */
  277. while ((i = get_next_test(tests)) != NULL) {
  278. const struct dependent *d;
  279. printf(" %-25s %s\n", i->key, i->name);
  280. list_del(&i->list);
  281. list_for_each(&i->dependencies, d, node)
  282. d->dependent->num_depends--;
  283. }
  284. }
  285. static void list_tests(void)
  286. {
  287. print_tests(&compulsory_tests, "Compulsory");
  288. print_tests(&normal_tests, "Normal");
  289. exit(0);
  290. }
  291. static char *strip(const void *ctx, const char *line)
  292. {
  293. line += strcspn(line, IDENT_CHARS "-");
  294. return talloc_strndup(ctx, line, strspn(line, IDENT_CHARS "-"));
  295. }
  296. static void add_info_fails(struct ccan_file *info)
  297. {
  298. struct doc_section *d;
  299. unsigned int i;
  300. list_for_each(get_ccan_file_docs(info), d, list) {
  301. if (!streq(d->type, "fails"))
  302. continue;
  303. for (i = 0; i < d->num_lines; i++)
  304. btree_insert(info_exclude, strip(info, d->lines[i]));
  305. break;
  306. }
  307. }
  308. int main(int argc, char *argv[])
  309. {
  310. int c;
  311. bool summary = false;
  312. unsigned int score = 0, total_score = 0;
  313. struct manifest *m;
  314. struct ccanlint *i;
  315. const char *prefix = "", *dir = talloc_getcwd(NULL);
  316. init_tests();
  317. cmdline_exclude = btree_new(btree_strcmp);
  318. info_exclude = btree_new(btree_strcmp);
  319. /* I'd love to use long options, but that's not standard. */
  320. /* FIXME: popt ccan package? */
  321. while ((c = getopt(argc, argv, "sd:vnlx:t:k:")) != -1) {
  322. switch (c) {
  323. case 'd':
  324. if (optarg[0] != '/')
  325. dir = talloc_asprintf_append(NULL, "%s/%s",
  326. dir, optarg);
  327. else
  328. dir = optarg;
  329. prefix = talloc_append_string(talloc_basename(NULL,
  330. optarg),
  331. ": ");
  332. break;
  333. case 'l':
  334. list_tests();
  335. case 's':
  336. summary = true;
  337. break;
  338. case 'v':
  339. verbose++;
  340. break;
  341. case 'n':
  342. safe_mode = true;
  343. break;
  344. case 'k':
  345. keep_test(optarg);
  346. break;
  347. case 'x':
  348. btree_insert(cmdline_exclude, optarg);
  349. break;
  350. case 't':
  351. timeout = atoi(optarg);
  352. if (!timeout)
  353. errx(1, "Invalid timeout %s: 1 ms minumum",
  354. optarg);
  355. break;
  356. default:
  357. usage(argv[0]);
  358. }
  359. }
  360. if (optind < argc)
  361. usage(argv[0]);
  362. if (verbose >= 2)
  363. compile_verbose = true;
  364. if (verbose >= 3)
  365. tools_verbose = true;
  366. /* We move into temporary directory, so gcov dumps its files there. */
  367. if (chdir(temp_dir(talloc_autofree_context())) != 0)
  368. err(1, "Error changing to %s temporary dir", temp_dir(NULL));
  369. m = get_manifest(talloc_autofree_context(), dir);
  370. /* Create a symlink from temp dir back to src dir's test directory. */
  371. if (symlink(talloc_asprintf(m, "%s/test", dir),
  372. talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
  373. err(1, "Creating test symlink in %s", temp_dir(NULL));
  374. /* If you don't pass the compulsory tests, you don't even get a score */
  375. if (verbose)
  376. printf("Compulsory tests:\n");
  377. while ((i = get_next_test(&compulsory_tests)) != NULL) {
  378. if (!run_test(i, summary, &score, &total_score, m)) {
  379. errx(1, "%s%s failed", prefix, i->name);
  380. }
  381. }
  382. add_info_fails(m->info_file);
  383. if (verbose)
  384. printf("\nNormal tests:\n");
  385. while ((i = get_next_test(&normal_tests)) != NULL)
  386. run_test(i, summary, &score, &total_score, m);
  387. printf("%sTotal score: %u/%u\n", prefix, score, total_score);
  388. return 0;
  389. }