Skip to content

Commit 0ce621f

Browse files
authored
Merge pull request #1710 from haytham918/clang-tidy-feature
Add and Test Clang-tidy
2 parents fb36b9d + 8debe0c commit 0ce621f

File tree

6 files changed

+109
-16
lines changed

6 files changed

+109
-16
lines changed

Makefile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,26 @@ mrproperer: mrproper
128128
checkfast:
129129
python3 -m buildsystem.codecompliance --fast
130130

131-
.PHONY: checkall
132-
checkall:
133-
python3 -m buildsystem.codecompliance --all
131+
.PHONY: checkmerge
132+
checkmerge:
133+
python3 -m buildsystem.codecompliance --merge
134134

135135
.PHONY: checkchanged
136136
checkchanged:
137-
python3 -m buildsystem.codecompliance --all --only-changed-files=origin/master
137+
python3 -m buildsystem.codecompliance --merge --only-changed-files=origin/master
138138

139139
.PHONY: checkuncommited
140140
checkuncommited:
141-
python3 -m buildsystem.codecompliance --all --only-changed-files=HEAD
141+
python3 -m buildsystem.codecompliance --merge --only-changed-files=HEAD
142142

143143
.PHONY: checkpy
144144
checkpy:
145145
python3 -m buildsystem.codecompliance --pystyle --pylint
146146

147+
.PHONY: checkall
148+
checkall:
149+
python3 -m buildsystem.codecompliance --all
150+
147151
.PHONY: help
148152
help: $(BUILDDIR)/Makefile
149153
@echo "openage Makefile"

buildsystem/codecompliance/__main__.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2014-2024 the openage authors. See copying.md for legal info.
1+
# Copyright 2014-2025 the openage authors. See copying.md for legal info.
22

33
"""
44
Entry point for the code compliance checker.
@@ -18,16 +18,24 @@ def parse_args():
1818
""" Returns the raw argument namespace. """
1919

2020
cli = argparse.ArgumentParser()
21-
cli.add_argument("--fast", action="store_true",
22-
help="do all checks that can be performed quickly")
23-
cli.add_argument("--all", action="store_true",
24-
help="do all checks, even the really slow ones")
21+
check_types = cli.add_mutually_exclusive_group()
22+
check_types.add_argument("--fast", action="store_true",
23+
help="do all checks that can be performed quickly")
24+
check_types.add_argument("--merge", action="store_true",
25+
help="do all checks that are required before merges to master")
26+
check_types.add_argument("--all", action="store_true",
27+
help="do all checks, even the really slow ones")
28+
2529
cli.add_argument("--only-changed-files", metavar='GITREF',
2630
help=("slow checks are only done on files that have "
2731
"changed since GITREF."))
2832
cli.add_argument("--authors", action="store_true",
2933
help=("check whether all git authors are in copying.md. "
3034
"repo must be a git repository."))
35+
cli.add_argument("--clang-tidy", action="store_true",
36+
help=("Check the C++ code with clang-tidy. Make sure you have build the "
37+
"project with ./configure --clang-tidy or have set "
38+
"CMAKE_CXX_CLANG_TIDY for your CMake build."))
3139
cli.add_argument("--cppstyle", action="store_true",
3240
help="check the cpp code style")
3341
cli.add_argument("--cython", action="store_true",
@@ -76,7 +84,7 @@ def process_args(args, error):
7684
# set up log level
7785
log_setup(args.verbose - args.quiet)
7886

79-
if args.fast or args.all:
87+
if args.fast or args.merge or args.all:
8088
# enable "fast" tests
8189
args.authors = True
8290
args.cppstyle = True
@@ -86,16 +94,19 @@ def process_args(args, error):
8694
args.filemodes = True
8795
args.textfiles = True
8896

89-
if args.all:
90-
# enable tests that take a bit longer
91-
97+
if args.merge or args.all:
98+
# enable tests that are required before merging to master
9299
args.pystyle = True
93100
args.pylint = True
94101
args.test_git_change_years = True
95102

103+
if args.all:
104+
# enable tests that take a bit longer
105+
args.clang_tidy = True
106+
96107
if not any((args.headerguards, args.legal, args.authors, args.pystyle,
97108
args.cppstyle, args.cython, args.test_git_change_years,
98-
args.pylint, args.filemodes, args.textfiles)):
109+
args.pylint, args.filemodes, args.textfiles, args.clang_tidy)):
99110
error("no checks were specified")
100111

101112
has_git = bool(shutil.which('git'))
@@ -128,6 +139,10 @@ def process_args(args, error):
128139
if not importlib.util.find_spec('pylint'):
129140
error("pylint python module required for linting")
130141

142+
if args.clang_tidy:
143+
if not shutil.which('clang-tidy'):
144+
error("--clang-tidy requires clang-tidy to be installed")
145+
131146

132147
def get_changed_files(gitref):
133148
"""
@@ -264,6 +279,9 @@ def find_all_issues(args, check_files=None):
264279
from .modes import find_issues
265280
yield from find_issues(check_files, ('openage', 'buildsystem',
266281
'libopenage', 'etc/gdb_pretty'))
282+
if args.clang_tidy:
283+
from .clangtidy import find_issues
284+
yield from find_issues(check_files, ('libopenage', ))
267285

268286

269287
if __name__ == '__main__':
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright 2024-2024 the openage authors. See copying.md for legal info.
2+
3+
"""
4+
Checks clang-tidy errors on cpp files
5+
"""
6+
7+
import subprocess
8+
from .cppstyle import filter_file_list
9+
from .util import findfiles
10+
11+
12+
def find_issues(check_files, dirnames):
13+
"""
14+
Invoke clang-tidy to check C++ files for issues.
15+
Yields issues found by clang-tidy in real-time.
16+
"""
17+
# Specify the checks to include
18+
# 4 checks we focus on
19+
checks_to_include = [
20+
'clang-analyzer-*',
21+
'bugprone-*',
22+
'concurrency-*',
23+
'performance-*'
24+
]
25+
# Create the checks string
26+
checks = ', '.join(checks_to_include)
27+
28+
# Invocation command
29+
invocation = ['clang-tidy', f'-checks=-*,{checks}']
30+
31+
# Use utility functions from util.py and cppstyle.py
32+
if check_files is not None:
33+
filenames = list(filter_file_list(check_files, dirnames))
34+
else:
35+
filenames = list(filter_file_list(findfiles(dirnames), dirnames))
36+
37+
if not filenames:
38+
print("No files to check.")
39+
return # No files to check
40+
41+
for filename in filenames:
42+
# Run clang-tidy for each file
43+
print(f"Starting clang-tidy check on file: {filename}")
44+
try:
45+
with subprocess.Popen(
46+
invocation + [filename],
47+
stdout=subprocess.PIPE,
48+
stderr=subprocess.PIPE,
49+
text=True
50+
) as process:
51+
# Stream output in real-time
52+
while True:
53+
output = process.stdout.readline()
54+
if output:
55+
yield ("clang-tidy output", output.strip(), None)
56+
elif process.poll() is not None:
57+
break
58+
59+
# Capture remaining errors (if any)
60+
for error_line in process.stderr:
61+
yield ("clang-tidy error", error_line.strip(), None)
62+
63+
# Handle exception
64+
except subprocess.SubprocessError as exc:
65+
yield (
66+
"clang-tidy error",
67+
f"An error occurred while running clang-tidy on {filename}: {str(exc)}",
68+
None
69+
)

copying.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ _the openage authors_ are:
161161
| David Wever | dmwever | dmwever à crimson dawt ua dawt edu |
162162
| Michael Lynch | mtlynch | git à mtlynch dawt io |
163163
| Ngô Xuân Minh | | xminh dawt ngo dawt 00 à gmail dawt com |
164+
| Haytham Tang | haytham918 | yunxuant à umich dawt edu |
164165

165166
If you're a first-time committer, add yourself to the above list. This is not
166167
just for legal reasons, but also to keep an overview of all those nicknames.

doc/building.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Dependency list:
3939
CR opengl >=3.3
4040
CR libepoxy
4141
CR libpng
42+
S clang-tidy
4243
R dejavu font
4344
CR eigen >=3
4445
CR freetype2

kevinfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
sanity_check:
88
- skip (? if job != "debian" ?)
9-
make checkall
9+
make checkmerge
1010

1111
# Various optimisation options can affect warnings compiler generates.
1212
# Make sure both release and debug are tested. Arch job has more checks,

0 commit comments

Comments
 (0)