ccanlint.c 12 KB

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