如何处理来自clang静态代码分析的警告作为Xcode 3中的错误? [英] How to treat warnings from clang static code analysis as errors in Xcode 3?

查看:364
本文介绍了如何处理来自clang静态代码分析的警告作为Xcode 3中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RUN_CLANG_STATIC_ANALYZER(运行静态分析器)项目设置在我们的项目中发现了重要问题。我们已经解决了这些问题,我们希望防止未来的问题蔓延。

The RUN_CLANG_STATIC_ANALYZER ("Run Static Analyzer") project setting has found important issues in our project. We have addressed them and we want to prevent future issues from creeping in.

我们正在尝试将clang分析警告信息改为
, a href =http://stackoverflow.com/questions/2400749/should-static-analysis-warnings-fail-the-ci-build>打破我们的构建。到目前为止,尽管已经启用了-Werror(处理警告作为错误),但没有成功。

We're trying to get clang analysis warnings to be treated as errors to break our build. So far no success despite having -Werror ("Treat Warnings as Errors") enabled.

在Xcode中生成以下分析调用:

The following analysis call generated within Xcode:


/ Developer / usr / bin / clang -x objective-c [...] -analyze [...] / TroubledCode.m -o [...] / TroubledTarget.build/StaticAnalyzer/normal/i386/TroubledCode.plist

/Developer/usr/bin/clang -x objective-c [...] --analyze [...]/TroubledCode.m -o [...]/TroubledTarget.build/StaticAnalyzer/normal/i386/TroubledCode.plist

产生静态代码分析警告:

produces a static code analysis warning:

[...]/TroubledCode.m:38:34: warning: Potential leak of an object allocated on line 38 and stored into 'leakingManager'
    Manager *leakingManager = [[Manager alloc] init];
                              ^
1 warning generated.

但Xcode报告Build Succeeded ... 1分析器结果。我们正在寻找的解决方案会使上面的示例生成构建失败。

but Xcode reports "Build Succeeded ... 1 analyzer result". The solution we're looking for would make the example above generate a "Build Failed".

我采用了Jim的建议并创建了一个构建脚本。

I took Jim's advice and created a build script.

为了避免误报,我经历了确保它忽略了外来分析残留物。此解决方案应该在从Xcode IDE构建时以及使用 xcodebuild 构建项目时工作。

To avoid false alarms, I went through the trouble of making sure it ignores extraneous analysis residue. This solution should work when building from the Xcode IDE and when building your project using xcodebuild.

要打开Xcode 3分析警告到构建错误:

To turn Xcode 3 analysis warnings into build errors:


  • 双击相关的项目或目标。

  • 建立标签,勾选[设定]> [连结]> [写入连结地图]档案下的方块。

该设定也称为

That setting is also known as LD_GENERATE_MAP_FILE.


  • 在组和文件>目标下,单击目标的公开三角

  • 右键点击链接二进制库与库阶段。

  • 选择添加> >新建运行脚本生成阶段

  • 可选:重命名刚刚添加到将诽谤警告视为错误的运行脚本阶段。


  • Under "Groups & Files" > "Targets", click the disclosure triangle of the target you'd like to add this feature to.
  • Right-click the "Link Binary With Libraries" phase.
  • select Add > New Build Phase > New Run Script Build Phase
  • optional: Rename the "Run Script" phase you just added to "Treat Clang Warnings as Errors".
  • Double-click the new script phase if it's not already open.

复制以下内容并将其粘贴到脚本部分。 / p>

Copy the content below and paste it into the "Script" section.

error_count=0

##

function verify_clang_analysis_at_path()
{
  local analysis_path=$1
  local plist_tool=/usr/libexec/PlistBuddy
  local diagnostics=$($plist_tool -c "print diagnostics" $analysis_path)

  if [[ $diagnostics != $'Array {\n}' ]]
  then
    ((error_count++))
  fi
}

function verify_clang_analysis_for_object_file()
{
  local object_file=$1
  local analysis_directory=$TARGET_TEMP_DIR/StaticAnalyzer/$CURRENT_VARIANT/$CURRENT_ARCH
  local analysis_path=$analysis_directory/${object_file%.*}.plist

  # if this object file corresponds to a source file that clang analyzed...
  if [ -e $analysis_path ]
  then
    verify_clang_analysis_at_path $analysis_path
  fi
}

##

object_directory=$OBJECT_FILE_DIR-$CURRENT_VARIANT/$CURRENT_ARCH
object_path_pattern=${object_directory}'/\(.\)\+\.o$'

index_pattern='\[[[:space:]0-9]*\][[:space:]]'

object_paths=$( 
  grep $object_path_pattern $LD_MAP_FILE_PATH | sed s/$index_pattern//
)

##

for object_path in $object_paths 
do
  object_file=${object_path##*/}
  verify_clang_analysis_for_object_file $object_file
done

if [ $error_count -gt 0 ]
then
   echo "Clang static code analysis failed for" $error_count "source file(s)."
fi

exit $error_count



更新



Mike Vosseller

Update

Mike Vosseller has an upgraded version of this script for Xcode 5.

推荐答案

我们一直计划单独制作一个构建步骤以运行分析器并检查结果。我们将失败的构建服务器上的那种方式。这不会帮助你在本地,虽然。

We've been planning on having a separate build step to run the analyzer and check the results. We'll fail the build on the build server that way. That won't help you locally, though.

这篇关于如何处理来自clang静态代码分析的警告作为Xcode 3中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆