ccanlint.h 4.8 KB

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