ccanlint.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 <stdarg.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <err.h>
  28. #include <ctype.h>
  29. #include <ccan/btree/btree.h>
  30. #include <ccan/str/str.h>
  31. #include <ccan/str_talloc/str_talloc.h>
  32. #include <ccan/talloc/talloc.h>
  33. #include <ccan/opt/opt.h>
  34. #include <ccan/foreach/foreach.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. #if 0
  44. static void indent_print(const char *string)
  45. {
  46. while (*string) {
  47. unsigned int line = strcspn(string, "\n");
  48. printf("\t%.*s", line, string);
  49. if (string[line] == '\n') {
  50. printf("\n");
  51. line++;
  52. }
  53. string += line;
  54. }
  55. }
  56. #endif
  57. bool ask(const char *question)
  58. {
  59. char reply[2];
  60. printf("%s ", question);
  61. fflush(stdout);
  62. return fgets(reply, sizeof(reply), stdin) != NULL
  63. && toupper(reply[0]) == 'Y';
  64. }
  65. static const char *should_skip(struct manifest *m, struct ccanlint *i)
  66. {
  67. if (btree_lookup(cmdline_exclude, i->key))
  68. return "excluded on command line";
  69. if (btree_lookup(info_exclude, i->key))
  70. return "excluded in _info file";
  71. if (i->skip)
  72. return i->skip;
  73. if (i->skip_fail)
  74. return "dependency failed";
  75. if (i->can_run)
  76. return i->can_run(m);
  77. return NULL;
  78. }
  79. static bool run_test(struct ccanlint *i,
  80. bool quiet,
  81. unsigned int *running_score,
  82. unsigned int *running_total,
  83. struct manifest *m)
  84. {
  85. unsigned int timeleft;
  86. const struct dependent *d;
  87. const char *skip;
  88. struct score *score;
  89. //one less test to run through
  90. list_for_each(&i->dependencies, d, node)
  91. d->dependent->num_depends--;
  92. score = talloc(m, struct score);
  93. list_head_init(&score->per_file_errors);
  94. score->error = NULL;
  95. score->pass = false;
  96. score->score = 0;
  97. score->total = 1;
  98. skip = should_skip(m, i);
  99. if (skip) {
  100. skip:
  101. if (verbose && !streq(skip, "not relevant to target"))
  102. printf("%s: skipped (%s)\n", i->name, skip);
  103. /* If we're skipping this because a prereq failed, we fail:
  104. * count it as a score of 1. */
  105. if (i->skip_fail)
  106. (*running_total)++;
  107. list_del(&i->list);
  108. list_add_tail(&finished_tests, &i->list);
  109. list_for_each(&i->dependencies, d, node) {
  110. if (d->dependent->skip)
  111. continue;
  112. d->dependent->skip = "dependency was skipped";
  113. d->dependent->skip_fail = i->skip_fail;
  114. }
  115. return i->skip_fail ? false : true;
  116. }
  117. timeleft = timeout ? timeout : default_timeout_ms;
  118. i->check(m, i->keep_results, &timeleft, score);
  119. if (timeout && timeleft == 0) {
  120. skip = "timeout";
  121. goto skip;
  122. }
  123. assert(score->score <= score->total);
  124. if ((!score->pass && !quiet)
  125. || (score->score < score->total && verbose)
  126. || verbose > 1) {
  127. printf("%s: %s", i->name, score->pass ? "PASS" : "FAIL");
  128. if (score->total > 1)
  129. printf(" (+%u/%u)", score->score, score->total);
  130. printf("\n");
  131. }
  132. if (!quiet && !score->pass) {
  133. struct file_error *f;
  134. if (score->error)
  135. printf("%s:\n", score->error);
  136. list_for_each(&score->per_file_errors, f, list) {
  137. if (f->line)
  138. printf("%s:%u:%s\n",
  139. f->file->fullname, f->line, f->error);
  140. else if (f->file)
  141. printf("%s:%s\n", f->file->fullname, f->error);
  142. else
  143. printf("%s\n", f->error);
  144. }
  145. if (i->handle)
  146. i->handle(m, score);
  147. }
  148. *running_score += score->score;
  149. *running_total += score->total;
  150. list_del(&i->list);
  151. list_add_tail(&finished_tests, &i->list);
  152. if (!score->pass) {
  153. /* Skip any tests which depend on this one. */
  154. list_for_each(&i->dependencies, d, node) {
  155. if (d->dependent->skip)
  156. continue;
  157. d->dependent->skip = "dependency failed";
  158. d->dependent->skip_fail = true;
  159. }
  160. }
  161. return score->pass;
  162. }
  163. static void register_test(struct list_head *h, struct ccanlint *test, ...)
  164. {
  165. va_list ap;
  166. struct ccanlint *depends;
  167. struct dependent *dchild;
  168. list_add(h, &test->list);
  169. va_start(ap, test);
  170. /* Careful: we might have been initialized by a dependent. */
  171. if (test->dependencies.n.next == NULL)
  172. list_head_init(&test->dependencies);
  173. //dependent(s) args (if any), last one is NULL
  174. while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
  175. dchild = malloc(sizeof(*dchild));
  176. dchild->dependent = test;
  177. /* The thing we depend on might not be initialized yet! */
  178. if (depends->dependencies.n.next == NULL)
  179. list_head_init(&depends->dependencies);
  180. list_add_tail(&depends->dependencies, &dchild->node);
  181. test->num_depends++;
  182. }
  183. va_end(ap);
  184. }
  185. /**
  186. * get_next_test - retrieves the next test to be processed
  187. **/
  188. static inline struct ccanlint *get_next_test(struct list_head *test)
  189. {
  190. struct ccanlint *i;
  191. if (list_empty(test))
  192. return NULL;
  193. list_for_each(test, i, list) {
  194. if (i->num_depends == 0)
  195. return i;
  196. }
  197. errx(1, "Can't make process; test dependency cycle");
  198. }
  199. static void init_tests(void)
  200. {
  201. const struct ccanlint *i;
  202. struct btree *keys, *names;
  203. #undef REGISTER_TEST
  204. #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__, NULL)
  205. #include "generated-normal-tests"
  206. #undef REGISTER_TEST
  207. #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__, NULL)
  208. #include "generated-compulsory-tests"
  209. /* Self-consistency check: make sure no two tests
  210. have the same key or name. */
  211. keys = btree_new(btree_strcmp);
  212. names = btree_new(btree_strcmp);
  213. list_for_each(&compulsory_tests, i, list) {
  214. if (!btree_insert(keys, i->key))
  215. errx(1, "BUG: Duplicate test key '%s'", i->key);
  216. if (!btree_insert(keys, i->name))
  217. errx(1, "BUG: Duplicate test name '%s'", i->name);
  218. }
  219. list_for_each(&normal_tests, i, list) {
  220. if (!btree_insert(keys, i->key))
  221. errx(1, "BUG: Duplicate test key '%s'", i->key);
  222. if (!btree_insert(keys, i->name))
  223. errx(1, "BUG: Duplicate test name '%s'", i->name);
  224. }
  225. btree_delete(keys);
  226. btree_delete(names);
  227. if (!verbose)
  228. return;
  229. printf("\nCompulsory Tests\n");
  230. list_for_each(&compulsory_tests, i, list) {
  231. printf("%s depends on %u others\n", i->name, i->num_depends);
  232. if (!list_empty(&i->dependencies)) {
  233. const struct dependent *d;
  234. printf("These depend on us:\n");
  235. list_for_each(&i->dependencies, d, node)
  236. printf("\t%s\n", d->dependent->name);
  237. }
  238. }
  239. printf("\nNormal Tests\n");
  240. list_for_each(&normal_tests, i, list) {
  241. printf("%s depends on %u others\n", i->name, i->num_depends);
  242. if (!list_empty(&i->dependencies)) {
  243. const struct dependent *d;
  244. printf("These depend on us:\n");
  245. list_for_each(&i->dependencies, d, node)
  246. printf("\t%s\n", d->dependent->name);
  247. }
  248. }
  249. }
  250. static struct ccanlint *find_test(const char *key)
  251. {
  252. struct ccanlint *i;
  253. list_for_each(&compulsory_tests, i, list)
  254. if (streq(i->key, key))
  255. return i;
  256. list_for_each(&normal_tests, i, list)
  257. if (streq(i->key, key))
  258. return i;
  259. return NULL;
  260. }
  261. static char *keep_test(const char *testname, void *unused)
  262. {
  263. struct ccanlint *i = find_test(testname);
  264. if (!i)
  265. errx(1, "No test %s to --keep", testname);
  266. i->keep_results = true;
  267. return NULL;
  268. }
  269. static char *skip_test(const char *testname, void *unused)
  270. {
  271. btree_insert(cmdline_exclude, optarg);
  272. return NULL;
  273. }
  274. static void print_tests(struct list_head *tests, const char *type)
  275. {
  276. struct ccanlint *i;
  277. printf("%s tests:\n", type);
  278. /* This makes them print in topological order. */
  279. while ((i = get_next_test(tests)) != NULL) {
  280. const struct dependent *d;
  281. printf(" %-25s %s\n", i->key, i->name);
  282. list_del(&i->list);
  283. list_for_each(&i->dependencies, d, node)
  284. d->dependent->num_depends--;
  285. }
  286. }
  287. static char *list_tests(void *arg)
  288. {
  289. print_tests(&compulsory_tests, "Compulsory");
  290. print_tests(&normal_tests, "Normal");
  291. exit(0);
  292. }
  293. static char *strip(const void *ctx, const char *line)
  294. {
  295. line += strcspn(line, IDENT_CHARS "-");
  296. return talloc_strndup(ctx, line, strspn(line, IDENT_CHARS "-"));
  297. }
  298. static void add_info_fails(struct ccan_file *info)
  299. {
  300. struct doc_section *d;
  301. unsigned int i;
  302. list_for_each(get_ccan_file_docs(info), d, list) {
  303. if (!streq(d->type, "fails"))
  304. continue;
  305. for (i = 0; i < d->num_lines; i++)
  306. btree_insert(info_exclude, strip(info, d->lines[i]));
  307. break;
  308. }
  309. }
  310. static bool depends_on(struct ccanlint *i, struct ccanlint *target)
  311. {
  312. const struct dependent *d;
  313. if (i == target)
  314. return true;
  315. list_for_each(&i->dependencies, d, node) {
  316. if (depends_on(d->dependent, target))
  317. return true;
  318. }
  319. return false;
  320. }
  321. /* O(N^2), who cares? */
  322. static void skip_unrelated_tests(struct ccanlint *target)
  323. {
  324. struct ccanlint *i;
  325. struct list_head *list;
  326. foreach_ptr(list, &compulsory_tests, &normal_tests)
  327. list_for_each(list, i, list)
  328. if (!depends_on(i, target))
  329. i->skip = "not relevant to target";
  330. }
  331. int main(int argc, char *argv[])
  332. {
  333. bool summary = false;
  334. unsigned int score = 0, total_score = 0;
  335. struct manifest *m;
  336. struct ccanlint *i;
  337. const char *prefix = "";
  338. char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL;
  339. init_tests();
  340. cmdline_exclude = btree_new(btree_strcmp);
  341. info_exclude = btree_new(btree_strcmp);
  342. opt_register_arg("--dir|-d", opt_set_charp, opt_show_charp, &dir,
  343. "use this directory");
  344. opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
  345. "do not compile anything");
  346. opt_register_noarg("-l|--list-tests", list_tests, NULL,
  347. "list tests ccanlint performs (and exit)");
  348. opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
  349. "keep results of <testname> (can be used multiple times)");
  350. opt_register_noarg("--summary|-s", opt_set_bool, &summary,
  351. "simply give one line summary");
  352. opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
  353. "verbose mode (up to -vvvv)");
  354. opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
  355. "exclude <testname> (can be used multiple times)");
  356. opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
  357. NULL, &timeout,
  358. "ignore (terminate) tests that are slower than this");
  359. opt_register_arg("--target <testname>", opt_set_charp,
  360. NULL, &target,
  361. "only run one test (and its prerequisites)");
  362. opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
  363. "\nA program for checking and guiding development"
  364. " of CCAN modules.",
  365. "This usage message");
  366. opt_parse(&argc, argv, opt_log_stderr_exit);
  367. if (dir[0] != '/')
  368. dir = talloc_asprintf_append(NULL, "%s/%s", base_dir, dir);
  369. if (dir != base_dir)
  370. prefix = talloc_append_string(talloc_basename(NULL, dir), ": ");
  371. if (verbose >= 3)
  372. compile_verbose = true;
  373. if (verbose >= 4)
  374. tools_verbose = true;
  375. /* We move into temporary directory, so gcov dumps its files there. */
  376. if (chdir(temp_dir(talloc_autofree_context())) != 0)
  377. err(1, "Error changing to %s temporary dir", temp_dir(NULL));
  378. m = get_manifest(talloc_autofree_context(), dir);
  379. /* Create a symlink from temp dir back to src dir's test directory. */
  380. if (symlink(talloc_asprintf(m, "%s/test", dir),
  381. talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
  382. err(1, "Creating test symlink in %s", temp_dir(NULL));
  383. if (target) {
  384. struct ccanlint *test;
  385. test = find_test(target);
  386. if (!test)
  387. err(1, "Unknown test to run '%s'", target);
  388. skip_unrelated_tests(test);
  389. }
  390. /* If you don't pass the compulsory tests, you get a score of 0. */
  391. while ((i = get_next_test(&compulsory_tests)) != NULL) {
  392. if (!run_test(i, summary, &score, &total_score, m)) {
  393. printf("%sTotal score: 0/%u\n", prefix, total_score);
  394. errx(1, "%s%s failed", prefix, i->name);
  395. }
  396. }
  397. add_info_fails(m->info_file);
  398. while ((i = get_next_test(&normal_tests)) != NULL)
  399. run_test(i, summary, &score, &total_score, m);
  400. printf("%sTotal score: %u/%u\n", prefix, score, total_score);
  401. return 0;
  402. }