代码静态分析工具Infer实践

Infer是一款由Facebook开源的代码静态分析工具,支持Java、Objective-C、C和C++,能检测出null pointer exceptions、resource leaks等问题。本文介绍了Infer的安装、工作流、实例操作以及如何自定义检验模型,帮助提升代码质量和早期发现潜在问题。
摘要由CSDN通过智能技术生成

0.背景

静态代码分析可以提高代码质量和尽早的发现bugs,减少后期排查问题的时间。

Infer是facebook开源的一款代码静态分析工具,现支持的语言有Java、Objective-C、C和C++; 对Android和Java代码可以发现null pointer exceptions和resource leaks等;对iOS、C和C++代码可以发现memory leak等。

谁在使用,facebook、instagram、UBER、WhatsApp等等;

在facebook内部,由2个小团队构建了这个静态分析工具,支持了上千名工程师和百万行级代码。

本文将介绍Infer的使用。

1. 安装Infer

infer只支持Mac和Linux系统

1-1. docker方式

[root@localhost infer_docker]# curl -sSO  https://raw.githubusercontent.com/facebook/infer/master/docker/Dockerfile

[root@localhost infer_docker]# curl -sSO https://raw.githubusercontent.com/facebook/infer/master/docker/run.sh

sh run.sh

1-2. Mac

hugangdeMacBook-Pro:~ hugang$ brew update
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- mach (LoadError)

解决办法(重装brew):
hugangdeMacBook-Pro:~ hugang#  ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

hugangdeMacBook-Pro:~ hugang# ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"


hugangdeMacBook-Pro:~ hugang$ brew install infer

1-3. Linux下release版本

infer的依赖:https://github.com/facebook/infer/blob/master/INSTALL.md#pre-compiled-versions
Infer dependencies for Linux

Here are the prerequisites to be able to compile Infer on Linux. This is required to be able to use the release (faster), or to compile everything from source (see the end of this document).

opam >= 1.2.0
Python 2.7
Java (only needed for the Java analysis)
gcc >= 4.7.2 or clang >= 3.1 (only needed for the C/Objective-C analysis)
autoconf >= 2.63 and automake >= 1.11.1 (if building from git)
其中依赖opam(OPAM is a source-based package manager for OCaml)安装如下:

http://opam.ocaml.org/doc/Install.html#FedoraCentOSandRHEL

http://software.opensuse.org/download.html?project=home%3Aocaml&package=opam

对于 CentOS 7,请以 根用户 root 运行下面命令:

cd /etc/yum.repos.d/
wget http://download.opensuse.org/repositories/home:ocaml/CentOS_7/home:ocaml.repo
yum install opam
对于 CentOS 6,请以 根用户 root 运行下面命令:

cd /etc/yum.repos.d/
wget http://download.opensuse.org/repositories/home:ocaml/CentOS_6/home:ocaml.repo
yum install opam

依赖解决后,下载infer的release版本:
https://github.com/facebook/infer/releases/tag/v0.9.4.1

wget https://github.com/facebook/infer/rele
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值