ccanlint.h 5.5 KB

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