ccanlint.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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", score->error);
  134. if (!quiet && !score->pass && i->handle)
  135. i->handle(m, score);
  136. }
  137. *running_score += score->score;
  138. *running_total += score->total;
  139. list_del(&i->list);
  140. list_add_tail(&finished_tests, &i->list);
  141. if (!score->pass) {
  142. /* Skip any tests which depend on this one. */
  143. list_for_each(&i->dependencies, d, node) {
  144. if (d->dependent->skip)
  145. continue;
  146. d->dependent->skip = "dependency failed";
  147. d->dependent->skip_fail = true;
  148. }
  149. }
  150. return score->pass;
  151. }
  152. static void register_test(struct list_head *h, struct ccanlint *test)
  153. {
  154. list_add(h, &test->list);
  155. }
  156. /**
  157. * get_next_test - retrieves the next test to be processed
  158. **/
  159. static inline struct ccanlint *get_next_test(struct list_head *test)
  160. {
  161. struct ccanlint *i;
  162. if (list_empty(test))
  163. return NULL;
  164. list_for_each(test, i, list) {
  165. if (i->num_depends == 0)
  166. return i;
  167. }
  168. errx(1, "Can't make process; test dependency cycle");
  169. }
  170. static struct ccanlint *find_test(const char *key)
  171. {
  172. struct ccanlint *i;
  173. list_for_each(&compulsory_tests, i, list)
  174. if (streq(i->key, key))
  175. return i;
  176. list_for_each(&normal_tests, i, list)
  177. if (streq(i->key, key))
  178. return i;
  179. return NULL;
  180. }
  181. #undef REGISTER_TEST
  182. #define REGISTER_TEST(name, ...) extern struct ccanlint name
  183. #include "generated-normal-tests"
  184. #include "generated-compulsory-tests"
  185. static void init_tests(void)
  186. {
  187. struct ccanlint *c;
  188. struct btree *keys, *names;
  189. struct list_head *list;
  190. #undef REGISTER_TEST
  191. #define REGISTER_TEST(name) register_test(&normal_tests, &name)
  192. #include "generated-normal-tests"
  193. #undef REGISTER_TEST
  194. #define REGISTER_TEST(name) register_test(&compulsory_tests, &name)
  195. #include "generated-compulsory-tests"
  196. /* Initialize dependency lists. */
  197. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  198. list_for_each(list, c, list) {
  199. list_head_init(&c->dependencies);
  200. }
  201. }
  202. /* Resolve dependencies. */
  203. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  204. list_for_each(list, c, list) {
  205. char **deps = strsplit(NULL, c->needs, " ", NULL);
  206. unsigned int i;
  207. for (i = 0; deps[i]; i++) {
  208. struct ccanlint *dep;
  209. struct dependent *dchild;
  210. dep = find_test(deps[i]);
  211. if (!dep)
  212. errx(1, "BUG: unknown dep '%s' for %s",
  213. deps[i], c->key);
  214. dchild = talloc(NULL, struct dependent);
  215. dchild->dependent = c;
  216. list_add_tail(&dep->dependencies,
  217. &dchild->node);
  218. c->num_depends++;
  219. }
  220. talloc_free(deps);
  221. }
  222. }
  223. /* Self-consistency check: make sure no two tests
  224. have the same key or name. */
  225. keys = btree_new(btree_strcmp);
  226. names = btree_new(btree_strcmp);
  227. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  228. list_for_each(list, c, list) {
  229. if (!btree_insert(keys, c->key))
  230. errx(1, "BUG: Duplicate test key '%s'",
  231. c->key);
  232. if (!btree_insert(names, c->name))
  233. errx(1, "BUG: Duplicate test name '%s'",
  234. c->name);
  235. }
  236. }
  237. btree_delete(keys);
  238. btree_delete(names);
  239. if (!verbose)
  240. return;
  241. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  242. printf("\%s Tests\n",
  243. list == &compulsory_tests ? "Compulsory" : "Normal");
  244. if (!list_empty(&c->dependencies)) {
  245. const struct dependent *d;
  246. printf("These depend on us:\n");
  247. list_for_each(&c->dependencies, d, node)
  248. printf("\t%s\n", d->dependent->name);
  249. }
  250. }
  251. }
  252. static char *keep_test(const char *testname, void *unused)
  253. {
  254. struct ccanlint *i = find_test(testname);
  255. if (!i)
  256. errx(1, "No test %s to --keep", testname);
  257. i->keep_results = true;
  258. return NULL;
  259. }
  260. static char *skip_test(const char *testname, void *unused)
  261. {
  262. btree_insert(cmdline_exclude, testname);
  263. return NULL;
  264. }
  265. static void print_tests(struct list_head *tests, const char *type)
  266. {
  267. struct ccanlint *i;
  268. printf("%s tests:\n", type);
  269. /* This makes them print in topological order. */
  270. while ((i = get_next_test(tests)) != NULL) {
  271. const struct dependent *d;
  272. printf(" %-25s %s\n", i->key, i->name);
  273. list_del(&i->list);
  274. list_for_each(&i->dependencies, d, node)
  275. d->dependent->num_depends--;
  276. }
  277. }
  278. static char *list_tests(void *arg)
  279. {
  280. print_tests(&compulsory_tests, "Compulsory");
  281. print_tests(&normal_tests, "Normal");
  282. exit(0);
  283. }
  284. static void test_dgraph_vertices(struct list_head *tests, const char *style)
  285. {
  286. const struct ccanlint *i;
  287. list_for_each(tests, i, list) {
  288. /*
  289. * todo: escape labels in case ccanlint test keys have
  290. * characters interpreted as GraphViz syntax.
  291. */
  292. printf("\t\"%p\" [label=\"%s\"%s]\n", i, i->key, style);
  293. }
  294. }
  295. static void test_dgraph_edges(struct list_head *tests)
  296. {
  297. const struct ccanlint *i;
  298. const struct dependent *d;
  299. list_for_each(tests, i, list)
  300. list_for_each(&i->dependencies, d, node)
  301. printf("\t\"%p\" -> \"%p\"\n", d->dependent, i);
  302. }
  303. static char *test_dependency_graph(void *arg)
  304. {
  305. puts("digraph G {");
  306. test_dgraph_vertices(&compulsory_tests, ", style=filled, fillcolor=yellow");
  307. test_dgraph_vertices(&normal_tests, "");
  308. test_dgraph_edges(&compulsory_tests);
  309. test_dgraph_edges(&normal_tests);
  310. puts("}");
  311. exit(0);
  312. }
  313. /* Remove empty lines. */
  314. static char **collapse(char **lines, unsigned int *nump)
  315. {
  316. unsigned int i, j;
  317. for (i = j = 0; lines[i]; i++) {
  318. if (lines[i][0])
  319. lines[j++] = lines[i];
  320. }
  321. if (nump)
  322. *nump = j;
  323. return lines;
  324. }
  325. static void add_info_options(struct ccan_file *info, bool mark_fails)
  326. {
  327. struct doc_section *d;
  328. unsigned int i;
  329. struct ccanlint *test;
  330. list_for_each(get_ccan_file_docs(info), d, list) {
  331. if (!streq(d->type, "ccanlint"))
  332. continue;
  333. for (i = 0; i < d->num_lines; i++) {
  334. char **words = collapse(strsplit(d, d->lines[i], " \t",
  335. NULL), NULL);
  336. if (!words[0])
  337. continue;
  338. if (strncmp(words[0], "//", 2) == 0)
  339. continue;
  340. test = find_test(words[0]);
  341. if (!test) {
  342. warnx("%s: unknown ccanlint test '%s'",
  343. info->fullname, words[0]);
  344. continue;
  345. }
  346. if (!words[1]) {
  347. warnx("%s: no argument to test '%s'",
  348. info->fullname, words[0]);
  349. continue;
  350. }
  351. /* Known failure? */
  352. if (strcasecmp(words[1], "FAIL") == 0) {
  353. if (mark_fails)
  354. btree_insert(info_exclude, words[0]);
  355. } else {
  356. if (!test->takes_options)
  357. warnx("%s: %s doesn't take options",
  358. info->fullname, words[0]);
  359. /* Copy line exactly into options. */
  360. test->options = strstr(d->lines[i], words[0])
  361. + strlen(words[0]);
  362. }
  363. }
  364. }
  365. }
  366. static bool depends_on(struct ccanlint *i, struct ccanlint *target)
  367. {
  368. const struct dependent *d;
  369. if (i == target)
  370. return true;
  371. list_for_each(&i->dependencies, d, node) {
  372. if (depends_on(d->dependent, target))
  373. return true;
  374. }
  375. return false;
  376. }
  377. /* O(N^2), who cares? */
  378. static void skip_unrelated_tests(struct ccanlint *target)
  379. {
  380. struct ccanlint *i;
  381. struct list_head *list;
  382. foreach_ptr(list, &compulsory_tests, &normal_tests)
  383. list_for_each(list, i, list)
  384. if (!depends_on(i, target))
  385. i->skip = "not relevant to target";
  386. }
  387. int main(int argc, char *argv[])
  388. {
  389. bool summary = false;
  390. unsigned int score = 0, total_score = 0;
  391. struct manifest *m;
  392. struct ccanlint *i;
  393. const char *prefix = "";
  394. char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL;
  395. init_tests();
  396. cmdline_exclude = btree_new(btree_strcmp);
  397. info_exclude = btree_new(btree_strcmp);
  398. opt_register_arg("--dir|-d", opt_set_charp, opt_show_charp, &dir,
  399. "use this directory");
  400. opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
  401. "do not compile anything");
  402. opt_register_noarg("-l|--list-tests", list_tests, NULL,
  403. "list tests ccanlint performs (and exit)");
  404. opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
  405. "print dependency graph of tests in Graphviz .dot format");
  406. opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
  407. "keep results of <testname> (can be used multiple times)");
  408. opt_register_noarg("--summary|-s", opt_set_bool, &summary,
  409. "simply give one line summary");
  410. opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
  411. "verbose mode (up to -vvvv)");
  412. opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
  413. "exclude <testname> (can be used multiple times)");
  414. opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
  415. NULL, &timeout,
  416. "ignore (terminate) tests that are slower than this");
  417. opt_register_arg("--target <testname>", opt_set_charp,
  418. NULL, &target,
  419. "only run one test (and its prerequisites)");
  420. opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
  421. "\nA program for checking and guiding development"
  422. " of CCAN modules.",
  423. "This usage message");
  424. opt_parse(&argc, argv, opt_log_stderr_exit);
  425. if (dir[0] != '/')
  426. dir = talloc_asprintf_append(NULL, "%s/%s", base_dir, dir);
  427. while (strends(dir, "/"))
  428. dir[strlen(dir)-1] = '\0';
  429. if (dir != base_dir)
  430. prefix = talloc_append_string(talloc_basename(NULL, dir), ": ");
  431. if (verbose >= 3)
  432. compile_verbose = true;
  433. if (verbose >= 4)
  434. tools_verbose = true;
  435. /* We move into temporary directory, so gcov dumps its files there. */
  436. if (chdir(temp_dir(talloc_autofree_context())) != 0)
  437. err(1, "Error changing to %s temporary dir", temp_dir(NULL));
  438. m = get_manifest(talloc_autofree_context(), dir);
  439. /* Create a symlink from temp dir back to src dir's test directory. */
  440. if (symlink(talloc_asprintf(m, "%s/test", dir),
  441. talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
  442. err(1, "Creating test symlink in %s", temp_dir(NULL));
  443. if (target) {
  444. struct ccanlint *test;
  445. test = find_test(target);
  446. if (!test)
  447. errx(1, "Unknown test to run '%s'", target);
  448. skip_unrelated_tests(test);
  449. }
  450. /* If you don't pass the compulsory tests, you get a score of 0. */
  451. while ((i = get_next_test(&compulsory_tests)) != NULL) {
  452. if (!run_test(i, summary, &score, &total_score, m)) {
  453. printf("%sTotal score: 0/%u\n", prefix, total_score);
  454. errx(1, "%s%s failed", prefix, i->name);
  455. }
  456. }
  457. /* --target overrides known FAIL from _info */
  458. add_info_options(m->info_file, !target);
  459. while ((i = get_next_test(&normal_tests)) != NULL)
  460. run_test(i, summary, &score, &total_score, m);
  461. printf("%sTotal score: %u/%u\n", prefix, score, total_score);
  462. return 0;
  463. }