idempotent.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "ccanlint.h"
  2. #include <ccan/talloc/talloc.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. #include "../tools.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 MY_HEADER_H\n"
  20. "#define MY_HEADER_H\n"
  21. "...\n"
  22. "#endif /* MY_HEADER_H */\n";
  23. static char *get_ifndef_sym(char *line)
  24. {
  25. line += strspn(line, SPACE_CHARS);
  26. if (line[0] == '#')
  27. {
  28. line++;
  29. line += strspn(line, SPACE_CHARS);
  30. if (strstarts(line, "ifndef") && isspace(line[6]))
  31. return line+6+strspn(line+6, SPACE_CHARS);
  32. else if (strstarts(line, "if"))
  33. {
  34. line += 2;
  35. line += strspn(line, SPACE_CHARS);
  36. if (line[0] == '!')
  37. {
  38. line++;
  39. line += strspn(line, SPACE_CHARS);
  40. if (strstarts(line, "defined"))
  41. {
  42. line += 7;
  43. line += strspn(line, SPACE_CHARS);
  44. if (line[0] == '(')
  45. {
  46. line++;
  47. line += strspn(line,
  48. SPACE_CHARS);
  49. }
  50. return line;
  51. }
  52. }
  53. }
  54. }
  55. return NULL;
  56. }
  57. static int is_define(char *line, char *id, size_t id_len)
  58. {
  59. line += strspn(line, SPACE_CHARS);
  60. if (line[0] == '#')
  61. {
  62. line++;
  63. line += strspn(line, SPACE_CHARS);
  64. if (strstarts(line, "define") && isspace(line[6]))
  65. {
  66. line += 6;
  67. line += strspn(line, SPACE_CHARS);
  68. if (strspn(line, IDENT_CHARS) == id_len &&
  69. memcmp(id, line, id_len) == 0)
  70. return 1;
  71. }
  72. }
  73. return 0;
  74. }
  75. static char *report_idem(struct ccan_file *f, char *sofar)
  76. {
  77. char **lines;
  78. char *id;
  79. size_t id_len;
  80. lines = get_ccan_file_lines(f);
  81. if (f->num_lines < 3)
  82. /* FIXME: We assume small headers probably uninteresting. */
  83. return NULL;
  84. id = get_ifndef_sym(lines[0]);
  85. if (!id)
  86. return talloc_asprintf_append(sofar,
  87. "%s:1:expect first line to be #ifndef.\n", f->name);
  88. id_len = strspn(id, IDENT_CHARS);
  89. if (!is_define(lines[1], id, id_len))
  90. return talloc_asprintf_append(sofar,
  91. "%s:2:expect second line to be '#define %.*s'.\n",
  92. f->name, (int)id_len, id);
  93. return sofar;
  94. }
  95. static void *check_idempotent(struct manifest *m)
  96. {
  97. struct ccan_file *f;
  98. char *report = NULL;
  99. list_for_each(&m->h_files, f, list)
  100. report = report_idem(f, report);
  101. return report;
  102. }
  103. static const char *describe_idempotent(struct manifest *m, void *check_result)
  104. {
  105. return talloc_asprintf(check_result,
  106. "Some headers not idempotent:\n"
  107. "%s\n%s", (char *)check_result,
  108. explain);
  109. }
  110. struct ccanlint idempotent = {
  111. .name = "Headers are #ifndef/#define idempotent wrapped",
  112. .total_score = 1,
  113. .check = check_idempotent,
  114. .describe = describe_idempotent,
  115. };