examples_compile.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/cast/cast.h>
  5. #include <ccan/str/str.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <ctype.h>
  13. #include <assert.h>
  14. #include <err.h>
  15. #include "../compulsory_tests/build.h"
  16. static const char *can_run(struct manifest *m)
  17. {
  18. if (safe_mode)
  19. return "Safe mode enabled";
  20. if (list_empty(&m->examples))
  21. return "No examples to compile";
  22. return NULL;
  23. }
  24. static void add_mod(struct manifest ***deps, struct manifest *m)
  25. {
  26. unsigned int num = talloc_get_size(*deps) / sizeof(*deps);
  27. *deps = talloc_realloc(NULL, *deps, struct manifest *, num + 1);
  28. (*deps)[num] = m;
  29. }
  30. static bool have_mod(struct manifest *deps[], const char *basename)
  31. {
  32. unsigned int i;
  33. for (i = 0; i < talloc_get_size(deps) / sizeof(*deps); i++)
  34. if (strcmp(deps[i]->basename, basename) == 0)
  35. return true;
  36. return false;
  37. }
  38. static void add_dep(struct manifest ***deps, const char *basename)
  39. {
  40. unsigned int i;
  41. struct manifest *m;
  42. char *errstr;
  43. if (have_mod(*deps, basename))
  44. return;
  45. m = get_manifest(*deps, talloc_asprintf(*deps, "%s/ccan/%s",
  46. ccan_dir, basename));
  47. errstr = build_submodule(m, cflags, COMPILE_NORMAL);
  48. if (errstr)
  49. errx(1, "%s", errstr);
  50. add_mod(deps, m);
  51. /* Get that modules depends as well... */
  52. assert(!safe_mode);
  53. if (m->info_file) {
  54. char **infodeps;
  55. infodeps = get_deps(m, m->dir, false,
  56. &m->info_file->compiled[COMPILE_NORMAL]);
  57. for (i = 0; infodeps[i]; i++) {
  58. if (strstarts(infodeps[i], "ccan/"))
  59. add_dep(deps, infodeps[i] + strlen("ccan/"));
  60. }
  61. }
  62. }
  63. static char *example_obj_list(struct manifest *m, struct ccan_file *f)
  64. {
  65. struct manifest **deps = talloc_array(f, struct manifest *, 0);
  66. char **lines, *list;
  67. unsigned int i;
  68. /* This one for a start. */
  69. add_dep(&deps, m->basename);
  70. /* Other modules implied by includes. */
  71. for (lines = get_ccan_file_lines(f); *lines; lines++) {
  72. unsigned preflen = strspn(*lines, " \t");
  73. if (strstarts(*lines + preflen, "#include <ccan/")) {
  74. char *modname;
  75. modname = talloc_strdup(f, *lines + preflen
  76. + strlen("#include <ccan/"));
  77. modname[strcspn(modname, "/")] = '\0';
  78. if (!have_mod(deps, modname))
  79. add_dep(&deps, modname);
  80. }
  81. }
  82. list = talloc_strdup(f, "");
  83. for (i = 0; i < talloc_get_size(deps) / sizeof(*deps); i++) {
  84. if (deps[i]->compiled[COMPILE_NORMAL])
  85. list = talloc_asprintf_append(list, " %s",
  86. deps[i]->compiled
  87. [COMPILE_NORMAL]);
  88. }
  89. return list;
  90. }
  91. /* FIXME: Test with reduced features! */
  92. static char *lib_list(const struct manifest *m)
  93. {
  94. unsigned int i, num;
  95. char **libs;
  96. char *ret = talloc_strdup(m, "");
  97. libs = get_libs(m, m->dir, &num,
  98. &m->info_file->compiled[COMPILE_NORMAL]);
  99. for (i = 0; i < num; i++)
  100. ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
  101. return ret;
  102. }
  103. static bool compile(const void *ctx,
  104. struct manifest *m,
  105. struct ccan_file *file,
  106. bool keep, char **output)
  107. {
  108. file->compiled[COMPILE_NORMAL]
  109. = maybe_temp_file(ctx, "", keep, file->fullname);
  110. if (!compile_and_link(ctx, file->fullname, ccan_dir,
  111. example_obj_list(m, file),
  112. compiler, cflags,
  113. lib_list(m), file->compiled[COMPILE_NORMAL],
  114. output)) {
  115. /* Don't keep failures. */
  116. if (keep)
  117. unlink(file->compiled[COMPILE_NORMAL]);
  118. talloc_free(file->compiled[COMPILE_NORMAL]);
  119. file->compiled[COMPILE_NORMAL] = NULL;
  120. return false;
  121. }
  122. return true;
  123. }
  124. static char *start_main(char *ret, const char *why)
  125. {
  126. return talloc_asprintf_append(ret,
  127. "/* The example %s, so fake function wrapper inserted */\n"
  128. "int main(int argc, char *argv[])\n"
  129. "{\n", why);
  130. }
  131. /* We only handle simple function definitions here. */
  132. static char *add_func(char *others, const char *line)
  133. {
  134. const char *p, *end = strchr(line, '(') - 1;
  135. while (cisspace(*end)) {
  136. end--;
  137. if (end == line)
  138. return others;
  139. }
  140. for (p = end; cisalnum(*p) || *p == '_'; p--) {
  141. if (p == line)
  142. return others;
  143. }
  144. return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
  145. (unsigned)(end - p), p+1);
  146. }
  147. static void strip_leading_whitespace(char **lines)
  148. {
  149. unsigned int i, min_span = -1U;
  150. for (i = 0; lines[i]; i++) {
  151. unsigned int span = strspn(lines[i], " \t");
  152. /* All whitespace? Ignore */
  153. if (!lines[i][span])
  154. continue;
  155. if (span < min_span)
  156. min_span = span;
  157. }
  158. for (i = 0; lines[i]; i++)
  159. if (strlen(lines[i]) >= min_span)
  160. lines[i] += min_span;
  161. }
  162. static bool looks_internal(char **lines, char **why)
  163. {
  164. unsigned int i;
  165. bool last_ended = true; /* Did last line finish a statement? */
  166. for (i = 0; lines[i]; i++) {
  167. /* Skip leading whitespace. */
  168. const char *line = lines[i] + strspn(lines[i], " \t");
  169. unsigned len = strspn(line, IDENT_CHARS);
  170. if (!line[0] || cisspace(line[0]) || strstarts(line, "//"))
  171. continue;
  172. /* The winners. */
  173. if (strstarts(line, "if") && len == 2) {
  174. *why = cast_const(char *, "starts with if");
  175. return true;
  176. }
  177. if (strstarts(line, "for") && len == 3) {
  178. *why = cast_const(char *, "starts with for");
  179. return true;
  180. }
  181. if (strstarts(line, "while") && len == 5) {
  182. *why = cast_const(char *, "starts with while");
  183. return true;
  184. }
  185. if (strstarts(line, "do") && len == 2) {
  186. *why = cast_const(char *, "starts with do");
  187. return true;
  188. }
  189. /* The losers. */
  190. if (strstarts(line, "#include")) {
  191. *why = cast_const(char *, "starts with #include");
  192. return false;
  193. }
  194. if (last_ended && strchr(line, '(')) {
  195. if (strstarts(line, "static")) {
  196. *why = cast_const(char *,
  197. "starts with static"
  198. " and contains (");
  199. return false;
  200. }
  201. if (strends(line, ")")) {
  202. *why = cast_const(char *,
  203. "contains ( and ends with )");
  204. return false;
  205. }
  206. }
  207. /* Single identifier then operator == inside function. */
  208. if (last_ended && len
  209. && cispunct(line[len+strspn(line+len, " ")])) {
  210. *why = cast_const(char *, "starts with identifier"
  211. " then punctuation");
  212. return true;
  213. }
  214. last_ended = (strends(line, "}")
  215. || strends(line, ";")
  216. || streq(line, "..."));
  217. }
  218. /* No idea... Say yes? */
  219. *why = cast_const(char *, "gave no clues");
  220. return true;
  221. }
  222. /* Examples will often build on prior ones. Try combining them. */
  223. static char **combine(const void *ctx, char **lines, char **prev)
  224. {
  225. unsigned int i, lines_total, prev_total, count;
  226. char **ret;
  227. const char *reasoning;
  228. char *why = NULL;
  229. if (!prev)
  230. return NULL;
  231. /* If it looks internal, put prev at start. */
  232. if (looks_internal(lines, &why)) {
  233. count = 0;
  234. reasoning = "seemed to belong inside a function";
  235. } else {
  236. /* Try inserting in first elided position */
  237. for (count = 0; lines[count]; count++) {
  238. if (strcmp(lines[count], "...") == 0)
  239. break;
  240. }
  241. if (!lines[count]) {
  242. /* Try at start anyway? */
  243. count = 0;
  244. reasoning = "didn't seem to belong inside"
  245. " a function, so we prepended the previous"
  246. " example";
  247. } else {
  248. reasoning = "didn't seem to belong inside"
  249. " a function, so we put the previous example"
  250. " at the first ...";
  251. count++;
  252. }
  253. }
  254. for (i = 0; lines[i]; i++);
  255. lines_total = i;
  256. for (i = 0; prev[i]; i++);
  257. prev_total = i;
  258. ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
  259. ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */\n",
  260. why, reasoning);
  261. memcpy(ret+1, lines, count * sizeof(ret[0]));
  262. memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
  263. memcpy(ret+1 + count + prev_total, lines + count,
  264. (lines_total - count + 1) * sizeof(ret[0]));
  265. return ret;
  266. }
  267. /* Only handles very simple comments. */
  268. static char *strip_comment(const void *ctx, const char *orig_line)
  269. {
  270. char *p, *ret = talloc_strdup(ctx, orig_line);
  271. p = strstr(ret, "/*");
  272. if (!p)
  273. p = strstr(ret, "//");
  274. if (p)
  275. *p = '\0';
  276. return ret;
  277. }
  278. static char *mangle(struct manifest *m, char **lines)
  279. {
  280. char *ret, *use_funcs = NULL, *why;
  281. bool in_function = false, fake_function = false, has_main = false;
  282. unsigned int i;
  283. ret = talloc_asprintf(m,
  284. "/* Include header from module. */\n"
  285. "#include <ccan/%s/%s.h>\n"
  286. "/* Prepend a heap of headers. */\n"
  287. "#include <assert.h>\n"
  288. "#include <err.h>\n"
  289. "#include <errno.h>\n"
  290. "#include <fcntl.h>\n"
  291. "#include <limits.h>\n"
  292. "#include <stdbool.h>\n"
  293. "#include <stdint.h>\n"
  294. "#include <stdio.h>\n"
  295. "#include <stdlib.h>\n"
  296. "#include <string.h>\n"
  297. "#include <sys/stat.h>\n"
  298. "#include <sys/types.h>\n"
  299. "#include <unistd.h>\n",
  300. m->basename, m->basename);
  301. ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
  302. "extern int somefunc(void);\n"
  303. "int somefunc(void) { return 0; }\n"
  304. "extern char somestring[];\n"
  305. "char somestring[] = \"hello world\";\n");
  306. if (looks_internal(lines, &why)) {
  307. /* Wrap it all in main(). */
  308. ret = start_main(ret, why);
  309. fake_function = true;
  310. in_function = true;
  311. has_main = true;
  312. } else
  313. ret = talloc_asprintf_append(ret,
  314. "/* The example %s, so didn't wrap in main() */\n",
  315. why);
  316. /* Primitive, very primitive. */
  317. for (i = 0; lines[i]; i++) {
  318. char *line = strip_comment(ret, lines[i]);
  319. /* } at start of line ends a function. */
  320. if (in_function) {
  321. if (line[0] == '}')
  322. in_function = false;
  323. } else {
  324. /* Character at start of line, with ( and no ;
  325. * == function start. Ignore comments. */
  326. if (!cisspace(line[0])
  327. && strchr(line, '(')
  328. && !strchr(line, ';')
  329. && !strstr(line, "//")) {
  330. in_function = true;
  331. if (strncmp(line, "int main", 8) == 0)
  332. has_main = true;
  333. if (strncmp(line, "static", 6) == 0) {
  334. use_funcs = add_func(use_funcs,
  335. line);
  336. }
  337. }
  338. }
  339. /* ... means elided code. */
  340. if (strcmp(line, "...") == 0) {
  341. if (!in_function && !has_main
  342. && looks_internal(lines + i + 1, &why)) {
  343. /* This implies we start a function here. */
  344. ret = start_main(ret, why);
  345. has_main = true;
  346. fake_function = true;
  347. in_function = true;
  348. }
  349. ret = talloc_asprintf_append(ret,
  350. "/* ... removed */\n");
  351. continue;
  352. }
  353. ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
  354. }
  355. if (!has_main) {
  356. ret = talloc_asprintf_append(ret,
  357. "/* Need a main to link successfully. */\n"
  358. "int main(void)\n{\n");
  359. fake_function = true;
  360. }
  361. if (use_funcs) {
  362. ret = talloc_asprintf_append(ret,
  363. "/* Get rid of unused warnings"
  364. " by printing addresses of"
  365. " static funcs. */\n");
  366. if (!fake_function) {
  367. ret = talloc_asprintf_append(ret,
  368. "int use_funcs(void);\n"
  369. "int use_funcs(void) {\n");
  370. fake_function = true;
  371. }
  372. ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
  373. }
  374. if (fake_function)
  375. ret = talloc_asprintf_append(ret, "return 0;\n"
  376. "}\n");
  377. return ret;
  378. }
  379. static struct ccan_file *mangle_example(struct manifest *m,
  380. struct ccan_file *example,
  381. char **lines,
  382. bool keep)
  383. {
  384. char *name, *contents;
  385. int fd;
  386. struct ccan_file *f;
  387. name = maybe_temp_file(example, ".c", keep,
  388. talloc_asprintf(m, "%s/mangled-%s",
  389. m->dir, example->name));
  390. f = new_ccan_file(example,
  391. talloc_dirname(example, name),
  392. talloc_basename(example, name));
  393. talloc_steal(f, name);
  394. fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
  395. if (fd < 0)
  396. return NULL;
  397. contents = mangle(m, lines);
  398. if (write(fd, contents, strlen(contents)) != strlen(contents)) {
  399. close(fd);
  400. return NULL;
  401. }
  402. close(fd);
  403. f->contents = talloc_steal(f, contents);
  404. list_add(&m->mangled_examples, &f->list);
  405. return f;
  406. }
  407. /* If an example has expected output, it's complete and should not be
  408. * included in future examples. */
  409. static bool has_expected_output(char **lines)
  410. {
  411. unsigned int i;
  412. for (i = 0; lines[i]; i++) {
  413. char *p = lines[i] + strspn(lines[i], " \t");
  414. if (!strstarts(p, "//"))
  415. continue;
  416. p += strspn(p, "/ ");
  417. if (strncasecmp(p, "given", strlen("given")) == 0)
  418. return true;
  419. }
  420. return false;
  421. }
  422. static unsigned int try_compiling(struct manifest *m,
  423. struct ccan_file *i,
  424. char **prev,
  425. bool keep,
  426. struct ccan_file *mangled[3],
  427. bool res[3],
  428. char *err[3],
  429. char **lines[3])
  430. {
  431. unsigned int num;
  432. /* Try standalone. */
  433. mangled[0] = i;
  434. res[0] = compile(i, m, mangled[0], keep, &err[0]);
  435. lines[0] = get_ccan_file_lines(i);
  436. if (res[0] && streq(err[0], ""))
  437. return 1;
  438. if (prev) {
  439. lines[1] = combine(i, get_ccan_file_lines(i), prev);
  440. mangled[1] = mangle_example(m, i, lines[1], keep);
  441. res[1] = compile(i, m, mangled[1], keep, &err[1]);
  442. if (res[1] && streq(err[1], "")) {
  443. return 2;
  444. }
  445. num = 2;
  446. } else
  447. num = 1;
  448. /* Try standalone. */
  449. lines[num] = get_ccan_file_lines(i);
  450. mangled[num] = mangle_example(m, i, lines[num], keep);
  451. res[num] = compile(i, m, mangled[num], keep, &err[num]);
  452. return num+1;
  453. }
  454. static void build_examples(struct manifest *m, bool keep,
  455. unsigned int *timeleft, struct score *score)
  456. {
  457. struct ccan_file *i;
  458. char **prev = NULL;
  459. bool warnings = false;
  460. score->total = 0;
  461. score->pass = true;
  462. list_for_each(&m->examples, i, list) {
  463. char *err[3];
  464. struct ccan_file *file[3] = { NULL, NULL, NULL };
  465. bool res[3];
  466. unsigned num, j;
  467. char **lines[3];
  468. const char *error;
  469. score->total++;
  470. /* Simplify our dumb parsing. */
  471. strip_leading_whitespace(get_ccan_file_lines(i));
  472. num = try_compiling(m, i, prev, keep, file, res, err, lines);
  473. /* First look for a compile without any warnings. */
  474. for (j = 0; j < num; j++) {
  475. if (res[j] && streq(err[j], "")) {
  476. if (!has_expected_output(lines[j]))
  477. prev = lines[j];
  478. score->score++;
  479. goto next;
  480. }
  481. }
  482. /* Now accept anything which succeeded. */
  483. for (j = 0; j < num; j++) {
  484. if (res[j]) {
  485. if (!has_expected_output(lines[j]))
  486. prev = lines[j];
  487. score->score++;
  488. warnings = true;
  489. score_file_error(score, file[j], 0,
  490. "Compiling extracted example"
  491. " gave warnings:\n"
  492. "Example:\n"
  493. "%s\n"
  494. "Compiler:\n"
  495. "%s",
  496. get_ccan_file_contents(file[j]),
  497. err[j]);
  498. goto next;
  499. }
  500. }
  501. score->pass = false;
  502. if (!verbose) {
  503. if (num == 3)
  504. error = "Compiling standalone, adding headers, "
  505. "and including previous "
  506. "example all failed";
  507. else
  508. error = "Standalone compile and"
  509. " adding headers both failed";
  510. } else {
  511. if (num == 3) {
  512. error = talloc_asprintf(score,
  513. "Standalone example:\n"
  514. "%s\n"
  515. "Errors: %s\n\n"
  516. "Combining with previous example:\n"
  517. "%s\n"
  518. "Errors: %s\n\n"
  519. "Adding headers, wrappers:\n"
  520. "%s\n"
  521. "Errors: %s\n\n",
  522. get_ccan_file_contents(file[0]),
  523. err[0],
  524. get_ccan_file_contents(file[1]),
  525. err[1],
  526. get_ccan_file_contents(file[2]),
  527. err[2]);
  528. } else {
  529. error = talloc_asprintf(score,
  530. "Standalone example:\n"
  531. "%s\n"
  532. "Errors: %s\n\n"
  533. "Adding headers, wrappers:\n"
  534. "%s\n"
  535. "Errors: %s\n\n",
  536. get_ccan_file_contents(file[0]),
  537. err[0],
  538. get_ccan_file_contents(file[1]),
  539. err[1]);
  540. }
  541. }
  542. score_file_error(score, i, 0, "%s", error);
  543. /* This didn't work, so not a candidate for combining. */
  544. prev = NULL;
  545. next:
  546. ;
  547. }
  548. /* An extra point if they all compiled without warnings. */
  549. if (!list_empty(&m->examples)) {
  550. score->total++;
  551. if (!warnings)
  552. score->score++;
  553. }
  554. }
  555. struct ccanlint examples_compile = {
  556. .key = "examples_compile",
  557. .name = "Module examples compile",
  558. .check = build_examples,
  559. .can_run = can_run,
  560. .needs = "examples_exist module_builds"
  561. };
  562. REGISTER_TEST(examples_compile);