ccanlint.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. #include <ccan/grab_file/grab_file.h>
  35. #include <ccan/cast/cast.h>
  36. int verbose = 0;
  37. static LIST_HEAD(compulsory_tests);
  38. static LIST_HEAD(normal_tests);
  39. static LIST_HEAD(finished_tests);
  40. bool safe_mode = false;
  41. static struct btree *cmdline_exclude;
  42. static struct btree *info_exclude;
  43. static unsigned int timeout;
  44. /* These are overridden at runtime if we can find config.h */
  45. const char *compiler = NULL;
  46. const char *cflags = NULL;
  47. const char *config_header;
  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. static const char *should_skip(struct manifest *m, struct ccanlint *i)
  71. {
  72. if (btree_lookup(cmdline_exclude, i->key))
  73. return "excluded on command line";
  74. if (btree_lookup(info_exclude, i->key))
  75. return "excluded in _info file";
  76. if (i->skip)
  77. return i->skip;
  78. if (i->skip_fail)
  79. return "dependency failed";
  80. if (i->can_run)
  81. return i->can_run(m);
  82. return NULL;
  83. }
  84. static bool run_test(struct ccanlint *i,
  85. bool quiet,
  86. unsigned int *running_score,
  87. unsigned int *running_total,
  88. struct manifest *m)
  89. {
  90. unsigned int timeleft;
  91. const struct dependent *d;
  92. const char *skip;
  93. struct score *score;
  94. //one less test to run through
  95. list_for_each(&i->dependencies, d, node)
  96. d->dependent->num_depends--;
  97. score = talloc(m, struct score);
  98. list_head_init(&score->per_file_errors);
  99. score->error = NULL;
  100. score->pass = false;
  101. score->score = 0;
  102. score->total = 1;
  103. skip = should_skip(m, i);
  104. if (skip) {
  105. skip:
  106. if (verbose && !streq(skip, "not relevant to target"))
  107. printf("%s: skipped (%s)\n", i->name, skip);
  108. /* If we're skipping this because a prereq failed, we fail:
  109. * count it as a score of 1. */
  110. if (i->skip_fail)
  111. (*running_total)++;
  112. list_del(&i->list);
  113. list_add_tail(&finished_tests, &i->list);
  114. list_for_each(&i->dependencies, d, node) {
  115. if (d->dependent->skip)
  116. continue;
  117. d->dependent->skip = "dependency was skipped";
  118. d->dependent->skip_fail = i->skip_fail;
  119. }
  120. return i->skip_fail ? false : true;
  121. }
  122. timeleft = timeout ? timeout : default_timeout_ms;
  123. i->check(m, i->keep_results, &timeleft, score);
  124. if (timeout && timeleft == 0) {
  125. skip = "timeout";
  126. goto skip;
  127. }
  128. assert(score->score <= score->total);
  129. if ((!score->pass && !quiet)
  130. || (score->score < score->total && verbose)
  131. || verbose > 1) {
  132. printf("%s (%s): %s", i->name, i->key, score->pass ? "PASS" : "FAIL");
  133. if (score->total > 1)
  134. printf(" (+%u/%u)", score->score, score->total);
  135. printf("\n");
  136. }
  137. if ((!quiet && !score->pass) || verbose) {
  138. if (score->error) {
  139. printf("%s%s", score->error,
  140. strends(score->error, "\n") ? "" : "\n");
  141. }
  142. }
  143. if (!quiet && score->score < score->total && i->handle)
  144. i->handle(m, score);
  145. *running_score += score->score;
  146. *running_total += score->total;
  147. list_del(&i->list);
  148. list_add_tail(&finished_tests, &i->list);
  149. if (!score->pass) {
  150. /* Skip any tests which depend on this one. */
  151. list_for_each(&i->dependencies, d, node) {
  152. if (d->dependent->skip)
  153. continue;
  154. d->dependent->skip = "dependency failed";
  155. d->dependent->skip_fail = true;
  156. }
  157. }
  158. return score->pass;
  159. }
  160. static void register_test(struct list_head *h, struct ccanlint *test)
  161. {
  162. list_add(h, &test->list);
  163. }
  164. /**
  165. * get_next_test - retrieves the next test to be processed
  166. **/
  167. static inline struct ccanlint *get_next_test(struct list_head *test)
  168. {
  169. struct ccanlint *i;
  170. if (list_empty(test))
  171. return NULL;
  172. list_for_each(test, i, list) {
  173. if (i->num_depends == 0)
  174. return i;
  175. }
  176. errx(1, "Can't make process; test dependency cycle");
  177. }
  178. static struct ccanlint *find_test(const char *key)
  179. {
  180. struct ccanlint *i;
  181. list_for_each(&compulsory_tests, i, list)
  182. if (streq(i->key, key))
  183. return i;
  184. list_for_each(&normal_tests, i, list)
  185. if (streq(i->key, key))
  186. return i;
  187. return NULL;
  188. }
  189. #undef REGISTER_TEST
  190. #define REGISTER_TEST(name, ...) extern struct ccanlint name
  191. #include "generated-normal-tests"
  192. #include "generated-compulsory-tests"
  193. static void init_tests(void)
  194. {
  195. struct ccanlint *c;
  196. struct btree *keys, *names;
  197. struct list_head *list;
  198. #undef REGISTER_TEST
  199. #define REGISTER_TEST(name) register_test(&normal_tests, &name)
  200. #include "generated-normal-tests"
  201. #undef REGISTER_TEST
  202. #define REGISTER_TEST(name) register_test(&compulsory_tests, &name)
  203. #include "generated-compulsory-tests"
  204. /* Initialize dependency lists. */
  205. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  206. list_for_each(list, c, list) {
  207. list_head_init(&c->dependencies);
  208. }
  209. }
  210. /* Resolve dependencies. */
  211. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  212. list_for_each(list, c, list) {
  213. char **deps = strsplit(NULL, c->needs, " ");
  214. unsigned int i;
  215. for (i = 0; deps[i]; i++) {
  216. struct ccanlint *dep;
  217. struct dependent *dchild;
  218. dep = find_test(deps[i]);
  219. if (!dep)
  220. errx(1, "BUG: unknown dep '%s' for %s",
  221. deps[i], c->key);
  222. dchild = talloc(NULL, struct dependent);
  223. dchild->dependent = c;
  224. list_add_tail(&dep->dependencies,
  225. &dchild->node);
  226. c->num_depends++;
  227. }
  228. talloc_free(deps);
  229. }
  230. }
  231. /* Self-consistency check: make sure no two tests
  232. have the same key or name. */
  233. keys = btree_new(btree_strcmp);
  234. names = btree_new(btree_strcmp);
  235. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  236. list_for_each(list, c, list) {
  237. if (!btree_insert(keys, c->key))
  238. errx(1, "BUG: Duplicate test key '%s'",
  239. c->key);
  240. if (!btree_insert(names, c->name))
  241. errx(1, "BUG: Duplicate test name '%s'",
  242. c->name);
  243. }
  244. }
  245. btree_delete(keys);
  246. btree_delete(names);
  247. }
  248. static void print_test_depends(void)
  249. {
  250. struct list_head *list;
  251. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  252. struct ccanlint *c;
  253. printf("\%s Tests\n",
  254. list == &compulsory_tests ? "Compulsory" : "Normal");
  255. list_for_each(list, c, list) {
  256. if (!list_empty(&c->dependencies)) {
  257. const struct dependent *d;
  258. printf("These depend on %s:\n", c->key);
  259. list_for_each(&c->dependencies, d, node)
  260. printf("\t%s\n", d->dependent->key);
  261. }
  262. }
  263. }
  264. }
  265. static int show_tmpdir(const char *dir)
  266. {
  267. printf("You can find ccanlint working files in '%s'\n", dir);
  268. return 0;
  269. }
  270. static char *keep_test(const char *testname, void *unused)
  271. {
  272. struct ccanlint *i;
  273. if (streq(testname, "all")) {
  274. struct list_head *list;
  275. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  276. list_for_each(list, i, list)
  277. i->keep_results = true;
  278. }
  279. } else {
  280. i = find_test(testname);
  281. if (!i)
  282. errx(1, "No test %s to --keep", testname);
  283. i->keep_results = true;
  284. }
  285. /* Don't automatically destroy temporary dir. */
  286. talloc_set_destructor(temp_dir(NULL), show_tmpdir);
  287. return NULL;
  288. }
  289. static char *skip_test(const char *testname, void *unused)
  290. {
  291. btree_insert(cmdline_exclude, testname);
  292. return NULL;
  293. }
  294. static void print_tests(struct list_head *tests, const char *type)
  295. {
  296. struct ccanlint *i;
  297. printf("%s tests:\n", type);
  298. /* This makes them print in topological order. */
  299. while ((i = get_next_test(tests)) != NULL) {
  300. const struct dependent *d;
  301. printf(" %-25s %s\n", i->key, i->name);
  302. list_del(&i->list);
  303. list_for_each(&i->dependencies, d, node)
  304. d->dependent->num_depends--;
  305. }
  306. }
  307. static char *list_tests(void *arg)
  308. {
  309. print_tests(&compulsory_tests, "Compulsory");
  310. print_tests(&normal_tests, "Normal");
  311. exit(0);
  312. }
  313. static void test_dgraph_vertices(struct list_head *tests, const char *style)
  314. {
  315. const struct ccanlint *i;
  316. list_for_each(tests, i, list) {
  317. /*
  318. * todo: escape labels in case ccanlint test keys have
  319. * characters interpreted as GraphViz syntax.
  320. */
  321. printf("\t\"%p\" [label=\"%s\"%s]\n", i, i->key, style);
  322. }
  323. }
  324. static void test_dgraph_edges(struct list_head *tests)
  325. {
  326. const struct ccanlint *i;
  327. const struct dependent *d;
  328. list_for_each(tests, i, list)
  329. list_for_each(&i->dependencies, d, node)
  330. printf("\t\"%p\" -> \"%p\"\n", d->dependent, i);
  331. }
  332. static char *test_dependency_graph(void *arg)
  333. {
  334. puts("digraph G {");
  335. test_dgraph_vertices(&compulsory_tests, ", style=filled, fillcolor=yellow");
  336. test_dgraph_vertices(&normal_tests, "");
  337. test_dgraph_edges(&compulsory_tests);
  338. test_dgraph_edges(&normal_tests);
  339. puts("}");
  340. exit(0);
  341. }
  342. /* Remove empty lines. */
  343. static char **collapse(char **lines, unsigned int *nump)
  344. {
  345. unsigned int i, j;
  346. for (i = j = 0; lines[i]; i++) {
  347. if (lines[i][0])
  348. lines[j++] = lines[i];
  349. }
  350. if (nump)
  351. *nump = j;
  352. return lines;
  353. }
  354. static void add_info_options(struct ccan_file *info, bool mark_fails)
  355. {
  356. struct doc_section *d;
  357. unsigned int i;
  358. struct ccanlint *test;
  359. list_for_each(get_ccan_file_docs(info), d, list) {
  360. if (!streq(d->type, "ccanlint"))
  361. continue;
  362. for (i = 0; i < d->num_lines; i++) {
  363. char **words = collapse(strsplit(d, d->lines[i], " \t"),
  364. NULL);
  365. if (!words[0])
  366. continue;
  367. if (strncmp(words[0], "//", 2) == 0)
  368. continue;
  369. test = find_test(words[0]);
  370. if (!test) {
  371. warnx("%s: unknown ccanlint test '%s'",
  372. info->fullname, words[0]);
  373. continue;
  374. }
  375. if (!words[1]) {
  376. warnx("%s: no argument to test '%s'",
  377. info->fullname, words[0]);
  378. continue;
  379. }
  380. /* Known failure? */
  381. if (strcasecmp(words[1], "FAIL") == 0) {
  382. if (mark_fails)
  383. btree_insert(info_exclude, words[0]);
  384. } else {
  385. if (!test->takes_options)
  386. warnx("%s: %s doesn't take options",
  387. info->fullname, words[0]);
  388. /* Copy line exactly into options. */
  389. test->options = strstr(d->lines[i], words[0])
  390. + strlen(words[0]);
  391. }
  392. }
  393. }
  394. }
  395. static bool depends_on(struct ccanlint *i, struct ccanlint *target)
  396. {
  397. const struct dependent *d;
  398. if (i == target)
  399. return true;
  400. list_for_each(&i->dependencies, d, node) {
  401. if (depends_on(d->dependent, target))
  402. return true;
  403. }
  404. return false;
  405. }
  406. /* O(N^2), who cares? */
  407. static void skip_unrelated_tests(struct ccanlint *target)
  408. {
  409. struct ccanlint *i;
  410. struct list_head *list;
  411. foreach_ptr(list, &compulsory_tests, &normal_tests)
  412. list_for_each(list, i, list)
  413. if (!depends_on(i, target))
  414. i->skip = "not relevant to target";
  415. }
  416. static char *demangle_string(char *string)
  417. {
  418. unsigned int i;
  419. const char mapfrom[] = "abfnrtv";
  420. const char mapto[] = "\a\b\f\n\r\t\v";
  421. if (!strchr(string, '"'))
  422. return NULL;
  423. string = strchr(string, '"') + 1;
  424. if (!strrchr(string, '"'))
  425. return NULL;
  426. *strrchr(string, '"') = '\0';
  427. for (i = 0; i < strlen(string); i++) {
  428. if (string[i] == '\\') {
  429. char repl;
  430. unsigned len = 0;
  431. const char *p = strchr(mapfrom, string[i+1]);
  432. if (p) {
  433. repl = mapto[p - mapfrom];
  434. len = 1;
  435. } else if (strlen(string+i+1) >= 3) {
  436. if (string[i+1] == 'x') {
  437. repl = (string[i+2]-'0')*16
  438. + string[i+3]-'0';
  439. len = 3;
  440. } else if (cisdigit(string[i+1])) {
  441. repl = (string[i+2]-'0')*8*8
  442. + (string[i+3]-'0')*8
  443. + (string[i+4]-'0');
  444. len = 3;
  445. }
  446. }
  447. if (len == 0) {
  448. repl = string[i+1];
  449. len = 1;
  450. }
  451. string[i] = repl;
  452. memmove(string + i + 1, string + i + len + 1,
  453. strlen(string + i + len + 1) + 1);
  454. }
  455. }
  456. return string;
  457. }
  458. static void read_config_header(void)
  459. {
  460. char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
  461. char **lines;
  462. unsigned int i;
  463. config_header = grab_file(NULL, fname, NULL);
  464. if (!config_header) {
  465. talloc_free(fname);
  466. return;
  467. }
  468. lines = strsplit(config_header, config_header, "\n");
  469. for (i = 0; i < talloc_array_length(lines) - 1; i++) {
  470. char *sym;
  471. const char **line = (const char **)&lines[i];
  472. if (!get_token(line, "#"))
  473. continue;
  474. if (!get_token(line, "define"))
  475. continue;
  476. sym = get_symbol_token(lines, line);
  477. if (streq(sym, "CCAN_COMPILER") && !compiler) {
  478. compiler = demangle_string(lines[i]);
  479. if (!compiler)
  480. errx(1, "%s:%u:could not parse CCAN_COMPILER",
  481. fname, i+1);
  482. if (verbose > 1)
  483. printf("%s: compiler set to '%s'\n",
  484. fname, compiler);
  485. } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
  486. cflags = demangle_string(lines[i]);
  487. if (!cflags)
  488. errx(1, "%s:%u:could not parse CCAN_CFLAGS",
  489. fname, i+1);
  490. if (verbose > 1)
  491. printf("%s: compiler flags set to '%s'\n",
  492. fname, cflags);
  493. }
  494. }
  495. if (!compiler)
  496. compiler = CCAN_COMPILER;
  497. if (!cflags)
  498. compiler = CCAN_CFLAGS;
  499. }
  500. static char *opt_set_const_charp(const char *arg, const char **p)
  501. {
  502. return opt_set_charp(arg, cast_const2(char **, p));
  503. }
  504. int main(int argc, char *argv[])
  505. {
  506. bool summary = false, pass = true;
  507. unsigned int score = 0, total_score = 0;
  508. struct manifest *m;
  509. struct ccanlint *i;
  510. const char *prefix = "";
  511. char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL;
  512. init_tests();
  513. cmdline_exclude = btree_new(btree_strcmp);
  514. info_exclude = btree_new(btree_strcmp);
  515. opt_register_arg("--dir|-d", opt_set_charp, opt_show_charp, &dir,
  516. "use this directory");
  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_arg("-k|--keep <testname>", keep_test, NULL, NULL,
  524. "keep results of <testname>"
  525. " (can be used multiple times, or 'all')");
  526. opt_register_noarg("--summary|-s", opt_set_bool, &summary,
  527. "simply give one line summary");
  528. opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
  529. "verbose mode (up to -vvvv)");
  530. opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
  531. "exclude <testname> (can be used multiple times)");
  532. opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
  533. NULL, &timeout,
  534. "ignore (terminate) tests that are slower than this");
  535. opt_register_arg("--target <testname>", opt_set_charp,
  536. NULL, &target,
  537. "only run one test (and its prerequisites)");
  538. opt_register_arg("--compiler <compiler>", opt_set_const_charp,
  539. NULL, &compiler, "set the compiler");
  540. opt_register_arg("--cflags <flags>", opt_set_const_charp,
  541. NULL, &cflags, "set the compiler flags");
  542. opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
  543. "\nA program for checking and guiding development"
  544. " of CCAN modules.",
  545. "This usage message");
  546. /* We move into temporary directory, so gcov dumps its files there. */
  547. if (chdir(temp_dir(talloc_autofree_context())) != 0)
  548. err(1, "Error changing to %s temporary dir", temp_dir(NULL));
  549. opt_parse(&argc, argv, opt_log_stderr_exit);
  550. if (dir[0] != '/')
  551. dir = talloc_asprintf_append(NULL, "%s/%s", base_dir, dir);
  552. while (strends(dir, "/"))
  553. dir[strlen(dir)-1] = '\0';
  554. if (dir != base_dir)
  555. prefix = talloc_append_string(talloc_basename(NULL, dir), ": ");
  556. if (verbose >= 3) {
  557. compile_verbose = true;
  558. print_test_depends();
  559. }
  560. if (verbose >= 4)
  561. tools_verbose = true;
  562. m = get_manifest(talloc_autofree_context(), dir);
  563. read_config_header();
  564. /* Create a symlink from temp dir back to src dir's test directory. */
  565. if (symlink(talloc_asprintf(m, "%s/test", dir),
  566. talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
  567. err(1, "Creating test symlink in %s", temp_dir(NULL));
  568. if (target) {
  569. struct ccanlint *test;
  570. test = find_test(target);
  571. if (!test)
  572. errx(1, "Unknown test to run '%s'", target);
  573. skip_unrelated_tests(test);
  574. }
  575. /* If you don't pass the compulsory tests, you get a score of 0. */
  576. while ((i = get_next_test(&compulsory_tests)) != NULL) {
  577. if (!run_test(i, summary, &score, &total_score, m)) {
  578. printf("%sTotal score: 0/%u\n", prefix, total_score);
  579. errx(1, "%s%s failed", prefix, i->name);
  580. }
  581. }
  582. /* --target overrides known FAIL from _info */
  583. if (m->info_file)
  584. add_info_options(m->info_file, !target);
  585. while ((i = get_next_test(&normal_tests)) != NULL)
  586. pass &= run_test(i, summary, &score, &total_score, m);
  587. printf("%sTotal score: %u/%u\n", prefix, score, total_score);
  588. return pass ? 0 : 1;
  589. }