ccanlint.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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-compulsory-tests"
  8. #include "generated-normal-tests"
  9. #undef REGISTER_TEST
  10. #define REGISTER_TEST(name, ...)
  11. /* 0 == Describe failed tests.
  12. 1 == Describe results for partial failures.
  13. 2 == One line per test, plus details of failures.
  14. Mainly for debugging ccanlint:
  15. 3 == Describe every object built.
  16. 4 == Describe every action. */
  17. extern int verbose;
  18. struct manifest {
  19. char *dir;
  20. /* The module name, ie. final element of dir name */
  21. char *basename;
  22. struct ccan_file *info_file;
  23. /* Linked off deps. */
  24. struct list_node list;
  25. /* Where our final compiled output is */
  26. char *compiled;
  27. struct list_head c_files;
  28. struct list_head h_files;
  29. struct list_head run_tests;
  30. struct list_head api_tests;
  31. struct list_head compile_ok_tests;
  32. struct list_head compile_fail_tests;
  33. struct list_head other_test_c_files;
  34. struct list_head other_test_files;
  35. struct list_head other_files;
  36. struct list_head examples;
  37. struct list_head mangled_examples;
  38. /* From tests/check_depends_exist.c */
  39. struct list_head deps;
  40. };
  41. struct manifest *get_manifest(const void *ctx, const char *dir);
  42. struct file_error {
  43. struct list_node list;
  44. struct ccan_file *file;
  45. unsigned int line; /* 0 not to print */
  46. const char *error;
  47. };
  48. struct score {
  49. bool pass;
  50. unsigned int score, total;
  51. const char *error;
  52. struct list_head per_file_errors;
  53. };
  54. struct ccanlint {
  55. struct list_node list;
  56. /* More concise unique name of test. */
  57. const char *key;
  58. /* Unique name of test */
  59. const char *name;
  60. /* Can we run this test? Return string explaining why, if not. */
  61. const char *(*can_run)(struct manifest *m);
  62. /* keep is set if you should keep the results.
  63. * If timeleft is set to 0, means it timed out.
  64. * score is the result, and a talloc context freed after all our
  65. * depends are done. */
  66. void (*check)(struct manifest *m,
  67. bool keep, unsigned int *timeleft, struct score *score);
  68. /* Can we do something about it? (NULL if not) */
  69. void (*handle)(struct manifest *m, struct score *score);
  70. /* Options from _info. */
  71. char *options;
  72. /* If not set, we'll give an error if they try to set options. */
  73. bool takes_options;
  74. /* Internal use fields: */
  75. /* Who depends on us? */
  76. struct list_head dependencies;
  77. /* How many things do we (still) depend on? */
  78. unsigned int num_depends;
  79. /* Did we skip a dependency? If so, must skip this, too. */
  80. const char *skip;
  81. /* Did we fail a dependency? If so, skip and mark as fail. */
  82. bool skip_fail;
  83. /* Did the user want to keep these results? */
  84. bool keep_results;
  85. };
  86. /* Ask the user a yes/no question: the answer is NO if there's an error. */
  87. bool ask(const char *question);
  88. enum line_info_type {
  89. PREPROC_LINE, /* Line starts with # */
  90. CODE_LINE, /* Code (ie. not pure comment). */
  91. DOC_LINE, /* Line with kernel-doc-style comment. */
  92. COMMENT_LINE, /* (pure) comment line */
  93. };
  94. /* So far, only do simple #ifdef/#ifndef/#if defined/#if !defined tests,
  95. * and #if <SYMBOL>/#if !<SYMBOL> */
  96. struct pp_conditions {
  97. /* We're inside another ifdef? */
  98. struct pp_conditions *parent;
  99. enum {
  100. PP_COND_IF,
  101. PP_COND_IFDEF,
  102. PP_COND_UNKNOWN,
  103. } type;
  104. bool inverse;
  105. const char *symbol;
  106. };
  107. /* Preprocessor information about each line. */
  108. struct line_info {
  109. enum line_info_type type;
  110. /* Is this actually a continuation of line above? (which ends in \) */
  111. bool continued;
  112. /* Conditions for this line to be compiled. */
  113. struct pp_conditions *cond;
  114. };
  115. struct ccan_file {
  116. struct list_node list;
  117. /* Name (usually, within m->dir). */
  118. char *name;
  119. /* Full path name. */
  120. char *fullname;
  121. /* Pristine version of the original file.
  122. * Use get_ccan_file_contents to fill this. */
  123. const char *contents;
  124. size_t contents_size;
  125. /* Use get_ccan_file_lines / get_ccan_line_info to fill these. */
  126. unsigned int num_lines;
  127. char **lines;
  128. struct line_info *line_info;
  129. struct list_head *doc_sections;
  130. /* If this file gets compiled (eg. .C file to .o file), result here. */
  131. char *compiled;
  132. /* Compiled with coverage information. */
  133. char *cov_compiled;
  134. /* Leak output from valgrind. */
  135. char *leak_info;
  136. };
  137. /* A new ccan_file, with the given name (talloc_steal onto returned value). */
  138. struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name);
  139. /* Use this rather than accessing f->contents directly: loads on demand. */
  140. const char *get_ccan_file_contents(struct ccan_file *f);
  141. /* Use this rather than accessing f->lines directly: loads on demand. */
  142. char **get_ccan_file_lines(struct ccan_file *f);
  143. /* Use this rather than accessing f->lines directly: loads on demand. */
  144. struct line_info *get_ccan_line_info(struct ccan_file *f);
  145. enum line_compiled {
  146. NOT_COMPILED,
  147. COMPILED,
  148. MAYBE_COMPILED,
  149. };
  150. /* Simple evaluator. If symbols are set this way, is this condition true?
  151. * NULL values mean undefined, NULL symbol terminates. */
  152. enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
  153. const char *symbol,
  154. const unsigned int *value, ...);
  155. /* Get token if it's equal to token. */
  156. bool get_token(const char **line, const char *token);
  157. /* Talloc copy of symbol token, or NULL. Increment line. */
  158. char *get_symbol_token(void *ctx, const char **line);
  159. /* Similarly for ->doc_sections */
  160. struct list_head *get_ccan_file_docs(struct ccan_file *f);
  161. /* Add an error about this file (and line, if non-zero) to the score struct */
  162. void score_file_error(struct score *, struct ccan_file *f, unsigned line,
  163. const char *error);
  164. /* Normal tests. */
  165. extern struct ccanlint trailing_whitespace;
  166. /* Dependencies */
  167. struct dependent {
  168. struct list_node node;
  169. struct ccanlint *dependent;
  170. };
  171. /* Are we happy to compile stuff, or just non-intrusive tests? */
  172. extern bool safe_mode;
  173. /* Where is the ccan dir? Available after first manifest. */
  174. extern const char *ccan_dir;
  175. #endif /* CCAN_LINT_H */