2021-10-26 21:49:28 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
2023-06-01 14:21:52 +00:00
|
|
|
import pathlib
|
2021-10-26 21:49:28 +00:00
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
# Ensure copyright headers match this format and are followed by a blank line:
|
|
|
|
# /*
|
|
|
|
# * Copyright (c) YYYY(-YYYY), Whatever
|
|
|
|
# * ... more of these ...
|
|
|
|
# *
|
|
|
|
# * SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
# */
|
|
|
|
GOOD_LICENSE_HEADER_PATTERN = re.compile(
|
|
|
|
'^/\\*\n' +
|
|
|
|
'( \\* Copyright \\(c\\) [0-9]{4}(-[0-9]{4})?, .*\n)+' +
|
|
|
|
' \\*\n' +
|
|
|
|
' \\* SPDX-License-Identifier: BSD-2-Clause\n' +
|
|
|
|
' \\*/\n' +
|
|
|
|
'\n')
|
|
|
|
LICENSE_HEADER_CHECK_EXCLUDES = {
|
|
|
|
'AK/Checked.h',
|
|
|
|
'AK/Function.h',
|
|
|
|
}
|
|
|
|
|
|
|
|
# We check that "#pragma once" is present
|
|
|
|
PRAGMA_ONCE_STRING = '#pragma once'
|
|
|
|
|
|
|
|
# We make sure that there's a blank line before and after pragma once
|
|
|
|
GOOD_PRAGMA_ONCE_PATTERN = re.compile('(^|\\S\n\n)#pragma once(\n\n\\S.|$)')
|
|
|
|
|
2023-01-03 14:30:44 +00:00
|
|
|
# LibC is supposed to be a system library; don't mention the directory.
|
|
|
|
BAD_INCLUDE_LIBC = re.compile("# *include <LibC/")
|
|
|
|
|
2023-06-07 12:35:35 +00:00
|
|
|
# Serenity C++ code must not use LibC's or libc++'s complex number implementation.
|
|
|
|
BAD_INCLUDE_COMPLEX = re.compile("# *include <c[c]?omplex")
|
|
|
|
|
2023-06-01 14:21:52 +00:00
|
|
|
# Make sure that all includes are either system includes or immediately resolvable local includes
|
|
|
|
ANY_INCLUDE_PATTERN = re.compile('^ *# *include\\b.*[>"](?!\\)).*$', re.M)
|
|
|
|
SYSTEM_INCLUDE_PATTERN = re.compile("^ *# *include *<([^>]+)>(?: /[*/].*)?$")
|
|
|
|
LOCAL_INCLUDE_PATTERN = re.compile('^ *# *include *"([^>]+)"(?: /[*/].*)?$')
|
2023-06-20 17:50:18 +00:00
|
|
|
|
2023-06-01 14:21:52 +00:00
|
|
|
INCLUDE_CHECK_EXCLUDES = {
|
|
|
|
}
|
|
|
|
|
2023-08-29 12:07:25 +00:00
|
|
|
LOCAL_INCLUDE_ROOT_OVERRIDES = {
|
|
|
|
}
|
|
|
|
|
2023-06-20 17:50:18 +00:00
|
|
|
LOCAL_INCLUDE_SUFFIX_EXCLUDES = [
|
|
|
|
# Some Qt files are required to include their .moc files, which will be located in a deep
|
|
|
|
# subdirectory that we won't find from here.
|
|
|
|
'.moc',
|
|
|
|
]
|
|
|
|
|
2021-10-26 21:49:28 +00:00
|
|
|
|
2022-09-18 18:10:57 +00:00
|
|
|
def should_check_file(filename):
|
|
|
|
if not filename.endswith('.cpp') and not filename.endswith('.h'):
|
|
|
|
return False
|
|
|
|
if filename.startswith('Base/'):
|
|
|
|
return False
|
|
|
|
return True
|
2021-10-26 21:49:28 +00:00
|
|
|
|
|
|
|
|
2022-09-18 18:10:57 +00:00
|
|
|
def find_files_here_or_argv():
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
raw_list = sys.argv[1:]
|
|
|
|
else:
|
|
|
|
process = subprocess.run(["git", "ls-files"], check=True, capture_output=True)
|
|
|
|
raw_list = process.stdout.decode().strip('\n').split('\n')
|
|
|
|
|
|
|
|
return filter(should_check_file, raw_list)
|
|
|
|
|
|
|
|
|
2023-06-01 14:21:52 +00:00
|
|
|
def is_in_prefix_list(filename, prefix_list):
|
|
|
|
return any(
|
|
|
|
filename.startswith(prefix) for prefix in prefix_list
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-29 12:07:25 +00:00
|
|
|
def find_matching_prefix(filename, prefix_list):
|
|
|
|
matching_prefixes = [prefix for prefix in prefix_list if filename.startswith(prefix)]
|
|
|
|
assert len(matching_prefixes) <= 1
|
|
|
|
return matching_prefixes[0] if matching_prefixes else None
|
|
|
|
|
|
|
|
|
2022-09-18 18:10:57 +00:00
|
|
|
def run():
|
2021-10-26 21:49:28 +00:00
|
|
|
errors_license = []
|
|
|
|
errors_pragma_once_bad = []
|
|
|
|
errors_pragma_once_missing = []
|
2023-01-03 14:30:44 +00:00
|
|
|
errors_include_libc = []
|
2023-06-01 14:21:52 +00:00
|
|
|
errors_include_weird_format = []
|
|
|
|
errors_include_missing_local = []
|
2023-06-07 12:35:35 +00:00
|
|
|
errors_include_bad_complex = []
|
2021-10-26 21:49:28 +00:00
|
|
|
|
2022-09-18 18:10:57 +00:00
|
|
|
for filename in find_files_here_or_argv():
|
2021-10-26 21:49:28 +00:00
|
|
|
with open(filename, "r") as f:
|
|
|
|
file_content = f.read()
|
2023-06-01 14:21:52 +00:00
|
|
|
if not is_in_prefix_list(filename, LICENSE_HEADER_CHECK_EXCLUDES):
|
2021-10-26 21:49:28 +00:00
|
|
|
if not GOOD_LICENSE_HEADER_PATTERN.search(file_content):
|
|
|
|
errors_license.append(filename)
|
|
|
|
if filename.endswith('.h'):
|
2024-11-09 17:50:33 +00:00
|
|
|
if GOOD_PRAGMA_ONCE_PATTERN.search(file_content):
|
2021-10-26 21:49:28 +00:00
|
|
|
# Excellent, the formatting is correct.
|
|
|
|
pass
|
|
|
|
elif PRAGMA_ONCE_STRING in file_content:
|
|
|
|
# Bad, the '#pragma once' is present but it's formatted wrong.
|
|
|
|
errors_pragma_once_bad.append(filename)
|
|
|
|
else:
|
|
|
|
# Bad, the '#pragma once' is missing completely.
|
|
|
|
errors_pragma_once_missing.append(filename)
|
2023-04-29 18:37:40 +00:00
|
|
|
if BAD_INCLUDE_LIBC.search(file_content):
|
|
|
|
errors_include_libc.append(filename)
|
2023-06-07 12:35:35 +00:00
|
|
|
if BAD_INCLUDE_COMPLEX.search(file_content):
|
|
|
|
errors_include_bad_complex.append(filename)
|
2023-06-01 14:21:52 +00:00
|
|
|
if not is_in_prefix_list(filename, INCLUDE_CHECK_EXCLUDES):
|
2023-08-29 12:07:25 +00:00
|
|
|
if include_root := find_matching_prefix(filename, LOCAL_INCLUDE_ROOT_OVERRIDES):
|
|
|
|
local_include_root = pathlib.Path(include_root)
|
|
|
|
else:
|
|
|
|
local_include_root = pathlib.Path(filename).parent
|
2023-06-01 14:21:52 +00:00
|
|
|
for include_line in ANY_INCLUDE_PATTERN.findall(file_content):
|
|
|
|
if SYSTEM_INCLUDE_PATTERN.match(include_line):
|
|
|
|
# Don't try to resolve system-style includes, as these might depend on generators.
|
|
|
|
continue
|
|
|
|
local_match = LOCAL_INCLUDE_PATTERN.match(include_line)
|
|
|
|
if local_match is None:
|
|
|
|
print(f"Cannot parse include-line '{include_line}' in {filename}")
|
|
|
|
if filename not in errors_include_weird_format:
|
|
|
|
errors_include_weird_format.append(filename)
|
|
|
|
continue
|
2023-06-20 17:50:18 +00:00
|
|
|
|
2023-06-01 14:21:52 +00:00
|
|
|
relative_filename = local_match.group(1)
|
2023-08-29 12:07:25 +00:00
|
|
|
referenced_file = local_include_root.joinpath(relative_filename)
|
2023-06-20 17:50:18 +00:00
|
|
|
|
|
|
|
if referenced_file.suffix in LOCAL_INCLUDE_SUFFIX_EXCLUDES:
|
|
|
|
continue
|
|
|
|
|
2023-06-01 14:21:52 +00:00
|
|
|
if not referenced_file.exists():
|
|
|
|
print(f"In {filename}: Cannot find {referenced_file}")
|
|
|
|
if filename not in errors_include_missing_local:
|
|
|
|
errors_include_missing_local.append(filename)
|
|
|
|
|
|
|
|
have_errors = False
|
2021-10-26 21:49:28 +00:00
|
|
|
if errors_license:
|
|
|
|
print("Files with bad licenses:", " ".join(errors_license))
|
2023-06-01 14:21:52 +00:00
|
|
|
have_errors = True
|
2021-10-26 21:49:28 +00:00
|
|
|
if errors_pragma_once_missing:
|
|
|
|
print("Files without #pragma once:", " ".join(errors_pragma_once_missing))
|
2023-06-01 14:21:52 +00:00
|
|
|
have_errors = True
|
2021-10-26 21:49:28 +00:00
|
|
|
if errors_pragma_once_bad:
|
|
|
|
print("Files with a bad #pragma once:", " ".join(errors_pragma_once_bad))
|
2023-06-01 14:21:52 +00:00
|
|
|
have_errors = True
|
2023-01-03 14:30:44 +00:00
|
|
|
if errors_include_libc:
|
2023-06-01 14:21:52 +00:00
|
|
|
print(
|
|
|
|
"Files that include a LibC header using #include <LibC/...>:",
|
|
|
|
" ".join(errors_include_libc),
|
|
|
|
)
|
|
|
|
have_errors = True
|
|
|
|
if errors_include_weird_format:
|
|
|
|
print(
|
|
|
|
"Files that contain badly-formatted #include statements:",
|
|
|
|
" ".join(errors_include_weird_format),
|
|
|
|
)
|
|
|
|
have_errors = True
|
|
|
|
if errors_include_missing_local:
|
|
|
|
print(
|
|
|
|
"Files that #include a missing local file:",
|
|
|
|
" ".join(errors_include_missing_local),
|
|
|
|
)
|
|
|
|
have_errors = True
|
2023-06-07 12:35:35 +00:00
|
|
|
if errors_include_bad_complex:
|
|
|
|
print(
|
|
|
|
"Files that include a non-AK complex header:",
|
|
|
|
" ".join(errors_include_bad_complex),
|
|
|
|
)
|
|
|
|
have_errors = True
|
2023-06-01 14:21:52 +00:00
|
|
|
|
|
|
|
if have_errors:
|
2021-10-26 21:49:28 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
os.chdir(os.path.dirname(__file__) + "/..")
|
|
|
|
run()
|