hash_if.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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, struct score *score)
  18. {
  19. struct list_head *list;
  20. const char *explanation =
  21. "\n\t(#if works like #ifdef, but with gcc's -Wundef, we can detect\n"
  22. "\tmistyped or unknown configuration options)";
  23. /* We don't fail ccanlint for this. */
  24. score->pass = true;
  25. foreach_ptr(list, &m->c_files, &m->h_files,
  26. &m->run_tests, &m->api_tests,
  27. &m->compile_ok_tests, &m->compile_fail_tests,
  28. &m->other_test_c_files) {
  29. struct ccan_file *f;
  30. list_for_each(list, f, list) {
  31. unsigned int i;
  32. char **lines = get_ccan_file_lines(f);
  33. for (i = 0; lines[i]; i++) {
  34. const char *line = lines[i];
  35. char *sym;
  36. if (!get_token(&line, "#"))
  37. continue;
  38. if (!(get_token(&line, "if")
  39. && get_token(&line, "defined")
  40. && get_token(&line, "("))
  41. && !get_token(&line, "ifdef"))
  42. continue;
  43. sym = get_symbol_token(lines, &line);
  44. if (!sym || !strstarts(sym, "HAVE_"))
  45. continue;
  46. score_file_error(score, f, i+1,
  47. "%s should be tested with #if"
  48. "%s",
  49. sym, explanation);
  50. explanation = "";
  51. }
  52. }
  53. }
  54. if (!score->error) {
  55. score->score = score->total;
  56. }
  57. }
  58. struct ccanlint hash_if = {
  59. .key = "hash_if",
  60. .name = "Features are checked with #if not #ifdef",
  61. .check = check_hash_if,
  62. .needs = "info_exists"
  63. };
  64. REGISTER_TEST(hash_if);