ccanlint.h 7.6 KB

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