ccanlint.h 5.8 KB

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