ccanlint.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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[80];
  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) || verbose) {
  133. struct file_error *f;
  134. unsigned int lines = 1;
  135. if (score->error)
  136. printf("%s%s\n", score->error,
  137. list_empty(&score->per_file_errors) ? "" : ":");
  138. list_for_each(&score->per_file_errors, f, list) {
  139. if (f->line)
  140. printf("%s:%u:%s\n",
  141. f->file->fullname, f->line, f->error);
  142. else if (f->file)
  143. printf("%s:%s\n", f->file->fullname, f->error);
  144. else
  145. printf("%s\n", f->error);
  146. if (verbose < 2 && ++lines > 5) {
  147. printf("... more (use -vv to see them all)\n");
  148. break;
  149. }
  150. }
  151. if (!quiet && !score->pass && i->handle)
  152. i->handle(m, score);
  153. }
  154. *running_score += score->score;
  155. *running_total += score->total;
  156. list_del(&i->list);
  157. list_add_tail(&finished_tests, &i->list);
  158. if (!score->pass) {
  159. /* Skip any tests which depend on this one. */
  160. list_for_each(&i->dependencies, d, node) {
  161. if (d->dependent->skip)
  162. continue;
  163. d->dependent->skip = "dependency failed";
  164. d->dependent->skip_fail = true;
  165. }
  166. }
  167. return score->pass;
  168. }
  169. static void register_test(struct list_head *h, struct ccanlint *test, ...)
  170. {
  171. va_list ap;
  172. struct ccanlint *depends;
  173. struct dependent *dchild;
  174. list_add(h, &test->list);
  175. va_start(ap, test);
  176. /* Careful: we might have been initialized by a dependent. */
  177. if (test->dependencies.n.next == NULL)
  178. list_head_init(&test->dependencies);
  179. //dependent(s) args (if any), last one is NULL
  180. while ((depends = va_arg(ap, struct ccanlint *)) != NULL) {
  181. dchild = malloc(sizeof(*dchild));
  182. dchild->dependent = test;
  183. /* The thing we depend on might not be initialized yet! */
  184. if (depends->dependencies.n.next == NULL)
  185. list_head_init(&depends->dependencies);
  186. list_add_tail(&depends->dependencies, &dchild->node);
  187. test->num_depends++;
  188. }
  189. va_end(ap);
  190. }
  191. /**
  192. * get_next_test - retrieves the next test to be processed
  193. **/
  194. static inline struct ccanlint *get_next_test(struct list_head *test)
  195. {
  196. struct ccanlint *i;
  197. if (list_empty(test))
  198. return NULL;
  199. list_for_each(test, i, list) {
  200. if (i->num_depends == 0)
  201. return i;
  202. }
  203. errx(1, "Can't make process; test dependency cycle");
  204. }
  205. static void init_tests(void)
  206. {
  207. const struct ccanlint *i;
  208. struct btree *keys, *names;
  209. #undef REGISTER_TEST
  210. #define REGISTER_TEST(name, ...) register_test(&normal_tests, &name, __VA_ARGS__, NULL)
  211. #include "generated-normal-tests"
  212. #undef REGISTER_TEST
  213. #define REGISTER_TEST(name, ...) register_test(&compulsory_tests, &name, __VA_ARGS__, NULL)
  214. #include "generated-compulsory-tests"
  215. /* Self-consistency check: make sure no two tests
  216. have the same key or name. */
  217. keys = btree_new(btree_strcmp);
  218. names = btree_new(btree_strcmp);
  219. list_for_each(&compulsory_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. list_for_each(&normal_tests, i, list) {
  226. if (!btree_insert(keys, i->key))
  227. errx(1, "BUG: Duplicate test key '%s'", i->key);
  228. if (!btree_insert(keys, i->name))
  229. errx(1, "BUG: Duplicate test name '%s'", i->name);
  230. }
  231. btree_delete(keys);
  232. btree_delete(names);
  233. if (!verbose)
  234. return;
  235. printf("\nCompulsory Tests\n");
  236. list_for_each(&compulsory_tests, i, list) {
  237. printf("%s depends on %u others\n", i->name, i->num_depends);
  238. if (!list_empty(&i->dependencies)) {
  239. const struct dependent *d;
  240. printf("These depend on us:\n");
  241. list_for_each(&i->dependencies, d, node)
  242. printf("\t%s\n", d->dependent->name);
  243. }
  244. }
  245. printf("\nNormal Tests\n");
  246. list_for_each(&normal_tests, i, list) {
  247. printf("%s depends on %u others\n", i->name, i->num_depends);
  248. if (!list_empty(&i->dependencies)) {
  249. const struct dependent *d;
  250. printf("These depend on us:\n");
  251. list_for_each(&i->dependencies, d, node)
  252. printf("\t%s\n", d->dependent->name);
  253. }
  254. }
  255. }
  256. static struct ccanlint *find_test(const char *key)
  257. {
  258. struct ccanlint *i;
  259. list_for_each(&compulsory_tests, i, list)
  260. if (streq(i->key, key))
  261. return i;
  262. list_for_each(&normal_tests, i, list)
  263. if (streq(i->key, key))
  264. return i;
  265. return NULL;
  266. }
  267. static char *keep_test(const char *testname, void *unused)
  268. {
  269. struct ccanlint *i = find_test(testname);
  270. if (!i)
  271. errx(1, "No test %s to --keep", testname);
  272. i->keep_results = true;
  273. return NULL;
  274. }
  275. static char *skip_test(const char *testname, void *unused)
  276. {
  277. btree_insert(cmdline_exclude, testname);
  278. return NULL;
  279. }
  280. static void print_tests(struct list_head *tests, const char *type)
  281. {
  282. struct ccanlint *i;
  283. printf("%s tests:\n", type);
  284. /* This makes them print in topological order. */
  285. while ((i = get_next_test(tests)) != NULL) {
  286. const struct dependent *d;
  287. printf(" %-25s %s\n", i->key, i->name);
  288. list_del(&i->list);
  289. list_for_each(&i->dependencies, d, node)
  290. d->dependent->num_depends--;
  291. }
  292. }
  293. static char *list_tests(void *arg)
  294. {
  295. print_tests(&compulsory_tests, "Compulsory");
  296. print_tests(&normal_tests, "Normal");
  297. exit(0);
  298. }
  299. /* Remove empty lines. */
  300. static char **collapse(char **lines, unsigned int *nump)
  301. {
  302. unsigned int i, j;
  303. for (i = j = 0; lines[i]; i++) {
  304. if (lines[i][0])
  305. lines[j++] = lines[i];
  306. }
  307. if (nump)
  308. *nump = j;
  309. return lines;
  310. }
  311. static void add_info_options(struct ccan_file *info, bool mark_fails)
  312. {
  313. struct doc_section *d;
  314. unsigned int i;
  315. struct ccanlint *test;
  316. list_for_each(get_ccan_file_docs(info), d, list) {
  317. if (!streq(d->type, "ccanlint"))
  318. continue;
  319. for (i = 0; i < d->num_lines; i++) {
  320. char **words = collapse(strsplit(d, d->lines[i], " \t",
  321. NULL), NULL);
  322. if (!words[0])
  323. continue;
  324. if (strncmp(words[0], "//", 2) == 0)
  325. continue;
  326. test = find_test(words[0]);
  327. if (!test) {
  328. warnx("%s: unknown ccanlint test '%s'",
  329. info->fullname, words[0]);
  330. continue;
  331. }
  332. if (!words[1]) {
  333. warnx("%s: no argument to test '%s'",
  334. info->fullname, words[0]);
  335. continue;
  336. }
  337. /* Known failure? */
  338. if (strcasecmp(words[1], "FAIL") == 0) {
  339. if (mark_fails)
  340. btree_insert(info_exclude, words[0]);
  341. } else {
  342. if (!test->takes_options)
  343. warnx("%s: %s doesn't take options",
  344. info->fullname, words[0]);
  345. /* Copy line exactly into options. */
  346. test->options = strstr(d->lines[i], words[0])
  347. + strlen(words[0]);
  348. }
  349. }
  350. }
  351. }
  352. static bool depends_on(struct ccanlint *i, struct ccanlint *target)
  353. {
  354. const struct dependent *d;
  355. if (i == target)
  356. return true;
  357. list_for_each(&i->dependencies, d, node) {
  358. if (depends_on(d->dependent, target))
  359. return true;
  360. }
  361. return false;
  362. }
  363. /* O(N^2), who cares? */
  364. static void skip_unrelated_tests(struct ccanlint *target)
  365. {
  366. struct ccanlint *i;
  367. struct list_head *list;
  368. foreach_ptr(list, &compulsory_tests, &normal_tests)
  369. list_for_each(list, i, list)
  370. if (!depends_on(i, target))
  371. i->skip = "not relevant to target";
  372. }
  373. int main(int argc, char *argv[])
  374. {
  375. bool summary = false;
  376. unsigned int score = 0, total_score = 0;
  377. struct manifest *m;
  378. struct ccanlint *i;
  379. const char *prefix = "";
  380. char *dir = talloc_getcwd(NULL), *base_dir = dir, *target = NULL;
  381. init_tests();
  382. cmdline_exclude = btree_new(btree_strcmp);
  383. info_exclude = btree_new(btree_strcmp);
  384. opt_register_arg("--dir|-d", opt_set_charp, opt_show_charp, &dir,
  385. "use this directory");
  386. opt_register_noarg("-n|--safe-mode", opt_set_bool, &safe_mode,
  387. "do not compile anything");
  388. opt_register_noarg("-l|--list-tests", list_tests, NULL,
  389. "list tests ccanlint performs (and exit)");
  390. opt_register_arg("-k|--keep <testname>", keep_test, NULL, NULL,
  391. "keep results of <testname> (can be used multiple times)");
  392. opt_register_noarg("--summary|-s", opt_set_bool, &summary,
  393. "simply give one line summary");
  394. opt_register_noarg("--verbose|-v", opt_inc_intval, &verbose,
  395. "verbose mode (up to -vvvv)");
  396. opt_register_arg("-x|--exclude <testname>", skip_test, NULL, NULL,
  397. "exclude <testname> (can be used multiple times)");
  398. opt_register_arg("-t|--timeout <milleseconds>", opt_set_uintval,
  399. NULL, &timeout,
  400. "ignore (terminate) tests that are slower than this");
  401. opt_register_arg("--target <testname>", opt_set_charp,
  402. NULL, &target,
  403. "only run one test (and its prerequisites)");
  404. opt_register_noarg("-?|-h|--help", opt_usage_and_exit,
  405. "\nA program for checking and guiding development"
  406. " of CCAN modules.",
  407. "This usage message");
  408. opt_parse(&argc, argv, opt_log_stderr_exit);
  409. if (dir[0] != '/')
  410. dir = talloc_asprintf_append(NULL, "%s/%s", base_dir, dir);
  411. if (dir != base_dir)
  412. prefix = talloc_append_string(talloc_basename(NULL, dir), ": ");
  413. if (verbose >= 3)
  414. compile_verbose = true;
  415. if (verbose >= 4)
  416. tools_verbose = true;
  417. /* We move into temporary directory, so gcov dumps its files there. */
  418. if (chdir(temp_dir(talloc_autofree_context())) != 0)
  419. err(1, "Error changing to %s temporary dir", temp_dir(NULL));
  420. m = get_manifest(talloc_autofree_context(), dir);
  421. /* Create a symlink from temp dir back to src dir's test directory. */
  422. if (symlink(talloc_asprintf(m, "%s/test", dir),
  423. talloc_asprintf(m, "%s/test", temp_dir(NULL))) != 0)
  424. err(1, "Creating test symlink in %s", temp_dir(NULL));
  425. if (target) {
  426. struct ccanlint *test;
  427. test = find_test(target);
  428. if (!test)
  429. err(1, "Unknown test to run '%s'", target);
  430. skip_unrelated_tests(test);
  431. }
  432. /* If you don't pass the compulsory tests, you get a score of 0. */
  433. while ((i = get_next_test(&compulsory_tests)) != NULL) {
  434. if (!run_test(i, summary, &score, &total_score, m)) {
  435. printf("%sTotal score: 0/%u\n", prefix, total_score);
  436. errx(1, "%s%s failed", prefix, i->name);
  437. }
  438. }
  439. /* --target overrides known FAIL from _info */
  440. add_info_options(m->info_file, !target);
  441. while ((i = get_next_test(&normal_tests)) != NULL)
  442. run_test(i, summary, &score, &total_score, m);
  443. printf("%sTotal score: %u/%u\n", prefix, score, total_score);
  444. return 0;
  445. }