examples_compile.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <ctype.h>
  11. static const char *can_run(struct manifest *m)
  12. {
  13. if (safe_mode)
  14. return "Safe mode enabled";
  15. return NULL;
  16. }
  17. static char *obj_list(const struct manifest *m)
  18. {
  19. char *list;
  20. struct ccan_file *i;
  21. /* Object files for this module. */
  22. list_for_each(&m->c_files, i, list)
  23. list = talloc_asprintf_append(list, " %s", i->compiled);
  24. /* Other ccan modules we depend on. */
  25. list_for_each(&m->dep_dirs, i, list) {
  26. if (i->compiled)
  27. list = talloc_asprintf_append(list, " %s", i->compiled);
  28. }
  29. return list;
  30. }
  31. static char *lib_list(const struct manifest *m)
  32. {
  33. unsigned int i, num;
  34. char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
  35. char *ret = talloc_strdup(m, "");
  36. for (i = 0; i < num; i++)
  37. ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
  38. return ret;
  39. }
  40. static char *compile(const void *ctx,
  41. struct manifest *m,
  42. struct ccan_file *file,
  43. bool keep)
  44. {
  45. char *errmsg;
  46. file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
  47. errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
  48. obj_list(m), "", lib_list(m), file->compiled);
  49. if (errmsg) {
  50. talloc_free(file->compiled);
  51. return errmsg;
  52. }
  53. return NULL;
  54. }
  55. struct score {
  56. unsigned int score;
  57. char *errors;
  58. };
  59. static char *start_main(char *ret)
  60. {
  61. return talloc_asprintf_append(ret,
  62. "/* Fake function wrapper inserted */\n"
  63. "int main(int argc, char *argv[])\n"
  64. "{\n");
  65. }
  66. /* We only handle simple function definitions here. */
  67. static char *add_func(char *others, const char *line)
  68. {
  69. const char *p, *end = strchr(line, '(') - 1;
  70. while (isblank(*end)) {
  71. end--;
  72. if (end == line)
  73. return others;
  74. }
  75. for (p = end; isalnum(*p) || *p == '_'; p--) {
  76. if (p == line)
  77. return others;
  78. }
  79. return talloc_asprintf_append(others, "printf(\"%%p\", %.*s);\n",
  80. end - p + 1, p);
  81. }
  82. static bool looks_internal(const char *p)
  83. {
  84. return (strncmp(p, "#", 1) != 0
  85. && strncmp(p, "static", 6) != 0
  86. && strncmp(p, "struct", 6) != 0
  87. && strncmp(p, "union", 5) != 0);
  88. }
  89. static void strip_leading_whitespace(char **lines, unsigned prefix_len)
  90. {
  91. unsigned int i;
  92. for (i = 0; lines[i]; i++)
  93. lines[i] += prefix_len;
  94. }
  95. static char *mangle(struct manifest *m, struct ccan_file *example)
  96. {
  97. char **lines = get_ccan_file_lines(example);
  98. char *ret, *use_funcs = NULL;
  99. bool in_function = false, fake_function = false, has_main = false;
  100. unsigned int i;
  101. ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
  102. "#include <assert.h>\n"
  103. "#include <err.h>\n"
  104. "#include <fcntl.h>\n"
  105. "#include <stdbool.h>\n"
  106. "#include <stdint.h>\n"
  107. "#include <stdio.h>\n"
  108. "#include <stdlib.h>\n"
  109. "#include <string.h>\n"
  110. "#include <sys/stat.h>\n"
  111. "#include <sys/types.h>\n"
  112. "#include <unistd.h>\n");
  113. ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
  114. "#include <ccan/%s/%s.h>\n",
  115. m->basename, m->basename);
  116. ret = talloc_asprintf_append(ret, "/* Useful dummmy functions. */\n"
  117. "int somefunc(void);\n"
  118. "int somefunc(void) { return 0; }\n");
  119. /* Starts indented? */
  120. if (lines[0] && isblank(lines[0][0])) {
  121. unsigned prefix = strspn(lines[0], " \t");
  122. if (looks_internal(lines[0] + prefix)) {
  123. /* Wrap it all in main(). */
  124. ret = start_main(ret);
  125. fake_function = true;
  126. in_function = true;
  127. has_main = true;
  128. } else
  129. strip_leading_whitespace(lines, prefix);
  130. }
  131. /* Primitive, very primitive. */
  132. for (i = 0; lines[i]; i++) {
  133. /* } at start of line ends a function. */
  134. if (in_function) {
  135. if (lines[i][0] == '}')
  136. in_function = false;
  137. } else {
  138. /* Character at start of line, with ( and no ;
  139. * == function start. */
  140. if (!isblank(lines[i][0])
  141. && strchr(lines[i], '(')
  142. && !strchr(lines[i], ';')) {
  143. in_function = true;
  144. if (strncmp(lines[i], "int main", 8) == 0)
  145. has_main = true;
  146. if (strncmp(lines[i], "static", 6) == 0) {
  147. use_funcs = add_func(use_funcs,
  148. lines[i]);
  149. }
  150. }
  151. }
  152. /* ... means elided code. If followed by spaced line, means
  153. * next part is supposed to be inside a function. */
  154. if (strcmp(lines[i], "...") == 0) {
  155. if (!in_function
  156. && lines[i+1]
  157. && isblank(lines[i+1][0])) {
  158. /* This implies we start a function here. */
  159. ret = start_main(ret);
  160. fake_function = true;
  161. in_function = true;
  162. }
  163. ret = talloc_asprintf_append(ret,
  164. "/* ... removed */\n");
  165. continue;
  166. }
  167. ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
  168. }
  169. /* Need a main to link successfully. */
  170. if (!has_main) {
  171. ret = talloc_asprintf_append(ret, "int main(void)\n{\n");
  172. fake_function = true;
  173. }
  174. /* Get rid of unused warnings by printing addresses of static funcs. */
  175. if (use_funcs) {
  176. if (!fake_function) {
  177. ret = talloc_asprintf_append(ret,
  178. "int use_funcs(void);\n"
  179. "int use_funcs(void) {\n");
  180. fake_function = true;
  181. }
  182. ret = talloc_asprintf_append(ret, " %s\n", use_funcs);
  183. }
  184. if (fake_function)
  185. ret = talloc_asprintf_append(ret, "return 0;\n"
  186. "}\n");
  187. return ret;
  188. }
  189. static struct ccan_file *mangle_example(struct manifest *m,
  190. struct ccan_file *example, bool keep)
  191. {
  192. char *name, *contents;
  193. int fd;
  194. struct ccan_file *f;
  195. name = maybe_temp_file(example, ".c", keep,
  196. talloc_asprintf(m, "%s/mangled-%s",
  197. m->dir, example->name));
  198. f = new_ccan_file(example,
  199. talloc_dirname(example, name),
  200. talloc_basename(example, name));
  201. talloc_steal(f, name);
  202. fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
  203. if (fd < 0)
  204. return NULL;
  205. contents = mangle(m, example);
  206. if (write(fd, contents, strlen(contents)) != strlen(contents)) {
  207. close(fd);
  208. return NULL;
  209. }
  210. close(fd);
  211. return f;
  212. }
  213. static void *build_examples(struct manifest *m, bool keep,
  214. unsigned int *timeleft)
  215. {
  216. struct ccan_file *i;
  217. struct score *score = talloc(m, struct score);
  218. score->score = 0;
  219. score->errors = NULL;
  220. list_for_each(&m->examples, i, list) {
  221. char *ret;
  222. examples_compile.total_score++;
  223. ret = compile(score, m, i, keep);
  224. if (!ret)
  225. score->score++;
  226. else {
  227. struct ccan_file *mangle = mangle_example(m, i, keep);
  228. talloc_free(ret);
  229. ret = compile(score, m, mangle, keep);
  230. if (!ret)
  231. score->score++;
  232. else {
  233. if (!score->errors)
  234. score->errors = ret;
  235. else {
  236. score->errors
  237. = talloc_append_string(score->errors,
  238. ret);
  239. talloc_free(ret);
  240. }
  241. }
  242. }
  243. }
  244. return score;
  245. }
  246. static unsigned int score_examples(struct manifest *m, void *check_result)
  247. {
  248. struct score *score = check_result;
  249. return score->score;
  250. }
  251. static const char *describe(struct manifest *m, void *check_result)
  252. {
  253. struct score *score = check_result;
  254. if (verbose >= 2 && score->errors)
  255. return talloc_asprintf(m, "Compile errors building examples:\n"
  256. "%s", score->errors);
  257. return NULL;
  258. }
  259. struct ccanlint examples_compile = {
  260. .key = "examples-compile",
  261. .name = "Module examples compile",
  262. .score = score_examples,
  263. .check = build_examples,
  264. .describe = describe,
  265. .can_run = can_run,
  266. };
  267. REGISTER_TEST(examples_compile, &has_examples, NULL);