examples_compile.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/str/str.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <ctype.h>
  12. #include <assert.h>
  13. static const char *can_run(struct manifest *m)
  14. {
  15. if (safe_mode)
  16. return "Safe mode enabled";
  17. if (list_empty(&m->examples))
  18. return "No examples to compile";
  19. return NULL;
  20. }
  21. /* FIXME: We should build if it doesn't exist... */
  22. static bool expect_obj_file(const char *dir)
  23. {
  24. struct manifest *dep_man;
  25. bool has_c_files;
  26. dep_man = get_manifest(dir, dir);
  27. /* If it has C files, we expect an object file built from them. */
  28. has_c_files = !list_empty(&dep_man->c_files);
  29. talloc_free(dep_man);
  30. return has_c_files;
  31. }
  32. static char *add_dep(const struct manifest *m, char *list, const char *mod)
  33. {
  34. char **deps, *obj;
  35. unsigned int i;
  36. /* Not ourselves. */
  37. if (streq(m->basename, mod))
  38. return list;
  39. /* Not if there's no object file for that module */
  40. if (!expect_obj_file(talloc_asprintf(list, "%s/ccan/%s", ccan_dir,mod)))
  41. return list;
  42. obj = talloc_asprintf(list, "%s/ccan/%s.o", ccan_dir, mod);
  43. /* Not anyone we've already included. */
  44. if (strstr(list, obj))
  45. return list;
  46. list = talloc_asprintf_append(list, " %s", obj);
  47. /* Get that modules depends as well... */
  48. assert(!safe_mode);
  49. deps = get_deps(m, talloc_asprintf(list, "%s/ccan/%s", ccan_dir, mod),
  50. false, NULL);
  51. for (i = 0; deps[i]; i++) {
  52. if (strstarts(deps[i], "ccan/"))
  53. list = add_dep(m, list, deps[i] + strlen("ccan/"));
  54. }
  55. return list;
  56. }
  57. static char *obj_list(const struct manifest *m, struct ccan_file *f)
  58. {
  59. char *list = talloc_strdup(m, "");
  60. struct ccan_file *i;
  61. char **lines;
  62. /* Object files for this module. */
  63. list_for_each(&m->c_files, i, list)
  64. list = talloc_asprintf_append(list, " %s", i->compiled);
  65. /* Other ccan modules we depend on. */
  66. list_for_each(&m->dep_dirs, i, list) {
  67. if (i->compiled)
  68. list = talloc_asprintf_append(list, " %s", i->compiled);
  69. }
  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. const char *mod;
  75. unsigned modlen;
  76. mod = *lines + preflen + strlen("#include <ccan/");
  77. modlen = strcspn(mod, "/");
  78. mod = talloc_strndup(f, mod, modlen);
  79. list = add_dep(m, list, mod);
  80. }
  81. }
  82. return list;
  83. }
  84. static char *lib_list(const struct manifest *m)
  85. {
  86. unsigned int i, num;
  87. char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
  88. char *ret = talloc_strdup(m, "");
  89. for (i = 0; i < num; i++)
  90. ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
  91. return ret;
  92. }
  93. static char *compile(const void *ctx,
  94. struct manifest *m,
  95. struct ccan_file *file,
  96. bool keep)
  97. {
  98. char *errmsg;
  99. file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
  100. errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
  101. obj_list(m, file),
  102. "", lib_list(m), file->compiled);
  103. if (errmsg) {
  104. talloc_free(file->compiled);
  105. file->compiled = NULL;
  106. return errmsg;
  107. }
  108. return NULL;
  109. }
  110. static char *start_main(char *ret, const char *why)
  111. {
  112. return talloc_asprintf_append(ret,
  113. "/* The example %s, so fake function wrapper inserted */\n"
  114. "int main(int argc, char *argv[])\n"
  115. "{\n", why);
  116. }
  117. /* We only handle simple function definitions here. */
  118. static char *add_func(char *others, const char *line)
  119. {
  120. const char *p, *end = strchr(line, '(') - 1;
  121. while (isblank(*end)) {
  122. end--;
  123. if (end == line)
  124. return others;
  125. }
  126. for (p = end; isalnum(*p) || *p == '_'; p--) {
  127. if (p == line)
  128. return others;
  129. }
  130. return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
  131. (unsigned)(end - p + 1), p);
  132. }
  133. static void strip_leading_whitespace(char **lines)
  134. {
  135. unsigned int i, min_span = -1U;
  136. for (i = 0; lines[i]; i++) {
  137. unsigned int span = strspn(lines[i], " \t");
  138. /* All whitespace? Ignore */
  139. if (!lines[i][span])
  140. continue;
  141. if (span < min_span)
  142. min_span = span;
  143. }
  144. for (i = 0; lines[i]; i++)
  145. if (strlen(lines[i]) >= min_span)
  146. lines[i] += min_span;
  147. }
  148. static bool looks_internal(char **lines, char **why)
  149. {
  150. unsigned int i;
  151. bool last_ended = true; /* Did last line finish a statement? */
  152. for (i = 0; lines[i]; i++) {
  153. /* Skip leading whitespace. */
  154. const char *line = lines[i] + strspn(lines[i], " \t");
  155. unsigned len = strspn(line, IDENT_CHARS);
  156. if (!line[0] || isblank(line[0]) || strstarts(line, "//"))
  157. continue;
  158. /* The winners. */
  159. if (strstarts(line, "if") && len == 2) {
  160. *why = "starts with if";
  161. return true;
  162. }
  163. if (strstarts(line, "for") && len == 3) {
  164. *why = "starts with for";
  165. return true;
  166. }
  167. if (strstarts(line, "while") && len == 5) {
  168. *why = "starts with while";
  169. return true;
  170. }
  171. if (strstarts(line, "do") && len == 2) {
  172. *why = "starts with do";
  173. return true;
  174. }
  175. /* The losers. */
  176. if (strstarts(line, "#include")) {
  177. *why = "starts with #include";
  178. return false;
  179. }
  180. if (last_ended && strchr(line, '(')) {
  181. if (strstarts(line, "static")) {
  182. *why = "starts with static and contains (";
  183. return false;
  184. }
  185. if (strends(line, ")")) {
  186. *why = "contains ( and ends with )";
  187. return false;
  188. }
  189. }
  190. /* Single identifier then operator == inside function. */
  191. if (last_ended && len
  192. && ispunct(line[len+strspn(line+len, " ")])) {
  193. *why = "starts with identifier then punctuation";
  194. return true;
  195. }
  196. last_ended = (strends(line, "}")
  197. || strends(line, ";")
  198. || streq(line, "..."));
  199. }
  200. /* No idea... Say yes? */
  201. *why = "gave no clues";
  202. return true;
  203. }
  204. /* Examples will often build on prior ones. Try combining them. */
  205. static char **combine(const void *ctx, char **lines, char **prev)
  206. {
  207. unsigned int i, lines_total, prev_total, count;
  208. char **ret;
  209. const char *reasoning;
  210. char *why = NULL;
  211. if (!prev)
  212. return NULL;
  213. /* If it looks internal, put prev at start. */
  214. if (looks_internal(lines, &why)) {
  215. count = 0;
  216. reasoning = "seemed to belong inside a function";
  217. } else {
  218. /* Try inserting in first elided position */
  219. for (count = 0; lines[count]; count++) {
  220. if (strcmp(lines[count], "...") == 0)
  221. break;
  222. }
  223. if (!lines[count]) {
  224. /* Try at start anyway? */
  225. count = 0;
  226. reasoning = "didn't seem to belong inside"
  227. " a function, so we prepended the previous"
  228. " example";
  229. } else {
  230. reasoning = "didn't seem to belong inside"
  231. " a function, so we put the previous example"
  232. " at the first ...";
  233. count++;
  234. }
  235. }
  236. for (i = 0; lines[i]; i++);
  237. lines_total = i;
  238. for (i = 0; prev[i]; i++);
  239. prev_total = i;
  240. ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
  241. ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */\n",
  242. why, reasoning);
  243. memcpy(ret+1, lines, count * sizeof(ret[0]));
  244. memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
  245. memcpy(ret+1 + count + prev_total, lines + count,
  246. (lines_total - count + 1) * sizeof(ret[0]));
  247. return ret;
  248. }
  249. static char *mangle(struct manifest *m, char **lines)
  250. {
  251. char *ret, *use_funcs = NULL, *why;
  252. bool in_function = false, fake_function = false, has_main = false;
  253. unsigned int i;
  254. ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
  255. "#include <assert.h>\n"
  256. "#include <err.h>\n"
  257. "#include <errno.h>\n"
  258. "#include <fcntl.h>\n"
  259. "#include <limits.h>\n"
  260. "#include <stdbool.h>\n"
  261. "#include <stdint.h>\n"
  262. "#include <stdio.h>\n"
  263. "#include <stdlib.h>\n"
  264. "#include <string.h>\n"
  265. "#include <sys/stat.h>\n"
  266. "#include <sys/types.h>\n"
  267. "#include <unistd.h>\n");
  268. ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
  269. "#include <ccan/%s/%s.h>\n",
  270. m->basename, m->basename);
  271. ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
  272. "extern int somefunc(void);\n"
  273. "int somefunc(void) { return 0; }\n"
  274. "extern char somestring[];\n"
  275. "char somestring[] = \"hello world\";\n");
  276. if (looks_internal(lines, &why)) {
  277. /* Wrap it all in main(). */
  278. ret = start_main(ret, why);
  279. fake_function = true;
  280. in_function = true;
  281. has_main = true;
  282. } else
  283. ret = talloc_asprintf_append(ret,
  284. "/* The example %s, so didn't wrap in main() */\n",
  285. why);
  286. /* Primitive, very primitive. */
  287. for (i = 0; lines[i]; i++) {
  288. /* } at start of line ends a function. */
  289. if (in_function) {
  290. if (lines[i][0] == '}')
  291. in_function = false;
  292. } else {
  293. /* Character at start of line, with ( and no ;
  294. * == function start. Ignore comments. */
  295. if (!isblank(lines[i][0])
  296. && strchr(lines[i], '(')
  297. && !strchr(lines[i], ';')
  298. && !strstr(lines[i], "//")) {
  299. in_function = true;
  300. if (strncmp(lines[i], "int main", 8) == 0)
  301. has_main = true;
  302. if (strncmp(lines[i], "static", 6) == 0) {
  303. use_funcs = add_func(use_funcs,
  304. lines[i]);
  305. }
  306. }
  307. }
  308. /* ... means elided code. */
  309. if (strcmp(lines[i], "...") == 0) {
  310. if (!in_function && !has_main
  311. && looks_internal(lines + i + 1, &why)) {
  312. /* This implies we start a function here. */
  313. ret = start_main(ret, why);
  314. has_main = true;
  315. fake_function = true;
  316. in_function = true;
  317. }
  318. ret = talloc_asprintf_append(ret,
  319. "/* ... removed */\n");
  320. continue;
  321. }
  322. ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
  323. }
  324. if (!has_main) {
  325. ret = talloc_asprintf_append(ret,
  326. "/* Need a main to link successfully. */\n"
  327. "int main(void)\n{\n");
  328. fake_function = true;
  329. }
  330. if (use_funcs) {
  331. ret = talloc_asprintf_append(ret,
  332. "/* Get rid of unused warnings"
  333. " by printing addresses of"
  334. " static funcs. */");
  335. if (!fake_function) {
  336. ret = talloc_asprintf_append(ret,
  337. "int use_funcs(void);\n"
  338. "int use_funcs(void) {\n");
  339. fake_function = true;
  340. }
  341. ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
  342. }
  343. if (fake_function)
  344. ret = talloc_asprintf_append(ret, "return 0;\n"
  345. "}\n");
  346. return ret;
  347. }
  348. static struct ccan_file *mangle_example(struct manifest *m,
  349. struct ccan_file *example,
  350. char **lines,
  351. bool keep)
  352. {
  353. char *name, *contents;
  354. int fd;
  355. struct ccan_file *f;
  356. name = maybe_temp_file(example, ".c", keep,
  357. talloc_asprintf(m, "%s/mangled-%s",
  358. m->dir, example->name));
  359. f = new_ccan_file(example,
  360. talloc_dirname(example, name),
  361. talloc_basename(example, name));
  362. talloc_steal(f, name);
  363. fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
  364. if (fd < 0)
  365. return NULL;
  366. contents = mangle(m, lines);
  367. if (write(fd, contents, strlen(contents)) != strlen(contents)) {
  368. close(fd);
  369. return NULL;
  370. }
  371. close(fd);
  372. f->contents = talloc_steal(f, contents);
  373. list_add(&m->mangled_examples, &f->list);
  374. return f;
  375. }
  376. static void build_examples(struct manifest *m, bool keep,
  377. unsigned int *timeleft, struct score *score)
  378. {
  379. struct ccan_file *i;
  380. char **prev = NULL;
  381. score->total = 0;
  382. score->pass = true;
  383. list_for_each(&m->examples, i, list) {
  384. char *ret, *ret1, *ret2 = NULL;
  385. struct ccan_file *mangle1, *mangle2 = NULL;
  386. char *err;
  387. score->total++;
  388. /* Simplify our dumb parsing. */
  389. strip_leading_whitespace(get_ccan_file_lines(i));
  390. ret = compile(i, m, i, keep);
  391. if (!ret) {
  392. prev = get_ccan_file_lines(i);
  393. score->score++;
  394. continue;
  395. }
  396. /* Try standalone. */
  397. mangle1 = mangle_example(m, i, get_ccan_file_lines(i), keep);
  398. ret1 = compile(i, m, mangle1, keep);
  399. if (!ret1) {
  400. prev = get_ccan_file_lines(i);
  401. score->score++;
  402. continue;
  403. }
  404. /* Try combining with previous (successful) example... */
  405. if (prev) {
  406. char **new = combine(i, get_ccan_file_lines(i), prev);
  407. mangle2 = mangle_example(m, i, new, keep);
  408. ret2 = compile(i, m, mangle2, keep);
  409. if (!ret2) {
  410. prev = new;
  411. score->score++;
  412. continue;
  413. }
  414. }
  415. score->pass = false;
  416. score->error = "Compiling extracted examples failed";
  417. if (!verbose) {
  418. if (mangle2)
  419. err = "Standalone, adding headers, "
  420. "and including previous "
  421. "example all failed";
  422. else
  423. err = "Standalone compile and"
  424. " adding headers both failed";
  425. } else {
  426. err = talloc_asprintf("Standalone example:\n"
  427. "%s\n"
  428. "Errors: %s\n\n"
  429. "Adding headers, wrappers:\n"
  430. "%s\n"
  431. "Errors: %s\n\n",
  432. get_ccan_file_contents(i),
  433. ret,
  434. get_ccan_file_contents(mangle1),
  435. ret1);
  436. if (mangle2)
  437. err = talloc_asprintf_append(err,
  438. "Combining with previous example:\n"
  439. "%s\n"
  440. "Errors: %s\n\n",
  441. get_ccan_file_contents(mangle2),
  442. ret2);
  443. }
  444. score_file_error(score, i, 0, err);
  445. /* This didn't work, so not a candidate for combining. */
  446. prev = NULL;
  447. }
  448. }
  449. struct ccanlint examples_compile = {
  450. .key = "examples-compile",
  451. .name = "Module examples compile",
  452. .check = build_examples,
  453. .can_run = can_run,
  454. };
  455. REGISTER_TEST(examples_compile, &has_examples, NULL);