使用OClint进行iOS项目的静态代码扫描
2016-03-03 12:03
495 查看
使用OClint进行iOS项目的静态代码扫描
原文链接:http://blog.yourtion.com/static-code-analysis-ios-using-oclint.html最近需要一个静态分析代码工具,帮助我们发布运行应用前找到代码潜在的问题。
其实对于iOS开发,我们的日常开发上已经用到了这样一个静态分析的工具,那就是
Clang,
Clang是支持
C、
C++、
Objective-C和
Swift的一个前端编译工具,他将
OC或者
Swift的代码输出抽象语法树(Abstract Syntax Tree),然后编译成 LLVM 的 bitcode,最后由 LLVM 编译成 machine code。这个工具支撑着我们日常的开发和调试。
OCLint就是一个建立在
Clang上的工具,能够发现代码中潜在的问题。具体的功能请见官方文档,这里主要介绍的是其安装与使用。
安装软件
XCtool
brew install xctools
OClint
brew tap oclint/formulae brew install oclint
测试
进入项目目录(以SuperLogger为例),
下载项目切换到有问题的位置(),并进入
SuperLoggerDemo目录:
git clone https://github.com/yourtion/SuperLogger.git cd SuperLogger git checkout 0e64637459996ed91e0dd15718efb5d7200a9971 cd SuperLoggerDemo
测试执行:
# Cleanup before building rm -f compile_commands.json xctool -project SuperLoggerDemo.xcodeproj -scheme SuperLoggerDemo clean # Build Project xctool build \ -project SuperLoggerDemo.xcodeproj -scheme SuperLoggerDemo \ -reporter json-compilation-database:compile_commands.json # Analyze Project oclint-json-compilation-database -e Pods -- \ -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 | sed 's/\(.*\.\m\{1,2\}:[0-9]*:[0-9]*:\)/\1 warning:/' # Final cleanup rm -f compile_commands.json
输出
OCLint Report Summary: TotalFiles=14 FilesWithViolations=4 P1=0 P2=2 P3=6 SuperLoggerPreviewView.m:77:37: warning: replace with container literal [migration|P3] SuperLogerListView.m:206:37: warning: empty catch statement [empty|P2] SuperLogerListView.m:25:15: warning: empty if statement [empty|P2] SuperLogerListView.m:119:1: warning: long method [size|P3] Method with 92 lines exceeds limit of 50 SuperLogerListView.m:171:41: warning: replace with container literal [migration|P3] SuperLogerListView.m:110:21: warning: replace with object subscripting [migration|P3] SuperLogger.m:60:30: warning: replace with object subscripting [migration|P3] SuperLogger.m:108:31: warning: replace with object subscripting [migration|P3] [OCLint (http://oclint.org) v0.10.2]
具体问题可以参考:http://docs.oclint.org/en/stable/rules/index.html
修复后
OCLint Report Summary: TotalFiles=14 FilesWithViolations=0 P1=0 P2=0 P3=0 [OCLint (http://oclint.org) v0.10.2]
大概的功能就是这样,使用过程有什么问题欢迎大家一起交流
原文链接:http://blog.yourtion.com/static-code-analysis-ios-using-oclint.html
相关文章推荐
- iOS Gif图片展示N种方式(原生+第三方)
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS开发 之 可穿戴设备 蓝牙4.0 BLE 开发
- 仿ios弹出层提示框
- IOS中的几种比较常见的延时方法的介绍及各自优缺点
- iOS 设备获取唯一标识符汇总
- IOS如何在Host App 与 App Extension 之间发送通知
- IOS 实现不定参数方法
- iOS关闭键盘的五种方法(总结的很好的博文)
- iOS-lazyLoading懒加载
- iOS在语音时处理中断
- iOS didReceiveMemoryWarning 的处理
- iOS开发学习_OC基础_(一).类、对象和方法(1)
- iOS- "unacceptable content-type: text/plain"等content-type bug解决方案
- iOS开发--根据label内容动态设定label大小
- iOS拨打电话方法(3种)
- IOS多线程之NSThread、NSOperation、GCD
- Github Star集(iOS)-(持续更新)
- iOS 多线程基础
- iOS 获取设备唯一标示符