2

I use cmake (3.23+) to generate solution files for MSVC (via CMakeLists.txt).

I'd like to enable static analysis for generated projects, and make it use my custom ruleset. I am aware that MSVC supports a CmakeSettings.json file that can do this, but I don't understand how it relates to CMakeLists.txt/how it should be included here.

I've tried to do it via command line parameters:

target_compile_options ( ${PROJECT_NAME}
    PRIVATE /MP /analyze /analyze:ruleset ${CMAKE_CURRENT_SOURCE_DIR}/use-after-move.ruleset
)

But the compiler treats the ruleset as another .cpp file to compile. The generated command line looks like this:

CL.exe /c ...list of include directories... /Zi /nologo /W1 /WX- /diagnostics:column /MP /Od /Ob0 /D _MBCS /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /GR /std:c++latest /Fo"editor-lib.dir\Debug\\" /Fd".\vsbuild\lib\Debug\editor-lib-d.pdb" /external:W1 /Gd /TP /analyze /errorReport:prompt path/to/use-after-move.ruleset ...list of source files...

I tried wrapping the /analyze:ruleset into double quotes, but with the same outcome. I also tried to wrapping it with single quotes which made it show up in the compile options, however like this:

'/analyze:ruleset' path/to/ruleset

I also might be using these options wrong as the analysis is not performed when I manually plug them in project properties. Analysis is only performed when I drop the /analyze:ruleset and keep in just /analyze (but then it defaults to Microsoft's All Rules).

doomista
  • 501
  • 2
  • 10
  • @starball I only have a single target_compile_options command for this project and the only place I used /analyze. Looking at the command line in MSVC, the /analyze:ruleset is not even there and the path to ruleset just sits alone. Also I doubt it works properly in MSVC, because I've tried to enable it via command line parameters directly in the project properties (without cmake) on two different machines with MSVC 17.4+ and it never worked. It only worked when setting it in property sheet/project properties for code analysis. – doomista Jan 06 '23 at 18:23
  • Thanks for reminding me to update the question. It seems like cmake deduplication feature just erases the `/analyze:ruleset` option. Second thing is that MSVC doesn't treat `/analyze:ruleset` as I would expect (see the updated clarification). – doomista Jan 07 '23 at 09:50

1 Answers1

1

I found an answer in this SO thread: How to add Property to Affect Code Analysis in CMake

Create user property sheet (saved as EnableUseAfterMoveAnalysis.props):

<Project>
    <PropertyGroup>
        <RunCodeAnalysis>true</RunCodeAnalysis>
        <CodeAnalysisRuleset>path/to/use-after-move.ruleset</CodeAnalysisRuleset>
    </PropertyGroup>
</Project>

And then in CMakeLists.txt bind it to the project using set_property:

set_property(
    TARGET ${PROJECT_NAME}
    PROPERTY VS_USER_PROPS "path/to/EnableUseAfterMoveAnalysis.props"
)
doomista
  • 501
  • 2
  • 10