ccanlint.c 11 KB

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