ccanlint.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. if (!quiet && !score->pass && i->handle)
  143. i->handle(m, score);
  144. }
  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. if (!verbose)
  248. return;
  249. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  250. printf("\%s Tests\n",
  251. list == &compulsory_tests ? "Compulsory" : "Normal");
  252. if (!list_empty(&c->dependencies)) {
  253. const struct dependent *d;
  254. printf("These depend on us:\n");
  255. list_for_each(&c->dependencies, d, node)
  256. printf("\t%s\n", d->dependent->name);
  257. }
  258. }
  259. }
  260. static int show_tmpdir(const char *dir)
  261. {
  262. printf("You can find ccanlint working files in '%s'\n", dir);
  263. return 0;
  264. }
  265. static char *keep_test(const char *testname, void *unused)
  266. {
  267. struct ccanlint *i;
  268. if (streq(testname, "all")) {
  269. struct list_head *list;
  270. foreach_ptr(list, &compulsory_tests, &normal_tests) {
  271. list_for_each(list, i, list)
  272. i->keep_results = true;
  273. }
  274. } else {
  275. i = find_test(testname);
  276. if (!i)
  277. errx(1, "No test %s to --keep", testname);
  278. i->keep_results = true;
  279. }
  280. /* Don't automatically destroy temporary dir. */
  281. talloc_set_destructor(temp_dir(NULL), show_tmpdir);
  282. return NULL;
  283. }
  284. static char *skip_test(const char *testname, void *unused)
  285. {
  286. btree_insert(cmdline_exclude, testname);
  287. return NULL;
  288. }
  289. static void print_tests(struct list_head *tests, const char *type)
  290. {
  291. struct ccanlint *i;
  292. printf("%s tests:\n", type);
  293. /* This makes them print in topological order. */
  294. while ((i = get_next_test(tests)) != NULL) {
  295. const struct dependent *d;
  296. printf(" %-25s %s\n", i->key, i->name);
  297. list_del(&i->list);
  298. list_for_each(&i->dependencies, d, node)
  299. d->dependent->num_depends--;
  300. }
  301. }
  302. static char *list_tests(void *arg)
  303. {
  304. print_tests(&compulsory_tests, "Compulsory");
  305. print_tests(&normal_tests, "Normal");
  306. exit(0);
  307. }
  308. static void test_dgraph_vertices(struct list_head *tests, const char *style)
  309. {
  310. const struct ccanlint *i;
  311. list_for_each(tests, i, list) {
  312. /*
  313. * todo: escape labels in case ccanlint test keys have
  314. * characters interpreted as GraphViz syntax.
  315. */
  316. printf("\t\"%p\" [label=\"%s\"%s]\n", i, i->key, style);
  317. }
  318. }
  319. static void test_dgraph_edges(struct list_head *tests)
  320. {
  321. const struct ccanlint *i;
  322. const struct dependent *d;
  323. list_for_each(tests, i, list)
  324. list_for_each(&i->dependencies, d, node)
  325. printf("\t\"%p\" -> \"%p\"\n", d->dependent, i);
  326. }
  327. static char *test_dependency_graph(void *arg)
  328. {
  329. puts("digraph G {");
  330. test_dgraph_vertices(&compulsory_tests, ", style=filled, fillcolor=yellow");
  331. test_dgraph_vertices(&normal_tests, "");
  332. test_dgraph_edges(&compulsory_tests);
  333. test_dgraph_edges(&normal_tests);
  334. puts("}");
  335. exit(0);
  336. }
  337. /* Remove empty lines. */
  338. static char **collapse(char **lines, unsigned int *nump)
  339. {
  340. unsigned int i, j;
  341. for (i = j = 0; lines[i]; i++) {
  342. if (lines[i][0])
  343. lines[j++] = lines[i];
  344. }
  345. if (nump)
  346. *nump = j;
  347. return lines;
  348. }
  349. static void add_info_options(struct ccan_file *info, bool mark_fails)
  350. {
  351. struct doc_section *d;
  352. unsigned int i;
  353. struct ccanlint *test;
  354. list_for_each(get_ccan_file_docs(info), d, list) {
  355. if (!streq(d->type, "ccanlint"))
  356. continue;
  357. for (i = 0; i < d->num_lines; i++) {
  358. char **words = collapse(strsplit(d, d->lines[i], " \t"),
  359. NULL);
  360. if (!words[0])
  361. continue;
  362. if (strncmp(words[0], "//", 2) == 0)
  363. continue;
  364. test = find_test(words[0]);
  365. if (!test) {
  366. warnx("%s: unknown ccanlint test '%s'",
  367. info->fullname, words[0]);
  368. continue;
  369. }
  370. if (!words[1]) {
  371. warnx("%s: no argument to test '%s'",
  372. info->fullname, words[0]);
  373. continue;
  374. }
  375. /* Known failure? */
  376. if (strcasecmp(words[1], "FAIL") == 0) {
  377. if (mark_fails)
  378. btree_insert(info_exclude, words[0]);
  379. } else {
  380. if (!test->takes_options)
  381. warnx("%s: %s doesn't take options",
  382. info->fullname, words[0]);
  383. /* Copy line exactly into options. */
  384. test->options = strstr(d->lines[i], words[0])
  385. + strlen(words[0]);
  386. }
  387. }
  388. }
  389. }
  390. static bool depends_on(struct ccanlint *i, struct ccanlint *target)
  391. {
  392. const struct dependent *d;
  393. if (i == target)
  394. return true;
  395. list_for_each(&i->dependencies, d, node) {
  396. if (depends_on(d->dependent, target))
  397. return true;
  398. }
  399. return false;
  400. }
  401. /* O(N^2), who cares? */
  402. static void skip_unrelated_tests(struct ccanlint *target)
  403. {
  404. struct ccanlint *i;
  405. struct list_head *list;
  406. foreach_ptr(list, &compulsory_tests, &normal_tests)
  407. list_for_each(list, i, list)
  408. if (!depends_on(i, target))
  409. i->skip = "not relevant to target";
  410. }
  411. static char *demangle_string(char *string)
  412. {
  413. unsigned int i;
  414. const char mapfrom[] = "abfnrtv";
  415. const char mapto[] = "\a\b\f\n\r\t\v";
  416. if (!strchr(string, '"'))
  417. return NULL;
  418. string = strchr(string, '"') + 1;
  419. if (!strrchr(string, '"'))
  420. return NULL;
  421. *strrchr(string, '"') = '\0';
  422. for (i = 0; i < strlen(string); i++) {
  423. if (string[i] == '\\') {
  424. char repl;
  425. unsigned len = 0;
  426. const char *p = strchr(mapfrom, string[i+1]);
  427. if (p) {
  428. repl = mapto[p - mapfrom];
  429. len = 1;
  430. } else if (strlen(string+i+1) >= 3) {
  431. if (string[i+1] == 'x') {
  432. repl = (string[i+2]-'0')*16
  433. + string[i+3]-'0';
  434. len = 3;
  435. } else if (cisdigit(string[i+1])) {
  436. repl = (string[i+2]-'0')*8*8
  437. + (string[i+3]-'0')*8
  438. + (string[i+4]-'0');
  439. len = 3;
  440. }
  441. }
  442. if (len == 0) {
  443. repl = string[i+1];
  444. len = 1;
  445. }
  446. string[i] = repl;
  447. memmove(string + i + 1, string + i + len + 1,
  448. strlen(string + i + len + 1) + 1);
  449. }
  450. }
  451. return string;
  452. }
  453. static void read_config_header(void)
  454. {
  455. char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
  456. char **lines;
  457. unsigned int i;
  458. config_header = grab_file(NULL, fname, NULL);
  459. if (!config_header) {
  460. talloc_free(fname);
  461. return;
  462. }
  463. lines = strsplit(config_header, config_header, "\n");
  464. for (i = 0; i < talloc_array_length(lines) - 1; i++) {
  465. char *sym;
  466. const char **line = (const char **)&lines[i];
  467. if (!get_token(line, "#"))
  468. continue;
  469. if (!get_token(line, "define"))
  470. continue;
  471. sym = get_symbol_token(lines, line);
  472. if (streq(sym, "CCAN_COMPILER") && !compiler) {
  473. compiler = demangle_string(lines[i]);
  474. if (!compiler)
  475. errx(1, "%s:%u:could not parse CCAN_COMPILER",
  476. fname, i+1);
  477. if (verbose > 1)
  478. printf("%s: compiler set to '%s'\n",
  479. fname, compiler);
  480. } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
  481. cflags = demangle_string(lines[i]);
  482. if (!cflags)
  483. errx(1, "%s:%u:could not parse CCAN_CFLAGS",
  484. fname, i+1);
  485. if (verbose > 1)
  486. printf("%s: compiler flags set to '%s'\n",
  487. fname, cflags);
  488. }
  489. }
  490. if (!compiler)
  491. compiler = CCAN_COMPILER;
  492. if (!cflags)
  493. compiler = CCAN_CFLAGS;
  494. }
  495. static char *opt_set_const_charp(const char *arg, const char **p)
  496. {
  497. return opt_set_charp(arg, cast_const2(char **, p));
  498. }
  499. int main(int argc, char *argv[])
  500. {
  501. bool summary = false;
  502. unsigned int score = 0, total_score = 0;
  503. struct manifest *m;
  504. struct ccanlint *i;
  505. const char *prefix = "";
  506. char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL;
  507. init_tests();
  508. cmdline_exclude = btree_new(btree_strcmp);
  509. info_exclude = btree_new(btree_strcmp);
  510. opt_register_arg("--dir|-d", opt_set_charp, opt_show_charp, &dir,
  511. "use this directory");
  512. opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
  513. "do not compile anything");
  514. opt_register_noarg("-l|--list-tests", list_tests, NULL,
  515. "list tests ccanlint performs (and exit)");
  516. opt_register_noarg("--test-dep-graph", test_dependency_graph, NULL,
  517. "print dependency graph of tests in Graphviz .dot format");
  518. opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
  519. "keep results of <testname>"
  520. " (can be used multiple times, or 'all')");
  521. opt_register_noarg("--summary|-s", opt_set_bool, &summary,
  522. "simply give one line summary");
  523. opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
  524. "verbose mode (up to -vvvv)");
  525. opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
  526. "exclude <testname> (can be used multiple times)");
  527. opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
  528. NULL, &timeout,
  529. "ignore (terminate) tests that are slower than this");
  530. opt_register_arg("--target <testname>", opt_set_charp,
  531. NULL, &target,
  532. "only run one test (and its prerequisites)");
  533. opt_register_arg("--compiler <compiler>", opt_set_const_charp,
  534. NULL, &compiler, "set the compiler");
  535. opt_register_arg("--cflags <flags>", opt_set_const_charp,
  536. NULL, &cflags, "set the compiler flags");
  537. opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
  538. "\nA program for checking and guiding development"
  539. " of CCAN modules.",
  540. "This usage message");
  541. /* We move into temporary directory, so gcov dumps its files there. */
  542. if (chdir(temp_dir(talloc_autofree_context())) != 0)
  543. err(1, "Error changing to %s temporary dir", temp_dir(NULL));
  544. opt_parse(&argc, argv, opt_log_stderr_exit);
  545. if (dir[0] != '/')
  546. dir = talloc_asprintf_append(NULL, "%s/%s", base_dir, dir);
  547. while (strends(dir, "/"))
  548. dir[strlen(dir)-1] = '\0';
  549. if (dir != base_dir)
  550. prefix = talloc_append_string(talloc_basename(NULL, dir), ": ");
  551. if (verbose >= 3)
  552. compile_verbose = true;
  553. if (verbose >= 4)
  554. tools_verbose = true;
  555. m = get_manifest(talloc_autofree_context(), dir);
  556. read_config_header();
  557. /* Create a symlink from temp dir back to src dir's test directory. */
  558. if (symlink(talloc_asprintf(m, "%s/test", dir),
  559. talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
  560. err(1, "Creating test symlink in %s", temp_dir(NULL));
  561. if (target) {
  562. struct ccanlint *test;
  563. test = find_test(target);
  564. if (!test)
  565. errx(1, "Unknown test to run '%s'", target);
  566. skip_unrelated_tests(test);
  567. }
  568. /* If you don't pass the compulsory tests, you get a score of 0. */
  569. while ((i = get_next_test(&compulsory_tests)) != NULL) {
  570. if (!run_test(i, summary, &score, &total_score, m)) {
  571. printf("%sTotal score: 0/%u\n", prefix, total_score);
  572. errx(1, "%s%s failed", prefix, i->name);
  573. }
  574. }
  575. /* --target overrides known FAIL from _info */
  576. if (m->info_file)
  577. add_info_options(m->info_file, !target);
  578. while ((i = get_next_test(&normal_tests)) != NULL)
  579. run_test(i, summary, &score, &total_score, m);
  580. printf("%sTotal score: %u/%u\n", prefix, score, total_score);
  581. return 0;
  582. }