禁用Code Analysis输出MSBuild

5

我正在使用构建定义来构建项目。执行时还会进行代码分析。代码分析会输出各种文件,包括:

ConsoleApplication2.exe.CodeAnalysisLog.xml
ConsoleApplication2.exe.lastcodeanalysissucceeded

是否有办法通过参数或类似的方式在我的构建定义中禁用这个输出?

3个回答

4

可以使用CodeAnalysisGenerateSuccessFile选项禁用成功标记文件的生成。 例如:

<CodeAnalysisGenerateSuccessFile>false</CodeAnalysisGenerateSuccessFile>

没有选项可以防止生成日志文件,但您可以通过 CodeAnalysisLogFile 选项将其移动到其他位置。例如,要将其放置在项目根文件夹中,可以使用以下命令:

<CodeAnalysisLogFile>CodeAnalysisLog.xml</CodeAnalysisLogFile>

2
你把这个 XML 标签 <CodeAnalysisGenerateSuccessFile> 放在哪里了?如果我把它放在主 *.vcxproj 文件中的 <Project ...> 下面,那么它就无法正常工作,并且在尝试重新加载项目时会生成一个错误。 - Code Doggo

0

我已经找到了一个关于lastcodeanalysissucceeded文件的选项。

通过提供输入参数:/p:CodeAnalysisGenerateSuccessFile=false,该文件不会被生成。

我还没有找到一个禁用日志文件输出的输入参数。下面所述的模式设置并未涵盖它。

<xs:element name="CodeAnalysisInputAssembly" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisInputAssembly" _locComment="" -->Path to the assembly to be analyzed by Code Analysis. The default is '$(OutDir)$(TargetName)$(TargetExt)'.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisLogFile" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisLogFile" _locComment="" -->Path to the output file for the Code Analysis report. The default is '$(CodeAnalysisInputAssembly).CodeAnalysisLog.xml'.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisLogFileXsl" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisLogFileXsl" _locComment="" -->Path to the XSL style sheet to reference in the Code Analysis output report. This report is specified in $(CodeAnalysisLogFile). The default is an empty string ('').</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisModuleSuppressionsFile" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisModuleSuppressionsFile" _locComment="" -->Name of the file, without the path, where Code Analysis project-level suppressions are stored. The default is 'GlobalSuppressions$(DefaultLanguageSourceExtension)'.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisOverrideRuleVisibilities" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisOverrideRuleVisibilities" _locComment="" -->Indicates whether to run all overridable Code Analysis rules against all targets. This will cause specific rules, such as those within the Design and Naming categories, to run against both public and internal APIs, instead of only public APIs. The default is false.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisOutputToConsole" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisOutputToConsole" _locComment="" -->Indicates whether to output Code Analysis warnings and errors to the console. The default is false.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisVerbose" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisVerbose" _locComment="" -->Indicates whether to output verbose Code Analysis diagnostic info to the console. The default is false.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisPath" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisPath" _locComment="" -->Path to the Code Analysis installation folder. The default is '$(VSINSTALLDIR)\Team Tools\Static Analysis Tools\FxCop'.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisPlatformPath" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisPlatformPath" _locComment="" -->Path to the .NET Framework folder that contains platform assemblies, such as mscorlib.dll and System.dll. The default is an empty string ('').</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisProject" type="msb:StringPropertyType"  substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisProject" _locComment="" -->Path to the Code Analysis project (*.fxcop) to load. The default is an empty string ('').</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisQuiet" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisQuiet" _locComment="" -->Indicates whether to suppress all Code Analysis console output other than errors and warnings. This applies when $(CodeAnalysisOutputToConsole) is true. The default is false.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisRuleAssemblies" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisRuleAssemblies" _locComment="" -->Semicolon-separated list of paths either to Code Analysis rule assemblies or to folders that contain Code Analysis rule assemblies. The paths are in the form '[+|-][!][file|folder]', where '+' enables all rules in rule assembly, '-' disables all rules in rule assembly, and '!' causes all rules in rule assembly to be treated as errors. For example '+D:\Projects\Rules\NamingRules.dll;+!D:\Projects\Rules\SecurityRules.dll'. The default is '$(CodeAnalysisPath)\Rules'.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisRuleDirectories" type="msb:StringPropertyType" substitutionGroup="msb:Property">
     <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisRuleDirectories" _locComment="" -->Semicolon-separated list of directories in which to search for rules when resolving a rule set. The default is '$(CodeAnalysisPath)\Rules' unless the CodeAnalysisIgnoreBuiltInRules property is set to true.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisRules" type="msb:StringPropertyType" substitutionGroup="msb:Property">
     <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisRules" _locComment="" -->Semicolon-separated list of Code Analysis rules. The rules are in the form '[+|-][!]Category#CheckId', where '+' enables the rule, '-' disables the rule, and '!' causes the rule to be treated as an error. For example, '-Microsoft.Naming#CA1700;+!Microsoft.Naming#CA1701'. The default is an empty string ('') which enables all rules.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisRuleSet" type="msb:StringPropertyType" substitutionGroup="msb:Property">
     <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisRuleSet" _locComment="" -->A .ruleset file which contains a list of rules to run during analysis. The string can be a full path, a path relative to the project file, or a file name. If a file name is specified, the CodeAnalysisRuleSetDirectories property will be searched to find the file. The default is an empty string ('').</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisRuleSetDirectories" type="msb:StringPropertyType" substitutionGroup="msb:Property">
     <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisRuleSetDirectories" _locComment="" -->Semicolon-separated list of directories in which to search for rule sets. The default is '$(VSINSTALLDIR)\Team Tools\Static Analysis Tools\Rule Sets' unless the CodeAnalysisIgnoreBuiltInRuleSets property is set to true.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisSaveMessagesToReport" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisSaveMessagesToReport" _locComment="" -->Comma-separated list of the type ('Active', 'Excluded', or 'Absent') of warnings and errors to save to the output report file. The default is 'Active'.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisSearchGlobalAssemblyCache" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisSearchGlobalAssemblyCache" _locComment="" -->Indicates whether Code Analysis should search the Global Assembly Cache (GAC) for missing references that are encountered during analysis. The default is true.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisSummary" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisSummary" _locComment="" -->Indicates whether to output a Code Analysis summary to the console after analysis. The default is false.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisTimeout" type="msb:StringPropertyType" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisTimeout" _locComment="" -->The time, in seconds, that Code Analysis should wait for analysis of a single item to complete before it aborts analysis. Specify 0 to cause Code Analysis to wait indefinitely. The default is 120.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisTreatWarningsAsErrors" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisTreatWarningsAsErrors" _locComment="" -->Indicates whether to treat all Code Analysis warnings as errors. The default is false.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisUpdateProject" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisUpdateProject" _locComment="" -->Indicates whether to update the Code Analysis project (*.fxcop) specified in $(CodeAnalysisProject). This applies when there are changes during analysis. The default is false.</xs:documentation>
    </xs:annotation>
</xs:element>
<xs:element name="CodeAnalysisUseTypeNameInSuppression" type="msb:boolean" substitutionGroup="msb:Property">
    <xs:annotation>
        <xs:documentation><!-- _locID_text="CodeAnalysisUseTypeNameInSuppression" _locComment="" -->Indicates whether to include the name of the rule when Code Analysis emits a suppression. The default is true.</xs:documentation>
    </xs:annotation>

你在哪里提供了p:CodeAnalysisGenerateSuccessFile=false标志?右击我的 Visual C++ 项目并转到“属性->C/C++->命令行”,将此标志放在“附加选项”中并不能停止其生成。 - Code Doggo

0
我尝试了Matthijs和Nicole Calinoiu提供的两个答案,但在进一步调查后,我偶然发现了StackOverflow上的this question
简而言之,通常不建议禁用这些文件的输出。根据上述链接中seva titov的回答:
“首先,删除.lastcodeanalysissucceededd将导致即使没有更改也会重新运行代码分析。其次,删除.CodeAnalysisLog.xml将使得几乎不可能调查分析错误和警告的详细信息。因此,最好只是将这两个文件移动到中间文件位置。这个位置是所有*.obj文件、*.log文件和其他元文件存在的地方。要做到这一点,请右键单击您的项目,然后单击“卸载项目”。”

unload project

然后,右键单击项目并单击编辑p͟r͟o͟j͟e͟c͟t͟.vcxproj。在图片中,我点击编辑Test1.vcxproj,因为我用于演示的项目名为Test1

edit vcxproj file

然后,在项目标签</Project>的末尾,您会看到一堆<Import...>语句。在这些导入语句之后放置该问题中描述的属性组即可。
<PropertyGroup>
    <CodeAnalysisLogFile>$(IntermediateOutputPath)$(TargetFileName).CodeAnalysisLog.xml</CodeAnalysisLogFile>
    <CodeAnalysisSucceededFile>$(IntermediateOutputPath)$(TargetFileName).lastcodeanalysissucceeded</CodeAnalysisSucceededFile>
</PropertyGroup>

例如,它应该看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
    <!-- 
        ...
        A lot of other stuff not shown
        ...
    -->
    
    <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
    <ImportGroup Label="ExtensionTargets">
    </ImportGroup>

    <!-- The following will move the code analysis output files and put them in the intermediate folder -->
    <PropertyGroup>
        <CodeAnalysisLogFile>$(IntermediateOutputPath)$(TargetFileName).CodeAnalysisLog.xml</CodeAnalysisLogFile>
        <CodeAnalysisSucceededFile>$(IntermediateOutputPath)$(TargetFileName).lastcodeanalysissucceeded</CodeAnalysisSucceededFile>
    </PropertyGroup>
</Project>

(注意:请将<CodeAnalysisSucceededFile><CodeAnalysisLogFile>标签的内容保持在单行上,如上所示。如果您跨越几行放置内容,Visual Studio 2019会抛出“路径中存在非法字符”的错误。)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接