ccanlint.h 6.9 KB

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