ccanlint.h 5.0 KB

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