ccanlint.h 6.4 KB

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