ccanlint.h 5.2 KB

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