ccanlint.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * ccanlint: assorted checks and advice for a ccan package
  3. * Copyright (C) 2008 Rusty Russell, Idris Soule
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 2 of the License, or (at your option)
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include "ccanlint.h"
  20. #include "../tools.h"
  21. #include <unistd.h>
  22. #include <stdarg.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[2];
  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) {
  132. struct file_error *f;
  133. if (score->error)
  134. printf("%s:\n", score->error);
  135. list_for_each(&score->per_file_errors, f, list) {
  136. if (f->line)
  137. printf("%s:%u:%s\n",
  138. f->file->fullname, f->line, f->error);
  139. else if (f->file)
  140. printf("%s:%s\n", f->file->fullname, f->error);
  141. else
  142. printf("%s\n", f->error);
  143. }
  144. if (i->handle)
  145. i->handle(m, score);
  146. }
  147. *running_score += score->score;
  148. *running_total += score->total;
  149. list_del(&i->list);
  150. list_add_tail(&finished_tests, &i->list);
  151. if (!score->pass) {
  152. /* Skip any tests which depend on this one. */
  153. list_for_each(&i->dependencies, d, node) {
  154. if (d->dependent->skip)
  155. continue;
  156. d->dependent->skip = "dependency failed";
  157. d->dependent->skip_fail = true;
  158. }
  159. }
  160. return score->pass;
  161. }
  162. static void register_test(struct list_head *h, struct ccanlint *test, ...)
  163. {
  164. va_list ap;
  165. struct ccanlint *depends;
  166. struct dependent *dchild;
  167. list_add(h, &test->list);
  168. va_start(ap, test);
  169. /* Careful: we might have been initialized by a dependent. */
  170. if (test->dependencies.n.next == NULL)
  171. list_head_init(&test->dependencies);
  172. //dependent(s) args (if any), last one is NULL
  173. while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
  174. dchild = malloc(sizeof(*dchild));
  175. dchild->dependent = test;
  176. /* The thing we depend on might not be initialized yet! */
  177. if (depends->dependencies.n.next == NULL)
  178. list_head_init(&depends->dependencies);
  179. list_add_tail(&depends->dependencies, &dchild->node);
  180. test->num_depends++;
  181. }
  182. va_end(ap);
  183. }
  184. /**
  185. * get_next_test - retrieves the next test to be processed
  186. **/
  187. static inline struct ccanlint *get_next_test(struct list_head *test)
  188. {
  189. struct ccanlint *i;
  190. if (list_empty(test))
  191. return NULL;
  192. list_for_each(test, i, list) {
  193. if (i->num_depends == 0)
  194. return i;
  195. }
  196. errx(1, "Can't make process; test dependency cycle");
  197. }
  198. static void init_tests(void)
  199. {
  200. const struct ccanlint *i;
  201. struct btree *keys, *names;
  202. #undef REGISTER_TEST
  203. #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__, NULL)
  204. #include "generated-normal-tests"
  205. #undef REGISTER_TEST
  206. #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__, NULL)
  207. #include "generated-compulsory-tests"
  208. /* Self-consistency check: make sure no two tests
  209. have the same key or name. */
  210. keys = btree_new(btree_strcmp);
  211. names = btree_new(btree_strcmp);
  212. list_for_each(&compulsory_tests, i, list) {
  213. if (!btree_insert(keys, i->key))
  214. errx(1, "BUG: Duplicate test key '%s'", i->key);
  215. if (!btree_insert(keys, i->name))
  216. errx(1, "BUG: Duplicate test name '%s'", i->name);
  217. }
  218. list_for_each(&normal_tests, i, list) {
  219. if (!btree_insert(keys, i->key))
  220. errx(1, "BUG: Duplicate test key '%s'", i->key);
  221. if (!btree_insert(keys, i->name))
  222. errx(1, "BUG: Duplicate test name '%s'", i->name);
  223. }
  224. btree_delete(keys);
  225. btree_delete(names);
  226. if (!verbose)
  227. return;
  228. printf("\nCompulsory Tests\n");
  229. list_for_each(&compulsory_tests, i, list) {
  230. printf("%s depends on %u others\n", i->name, i->num_depends);
  231. if (!list_empty(&i->dependencies)) {
  232. const struct dependent *d;
  233. printf("These depend on us:\n");
  234. list_for_each(&i->dependencies, d, node)
  235. printf("\t%s\n", d->dependent->name);
  236. }
  237. }
  238. printf("\nNormal Tests\n");
  239. list_for_each(&normal_tests, i, list) {
  240. printf("%s depends on %u others\n", i->name, i->num_depends);
  241. if (!list_empty(&i->dependencies)) {
  242. const struct dependent *d;
  243. printf("These depend on us:\n");
  244. list_for_each(&i->dependencies, d, node)
  245. printf("\t%s\n", d->dependent->name);
  246. }
  247. }
  248. }
  249. static struct ccanlint *find_test(const char *key)
  250. {
  251. struct ccanlint *i;
  252. list_for_each(&compulsory_tests, i, list)
  253. if (streq(i->key, key))
  254. return i;
  255. list_for_each(&normal_tests, i, list)
  256. if (streq(i->key, key))
  257. return i;
  258. return NULL;
  259. }
  260. static char *keep_test(const char *testname, void *unused)
  261. {
  262. struct ccanlint *i = find_test(testname);
  263. if (!i)
  264. errx(1, "No test %s to --keep", testname);
  265. i->keep_results = true;
  266. return NULL;
  267. }
  268. static char *skip_test(const char *testname, void *unused)
  269. {
  270. btree_insert(cmdline_exclude, optarg);
  271. return NULL;
  272. }
  273. static void print_tests(struct list_head *tests, const char *type)
  274. {
  275. struct ccanlint *i;
  276. printf("%s tests:\n", type);
  277. /* This makes them print in topological order. */
  278. while ((i = get_next_test(tests)) != NULL) {
  279. const struct dependent *d;
  280. printf(" %-25s %s\n", i->key, i->name);
  281. list_del(&i->list);
  282. list_for_each(&i->dependencies, d, node)
  283. d->dependent->num_depends--;
  284. }
  285. }
  286. static char *list_tests(void *arg)
  287. {
  288. print_tests(&compulsory_tests, "Compulsory");
  289. print_tests(&normal_tests, "Normal");
  290. exit(0);
  291. }
  292. static char *strip(const void *ctx, const char *line)
  293. {
  294. line += strcspn(line, IDENT_CHARS "-");
  295. return talloc_strndup(ctx, line, strspn(line, IDENT_CHARS "-"));
  296. }
  297. static void add_info_fails(struct ccan_file *info)
  298. {
  299. struct doc_section *d;
  300. unsigned int i;
  301. list_for_each(get_ccan_file_docs(info), d, list) {
  302. if (!streq(d->type, "fails"))
  303. continue;
  304. for (i = 0; i < d->num_lines; i++)
  305. btree_insert(info_exclude, strip(info, d->lines[i]));
  306. break;
  307. }
  308. }
  309. static bool depends_on(struct ccanlint *i, struct ccanlint *target)
  310. {
  311. const struct dependent *d;
  312. if (i == target)
  313. return true;
  314. list_for_each(&i->dependencies, d, node) {
  315. if (depends_on(d->dependent, target))
  316. return true;
  317. }
  318. return false;
  319. }
  320. /* O(N^2), who cares? */
  321. static void skip_unrelated_tests(struct ccanlint *target)
  322. {
  323. struct ccanlint *i;
  324. struct list_head *list;
  325. foreach_ptr(list, &compulsory_tests, &normal_tests)
  326. list_for_each(list, i, list)
  327. if (!depends_on(i, target))
  328. i->skip = "not relevant to target";
  329. }
  330. int main(int argc, char *argv[])
  331. {
  332. bool summary = false;
  333. unsigned int score = 0, total_score = 0;
  334. struct manifest *m;
  335. struct ccanlint *i;
  336. const char *prefix = "";
  337. char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL;
  338. init_tests();
  339. cmdline_exclude = btree_new(btree_strcmp);
  340. info_exclude = btree_new(btree_strcmp);
  341. opt_register_arg("--dir|-d", opt_set_charp, opt_show_charp, &dir,
  342. "use this directory");
  343. opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
  344. "do not compile anything");
  345. opt_register_noarg("-l|--list-tests", list_tests, NULL,
  346. "list tests ccanlint performs (and exit)");
  347. opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
  348. "keep results of <testname> (can be used multiple times)");
  349. opt_register_noarg("--summary|-s", opt_set_bool, &summary,
  350. "simply give one line summary");
  351. opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
  352. "verbose mode (can specify more than once)");
  353. opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
  354. "exclude <testname> (can be used multiple times)");
  355. opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
  356. NULL, &timeout,
  357. "ignore (terminate) tests that are slower than this");
  358. opt_register_arg("--target <testname>", opt_set_charp,
  359. NULL, &target,
  360. "only run one test (and its prerequisites)");
  361. opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
  362. "\nA program for checking and guiding development"
  363. " of CCAN modules.",
  364. "This usage message");
  365. opt_parse(&argc, argv, opt_log_stderr_exit);
  366. if (dir[0] != '/')
  367. dir = talloc_asprintf_append(NULL, "%s/%s", base_dir, dir);
  368. if (dir != base_dir)
  369. prefix = talloc_append_string(talloc_basename(NULL, dir), ": ");
  370. if (verbose >= 2)
  371. compile_verbose = true;
  372. if (verbose >= 3)
  373. tools_verbose = true;
  374. /* We move into temporary directory, so gcov dumps its files there. */
  375. if (chdir(temp_dir(talloc_autofree_context())) != 0)
  376. err(1, "Error changing to %s temporary dir", temp_dir(NULL));
  377. m = get_manifest(talloc_autofree_context(), dir);
  378. /* Create a symlink from temp dir back to src dir's test directory. */
  379. if (symlink(talloc_asprintf(m, "%s/test", dir),
  380. talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
  381. err(1, "Creating test symlink in %s", temp_dir(NULL));
  382. if (target) {
  383. struct ccanlint *test;
  384. test = find_test(target);
  385. if (!test)
  386. err(1, "Unknown test to run '%s'", target);
  387. skip_unrelated_tests(test);
  388. }
  389. /* If you don't pass the compulsory tests, you get a score of 0. */
  390. while ((i = get_next_test(&compulsory_tests)) != NULL) {
  391. if (!run_test(i, summary, &score, &total_score, m)) {
  392. printf("%sTotal score: 0/%u\n", prefix, total_score);
  393. errx(1, "%s%s failed", prefix, i->name);
  394. }
  395. }
  396. add_info_fails(m->info_file);
  397. while ((i = get_next_test(&normal_tests)) != NULL)
  398. run_test(i, summary, &score, &total_score, m);
  399. printf("%sTotal score: %u/%u\n", prefix, score, total_score);
  400. return 0;
  401. }