ccanlint.h 4.6 KB

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