check-style.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. import os
  3. import re
  4. import subprocess
  5. import sys
  6. # Ensure copyright headers match this format and are followed by a blank line:
  7. # /*
  8. # * Copyright (c) YYYY(-YYYY), Whatever
  9. # * ... more of these ...
  10. # *
  11. # * SPDX-License-Identifier: BSD-2-Clause
  12. # */
  13. GOOD_LICENSE_HEADER_PATTERN = re.compile(
  14. '^/\\*\n' +
  15. '( \\* Copyright \\(c\\) [0-9]{4}(-[0-9]{4})?, .*\n)+' +
  16. ' \\*\n' +
  17. ' \\* SPDX-License-Identifier: BSD-2-Clause\n' +
  18. ' \\*/\n' +
  19. '\n')
  20. LICENSE_HEADER_CHECK_EXCLUDES = {
  21. 'AK/Checked.h',
  22. 'AK/Function.h',
  23. 'Userland/Libraries/LibC/elf.h',
  24. 'Userland/DevTools/HackStudio/LanguageServers/Cpp/Tests/',
  25. 'Userland/Libraries/LibCpp/Tests/parser/',
  26. 'Userland/Libraries/LibCpp/Tests/preprocessor/'
  27. }
  28. # We check that "#pragma once" is present
  29. PRAGMA_ONCE_STRING = '#pragma once'
  30. PRAGMA_ONCE_CHECK_EXCLUDES = {
  31. 'Userland/Libraries/LibC/assert.h',
  32. }
  33. # We make sure that there's a blank line before and after pragma once
  34. GOOD_PRAGMA_ONCE_PATTERN = re.compile('(^|\\S\n\n)#pragma once(\n\n\\S.|$)')
  35. # We check that "#include <LibM/math.h>" is not being used
  36. LIBM_MATH_H_INCLUDE_STRING = '#include <LibM/math.h>'
  37. GIT_LS_FILES = ['git', 'ls-files', '--', '*.cpp', '*.h', ':!:Base', ':!:Kernel/FileSystem/ext2_fs.h']
  38. def run():
  39. files = subprocess.run(GIT_LS_FILES, check=True, capture_output=True).stdout.decode().strip('\n').split('\n')
  40. assert len(files) > 1000
  41. errors_license = []
  42. errors_libm_math_h = []
  43. errors_pragma_once_bad = []
  44. errors_pragma_once_missing = []
  45. for filename in files:
  46. with open(filename, "r") as f:
  47. file_content = f.read()
  48. if not any(filename.startswith(forbidden_prefix) for forbidden_prefix in LICENSE_HEADER_CHECK_EXCLUDES):
  49. if not GOOD_LICENSE_HEADER_PATTERN.search(file_content):
  50. errors_license.append(filename)
  51. if LIBM_MATH_H_INCLUDE_STRING in file_content:
  52. errors_libm_math_h.append(filename)
  53. if filename.endswith('.h'):
  54. if any(filename.startswith(forbidden_prefix) for forbidden_prefix in PRAGMA_ONCE_CHECK_EXCLUDES):
  55. # File was excluded
  56. pass
  57. elif GOOD_PRAGMA_ONCE_PATTERN.search(file_content):
  58. # Excellent, the formatting is correct.
  59. pass
  60. elif PRAGMA_ONCE_STRING in file_content:
  61. # Bad, the '#pragma once' is present but it's formatted wrong.
  62. errors_pragma_once_bad.append(filename)
  63. else:
  64. # Bad, the '#pragma once' is missing completely.
  65. errors_pragma_once_missing.append(filename)
  66. if errors_license:
  67. print("Files with bad licenses:", " ".join(errors_license))
  68. if errors_pragma_once_missing:
  69. print("Files without #pragma once:", " ".join(errors_pragma_once_missing))
  70. if errors_pragma_once_bad:
  71. print("Files with a bad #pragma once:", " ".join(errors_pragma_once_bad))
  72. if errors_libm_math_h:
  73. print("Files including LibM/math.h (include just 'math.h' instead):", " ".join(errors_libm_math_h))
  74. if errors_license or errors_pragma_once_missing or errors_pragma_once_bad or errors_libm_math_h:
  75. sys.exit(1)
  76. if __name__ == '__main__':
  77. os.chdir(os.path.dirname(__file__) + "/..")
  78. run()