examples_compile.c 12 KB

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