headers_idempotent.c 4.9 KB

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