ccanlint.h 6.4 KB

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