ccanlint.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #include "generated-compulsory-tests"
  8. #include "generated-normal-tests"
  9. #undef REGISTER_TEST
  10. #define REGISTER_TEST(name, ...)
  11. /* 0 == Describe failed tests.
  12. 1 == Describe results for partial failures.
  13. 2 == One line per test, plus details of failures.
  14. Mainly for debugging ccanlint:
  15. 3 == Describe every object built.
  16. 4 == Describe every action. */
  17. extern int verbose;
  18. struct manifest {
  19. char *dir;
  20. /* The module name, ie. final element of dir name */
  21. char *basename;
  22. struct ccan_file *info_file;
  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 dep_dirs;
  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. /* Internal use fields: */
  67. /* Who depends on us? */
  68. struct list_head dependencies;
  69. /* How many things do we (still) depend on? */
  70. unsigned int num_depends;
  71. /* Did we skip a dependency? If so, must skip this, too. */
  72. const char *skip;
  73. /* Did we fail a dependency? If so, skip and mark as fail. */
  74. bool skip_fail;
  75. /* Did the user want to keep these results? */
  76. bool keep_results;
  77. };
  78. /* Ask the user a yes/no question: the answer is NO if there's an error. */
  79. bool ask(const char *question);
  80. enum line_info_type {
  81. PREPROC_LINE, /* Line starts with # */
  82. CODE_LINE, /* Code (ie. not pure comment). */
  83. DOC_LINE, /* Line with kernel-doc-style comment. */
  84. COMMENT_LINE, /* (pure) comment line */
  85. };
  86. /* So far, only do simple #ifdef/#ifndef/#if defined/#if !defined tests,
  87. * and #if <SYMBOL>/#if !<SYMBOL> */
  88. struct pp_conditions {
  89. /* We're inside another ifdef? */
  90. struct pp_conditions *parent;
  91. enum {
  92. PP_COND_IF,
  93. PP_COND_IFDEF,
  94. PP_COND_UNKNOWN,
  95. } type;
  96. bool inverse;
  97. const char *symbol;
  98. };
  99. /* Preprocessor information about each line. */
  100. struct line_info {
  101. enum line_info_type type;
  102. /* Is this actually a continuation of line above? (which ends in \) */
  103. bool continued;
  104. /* Conditions for this line to be compiled. */
  105. struct pp_conditions *cond;
  106. };
  107. struct ccan_file {
  108. struct list_node list;
  109. /* Name (usually, within m->dir). */
  110. char *name;
  111. /* Full path name. */
  112. char *fullname;
  113. /* Pristine version of the original file.
  114. * Use get_ccan_file_contents to fill this. */
  115. const char *contents;
  116. size_t contents_size;
  117. /* Use get_ccan_file_lines / get_ccan_line_info to fill these. */
  118. unsigned int num_lines;
  119. char **lines;
  120. struct line_info *line_info;
  121. struct list_head *doc_sections;
  122. /* If this file gets compiled (eg. .C file to .o file), result here. */
  123. char *compiled;
  124. /* Compiled with coverage information. */
  125. char *cov_compiled;
  126. };
  127. /* A new ccan_file, with the given name (talloc_steal onto returned value). */
  128. struct ccan_file *new_ccan_file(const void *ctx, const char *dir, char *name);
  129. /* Use this rather than accessing f->contents directly: loads on demand. */
  130. const char *get_ccan_file_contents(struct ccan_file *f);
  131. /* Use this rather than accessing f->lines directly: loads on demand. */
  132. char **get_ccan_file_lines(struct ccan_file *f);
  133. /* Use this rather than accessing f->lines directly: loads on demand. */
  134. struct line_info *get_ccan_line_info(struct ccan_file *f);
  135. enum line_compiled {
  136. NOT_COMPILED,
  137. COMPILED,
  138. MAYBE_COMPILED,
  139. };
  140. /* Simple evaluator. If symbols are set this way, is this condition true?
  141. * NULL values mean undefined, NULL symbol terminates. */
  142. enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
  143. const char *symbol,
  144. const unsigned int *value, ...);
  145. /* Get token if it's equal to token. */
  146. bool get_token(const char **line, const char *token);
  147. /* Talloc copy of symbol token, or NULL. Increment line. */
  148. char *get_symbol_token(void *ctx, const char **line);
  149. /* Similarly for ->doc_sections */
  150. struct list_head *get_ccan_file_docs(struct ccan_file *f);
  151. /* Add an error about this file (and line, if non-zero) to the score struct */
  152. void score_file_error(struct score *, struct ccan_file *f, unsigned line,
  153. const char *error);
  154. /* Normal tests. */
  155. extern struct ccanlint trailing_whitespace;
  156. /* Dependencies */
  157. struct dependent {
  158. struct list_node node;
  159. struct ccanlint *dependent;
  160. };
  161. /* Are we happy to compile stuff, or just non-intrusive tests? */
  162. extern bool safe_mode;
  163. /* Where is the ccan dir? Available after first manifest. */
  164. extern const char *ccan_dir;
  165. #endif /* CCAN_LINT_H */