header_guards.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. from pathlib import Path
  5. if len(sys.argv) < 2:
  6. print("Invalid usage of header_guards.py, it should be called with a path to one or multiple files.")
  7. sys.exit(1)
  8. HEADER_CHECK_OFFSET = 30
  9. HEADER_BEGIN_OFFSET = 31
  10. HEADER_END_OFFSET = -1
  11. changed = []
  12. invalid = []
  13. for file in sys.argv[1:]:
  14. with open(file, "rt", encoding="utf-8", newline="\n") as f:
  15. lines = f.readlines()
  16. if len(lines) <= HEADER_BEGIN_OFFSET:
  17. continue # Most likely a dummy file.
  18. if lines[HEADER_CHECK_OFFSET].startswith("#import"):
  19. continue # Early catch obj-c file.
  20. split = file.split("/") # Already in posix-format.
  21. prefix = ""
  22. if split[0] == "modules" and split[-1] == "register_types.h":
  23. prefix = f"{split[1]}_" # Name of module.
  24. elif split[0] == "platform" and (file.endswith("api/api.h") or "/export/" in file):
  25. prefix = f"{split[1]}_" # Name of platform.
  26. elif file.startswith("modules/mono/utils") and "mono" not in split[-1]:
  27. prefix = "MONO_"
  28. elif file == "servers/rendering/storage/utilities.h":
  29. prefix = "RENDERER_"
  30. suffix = ""
  31. if "dummy" in file and "dummy" not in split[-1]:
  32. suffix = "_DUMMY"
  33. elif "gles3" in file and "gles3" not in split[-1]:
  34. suffix = "_GLES3"
  35. elif "renderer_rd" in file and "rd" not in split[-1]:
  36. suffix = "_RD"
  37. elif split[-1] == "ustring.h":
  38. suffix = "_GODOT"
  39. name = (f"{prefix}{Path(file).stem}{suffix}{Path(file).suffix}".upper()
  40. .replace(".", "_").replace("-", "_").replace(" ", "_")) # fmt: skip
  41. HEADER_CHECK = f"#ifndef {name}\n"
  42. HEADER_BEGIN = f"#define {name}\n"
  43. HEADER_END = f"#endif // {name}\n"
  44. if (
  45. lines[HEADER_CHECK_OFFSET] == HEADER_CHECK
  46. and lines[HEADER_BEGIN_OFFSET] == HEADER_BEGIN
  47. and lines[HEADER_END_OFFSET] == HEADER_END
  48. ):
  49. continue
  50. # Guards might exist but with the wrong names.
  51. if (
  52. lines[HEADER_CHECK_OFFSET].startswith("#ifndef")
  53. and lines[HEADER_BEGIN_OFFSET].startswith("#define")
  54. and lines[HEADER_END_OFFSET].startswith("#endif")
  55. ):
  56. lines[HEADER_CHECK_OFFSET] = HEADER_CHECK
  57. lines[HEADER_BEGIN_OFFSET] = HEADER_BEGIN
  58. lines[HEADER_END_OFFSET] = HEADER_END
  59. with open(file, "wt", encoding="utf-8", newline="\n") as f:
  60. f.writelines(lines)
  61. changed.append(file)
  62. continue
  63. header_check = -1
  64. header_begin = -1
  65. header_end = -1
  66. pragma_once = -1
  67. objc = False
  68. for idx, line in enumerate(lines):
  69. if not line.startswith("#"):
  70. continue
  71. elif line.startswith("#ifndef") and header_check == -1:
  72. header_check = idx
  73. elif line.startswith("#define") and header_begin == -1:
  74. header_begin = idx
  75. elif line.startswith("#endif") and header_end == -1:
  76. header_end = idx
  77. elif line.startswith("#pragma once"):
  78. pragma_once = idx
  79. break
  80. elif line.startswith("#import"):
  81. objc = True
  82. break
  83. if objc:
  84. continue
  85. if pragma_once != -1:
  86. lines.pop(pragma_once)
  87. lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
  88. lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
  89. lines.append("\n")
  90. lines.append(HEADER_END)
  91. with open(file, "wt", encoding="utf-8", newline="\n") as f:
  92. f.writelines(lines)
  93. changed.append(file)
  94. continue
  95. if header_check == -1 and header_begin == -1 and header_end == -1:
  96. # Guards simply didn't exist
  97. lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
  98. lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
  99. lines.append("\n")
  100. lines.append(HEADER_END)
  101. with open(file, "wt", encoding="utf-8", newline="\n") as f:
  102. f.writelines(lines)
  103. changed.append(file)
  104. continue
  105. if header_check != -1 and header_begin != -1 and header_end != -1:
  106. # All prepends "found", see if we can salvage this.
  107. if header_check == header_begin - 1 and header_begin < header_end:
  108. lines.pop(header_check)
  109. lines.pop(header_begin - 1)
  110. lines.pop(header_end - 2)
  111. if lines[header_end - 3] == "\n":
  112. lines.pop(header_end - 3)
  113. lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
  114. lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
  115. lines.append("\n")
  116. lines.append(HEADER_END)
  117. with open(file, "wt", encoding="utf-8", newline="\n") as f:
  118. f.writelines(lines)
  119. changed.append(file)
  120. continue
  121. invalid.append(file)
  122. if changed:
  123. for file in changed:
  124. print(f"FIXED: {file}")
  125. if invalid:
  126. for file in invalid:
  127. print(f"REQUIRES MANUAL CHANGES: {file}")
  128. sys.exit(1)