ccanlint.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. * ccanlint: assorted checks and advice for a ccan package
  3. * Copyright (C) 2008 Rusty Russell, Idris Soule
  4. * Copyright (C) 2010 Rusty Russell, Idris Soule
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc., 51
  18. * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "ccanlint.h"
  21. #include "../tools.h"
  22. #include "../read_config_header.h"
  23. #include <unistd.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <err.h>
  28. #include <ctype.h>
  29. #include <ccan/str/str.h>
  30. #include <ccan/take/take.h>
  31. #include <ccan/opt/opt.h>
  32. #include <ccan/foreach/foreach.h>
  33. #include <ccan/cast/cast.h>
  34. #include <ccan/tlist/tlist.h>
  35. #include <ccan/tal/path/path.h>
  36. #include <ccan/strmap/strmap.h>
  37. struct ccanlint_map {
  38. STRMAP_MEMBERS(struct ccanlint *);
  39. };
  40. int verbose = 0;
  41. static struct ccanlint_map tests;
  42. bool safe_mode = false;
  43. bool keep_results = false;
  44. static bool targeting = false;
  45. static unsigned int timeout;
  46. const char *config_header;
  47. const char *ccan_dir;
  48. #if 0
  49. static void indent_print(const char *string)
  50. {
  51. while (*string) {
  52. unsigned int line = strcspn(string, "\n");
  53. printf("\t%.*s", line, string);
  54. if (string[line] == '\n') {
  55. printf("\n");
  56. line++;
  57. }
  58. string += line;
  59. }
  60. }
  61. #endif
  62. bool ask(const char *question)
  63. {
  64. char reply[80];
  65. printf("%s ", question);
  66. fflush(stdout);
  67. return fgets(reply, sizeof(reply), stdin) != NULL
  68. && toupper(reply[0]) == 'Y';
  69. }
  70. /* Skip, but don't remove. */
  71. static bool skip_test(struct dgraph_node *node, const char *why)
  72. {
  73. struct ccanlint *c = container_of(node, struct ccanlint, node);
  74. c->skip = why;
  75. return true;
  76. }
  77. static const char *dep_failed(struct manifest *m)
  78. {
  79. return "dependency couldn't run";
  80. }
  81. static bool cannot_run(struct dgraph_node *node, void *all)
  82. {
  83. struct ccanlint *c = container_of(node, struct ccanlint, node);
  84. c->can_run = dep_failed;
  85. return true;
  86. }
  87. struct run_info {
  88. bool quiet;
  89. unsigned int score, total;
  90. struct manifest *m;
  91. const char *prefix;
  92. bool pass;
  93. };
  94. static bool run_test(struct dgraph_node *n, struct run_info *run)
  95. {
  96. struct ccanlint *i = container_of(n, struct ccanlint, node);
  97. unsigned int timeleft;
  98. struct score *score;
  99. if (i->done)
  100. return true;
  101. score = tal(run->m, struct score);
  102. list_head_init(&score->per_file_errors);
  103. score->error = NULL;
  104. score->pass = false;
  105. score->score = 0;
  106. score->total = 1;
  107. /* We can see skipped things in two cases:
  108. * (1) _info excluded them (presumably because they fail).
  109. * (2) A prerequisite failed.
  110. */
  111. if (i->skip) {
  112. if (verbose)
  113. printf("%s%s: skipped (%s)\n",
  114. run->prefix, i->name, i->skip);
  115. /* Pass us up to the test which failed, not us. */
  116. score->pass = true;
  117. goto out;
  118. }
  119. if (i->can_run) {
  120. i->skip = i->can_run(run->m);
  121. if (i->skip) {
  122. /* Test doesn't apply, or can't run? That's OK. */
  123. if (verbose > 1)
  124. printf("%s%s: skipped (%s)\n",
  125. run->prefix, i->name, i->skip);
  126. /* Mark our dependencies to skip. */
  127. dgraph_traverse_from(&i->node, cannot_run, NULL);
  128. score->pass = true;
  129. score->total = 0;
  130. goto out;
  131. }
  132. }
  133. timeleft = timeout ? timeout : default_timeout_ms;
  134. i->check(run->m, &timeleft, score);
  135. if (timeout && timeleft == 0) {
  136. i->skip = "timeout";
  137. if (verbose)
  138. printf("%s%s: skipped (%s)\n",
  139. run->prefix, i->name, i->skip);
  140. /* Mark our dependencies to skip. */
  141. dgraph_traverse_from(&i->node, skip_test,
  142. "dependency timed out");
  143. score->pass = true;
  144. score->total = 0;
  145. goto out;
  146. }
  147. assert(score->score <= score->total);
  148. if ((!score->pass && !run->quiet)
  149. || (score->score < score->total && verbose)
  150. || verbose > 1) {
  151. printf("%s%s (%s): %s",
  152. run->prefix, i->name, i->key,
  153. score->pass ? "PASS" : "FAIL");
  154. if (score->total > 1)
  155. printf(" (+%u/%u)", score->score, score->total);
  156. printf("\n");
  157. }
  158. if ((!run->quiet && !score->pass) || verbose) {
  159. if (score->error) {
  160. printf("%s%s", score->error,
  161. strends(score->error, "\n") ? "" : "\n");
  162. }
  163. }
  164. if (!run->quiet && score->score < score->total && i->handle)
  165. i->handle(run->m, score);
  166. if (!score->pass) {
  167. /* Skip any tests which depend on this one. */
  168. dgraph_traverse_from(&i->node, skip_test, "dependency failed");
  169. }
  170. out:
  171. run->score += score->score;
  172. run->total += score->total;
  173. /* FIXME: Free score. */
  174. run->pass &= score->pass;
  175. i->done = true;
  176. if (!score->pass && i->compulsory) {
  177. warnx("%s%s failed", run->prefix, i->name);
  178. run->score = 0;
  179. return false;
  180. }
  181. return true;
  182. }
  183. static void register_test(struct ccanlint *test)
  184. {
  185. if (!strmap_add(&tests, test->key, test))
  186. err(1, "Adding test %s", test->key);
  187. test->options = tal_arr(NULL, char *, 1);
  188. test->options[0] = NULL;
  189. dgraph_init_node(&test->node);
  190. }
  191. static bool get_test(const char *member, struct ccanlint *i,
  192. struct ccanlint **ret)
  193. {
  194. if (tlist_empty(&i->node.edge[DGRAPH_TO])) {
  195. *ret = i;
  196. return false;
  197. }
  198. return true;
  199. }
  200. /**
  201. * get_next_test - retrieves the next test to be processed
  202. **/
  203. static inline struct ccanlint *get_next_test(void)
  204. {
  205. struct ccanlint *i = NULL;
  206. strmap_iterate(&tests, get_test, &i);
  207. if (i)
  208. return i;
  209. if (strmap_empty(&tests))
  210. return NULL;
  211. errx(1, "Can't make process; test dependency cycle");
  212. }
  213. static struct ccanlint *find_test(const char *key)
  214. {
  215. return strmap_get(&tests, key);
  216. }
  217. bool is_excluded(const char *name)
  218. {
  219. return find_test(name)->skip != NULL;
  220. }
  221. static bool init_deps(const char *member, struct ccanlint *c, void *unused)
  222. {
  223. char **deps = tal_strsplit(NULL, c->needs, " ", STR_EMPTY_OK);
  224. unsigned int i;
  225. for (i = 0; deps[i]; i++) {
  226. struct ccanlint *dep;
  227. dep = find_test(deps[i]);
  228. if (!dep)
  229. errx(1, "BUG: unknown dep '%s' for %s",
  230. deps[i], c->key);
  231. dgraph_add_edge(&dep->node, &c->node);
  232. }
  233. tal_free(deps);
  234. return true;
  235. }
  236. static bool check_names(const char *member, struct ccanlint *c,
  237. struct ccanlint_map *names)
  238. {
  239. if (!strmap_add(names, c->name, c))
  240. err(1, "Duplicate name %s", c->name);
  241. return true;
  242. }
  243. static void init_tests(void)
  244. {
  245. struct ccanlint_map names;
  246. struct ccanlint **table;
  247. size_t i, num;
  248. strmap_init(&tests);
  249. table = autodata_get(ccanlint_tests, &num);
  250. for (i = 0; i < num; i++)
  251. register_test(table[i]);
  252. autodata_free(table);
  253. strmap_iterate(&tests, init_deps, NULL);
  254. /* Check for duplicate names. */
  255. strmap_init(&names);
  256. strmap_iterate(&tests, check_names, &names);
  257. strmap_clear(&names);
  258. }
  259. static bool reset_test(struct dgraph_node *node, void *unused)
  260. {
  261. struct ccanlint *c = container_of(node, struct ccanlint, node);
  262. c->skip = NULL;
  263. c->done = false;
  264. return true;
  265. }
  266. static void reset_tests(struct dgraph_node *all)
  267. {
  268. dgraph_traverse_to(all, reset_test, NULL);
  269. }
  270. static bool print_deps(const char *member, struct ccanlint *c, void *unused)
  271. {
  272. if (!tlist_empty(&c->node.edge[DGRAPH_FROM])) {
  273. struct dgraph_edge *e;
  274. printf("These depend on %s:\n", c->key);
  275. dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
  276. struct ccanlint *to = container_of(e->n[DGRAPH_TO],
  277. struct ccanlint,
  278. node);
  279. printf("\t%s\n", to->key);
  280. }
  281. }
  282. return true;
  283. }
  284. static void print_test_depends(void)
  285. {
  286. printf("Tests:\n");
  287. strmap_iterate(&tests, print_deps, NULL);
  288. }
  289. static void show_tmpdir(const char *dir)
  290. {
  291. printf("You can find ccanlint working files in '%s'\n", dir);
  292. }
  293. static char *keep_tests(void *unused)
  294. {
  295. keep_results = true;
  296. /* Don't automatically destroy temporary dir. */
  297. keep_temp_dir();
  298. tal_add_destructor(temp_dir(), show_tmpdir);
  299. return NULL;
  300. }
  301. static bool remove_test(struct dgraph_node *node, const char *why)
  302. {
  303. struct ccanlint *c = container_of(node, struct ccanlint, node);
  304. c->skip = why;
  305. dgraph_clear_node(node);
  306. return true;
  307. }
  308. static char *exclude_test(const char *testname, void *unused)
  309. {
  310. struct ccanlint *i = find_test(testname);
  311. if (!i)
  312. return tal_fmt(NULL, "No test %s to --exclude", testname);
  313. /* Remove this, and everything which depends on it. */
  314. dgraph_traverse_from(&i->node, remove_test, "excluded on command line");
  315. remove_test(&i->node, "excluded on command line");
  316. return NULL;
  317. }
  318. static void skip_test_and_deps(struct ccanlint *c, const char *why)
  319. {
  320. /* Skip this, and everything which depends on us. */
  321. dgraph_traverse_from(&c->node, skip_test, why);
  322. skip_test(&c->node, why);
  323. }
  324. static char *list_tests(void *arg)
  325. {
  326. struct ccanlint *i;
  327. printf("Tests:\n");
  328. /* This makes them print in topological order. */
  329. while ((i = get_next_test()) != NULL) {
  330. printf(" %-25s %s\n", i->key, i->name);
  331. dgraph_clear_node(&i->node);
  332. strmap_del(&tests, i->key, NULL);
  333. }
  334. exit(0);
  335. }
  336. static bool draw_test(const char *member, struct ccanlint *c, const char *style)
  337. {
  338. /*
  339. * todo: escape labels in case ccanlint test keys have
  340. * characters interpreted as GraphViz syntax.
  341. */
  342. printf("\t\"%p\" [label=\"%s\"%s]\n", c, c->key, style);
  343. return true;
  344. }
  345. static void test_dgraph_vertices(const char *style)
  346. {
  347. strmap_iterate(&tests, draw_test, style);
  348. }
  349. static bool draw_edges(const char *member, struct ccanlint *c, void *unused)
  350. {
  351. struct dgraph_edge *e;
  352. dgraph_for_each_edge(&c->node, e, DGRAPH_FROM) {
  353. struct ccanlint *to = container_of(e->n[DGRAPH_TO],
  354. struct ccanlint,
  355. node);
  356. printf("\t\"%p\" -> \"%p\"\n", c->name, to->name);
  357. }
  358. return true;
  359. }
  360. static void test_dgraph_edges(void)
  361. {
  362. strmap_iterate(&tests, draw_edges, NULL);
  363. }
  364. static char *test_dependency_graph(void *arg)
  365. {
  366. puts("digraph G {");
  367. test_dgraph_vertices("");
  368. test_dgraph_edges();
  369. puts("}");
  370. exit(0);
  371. }
  372. static void add_options(struct ccanlint *test, char **options,
  373. unsigned int num_options)
  374. {
  375. unsigned int num;
  376. if (!test->options)
  377. num = 0;
  378. else
  379. /* -1, because last one is NULL. */
  380. num = tal_count(test->options) - 1;
  381. tal_resize(&test->options, num + num_options + 1);
  382. memcpy(&test->options[num], options, (num_options + 1)*sizeof(char *));
  383. }
  384. void add_info_options(struct ccan_file *info)
  385. {
  386. struct doc_section *d;
  387. unsigned int i;
  388. struct ccanlint *test;
  389. list_for_each(get_ccan_file_docs(info), d, list) {
  390. if (!streq(d->type, "ccanlint"))
  391. continue;
  392. for (i = 0; i < d->num_lines; i++) {
  393. char **words = tal_strsplit(d, d->lines[i], " \t",
  394. STR_NO_EMPTY);
  395. if (!words[0])
  396. continue;
  397. if (strncmp(words[0], "//", 2) == 0)
  398. continue;
  399. test = find_test(words[0]);
  400. if (!test) {
  401. warnx("%s: unknown ccanlint test '%s'",
  402. info->fullname, words[0]);
  403. continue;
  404. }
  405. if (!words[1]) {
  406. warnx("%s: no argument to test '%s'",
  407. info->fullname, words[0]);
  408. continue;
  409. }
  410. /* Known failure? */
  411. if (strcasecmp(words[1], "FAIL") == 0) {
  412. if (!targeting)
  413. skip_test_and_deps(test,
  414. "excluded in _info"
  415. " file");
  416. } else {
  417. if (!test->takes_options)
  418. warnx("%s: %s doesn't take options",
  419. info->fullname, words[0]);
  420. add_options(test, words+1, tal_count(words)-1);
  421. }
  422. }
  423. }
  424. }
  425. /* If options are of form "filename:<option>" they only apply to that file */
  426. char **per_file_options(const struct ccanlint *test, struct ccan_file *f)
  427. {
  428. char **ret;
  429. unsigned int i, j = 0;
  430. /* Fast path. */
  431. if (!test->options[0])
  432. return test->options;
  433. ret = tal_arr(f, char *, tal_count(test->options));
  434. for (i = 0; test->options[i]; i++) {
  435. char *optname;
  436. if (!test->options[i] || !strchr(test->options[i], ':')) {
  437. optname = test->options[i];
  438. } else if (strstarts(test->options[i], f->name)
  439. && test->options[i][strlen(f->name)] == ':') {
  440. optname = test->options[i] + strlen(f->name) + 1;
  441. } else
  442. continue;
  443. /* FAIL overrides anything else. */
  444. if (streq(optname, "FAIL")) {
  445. ret = tal_arr(f, char *, 2);
  446. ret[0] = (char *)"FAIL";
  447. ret[1] = NULL;
  448. return ret;
  449. }
  450. ret[j++] = optname;
  451. }
  452. ret[j] = NULL;
  453. /* Shrink it to size so tal_array_length() works as expected. */
  454. tal_resize(&ret, j + 1);
  455. return ret;
  456. }
  457. static char *opt_set_const_charp(const char *arg, const char **p)
  458. {
  459. return opt_set_charp(arg, cast_const2(char **, p));
  460. }
  461. static char *opt_set_target(const char *arg, struct dgraph_node *all)
  462. {
  463. struct ccanlint *t = find_test(arg);
  464. if (!t)
  465. return tal_fmt(NULL, "unknown --target %s", arg);
  466. targeting = true;
  467. dgraph_add_edge(&t->node, all);
  468. return NULL;
  469. }
  470. static bool run_tests(struct dgraph_node *all,
  471. bool summary,
  472. struct manifest *m,
  473. const char *prefix)
  474. {
  475. struct run_info run;
  476. run.quiet = summary;
  477. run.m = m;
  478. run.prefix = prefix;
  479. run.score = run.total = 0;
  480. run.pass = true;
  481. dgraph_traverse_to(all, run_test, &run);
  482. printf("%sTotal score: %u/%u\n", prefix, run.score, run.total);
  483. return run.pass;
  484. }
  485. static bool add_to_all(const char *member, struct ccanlint *c,
  486. struct dgraph_node *all)
  487. {
  488. /* If we're excluded on cmdline, don't add. */
  489. if (!c->skip)
  490. dgraph_add_edge(&c->node, all);
  491. return true;
  492. }
  493. static bool test_module(struct dgraph_node *all,
  494. const char *dir, const char *prefix, bool summary)
  495. {
  496. struct manifest *m = get_manifest(autofree(), dir);
  497. char *testlink = path_join(NULL, temp_dir(), "test");
  498. /* Create a symlink from temp dir back to src dir's
  499. * test directory. */
  500. unlink(testlink);
  501. if (symlink(path_join(m, dir, "test"), testlink) != 0)
  502. err(1, "Creating test symlink in %s", temp_dir());
  503. return run_tests(all, summary, m, prefix);
  504. }
  505. int main(int argc, char *argv[])
  506. {
  507. bool summary = false, pass = true;
  508. unsigned int i;
  509. const char *prefix = "";
  510. char *cwd = path_cwd(NULL), *dir;
  511. struct ccanlint top; /* cannot_run may try to set ->can_run */
  512. const char *override_compiler = NULL, *override_cflags = NULL;
  513. /* Empty graph node to which we attach everything else. */
  514. dgraph_init_node(&top.node);
  515. opt_register_early_noarg("--verbose|-v", opt_inc_intval, &verbose,
  516. "verbose mode (up to -vvvv)");
  517. opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
  518. "do not compile anything");
  519. opt_register_noarg("-l|--list-tests", list_tests, NULL,
  520. "list tests ccanlint performs (and exit)");
  521. opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
  522. "print dependency graph of tests in Graphviz .dot format");
  523. opt_register_noarg("-k|--keep", keep_tests, NULL,
  524. "do not delete ccanlint working files");
  525. opt_register_noarg("--summary|-s", opt_set_bool, &summary,
  526. "simply give one line summary");
  527. opt_register_arg("-x|--exclude <testname>", exclude_test, NULL, NULL,
  528. "exclude <testname> (can be used multiple times)");
  529. opt_register_arg("--timeout <milleseconds>", opt_set_uintval,
  530. NULL, &timeout,
  531. "ignore (terminate) tests that are slower than this");
  532. opt_register_arg("-t|--target <testname>", opt_set_target, NULL,
  533. &top.node,
  534. "only run one test (and its prerequisites)");
  535. opt_register_arg("--compiler <compiler>", opt_set_const_charp,
  536. NULL, &override_compiler, "set the compiler");
  537. opt_register_arg("--cflags <flags>", opt_set_const_charp,
  538. NULL, &override_cflags, "set the compiler flags");
  539. opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
  540. "\nA program for checking and guiding development"
  541. " of CCAN modules.",
  542. "This usage message");
  543. /* Do verbose before anything else... */
  544. opt_early_parse(argc, argv, opt_log_stderr_exit);
  545. /* We move into temporary directory, so gcov dumps its files there. */
  546. if (chdir(temp_dir()) != 0)
  547. err(1, "Error changing to %s temporary dir", temp_dir());
  548. init_tests();
  549. if (verbose >= 3) {
  550. compile_verbose = true;
  551. print_test_depends();
  552. }
  553. if (verbose >= 4)
  554. tools_verbose = true;
  555. opt_parse(&argc, argv, opt_log_stderr_exit);
  556. if (!targeting)
  557. strmap_iterate(&tests, add_to_all, &top.node);
  558. if (argc == 1)
  559. dir = cwd;
  560. else
  561. dir = path_join(NULL, cwd, argv[1]);
  562. ccan_dir = find_ccan_dir(dir);
  563. if (!ccan_dir)
  564. errx(1, "Cannot find ccan/ base directory in %s", dir);
  565. config_header = read_config_header(ccan_dir, verbose > 1);
  566. /* We do this after read_config_header has set compiler & cflags */
  567. if (override_cflags)
  568. cflags = override_cflags;
  569. if (override_compiler)
  570. compiler = override_compiler;
  571. if (argc == 1)
  572. pass = test_module(&top.node, cwd, "", summary);
  573. else {
  574. for (i = 1; i < argc; i++) {
  575. dir = path_canon(NULL,
  576. take(path_join(NULL, cwd, argv[i])));
  577. if (!dir)
  578. err(1, "Cannot get canonical name of '%s'",
  579. argv[i]);
  580. prefix = path_join(NULL, ccan_dir, "ccan");
  581. prefix = path_rel(NULL, take(prefix), dir);
  582. prefix = tal_strcat(NULL, take(prefix), ": ");
  583. pass &= test_module(&top.node, dir, prefix, summary);
  584. reset_tests(&top.node);
  585. }
  586. }
  587. return pass ? 0 : 1;
  588. }