前言:从长远来看,代码检查对于个人成长及公司发展大有裨益。本文主要介绍 Objective-C 项目代码检查,如有谬误欢迎指正👏🏻
1. Objective-C 项目代码检查方式
- Xcode Analyze
- OCLint
Xcode Analyze 本文不做介绍,默认快捷键
shift+cmd+B运行分析即可。以下介绍 OCLint 执行代码检查。
2. OCLint 简介
OCLint 官网地址:oclint.org
OCLint 仓库地址:oclint
A static source code analysis tool to improve quality and reduce defects for C, C++ and Objective-C
一个用于C、C++ 和 Objective-C 语言提升质量、减少缺陷的代码静态检查工具。
3. OCLint 代码检查
3.1. OCLint 安装
OCLint 三种安装方式:Homebrew、安装包、源码编译。
3.1.1. Homebrew 安装
前置条件:Homebrew
3.1.1.1. 更新 Homebrew 自身
必须,确保通过 brew install oclint 安装的是最新版本,否则安装旧版本未适配 Xcode 13 导致最终无法正常生成报表。
brew update
3.1.1.2. 安装 OCLint
brew tap oclint/formulae
brew install oclint
3.1.1.3. 验证
oclint --version
正常输出:
OCLint (<https://oclint.org>):
OCLint version 22.02.
Built Feb 19 2022 (21:13:26).
3.1.2. 安装包安装
3.1.2.1. 下载安装包
进入 oclint 仓库,选择 Releases,即可看到所有已发布版本。目前最新版本 22.02,选择 macOS 版本:oclint-22.02-llvm-13.0.1-x86_64-darwin-macos-12.2-xcode-13.2.tar.gz。
3.1.2.2. 解压
下载完成后,解压到自己习惯的位置,以我放置的目录为例:
/Applications/OCLint/oclint-22.02
3.1.2.3. 更新环境变量PATH:
打开配置文件(编辑器或 vim 均可),配置文件路径(Intel 与 M1 一致,yourname 为自己电脑名称,.zshrc 和 .bash_profile 区别此处不赘述):
~/.zshrc 或 /Users/yourname/.zshrc 添加以下内容:
# OCLint(此处替换为自己的路径)
OCLINT=/Applications/OCLint/oclint-22.02
export PATH=$OCLINT/bin:$PATH
- 使配置文件生效:
source ~/.zshrc
或
source /Users/yourname/.zshrc
3.1.2.3. 验证
oclint --version
正常输出:
OCLint (<https://oclint.org>):
OCLint version 22.02.
Built Feb 19 2022 (21:13:26).
3.1.3. 源码编译安装
前置条件:Homebrew
3.1.3.1. 更新 Homebrew 自身
3.1.3.2. 安装 CMake 和 Ninja 编译工具
brew install cmake ninja
3.1.3.3. 下载 OCLint 源码
两种方式下载源码:
1). 进入 oclint 仓库,选择 Code-DownloadZIP,下载并解压;
2). git clone
git clone https://github.com/oclint/oclint.git
3.1.3.4. 编译 OCLint 源码
进入 oclint-scripts 目录,执行:
./make
1). 编译时间较长且必须翻墙,过程中会下载 oclint-json-compilation-database、oclint-xcodebuild、llvm 源码以及 clang 源码;
2). 编译结束后,build 目录下即编译完成的 OCLint;
3.1.3.5. 更新环境变量 PATH,同 3.1.2.3
打开配置文件:
~/.zshrc 添加以下内容:
# OCLint(此处替换为自己的路径)
OCLINT=/Applications/OCLint/oclint-22.02
export PATH=$OCLINT/bin:$PATH
配置文件生效:
source ~/.zshrc
3.1.3.6. 验证
oclint --version
正常输出:
OCLint (<https://oclint.org>):
OCLint version 22.02.
Built Feb 19 2022 (21:13:26).
3.2 XCPretty 安装
前置条件:Ruby Gem
执行:
gem install xcpretty
3.3 OCLint 基本使用
前置条件:Command Line Tools,一般 Xcode 自带
3.3.1. 获取项目信息(可选)
进入项目目录,执行
xcodebuild -list
输出:
Information about project "SViewIOS":
Targets:
SViewIOS
SViewTests
SViewUITests
ScreenCapture
Build Configurations:
Debug
Release
If no build configuration is specified and -scheme is not passed then "Release" is used.
Schemes:
ScreenCapture
SViewIOS
3.3.2. 编译项目
执行:
xcodebuild -scheme SViewIOS -workspace SViewIOS.xcworkspace clean && xcodebuild -scheme SViewIOS -workspace SViewIOS.xcworkspace -configuration Debug | xcpretty -r json-compilation-database -o compile_commands.json
以上实际执行了 3 步,clean、build、格式化,需要注意的是:
clean操作需要每次都要执行,否则报错,暂未找到原因和方法;- 工作空间项目(cocoapods 管理或手动创建
xcworkspace),必须添加-workspace xxx.xcworkspace; xcpretty主要用来生成格式化的json文件;
3.3.3. 生成 HTML
执行:
oclint-json-compilation-database -e Pods -e SViewIOS/Library/ -e SViewIOS/SDK/ -e iOSSView2D -e iOSSViewBase -- -report-type html -rc LONG_LINE=200 -max-priority-1=100000 -max-priority-2=100000 -max-priority-3=100000 >> SViewOCLintReport.html
参数说明:
- -e 排除项
- LONE_LINE:每行代码最大字符数
- -max-priority-x:各级别最大数量,设置大一些,否则可能会由于项目较大出现生成文件失败的情况 报错:
oclint: error: one compiler command contains multiple jobs
解决方法:
- 进入
Target-BuildSetting,搜索compiler_index,将Enable Index-While-Building Functionality设置为NO; - 打开
podfile文件,在target 'xxx' do之前添加:
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['COMPILER_INDEX_STORE_ENABLE'] = "NO"
end
end
end
报表生成时间较长,生成后打开对应 html 文件查看结果,根据 location 对应解决问题:
TODO
- 补充过程中遇到的问题
- 完善细节描述,如去除项目信息、部分名词或执行原因解释等等;
- 添加 Xcode 集成 OCLint 部分;
- 自定义检查规则