ccanlint.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 <unistd.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <err.h>
  27. #include <ctype.h>
  28. #include <ccan/btree/btree.h>
  29. #include <ccan/str/str.h>
  30. #include <ccan/str_talloc/str_talloc.h>
  31. #include <ccan/talloc/talloc.h>
  32. #include <ccan/opt/opt.h>
  33. #include <ccan/foreach/foreach.h>
  34. int verbose = 0;
  35. static LIST_HEAD(compulsory_tests);
  36. static LIST_HEAD(normal_tests);
  37. static LIST_HEAD(finished_tests);
  38. bool safe_mode = false;
  39. static struct btree *cmdline_exclude;
  40. static struct btree *info_exclude;
  41. static unsigned int timeout;
  42. #if 0
  43. static void indent_print(const char *string)
  44. {
  45. while (*string) {
  46. unsigned int line = strcspn(string, "\n");
  47. printf("\t%.*s", line, string);
  48. if (string[line] == '\n') {
  49. printf("\n");
  50. line++;
  51. }
  52. string += line;
  53. }
  54. }
  55. #endif
  56. bool ask(const char *question)
  57. {
  58. char reply[80];
  59. printf("%s ", question);
  60. fflush(stdout);
  61. return fgets(reply, sizeof(reply), stdin) != NULL
  62. && toupper(reply[0]) == 'Y';
  63. }
  64. static const char *should_skip(struct manifest *m, struct ccanlint *i)
  65. {
  66. if (btree_lookup(cmdline_exclude, i->key))
  67. return "excluded on command line";
  68. if (btree_lookup(info_exclude, i->key))
  69. return "excluded in _info file";
  70. if (i->skip)
  71. return i->skip;
  72. if (i->skip_fail)
  73. return "dependency failed";
  74. if (i->can_run)
  75. return i->can_run(m);
  76. return NULL;
  77. }
  78. static bool run_test(struct ccanlint *i,
  79. bool quiet,
  80. unsigned int *running_score,
  81. unsigned int *running_total,
  82. struct manifest *m)
  83. {
  84. unsigned int timeleft;
  85. const struct dependent *d;
  86. const char *skip;
  87. struct score *score;
  88. //one less test to run through
  89. list_for_each(&i->dependencies, d, node)
  90. d->dependent->num_depends--;
  91. score = talloc(m, struct score);
  92. list_head_init(&score->per_file_errors);
  93. score->error = NULL;
  94. score->pass = false;
  95. score->score = 0;
  96. score->total = 1;
  97. skip = should_skip(m, i);
  98. if (skip) {
  99. skip:
  100. if (verbose && !streq(skip, "not relevant to target"))
  101. printf("%s: skipped (%s)\n", i->name, skip);
  102. /* If we're skipping this because a prereq failed, we fail:
  103. * count it as a score of 1. */
  104. if (i->skip_fail)
  105. (*running_total)++;
  106. list_del(&i->list);
  107. list_add_tail(&finished_tests, &i->list);
  108. list_for_each(&i->dependencies, d, node) {
  109. if (d->dependent->skip)
  110. continue;
  111. d->dependent->skip = "dependency was skipped";
  112. d->dependent->skip_fail = i->skip_fail;
  113. }
  114. return i->skip_fail ? false : true;
  115. }
  116. timeleft = timeout ? timeout : default_timeout_ms;
  117. i->check(m, i->keep_results, &timeleft, score);
  118. if (timeout && timeleft == 0) {
  119. skip = "timeout";
  120. goto skip;
  121. }
  122. assert(score->score <= score->total);
  123. if ((!score->pass && !quiet)
  124. || (score->score < score->total && verbose)
  125. || verbose > 1) {
  126. printf("%s (%s): %s", i->name, i->key, score->pass ? "PASS" : "FAIL");
  127. if (score->total > 1)
  128. printf(" (+%u/%u)", score->score, score->total);
  129. printf("\n");
  130. }
  131. if ((!quiet && !score->pass) || verbose) {
  132. if (score->error) {
  133. printf("%s%s", score->error,
  134. strends(score->error, "\n") ? "" : "\n");
  135. }
  136. if (!quiet && !score->pass && i->handle)
  137. i->handle(m, score);
  138. }
  139. *running_score += score->score;
  140. *running_total += score->total;
  141. list_del(&i->list);
  142. list_add_tail(&finished_tests, &i->list);
  143. if (!score->pass) {
  144. /* Skip any tests which depend on this one. */
  145. list_for_each(&i->dependencies, d, node) {
  146. if (d->dependent->skip)
  147. continue;
  148. d->dependent->skip = "dependency failed";
  149. d->dependent->skip_fail = true;
  150. }
  151. }
  152. return score->pass;
  153. }
  154. static void register_test(struct list_head *h, struct ccanlint *test)
  155. {
  156. list_add(h, &test->list);
  157. }
  158. /**
  159. * get_next_test - retrieves the next test to be processed
  160. **/
  161. static inline struct ccanlint *get_next_test(struct list_head *test)
  162. {
  163. struct ccanlint *i;
  164. if (list_empty(test))
  165. return NULL;
  166. list_for_each(test, i, list) {
  167. if (i->num_depends == 0)
  168. return i;
  169. }
  170. errx(1, "Can't make process; test dependency cycle");
  171. }
  172. static struct ccanlint *find_test(const char *key)
  173. {
  174. struct ccanlint *i;
  175. list_for_each(&compulsory_tests, i, list)
  176. if (streq(i->key, key))
  177. return i;
  178. list_for_each(&normal_tests, i, list)
  179. if (streq(i->key, key))
  180. return i;
  181. return NULL;
  182. }
  183. #undef REGISTER_TEST
  184. #define REGISTER_TEST(name, ...) extern struct ccanlint name
  185. #include "generated-normal-tests"
  186. #include "generated-compulsory-tests"
  187. static void init_tests(void)
  188. {
  189. struct ccanlint *c;
  190. struct btree *keys, *names;
  191. struct list_head *list;
  192. #undef REGISTER_TEST
  193. #define REGISTER_TEST(name) register_test(&normal_tests, &name)
  194. #include "generated-normal-tests"
  195. #undef REGISTER_TEST
  196. #define REGISTER_TEST(name) register_test(&compulsory_tests, &name)
  197. #include "generated-compulsory-tests"
  198. /* Initialize dependency lists. */
  199. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  200. list_for_each(list, c, list) {
  201. list_head_init(&c->dependencies);
  202. }
  203. }
  204. /* Resolve dependencies. */
  205. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  206. list_for_each(list, c, list) {
  207. char **deps = strsplit(NULL, c->needs, " ");
  208. unsigned int i;
  209. for (i = 0; deps[i]; i++) {
  210. struct ccanlint *dep;
  211. struct dependent *dchild;
  212. dep = find_test(deps[i]);
  213. if (!dep)
  214. errx(1, "BUG: unknown dep '%s' for %s",
  215. deps[i], c->key);
  216. dchild = talloc(NULL, struct dependent);
  217. dchild->dependent = c;
  218. list_add_tail(&dep->dependencies,
  219. &dchild->node);
  220. c->num_depends++;
  221. }
  222. talloc_free(deps);
  223. }
  224. }
  225. /* Self-consistency check: make sure no two tests
  226. have the same key or name. */
  227. keys = btree_new(btree_strcmp);
  228. names = btree_new(btree_strcmp);
  229. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  230. list_for_each(list, c, list) {
  231. if (!btree_insert(keys, c->key))
  232. errx(1, "BUG: Duplicate test key '%s'",
  233. c->key);
  234. if (!btree_insert(names, c->name))
  235. errx(1, "BUG: Duplicate test name '%s'",
  236. c->name);
  237. }
  238. }
  239. btree_delete(keys);
  240. btree_delete(names);
  241. if (!verbose)
  242. return;
  243. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  244. printf("\%s Tests\n",
  245. list == &compulsory_tests ? "Compulsory" : "Normal");
  246. if (!list_empty(&c->dependencies)) {
  247. const struct dependent *d;
  248. printf("These depend on us:\n");
  249. list_for_each(&c->dependencies, d, node)
  250. printf("\t%s\n", d->dependent->name);
  251. }
  252. }
  253. }
  254. static int show_tmpdir(char *dir)
  255. {
  256. printf("You can find ccanlint working files in '%s'\n", dir);
  257. return 0;
  258. }
  259. static char *keep_test(const char *testname, void *unused)
  260. {
  261. struct ccanlint *i;
  262. if (streq(testname, "all")) {
  263. struct list_head *list;
  264. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  265. list_for_each(list, i, list)
  266. i->keep_results = true;
  267. }
  268. } else {
  269. i = find_test(testname);
  270. if (!i)
  271. errx(1, "No test %s to --keep", testname);
  272. i->keep_results = true;
  273. }
  274. /* Don't automatically destroy temporary dir. */
  275. talloc_set_destructor(temp_dir(NULL), show_tmpdir);
  276. return NULL;
  277. }
  278. static char *skip_test(const char *testname, void *unused)
  279. {
  280. btree_insert(cmdline_exclude, testname);
  281. return NULL;
  282. }
  283. static void print_tests(struct list_head *tests, const char *type)
  284. {
  285. struct ccanlint *i;
  286. printf("%s tests:\n", type);
  287. /* This makes them print in topological order. */
  288. while ((i = get_next_test(tests)) != NULL) {
  289. const struct dependent *d;
  290. printf(" %-25s %s\n", i->key, i->name);
  291. list_del(&i->list);
  292. list_for_each(&i->dependencies, d, node)
  293. d->dependent->num_depends--;
  294. }
  295. }
  296. static char *list_tests(void *arg)
  297. {
  298. print_tests(&compulsory_tests, "Compulsory");
  299. print_tests(&normal_tests, "Normal");
  300. exit(0);
  301. }
  302. static void test_dgraph_vertices(struct list_head *tests, const char *style)
  303. {
  304. const struct ccanlint *i;
  305. list_for_each(tests, i, list) {
  306. /*
  307. * todo: escape labels in case ccanlint test keys have
  308. * characters interpreted as GraphViz syntax.
  309. */
  310. printf("\t\"%p\" [label=\"%s\"%s]\n", i, i->key, style);
  311. }
  312. }
  313. static void test_dgraph_edges(struct list_head *tests)
  314. {
  315. const struct ccanlint *i;
  316. const struct dependent *d;
  317. list_for_each(tests, i, list)
  318. list_for_each(&i->dependencies, d, node)
  319. printf("\t\"%p\" -> \"%p\"\n", d->dependent, i);
  320. }
  321. static char *test_dependency_graph(void *arg)
  322. {
  323. puts("digraph G {");
  324. test_dgraph_vertices(&compulsory_tests, ", style=filled, fillcolor=yellow");
  325. test_dgraph_vertices(&normal_tests, "");
  326. test_dgraph_edges(&compulsory_tests);
  327. test_dgraph_edges(&normal_tests);
  328. puts("}");
  329. exit(0);
  330. }
  331. /* Remove empty lines. */
  332. static char **collapse(char **lines, unsigned int *nump)
  333. {
  334. unsigned int i, j;
  335. for (i = j = 0; lines[i]; i++) {
  336. if (lines[i][0])
  337. lines[j++] = lines[i];
  338. }
  339. if (nump)
  340. *nump = j;
  341. return lines;
  342. }
  343. static void add_info_options(struct ccan_file *info, bool mark_fails)
  344. {
  345. struct doc_section *d;
  346. unsigned int i;
  347. struct ccanlint *test;
  348. list_for_each(get_ccan_file_docs(info), d, list) {
  349. if (!streq(d->type, "ccanlint"))
  350. continue;
  351. for (i = 0; i < d->num_lines; i++) {
  352. char **words = collapse(strsplit(d, d->lines[i], " \t"),
  353. NULL);
  354. if (!words[0])
  355. continue;
  356. if (strncmp(words[0], "//", 2) == 0)
  357. continue;
  358. test = find_test(words[0]);
  359. if (!test) {
  360. warnx("%s: unknown ccanlint test '%s'",
  361. info->fullname, words[0]);
  362. continue;
  363. }
  364. if (!words[1]) {
  365. warnx("%s: no argument to test '%s'",
  366. info->fullname, words[0]);
  367. continue;
  368. }
  369. /* Known failure? */
  370. if (strcasecmp(words[1], "FAIL") == 0) {
  371. if (mark_fails)
  372. btree_insert(info_exclude, words[0]);
  373. } else {
  374. if (!test->takes_options)
  375. warnx("%s: %s doesn't take options",
  376. info->fullname, words[0]);
  377. /* Copy line exactly into options. */
  378. test->options = strstr(d->lines[i], words[0])
  379. + strlen(words[0]);
  380. }
  381. }
  382. }
  383. }
  384. static bool depends_on(struct ccanlint *i, struct ccanlint *target)
  385. {
  386. const struct dependent *d;
  387. if (i == target)
  388. return true;
  389. list_for_each(&i->dependencies, d, node) {
  390. if (depends_on(d->dependent, target))
  391. return true;
  392. }
  393. return false;
  394. }
  395. /* O(N^2), who cares? */
  396. static void skip_unrelated_tests(struct ccanlint *target)
  397. {
  398. struct ccanlint *i;
  399. struct list_head *list;
  400. foreach_ptr(list, &compulsory_tests, &normal_tests)
  401. list_for_each(list, i, list)
  402. if (!depends_on(i, target))
  403. i->skip = "not relevant to target";
  404. }
  405. int main(int argc, char *argv[])
  406. {
  407. bool summary = false;
  408. unsigned int score = 0, total_score = 0;
  409. struct manifest *m;
  410. struct ccanlint *i;
  411. const char *prefix = "";
  412. char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL;
  413. init_tests();
  414. cmdline_exclude = btree_new(btree_strcmp);
  415. info_exclude = btree_new(btree_strcmp);
  416. opt_register_arg("--dir|-d", opt_set_charp, opt_show_charp, &dir,
  417. "use this directory");
  418. opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
  419. "do not compile anything");
  420. opt_register_noarg("-l|--list-tests", list_tests, NULL,
  421. "list tests ccanlint performs (and exit)");
  422. opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
  423. "print dependency graph of tests in Graphviz .dot format");
  424. opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
  425. "keep results of <testname>"
  426. " (can be used multiple times, or 'all')");
  427. opt_register_noarg("--summary|-s", opt_set_bool, &summary,
  428. "simply give one line summary");
  429. opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
  430. "verbose mode (up to -vvvv)");
  431. opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
  432. "exclude <testname> (can be used multiple times)");
  433. opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
  434. NULL, &timeout,
  435. "ignore (terminate) tests that are slower than this");
  436. opt_register_arg("--target <testname>", opt_set_charp,
  437. NULL, &target,
  438. "only run one test (and its prerequisites)");
  439. opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
  440. "\nA program for checking and guiding development"
  441. " of CCAN modules.",
  442. "This usage message");
  443. /* We move into temporary directory, so gcov dumps its files there. */
  444. if (chdir(temp_dir(talloc_autofree_context())) != 0)
  445. err(1, "Error changing to %s temporary dir", temp_dir(NULL));
  446. opt_parse(&argc, argv, opt_log_stderr_exit);
  447. if (dir[0] != '/')
  448. dir = talloc_asprintf_append(NULL, "%s/%s", base_dir, dir);
  449. while (strends(dir, "/"))
  450. dir[strlen(dir)-1] = '\0';
  451. if (dir != base_dir)
  452. prefix = talloc_append_string(talloc_basename(NULL, dir), ": ");
  453. if (verbose >= 3)
  454. compile_verbose = true;
  455. if (verbose >= 4)
  456. tools_verbose = true;
  457. m = get_manifest(talloc_autofree_context(), dir);
  458. /* Create a symlink from temp dir back to src dir's test directory. */
  459. if (symlink(talloc_asprintf(m, "%s/test", dir),
  460. talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
  461. err(1, "Creating test symlink in %s", temp_dir(NULL));
  462. if (target) {
  463. struct ccanlint *test;
  464. test = find_test(target);
  465. if (!test)
  466. errx(1, "Unknown test to run '%s'", target);
  467. skip_unrelated_tests(test);
  468. }
  469. /* If you don't pass the compulsory tests, you get a score of 0. */
  470. while ((i = get_next_test(&compulsory_tests)) != NULL) {
  471. if (!run_test(i, summary, &score, &total_score, m)) {
  472. printf("%sTotal score: 0/%u\n", prefix, total_score);
  473. errx(1, "%s%s failed", prefix, i->name);
  474. }
  475. }
  476. /* --target overrides known FAIL from _info */
  477. add_info_options(m->info_file, !target);
  478. while ((i = get_next_test(&normal_tests)) != NULL)
  479. run_test(i, summary, &score, &total_score, m);
  480. printf("%sTotal score: %u/%u\n", prefix, score, total_score);
  481. return 0;
  482. }