OCLint 静态代码分析
环境
Homebrew终端安装/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"oclint安装brew install oclintxcpretty安装gem install xcpretty
脚本 关键点
- 明确项目是否依赖
CocoaPods - 使用
xcodebuild -list, 明确schemetargetconfiguration
xcodebuild cleanxcodebuild -target xxx -scheme xxx或者xcodebuild -workspace xxx -scheme xxxxcodebuild |xcpretty -r json-compilation-database
在build/reports中得到文件compilation_db.json

-
将
compilation_db.json移到与sh脚本同路径下
cp ./build/reports/compilation_db.json ./compile_commands.json -
oclint-json-compilation-database -- -report-type html -o oclint_result.html生成oclint_result.html文件 -
使用
-e忽略不需要分析的目录, 比如CocoaPos生成的第三方库
Pods文件, 命令-e Pods -
oclint分析错误报”too many errors emitted, stopping now”是因为项目中错误太多, 超过了oclint的默认上限, 所以oclint停止了工作.
设置-max-priority-1 -max-priority-2 -max-priority-3的值, 提高上限,
例:oclint-json-compilation-database -- -max-priority-1 10000 -max-priority-2 10000 -max-priority-3 10000 -rc LONG_LINE=150 -report-type pmd -o oclint.xml
-
oclint升级到0.11版本以上 -
注意脚本的执行环境路径配置
~/.bash_profile或者/etc/profile
脚本的全文
#! /bin/sh
#或者source ~/.bash_profile
source /etc/profile
if which oclint 2>/dev/null; then
echo 'oclint exist'
else
brew tap oclint/formulae
brew install oclint
fi
if which xcpretty 2>/dev/null; then
echo 'xcpretty exist'
else
gem install xcpretty
fi
# workspace 和 target 只能使用一种.
#myworkspace=XXX.xcworkspace # 替换workspace的名字
#myscheme=XXX # 替换scheme的名字
targetname=HttpManager
myscheme=HttpManager
xcodebuild -target $targetname -scheme $myscheme clean&&
xcodebuild -target $targetname -scheme $myscheme \
-configuration Debug \
| xcpretty -r json-compilation-database
cp ./build/reports/compilation_db.json ./compile_commands.json
if [ -f ./compile_commands.json ]; then echo "compile_commands.json 文件存在";
else echo "-----compile_commands.json文件不存在-----"; fi
oclint-json-compilation-database -- \
-report-type html -o oclint_result.html \
-rc LONG_LINE=200 \
-rc=NCSS_METHOD=100 \
-max-priority-1=100000 \
-max-priority-2=100000 \
-max-priority-3=100000; \
rm compile_commands.json;
if [ -f ./oclint_result.html ]; then echo '-----分析完毕-----'
else echo "-----分析失败-----"; fi
结果

- 也可以 设置 结果为
-report-type xcode


#! /bin/sh
#或者source ~/.bash_profile
source /etc/profile
cd ${SRCROOT}
if which oclint 2>/dev/null; then
echo 'oclint exist'
else
brew tap oclint/formulae
brew install oclint
fi
if which xcpretty 2>/dev/null; then
echo 'xcpretty exist'
else
gem install xcpretty
fi
xcodebuild clean&&
xcodebuild | xcpretty -r json-compilation-database
cp ./build/reports/compilation_db.json ./compile_commands.json
if [ -f ./compile_commands.json ]; then echo "compile_commands.json 文件存在";
else echo "-----compile_commands.json文件不存在-----"; fi
oclint-json-compilation-database -- \
-report-type xcode \
-rc LONG_LINE=200 \
-rc=NCSS_METHOD=100 \
-max-priority-1=100000 \
-max-priority-2=100000 \
-max-priority-3=100000; \
rm compile_commands.json;
问题
- 随意创建的项目测试没有问题, 不过我公司的项目测试的时候,发现使用
-report-type xcode法忽略某些第三方库-e [filePath]也没用作用. 但是-report-type html却可以.
Discussion