ccanlint.h 6.0 KB

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