ccanlint.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #ifndef CCAN_LINT_H
  2. #define CCAN_LINT_H
  3. #include <ccan/list/list.h>
  4. #include <stdbool.h>
  5. #include "../doc_extract.h"
  6. #define REGISTER_TEST(name, ...) extern struct ccanlint name
  7. #include "generated-init-tests"
  8. #undef REGISTER_TEST
  9. #define REGISTER_TEST(name, ...)
  10. struct manifest {
  11. char *basename;
  12. struct ccan_file *info_file;
  13. struct list_head c_files;
  14. struct list_head h_files;
  15. struct list_head run_tests;
  16. struct list_head api_tests;
  17. struct list_head compile_ok_tests;
  18. struct list_head compile_fail_tests;
  19. struct list_head other_test_files;
  20. struct list_head other_files;
  21. };
  22. struct manifest *get_manifest(void);
  23. struct ccanlint {
  24. struct list_node list;
  25. /* Unique name of test */
  26. const char *name;
  27. /* Total score that this test is worth. 0 means compulsory tests. */
  28. unsigned int total_score;
  29. /* If this returns non-NULL, it means the check failed. */
  30. void *(*check)(struct manifest *m);
  31. /* The non-NULL return from check is passed to one of these: */
  32. /* So, what did this get out of the total_score? (NULL means 0). */
  33. unsigned int (*score)(struct manifest *m, void *check_result);
  34. /* Verbose description of what was wrong. */
  35. const char *(*describe)(struct manifest *m, void *check_result);
  36. /* Can we do something about it? (NULL if not) */
  37. void (*handle)(struct manifest *m, void *check_result);
  38. /* Internal use fields: */
  39. /* Who depends on us? */
  40. struct list_head dependencies;
  41. /* How many things do we (still) depend on? */
  42. unsigned int num_depends;
  43. };
  44. /* Ask the user a yes/no question: the answer is NO if there's an error. */
  45. bool ask(const char *question);
  46. enum line_info_type {
  47. PREPROC_LINE, /* Line starts with # */
  48. CODE_LINE, /* Code (ie. not pure comment). */
  49. DOC_LINE, /* Line with kernel-doc-style comment. */
  50. COMMENT_LINE, /* (pure) comment line */
  51. };
  52. /* So far, only do simple #ifdef/#ifndef/#if defined/#if !defined tests,
  53. * and #if <SYMBOL>/#if !<SYMBOL> */
  54. struct pp_conditions {
  55. /* We're inside another ifdef? */
  56. struct pp_conditions *parent;
  57. enum {
  58. PP_COND_IF,
  59. PP_COND_IFDEF,
  60. PP_COND_UNKNOWN,
  61. } type;
  62. bool inverse;
  63. const char *symbol;
  64. };
  65. /* Preprocessor information about each line. */
  66. struct line_info {
  67. enum line_info_type type;
  68. /* Is this actually a continuation of line above? (which ends in \) */
  69. bool continued;
  70. /* Conditions for this line to be compiled. */
  71. struct pp_conditions *cond;
  72. };
  73. struct ccan_file {
  74. struct list_node list;
  75. char *name;
  76. /* Pristine version of the original file.
  77. * Use get_ccan_file_lines to fill this. */
  78. const char *contents;
  79. size_t contents_size;
  80. /* Use get_ccan_file_lines / get_ccan_line_info to fill these. */
  81. unsigned int num_lines;
  82. char **lines;
  83. struct line_info *line_info;
  84. struct list_head *doc_sections;
  85. };
  86. /* Use this rather than accessing f->lines directly: loads on demand. */
  87. char **get_ccan_file_lines(struct ccan_file *f);
  88. /* Use this rather than accessing f->lines directly: loads on demand. */
  89. struct line_info *get_ccan_line_info(struct ccan_file *f);
  90. enum line_compiled {
  91. NOT_COMPILED,
  92. COMPILED,
  93. MAYBE_COMPILED,
  94. };
  95. /* Simple evaluator. If symbols are set this way, is this condition true?
  96. * NULL values mean undefined, NULL symbol terminates. */
  97. enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
  98. const char *symbol,
  99. const unsigned int *value, ...);
  100. /* Get token if it's equal to token. */
  101. bool get_token(const char **line, const char *token);
  102. /* Talloc copy of symbol token, or NULL. Increment line. */
  103. char *get_symbol_token(void *ctx, const char **line);
  104. /* Similarly for ->doc_sections */
  105. struct list_head *get_ccan_file_docs(struct ccan_file *f);
  106. /* Call the reporting on every line in the file. sofar contains
  107. * previous results. */
  108. char *report_on_lines(struct list_head *files,
  109. char *(*report)(const char *),
  110. char *sofar);
  111. /* Normal tests. */
  112. extern struct ccanlint trailing_whitespace;
  113. /* Dependencies */
  114. struct dependent {
  115. struct list_node node;
  116. struct ccanlint *dependent;
  117. };
  118. #endif /* CCAN_LINT_H */