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

Enable code analysis via clang-tidy in Visual Studio #2187

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
6 changes: 6 additions & 0 deletions src/_premake_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@
}
}

api.register {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it seems visualstudio specific, put it in vstudio/_preload.lua instead.
Or maybe other generators "just" have to call clang-tidy source.cpp $(CXXFLAGS) after cxx source.cpp $(CXXFLAGS)...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it seems visualstudio specific, put it in vstudio/_preload.lua instead.

This suggestion is easier, and something I have now added a commit for. Perhaps clang-tidy for other generators can be future work.

name = "clangtidy",
scope = "config",
kind = "boolean"
}

api.register {
name = "compilebuildoutputs",
scope = "config",
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)
Loading