Hello, World!
Follow the instructions in this page to try Infer on a few small examples. You should be able to see the bugs reported by Infer, fix the bugs and run Infer again to check that they are not reported anymore. This should give you a first idea of how Infer works. See the User Guide for more details about the use of Infer.
All these examples can be found in the
infer/examples
directory distributed with Infer.
- Hello world Java
- Hello world Objective-C
- Hello world C
- Hello world Android
- Hello world iOS
- Hello world Make
Hello world Java
Here is a simple Java example to illustrate Infer at work.
To run Infer, type the following in your terminal from the same directory as
Hello.java
.
You should see the following error reported by Infer.
Now edit the file to add null checks:
Run Infer again. This time we get no error: Infer reports No issues found
.
Hello world Objective-C
Here is a simple Objective-C example to illustrate Infer at work.
To run Infer, type the following in your terminal from the same directory as
Hello.m
.
You should see the following error reported by Infer.
Now edit the file to use the getter instead of accessing the instance variable:
Run Infer again. This time we get no error: Infer reports No issues found
.
Hello world C
Here is a simple C example to illustrate Infer at work.
To run Infer, type the following in your terminal from the same directory as
hello.c
.
You should see the following error reported by Infer.
Now edit the file to add null checks:
Run Infer again. This time we get no error: Infer reports No issues found
.
When analyzing C files, Infer captures the gcc command and runs clang instead to parse them. Thus you may get compiler errors and warnings that differ from gcc's. So in particular, the following two commands are equivalent:
Hello world Android
To be able to analyze the sample Android app, make sure that you have the Android SDK 22 installed and up to date, as well as the "Android SDK Build-tools" and "Android Support Repository" components.
Go to the sample Android app in
infer/examples/android_hello
and create a local.properties
file with a single line
sdk.dir=<location of your Android SDK>
. This sample Android app uses
gradle as its build system. You do not need to install
gradle to try it out though, thanks to the
gradlew
script in the project that will download gradle and the project's dependencies
for you.
After editing local.properties
, run
Infer will output the list of found bugs:
Differential analysis
If you run Infer again without changing any files, you will notice that this time nothing gets analyzed. This is because gradle is incremental: everything was compiled already so nothing gets recompiled. Infer captures the compilation commands to know which files to analyze, hence it analyzes nothing in this case. There are three solutions to remedy this:
Run gradlew clean in between Infer runs.
./gradlew cleanThis causes gradle to recompile everything each time, and subsequently Infer to capture all the files again.
Run Infer indicating that the capture of compilation commands should continue, using option
--continue
(or-c
for short).infer run --continue -- ./gradlew build
This makes Infer add the effects of the new compilation commands to the previous ones, and start a new analysis of the entire code.
Run Infer in reactive mode after a code change, using option
--reactive
(or-r
for short).infer run --reactive -- ./gradlew build
This makes Infer analyze the effects of the code change, without re-analyzing everything. Note that only the modified files, and those dependent on them, are re-analyzed. This analysis mode can be significantly faster.
You can learn more about the particulars of each solution in the Infer workflow page.
Hello world iOS
Go to the sample iOS app in
infer/examples/ios_hello
and run Infer on it:
Infer will output the list of found bugs:
Similarly to the case of gradle,
running the command above a second time will yield no analysis results, as
nothing gets recompiled. Either add the --reactive
(or -r
) flag to the
infer
command:
or ask the build system to reinitialize the directory before running Infer again, using
Hello world Make
Go to the sample C project in
infer/examples/c_hello
and run Infer on it:
Infer will output the list of found bugs:
Similarly to the case of gradle,
running infer run -- make
a second time will yield no analysis results, as
nothing gets recompiled. Either add the --reactive
(or -r
) flag to the
infer
command:
or run
before analyzing the project again.