Skip to content

Commit

Permalink
Merge pull request #2187 from theComputeKid/patch-1
Browse files Browse the repository at this point in the history
Enable code analysis via clang-tidy in Visual Studio
  • Loading branch information
samsinsane authored Feb 27, 2024
2 parents ca21153 + 3b76274 commit 4ca288b
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
6 changes: 6 additions & 0 deletions modules/vstudio/_preload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@
}
}

p.api.register {
name = "clangtidy",
scope = "config",
kind = "boolean"
}

p.api.register {
name = "vsprops",
scope = "config",
Expand Down
1 change: 1 addition & 0 deletions modules/vstudio/tests/_tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ return {
-- Visual Studio 2019+ C/C++ Projects
"vc2019/test_compile_settings.lua",
"vc2019/test_link.lua",
"vc2019/test_output_props.lua",
"vc2019/test_toolset_settings.lua",

-- Visual Studio 2022+ C/C++ Projects
Expand Down
18 changes: 18 additions & 0 deletions modules/vstudio/tests/vc2010/test_output_props.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@
vc2010.outputProperties(cfg)
end

--
-- Ensure clangtidy is not enabled for vc2010.
--

function suite.onClangTidy()
clangtidy "On"
prepare()
test.capture [[
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>bin\Debug\</OutDir>
<IntDir>obj\Debug\</IntDir>
<TargetName>MyProject</TargetName>
<TargetExt>.exe</TargetExt>
</PropertyGroup>
]]
end


--
-- Check the structure with the default project values.
Expand Down
64 changes: 64 additions & 0 deletions modules/vstudio/tests/vc2019/test_output_props.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
--
-- tests/actions/vstudio/vc2019/test_output_props.lua
-- Validate generation of the output property groups.
-- Copyright (c) 2024 Jason Perkins and the Premake project
--

local p = premake
local suite = test.declare("vstudio_vs2019_output_props")
local vc2010 = p.vstudio.vc2010


--
-- Setup
--

local wks, prj

function suite.setup()
p.action.set("vs2019")
wks, prj = test.createWorkspace()
end

local function prepare()
local cfg = test.getconfig(prj, "Debug")
vc2010.outputProperties(cfg)
end

--
-- Check clangtidy code analysis enabled.
--

function suite.onClangTidy_Enabled()
clangtidy "On"
prepare()
test.capture [[
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>bin\Debug\</OutDir>
<IntDir>obj\Debug\</IntDir>
<TargetName>MyProject</TargetName>
<TargetExt>.exe</TargetExt>
<EnableClangTidyCodeAnalysis>true</EnableClangTidyCodeAnalysis>
</PropertyGroup>
]]
end

--
-- Check clangtidy code analysis disabled.
--

function suite.onClangTidy_Disabled()
clangtidy "Off"
prepare()
test.capture [[
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>bin\Debug\</OutDir>
<IntDir>obj\Debug\</IntDir>
<TargetName>MyProject</TargetName>
<TargetExt>.exe</TargetExt>
<EnableClangTidyCodeAnalysis>false</EnableClangTidyCodeAnalysis>
</PropertyGroup>
]]
end
6 changes: 6 additions & 0 deletions modules/vstudio/vs2010_vcxproj.lua
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@
m.extensionsToDeleteOnClean,
m.executablePath,
m.allModulesPublic,
m.clangtidy,
}
end
end
Expand Down Expand Up @@ -3036,6 +3037,11 @@
m.element("TargetName", nil, "%s%s", cfg.buildtarget.prefix, cfg.buildtarget.basename)
end

function m.clangtidy(cfg)
if _ACTION >= "vs2019" and cfg.clangtidy ~= nil then
m.element("EnableClangTidyCodeAnalysis", nil, iif(cfg.clangtidy, "true", "false"))
end
end

function m.latestTargetPlatformVersion(prj)
-- See https://developercommunity.visualstudio.com/content/problem/140294/windowstargetplatformversion-makes-it-impossible-t.html
Expand Down
26 changes: 26 additions & 0 deletions website/docs/clangtidy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Enables clang-tidy code analysis for Visual Studio.

The `clangtidy` option enables running clang-tidy code analysis in Visual Studio projects.

```lua
clangtidy("value")
```

### Parameters ###

`value` is one of:

- `On`
- `Off`

### Applies To ###

The `config` scope.

### Availability ###

Premake 5.0.0 beta 3 or later for Visual Studio 2019 and later.

### See Also ###

* [Using Clang-Tidy in Visual Studio](https://learn.microsoft.com/en-us/cpp/code-quality/clang-tidy?view=msvc-170)

0 comments on commit 4ca288b

Please sign in to comment.