如何将克朗静态代码分析的警告视为Xcode 3中的错误?
-
14-11-2019 - |
题
问题
run_clang_static_Analyzer(“运行静态分析仪”)项目设置在我们的项目中找到了重要问题。我们已经解决了他们,我们希望防止未来的问题蔓延。
我们试图让Clang分析警告 被视为休息我们的build 。到目前为止没有成功,尽管有 - 驱动器(“将警告视为错误”)启用。
问题的示例
在Xcode中生成的以下分析呼叫:
/ developer / usr / bin / clang-x目标-c [...] / trooubledcode.plist
生成静态代码分析警告:
.[...]/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报告“构建成功... 1分析仪结果”。我们正在寻找的解决方案将使上面的示例生成“构建失败”。
解决方案
我采取了吉姆的建议并创建了一个构建脚本。 为了避免误报,我经历了确保它忽略外来分析残留物的麻烦。此解决方案应在从Xcode IDE中构建以及使用
xcodebuild
构建项目时工作。将Xcode 3分析警告转换为构建错误:
- 双击项目或目标的目标。
- 在“构建”选项卡下,选中“设置”下的“设置”>“链接”>“写入链接映射文件”
该设置也称为
LD_GENERATE_MAP_FILE
。
- 在“组和文件”>“目标”下,单击您想要将此功能添加到的目标的披露三角形。
- 右键单击“带有库的链接二进制”阶段。
- 选择添加>新建阶段>新运行脚本构建阶段
- 可选:重命名您刚刚添加到“将clang警告视为错误”的“运行脚本”阶段。
- 如果尚未打开,请双击新脚本阶段。
复制下面的内容并将其粘贴到“脚本”部分中。
.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
更新
解决方案
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.
其他提示
Xcode has a "Treat warnings as errors" checkbox. If you can't find it, which has been said for some iPhone projects, just add "-Werror" to your WARNING_CFLAGS in the Build Settings for your target(s) in question.