Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and Test Clang-tidy #1710

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion buildsystem/codecompliance/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def parse_args():
help="increase program verbosity")
cli.add_argument("-q", "--quiet", action="count", default=0,
help="decrease program verbosity")
cli.add_argument("--clangtidy", action="store_true",
help="Check the C++ code with clang-tidy.")

args = cli.parse_args()
process_args(args, cli.error)
Expand Down Expand Up @@ -92,10 +94,11 @@ def process_args(args, error):
args.pystyle = True
args.pylint = True
args.test_git_change_years = True
args.clangtidy = True

if not any((args.headerguards, args.legal, args.authors, args.pystyle,
args.cppstyle, args.cython, args.test_git_change_years,
args.pylint, args.filemodes, args.textfiles)):
args.pylint, args.filemodes, args.textfiles, args.clangtidy)):
error("no checks were specified")

has_git = bool(shutil.which('git'))
Expand Down Expand Up @@ -127,6 +130,9 @@ def process_args(args, error):
if args.pylint:
if not importlib.util.find_spec('pylint'):
error("pylint python module required for linting")
if args.clangtidy:
if not shutil.which('clang-tidy'):
error("--clang-tidy requires clang-tidy to be installed")


def get_changed_files(gitref):
Expand Down Expand Up @@ -264,6 +270,9 @@ def find_all_issues(args, check_files=None):
from .modes import find_issues
yield from find_issues(check_files, ('openage', 'buildsystem',
'libopenage', 'etc/gdb_pretty'))
if args.clangtidy:
from .clangtidy import find_issues
yield from find_issues(check_files, ('libopenage', ))


if __name__ == '__main__':
Expand Down
66 changes: 66 additions & 0 deletions buildsystem/codecompliance/clangtidy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2024-2024 the openage authors. See copying.md for legal info.

"""
Checks clang-tidy errors on cpp files
"""

import subprocess
from .cppstyle import filter_file_list
from .util import findfiles


def find_issues(check_files, dirnames):
"""
Invoke clang-tidy to check C++ files for issues.
Yields issues found by clang-tidy in real-time.
"""
# Specify the checks to include
checks_to_include = [
'clang-analyzer-*',
'bugprone-*',
'concurrency-*',
'performance-*'
]
# Create the checks string
checks = ', '.join(checks_to_include)

# Invocation command
invocation = ['clang-tidy', f'-checks=-*,{checks}']

if check_files is not None:
filenames = list(filter_file_list(check_files, dirnames))
else:
filenames = list(filter_file_list(findfiles(dirnames), dirnames))

if not filenames:
print("No files to check.")
return # No files to check

for filename in filenames:
# Run clang-tidy for each file
print(f"Starting clang-tidy check on file: {filename}")
try:
with subprocess.Popen(
invocation + [filename],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
) as process:
# Stream output in real-time
while True:
output = process.stdout.readline()
if output:
yield ("clang-tidy output", output.strip(), None)
elif process.poll() is not None:
break

# Capture remaining errors (if any)
for error_line in process.stderr:
yield ("clang-tidy error", error_line.strip(), None)

except subprocess.SubprocessError as exc:
yield (
"clang-tidy error",
f"An error occurred while running clang-tidy on {filename}: {str(exc)}",
None
)
1 change: 1 addition & 0 deletions copying.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ _the openage authors_ are:
| Edvin Lindholm | EdvinLndh | edvinlndh à gmail dawt com |
| Jeremiah Morgan | jere8184 | jeremiahmorgan dawt bham à outlook dawt com |
| Tobias Alam | alamt22 | tobiasal à umich dawt edu |
| Haytham Tang | haytham918 | yunxuant à umich dawt edu |

If you're a first-time committer, add yourself to the above list. This is not
just for legal reasons, but also to keep an overview of all those nicknames.
Expand Down
1 change: 1 addition & 0 deletions doc/building.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Dependency list:
CR qt6 >=6.2 (Core, Quick, QuickControls, Multimedia modules)
CR toml11
CR O vulkan
S clang-tidy
haytham918 marked this conversation as resolved.
Show resolved Hide resolved

A An installed version of any of the following (wine is your friend).
Other versions _might_ work:
Expand Down