hash_if.c 1.7 KB

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