TeamCity 和 cppcheck 输出模板

TeamCity and cppcheck output template

我 运行 在 TeamCity 中对我的代码进行 cppcheck,并希望将其错误报告为构建问题。所以我把cppcheck的输出格式改成了

"##teamcity[buildProblem\tdescription='{file}({line}):\t{severity}:\t{message}']"

总体思路还可以。但问题是某些消息包含字符 ',这会导致解析输出时出错,因为 TeamCity 需要转义撇号。例如,这是我的构建日志的摘录:

[17:20:05][Step 2/2] Checking ..\..\..\services_package\Services\FaultsManager\FaultsManager.c...
[17:20:14][Step 2/2] ##teamcity[buildProblem    description='..\..\..\services_package\Services\FaultsManager\FaultsManager.c(83):  style:  The scope of the variable 'channelID' can be reduced.']
[17:20:15]
[Step 2/2] Property value not found
Valid property list format is (name( )*=( )*'escaped_value'( )*)* where escape symbol is "|"
[17:20:14][Step 2/2] ##teamcity[buildProblem description='..\..\..\services_package\Services\FaultsManager\CommonDef.h(32): warning:    Redundant code: Found a statement that begins with numeric constant.']

报告了第二个错误,但没有报告第一个错误。我认为这是因为第一个包含 'channelID' 并且这混淆了解析器。

如何让 TeamCity 很好地显示错误消息?显然,如果分析失败,我可以让它使构建失败,但我希望概述页面显示有意义的数据——错误数量、新错误数量、错误列表等(类似于失败的测试)。

我最终写了一个脚本,它 post- 处理输出:http://blogs.microsoft.co.il/dinazil/2015/08/31/more-on-code-quality/

还有另一种可能性,我在 my blog post in Russian. You can use Teamcity XML report processing 及其所有相关功能("Code Inspection" 选项卡、指标、自动更改跟踪)中写过,但您需要转换 cppcheck 的 XML 输出为 Teamcity 可以理解的内容。为此我使用了 XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes" method="xml"></xsl:output>
<xsl:template match="/">
  <checkstyle>
    <xsl:attribute name="version">
      <xsl:value-of select="results/cppcheck[@version]"/>
    </xsl:attribute>
    <xsl:for-each-group select="results/errors/error" group-by="(location/@file)[1]">
      <file>
        <xsl:attribute name="name">
          <xsl:value-of select="current-grouping-key()"/>
        </xsl:attribute>
        <xsl:for-each select="current-group()">
          <error line="{(location/@line)[1]}" message="{@msg}" severity="{@severity}" source="{@id}" />
        </xsl:for-each>
      </file>
    </xsl:for-each-group>
  </checkstyle>
</xsl:template>
</xsl:stylesheet>

生成 Checkstyle XML。它是这样使用的:

$ cppcheck --enable=all --xml --xml-version=2 . 2>cppcheck-report.xml
$ saxonb-xslt cppcheck-report.xml cppcheck-2-to-checkstyle.xslt >cppcheck-checkstyle.xml

处理结果的 Teamcity 配置 XML:

以这种方式集成 cppcheck 有一些优势(除了 "Code Inspection" 选项卡,它的界面比普通日志行更好),因为您可以更好地控制构建行为。使用 "buildProblem" 你的构建失败了,当你处理一个严格控制的小型干净代码库时,这可能没问题,但当你处理某种拥有数千个的巨大遗留代码库时,这肯定不是警告(修复所有警告太昂贵,而且 cppcheck 也有误报)。它允许您制定一些规则,例如 "warning/error counters should not increase" 并轻松捕获新问题,例如:

请注意,它使用 "latest successful build" 作为参考点,因此如果有人修复了一些旧问题,您将获得新的(较低的)参考计数,后续构建将与之进行比较。