check-style.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/usr/bin/env python3
  2. import os
  3. import pathlib
  4. import re
  5. import subprocess
  6. import sys
  7. # Ensure copyright headers match this format and are followed by a blank line:
  8. # /*
  9. # * Copyright (c) YYYY(-YYYY), Whatever
  10. # * ... more of these ...
  11. # *
  12. # * SPDX-License-Identifier: BSD-2-Clause
  13. # */
  14. GOOD_LICENSE_HEADER_PATTERN = re.compile(
  15. '^/\\*\n' +
  16. '( \\* Copyright \\(c\\) [0-9]{4}(-[0-9]{4})?, .*\n)+' +
  17. ' \\*\n' +
  18. ' \\* SPDX-License-Identifier: BSD-2-Clause\n' +
  19. ' \\*/\n' +
  20. '\n')
  21. LICENSE_HEADER_CHECK_EXCLUDES = {
  22. 'AK/Checked.h',
  23. 'AK/Function.h',
  24. 'Userland/Libraries/LibJS/SafeFunction.h',
  25. }
  26. # We check that "#pragma once" is present
  27. PRAGMA_ONCE_STRING = '#pragma once'
  28. PRAGMA_ONCE_CHECK_EXCLUDES = {
  29. 'Ladybird/AppKit/System/Detail/Header.h',
  30. 'Ladybird/AppKit/System/Detail/Footer.h',
  31. }
  32. # We make sure that there's a blank line before and after pragma once
  33. GOOD_PRAGMA_ONCE_PATTERN = re.compile('(^|\\S\n\n)#pragma once(\n\n\\S.|$)')
  34. # LibC is supposed to be a system library; don't mention the directory.
  35. BAD_INCLUDE_LIBC = re.compile("# *include <LibC/")
  36. # Serenity C++ code must not use LibC's or libc++'s complex number implementation.
  37. BAD_INCLUDE_COMPLEX = re.compile("# *include <c[c]?omplex")
  38. # Make sure that all includes are either system includes or immediately resolvable local includes
  39. ANY_INCLUDE_PATTERN = re.compile('^ *# *include\\b.*[>"](?!\\)).*$', re.M)
  40. SYSTEM_INCLUDE_PATTERN = re.compile("^ *# *include *<([^>]+)>(?: /[*/].*)?$")
  41. LOCAL_INCLUDE_PATTERN = re.compile('^ *# *include *"([^>]+)"(?: /[*/].*)?$')
  42. INCLUDE_CHECK_EXCLUDES = {
  43. }
  44. LOCAL_INCLUDE_ROOT_OVERRIDES = {
  45. }
  46. LOCAL_INCLUDE_SUFFIX_EXCLUDES = [
  47. # Some Qt files are required to include their .moc files, which will be located in a deep
  48. # subdirectory that we won't find from here.
  49. '.moc',
  50. ]
  51. def should_check_file(filename):
  52. if not filename.endswith('.cpp') and not filename.endswith('.h'):
  53. return False
  54. if filename.startswith('Base/'):
  55. return False
  56. return True
  57. def find_files_here_or_argv():
  58. if len(sys.argv) > 1:
  59. raw_list = sys.argv[1:]
  60. else:
  61. process = subprocess.run(["git", "ls-files"], check=True, capture_output=True)
  62. raw_list = process.stdout.decode().strip('\n').split('\n')
  63. return filter(should_check_file, raw_list)
  64. def is_in_prefix_list(filename, prefix_list):
  65. return any(
  66. filename.startswith(prefix) for prefix in prefix_list
  67. )
  68. def find_matching_prefix(filename, prefix_list):
  69. matching_prefixes = [prefix for prefix in prefix_list if filename.startswith(prefix)]
  70. assert len(matching_prefixes) <= 1
  71. return matching_prefixes[0] if matching_prefixes else None
  72. def run():
  73. errors_license = []
  74. errors_pragma_once_bad = []
  75. errors_pragma_once_missing = []
  76. errors_include_libc = []
  77. errors_include_weird_format = []
  78. errors_include_missing_local = []
  79. errors_include_bad_complex = []
  80. for filename in find_files_here_or_argv():
  81. with open(filename, "r") as f:
  82. file_content = f.read()
  83. if not is_in_prefix_list(filename, LICENSE_HEADER_CHECK_EXCLUDES):
  84. if not GOOD_LICENSE_HEADER_PATTERN.search(file_content):
  85. errors_license.append(filename)
  86. if filename.endswith('.h'):
  87. if is_in_prefix_list(filename, PRAGMA_ONCE_CHECK_EXCLUDES):
  88. # File was excluded
  89. pass
  90. elif GOOD_PRAGMA_ONCE_PATTERN.search(file_content):
  91. # Excellent, the formatting is correct.
  92. pass
  93. elif PRAGMA_ONCE_STRING in file_content:
  94. # Bad, the '#pragma once' is present but it's formatted wrong.
  95. errors_pragma_once_bad.append(filename)
  96. else:
  97. # Bad, the '#pragma once' is missing completely.
  98. errors_pragma_once_missing.append(filename)
  99. if BAD_INCLUDE_LIBC.search(file_content):
  100. errors_include_libc.append(filename)
  101. if BAD_INCLUDE_COMPLEX.search(file_content):
  102. errors_include_bad_complex.append(filename)
  103. if not is_in_prefix_list(filename, INCLUDE_CHECK_EXCLUDES):
  104. if include_root := find_matching_prefix(filename, LOCAL_INCLUDE_ROOT_OVERRIDES):
  105. local_include_root = pathlib.Path(include_root)
  106. else:
  107. local_include_root = pathlib.Path(filename).parent
  108. for include_line in ANY_INCLUDE_PATTERN.findall(file_content):
  109. if SYSTEM_INCLUDE_PATTERN.match(include_line):
  110. # Don't try to resolve system-style includes, as these might depend on generators.
  111. continue
  112. local_match = LOCAL_INCLUDE_PATTERN.match(include_line)
  113. if local_match is None:
  114. print(f"Cannot parse include-line '{include_line}' in {filename}")
  115. if filename not in errors_include_weird_format:
  116. errors_include_weird_format.append(filename)
  117. continue
  118. relative_filename = local_match.group(1)
  119. referenced_file = local_include_root.joinpath(relative_filename)
  120. if referenced_file.suffix in LOCAL_INCLUDE_SUFFIX_EXCLUDES:
  121. continue
  122. if not referenced_file.exists():
  123. print(f"In {filename}: Cannot find {referenced_file}")
  124. if filename not in errors_include_missing_local:
  125. errors_include_missing_local.append(filename)
  126. have_errors = False
  127. if errors_license:
  128. print("Files with bad licenses:", " ".join(errors_license))
  129. have_errors = True
  130. if errors_pragma_once_missing:
  131. print("Files without #pragma once:", " ".join(errors_pragma_once_missing))
  132. have_errors = True
  133. if errors_pragma_once_bad:
  134. print("Files with a bad #pragma once:", " ".join(errors_pragma_once_bad))
  135. have_errors = True
  136. if errors_include_libc:
  137. print(
  138. "Files that include a LibC header using #include <LibC/...>:",
  139. " ".join(errors_include_libc),
  140. )
  141. have_errors = True
  142. if errors_include_weird_format:
  143. print(
  144. "Files that contain badly-formatted #include statements:",
  145. " ".join(errors_include_weird_format),
  146. )
  147. have_errors = True
  148. if errors_include_missing_local:
  149. print(
  150. "Files that #include a missing local file:",
  151. " ".join(errors_include_missing_local),
  152. )
  153. have_errors = True
  154. if errors_include_bad_complex:
  155. print(
  156. "Files that include a non-AK complex header:",
  157. " ".join(errors_include_bad_complex),
  158. )
  159. have_errors = True
  160. if have_errors:
  161. sys.exit(1)
  162. if __name__ == '__main__':
  163. os.chdir(os.path.dirname(__file__) + "/..")
  164. run()