ccanlint.c 17 KB

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