check-style.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. 'Libraries/LibCore/SocketpairWindows.cpp',
  25. }
  26. # We check that "#pragma once" is present
  27. PRAGMA_ONCE_STRING = '#pragma once'
  28. # We make sure that there's a blank line before and after pragma once
  29. GOOD_PRAGMA_ONCE_PATTERN = re.compile('(^|\\S\n\n)#pragma once(\n\n\\S.|$)')
  30. # LibC is supposed to be a system library; don't mention the directory.
  31. BAD_INCLUDE_LIBC = re.compile("# *include <LibC/")
  32. # Serenity C++ code must not use LibC's or libc++'s complex number implementation.
  33. BAD_INCLUDE_COMPLEX = re.compile("# *include <c[c]?omplex")
  34. # Make sure that all includes are either system includes or immediately resolvable local includes
  35. ANY_INCLUDE_PATTERN = re.compile('^ *# *include\\b.*[>"](?!\\)).*$', re.M)
  36. SYSTEM_INCLUDE_PATTERN = re.compile("^ *# *include *<([^>]+)>(?: /[*/].*)?$")
  37. LOCAL_INCLUDE_PATTERN = re.compile('^ *# *include *"([^>]+)"(?: /[*/].*)?$')
  38. INCLUDE_CHECK_EXCLUDES = {
  39. }
  40. LOCAL_INCLUDE_ROOT_OVERRIDES = {
  41. }
  42. LOCAL_INCLUDE_SUFFIX_EXCLUDES = [
  43. # Some Qt files are required to include their .moc files, which will be located in a deep
  44. # subdirectory that we won't find from here.
  45. '.moc',
  46. ]
  47. # We check for and disallow any comments linking to the single-page HTML spec because it takes a long time to load.
  48. SINGLE_PAGE_HTML_SPEC_LINK = re.compile('//.*https://html\\.spec\\.whatwg\\.org/#')
  49. def should_check_file(filename):
  50. if not filename.endswith('.cpp') and not filename.endswith('.h'):
  51. return False
  52. if filename.startswith('Base/'):
  53. return False
  54. if filename.startswith('Meta/CMake/vcpkg/overlay-ports/'):
  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. errors_single_page_html_spec = []
  81. for filename in find_files_here_or_argv():
  82. with open(filename, mode="r", encoding='utf-8') as f:
  83. file_content = f.read()
  84. if not is_in_prefix_list(filename, LICENSE_HEADER_CHECK_EXCLUDES):
  85. if not GOOD_LICENSE_HEADER_PATTERN.search(file_content):
  86. errors_license.append(filename)
  87. if filename.endswith('.h'):
  88. if GOOD_PRAGMA_ONCE_PATTERN.search(file_content):
  89. # Excellent, the formatting is correct.
  90. pass
  91. elif PRAGMA_ONCE_STRING in file_content:
  92. # Bad, the '#pragma once' is present but it's formatted wrong.
  93. errors_pragma_once_bad.append(filename)
  94. else:
  95. # Bad, the '#pragma once' is missing completely.
  96. errors_pragma_once_missing.append(filename)
  97. if BAD_INCLUDE_LIBC.search(file_content):
  98. errors_include_libc.append(filename)
  99. if BAD_INCLUDE_COMPLEX.search(file_content):
  100. errors_include_bad_complex.append(filename)
  101. if not is_in_prefix_list(filename, INCLUDE_CHECK_EXCLUDES):
  102. if include_root := find_matching_prefix(filename, LOCAL_INCLUDE_ROOT_OVERRIDES):
  103. local_include_root = pathlib.Path(include_root)
  104. else:
  105. local_include_root = pathlib.Path(filename).parent
  106. for include_line in ANY_INCLUDE_PATTERN.findall(file_content):
  107. if SYSTEM_INCLUDE_PATTERN.match(include_line):
  108. # Don't try to resolve system-style includes, as these might depend on generators.
  109. continue
  110. local_match = LOCAL_INCLUDE_PATTERN.match(include_line)
  111. if local_match is None:
  112. print(f"Cannot parse include-line '{include_line}' in {filename}")
  113. if filename not in errors_include_weird_format:
  114. errors_include_weird_format.append(filename)
  115. continue
  116. relative_filename = local_match.group(1)
  117. referenced_file = local_include_root.joinpath(relative_filename)
  118. if referenced_file.suffix in LOCAL_INCLUDE_SUFFIX_EXCLUDES:
  119. continue
  120. if not referenced_file.exists():
  121. print(f"In {filename}: Cannot find {referenced_file}")
  122. if filename not in errors_include_missing_local:
  123. errors_include_missing_local.append(filename)
  124. if SINGLE_PAGE_HTML_SPEC_LINK.search(file_content):
  125. errors_single_page_html_spec.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 errors_single_page_html_spec:
  161. print(
  162. "Files with links to the single-page HTML spec:",
  163. " ".join(errors_single_page_html_spec)
  164. )
  165. have_errors = True
  166. if have_errors:
  167. sys.exit(1)
  168. if __name__ == '__main__':
  169. os.chdir(os.path.dirname(__file__) + "/..")
  170. run()