examples_compile.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. file->compiled = NULL;
  104. return errmsg;
  105. }
  106. return NULL;
  107. }
  108. struct score {
  109. unsigned int score;
  110. char *errors;
  111. };
  112. static char *start_main(char *ret, const char *why)
  113. {
  114. return talloc_asprintf_append(ret,
  115. "/* The example %s, so fake function wrapper inserted */\n"
  116. "int main(int argc, char *argv[])\n"
  117. "{\n", why);
  118. }
  119. /* We only handle simple function definitions here. */
  120. static char *add_func(char *others, const char *line)
  121. {
  122. const char *p, *end = strchr(line, '(') - 1;
  123. while (isblank(*end)) {
  124. end--;
  125. if (end == line)
  126. return others;
  127. }
  128. for (p = end; isalnum(*p) || *p == '_'; p--) {
  129. if (p == line)
  130. return others;
  131. }
  132. return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
  133. (unsigned)(end - p + 1), p);
  134. }
  135. static void strip_leading_whitespace(char **lines)
  136. {
  137. unsigned int i, min_span = -1U;
  138. for (i = 0; lines[i]; i++) {
  139. unsigned int span = strspn(lines[i], " \t");
  140. /* All whitespace? Ignore */
  141. if (!lines[i][span])
  142. continue;
  143. if (span < min_span)
  144. min_span = span;
  145. }
  146. for (i = 0; lines[i]; i++)
  147. if (strlen(lines[i]) >= min_span)
  148. lines[i] += min_span;
  149. }
  150. static bool looks_internal(char **lines, char **why)
  151. {
  152. unsigned int i;
  153. bool last_ended = true; /* Did last line finish a statement? */
  154. for (i = 0; lines[i]; i++) {
  155. /* Skip leading whitespace. */
  156. const char *line = lines[i] + strspn(lines[i], " \t");
  157. unsigned len = strspn(line, IDENT_CHARS);
  158. if (!line[0] || isblank(line[0]) || strstarts(line, "//"))
  159. continue;
  160. /* The winners. */
  161. if (strstarts(line, "if") && len == 2) {
  162. *why = "starts with if";
  163. return true;
  164. }
  165. if (strstarts(line, "for") && len == 3) {
  166. *why = "starts with for";
  167. return true;
  168. }
  169. if (strstarts(line, "while") && len == 5) {
  170. *why = "starts with while";
  171. return true;
  172. }
  173. if (strstarts(line, "do") && len == 2) {
  174. *why = "starts with do";
  175. return true;
  176. }
  177. /* The losers. */
  178. if (strstarts(line, "#include")) {
  179. *why = "starts with #include";
  180. return false;
  181. }
  182. if (last_ended && strchr(line, '(')) {
  183. if (strstarts(line, "static")) {
  184. *why = "starts with static and contains (";
  185. return false;
  186. }
  187. if (strends(line, ")")) {
  188. *why = "contains ( and ends with )";
  189. return false;
  190. }
  191. }
  192. /* Single identifier then operator == inside function. */
  193. if (last_ended && len
  194. && ispunct(line[len+strspn(line+len, " ")])) {
  195. *why = "starts with identifier then punctuation";
  196. return true;
  197. }
  198. last_ended = (strends(line, "}")
  199. || strends(line, ";")
  200. || streq(line, "..."));
  201. }
  202. /* No idea... Say yes? */
  203. *why = "gave no clues";
  204. return true;
  205. }
  206. /* Examples will often build on prior ones. Try combining them. */
  207. static char **combine(const void *ctx, char **lines, char **prev)
  208. {
  209. unsigned int i, lines_total, prev_total, count;
  210. char **ret;
  211. const char *reasoning;
  212. char *why = NULL;
  213. if (!prev)
  214. return NULL;
  215. /* If it looks internal, put prev at start. */
  216. if (looks_internal(lines, &why)) {
  217. count = 0;
  218. reasoning = "seemed to belong inside a function";
  219. } else {
  220. /* Try inserting in first elided position */
  221. for (count = 0; lines[count]; count++) {
  222. if (strcmp(lines[count], "...") == 0)
  223. break;
  224. }
  225. if (!lines[count]) {
  226. /* Try at start anyway? */
  227. count = 0;
  228. reasoning = "didn't seem to belong inside"
  229. " a function, so we prepended the previous"
  230. " example";
  231. } else {
  232. reasoning = "didn't seem to belong inside"
  233. " a function, so we put the previous example"
  234. " at the first ...";
  235. count++;
  236. }
  237. }
  238. for (i = 0; lines[i]; i++);
  239. lines_total = i;
  240. for (i = 0; prev[i]; i++);
  241. prev_total = i;
  242. ret = talloc_array(ctx, char *, 1 +lines_total + prev_total + 1);
  243. ret[0] = talloc_asprintf(ret, "/* The example %s, thus %s */\n",
  244. why, reasoning);
  245. memcpy(ret+1, lines, count * sizeof(ret[0]));
  246. memcpy(ret+1 + count, prev, prev_total * sizeof(ret[0]));
  247. memcpy(ret+1 + count + prev_total, lines + count,
  248. (lines_total - count + 1) * sizeof(ret[0]));
  249. return ret;
  250. }
  251. static char *mangle(struct manifest *m, char **lines)
  252. {
  253. char *ret, *use_funcs = NULL, *why;
  254. bool in_function = false, fake_function = false, has_main = false;
  255. unsigned int i;
  256. ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
  257. "#include <assert.h>\n"
  258. "#include <err.h>\n"
  259. "#include <errno.h>\n"
  260. "#include <fcntl.h>\n"
  261. "#include <limits.h>\n"
  262. "#include <stdbool.h>\n"
  263. "#include <stdint.h>\n"
  264. "#include <stdio.h>\n"
  265. "#include <stdlib.h>\n"
  266. "#include <string.h>\n"
  267. "#include <sys/stat.h>\n"
  268. "#include <sys/types.h>\n"
  269. "#include <unistd.h>\n");
  270. ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
  271. "#include <ccan/%s/%s.h>\n",
  272. m->basename, m->basename);
  273. ret = talloc_asprintf_append(ret, "/* Useful dummy functions. */\n"
  274. "extern int somefunc(void);\n"
  275. "int somefunc(void) { return 0; }\n"
  276. "extern char somestring[];\n"
  277. "char somestring[] = \"hello world\";\n");
  278. if (looks_internal(lines, &why)) {
  279. /* Wrap it all in main(). */
  280. ret = start_main(ret, why);
  281. fake_function = true;
  282. in_function = true;
  283. has_main = true;
  284. } else
  285. ret = talloc_asprintf_append(ret,
  286. "/* The example %s, so didn't wrap in main() */\n",
  287. why);
  288. /* Primitive, very primitive. */
  289. for (i = 0; lines[i]; i++) {
  290. /* } at start of line ends a function. */
  291. if (in_function) {
  292. if (lines[i][0] == '}')
  293. in_function = false;
  294. } else {
  295. /* Character at start of line, with ( and no ;
  296. * == function start. Ignore comments. */
  297. if (!isblank(lines[i][0])
  298. && strchr(lines[i], '(')
  299. && !strchr(lines[i], ';')
  300. && !strstr(lines[i], "//")) {
  301. in_function = true;
  302. if (strncmp(lines[i], "int main", 8) == 0)
  303. has_main = true;
  304. if (strncmp(lines[i], "static", 6) == 0) {
  305. use_funcs = add_func(use_funcs,
  306. lines[i]);
  307. }
  308. }
  309. }
  310. /* ... means elided code. */
  311. if (strcmp(lines[i], "...") == 0) {
  312. if (!in_function && !has_main
  313. && looks_internal(lines + i + 1, &why)) {
  314. /* This implies we start a function here. */
  315. ret = start_main(ret, why);
  316. has_main = true;
  317. fake_function = true;
  318. in_function = true;
  319. }
  320. ret = talloc_asprintf_append(ret,
  321. "/* ... removed */\n");
  322. continue;
  323. }
  324. ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
  325. }
  326. if (!has_main) {
  327. ret = talloc_asprintf_append(ret,
  328. "/* Need a main to link successfully. */\n"
  329. "int main(void)\n{\n");
  330. fake_function = true;
  331. }
  332. if (use_funcs) {
  333. ret = talloc_asprintf_append(ret,
  334. "/* Get rid of unused warnings"
  335. " by printing addresses of"
  336. " static funcs. */");
  337. if (!fake_function) {
  338. ret = talloc_asprintf_append(ret,
  339. "int use_funcs(void);\n"
  340. "int use_funcs(void) {\n");
  341. fake_function = true;
  342. }
  343. ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
  344. }
  345. if (fake_function)
  346. ret = talloc_asprintf_append(ret, "return 0;\n"
  347. "}\n");
  348. return ret;
  349. }
  350. static struct ccan_file *mangle_example(struct manifest *m,
  351. struct ccan_file *example,
  352. char **lines,
  353. bool keep)
  354. {
  355. char *name, *contents;
  356. int fd;
  357. struct ccan_file *f;
  358. name = maybe_temp_file(example, ".c", keep,
  359. talloc_asprintf(m, "%s/mangled-%s",
  360. m->dir, example->name));
  361. f = new_ccan_file(example,
  362. talloc_dirname(example, name),
  363. talloc_basename(example, name));
  364. talloc_steal(f, name);
  365. fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
  366. if (fd < 0)
  367. return NULL;
  368. contents = mangle(m, lines);
  369. if (write(fd, contents, strlen(contents)) != strlen(contents)) {
  370. close(fd);
  371. return NULL;
  372. }
  373. close(fd);
  374. f->contents = talloc_steal(f, contents);
  375. list_add(&m->mangled_examples, &f->list);
  376. return f;
  377. }
  378. static void *build_examples(struct manifest *m, bool keep,
  379. unsigned int *timeleft)
  380. {
  381. struct ccan_file *i;
  382. struct score *score = talloc(m, struct score);
  383. char **prev = NULL;
  384. score->score = 0;
  385. score->errors = talloc_strdup(score, "");
  386. examples_compile.total_score = 0;
  387. list_for_each(&m->examples, i, list) {
  388. char *ret, *ret1, *ret2 = NULL;
  389. struct ccan_file *mangle1, *mangle2 = NULL;
  390. examples_compile.total_score++;
  391. /* Simplify our dumb parsing. */
  392. strip_leading_whitespace(get_ccan_file_lines(i));
  393. ret = compile(i, m, i, keep);
  394. if (!ret) {
  395. prev = get_ccan_file_lines(i);
  396. score->score++;
  397. continue;
  398. }
  399. /* Try standalone. */
  400. mangle1 = mangle_example(m, i, get_ccan_file_lines(i), keep);
  401. ret1 = compile(i, m, mangle1, keep);
  402. if (!ret1) {
  403. prev = get_ccan_file_lines(i);
  404. score->score++;
  405. continue;
  406. }
  407. /* Try combining with previous (successful) example... */
  408. if (prev) {
  409. char **new = combine(i, get_ccan_file_lines(i), prev);
  410. mangle2 = mangle_example(m, i, new, keep);
  411. ret2 = compile(i, m, mangle2, keep);
  412. if (!ret2) {
  413. prev = new;
  414. score->score++;
  415. continue;
  416. }
  417. }
  418. score->errors = talloc_asprintf_append(score->errors,
  419. "%s: tried standalone example:\n"
  420. "%s\n"
  421. "Errors: %s\n\n",
  422. i->name,
  423. get_ccan_file_contents(i),
  424. ret);
  425. score->errors = talloc_asprintf_append(score->errors,
  426. "%s: tried adding headers, wrappers:\n"
  427. "%s\n"
  428. "Errors: %s\n\n",
  429. i->name,
  430. get_ccan_file_contents(mangle1),
  431. ret1);
  432. if (mangle2) {
  433. score->errors = talloc_asprintf_append(score->errors,
  434. "%s: tried combining with"
  435. " previous example:\n"
  436. "%s\n"
  437. "Errors: %s\n\n",
  438. i->name,
  439. get_ccan_file_contents(mangle2),
  440. ret2);
  441. }
  442. /* This didn't work, so not a candidate for combining. */
  443. prev = NULL;
  444. }
  445. if (strcmp(score->errors, "") == 0) {
  446. talloc_free(score);
  447. return NULL;
  448. }
  449. return score;
  450. }
  451. static unsigned int score_examples(struct manifest *m, void *check_result)
  452. {
  453. struct score *score = check_result;
  454. return score->score;
  455. }
  456. static const char *describe(struct manifest *m, void *check_result)
  457. {
  458. struct score *score = check_result;
  459. if (verbose >= 2 && score->errors)
  460. return talloc_asprintf(m, "Compile errors building examples:\n"
  461. "%s", score->errors);
  462. return NULL;
  463. }
  464. struct ccanlint examples_compile = {
  465. .key = "examples-compile",
  466. .name = "Module examples compile",
  467. .score = score_examples,
  468. .total_score = 3, /* This gets changed to # examples, if they exist */
  469. .check = build_examples,
  470. .describe = describe,
  471. .can_run = can_run,
  472. };
  473. REGISTER_TEST(examples_compile, &has_examples, NULL);