headers_idempotent.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/str/str.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <limits.h>
  9. #include <errno.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <err.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15. static void fix_name(char *name)
  16. {
  17. unsigned int i;
  18. for (i = 0; name[i]; i++) {
  19. if (cisalnum(name[i]))
  20. name[i] = toupper(name[i]);
  21. else
  22. name[i] = '_';
  23. }
  24. }
  25. static void handle_idem(struct manifest *m, struct score *score)
  26. {
  27. struct file_error *e;
  28. list_for_each(&score->per_file_errors, e, list) {
  29. char *name, *q, *tmpname;
  30. FILE *out;
  31. unsigned int i;
  32. /* Main header gets CCAN_FOO_H, others CCAN_FOO_XXX_H */
  33. if (strstarts(e->file->name, m->basename)
  34. || strlen(e->file->name) == strlen(m->basename) + 2)
  35. name = tal_fmt(score, "CCAN_%s_H", m->modname);
  36. else
  37. name = tal_fmt(score, "CCAN_%s_%s",
  38. m->modname, e->file->name);
  39. fix_name(name);
  40. q = tal_fmt(score,
  41. "Should I wrap %s in #ifndef/#define %s for you?",
  42. e->file->name, name);
  43. if (!ask(q))
  44. continue;
  45. tmpname = temp_file(score, ".h", e->file->name);
  46. out = fopen(tmpname, "w");
  47. if (!out)
  48. err(1, "Opening %s", tmpname);
  49. if (fprintf(out, "#ifndef %s\n#define %s\n", name, name) < 0)
  50. err(1, "Writing %s", tmpname);
  51. for (i = 0; e->file->lines[i]; i++)
  52. if (fprintf(out, "%s\n", e->file->lines[i]) < 0)
  53. err(1, "Writing %s", tmpname);
  54. if (fprintf(out, "#endif /* %s */\n", name) < 0)
  55. err(1, "Writing %s", tmpname);
  56. if (fclose(out) != 0)
  57. err(1, "Closing %s", tmpname);
  58. if (!move_file(tmpname, e->file->fullname))
  59. err(1, "Moving %s to %s", tmpname, e->file->fullname);
  60. }
  61. }
  62. static void check_idem(struct ccan_file *f, struct score *score)
  63. {
  64. struct line_info *line_info;
  65. unsigned int i, first_preproc_line;
  66. const char *line, *sym;
  67. line_info = get_ccan_line_info(f);
  68. if (tal_count(f->lines) < 4)
  69. /* FIXME: We assume small headers probably uninteresting. */
  70. return;
  71. for (i = 0; f->lines[i]; i++) {
  72. if (line_info[i].type == DOC_LINE
  73. || line_info[i].type == COMMENT_LINE)
  74. continue;
  75. if (line_info[i].type == CODE_LINE) {
  76. score_file_error(score, f, i+1,
  77. "Expect first non-comment line to be"
  78. " #ifndef.");
  79. return;
  80. } else if (line_info[i].type == PREPROC_LINE)
  81. break;
  82. }
  83. /* No code at all? Don't complain. */
  84. if (!f->lines[i])
  85. return;
  86. first_preproc_line = i;
  87. for (i = first_preproc_line+1; f->lines[i]; i++) {
  88. if (line_info[i].type == DOC_LINE
  89. || line_info[i].type == COMMENT_LINE)
  90. continue;
  91. if (line_info[i].type == CODE_LINE) {
  92. score_file_error(score, f, i+1,
  93. "Expect second non-comment line to be"
  94. " #define.");
  95. return;
  96. } else if (line_info[i].type == PREPROC_LINE)
  97. break;
  98. }
  99. /* No code at all? Weird. */
  100. if (!f->lines[i])
  101. return;
  102. /* We expect a condition around this line. */
  103. if (!line_info[i].cond) {
  104. score_file_error(score, f, first_preproc_line+1,
  105. "Expected #ifndef");
  106. return;
  107. }
  108. line = f->lines[i];
  109. /* We expect the condition to be ! IFDEF <symbol>. */
  110. if (line_info[i].cond->type != PP_COND_IFDEF
  111. || !line_info[i].cond->inverse) {
  112. score_file_error(score, f, first_preproc_line+1,
  113. "Expected #ifndef");
  114. return;
  115. }
  116. /* And this to be #define <symbol> */
  117. if (!get_token(&line, "#"))
  118. abort();
  119. if (!get_token(&line, "define")) {
  120. score_file_error(score, f, i+1,
  121. "expected '#define %s'",
  122. line_info[i].cond->symbol);
  123. return;
  124. }
  125. sym = get_symbol_token(f, &line);
  126. if (!sym || !streq(sym, line_info[i].cond->symbol)) {
  127. score_file_error(score, f, i+1,
  128. "expected '#define %s'",
  129. line_info[i].cond->symbol);
  130. return;
  131. }
  132. /* Record this for use in depends_accurate */
  133. f->idempotent_cond = line_info[i].cond;
  134. /* Rest of code should all be covered by that conditional. */
  135. for (i++; f->lines[i]; i++) {
  136. unsigned int val = 0;
  137. if (line_info[i].type == DOC_LINE
  138. || line_info[i].type == COMMENT_LINE)
  139. continue;
  140. if (get_ccan_line_pp(line_info[i].cond, sym, &val, NULL)
  141. != NOT_COMPILED) {
  142. score_file_error(score, f, i+1, "code outside"
  143. " idempotent region");
  144. return;
  145. }
  146. }
  147. }
  148. static void check_idempotent(struct manifest *m,
  149. unsigned int *timeleft UNNEEDED,
  150. struct score *score)
  151. {
  152. struct ccan_file *f;
  153. /* We don't fail ccanlint for this. */
  154. score->pass = true;
  155. list_for_each(&m->h_files, f, list) {
  156. check_idem(f, score);
  157. }
  158. if (!score->error) {
  159. score->score = score->total;
  160. }
  161. }
  162. struct ccanlint headers_idempotent = {
  163. .key = "headers_idempotent",
  164. .name = "Module headers are #ifndef/#define wrapped",
  165. .check = check_idempotent,
  166. .handle = handle_idem,
  167. .needs = "info_exists main_header_exists"
  168. };
  169. REGISTER_TEST(headers_idempotent);