header_guards.py 5.5 KB

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