随着业务的开发迭代速度越来越快,完全依赖人工保证工程质量显然显得力不从心,所以公司本着开发效率为前提,我开始学习了静态分析,它可以帮助我们在编码的阶段就可以及时发现代码错误,从而在根源上保证工程整体质量,也是我们iOS开发者最常用的一种代码调试技术.
Xcode自带的静态分析工具Analyze,通过静态语法分析能够找出在代码层面就能发现的内存泄露问题,还可以通过上下文分析出是否存在无用变量等问题.但是Analyze的功能还是有限,还是无法帮助我们在编写代码的阶段发现更多的问题.所以我们这里主要介绍一下我用的Infer静态分析器.
Infer是Facebook开源的使用OCaml语言编写的静态分析工具,可以对C,java和Objective-C代码进行静态分析,可以检查出空指针访问,资源泄露以及内存泄露.
安装
安装很简单 直接在终端输入:
brew install infer
接下来我们看一个事例
#import <Foundation/Foundation.h>
@interface Hello: NSObject
@property NSString* s;
@end
@implementation Hello
NSString* m() {
Hello* hello = nil;
return hello->_s;
}
@end
在终端输入
infer -- clang -c hello.m
运行结果如下
Capturing in make/cc mode...
Found 1 source file to analyze in /Users/ming/Downloads/jikeshijian/infer-out
Starting analysis...
legend:
"F" analyzing a file
"." analyzing a procedure
F.
*Found 5 issues*
hello.m:10: error: NULL_DEREFERENCE
pointer `hello` last assigned on line 9 could be null and is dereferenced at line 10, column 12.
8. NSString* m() {
9. Hello* hello = nil;
10. *>* return hello->_s;
11. }
hello.m:10: warning: DIRECT_ATOMIC_PROPERTY_ACCESS
Direct access to ivar `_s` of an atomic property at line 10, column 12. Accessing an ivar of an atomic property makes the property nonatomic.
8. NSString* m() {
9. Hello* hello = nil;
10. *>* return hello->_s;
11. }
hello.m:4: warning: ASSIGN_POINTER_WARNING
Property `s` is a pointer type marked with the `assign` attribute at line 4, column 1. Use a different attribute like `strong` or `weak`.
2.
3. @interface Hello: NSObject
4. *>*@property NSString* s;
5. @end
6.
hello.m:10: warning: DIRECT_ATOMIC_PROPERTY_ACCESS
Direct access to ivar `_s` of an atomic property at line 10, column 12. Accessing an ivar of an atomic property makes the property nonatomic.
8. NSString* m() {
9. Hello* hello = nil;
10. *>* return hello->_s;
11. }
hello.m:4: warning: ASSIGN_POINTER_WARNING
Property `s` is a pointer type marked with the `assign` attribute at line 4, column 1. Use a different attribute like `strong` or `weak`.
2.
3. @interface Hello: NSObject
4. *>*@property NSString* s;
5. @end
6.
*Summary of the reports*
DIRECT_ATOMIC_PROPERTY_ACCESS: 2
ASSIGN_POINTER_WARNING: 2
NULL_DEREF
可以看出,我们前面的hello.m代码里一共有误个问题,其中一个包括一个错误四个警告.第一个错误如下:
hello.m:10: error: NULL_DEREFERENCE
pointer `hello` last assigned on line 9 could be null and is dereferenced at line 10, column 12.
8. NSString* m() {
9. Hello* hello = nil;
10. *>* return hello->_s;
11. }
这个错误信息意思是,hello可能为空,需要去掉第10行12列的引用.我把这段代码修改下,去掉引用:
return hello.s;
在运行一遍infer命令:
infer -- clang -c hello.m
然后就发现只剩下一个警告了
hello.m:4: warning: ASSIGN_POINTER_WARNING
Property `s` is a pointer type marked with the `assign` attribute at line 4, column 1. Use a different attribute like `strong` or `weak`.
2.
3. @interface Hello: NSObject
4. *>*@property NSString* s;
5. @end
这个警告意思是说,属性s是指针类型,需要使用strong或者weak修饰,这是我们将s的属性改为strong再次运行infer,会发现没有问题了,运行结果如下:
Capturing in make/cc mode...
Found 1 source file to analyze in /Users/ming/Downloads/jikeshijian/infer-out
Starting analysis...
legend:
"F" analyzing a file
"." analyzing a procedure
F.
*No issues found
Infer就是这么的强大!很容易帮我们在编码过程中避免一些不易发觉的问题,我自己才安装这个工具,来这里做个笔记