Skip to content

Commit

Permalink
Merge pull request #5592 from xmake-io/tidy
Browse files Browse the repository at this point in the history
improve clang-tidy to support multiple sourcefiles
  • Loading branch information
waruqi authored Dec 25, 2024
2 parents c2d7c1d + a548e1f commit b029e65
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions xmake/modules/private/check/checkers/clang/tidy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
-- imports
import("core.base.option")
import("core.base.task")
import("core.base.semver")
import("core.project.config")
import("core.project.project")
import("lib.detect.find_tool")
Expand Down Expand Up @@ -81,8 +82,8 @@ function _add_target_files(sourcefiles, target)
end
end

-- check sourcefile
function _check_sourcefile(clang_tidy, sourcefile, opt)
-- check sourcefiles
function _check_sourcefiles(clang_tidy, sourcefiles, opt)
opt = opt or {}
local projectdir = project.directory()
local argv = {}
Expand All @@ -108,11 +109,35 @@ function _check_sourcefile(clang_tidy, sourcefile, opt)
if opt.quiet then
table.insert(argv, "--quiet")
end
if not path.is_absolute(sourcefile) then
sourcefile = path.absolute(sourcefile, projectdir)
-- https://github.com/llvm/llvm-project/pull/120547
if clang_tidy.version and semver.compare(clang_tidy.version, "19.1.6") > 0 and #sourcefiles > 32 then
for _, sourcefile in ipairs(sourcefiles) do
if not path.is_absolute(sourcefile) then
sourcefile = path.absolute(sourcefile, projectdir)
end
table.insert(argv, sourcefile)
end
local argsfile = os.tmpfile() .. ".args.txt"
io.writefile(argsfile, os.args(argv))
argv = {"@" .. argsfile}
os.execv(clang_tidy.program, argv, {curdir = projectdir})
os.rm(argsfile)
elseif #sourcefiles <= 32 then
for _, sourcefile in ipairs(sourcefiles) do
if not path.is_absolute(sourcefile) then
sourcefile = path.absolute(sourcefile, projectdir)
end
table.insert(argv, sourcefile)
end
os.execv(clang_tidy.program, argv, {curdir = projectdir})
else
for _, sourcefile in ipairs(sourcefiles) do
if not path.is_absolute(sourcefile) then
sourcefile = path.absolute(sourcefile, projectdir)
end
os.execv(clang_tidy.program, table.join(argv, sourcefile), {curdir = projectdir})
end
end
table.insert(argv, sourcefile)
os.execv(clang_tidy, argv, {curdir = projectdir})
end

-- do check
Expand Down Expand Up @@ -164,15 +189,7 @@ function _check(clang_tidy, opt)
end

-- check files
local jobs = tonumber(opt.jobs or "1")
runjobs("check_files", function (index)
local sourcefile = sourcefiles[index]
if sourcefile then
_check_sourcefile(clang_tidy, sourcefile, opt)
end
end, {total = #sourcefiles,
comax = jobs,
isolate = true})
_check_sourcefiles(clang_tidy, sourcefiles, opt)
end

function main(argv)
Expand All @@ -187,7 +204,7 @@ function main(argv)

-- find clang-tidy
local packages = {}
local clang_tidy = find_tool("clang-tidy")
local clang_tidy = find_tool("clang-tidy", {version = true})
if not clang_tidy then
table.join2(packages, install_packages("llvm"))
end
Expand All @@ -199,17 +216,18 @@ function main(argv)

-- we need to force detect and flush detect cache after loading all environments
if not clang_tidy then
clang_tidy = find_tool("clang-tidy", {force = true})
clang_tidy = find_tool("clang-tidy", {force = true, version = true})
end
assert(clang_tidy, "clang-tidy not found!")
print(clang_tidy)

-- list checks
if args.list then
_show_list(clang_tidy.program)
elseif args.create then
_create_config(clang_tidy.program, args)
else
_check(clang_tidy.program, args)
_check(clang_tidy, args)
end
os.setenvs(oldenvs)
end
Expand Down

0 comments on commit b029e65

Please sign in to comment.