ccanlint.h 5.7 KB

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