examples_compile.c 13 KB

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