使用OCLint静态分析iOS项目代码
关于OCLint C
OCLint是一个静态代码分析工具,通过检查C、C++和Objective - C代码,提高质量和减少缺陷,寻找潜在的问题,可能的错误,未使用的代码,复杂的代码,冗余代码,代码味道,不好的实践等等。
安装OCLint
XCTool
brew install xctoolOCLint
brew tap oclint/formulae brew install oclint
集成Xcode
- 项目中添加target,Other->Aggregate,名字随便起,例如
OCLint 选中这个target,切换到Build Phases,添加一个Run Script.
这里贴下目前使用的脚本if [ -f ~/.bash_profile ]; then source ~/.bash_profile fi hash oclint &> /dev/null if [ $? -eq 1 ]; then echo >&2 "oclint not found, analyzing stopped" exit 1 fi if [ ! -f "${TARGET_TEMP_DIR}"/xcodebuild.log ]; then echo "[*] xcodebuild.log not found, possibly clean was performed" echo "[*] starting xcodebuild to rebuild the project.." cd "${SRCROOT}" xcodebuild clean -configuration "${CONFIGURATION}" -project "${PROJECT_NAME}".xcodeproj #build xcodebuild.log xcodebuild -configuration "${CONFIGURATION}" -project "${PROJECT_NAME}".xcodeproj | tee "${TARGET_TEMP_DIR}"/xcodebuild.log fi cd "${TARGET_TEMP_DIR}" echo "[*] transforming xcodebuild.log into compile_commands.json..." #transform it into compile_commands.json oclint-xcodebuild echo "[*] starting analyzing" oclint-json-compilation-database -e Pods -- -p "${TARGET_TEMP_DIR}" \ -max-priority-1=100000 \ -max-priority-2=100000 \ -max-priority-3=100000 \ -disable-rule=InvertedLogic \ -disable-rule=CollapsibleIfStatements \ -disable-rule=UnusedMethodParameter \ -disable-rule=LongLine \ -disable-rule=LongVariableName \ -disable-rule=ShortVariableName \ -disable-rule=UselessParentheses \ -disable-rule=IvarAssignmentOutsideAccessorsOrInit \ -disable-rule=LongMethod \ -disable-rule=CyclomaticComplexity \ -disable-rule=TooManyParameters \ -disable-rule=PreferEarlyExit | sed -E 's/(.*\.(c|m{1,2}):[0-9]*:[0-9]*:)/\1 warning:/'具体规则可查看官方文档
直接build这个target,警告信息就是静态分析后的结果.如果项目较大,分析时间会很长.