ccanlint.c 14 KB

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