header_guards.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. changed = []
  9. invalid = []
  10. for file in sys.argv[1:]:
  11. header_start = -1
  12. HEADER_CHECK_OFFSET = -1
  13. with open(file.strip(), "rt", encoding="utf-8", newline="\n") as f:
  14. lines = f.readlines()
  15. for idx, line in enumerate(lines):
  16. sline = line.strip()
  17. if header_start < 0:
  18. if sline == "": # Skip empty lines at the top.
  19. continue
  20. if sline.startswith("/**********"): # Godot header starts this way.
  21. header_start = idx
  22. else:
  23. HEADER_CHECK_OFFSET = 0 # There is no Godot header.
  24. break
  25. else:
  26. if not sline.startswith("*") and not sline.startswith("/*"): # Not in the Godot header anymore.
  27. HEADER_CHECK_OFFSET = idx + 1 # The include should be two lines below the Godot header.
  28. break
  29. if HEADER_CHECK_OFFSET < 0:
  30. invalid.append(file)
  31. continue
  32. HEADER_BEGIN_OFFSET = HEADER_CHECK_OFFSET + 1
  33. HEADER_END_OFFSET = len(lines) - 1
  34. if HEADER_BEGIN_OFFSET >= HEADER_END_OFFSET:
  35. invalid.append(file)
  36. continue
  37. split = file.split("/") # Already in posix-format.
  38. prefix = ""
  39. if split[0] == "modules" and split[-1] == "register_types.h":
  40. prefix = f"{split[1]}_" # Name of module.
  41. elif split[0] == "platform" and (file.endswith("api/api.h") or "/export/" in file):
  42. prefix = f"{split[1]}_" # Name of platform.
  43. elif file.startswith("modules/mono/utils") and "mono" not in split[-1]:
  44. prefix = "MONO_"
  45. elif file == "servers/rendering/storage/utilities.h":
  46. prefix = "RENDERER_"
  47. suffix = ""
  48. if "dummy" in file and "dummy" not in split[-1]:
  49. suffix = "_DUMMY"
  50. elif "gles3" in file and "gles3" not in split[-1]:
  51. suffix = "_GLES3"
  52. elif "renderer_rd" in file and "rd" not in split[-1]:
  53. suffix = "_RD"
  54. elif split[-1] == "ustring.h":
  55. suffix = "_GODOT"
  56. name = (f"{prefix}{Path(file).stem}{suffix}{Path(file).suffix}".upper()
  57. .replace(".", "_").replace("-", "_").replace(" ", "_")) # fmt: skip
  58. HEADER_CHECK = f"#ifndef {name}\n"
  59. HEADER_BEGIN = f"#define {name}\n"
  60. HEADER_END = f"#endif // {name}\n"
  61. if (
  62. lines[HEADER_CHECK_OFFSET] == HEADER_CHECK
  63. and lines[HEADER_BEGIN_OFFSET] == HEADER_BEGIN
  64. and lines[HEADER_END_OFFSET] == HEADER_END
  65. ):
  66. continue
  67. # Guards might exist but with the wrong names.
  68. if (
  69. lines[HEADER_CHECK_OFFSET].startswith("#ifndef")
  70. and lines[HEADER_BEGIN_OFFSET].startswith("#define")
  71. and lines[HEADER_END_OFFSET].startswith("#endif")
  72. ):
  73. lines[HEADER_CHECK_OFFSET] = HEADER_CHECK
  74. lines[HEADER_BEGIN_OFFSET] = HEADER_BEGIN
  75. lines[HEADER_END_OFFSET] = HEADER_END
  76. with open(file, "wt", encoding="utf-8", newline="\n") as f:
  77. f.writelines(lines)
  78. changed.append(file)
  79. continue
  80. header_check = -1
  81. header_begin = -1
  82. header_end = -1
  83. pragma_once = -1
  84. objc = False
  85. for idx, line in enumerate(lines):
  86. if line.startswith("// #import"): # Some dummy obj-c files only have commented out import lines.
  87. objc = True
  88. break
  89. if not line.startswith("#"):
  90. continue
  91. elif line.startswith("#ifndef") and header_check == -1:
  92. header_check = idx
  93. elif line.startswith("#define") and header_begin == -1:
  94. header_begin = idx
  95. elif line.startswith("#endif") and header_end == -1:
  96. header_end = idx
  97. elif line.startswith("#pragma once"):
  98. pragma_once = idx
  99. break
  100. elif line.startswith("#import"):
  101. objc = True
  102. break
  103. if objc:
  104. continue
  105. if pragma_once != -1:
  106. lines.pop(pragma_once)
  107. lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
  108. lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
  109. lines.append("\n")
  110. lines.append(HEADER_END)
  111. with open(file, "wt", encoding="utf-8", newline="\n") as f:
  112. f.writelines(lines)
  113. changed.append(file)
  114. continue
  115. if header_check == -1 and header_begin == -1 and header_end == -1:
  116. # Guards simply didn't exist
  117. lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
  118. lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
  119. lines.append("\n")
  120. lines.append(HEADER_END)
  121. with open(file, "wt", encoding="utf-8", newline="\n") as f:
  122. f.writelines(lines)
  123. changed.append(file)
  124. continue
  125. if header_check != -1 and header_begin != -1 and header_end != -1:
  126. # All prepends "found", see if we can salvage this.
  127. if header_check == header_begin - 1 and header_begin < header_end:
  128. lines.pop(header_check)
  129. lines.pop(header_begin - 1)
  130. lines.pop(header_end - 2)
  131. if lines[header_end - 3] == "\n":
  132. lines.pop(header_end - 3)
  133. lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK)
  134. lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN)
  135. lines.append("\n")
  136. lines.append(HEADER_END)
  137. with open(file, "wt", encoding="utf-8", newline="\n") as f:
  138. f.writelines(lines)
  139. changed.append(file)
  140. continue
  141. invalid.append(file)
  142. if changed:
  143. for file in changed:
  144. print(f"FIXED: {file}")
  145. if invalid:
  146. for file in invalid:
  147. print(f"REQUIRES MANUAL CHANGES: {file}")
  148. sys.exit(1)