hash_if.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/str/str.h>
  5. #include <ccan/str_talloc/str_talloc.h>
  6. #include <ccan/foreach/foreach.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <limits.h>
  12. #include <errno.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <err.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. static void check_hash_if(struct manifest *m,
  19. unsigned int *timeleft, struct score *score)
  20. {
  21. struct list_head *list;
  22. const char *explanation =
  23. "\n\t(#if works like #ifdef, but with gcc's -Wundef, we can detect\n"
  24. "\tmistyped or unknown configuration options)";
  25. /* We don't fail ccanlint for this. */
  26. score->pass = true;
  27. foreach_ptr(list, &m->c_files, &m->h_files,
  28. &m->run_tests, &m->api_tests,
  29. &m->compile_ok_tests, &m->compile_fail_tests,
  30. &m->other_test_c_files) {
  31. struct ccan_file *f;
  32. list_for_each(list, f, list) {
  33. unsigned int i;
  34. char **lines = get_ccan_file_lines(f);
  35. for (i = 0; lines[i]; i++) {
  36. const char *line = lines[i];
  37. char *sym;
  38. if (!get_token(&line, "#"))
  39. continue;
  40. if (!(get_token(&line, "if")
  41. && get_token(&line, "defined")
  42. && get_token(&line, "("))
  43. && !get_token(&line, "ifdef"))
  44. continue;
  45. sym = get_symbol_token(lines, &line);
  46. if (!sym || !strstarts(sym, "HAVE_"))
  47. continue;
  48. score_file_error(score, f, i+1,
  49. "%s should be tested with #if"
  50. "%s",
  51. sym, explanation);
  52. explanation = "";
  53. }
  54. }
  55. }
  56. if (!score->error) {
  57. score->score = score->total;
  58. }
  59. }
  60. struct ccanlint hash_if = {
  61. .key = "hash_if",
  62. .name = "Features are checked with #if not #ifdef",
  63. .check = check_hash_if,
  64. .needs = "info_exists"
  65. };
  66. REGISTER_TEST(hash_if);