Skip to content

Commit ee75f6d

Browse files
authored
Merge pull request #6811 from xmake-io/tidy
improve clang-tidy
2 parents 3a0dffa + 5a1d0e6 commit ee75f6d

File tree

1 file changed

+32
-18
lines changed
  • xmake/modules/private/check/checkers/clang

1 file changed

+32
-18
lines changed

xmake/modules/private/check/checkers/clang/tidy.lua

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import("core.project.config")
2626
import("core.project.project")
2727
import("lib.detect.find_tool")
2828
import("async.runjobs")
29+
import("utils.progress")
2930
import("private.action.require.impl.packagenv")
3031
import("private.action.require.impl.install_packages")
3132

@@ -44,14 +45,14 @@ local options = {
4445
{nil, "checks", "kv", nil, "Set the given checks.",
4546
"e.g.",
4647
" - xmake check clang.tidy --checks=\"*\""},
47-
{"f", "files", "v", nil, "Set files path with pattern",
48+
{"f", "files", "kv", nil, "Set files path with pattern",
4849
"e.g.",
4950
" - xmake check clang.tidy -f src/main.c",
5051
" - xmake check clang.tidy -f 'src/*.c" .. path.envsep() .. "src/**.cpp'"},
51-
{nil, "target", "v", nil, "Check the sourcefiles of the given target.",
52+
{nil, "targets", "vs", nil, "Check the sourcefiles of the given target.",
5253
".e.g",
5354
" - xmake check clang.tidy",
54-
" - xmake check clang.tidy [target]"}
55+
" - xmake check clang.tidy [targets]"}
5556
}
5657

5758
-- show checks list
@@ -110,33 +111,44 @@ function _check_sourcefiles(clang_tidy, sourcefiles, opt)
110111
table.insert(argv, "--quiet")
111112
end
112113
-- https://github.com/llvm/llvm-project/pull/120547
113-
if clang_tidy.version and semver.compare(clang_tidy.version, "19.1.6") > 0 and #sourcefiles > 32 then
114+
local arguments_maxn = 32
115+
if clang_tidy.version and semver.compare(clang_tidy.version, "19.1.6") > 0 and #sourcefiles > arguments_maxn then
114116
for _, sourcefile in ipairs(sourcefiles) do
115117
if not path.is_absolute(sourcefile) then
116118
sourcefile = path.absolute(sourcefile, projectdir)
117119
end
118120
table.insert(argv, sourcefile)
119121
end
122+
progress.show(100, "clang-tidy.analyzing %s .. %d", sourcefiles[1], #sourcefiles)
120123
local argsfile = os.tmpfile() .. ".args.txt"
121124
io.writefile(argsfile, os.args(argv))
122125
argv = {"@" .. argsfile}
123-
os.execv(clang_tidy.program, argv, {curdir = projectdir})
126+
os.vrunv(clang_tidy.program, argv, {curdir = projectdir})
124127
os.rm(argsfile)
125-
elseif #sourcefiles <= 32 then
126-
for _, sourcefile in ipairs(sourcefiles) do
127-
if not path.is_absolute(sourcefile) then
128-
sourcefile = path.absolute(sourcefile, projectdir)
129-
end
130-
table.insert(argv, sourcefile)
131-
end
132-
os.execv(clang_tidy.program, argv, {curdir = projectdir})
133128
else
129+
-- split sourcefiles
130+
local sourcefiles_argv = {}
131+
local sourcefiles_jobs = {}
134132
for _, sourcefile in ipairs(sourcefiles) do
135133
if not path.is_absolute(sourcefile) then
136134
sourcefile = path.absolute(sourcefile, projectdir)
137135
end
138-
os.execv(clang_tidy.program, table.join(argv, sourcefile), {curdir = projectdir})
136+
table.insert(sourcefiles_argv, sourcefile)
137+
if #sourcefiles_argv >= arguments_maxn then
138+
table.insert(sourcefiles_jobs, sourcefiles_argv)
139+
sourcefiles_argv = {}
140+
end
141+
end
142+
if #sourcefiles_argv > 0 then
143+
table.insert(sourcefiles_jobs, sourcefiles_argv)
139144
end
145+
146+
-- run clang-tidy
147+
runjobs("checker.tidy", function (index, total, opt)
148+
local tidy_argv = sourcefiles_jobs[index]
149+
progress.show(index * 100 / total, "clang-tidy.analyzing %s .. %d", tidy_argv[1], #tidy_argv)
150+
os.vrunv(clang_tidy.program, tidy_argv, {curdir = projectdir})
151+
end, {total = #sourcefiles_jobs, comax = opt.jobs or os.default_njob()})
140152
end
141153
end
142154

@@ -178,9 +190,12 @@ function _check(clang_tidy, opt)
178190
end
179191
end
180192
else
181-
local targetname = opt.target
182-
if targetname then
183-
_add_target_files(sourcefiles, project.target(targetname))
193+
local targetnames = opt.targets
194+
if targetnames then
195+
for _, targetname in ipairs(targetnames) do
196+
local target = assert(project.target(targetname), "unknown target(%s)", targetname)
197+
_add_target_files(sourcefiles, target)
198+
end
184199
else
185200
for _, target in ipairs(project.ordertargets()) do
186201
_add_target_files(sourcefiles, target)
@@ -219,7 +234,6 @@ function main(argv)
219234
clang_tidy = find_tool("clang-tidy", {force = true, version = true})
220235
end
221236
assert(clang_tidy, "clang-tidy not found!")
222-
print(clang_tidy)
223237

224238
-- list checks
225239
if args.list then

0 commit comments

Comments
 (0)