ccanlint.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #ifndef CCAN_LINT_H
  2. #define CCAN_LINT_H
  3. #include "config.h"
  4. #include <ccan/list/list.h>
  5. #include <ccan/tal/tal.h>
  6. #include <ccan/dgraph/dgraph.h>
  7. #include <ccan/autodata/autodata.h>
  8. #include <stdbool.h>
  9. #include "../doc_extract.h"
  10. #include "../manifest.h"
  11. #include "../tools.h"
  12. #include "licenses.h"
  13. AUTODATA_TYPE(ccanlint_tests, struct ccanlint);
  14. #define REGISTER_TEST(test) AUTODATA(ccanlint_tests, &test)
  15. /* 0 == Describe failed tests.
  16. 1 == Describe results for partial failures.
  17. 2 == One line per test, plus details of failures.
  18. Mainly for debugging ccanlint:
  19. 3 == Describe every object built.
  20. 4 == Describe every action. */
  21. extern int verbose;
  22. /* Error in a particular file: stored off score->per_file_errors. */
  23. struct file_error {
  24. struct list_node list;
  25. struct ccan_file *file;
  26. unsigned int line;
  27. };
  28. /* The score for an individual test. */
  29. struct score {
  30. /* Starts as false: if not set to true, ccanlint exits non-zero.
  31. * Thus it is usually set for compilation or other serious failures. */
  32. bool pass;
  33. /* Starts at 0 and 1 respectively. */
  34. unsigned int score, total;
  35. /* The error message to print. */
  36. char *error;
  37. /* Per file errors, set by score_file_error() */
  38. struct list_head per_file_errors;
  39. };
  40. struct ccanlint {
  41. /* More concise unique name of test. */
  42. const char *key;
  43. /* Unique name of test */
  44. const char *name;
  45. /* Can we run this test? Return string explaining why, if not. */
  46. const char *(*can_run)(struct manifest *m);
  47. /* Should we stop immediately if test fails? */
  48. bool compulsory;
  49. /* If timeleft is set to 0, means it timed out.
  50. * score is the result, and a tal context freed after all our
  51. * depends are done. */
  52. void (*check)(struct manifest *m,
  53. unsigned int *timeleft, struct score *score);
  54. /* Can we do something about it? (NULL if not) */
  55. void (*handle)(struct manifest *m, struct score *score);
  56. /* Options from _info. */
  57. char **options;
  58. /* If not set, we'll give an error if they try to set options. */
  59. bool takes_options;
  60. /* Space-separated list of dependency keys. */
  61. const char *needs;
  62. /* Internal use fields: */
  63. /* We are a node in a dependency graph. */
  64. struct dgraph_node node;
  65. /* Did we skip a dependency? If so, must skip this, too. */
  66. const char *skip;
  67. /* Have we already run this? */
  68. bool done;
  69. };
  70. /* Ask the user a yes/no question: the answer is NO if there's an error. */
  71. bool ask(const char *question);
  72. enum line_info_type {
  73. PREPROC_LINE, /* Line starts with # */
  74. CODE_LINE, /* Code (ie. not pure comment). */
  75. DOC_LINE, /* Line with kernel-doc-style comment. */
  76. COMMENT_LINE, /* (pure) comment line */
  77. };
  78. /* So far, only do simple #ifdef/#ifndef/#if defined/#if !defined tests,
  79. * and #if <SYMBOL>/#if !<SYMBOL> */
  80. struct pp_conditions {
  81. /* We're inside another ifdef? */
  82. struct pp_conditions *parent;
  83. enum {
  84. PP_COND_IF,
  85. PP_COND_IFDEF,
  86. PP_COND_UNKNOWN,
  87. } type;
  88. bool inverse;
  89. const char *symbol;
  90. };
  91. /* Preprocessor information about each line. */
  92. struct line_info {
  93. enum line_info_type type;
  94. /* Is this actually a continuation of line above? (which ends in \) */
  95. bool continued;
  96. /* Conditions for this line to be compiled. */
  97. struct pp_conditions *cond;
  98. };
  99. /* Use this rather than accessing f->lines directly: loads on demand. */
  100. struct line_info *get_ccan_line_info(struct ccan_file *f);
  101. /* Use this rather than accessing f->simplified directly: loads on demand. */
  102. const char *get_ccan_simplified(struct ccan_file *f);
  103. enum line_compiled {
  104. NOT_COMPILED,
  105. COMPILED,
  106. MAYBE_COMPILED,
  107. };
  108. /* Simple evaluator. If symbols are set this way, is this condition true?
  109. * NULL values mean undefined, NULL symbol terminates. */
  110. enum line_compiled get_ccan_line_pp(struct pp_conditions *cond,
  111. const char *symbol,
  112. const unsigned int *value, ...);
  113. /* Get token if it's equal to token. */
  114. bool get_token(const char **line, const char *token);
  115. /* Tal copy of symbol token, or NULL. Increment line. */
  116. char *get_symbol_token(void *ctx, const char **line);
  117. /* Similarly for ->doc_sections */
  118. struct list_head *get_ccan_file_docs(struct ccan_file *f);
  119. /* Get NULL-terminated array options for this file for this test */
  120. char **per_file_options(const struct ccanlint *test, struct ccan_file *f);
  121. /* Append message about this file (and line, if non-zero) to the score->error */
  122. void score_file_error(struct score *, struct ccan_file *f, unsigned line,
  123. const char *errorfmt, ...);
  124. /* Append message to the score->error */
  125. void score_error(struct score *score, const char * source,
  126. const char *errorfmt, ...);
  127. /* Start a command in the background. */
  128. void run_command_async(const void *ctx, unsigned int time_ms,
  129. const char *fmt, ...);
  130. /* Async version of compile_and_link. */
  131. void compile_and_link_async(const void *ctx, unsigned int time_ms,
  132. const char *cfile, const char *ccandir,
  133. const char *objs, const char *compiler,
  134. const char *cflags,
  135. const char *libs, const char *outfile);
  136. /* Get results of a command, returning ctx (and free it). */
  137. void *collect_command(bool *ok, char **output);
  138. /* Find manifest for this dir and return compiled _info filename. */
  139. char *get_or_compile_info(const void *ctx, const char *dir);
  140. /* Normal tests. */
  141. extern struct ccanlint trailing_whitespace;
  142. /* Dependencies */
  143. struct dependent {
  144. struct list_node node;
  145. struct ccanlint *dependent;
  146. };
  147. /* Is this test excluded (cmdline or _info). */
  148. bool is_excluded(const char *name);
  149. /* Called to add options from _info, once it's located. */
  150. void add_info_options(struct manifest *m);
  151. /* Are we happy to compile stuff, or just non-intrusive tests? */
  152. extern bool safe_mode;
  153. /* Did the user want to keep all the results? */
  154. extern bool keep_results;
  155. /* Did we find non-ccan dependencies? */
  156. extern bool non_ccan_deps;
  157. /* Did we fail to build? */
  158. extern bool build_failed;
  159. /* Contents of config.h (or NULL if not found) */
  160. extern const char *config_header;
  161. /* Where is the ccan dir? */
  162. extern const char *ccan_dir;
  163. #endif /* CCAN_LINT_H */