【问题标题】:How do I force msbuild to create SARIF Files on CodeAnalysis如何强制 msbuild 在 CodeAnalysis 上创建 SARIF 文件
【发布时间】:2022-10-12 23:07:06
【问题描述】:

如果我在 Visual Studio 2022 中运行代码分析(在 c++ 项目上),我会为每个代码文件获得一个 XML 和一个 SARIF 文件。

不,我尝试使用 MSBuild 2022 运行代码分析:

MSBuild.exe solution.sln -p:Configuration=Release /p:RunCodeAnalysis=true

但是通过这个调用,我只得到代码分析 XML 文件,没有 SARIF 文件。

知道如何强制 MSBuild 创建 SARIF 文件吗?

【问题讨论】:

    标签: visual-studio msbuild static-analysis sarif


    【解决方案1】:

    尝试使用以下命令行:

    cl.exe <file/project path> /analyze:autolog:ext .nativecodeanalysis.sarif

    或者

    cl.exe <file/project path> /analyze:autolog:ext .sarif

    虽然 MSBuild.exe 调用 cl.exe 进行编译,但创建 .sarif 文件似乎只能用于直接使用 cl.exe 及其命令。

    以下是相关文档:Analysis log options

    /analyze:autolog:ext extension

    覆盖分析日志文件的默认扩展名,并改用扩展名。如果您使用 .sarif 扩展名,日志文件将使用 SARIF 格式而不是默认的 XML 格式。

    【讨论】:

    • 感谢你的回答!当我将路径指向带有 *.obj 文件的输出目录时,一些文件可以工作,但随后出现链接器错误(LINK:致命错误 LNK1104:无法打开文件'MSVCRT.lib')。也许将生成的 xml 文件转换为 sarif 比修复这个问题更容易......
    【解决方案2】:

    https://docs.microsoft.com/en-us/answers/questions/512275/what-to-do-with-static-code-analysis-result-xml-fi.html 描述了一个解决方案:

    Directory.build.props 文件添加到您的 Visual Studio 解决方案中:

    <?xml version="1.0" encoding="utf-8"?> 
     <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
       <ItemDefinitionGroup>
         <ClCompile>
             <AdditionalOptions>$(ClOptions) %(AdditionalOptions)</AdditionalOptions>
         </ClCompile>
       </ItemDefinitionGroup>
     </Project>
    

    现在我可以在我的 CI-Server (TeamCity) 上扩展我的 MSBuild 命令行:

    /p:RunCodeAnalysis=true /p:ClOptions="/analyze:log%20MyApp.nativecodeanalysis.combined.sarif"(我必须用%20 替换空格)。

    并生成一个 SARIF 文件,或者如果您希望每个代码文件都有一个 SARIF 文件:

    /p:RunCodeAnalysis=true /p:CaOptions="/analyze:log:format:sarif"

    如果你想添加额外的命令行开关,你必须用%20分隔它:

    /p:CaOptions=/analyze:log:format:sarif%20/analyze:log:compilerwarnings

    但:如果我在我的 Visual Studio 项目中激活 Clang-Tidy,我会收到错误 CLANGTIDY : error : no such file or directory: '/analyze:log' [clang-diagnostic-error]CLANGTIDY : error : unable to handle compilation, expected exactly one compiler job in ... - 有人对此有所了解(禁用 Clang-Tidy 除外)?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多