ccanlint.h 6.7 KB

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