ccanlint.h 7.0 KB

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