ccanlint.h 7.7 KB

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