examples_compile.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 if there's no object file for that module */
  35. if (!expect_obj_file(talloc_asprintf(list, "%s/ccan/%s", ccan_dir,mod)))
  36. return list;
  37. obj = talloc_asprintf(list, "%s/ccan/%s.o", ccan_dir, mod);
  38. /* Not anyone we've already included. */
  39. if (strstr(list, obj))
  40. return list;
  41. list = talloc_asprintf_append(list, " %s", obj);
  42. /* Get that modules depends as well... */
  43. assert(!safe_mode);
  44. deps = get_deps(m, talloc_asprintf(list, "%s/ccan/%s", ccan_dir, mod),
  45. false, NULL);
  46. for (i = 0; deps[i]; i++) {
  47. if (strstarts(deps[i], "ccan/"))
  48. list = add_dep(m, list, deps[i] + strlen("ccan/"));
  49. }
  50. return list;
  51. }
  52. static char *obj_list(const struct manifest *m, struct ccan_file *f)
  53. {
  54. char *list = talloc_strdup(m, "");
  55. struct ccan_file *i;
  56. char **lines;
  57. /* Object files for this module. */
  58. list_for_each(&m->c_files, i, list)
  59. list = talloc_asprintf_append(list, " %s", i->compiled);
  60. /* Other ccan modules we depend on. */
  61. list_for_each(&m->dep_dirs, i, list) {
  62. if (i->compiled)
  63. list = talloc_asprintf_append(list, " %s", i->compiled);
  64. }
  65. /* Other modules implied by includes. */
  66. for (lines = get_ccan_file_lines(f); *lines; lines++) {
  67. unsigned preflen = strspn(*lines, " \t");
  68. if (strstarts(*lines + preflen, "#include <ccan/")) {
  69. const char *mod;
  70. unsigned modlen;
  71. mod = *lines + preflen + strlen("#include <ccan/");
  72. modlen = strcspn(mod, "/");
  73. mod = talloc_strndup(f, mod, modlen);
  74. /* Not ourselves. */
  75. if (streq(m->basename, mod))
  76. continue;
  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. 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. for (i = 0; lines[i]; i++) {
  153. unsigned len = strspn(lines[i], IDENT_CHARS);
  154. /* The winners. */
  155. if (strstarts(lines[i], "if") && len == 2)
  156. return true;
  157. if (strstarts(lines[i], "for") && len == 3)
  158. return true;
  159. if (strstarts(lines[i], "while") && len == 5)
  160. return true;
  161. if (strstarts(lines[i], "do") && len == 2)
  162. return true;
  163. /* The losers. */
  164. if (strchr(lines[i], '(')) {
  165. if (strstarts(lines[i], "static"))
  166. return false;
  167. if (strends(lines[i], ")"))
  168. return false;
  169. }
  170. }
  171. /* No idea... Say no? */
  172. return false;
  173. }
  174. /* Examples will often build on prior ones. Try combining them. */
  175. static char **combine(char **lines, char **prev)
  176. {
  177. unsigned int i, lines_total, prev_total, count;
  178. char **ret;
  179. if (!prev)
  180. return NULL;
  181. strip_leading_whitespace(lines);
  182. /* If it looks internal, put prev at start. */
  183. if (looks_internal(lines)) {
  184. count = 0;
  185. } else {
  186. /* Try inserting in first elided position */
  187. for (count = 0; lines[count]; count++) {
  188. if (strcmp(lines[count], "...") == 0)
  189. break;
  190. }
  191. if (!lines[count])
  192. /* Try at start anyway? */
  193. count = 0;
  194. else
  195. count++;
  196. }
  197. for (i = 0; lines[i]; i++);
  198. lines_total = i;
  199. for (i = 0; prev[i]; i++);
  200. prev_total = i;
  201. ret = talloc_array(lines, char *, lines_total + prev_total + 1);
  202. memcpy(ret, lines, count * sizeof(ret[0]));
  203. memcpy(ret + count, prev, prev_total * sizeof(ret[0]));
  204. memcpy(ret + count + prev_total, lines + count,
  205. (lines_total - count + 1) * sizeof(ret[0]));
  206. return ret;
  207. }
  208. static char *mangle(struct manifest *m, char **lines)
  209. {
  210. char *ret, *use_funcs = NULL;
  211. bool in_function = false, fake_function = false, has_main = false;
  212. unsigned int i;
  213. ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
  214. "#include <assert.h>\n"
  215. "#include <err.h>\n"
  216. "#include <errno.h>\n"
  217. "#include <fcntl.h>\n"
  218. "#include <limits.h>\n"
  219. "#include <stdbool.h>\n"
  220. "#include <stdint.h>\n"
  221. "#include <stdio.h>\n"
  222. "#include <stdlib.h>\n"
  223. "#include <string.h>\n"
  224. "#include <sys/stat.h>\n"
  225. "#include <sys/types.h>\n"
  226. "#include <unistd.h>\n");
  227. ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
  228. "#include <ccan/%s/%s.h>\n",
  229. m->basename, m->basename);
  230. ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
  231. "int somefunc(void);\n"
  232. "int somefunc(void) { return 0; }\n");
  233. strip_leading_whitespace(lines);
  234. if (looks_internal(lines)) {
  235. /* Wrap it all in main(). */
  236. ret = start_main(ret);
  237. fake_function = true;
  238. in_function = true;
  239. has_main = true;
  240. }
  241. /* Primitive, very primitive. */
  242. for (i = 0; lines[i]; i++) {
  243. /* } at start of line ends a function. */
  244. if (in_function) {
  245. if (lines[i][0] == '}')
  246. in_function = false;
  247. } else {
  248. /* Character at start of line, with ( and no ;
  249. * == function start. Ignore comments. */
  250. if (!isblank(lines[i][0])
  251. && strchr(lines[i], '(')
  252. && !strchr(lines[i], ';')
  253. && !strstr(lines[i], "//")) {
  254. in_function = true;
  255. if (strncmp(lines[i], "int main", 8) == 0)
  256. has_main = true;
  257. if (strncmp(lines[i], "static", 6) == 0) {
  258. use_funcs = add_func(use_funcs,
  259. lines[i]);
  260. }
  261. }
  262. }
  263. /* ... means elided code. If followed by spaced line, means
  264. * next part is supposed to be inside a function. */
  265. if (strcmp(lines[i], "...") == 0) {
  266. if (!in_function
  267. && lines[i+1]
  268. && isblank(lines[i+1][0])) {
  269. /* This implies we start a function here. */
  270. ret = start_main(ret);
  271. has_main = true;
  272. fake_function = true;
  273. in_function = true;
  274. }
  275. ret = talloc_asprintf_append(ret,
  276. "/* ... removed */\n");
  277. continue;
  278. }
  279. ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
  280. }
  281. /* Need a main to link successfully. */
  282. if (!has_main) {
  283. ret = talloc_asprintf_append(ret, "int main(void)\n{\n");
  284. fake_function = true;
  285. }
  286. /* Get rid of unused warnings by printing addresses of static funcs. */
  287. if (use_funcs) {
  288. if (!fake_function) {
  289. ret = talloc_asprintf_append(ret,
  290. "int use_funcs(void);\n"
  291. "int use_funcs(void) {\n");
  292. fake_function = true;
  293. }
  294. ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
  295. }
  296. if (fake_function)
  297. ret = talloc_asprintf_append(ret, "return 0;\n"
  298. "}\n");
  299. return ret;
  300. }
  301. static struct ccan_file *mangle_example(struct manifest *m,
  302. struct ccan_file *example,
  303. char **lines,
  304. bool keep)
  305. {
  306. char *name, *contents;
  307. int fd;
  308. struct ccan_file *f;
  309. name = maybe_temp_file(example, ".c", keep,
  310. talloc_asprintf(m, "%s/mangled-%s",
  311. m->dir, example->name));
  312. f = new_ccan_file(example,
  313. talloc_dirname(example, name),
  314. talloc_basename(example, name));
  315. talloc_steal(f, name);
  316. fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
  317. if (fd < 0)
  318. return NULL;
  319. contents = mangle(m, lines);
  320. if (write(fd, contents, strlen(contents)) != strlen(contents)) {
  321. close(fd);
  322. return NULL;
  323. }
  324. close(fd);
  325. f->contents = talloc_steal(f, contents);
  326. return f;
  327. }
  328. static void *build_examples(struct manifest *m, bool keep,
  329. unsigned int *timeleft)
  330. {
  331. struct ccan_file *i;
  332. struct score *score = talloc(m, struct score);
  333. struct ccan_file *mangle;
  334. char **prev = NULL;
  335. score->score = 0;
  336. score->errors = NULL;
  337. list_for_each(&m->examples, i, list) {
  338. char *ret;
  339. examples_compile.total_score++;
  340. ret = compile(score, m, i, keep);
  341. if (!ret) {
  342. prev = get_ccan_file_lines(i);
  343. score->score++;
  344. continue;
  345. }
  346. talloc_free(ret);
  347. mangle = mangle_example(m, i, get_ccan_file_lines(i), keep);
  348. ret = compile(score, m, mangle, keep);
  349. if (!ret) {
  350. prev = get_ccan_file_lines(i);
  351. score->score++;
  352. continue;
  353. }
  354. /* Try combining with previous (successful) example... */
  355. if (prev) {
  356. prev = combine(get_ccan_file_lines(i), prev);
  357. talloc_free(ret);
  358. /* We're going to replace this failure. */
  359. if (keep)
  360. unlink(mangle->fullname);
  361. talloc_free(mangle);
  362. mangle = mangle_example(m, i, prev, keep);
  363. ret = compile(score, m, mangle, keep);
  364. if (!ret) {
  365. score->score++;
  366. continue;
  367. }
  368. }
  369. if (!score->errors)
  370. score->errors = ret;
  371. else {
  372. score->errors = talloc_append_string(score->errors,
  373. ret);
  374. talloc_free(ret);
  375. }
  376. /* This didn't work, so not a candidate for combining. */
  377. talloc_free(prev);
  378. prev = NULL;
  379. }
  380. return score;
  381. }
  382. static unsigned int score_examples(struct manifest *m, void *check_result)
  383. {
  384. struct score *score = check_result;
  385. return score->score;
  386. }
  387. static const char *describe(struct manifest *m, void *check_result)
  388. {
  389. struct score *score = check_result;
  390. if (verbose >= 2 && score->errors)
  391. return talloc_asprintf(m, "Compile errors building examples:\n"
  392. "%s", score->errors);
  393. return NULL;
  394. }
  395. struct ccanlint examples_compile = {
  396. .key = "examples-compile",
  397. .name = "Module examples compile",
  398. .score = score_examples,
  399. .check = build_examples,
  400. .describe = describe,
  401. .can_run = can_run,
  402. };
  403. REGISTER_TEST(examples_compile, &has_examples, NULL);