ccanlint.h 5.5 KB

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