headers_idempotent.c 4.9 KB

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