From c2dd35cd186fc184383f0b274f24032090bcc742 Mon Sep 17 00:00:00 2001 From: theComputeKid Date: Sat, 17 Feb 2024 13:40:37 +0000 Subject: [PATCH 1/2] Enable code analysis via clang-tidy in Visual Studio (#1) Signed-off-by: thecomputekid --- modules/vstudio/tests/_tests.lua | 1 + .../tests/vc2010/test_output_props.lua | 18 ++++++ .../tests/vc2019/test_output_props.lua | 64 +++++++++++++++++++ modules/vstudio/vs2010_vcxproj.lua | 6 ++ src/_premake_init.lua | 6 ++ website/docs/clangtidy.md | 26 ++++++++ 6 files changed, 121 insertions(+) create mode 100644 modules/vstudio/tests/vc2019/test_output_props.lua create mode 100644 website/docs/clangtidy.md diff --git a/modules/vstudio/tests/_tests.lua b/modules/vstudio/tests/_tests.lua index 3bf71afba0..b3f2fe12b4 100644 --- a/modules/vstudio/tests/_tests.lua +++ b/modules/vstudio/tests/_tests.lua @@ -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 diff --git a/modules/vstudio/tests/vc2010/test_output_props.lua b/modules/vstudio/tests/vc2010/test_output_props.lua index 9ec9a44579..582121c347 100644 --- a/modules/vstudio/tests/vc2010/test_output_props.lua +++ b/modules/vstudio/tests/vc2010/test_output_props.lua @@ -25,6 +25,24 @@ vc2010.outputProperties(cfg) end +-- +-- Ensure clangtidy is not enabled for vc2010. +-- + +function suite.onClangTidy() + clangtidy "On" + prepare() + test.capture [[ + + true + bin\Debug\ + obj\Debug\ + MyProject + .exe + + ]] +end + -- -- Check the structure with the default project values. diff --git a/modules/vstudio/tests/vc2019/test_output_props.lua b/modules/vstudio/tests/vc2019/test_output_props.lua new file mode 100644 index 0000000000..c9ba51e2f6 --- /dev/null +++ b/modules/vstudio/tests/vc2019/test_output_props.lua @@ -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 [[ + + true + bin\Debug\ + obj\Debug\ + MyProject + .exe + true + + ]] +end + +-- +-- Check clangtidy code analysis disabled. +-- + +function suite.onClangTidy_Disabled() + clangtidy "Off" + prepare() + test.capture [[ + + true + bin\Debug\ + obj\Debug\ + MyProject + .exe + false + + ]] +end diff --git a/modules/vstudio/vs2010_vcxproj.lua b/modules/vstudio/vs2010_vcxproj.lua index e2f77c0d17..337164f1ca 100644 --- a/modules/vstudio/vs2010_vcxproj.lua +++ b/modules/vstudio/vs2010_vcxproj.lua @@ -322,6 +322,7 @@ m.extensionsToDeleteOnClean, m.executablePath, m.allModulesPublic, + m.clangtidy, } end end @@ -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 diff --git a/src/_premake_init.lua b/src/_premake_init.lua index 88d6090bc4..0bec3f9394 100644 --- a/src/_premake_init.lua +++ b/src/_premake_init.lua @@ -169,6 +169,12 @@ } } + api.register { + name = "clangtidy", + scope = "config", + kind = "boolean" + } + api.register { name = "compilebuildoutputs", scope = "config", diff --git a/website/docs/clangtidy.md b/website/docs/clangtidy.md new file mode 100644 index 0000000000..25e04f5bd1 --- /dev/null +++ b/website/docs/clangtidy.md @@ -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) From 3b7627484952a88102d8ac81451c9d170d00bb56 Mon Sep 17 00:00:00 2001 From: thecomputekid Date: Mon, 19 Feb 2024 17:53:39 +0000 Subject: [PATCH 2/2] Move vs-specific option to vs folder. Signed-off-by: thecomputekid --- modules/vstudio/_preload.lua | 6 ++++++ src/_premake_init.lua | 6 ------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/vstudio/_preload.lua b/modules/vstudio/_preload.lua index cfd73e75af..47f5cddece 100644 --- a/modules/vstudio/_preload.lua +++ b/modules/vstudio/_preload.lua @@ -216,6 +216,12 @@ } } + p.api.register { + name = "clangtidy", + scope = "config", + kind = "boolean" + } + p.api.register { name = "vsprops", scope = "config", diff --git a/src/_premake_init.lua b/src/_premake_init.lua index 0bec3f9394..88d6090bc4 100644 --- a/src/_premake_init.lua +++ b/src/_premake_init.lua @@ -169,12 +169,6 @@ } } - api.register { - name = "clangtidy", - scope = "config", - kind = "boolean" - } - api.register { name = "compilebuildoutputs", scope = "config",